protected SecretKey initEnvelopeKey()

in sdk1/src/main/java/com/amazonaws/services/dynamodbv2/datamodeling/encryption/materials/WrappedRawMaterials.java [113:142]


  protected SecretKey initEnvelopeKey() throws GeneralSecurityException {
    Map<String, String> description = getMaterialDescription();
    if (description.containsKey(ENVELOPE_KEY)) {
      if (unwrappingKey == null) {
        throw new IllegalStateException("No private decryption key provided.");
      }
      byte[] encryptedKey = Base64.decode(description.get(ENVELOPE_KEY));
      String wrappingAlgorithm = unwrappingKey.getAlgorithm();
      if (description.containsKey(KEY_WRAPPING_ALGORITHM)) {
        wrappingAlgorithm = description.get(KEY_WRAPPING_ALGORITHM);
      }
      return unwrapKey(description, encryptedKey, wrappingAlgorithm);
    } else {
      SecretKey key =
          description.containsKey(CONTENT_KEY_ALGORITHM)
              ? generateContentKey(description.get(CONTENT_KEY_ALGORITHM))
              : generateContentKey(DEFAULT_ALGORITHM);

      String wrappingAlg =
          description.containsKey(KEY_WRAPPING_ALGORITHM)
              ? description.get(KEY_WRAPPING_ALGORITHM)
              : getTransformation(wrappingKey.getAlgorithm());
      byte[] encryptedKey = wrapKey(key, wrappingAlg);
      description.put(ENVELOPE_KEY, Base64.encodeToString(encryptedKey));
      description.put(CONTENT_KEY_ALGORITHM, key.getAlgorithm());
      description.put(KEY_WRAPPING_ALGORITHM, wrappingAlg);
      setMaterialDescription(description);
      return key;
    }
  }