/** * Builds an instruction object from the contents of an instruction file. * * @param instructionFile A non-null instruction file retrieved from S3 that contains encryption * information * @param materialsProvider The non-null encryption materials provider to be used to encrypt and * decrypt data. * @param cryptoProvider The crypto provider whose encryption implementation will be used to * encrypt and decrypt data. Null is ok and uses the preferred provider from * Security.getProviders(). * @return A non-null instruction object containing encryption information * @deprecated no longer used and will be removed in the future */ @Deprecated public static EncryptionInstruction buildInstructionFromInstructionFile( S3Object instructionFile, EncryptionMaterialsProvider materialsProvider, Provider cryptoProvider) { JSONObject instructionJSON = parseJSONInstruction(instructionFile); try { // Get fields from instruction object String encryptedSymmetricKeyB64 = instructionJSON.getString(Headers.CRYPTO_KEY); String ivB64 = instructionJSON.getString(Headers.CRYPTO_IV); String materialsDescriptionString = instructionJSON.tryGetString(Headers.MATERIALS_DESCRIPTION); Map<String, String> materialsDescription = convertJSONToMap(materialsDescriptionString); // Decode from Base 64 to standard binary bytes byte[] encryptedSymmetricKey = Base64.decode(encryptedSymmetricKeyB64); byte[] iv = Base64.decode(ivB64); if (encryptedSymmetricKey == null || iv == null) { // If necessary encryption info was not found in the instruction file, throw an exception. throw new AmazonClientException( String.format( "Necessary encryption info not found in the instruction file '%s' in bucket '%s'", instructionFile.getKey(), instructionFile.getBucketName())); } EncryptionMaterials materials = retrieveOriginalMaterials(materialsDescription, materialsProvider); // If we're unable to retrieve the original encryption materials, we can't decrypt the object, // so // throw an exception. if (materials == null) { throw new AmazonClientException( String.format( "Unable to retrieve the encryption materials that originally " + "encrypted object corresponding to instruction file '%s' in bucket '%s'.", instructionFile.getKey(), instructionFile.getBucketName())); } // Decrypt the symmetric key and create the symmetric cipher SecretKey symmetricKey = getDecryptedSymmetricKey(encryptedSymmetricKey, materials, cryptoProvider); CipherFactory cipherFactory = new CipherFactory(symmetricKey, Cipher.DECRYPT_MODE, iv, cryptoProvider); return new EncryptionInstruction( materialsDescription, encryptedSymmetricKey, symmetricKey, cipherFactory); } catch (JSONException e) { throw new AmazonClientException( "Unable to parse retrieved instruction file : " + e.getMessage()); } }
/** * Converts the JSON encoded materials description to a Map<String, String> * * @deprecated no longer used and will be removed in the future */ @Deprecated @SuppressWarnings("unchecked") // Suppresses Iterator<String> type warning private static Map<String, String> convertJSONToMap(String descriptionJSONString) { if (descriptionJSONString == null) { return null; } try { JSONObject descriptionJSON = new JSONObject(descriptionJSONString); Iterator<String> keysIterator = descriptionJSON.keys(); Map<String, String> materialsDescription = new HashMap<String, String>(); while (keysIterator.hasNext()) { String key = keysIterator.next(); materialsDescription.put(key, descriptionJSON.getString(key)); } return materialsDescription; } catch (JSONException e) { throw new AmazonClientException( "Unable to parse encryption materials description from metadata :" + e.getMessage()); } }
private JSONObject getJsonFromString(String jsonStr) { JSONObject jo = null; boolean successJson = false; while (!successJson) { try { jo = new JSONObject(jsonStr); successJson = true; } catch (JSONException je) { String exceptionTypeToFix = "Duplicate key "; String duplicated; if (je.getMessage().contains(exceptionTypeToFix)) { System.out.println("WARNING : Duplicate in json "); duplicated = je.getMessage().replaceAll("\"", "").replace("Duplicate key ", ""); System.out.println("WARNING : Transversing Duplicated json TAG : " + duplicated); while (jsonStr.contains(duplicated)) { jsonStr = jsonStr.replaceFirst( duplicated, duplicated.toUpperCase() + System.currentTimeMillis()); } } else { System.out.println("no json"); return null; } continue; } } String[] names = JSONObject.getNames(jo); for (String string : names) { System.out.print(string + " = "); try { System.out.println(jo.get(string)); } catch (JSONException e) { e.printStackTrace(); } } return jo; }