15
15
16
16
import { KmsClientSupplier } from './kms_client_supplier' // eslint-disable-line no-unused-vars
17
17
import {
18
- KMS , // eslint-disable-line no-unused-vars
19
- GenerateDataKeyInput , // eslint-disable-line no-unused-vars
20
- EncryptInput , // eslint-disable-line no-unused-vars
21
- DecryptInput , // eslint-disable-line no-unused-vars
22
- GenerateDataKeyOutput , // eslint-disable-line no-unused-vars
23
- RequiredGenerateDataKeyOutput , // eslint-disable-line no-unused-vars
24
- EncryptOutput , // eslint-disable-line no-unused-vars
25
- RequiredEncryptOutput , // eslint-disable-line no-unused-vars
26
- DecryptOutput , // eslint-disable-line no-unused-vars
27
- RequiredDecryptOutput // eslint-disable-line no-unused-vars
18
+ AwsEsdkKMSInterface , // eslint-disable-line no-unused-vars
19
+ GenerateDataKeyResponse , // eslint-disable-line no-unused-vars
20
+ RequiredGenerateDataKeyResponse , // eslint-disable-line no-unused-vars
21
+ EncryptResponse , // eslint-disable-line no-unused-vars
22
+ RequiredEncryptResponse , // eslint-disable-line no-unused-vars
23
+ DecryptResponse , // eslint-disable-line no-unused-vars
24
+ RequiredDecryptResponse // eslint-disable-line no-unused-vars
28
25
} from './kms_types'
29
26
import { regionFromKmsKeyArn } from './region_from_kms_key_arn'
30
27
import {
@@ -35,113 +32,110 @@ import {
35
32
36
33
export const KMS_PROVIDER_ID = 'aws-kms'
37
34
38
- export async function generateDataKey < Client extends KMS > (
35
+ export async function generateDataKey < Client extends AwsEsdkKMSInterface > (
39
36
clientProvider : KmsClientSupplier < Client > ,
40
37
NumberOfBytes : number ,
41
38
KeyId : string ,
42
39
EncryptionContext ?: EncryptionContext ,
43
40
GrantTokens ?: string [ ]
44
- ) : Promise < RequiredGenerateDataKeyOutput | false > {
41
+ ) : Promise < RequiredGenerateDataKeyResponse | false > {
45
42
const region = regionFromKmsKeyArn ( KeyId )
46
43
const client = clientProvider ( region )
47
44
48
45
/* Check for early return (Postcondition): Client region was not provided. */
49
46
if ( ! client ) return false
50
- const request : GenerateDataKeyInput < Client > = { KeyId, GrantTokens, NumberOfBytes, EncryptionContext } as GenerateDataKeyInput < Client >
51
- // @ts -ignore
52
- const v2vsV3Response = client . generateDataKey ( request )
53
- const v2vsV3Promise = ( v2vsV3Response . promise ? v2vsV3Response . promise ( ) : v2vsV3Response )
54
- const dataKey : GenerateDataKeyOutput < Client > = await v2vsV3Promise
47
+ const v2vsV3Response = client . generateDataKey ( { KeyId, GrantTokens, NumberOfBytes, EncryptionContext } )
48
+ const v2vsV3Promise = 'promise' in v2vsV3Response
49
+ ? v2vsV3Response . promise ( )
50
+ : v2vsV3Response
51
+ const dataKey = await v2vsV3Promise
55
52
56
53
return safeGenerateDataKey ( dataKey )
57
54
}
58
55
59
- export async function encrypt < Client extends KMS > (
56
+ export async function encrypt < Client extends AwsEsdkKMSInterface > (
60
57
clientProvider : KmsClientSupplier < Client > ,
61
58
Plaintext : Uint8Array ,
62
59
KeyId : string ,
63
60
EncryptionContext ?: EncryptionContext ,
64
61
GrantTokens ?: string [ ]
65
- ) : Promise < RequiredEncryptOutput | false > {
62
+ ) : Promise < RequiredEncryptResponse | false > {
66
63
const region = regionFromKmsKeyArn ( KeyId )
67
64
const client = clientProvider ( region )
68
65
69
66
/* Check for early return (Postcondition): Client region was not provided. */
70
67
if ( ! client ) return false
71
68
72
- const request : EncryptInput < Client > = { KeyId, Plaintext, EncryptionContext, GrantTokens } as EncryptInput < Client >
73
- // @ts -ignore
74
- const v2vsV3Response = client . encrypt ( request )
75
- const v2vsV3Promise = ( v2vsV3Response . promise ? v2vsV3Response . promise ( ) : v2vsV3Response )
76
- const kmsEDK : EncryptOutput < Client > = await v2vsV3Promise
69
+ const v2vsV3Response = client . encrypt ( { KeyId, Plaintext, EncryptionContext, GrantTokens } )
70
+ const v2vsV3Promise = 'promise' in v2vsV3Response
71
+ ? v2vsV3Response . promise ( )
72
+ : v2vsV3Response
73
+ const kmsEDK = await v2vsV3Promise
77
74
78
75
return safeEncryptOutput ( kmsEDK )
79
76
}
80
77
81
- export async function decrypt < Client extends KMS > (
78
+ export async function decrypt < Client extends AwsEsdkKMSInterface > (
82
79
clientProvider : KmsClientSupplier < Client > ,
83
80
{ providerId, providerInfo, encryptedDataKey } : EncryptedDataKey ,
84
81
EncryptionContext ?: EncryptionContext ,
85
82
GrantTokens ?: string [ ]
86
- ) : Promise < RequiredDecryptOutput | false > {
83
+ ) : Promise < RequiredDecryptResponse | false > {
87
84
/* Precondition: The EDK must be a KMS edk. */
88
85
needs ( providerId === KMS_PROVIDER_ID , 'Unsupported providerId' )
89
86
const region = regionFromKmsKeyArn ( providerInfo )
90
87
const client = clientProvider ( region )
91
88
/* Check for early return (Postcondition): Client region was not provided. */
92
89
if ( ! client ) return false
93
90
94
- const request : DecryptInput < Client > = {
95
- CiphertextBlob : encryptedDataKey ,
96
- EncryptionContext,
97
- GrantTokens } as DecryptInput < Client >
98
- // @ts -ignore
99
- const v2vsV3Response = client . decrypt ( request )
100
- const v2vsV3Promise = ( v2vsV3Response . promise ? v2vsV3Response . promise ( ) : v2vsV3Response )
101
- const dataKey : DecryptOutput < Client > = await v2vsV3Promise
91
+ const v2vsV3Response = client . decrypt ( { CiphertextBlob : encryptedDataKey , EncryptionContext, GrantTokens } )
92
+ const v2vsV3Promise = 'promise' in v2vsV3Response
93
+ ? v2vsV3Response . promise ( )
94
+ : v2vsV3Response
95
+ const dataKey = await v2vsV3Promise
102
96
103
97
return safeDecryptOutput ( dataKey )
104
98
}
105
99
106
100
export function kmsResponseToEncryptedDataKey ( {
107
101
KeyId : providerInfo ,
108
102
CiphertextBlob : encryptedDataKey
109
- } : RequiredEncryptOutput ) {
103
+ } : RequiredEncryptResponse ) {
110
104
return new EncryptedDataKey ( { providerId : KMS_PROVIDER_ID , providerInfo, encryptedDataKey } )
111
105
}
112
106
113
- function safeGenerateDataKey < Client extends KMS > (
114
- dataKey : GenerateDataKeyOutput < Client >
115
- ) : RequiredGenerateDataKeyOutput {
107
+ function safeGenerateDataKey (
108
+ dataKey : GenerateDataKeyResponse
109
+ ) : RequiredGenerateDataKeyResponse {
116
110
/* Postcondition: KMS must return serializable generate data key. */
117
111
needs ( typeof dataKey . KeyId === 'string' &&
118
112
dataKey . Plaintext instanceof Uint8Array &&
119
113
dataKey . CiphertextBlob instanceof Uint8Array , 'Malformed KMS response.' )
120
114
121
- return < RequiredGenerateDataKeyOutput > safePlaintext ( < RequiredGenerateDataKeyOutput > dataKey )
115
+ return < RequiredGenerateDataKeyResponse > safePlaintext ( < RequiredGenerateDataKeyResponse > dataKey )
122
116
}
123
117
124
- function safeEncryptOutput < Client extends KMS > (
125
- dataKey : EncryptOutput < Client >
126
- ) : RequiredEncryptOutput {
118
+ function safeEncryptOutput (
119
+ dataKey : EncryptResponse
120
+ ) : RequiredEncryptResponse {
127
121
/* Postcondition: KMS must return serializable encrypted data key. */
128
122
needs ( typeof dataKey . KeyId === 'string' &&
129
123
dataKey . CiphertextBlob instanceof Uint8Array , 'Malformed KMS response.' )
130
124
131
- return < RequiredEncryptOutput > dataKey
125
+ return < RequiredEncryptResponse > dataKey
132
126
}
133
127
134
- function safeDecryptOutput < Client extends KMS > (
135
- dataKey : DecryptOutput < Client >
136
- ) : RequiredDecryptOutput {
128
+ function safeDecryptOutput (
129
+ dataKey : DecryptResponse
130
+ ) : RequiredDecryptResponse {
137
131
/* Postcondition: KMS must return usable decrypted key. */
138
132
needs ( typeof dataKey . KeyId === 'string' &&
139
133
dataKey . Plaintext instanceof Uint8Array , 'Malformed KMS response.' )
140
134
141
- return < RequiredDecryptOutput > safePlaintext ( < RequiredDecryptOutput > dataKey )
135
+ return < RequiredDecryptResponse > safePlaintext ( < RequiredDecryptResponse > dataKey )
142
136
}
143
137
144
- function safePlaintext ( dataKey : RequiredDecryptOutput | RequiredGenerateDataKeyOutput ) : RequiredDecryptOutput | RequiredGenerateDataKeyOutput {
138
+ function safePlaintext ( dataKey : RequiredDecryptResponse | RequiredGenerateDataKeyResponse ) : RequiredDecryptResponse | RequiredGenerateDataKeyResponse {
145
139
/* The KMS Client *may* return a Buffer that is not isolated.
146
140
* i.e. the byteOffset !== 0.
147
141
* This means that the unencrypted data key is possibly accessible to someone else.
0 commit comments