-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption-keys-utils.ts
219 lines (187 loc) · 6.33 KB
/
encryption-keys-utils.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import {encodeArrayBufferToDOMString} from '@pashoo2/typed-array-utils';
import {eCRYPTO_UTILS_KEYS_USAGES} from '../encryption-utils/crypto-utils.const';
import {crypto} from '../init';
import {
calculateHashNative,
calculateHash,
} from '../hash-calculation-utils/hash-calculation-utils';
import {
CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS,
CONST_CRYPTO_KEYS_TYPES,
} from '../const';
import {
ENCRYPTIONS_KEYS_UTILS_JWK_FORMAT_OBJECT_KEYS,
MIN_JWK_PROPS_COUNT,
MIN_JWK_STRING_LENGTH,
} from './encryption-keys-utils.const';
import {
HASH_CALCULATION_UTILS_HASH_ALGORITHM,
HASH_CALCULATION_UTILS_DEFAULT_HASH_ALGORITHM,
} from '../const';
export const isCryptoKey = (v: any): v is CryptoKey =>
typeof v === 'object' &&
Object.getPrototypeOf(v)?.constructor?.name === 'CryptoKey';
export const isCryptoKeyPair = (
keyPair: any,
checkPrivateKeys = true
): keyPair is CryptoKeyPair => {
return (
typeof keyPair === 'object' &&
isCryptoKey(keyPair.publicKey) &&
(!checkPrivateKeys || isCryptoKey(keyPair.privateKey))
);
};
export const isCryptoKeyIncludesUsages = (
cryptoKey: CryptoKey,
expectedUsages: eCRYPTO_UTILS_KEYS_USAGES[] | eCRYPTO_UTILS_KEYS_USAGES
): boolean => {
const {usages} = cryptoKey;
if (typeof expectedUsages === 'string') {
return usages.includes(expectedUsages);
}
return usages.every(usage => (expectedUsages as string[]).includes(usage));
};
export const isCryptoKeyDataSign = (cryptoKey: any): cryptoKey is CryptoKey =>
isCryptoKey(cryptoKey) &&
isCryptoKeyIncludesUsages(cryptoKey, eCRYPTO_UTILS_KEYS_USAGES.sign);
export const isCryptoKeyDataVerify = (cryptoKey: any): cryptoKey is CryptoKey =>
isCryptoKey(cryptoKey) &&
isCryptoKeyIncludesUsages(cryptoKey, eCRYPTO_UTILS_KEYS_USAGES.verify);
export const isCryptoKeyDataEncryption = (
cryptoKey: any
): cryptoKey is CryptoKey =>
isCryptoKey(cryptoKey) &&
isCryptoKeyIncludesUsages(cryptoKey, eCRYPTO_UTILS_KEYS_USAGES.encrypt);
export const isCryptoKeyDataDecryption = (
cryptoKey: any
): cryptoKey is CryptoKey =>
isCryptoKey(cryptoKey) &&
isCryptoKeyIncludesUsages(cryptoKey, eCRYPTO_UTILS_KEYS_USAGES.decrypt);
export const isCryptoKeyPairExportedAsString = (keyPair: any): boolean => {
return typeof keyPair === 'string' && keyPair.length >= MIN_JWK_STRING_LENGTH;
};
export const isJWK = (
keyObject: object,
isReturnError = false
): Error | boolean => {
if (keyObject && typeof keyObject === 'object') {
const options = Object.keys(keyObject);
const optionsCount = options.length;
if (optionsCount > MIN_JWK_PROPS_COUNT) {
let idx = 0;
let optionName: string;
for (; idx < optionsCount; idx += 1) {
optionName = options[idx];
if (
!ENCRYPTIONS_KEYS_UTILS_JWK_FORMAT_OBJECT_KEYS.includes(optionName)
) {
return isReturnError
? new Error(`There is an unknown property ${optionName}`)
: false;
}
}
return true;
}
}
return isReturnError ? new Error('There is a wrong format of JWK') : false;
};
export const getJWK = (
key: any,
isReturnError = false
): JsonWebKey | boolean | Error => {
let keyObject = key;
if (typeof key === 'string' && key.length > MIN_JWK_STRING_LENGTH) {
try {
keyObject = JSON.parse(key);
} catch (e) {
return e;
}
}
const isJWKValid = isJWK(keyObject, isReturnError);
if (isJWKValid === true) {
return keyObject as JsonWebKey;
}
return isJWKValid instanceof Error
? isJWKValid
: new Error('There is a wrong format of JWK');
};
export const getJWKOrError = (key: any): JsonWebKey | Error =>
getJWK(key, true) as JsonWebKey | Error;
export const getJWKOrBool = (key: any): JsonWebKey | boolean =>
getJWK(key, false) as JsonWebKey | boolean;
export const exportCryptoKeyInFormat = async (
key: CryptoKey,
format: CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS
): Promise<ArrayBuffer | JsonWebKey | Error> => {
try {
const result = await crypto.subtle.exportKey(format, key);
if (result instanceof Error) {
console.error(result);
return new Error(
'exportCryptoKeyInFormat::error returned from the exportKey'
);
}
return result;
} catch (err) {
console.error(err);
return new Error(
'exportCryptoKeyInFormat::An error thrown when export the crypto key'
);
}
};
/**
* calculates hash string of the crypto key
* @param {CryptoKey} key
*/
export const calcCryptoKeyHash = async (
key: CryptoKey,
alg: HASH_CALCULATION_UTILS_HASH_ALGORITHM = HASH_CALCULATION_UTILS_DEFAULT_HASH_ALGORITHM
): Promise<Error | string> => {
if (!isCryptoKey(key)) {
return new Error('Key os not an instance of CryptoKey');
}
if (!key.extractable) {
return new Error('The crypto key is not extractable');
}
let format: CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS =
CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS.RAW;
const keyAlgName = key.algorithm.name.toLowerCase();
if (keyAlgName.includes('rsa-') || keyAlgName.includes('ecdsa')) {
if (key.type.includes(CONST_CRYPTO_KEYS_TYPES.PUBLIC)) {
format = CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS.SPKI;
} else {
format = CONST_CRYPTO_KEYS_TYPES_EXPORT_FORMATS.PKCS8;
}
}
const exportedCryptoKey = await exportCryptoKeyInFormat(key, format);
if (exportedCryptoKey instanceof Error) {
console.error(exportedCryptoKey);
return new Error('Failed to export the crypto key in the RAW format');
}
const hashCalcResult = await calculateHashNative(
exportedCryptoKey as ArrayBuffer,
alg
);
if (hashCalcResult instanceof Error) {
console.error(hashCalcResult);
return new Error('Failed to calculate a hash for the exported crypto key');
}
return encodeArrayBufferToDOMString(hashCalcResult);
};
// allow to absent for a private keys in a pairs
export const calcCryptoKeyPairHash = async (
cryptoPair: CryptoKeyPair
): Promise<Error | string> => {
const pending = [calcCryptoKeyHash(cryptoPair.publicKey)];
if (cryptoPair.privateKey) {
pending.push(calcCryptoKeyHash(cryptoPair.privateKey));
}
const results = await Promise.all(pending);
if (results[0] instanceof Error) {
return new Error('Failed to calculate hash of the private key');
}
if (results[1] instanceof Error) {
return new Error('Failed to calculate hash of the public key');
}
return calculateHash(`${results[0]}___${results[1]}`);
};