-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathraw_rsa_keyring_node.ts
174 lines (156 loc) · 5.09 KB
/
raw_rsa_keyring_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
KeyringNode,
needs,
NodeEncryptionMaterial,
NodeDecryptionMaterial,
EncryptedDataKey,
KeyringTrace,
KeyringTraceFlag,
immutableClass,
readOnlyProperty,
unwrapDataKey,
AwsEsdkKeyObject,
NodeAlgorithmSuite,
} from '@aws-crypto/material-management-node'
import {
constants,
publicEncrypt,
privateDecrypt,
randomBytes,
RsaPublicKey,
RsaPrivateKey,
} from 'crypto'
import {
_onEncrypt,
_onDecrypt,
WrapKey,
UnwrapKey,
} from '@aws-crypto/raw-keyring'
import { oaepHashSupported } from './oaep_hash_supported'
/* Interface question:
* When creating a keyring being able to define
* if the keyring can be used for encrypt/decrypt/both
* is a useful thing.
* Since RSA public keys can be derived from the private key
* what is the best way to signal the keyring usage?
* I have elected to explicitly pass public/private keys.
* I could have use the private key for publicEncrypt
* or more complicated options... Thoughts?
*/
interface RsaKey {
publicKey?: string | Buffer | AwsEsdkKeyObject
privateKey?: string | Buffer | AwsEsdkKeyObject
}
export type OaepHash = 'sha1' | 'sha256' | 'sha384' | 'sha512' | undefined
const supportedOaepHash: OaepHash[] = [
'sha1',
'sha256',
'sha384',
'sha512',
undefined,
]
export type RawRsaKeyringNodeInput = {
keyNamespace: string
keyName: string
rsaKey: RsaKey
padding?: number
oaepHash?: OaepHash
}
export class RawRsaKeyringNode extends KeyringNode {
public declare keyNamespace: string
public declare keyName: string
declare _wrapKey: WrapKey<NodeAlgorithmSuite>
declare _unwrapKey: UnwrapKey<NodeAlgorithmSuite>
constructor(input: RawRsaKeyringNodeInput) {
super()
const {
rsaKey,
keyName,
keyNamespace,
padding = constants.RSA_PKCS1_OAEP_PADDING,
oaepHash,
} = input
const { publicKey, privateKey } = rsaKey
/* Precondition: RsaKeyringNode needs either a public or a private key to operate. */
needs(publicKey || privateKey, 'No Key provided.')
/* Precondition: RsaKeyringNode needs identifying information for encrypt and decrypt. */
needs(keyName && keyNamespace, 'Identifying information must be defined.')
/* Precondition: The AWS ESDK only supports specific hash values for OAEP padding. */
needs(
padding === constants.RSA_PKCS1_OAEP_PADDING
? oaepHashSupported
? supportedOaepHash.includes(oaepHash)
: !oaepHash || oaepHash === 'sha1'
: !oaepHash,
'Unsupported oaepHash'
)
const _wrapKey = async (material: NodeEncryptionMaterial) => {
/* Precondition: Public key must be defined to support encrypt. */
if (!publicKey)
throw new Error(
'No public key defined in constructor. Encrypt disabled.'
)
const { buffer, byteOffset, byteLength } = unwrapDataKey(
material.getUnencryptedDataKey()
)
const encryptedDataKey = publicEncrypt(
{ key: publicKey, padding, oaepHash } as RsaPublicKey,
Buffer.from(buffer, byteOffset, byteLength)
)
const providerInfo = this.keyName
const providerId = this.keyNamespace
const flag = KeyringTraceFlag.WRAPPING_KEY_ENCRYPTED_DATA_KEY
const edk = new EncryptedDataKey({
encryptedDataKey,
providerInfo,
providerId,
})
return material.addEncryptedDataKey(edk, flag)
}
const _unwrapKey = async (
material: NodeDecryptionMaterial,
edk: EncryptedDataKey
) => {
/* Precondition: Private key must be defined to support decrypt. */
if (!privateKey)
throw new Error(
'No private key defined in constructor. Decrypt disabled.'
)
const trace: KeyringTrace = {
keyName: this.keyName,
keyNamespace: this.keyNamespace,
flags: KeyringTraceFlag.WRAPPING_KEY_DECRYPTED_DATA_KEY,
}
const { buffer, byteOffset, byteLength } = edk.encryptedDataKey
const encryptedDataKey = Buffer.from(buffer, byteOffset, byteLength)
const unencryptedDataKey = privateDecrypt(
{ key: privateKey, padding, oaepHash } as RsaPrivateKey,
encryptedDataKey
)
return material.setUnencryptedDataKey(unencryptedDataKey, trace)
}
readOnlyProperty(this, 'keyName', keyName)
readOnlyProperty(this, 'keyNamespace', keyNamespace)
readOnlyProperty(this, '_wrapKey', _wrapKey)
readOnlyProperty(this, '_unwrapKey', _unwrapKey)
}
_filter({ providerId, providerInfo }: EncryptedDataKey) {
const { keyNamespace, keyName } = this
return providerId === keyNamespace && providerInfo === keyName
}
_onEncrypt = _onEncrypt<NodeAlgorithmSuite, RawRsaKeyringNode>(
randomBytesAsync
)
_onDecrypt = _onDecrypt<NodeAlgorithmSuite, RawRsaKeyringNode>()
}
immutableClass(RawRsaKeyringNode)
async function randomBytesAsync(size: number): Promise<Buffer> {
return new Promise((resolve, reject) => {
randomBytes(size, (err: Error | null, buffer: Buffer) => {
if (err) return reject(err)
resolve(buffer)
})
})
}