-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportKey.js
executable file
·279 lines (212 loc) · 6.23 KB
/
importKey.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Promise<any> importKey(KeyFormat format, CryptoOperationData keyData, AlgorithmIdentifier? algorithm,
boolean extractable, KeyUsage[] keyUsages );
*/
var importKey = function(format,keyData,algorithm,extractable,keyUsages){
return new Promise(function(resolve,reject){
if (!algorithm){
reject('Algorithm not provided');
}
else if (!algorithm.name){
reject('Algorithm name not provided');
}
if (!keyData){
reject("keyData is null");
}
var algo = algorithm.name;
//Checking if the algo name in present in the suggested algorithms
if (importKeyalgos.indexOf(algo)==-1){
//Not correct. Check how to reject a DOMException
reject("The algorithm is not supported");
}
//Checking if the format is one of the recognized key format values
if (!checkRecognizedKeyFormatValues(format)){
reject ("SyntaxError");
}
//Checking if the any of the value of keyUsages are in the Recognized Key Usage Values
if (!checkRecognizedKeyUsageValues(keyUsages)){
reject ("InvalidAccessError");
}
switch(algo){
case "HMAC":
//If usages contains an entry which is not "sign" or "verify", then return an error named DataError.
if (keyUsages.indexOf("sign")==-1 && keyUsages.indexOf("verify")==-1){
reject ("DataError");
}
var name;
//Checking format
switch(format){
case "raw":
//Not sure what is octet string.
//TODO: Converting the keyData into normal String by convertArrayBufferViewToPlainText
/*if (keyData.length==0){
reject ("DataError");
}*/
if (algorithm.hasOwnProperty("hash") && algorithm.hash.hasOwnProperty("name")){
name = algorithm.hash.name;
}
else {
reject("SyntaxError");
}
break;
}
var data = convertArrayBufferViewToPlainText(keyData);
//hash is the new KeyAlgorithm
var hash = {
name : name
};
var HmacKeyAlgorithm = {
name : "HMAC",
hash : hash
};
var key = {
type : "secret",
extractable : extractable,
algorithm : HmacKeyAlgorithm,
//TODO: intersection of keyUsages and recognizedKeyUsageValues
usages : keyUsages,
data : keyData
};
resolve(key);
break;
case "AES-CBC":
var length;
if (keyUsages.indexOf("encrypt")==-1 && keyUsages.indexOf("decrypt")==-1 && keyUsages.indexOf("wrapKey")==-1 && keyUsages.indexOf("unwrapKey")==-1){
reject ("DataError");
}
switch(format){
case "raw":
//Not sure what is octet string.
//TODO: Converting the keyData into normal String by convertArrayBufferViewToPlainText
var checklength = keyData.length*8;
if (checklength!=128 && checklength!=192 && checklength!=256){
reject ("DataError");
}
else {
length = checklength;
}
break;
}
var data = convertArrayBufferViewToPlainText(keyData);
var AesKeyAlgorithm = {
name : "AES-CBC",
length : length
};
var key = {
type : "secret",
extractable : extractable,
algorithm : AesKeyAlgorithm,
//TODO: intersection of keyUsages and recognizedKeyUsageValues
usages : keyUsages,
data : keyData
};
resolve(key);
break;
case "RSA-OAEP":
case "RSASSA-PKCS1-v1_5":
if (!algorithm.hasOwnProperty("hash")){
reject("SyntaxError");
}
var hash;
var key;
var publicKey,privateKey;
if (!algorithm.hash.hasOwnProperty("name")){
reject ("DataError");
}
switch(format){
case "spki":
//TODO : What is all the stuff about parsing?
keyData = convertStringToHex(convertArrayBufferViewToPlainText(keyData));
publicKey = convert(keyData,'auto');
if (publicKey.length!=3){
reject("DataError");
}
var alg = publicKey[0];
if (alg == "1.2.840.113549.1.1.1"){
}
//TODO : id-RSAES-OAEP
if (hash){
}
else {
hash = algorithm.hash.name;
}
publicKey = convert(publicKey[2],'auto');
if (publicKey.length!=2){
reject("DataError");
}
publicKey.n = publicKey[0];
publicKey.e = publicKey[1];
key = {
type : "public",
extractable : extractable,
usages : keyUsages,
data : publicKey,
};
break;
case "pkcs8":
keyData = convertStringToHex(convertArrayBufferViewToPlainText(keyData));
privateKey = convert(keyData,'auto');
if (privateKey.length!=4){
reject("DataError");
}
var alg = privateKey[0];
if (alg == "1.2.840.113549.1.1.1"){
}
//TODO : id-RSAES-OAEP
if (hash){
}
else {
hash = algorithm.hash.name;
}
var privateKey = convert(privateKey[2],'auto');
if (privateKey.length!=9){
reject("DataError");
}
privateKey.version = privateKey[0];
privateKey.n = privateKey[1];
privateKey.e = privateKey[2];
privateKey.d = privateKey[3];
privateKey.p = privateKey[4];
privateKey.q = privateKey[5];
privateKey.dmp1 = privateKey[6];
privateKey.dmq1 = privateKey[7];
privateKey.iqmp = privateKey[8];
key = {
type : "private",
extractable : extractable,
usages : keyUsages,
data : privateKey,
};
break;
case "jwk":
if (keyData.hasOwnProperty('d')){
type = "private";
}
else {
if (!keyData.hasOwnProperty('n') || !keyData.hasOwnProperty('e')){
reject("Public Key not valid");
}
type = "public";
}
break;
default:
reject("NotSupportedError");
}
var keyAlgorithm = {
name : hash
};
console.log();
var RsaHashedKeyAlgorithm = {
name : "RSA-OAEP",
modulusLength : publicKey.n.length,
publicExponent : publicKey.e,
hash : keyAlgorithm
};
key.algorithm = RsaHashedKeyAlgorithm;
resolve(key);
break;
default:
break;
}
});
};