@@ -15,7 +15,8 @@ import {
15
15
WebCryptoDecryptionMaterial ,
16
16
KeyringTraceFlag ,
17
17
} from '@aws-crypto/material-management-browser'
18
- import { KMS } from 'aws-sdk'
18
+ import { KMS as V2KMS } from 'aws-sdk'
19
+ import { KMS as V3KMS } from '@aws-sdk/client-kms'
19
20
20
21
chai . use ( chaiAsPromised )
21
22
const { expect } = chai
@@ -50,7 +51,7 @@ describe('AwsKmsMrkAwareSymmetricKeyringBrowser::constructor', () => {
50
51
/* Injected from @aws-sdk/karma-credential-loader. */
51
52
declare const credentials : any
52
53
53
- describe ( 'AwsKmsMrkAwareSymmetricKeyringBrowser encrypt/decrypt' , ( ) => {
54
+ describe ( 'AwsKmsMrkAwareSymmetricKeyringBrowser can encrypt/decrypt with AWS SDK v2 client ' , ( ) => {
54
55
const westKeyId =
55
56
'arn:aws:kms:us-west-2:658956600833:key/mrk-80bd8ecdcd4342aebd84b7dc9da498a7'
56
57
const eastKeyId =
@@ -62,12 +63,75 @@ describe('AwsKmsMrkAwareSymmetricKeyringBrowser encrypt/decrypt', () => {
62
63
)
63
64
64
65
const encryptKeyring = new AwsKmsMrkAwareSymmetricKeyringBrowser ( {
65
- client : new KMS ( { region : 'us-west-2' , credentials } ) ,
66
+ client : new V2KMS ( { region : 'us-west-2' , credentials } ) ,
66
67
keyId : westKeyId ,
67
68
grantTokens,
68
69
} )
69
70
const decryptKeyring = new AwsKmsMrkAwareSymmetricKeyringBrowser ( {
70
- client : new KMS ( { region : 'us-east-1' , credentials } ) ,
71
+ client : new V2KMS ( { region : 'us-east-1' , credentials } ) ,
72
+ keyId : eastKeyId ,
73
+ grantTokens,
74
+ } )
75
+ let encryptedDataKey : EncryptedDataKey
76
+
77
+ it ( 'can encrypt and create unencrypted data key' , async ( ) => {
78
+ const material = new WebCryptoEncryptionMaterial ( suite , encryptionContext )
79
+ const test = await encryptKeyring . onEncrypt ( material )
80
+ expect ( test . hasValidKey ( ) ) . to . equal ( true )
81
+ const udk = test . getUnencryptedDataKey ( )
82
+ expect ( udk ) . to . have . lengthOf ( suite . keyLengthBytes )
83
+ expect ( test . encryptedDataKeys ) . to . have . lengthOf ( 1 )
84
+ const [ edk ] = test . encryptedDataKeys
85
+ encryptedDataKey = edk
86
+ } )
87
+
88
+ it ( 'can encrypt a pre-existing plaintext data key' , async ( ) => {
89
+ const seedMaterial = new WebCryptoEncryptionMaterial (
90
+ suite ,
91
+ encryptionContext
92
+ ) . setUnencryptedDataKey ( new Uint8Array ( suite . keyLengthBytes ) , {
93
+ keyName : 'keyName' ,
94
+ keyNamespace : 'keyNamespace' ,
95
+ flags : KeyringTraceFlag . WRAPPING_KEY_GENERATED_DATA_KEY ,
96
+ } )
97
+ const encryptTest = await encryptKeyring . onEncrypt ( seedMaterial )
98
+ expect ( encryptTest . hasValidKey ( ) ) . to . equal ( true )
99
+ expect ( encryptTest . encryptedDataKeys ) . to . have . lengthOf ( 1 )
100
+ const [ kmsEDK ] = encryptTest . encryptedDataKeys
101
+ expect ( kmsEDK . providerId ) . to . equal ( 'aws-kms' )
102
+ expect ( kmsEDK . providerInfo ) . to . equal ( westKeyId )
103
+ } )
104
+
105
+ it ( 'can decrypt an EncryptedDataKey' , async ( ) => {
106
+ const suite = new WebCryptoAlgorithmSuite (
107
+ AlgorithmSuiteIdentifier . ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256
108
+ )
109
+ const material = new WebCryptoDecryptionMaterial ( suite , encryptionContext )
110
+ const test = await decryptKeyring . onDecrypt ( material , [ encryptedDataKey ] )
111
+ expect ( test . hasValidKey ( ) ) . to . equal ( true )
112
+ // The UnencryptedDataKey should be zeroed, because the cryptoKey has been set
113
+ expect ( ( ) => test . getUnencryptedDataKey ( ) ) . to . throw ( )
114
+ } )
115
+ } )
116
+
117
+ describe ( 'AwsKmsMrkAwareSymmetricKeyringBrowser can encrypt/decrypt with AWS SDK v3 client' , ( ) => {
118
+ const westKeyId =
119
+ 'arn:aws:kms:us-west-2:658956600833:key/mrk-80bd8ecdcd4342aebd84b7dc9da498a7'
120
+ const eastKeyId =
121
+ 'arn:aws:kms:us-east-1:658956600833:key/mrk-80bd8ecdcd4342aebd84b7dc9da498a7'
122
+ const grantTokens = [ 'grant' ]
123
+ const encryptionContext = { some : 'context' }
124
+ const suite = new WebCryptoAlgorithmSuite (
125
+ AlgorithmSuiteIdentifier . ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256
126
+ )
127
+
128
+ const encryptKeyring = new AwsKmsMrkAwareSymmetricKeyringBrowser ( {
129
+ client : new V3KMS ( { region : 'us-west-2' , credentials } ) ,
130
+ keyId : westKeyId ,
131
+ grantTokens,
132
+ } )
133
+ const decryptKeyring = new AwsKmsMrkAwareSymmetricKeyringBrowser ( {
134
+ client : new V3KMS ( { region : 'us-east-1' , credentials } ) ,
71
135
keyId : eastKeyId ,
72
136
grantTokens,
73
137
} )
0 commit comments