Class: Aws::S3::EncryptionV3::KmsCipherProvider Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-s3/encryptionV3/kms_cipher_provider.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ KmsCipherProvider

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of KmsCipherProvider.



10
11
12
13
14
15
16
17
18
19
# File 'lib/aws-sdk-s3/encryptionV3/kms_cipher_provider.rb', line 10

def initialize(options = {})
  @kms_key_id = validate_kms_key(options[:kms_key_id])
  @kms_client = options[:kms_client]
  @key_wrap_schema = validate_key_wrap(
    options[:key_wrap_schema]
  )
  @content_encryption_schema = Utils.validate_cek(
    options[:content_encryption_schema]
  )
end

Instance Method Details

#decryption_cipher(envelope, options = {}) ⇒ Cipher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Given an encryption envelope, returns a decryption cipher.

Returns:

  • (Cipher)

    Given an encryption envelope, returns a decryption cipher.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/aws-sdk-s3/encryptionV3/kms_cipher_provider.rb', line 51

def decryption_cipher(envelope, options = {})
  case envelope['x-amz-w']
  when '12'
    cek_alg = envelope['x-amz-c']
    encryption_context =
      if !envelope['x-amz-t'].nil?
        Json.load(envelope['x-amz-t'])
      else
        ##= ../specification/s3-encryption/data-format/content-metadata.md#v3-only
        ##% If the mapkey x-amz-t is not present, the default Material Description value MUST be set to an empty map (`{}`).
        {}
      end
    ##= ../specification/s3-encryption/data-format/content-metadata.md#v3-only
    ##% - The wrapping algorithm value "12" MUST be translated to kms+context upon retrieval, and vice versa on write.
    raise Errors::CEKAlgMismatchError if cek_alg != encryption_context['aws:x-amz-cek-alg']

    if encryption_context != build_encryption_context(cek_alg, options)
      raise Errors::DecryptionError, 'Value of encryption context from'\
        ' envelope does not match the provided encryption context'
    end
  when '02'
    raise ArgumentError, 'Key mismatch - Client is configured' \
            ' with a KMS key and the x-amz-wrap-alg is AES/GCM.'
  when '22'
    raise ArgumentError, 'Key mismatch - Client is configured' \
            ' with a KMS key and the x-amz-wrap-alg is RSA-OAEP-SHA1.'
  when nil
    raise ArgumentError, 'Plaintext passthrough not supported'
  else
    # assert !envelope['x-amz-w'].nil?
    # because of the when above
    raise ArgumentError, 'Unsupported wrapping algorithm: ' \
        "#{envelope['x-amz-w']}"
  end

  any_cmk_mode = options[:kms_allow_decrypt_with_any_cmk]
  decrypt_options = {
    ciphertext_blob: decode64(envelope['x-amz-3']),
    encryption_context: encryption_context
  }
  decrypt_options[:key_id] = @kms_key_id unless any_cmk_mode

  data_key = Aws::Plugins::UserAgent.metric('S3_CRYPTO_V3') do
    @kms_client.decrypt(decrypt_options).plaintext
  end

  message_id = decode64(envelope['x-amz-i'])
  commitment_key = decode64(envelope['x-amz-d'])

  Utils.derive_alg_aes_256_gcm_hkdf_sha512_commit_key_cipher(data_key, message_id, commitment_key)
end

#encryption_cipher(options = {}) ⇒ Array<Hash,Cipher>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Creates and returns a new encryption envelope and encryption cipher.

Returns:

  • (Array<Hash,Cipher>)

    Creates and returns a new encryption envelope and encryption cipher.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aws-sdk-s3/encryptionV3/kms_cipher_provider.rb', line 23

def encryption_cipher(options = {})
  validate_key_for_encryption
  encryption_context = build_encryption_context(@content_encryption_schema, options)
  key_data = Aws::Plugins::UserAgent.metric('S3_CRYPTO_V3') do
    @kms_client.generate_data_key(
      key_id: @kms_key_id,
      encryption_context: encryption_context,
      key_spec: 'AES_256'
    )
  end
  cipher, message_id, commitment_key = Utils.generate_alg_aes_256_gcm_hkdf_sha512_commit_key_cipher(key_data.plaintext)
  ##= ../specification/s3-encryption/data-format/content-metadata.md#algorithm-suite-and-message-format-version-compatibility
  ##% Objects encrypted with ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY MUST use the V3 message format version only.
  envelope = {
    'x-amz-3' => encode64(key_data.ciphertext_blob),
    'x-amz-c' => @content_encryption_schema,
    'x-amz-w' => @key_wrap_schema,
    'x-amz-d' => encode64(commitment_key),
    'x-amz-i' => encode64(message_id),
    ##= ../specification/s3-encryption/data-format/content-metadata.md#v3-only
    ##% The Encryption Context value MUST be used for wrapping algorithm `kms+context` or `12`.
    'x-amz-t' => Json.dump(encryption_context)
  }
  [envelope, cipher]
end