-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rsaCryptographyProvider.ts
197 lines (176 loc) · 4.84 KB
/
rsaCryptographyProvider.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { RSA_PKCS1_OAEP_PADDING, RSA_PKCS1_PADDING } from "constants";
import { publicEncrypt } from "crypto";
import { createVerify } from "./crypto";
import {
DecryptOptions,
DecryptParameters,
DecryptResult,
EncryptOptions,
EncryptParameters,
EncryptResult,
JsonWebKey,
KeyWrapAlgorithm,
SignOptions,
SignResult,
SignatureAlgorithm,
UnwrapKeyOptions,
UnwrapResult,
VerifyOptions,
VerifyResult,
WrapKeyOptions,
WrapResult,
} from "..";
import { convertJWKtoPEM } from "./conversions";
import {
CryptographyProvider,
CryptographyProviderOperation,
LocalCryptographyUnsupportedError,
} from "./models";
/**
* An RSA cryptography provider supporting RSA algorithms.
*/
export class RsaCryptographyProvider implements CryptographyProvider {
constructor(key: JsonWebKey) {
this.key = key;
}
isSupported(algorithm: string, operation: CryptographyProviderOperation): boolean {
return (
this.applicableAlgorithms.includes(algorithm) && this.applicableOperations.includes(operation)
);
}
encrypt(encryptParameters: EncryptParameters, _options?: EncryptOptions): Promise<EncryptResult> {
this.ensureValid();
const keyPEM = convertJWKtoPEM(this.key);
const padding =
encryptParameters.algorithm === "RSA1_5" ? RSA_PKCS1_PADDING : RSA_PKCS1_OAEP_PADDING;
return Promise.resolve({
algorithm: encryptParameters.algorithm,
keyID: this.key.kid,
result: publicEncrypt(
{ key: keyPEM, padding: padding },
Buffer.from(encryptParameters.plaintext)
),
});
}
decrypt(
_decryptParameters: DecryptParameters,
_options?: DecryptOptions
): Promise<DecryptResult> {
throw new LocalCryptographyUnsupportedError(
"Decrypting using a local JsonWebKey is not supported."
);
}
wrapKey(
algorithm: KeyWrapAlgorithm,
keyToWrap: Uint8Array,
_options?: WrapKeyOptions
): Promise<WrapResult> {
this.ensureValid();
const keyPEM = convertJWKtoPEM(this.key);
const padding = algorithm === "RSA1_5" ? RSA_PKCS1_PADDING : RSA_PKCS1_OAEP_PADDING;
return Promise.resolve({
algorithm: algorithm as KeyWrapAlgorithm,
result: publicEncrypt({ key: keyPEM, padding }, Buffer.from(keyToWrap)),
keyID: this.key.kid,
});
}
unwrapKey(
_algorithm: KeyWrapAlgorithm,
_encryptedKey: Uint8Array,
_options?: UnwrapKeyOptions
): Promise<UnwrapResult> {
throw new LocalCryptographyUnsupportedError(
"Unwrapping a key using a local JsonWebKey is not supported."
);
}
sign(
_algorithm: SignatureAlgorithm,
_digest: Uint8Array,
_options?: SignOptions
): Promise<SignResult> {
throw new LocalCryptographyUnsupportedError(
"Signing a digest using a local JsonWebKey is not supported."
);
}
signData(
_algorithm: SignatureAlgorithm,
_data: Uint8Array,
_options?: SignOptions
): Promise<SignResult> {
throw new LocalCryptographyUnsupportedError(
"Signing a block of data using a local JsonWebKey is not supported."
);
}
async verify(
_algorithm: SignatureAlgorithm,
_digest: Uint8Array,
_signature: Uint8Array,
_options?: VerifyOptions
): Promise<VerifyResult> {
throw new LocalCryptographyUnsupportedError(
"Verifying a digest using a local JsonWebKey is not supported."
);
}
verifyData(
algorithm: SignatureAlgorithm,
data: Uint8Array,
signature: Uint8Array,
_options?: VerifyOptions
): Promise<VerifyResult> {
this.ensureValid();
const keyPEM = convertJWKtoPEM(this.key);
const verifier = createVerify(algorithm, data);
return Promise.resolve({
result: verifier.verify(keyPEM, Buffer.from(signature)),
keyID: this.key.kid,
});
}
/**
* The {@link JsonWebKey} used to perform crypto operations.
*/
private key: JsonWebKey;
/**
* The set of algorithms this provider supports
*/
private applicableAlgorithms: string[] = [
"RSA1_5",
"RSA-OAEP",
"PS256",
"RS256",
"PS384",
"RS384",
"PS512",
"RS512",
];
/**
* The set of operations this provider supports
*/
private applicableOperations: CryptographyProviderOperation[] = [
"encrypt",
"wrapKey",
"verifyData",
];
/**
* Mapping between signature algorithms and their corresponding hash algorithms. Externally used for testing.
* @internal
*/
signatureAlgorithmToHashAlgorithm: { [s: string]: string } = {
PS256: "SHA256",
RS256: "SHA256",
PS384: "SHA384",
RS384: "SHA384",
PS512: "SHA512",
RS512: "SHA512",
};
private ensureValid(): void {
if (
this.key &&
this.key.kty?.toUpperCase() !== "RSA" &&
this.key.kty?.toUpperCase() !== "RSA-HSM"
) {
throw new Error("Key type does not match the algorithm RSA");
}
}
}