-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportKey.js
executable file
·150 lines (124 loc) · 3.45 KB
/
exportKey.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
/*
Promise<any> exportKey(KeyFormat format, Key key);
*/
var exportKey = function(format,key){
var exportKeypromise = new Promise(function(resolve,reject){
if (!key){
reject("Key should be provided");
}
if (!key.algorithm){
reject('Key does not have an algorithm');
}
else if (!key.algorithm.name){
reject('Algorithm name not provided');
}
if (!format){
reject("format should not be null");
}
var algo = key.algorithm.name;
//Checking if the algo name in present in the suggested algorithms
if (exportKeyalgos.indexOf(algo)==-1){
//Not correct. Check how to reject a DOMException
reject(new DOMException(DOMException.NOT_SUPPORTED_ERR,"The algorithm is not supported"));
}
//Checking if the format is one of the recognized key format values
if (!checkRecognizedKeyFormatValues(format)){
reject ("SyntaxError");
}
if (!key.hasOwnProperty("extractable")){
reject("extractable attribute of key should be provided");
}
if (!key.extractable){
reject("InvalidAccessError");
}
switch(algo){
case "HMAC":
var data;
switch(format){
case "raw":
//TODO : Let data be the raw octets of the key represented by key. What are raw octets?
if (!key.hasOwnProperty("data")){
reject ("keyData is not provided to key");
}
data = key.data;
break;
}
//TODO : Return a new ArrayBuffer containing data.
resolve(data);
break;
case "AES-CBC":
var data;
switch(format){
case "raw":
//TODO : Let data be the raw octets of the key represented by key. What are raw octets?
if (!key.hasOwnProperty("data")){
reject ("keyData is not provided to key");
}
data = key.data;
break;
}
//TODO : Return a new ArrayBuffer containing data.
resolve(data);
break;
case "RSA-OAEP":
case "RSASSA-PKCS1-v1_5":
var result;
switch(format){
case "spki":
if (!key.hasOwnProperty("type")){
reject ("Type of key not provided");
}
else if (key.type != "public"){
reject("InvalidAccessError");
}
if (!key.algorithm.hash.hasOwnProperty(name)){
reject("Name attribute of hash algorithm is missing");
}
var temp = key.algorithm.hash.name;
var name;
if (temp == "SHA-1"){
name = "id-sha1";
}
else if (temp == "SHA-256"){
name = "id-sha256";
}
else if (temp == "SHA-384"){
name = "id-sha384";
}
else if (temp == "SHA-512"){
name = "id-sha512";
}
var hashAlgo = {
oid : name,
params : null
};
var maskAlgorithm = {
oid : "id-mgf1",
params : null
};
var subjectPublicKeyAlgorithm = {
oid : "id-RSAES-OAEP",
params : {
hashAlgorithm : hashAlgo,
MaskGenAlgorithm : maskAlgorithm
},
};
var subjectPublicKeyInfo = {
algorithm : subjectPublicKeyAlgorithm,
subjectPublicKey : key.data
};
result = subjectPublicKeyInfo;
break;
case "pkcs8":
if (!key.hasOwnProperty("type")){
reject ("Type of key not provided");
}
else if (key.type != "private"){
reject("InvalidAccessError");
}
}
break;
}
});
return exportKeypromise;
};