forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcrypto_util.js
More file actions
145 lines (135 loc) · 4.37 KB
/
Copy pathwebcrypto_util.js
File metadata and controls
145 lines (135 loc) · 4.37 KB
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
'use strict';
const {
KeyObjectHandle,
kKeyFormatDER,
kKeyFormatJWK,
kKeyEncodingPKCS8,
kKeyEncodingSPKI,
kKeyTypePublic,
kKeyTypePrivate,
kKeyTypeSecret,
} = internalBinding('crypto');
const {
validateKeyOps,
} = require('internal/crypto/util');
const {
lazyDOMException,
} = require('internal/util');
function importDerKey(keyData, isPublic) {
const handle = new KeyObjectHandle();
const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate;
const encoding = isPublic ? kKeyEncodingSPKI : kKeyEncodingPKCS8;
try {
handle.init(keyType, keyData, kKeyFormatDER, encoding, null, null);
} catch (err) {
throw lazyDOMException(
'Invalid keyData', { name: 'DataError', cause: err });
}
return handle;
}
function validateJwk(keyData, kty, extractable, usagesSet, expectedUse) {
if (typeof keyData.kty !== 'string')
throw lazyDOMException('Invalid keyData', 'DataError');
if (keyData.kty !== kty)
throw lazyDOMException('Invalid JWK "kty" Parameter', 'DataError');
switch (kty) {
case 'RSA':
if (typeof keyData.n !== 'string' ||
typeof keyData.e !== 'string' ||
(keyData.d !== undefined && typeof keyData.d !== 'string'))
throw lazyDOMException('Invalid keyData', 'DataError');
if (typeof keyData.d === 'string' &&
(typeof keyData.p !== 'string' ||
typeof keyData.q !== 'string' ||
typeof keyData.dp !== 'string' ||
typeof keyData.dq !== 'string' ||
typeof keyData.qi !== 'string'))
throw lazyDOMException('Invalid keyData', 'DataError');
break;
case 'EC':
if (typeof keyData.crv !== 'string' ||
typeof keyData.x !== 'string' ||
typeof keyData.y !== 'string' ||
(keyData.d !== undefined && typeof keyData.d !== 'string'))
throw lazyDOMException('Invalid keyData', 'DataError');
break;
case 'OKP':
if (typeof keyData.crv !== 'string' ||
typeof keyData.x !== 'string' ||
(keyData.d !== undefined && typeof keyData.d !== 'string'))
throw lazyDOMException('Invalid keyData', 'DataError');
break;
case 'oct':
if (typeof keyData.k !== 'string')
throw lazyDOMException('Invalid keyData', 'DataError');
break;
case 'AKP':
if (typeof keyData.alg !== 'string' ||
typeof keyData.pub !== 'string' ||
(keyData.priv !== undefined && typeof keyData.priv !== 'string'))
throw lazyDOMException('Invalid keyData', 'DataError');
break;
default: {
// It is not possible to get here because all possible cases are handled above.
const assert = require('internal/assert');
assert.fail('Unreachable code');
}
}
if (usagesSet.size > 0 && keyData.use !== undefined) {
if (keyData.use !== expectedUse)
throw lazyDOMException('Invalid JWK "use" Parameter', 'DataError');
}
validateKeyOps(keyData.key_ops, usagesSet);
if (keyData.ext !== undefined &&
keyData.ext === false &&
extractable === true) {
throw lazyDOMException(
'JWK "ext" Parameter and extractable mismatch',
'DataError');
}
}
function importJwkKey(isPublic, keyData) {
const handle = new KeyObjectHandle();
const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate;
try {
handle.init(keyType, keyData, kKeyFormatJWK, null, null, null);
} catch (err) {
throw lazyDOMException(
'Invalid keyData', { name: 'DataError', cause: err });
}
return handle;
}
function importRawKey(isPublic, keyData, format, name, namedCurve) {
const handle = new KeyObjectHandle();
const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate;
try {
handle.init(keyType, keyData, format, name ?? null, null, namedCurve ?? null);
} catch (err) {
throw lazyDOMException(
'Invalid keyData', { name: 'DataError', cause: err });
}
return handle;
}
function importSecretKey(keyData) {
const handle = new KeyObjectHandle();
handle.init(kKeyTypeSecret, keyData);
return handle;
}
function importJwkSecretKey(keyData) {
const handle = new KeyObjectHandle();
try {
handle.init(kKeyTypeSecret, keyData, kKeyFormatJWK, null, null);
} catch (err) {
throw lazyDOMException(
'Invalid keyData', { name: 'DataError', cause: err });
}
return handle;
}
module.exports = {
importDerKey,
importJwkKey,
importJwkSecretKey,
importRawKey,
importSecretKey,
validateJwk,
};