aws-sdk-s3 1.74.0 → 1.79.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/aws-sdk-s3.rb +2 -2
- data/lib/aws-sdk-s3/bucket.rb +2 -2
- data/lib/aws-sdk-s3/client.rb +148 -120
- data/lib/aws-sdk-s3/customizations/object.rb +12 -1
- data/lib/aws-sdk-s3/encryption.rb +2 -0
- data/lib/aws-sdk-s3/encryption/client.rb +11 -0
- data/lib/aws-sdk-s3/encryption/decrypt_handler.rb +64 -29
- data/lib/aws-sdk-s3/encryption/default_cipher_provider.rb +41 -5
- data/lib/aws-sdk-s3/encryption/encrypt_handler.rb +5 -5
- data/lib/aws-sdk-s3/encryption/io_decrypter.rb +7 -6
- data/lib/aws-sdk-s3/encryption/kms_cipher_provider.rb +32 -3
- data/lib/aws-sdk-s3/encryption/utils.rb +23 -0
- data/lib/aws-sdk-s3/encryptionV2/client.rb +201 -23
- data/lib/aws-sdk-s3/encryptionV2/decrypt_handler.rb +40 -12
- data/lib/aws-sdk-s3/encryptionV2/default_cipher_provider.rb +77 -10
- data/lib/aws-sdk-s3/encryptionV2/default_key_provider.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/encrypt_handler.rb +7 -4
- data/lib/aws-sdk-s3/encryptionV2/errors.rb +24 -0
- data/lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/io_decrypter.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/io_encrypter.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/key_provider.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/kms_cipher_provider.rb +90 -20
- data/lib/aws-sdk-s3/encryptionV2/materials.rb +2 -0
- data/lib/aws-sdk-s3/encryptionV2/utils.rb +2 -15
- data/lib/aws-sdk-s3/encryption_v2.rb +4 -1
- data/lib/aws-sdk-s3/file_uploader.rb +11 -0
- data/lib/aws-sdk-s3/multipart_file_uploader.rb +37 -2
- data/lib/aws-sdk-s3/multipart_upload_part.rb +1 -1
- data/lib/aws-sdk-s3/object.rb +1 -1
- data/lib/aws-sdk-s3/object_summary.rb +19 -3
- data/lib/aws-sdk-s3/plugins/accelerate.rb +3 -1
- data/lib/aws-sdk-s3/plugins/dualstack.rb +3 -1
- data/lib/aws-sdk-s3/plugins/sse_cpk.rb +1 -1
- data/lib/aws-sdk-s3/presigner.rb +2 -2
- data/lib/aws-sdk-s3/resource.rb +1 -1
- data/lib/aws-sdk-s3/types.rb +25 -8
- metadata +4 -4
@@ -1,16 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'forwardable'
|
2
4
|
|
3
5
|
module Aws
|
4
6
|
module S3
|
5
7
|
|
8
|
+
REQUIRED_PARAMS = [:key_wrap_schema, :content_encryption_schema, :security_profile]
|
9
|
+
SUPPORTED_SECURITY_PROFILES = [:v2, :v2_and_legacy]
|
10
|
+
|
6
11
|
# Provides an encryption client that encrypts and decrypts data client-side,
|
7
|
-
# storing the encrypted data in Amazon S3. The EncryptionV2::Client
|
8
|
-
# provides improved security over the Encryption::Client
|
9
|
-
# modern and secure algorithms.
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# Encryption::Client.
|
12
|
+
# storing the encrypted data in Amazon S3. The `EncryptionV2::Client` (V2 Client)
|
13
|
+
# provides improved security over the `Encryption::Client` (V1 Client)
|
14
|
+
# by using more modern and secure algorithms. You can use the V2 Client
|
15
|
+
# to continue decrypting objects encrypted using deprecated algorithms
|
16
|
+
# by setting security_profile: :v2_and_legacy. The latest V1 Client also
|
17
|
+
# supports reading and decrypting objects encrypted by the V2 Client.
|
14
18
|
#
|
15
19
|
# This client uses a process called "envelope encryption". Your private
|
16
20
|
# encryption keys and your data's plain-text are **never** sent to
|
@@ -47,7 +51,12 @@ module Aws
|
|
47
51
|
# key = OpenSSL::PKey::RSA.new(1024)
|
48
52
|
#
|
49
53
|
# # encryption client
|
50
|
-
# s3 = Aws::S3::EncryptionV2::Client.new(
|
54
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(
|
55
|
+
# encryption_key: key,
|
56
|
+
# key_wrap_schema: :rsa_oaep_sha1, # the key_wrap_schema must be rsa_oaep_sha1 for asymmetric keys
|
57
|
+
# content_encryption_schema: :aes_gcm_no_padding,
|
58
|
+
# security_profile: :v2 # use :v2_and_legacy to allow reading/decrypting objects encrypted by the V1 encryption client
|
59
|
+
# )
|
51
60
|
#
|
52
61
|
# # round-trip an object, encrypted/decrypted locally
|
53
62
|
# s3.put_object(bucket:'aws-sdk', key:'secret', body:'handshake')
|
@@ -59,6 +68,20 @@ module Aws
|
|
59
68
|
# Aws::S3::Client.new.get_object(bucket:'aws-sdk', key:'secret').body.read
|
60
69
|
# #=> "... cipher text ..."
|
61
70
|
#
|
71
|
+
# ## Required Configuration
|
72
|
+
#
|
73
|
+
# You must configure all of the following:
|
74
|
+
#
|
75
|
+
# * a key or key provider - See the Keys section below. The key provided determines
|
76
|
+
# the key wrapping schema(s) supported for both encryption and decryption.
|
77
|
+
# * `key_wrap_schema` - The key wrapping schema. It must match the type of key configured.
|
78
|
+
# * `content_encryption_schema` - The only supported value currently is `:aes_gcm_no_padding`.
|
79
|
+
# More options will be added in future releases.
|
80
|
+
# * `security_profile` - Determines the support for reading objects written
|
81
|
+
# using older key wrap or content encryption schemas. If you need to read
|
82
|
+
# legacy objects encrypted by an existing V1 Client, then set this to `:v2_and_legacy`.
|
83
|
+
# Otherwise, set it to `:v2`
|
84
|
+
#
|
62
85
|
# ## Keys
|
63
86
|
#
|
64
87
|
# For client-side encryption to work, you must provide one of the following:
|
@@ -67,15 +90,25 @@ module Aws
|
|
67
90
|
# * A {KeyProvider}
|
68
91
|
# * A KMS encryption key id
|
69
92
|
#
|
93
|
+
# Additionally, the key wrapping schema must agree with the type of the key:
|
94
|
+
# * :aes_gcm: An AES encryption key or a key provider.
|
95
|
+
# * :rsa_oaep_sha1: An RSA encryption key or key provider.
|
96
|
+
# * :kms_context: A KMS encryption key id
|
97
|
+
#
|
70
98
|
# ### An Encryption Key
|
71
99
|
#
|
72
100
|
# You can pass a single encryption key. This is used as a master key
|
73
101
|
# encrypting and decrypting all object keys.
|
74
102
|
#
|
75
|
-
# key = OpenSSL::Cipher.new("AES-256-ECB").random_key # symmetric key
|
76
|
-
# key = OpenSSL::PKey::RSA.new(1024) # asymmetric key pair
|
103
|
+
# key = OpenSSL::Cipher.new("AES-256-ECB").random_key # symmetric key - used with `key_wrap_schema: :aes_gcm`
|
104
|
+
# key = OpenSSL::PKey::RSA.new(1024) # asymmetric key pair - used with `key_wrap_schema: :rsa_oaep_sha1`
|
77
105
|
#
|
78
|
-
# s3 = Aws::S3::EncryptionV2::Client.new(
|
106
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(
|
107
|
+
# encryption_key: key,
|
108
|
+
# key_wrap_schema: :aes_gcm, # or :rsa_oaep_sha1 if using RSA
|
109
|
+
# content_encryption_schema: :aes_gcm_no_padding,
|
110
|
+
# security_profile: :v2
|
111
|
+
# )
|
79
112
|
#
|
80
113
|
# ### Key Provider
|
81
114
|
#
|
@@ -84,8 +117,9 @@ module Aws
|
|
84
117
|
#
|
85
118
|
# ### KMS Encryption Key Id
|
86
119
|
#
|
87
|
-
# If you pass the id
|
88
|
-
# then KMS will be used to
|
120
|
+
# If you pass the id of an AWS Key Management Service (KMS) key and
|
121
|
+
# use :kms_content for the key_wrap_schema, then KMS will be used to
|
122
|
+
# generate, encrypt and decrypt object keys.
|
89
123
|
#
|
90
124
|
# # keep track of the kms key id
|
91
125
|
# kms = Aws::KMS::Client.new
|
@@ -94,6 +128,9 @@ module Aws
|
|
94
128
|
# Aws::S3::EncryptionV2::Client.new(
|
95
129
|
# kms_key_id: key_id,
|
96
130
|
# kms_client: kms,
|
131
|
+
# key_wrap_schema: :kms_context,
|
132
|
+
# content_encryption_schema: :aes_gcm_no_padding,
|
133
|
+
# security_profile: :v2
|
97
134
|
# )
|
98
135
|
#
|
99
136
|
# ## Custom Key Providers
|
@@ -142,7 +179,12 @@ module Aws
|
|
142
179
|
#
|
143
180
|
# # chooses the key based on the materials description stored
|
144
181
|
# # with the encrypted object
|
145
|
-
# s3 = Aws::S3::EncryptionV2::Client.new(
|
182
|
+
# s3 = Aws::S3::EncryptionV2::Client.new(
|
183
|
+
# key_provider: keys,
|
184
|
+
# key_wrap_schema: ...,
|
185
|
+
# content_encryption_schema: :aes_gcm_no_padding,
|
186
|
+
# security_profile: :v2
|
187
|
+
# )
|
146
188
|
#
|
147
189
|
# ## Materials Description
|
148
190
|
#
|
@@ -176,6 +218,9 @@ module Aws
|
|
176
218
|
# key_provider: ...,
|
177
219
|
# envelope_location: :instruction_file,
|
178
220
|
# instruction_file_suffix: '.instruction' # default
|
221
|
+
# key_wrap_schema: ...,
|
222
|
+
# content_encryption_schema: :aes_gcm_no_padding,
|
223
|
+
# security_profile: :v2
|
179
224
|
# )
|
180
225
|
#
|
181
226
|
# When using an instruction file, multiple requests are made when
|
@@ -189,8 +234,19 @@ module Aws
|
|
189
234
|
extend Forwardable
|
190
235
|
def_delegators :@client, :config, :delete_object, :head_object, :build_request
|
191
236
|
|
192
|
-
# Creates a new encryption client. You must
|
193
|
-
#
|
237
|
+
# Creates a new encryption client. You must configure all of the following:
|
238
|
+
#
|
239
|
+
# * a key or key provider - The key provided also determines the key wrapping
|
240
|
+
# schema(s) supported for both encryption and decryption.
|
241
|
+
# * `key_wrap_schema` - The key wrapping schema. It must match the type of key configured.
|
242
|
+
# * `content_encryption_schema` - The only supported value currently is `:aes_gcm_no_padding`
|
243
|
+
# More options will be added in future releases.
|
244
|
+
# * `security_profile` - Determines the support for reading objects written
|
245
|
+
# using older key wrap or content encryption schemas. If you need to read
|
246
|
+
# legacy objects encrypted by an existing V1 Client, then set this to `:v2_and_legacy`.
|
247
|
+
# Otherwise, set it to `:v2`
|
248
|
+
#
|
249
|
+
# To configure the key you must provide one of the following set of options:
|
194
250
|
#
|
195
251
|
# * `:encryption_key`
|
196
252
|
# * `:kms_key_id`
|
@@ -209,12 +265,36 @@ module Aws
|
|
209
265
|
# then AWS Key Management Service (KMS) will be used to manage the
|
210
266
|
# object encryption keys. By default a {KMS::Client} will be
|
211
267
|
# constructed for KMS API calls. Alternatively, you can provide
|
212
|
-
# your own via `:kms_client`.
|
268
|
+
# your own via `:kms_client`. To only support decryption/reads, you may
|
269
|
+
# provide `:allow_decrypt_with_any_cmk` which will use
|
270
|
+
# the implicit CMK associated with the data during reads but will
|
271
|
+
# not allow you to encrypt/write objects with this client.
|
213
272
|
#
|
214
273
|
# @option options [#key_for] :key_provider Any object that responds
|
215
274
|
# to `#key_for`. This method should accept a materials description
|
216
275
|
# JSON document string and return return an encryption key.
|
217
276
|
#
|
277
|
+
# @option options [required, Symbol] :key_wrap_schema The Key wrapping
|
278
|
+
# schema to be used. It must match the type of key configured.
|
279
|
+
# Must be one of the following:
|
280
|
+
#
|
281
|
+
# * :kms_context (Must provide kms_key_id)
|
282
|
+
# * :aes_gcm (Must provide an AES (string) key)
|
283
|
+
# * :rsa_oaep_sha1 (Must provide an RSA key)
|
284
|
+
#
|
285
|
+
# @option options [required, Symbol] :content_encryption_schema
|
286
|
+
# Must be one of the following:
|
287
|
+
#
|
288
|
+
# * :aes_gcm_no_padding
|
289
|
+
#
|
290
|
+
# @option options [Required, Symbol] :security_profile
|
291
|
+
# Determines the support for reading objects written using older
|
292
|
+
# key wrap or content encryption schemas.
|
293
|
+
# Must be one of the following:
|
294
|
+
#
|
295
|
+
# * :v2 - Reads of legacy (v1) objects are NOT allowed
|
296
|
+
# * :v2_and_legacy - Enables reading of legacy (V1) schemas.
|
297
|
+
#
|
218
298
|
# @option options [Symbol] :envelope_location (:metadata) Where to
|
219
299
|
# store the envelope encryption keys. By default, the envelope is
|
220
300
|
# stored with the encrypted object. If you pass `:instruction_file`,
|
@@ -228,10 +308,14 @@ module Aws
|
|
228
308
|
# is constructed when using KMS to manage encryption keys.
|
229
309
|
#
|
230
310
|
def initialize(options = {})
|
311
|
+
validate_params(options)
|
231
312
|
@client = extract_client(options)
|
232
313
|
@cipher_provider = cipher_provider(options)
|
233
314
|
@envelope_location = extract_location(options)
|
234
315
|
@instruction_file_suffix = extract_suffix(options)
|
316
|
+
@kms_allow_decrypt_with_any_cmk =
|
317
|
+
options[:kms_key_id] == :kms_allow_decrypt_with_any_cmk
|
318
|
+
@security_profile = extract_security_profile(options)
|
235
319
|
end
|
236
320
|
|
237
321
|
# @return [S3::Client]
|
@@ -241,6 +325,14 @@ module Aws
|
|
241
325
|
# AWS Key Management Service (KMS).
|
242
326
|
attr_reader :key_provider
|
243
327
|
|
328
|
+
# @return [Symbol] Determines the support for reading objects written
|
329
|
+
# using older key wrap or content encryption schemas.
|
330
|
+
attr_reader :security_profile
|
331
|
+
|
332
|
+
# @return [Boolean] If true the provided KMS key_id will not be used
|
333
|
+
# during decrypt, allowing decryption with the key_id from the object.
|
334
|
+
attr_reader :kms_allow_decrypt_with_any_cmk
|
335
|
+
|
244
336
|
# @return [Symbol<:metadata, :instruction_file>]
|
245
337
|
attr_reader :envelope_location
|
246
338
|
|
@@ -272,9 +364,22 @@ module Aws
|
|
272
364
|
req.send_request
|
273
365
|
end
|
274
366
|
|
275
|
-
# Gets an object from Amazon S3, decrypting
|
367
|
+
# Gets an object from Amazon S3, decrypting data locally.
|
276
368
|
# See {S3::Client#get_object} for documentation on accepted
|
277
369
|
# request parameters.
|
370
|
+
# Warning: If you provide a block to get_object or set the request
|
371
|
+
# parameter :response_target to a Proc, then read the entire object to the
|
372
|
+
# end before you start using the decrypted data. This is to verify that
|
373
|
+
# the object has not been modified since it was encrypted.
|
374
|
+
#
|
375
|
+
# @option options [Symbol] :security_profile
|
376
|
+
# Determines the support for reading objects written using older
|
377
|
+
# key wrap or content encryption schemas. Overrides the value set
|
378
|
+
# on client construction if provided.
|
379
|
+
# Must be one of the following:
|
380
|
+
#
|
381
|
+
# * :v2 - Reads of legacy (v1) objects are NOT allowed
|
382
|
+
# * :v2_and_legacy - Enables reading of legacy (V1) schemas.
|
278
383
|
# @option params [String] :instruction_file_suffix The suffix
|
279
384
|
# used to find the instruction file containing the encryption
|
280
385
|
# envelope. You should not set this option when the envelope
|
@@ -282,30 +387,58 @@ module Aws
|
|
282
387
|
# {#instruction_file_suffix}.
|
283
388
|
# @option params [Hash] :kms_encryption_context Additional encryption
|
284
389
|
# context to use with KMS. Applies only when KMS is used.
|
285
|
-
# @option
|
390
|
+
# @option options [Boolean] :kms_allow_decrypt_with_any_cmk (false)
|
391
|
+
# By default the KMS CMK ID (kms_key_id) will be used during decrypt
|
392
|
+
# and will fail if there is a mismatch. Setting this to true
|
393
|
+
# will use the implicit CMK associated with the data.
|
286
394
|
# @option (see S3::Client#get_object)
|
287
395
|
# @return (see S3::Client#get_object)
|
288
396
|
# @see S3::Client#get_object
|
289
|
-
# @note The `:range` request parameter is not
|
397
|
+
# @note The `:range` request parameter is not supported.
|
290
398
|
def get_object(params = {}, &block)
|
291
399
|
if params[:range]
|
292
400
|
raise NotImplementedError, '#get_object with :range not supported'
|
293
401
|
end
|
294
402
|
envelope_location, instruction_file_suffix = envelope_options(params)
|
295
403
|
kms_encryption_context = params.delete(:kms_encryption_context)
|
404
|
+
kms_any_cmk_mode = kms_any_cmk_mode(params)
|
405
|
+
security_profile = security_profile_from_params(params)
|
406
|
+
|
296
407
|
req = @client.build_request(:get_object, params)
|
297
408
|
req.handlers.add(DecryptHandler)
|
298
409
|
req.context[:encryption] = {
|
299
410
|
cipher_provider: @cipher_provider,
|
300
411
|
envelope_location: envelope_location,
|
301
412
|
instruction_file_suffix: instruction_file_suffix,
|
302
|
-
kms_encryption_context: kms_encryption_context
|
413
|
+
kms_encryption_context: kms_encryption_context,
|
414
|
+
kms_allow_decrypt_with_any_cmk: kms_any_cmk_mode,
|
415
|
+
security_profile: security_profile
|
303
416
|
}
|
304
417
|
req.send_request(target: block)
|
305
418
|
end
|
306
419
|
|
307
420
|
private
|
308
421
|
|
422
|
+
# Validate required parameters exist and don't conflict.
|
423
|
+
# The cek_alg and wrap_alg are passed on to the CipherProviders
|
424
|
+
# and further validated there
|
425
|
+
def validate_params(options)
|
426
|
+
unless (missing_params = REQUIRED_PARAMS - options.keys).empty?
|
427
|
+
raise ArgumentError, "Missing required parameter(s): "\
|
428
|
+
"#{missing_params.map{ |s| ":#{s}" }.join(', ')}"
|
429
|
+
end
|
430
|
+
|
431
|
+
wrap_alg = options[:key_wrap_schema]
|
432
|
+
|
433
|
+
# validate that the wrap alg matches the type of key given
|
434
|
+
case wrap_alg
|
435
|
+
when :kms_context
|
436
|
+
unless options[:kms_key_id]
|
437
|
+
raise ArgumentError, 'You must provide :kms_key_id to use :kms_context'
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
309
442
|
def extract_client(options)
|
310
443
|
options[:client] || begin
|
311
444
|
options = options.dup
|
@@ -315,6 +448,7 @@ module Aws
|
|
315
448
|
options.delete(:encryption_key)
|
316
449
|
options.delete(:envelope_location)
|
317
450
|
options.delete(:instruction_file_suffix)
|
451
|
+
REQUIRED_PARAMS.each { |p| options.delete(p) }
|
318
452
|
S3::Client.new(options)
|
319
453
|
end
|
320
454
|
end
|
@@ -324,7 +458,7 @@ module Aws
|
|
324
458
|
KMS::Client.new(
|
325
459
|
region: @client.config.region,
|
326
460
|
credentials: @client.config.credentials,
|
327
|
-
|
461
|
+
)
|
328
462
|
end
|
329
463
|
end
|
330
464
|
|
@@ -333,10 +467,16 @@ module Aws
|
|
333
467
|
KmsCipherProvider.new(
|
334
468
|
kms_key_id: options[:kms_key_id],
|
335
469
|
kms_client: kms_client(options),
|
470
|
+
key_wrap_schema: options[:key_wrap_schema],
|
471
|
+
content_encryption_schema: options[:content_encryption_schema]
|
336
472
|
)
|
337
473
|
else
|
338
474
|
@key_provider = extract_key_provider(options)
|
339
|
-
DefaultCipherProvider.new(
|
475
|
+
DefaultCipherProvider.new(
|
476
|
+
key_provider: @key_provider,
|
477
|
+
key_wrap_schema: options[:key_wrap_schema],
|
478
|
+
content_encryption_schema: options[:content_encryption_schema]
|
479
|
+
)
|
340
480
|
end
|
341
481
|
end
|
342
482
|
|
@@ -382,6 +522,44 @@ module Aws
|
|
382
522
|
end
|
383
523
|
end
|
384
524
|
|
525
|
+
def kms_any_cmk_mode(params)
|
526
|
+
if !params[:kms_allow_decrypt_with_any_cmk].nil?
|
527
|
+
params.delete(:kms_allow_decrypt_with_any_cmk)
|
528
|
+
else
|
529
|
+
@kms_allow_decrypt_with_any_cmk
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
def extract_security_profile(options)
|
534
|
+
validate_security_profile(options[:security_profile])
|
535
|
+
end
|
536
|
+
|
537
|
+
def security_profile_from_params(params)
|
538
|
+
security_profile =
|
539
|
+
if !params[:security_profile].nil?
|
540
|
+
params.delete(:security_profile)
|
541
|
+
else
|
542
|
+
@security_profile
|
543
|
+
end
|
544
|
+
validate_security_profile(security_profile)
|
545
|
+
end
|
546
|
+
|
547
|
+
def validate_security_profile(security_profile)
|
548
|
+
unless SUPPORTED_SECURITY_PROFILES.include? security_profile
|
549
|
+
raise ArgumentError, "Unsupported security profile: :#{security_profile}. " \
|
550
|
+
"Please provide one of: #{SUPPORTED_SECURITY_PROFILES.map { |s| ":#{s}" }.join(', ')}"
|
551
|
+
end
|
552
|
+
if security_profile == :v2_and_legacy && !@warned_about_legacy
|
553
|
+
@warned_about_legacy = true
|
554
|
+
warn(
|
555
|
+
'The S3 Encryption Client is configured to read encrypted objects ' \
|
556
|
+
"with legacy encryption modes. If you don't have objects " \
|
557
|
+
'encrypted with these legacy modes, you should disable support ' \
|
558
|
+
'for them to enhance security.'
|
559
|
+
)
|
560
|
+
end
|
561
|
+
security_profile
|
562
|
+
end
|
385
563
|
end
|
386
564
|
end
|
387
565
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'base64'
|
2
4
|
|
3
5
|
module Aws
|
@@ -5,6 +7,7 @@ module Aws
|
|
5
7
|
module EncryptionV2
|
6
8
|
# @api private
|
7
9
|
class DecryptHandler < Seahorse::Client::Handler
|
10
|
+
@@warned_response_target_proc = false
|
8
11
|
|
9
12
|
V1_ENVELOPE_KEYS = %w(
|
10
13
|
x-amz-key
|
@@ -38,9 +41,22 @@ module Aws
|
|
38
41
|
AES/CBC/PKCS7Padding
|
39
42
|
)
|
40
43
|
|
44
|
+
AUTH_REQUIRED_CEK_ALGS = %w(AES/GCM/NoPadding)
|
45
|
+
|
41
46
|
def call(context)
|
42
47
|
attach_http_event_listeners(context)
|
43
48
|
apply_cse_user_agent(context)
|
49
|
+
|
50
|
+
if context[:response_target].is_a?(Proc) && !@@warned_response_target_proc
|
51
|
+
@@warned_response_target_proc = true
|
52
|
+
warn(':response_target is a Proc, or a block was provided. ' \
|
53
|
+
'Read the entire object to the ' \
|
54
|
+
'end before you start using the decrypted data. This is to ' \
|
55
|
+
'verify that the object has not been modified since it ' \
|
56
|
+
'was encrypted.')
|
57
|
+
|
58
|
+
end
|
59
|
+
|
44
60
|
@handler.call(context)
|
45
61
|
end
|
46
62
|
|
@@ -71,11 +87,11 @@ module Aws
|
|
71
87
|
end
|
72
88
|
|
73
89
|
def decryption_cipher(context)
|
74
|
-
if envelope = get_encryption_envelope(context)
|
90
|
+
if (envelope = get_encryption_envelope(context))
|
75
91
|
cipher = context[:encryption][:cipher_provider]
|
76
92
|
.decryption_cipher(
|
77
93
|
envelope,
|
78
|
-
|
94
|
+
context[:encryption]
|
79
95
|
)
|
80
96
|
[cipher, envelope]
|
81
97
|
else
|
@@ -145,10 +161,6 @@ module Aws
|
|
145
161
|
envelope
|
146
162
|
end
|
147
163
|
|
148
|
-
# When the x-amz-meta-x-amz-tag-len header is present, it indicates
|
149
|
-
# that the body of this object has a trailing auth tag. The header
|
150
|
-
# indicates the length of that tag.
|
151
|
-
#
|
152
164
|
# This method fetches the tag from the end of the object by
|
153
165
|
# making a GET Object w/range request. This auth tag is used
|
154
166
|
# to initialize the cipher, and the decrypter truncates the
|
@@ -160,8 +172,7 @@ module Aws
|
|
160
172
|
end
|
161
173
|
http_resp = context.http_response
|
162
174
|
content_length = http_resp.headers['content-length'].to_i
|
163
|
-
auth_tag_length = envelope
|
164
|
-
auth_tag_length = auth_tag_length.to_i / 8
|
175
|
+
auth_tag_length = auth_tag_length(envelope)
|
165
176
|
|
166
177
|
auth_tag = context.client.get_object(
|
167
178
|
bucket: context.params[:bucket],
|
@@ -181,14 +192,31 @@ module Aws
|
|
181
192
|
end
|
182
193
|
|
183
194
|
def body_contains_auth_tag?(envelope)
|
184
|
-
|
195
|
+
AUTH_REQUIRED_CEK_ALGS.include?(envelope['x-amz-cek-alg'])
|
196
|
+
end
|
197
|
+
|
198
|
+
# Determine the auth tag length from the algorithm
|
199
|
+
# Validate it against the value provided in the x-amz-tag-len
|
200
|
+
# Return the tag length in bytes
|
201
|
+
def auth_tag_length(envelope)
|
202
|
+
tag_length =
|
203
|
+
case envelope['x-amz-cek-alg']
|
204
|
+
when 'AES/GCM/NoPadding' then AES_GCM_TAG_LEN_BYTES
|
205
|
+
else
|
206
|
+
raise ArgumentError, 'Unsupported cek-alg: ' \
|
207
|
+
"#{envelope['x-amz-cek-alg']}"
|
208
|
+
end
|
209
|
+
if (tag_length * 8) != envelope['x-amz-tag-len'].to_i
|
210
|
+
raise Errors::DecryptionError, 'x-amz-tag-len does not match expected'
|
211
|
+
end
|
212
|
+
tag_length
|
185
213
|
end
|
186
214
|
|
187
215
|
def apply_cse_user_agent(context)
|
188
216
|
if context.config.user_agent_suffix.nil?
|
189
|
-
context.config.user_agent_suffix =
|
190
|
-
elsif !context.config.user_agent_suffix.include?
|
191
|
-
context.config.user_agent_suffix +=
|
217
|
+
context.config.user_agent_suffix = EC_USER_AGENT
|
218
|
+
elsif !context.config.user_agent_suffix.include? EC_USER_AGENT
|
219
|
+
context.config.user_agent_suffix += " #{EC_USER_AGENT}"
|
192
220
|
end
|
193
221
|
end
|
194
222
|
|