-
Notifications
You must be signed in to change notification settings - Fork 0
/
importKey.js
executable file
·219 lines (171 loc) · 5.16 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
/*
Promise<any> importKey(KeyFormat format, CryptoOperationData keyData, AlgorithmIdentifier? algorithm,
boolean extractable, KeyUsage[] keyUsages );
*/
var algorithms = require('./algorithms');
var functions = require('./functions');
var aes = require('./DJCL/src/aes').aes;
var encoding = require('./DJCL/src/encoding').encoding;
var hashing = require('./DJCL/src/hashing').hashing;
var rsa = require('./DJCL/src/rsa').rsa;
var Q = require('q');
exports.importKey = function(format,keyData,algorithm,extractable,keyUsages){
var importKeypromise = new Q.Promise(function(resolve,reject){
if (!algorithm){
reject('Algorithm not provided');
}
else if (!algorithm.name){
reject('Algorithm name not provided');
}
var algo = algorithm.name;
//Checking if the algo name in present in the suggested algorithms
if (algorithms.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 (!functions.checkRecognizedKeyFormatValues(format)){
reject ("SyntaxError");
}
//Checking if the any of the value of keyUsages are in the Recognized Key Usage Values
if (!functions.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 = functions.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
};
console.log(key);
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 = functions.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":
if (!algorithm.hasOwnProperty("hash")){
reject("SyntaxError");
}
var hash;
var type;
if (!algorithm.hash.hasOwnProperty("name")){
reject ("DataError");
}
else {
hash = algorithm.hash.name;
}
switch(format){
case "spki":
//TODO : What is all the stuff about parsing?
if (!keyData.hasOwnProperty('n') || !keyData.hasOwnProperty('e')){
reject("Public Key not valid");
}
type = "public";
break;
case "pkcs8":
//TODO : What is all the stuff about parsing?
type = "private";
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
};
var RsaHashedKeyAlgorithm = {
name : "RSA-OAEP",
modulusLength : keyData.n,
publicExponent : keyData.e,
hash : keyAlgorithm
};
var key = {
type : type,
extractable : extractable,
algorithm : RsaHashedKeyAlgorithm,
usages : keyUsages,
data : keyData
};
resolve(key);
break;
case "RSASSA-PKCS1-v1_5":
break;
}
});
return importKeypromise;
};