-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdecrypt_materials_manager_node.ts
120 lines (109 loc) · 3.42 KB
/
decrypt_materials_manager_node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49
50
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
buildAwsKmsMrkAwareDiscoveryMultiKeyringNode,
buildAwsKmsMrkAwareStrictMultiKeyringNode,
KeyringNode,
KmsKeyringNode,
MultiKeyringNode,
needs,
oaepHashSupported,
RawAesKeyringNode,
RawAesWrappingSuiteIdentifier,
RawRsaKeyringNode,
WrappingSuiteIdentifier,
} from '@aws-crypto/client-node'
import {
AESKey,
AesKeyInfo,
buildGetKeyring,
KeyInfoTuple,
KMSKey,
KmsKeyInfo,
KmsMrkAwareDiscoveryKeyInfo,
KmsMrkAwareKeyInfo,
RSAKey,
RsaKeyInfo,
} from '@aws-crypto/integration-vectors'
import { constants } from 'crypto'
const Bits2RawAesWrappingSuiteIdentifier: {
[key: number]: WrappingSuiteIdentifier
} = {
128: RawAesWrappingSuiteIdentifier.AES128_GCM_IV12_TAG16_NO_PADDING,
192: RawAesWrappingSuiteIdentifier.AES192_GCM_IV12_TAG16_NO_PADDING,
256: RawAesWrappingSuiteIdentifier.AES256_GCM_IV12_TAG16_NO_PADDING,
}
export const keyringNode = buildGetKeyring<KeyringNode>({
kmsKeyring,
kmsMrkAwareKeyring,
kmsMrkAwareDiscoveryKeyring,
aesKeyring,
rsaKeyring,
})
export function encryptMaterialsManagerNode(keyInfos: KeyInfoTuple[]) {
const [generator, ...children] = keyInfos.map(keyringNode)
return new MultiKeyringNode({ generator, children })
}
export function decryptMaterialsManagerNode(keyInfos: KeyInfoTuple[]) {
const children = keyInfos.map(keyringNode)
return new MultiKeyringNode({ children })
}
export function kmsKeyring(_keyInfo: KmsKeyInfo, key: KMSKey) {
const generatorKeyId = key['key-id']
return new KmsKeyringNode({ generatorKeyId })
}
export function kmsMrkAwareKeyring(_keyInfo: KmsMrkAwareKeyInfo, key: KMSKey) {
const generatorKeyId = key['key-id']
return buildAwsKmsMrkAwareStrictMultiKeyringNode({ generatorKeyId })
}
export function kmsMrkAwareDiscoveryKeyring(
keyInfo: KmsMrkAwareDiscoveryKeyInfo
) {
const regions = [keyInfo['default-mrk-region']]
const { 'aws-kms-discovery-filter': filter } = keyInfo
const discoveryFilter = filter
? { partition: filter.partition, accountIDs: filter['account-ids'] }
: undefined
return buildAwsKmsMrkAwareDiscoveryMultiKeyringNode({
discoveryFilter,
regions,
})
}
export function aesKeyring(keyInfo: AesKeyInfo, key: AESKey) {
const keyName = key['key-id']
const keyNamespace = keyInfo['provider-id']
const { encoding, material } = key
const unencryptedMasterKey = Buffer.alloc(key.bits / 8, material, encoding)
const wrappingSuite = Bits2RawAesWrappingSuiteIdentifier[key.bits]
return new RawAesKeyringNode({
keyName,
keyNamespace,
unencryptedMasterKey,
wrappingSuite,
utf8Sorting: true,
})
}
export function rsaKeyring(keyInfo: RsaKeyInfo, key: RSAKey) {
const keyName = key['key-id']
const keyNamespace = keyInfo['provider-id']
const rsaKey =
key.type === 'private'
? { privateKey: key.material }
: { publicKey: key.material }
const { padding, oaepHash } = rsaPadding(keyInfo)
return new RawRsaKeyringNode({
keyName,
keyNamespace,
rsaKey,
padding,
oaepHash,
})
}
export function rsaPadding(keyInfo: RsaKeyInfo) {
if (keyInfo['padding-algorithm'] === 'pkcs1')
return { padding: constants.RSA_PKCS1_PADDING }
const padding = constants.RSA_PKCS1_OAEP_PADDING
const oaepHash = keyInfo['padding-hash']
needs(oaepHashSupported || oaepHash === 'sha1', 'Not supported at this time.')
return { padding, oaepHash }
}