This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypted-api.js
251 lines (195 loc) · 7.34 KB
/
encrypted-api.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
"use strict";
(function(root)
{
/*
* Initialization
*/
var encryptedApi = function(secret1, secret2, error_handler)
{
if (!(this instanceof encryptedApi))
throw Error('encryptedApi must be instanitated with `new`');
this.signature_length = 64;
this.iv_length = 16;
this.id_length = 32;
this.shared_secret_minimum_length = 32;
this.error_handler = error_handler;
var nodejsroot = require('window-or-global');
this.crypto = nodejsroot.crypto || nodejsroot.msCrypto;
if (!this.crypto)
return this.reportError('encryptedApi: no suitable crypto implementation found.');
this.aesjs = require('aes-js');
this.jssha = require('jssha');
this.ajax = require('ajax-request');
this.secret1 = this.aesjs.utils.utf8.toBytes(secret1);
this.secret2 = this.aesjs.utils.utf8.toBytes(secret2);
this.requests = new Array();
}
/*
* API call execution
*/
encryptedApi.prototype.executeCall = function(url, method, callback, data)
{
if (typeof data !== 'undefined' && typeof data !== 'object' && typeof data !== 'string')
return this.reportError('EncryptedApi executeCall data must be an object or a string.');
if (this.checkSharedSecrets())
return;
var id = this.generateRequestId();
var iv = this.generateIv();
this.requests[id] = true;
var data = {
'id': id,
'timestamp': Math.floor(Date.now() / 1000),
'data': typeof data === 'undefined' ? {} : data,
'url': url,
'method': method.toLowerCase(),
'headers': null,
};
data = JSON.stringify(data);
var secret1 = this.secret1.slice(0, this.shared_secret_minimum_length);
var aesCtr = new this.aesjs.ModeOfOperation.ctr(secret1, new this.aesjs.Counter(this.aesjs.utils.hex.toBytes(iv)));
var encryptedData = this.aesjs.utils.hex.fromBytes(aesCtr.encrypt(this.aesjs.utils.utf8.toBytes(data)));
var sha512 = new this.jssha('SHA-512', 'TEXT');
sha512.setHMACKey(this.secret2, 'ARRAYBUFFER');
sha512.update(encryptedData + iv);
data = {
'data': encryptedData,
'iv': iv,
'signature': sha512.getHMAC('HEX'),
};
this.checkDataFormat(data.data);
this.checkIvFormat(data.iv);
this.checkSignatureFormat(data.signature);
var request = {
'url': url,
'method': method.toUpperCase(),
'data': data,
'encoding': 'utf-8',
'headers': {
'Accept': 'application/json',
},
};
if (method.toLowerCase() == 'get') {
request.method = 'POST';
request.headers = {
'Accept': 'application/json',
'X-HTTP-Method-Override': 'GET',
};
}
var self = this;
this.ajax(request, function(error, response, body) {
if (error)
return self.reportError('encryptedApi request failed: ' + error);
if (!response)
return self.reportError('encryptedApi received no response from endpoint: ' + url);
if (response.headers['content-type'] !== 'application/json')
return self.reportError('encryptedApi response is not JSON, returned Content-Type: ' + response.headers['content-type'] + ', returned body: ' + body);
var json = self.parseJSON(body);
if (!json.data || !json.iv || !json.signature)
return self.reportError('encryptedApi response JSON is not in expected format: ' + body);
self.checkDataFormat(json.data);
self.checkIvFormat(json.iv);
self.checkSignatureFormat(json.signature);
var sha512 = new self.jssha('SHA-512', 'TEXT');
sha512.setHMACKey(self.secret2, 'ARRAYBUFFER');
sha512.update(json.data + json.iv);
var signature = sha512.getHMAC('HEX');
if (signature !== json.signature)
return self.reportError('encryptedApi response signature is invalid, expected ' + signature + ', got ' + json.signature);
var secret1 = self.secret1.slice(0, self.shared_secret_minimum_length);
var aesCtr = new self.aesjs.ModeOfOperation.ctr(secret1, new self.aesjs.Counter(self.aesjs.utils.hex.toBytes(json.iv)));
var decryptedJson = self.parseJSON(self.aesjs.utils.utf8.fromBytes(aesCtr.decrypt(self.aesjs.utils.hex.toBytes(json.data))));
if (!decryptedJson.id || !decryptedJson.timestamp || !decryptedJson.data || !decryptedJson.headers)
return self.reportError('encryptedApi decrypted JSON is not in expected format: ' + JSON.stringify(decryptedJson));
if (!(decryptedJson.id in self.requests))
return self.reportError('enryptedApi received unknown request id: ' + id);
if (parseInt(decryptedJson.timestamp) < Math.floor(Date.now() / 1000) - 10)
return self.reportError('encryptedApi response timestamp invalid: ' + decryptedJson.timestamp);
if (callback instanceof Function)
callback(decryptedJson.data, decryptedJson);
delete self.requests[decryptedJson.id];
});
}
encryptedApi.prototype.generateRequestId = function()
{
return this.aesjs.utils.hex.fromBytes(this.generateRandomBytes(this.id_length));
}
encryptedApi.prototype.generateIv = function()
{
return this.aesjs.utils.hex.fromBytes(this.generateRandomBytes(this.iv_length));
}
encryptedApi.prototype.generateRandomBytes = function(length)
{
var bytes = new Uint8Array(length);
this.crypto.getRandomValues(bytes);
return bytes;
}
/*
* Sanity check functions
*/
encryptedApi.prototype.checkDataFormat = function(data)
{
return this.checkBinHexFormat(data);
}
encryptedApi.prototype.checkIvFormat = function(iv)
{
return this.checkBinHexFormat(iv, this.iv_length * 2);
}
encryptedApi.prototype.checkSignatureFormat = function(signature)
{
return this.checkBinHexFormat(signature, this.signature_length * 2);
}
encryptedApi.prototype.checkIdFormat = function(id)
{
return this.checkBinHexFormat(id, this.id_length * 2);
}
encryptedApi.prototype.checkSharedSecrets = function()
{
if (this.secret1.length < this.shared_secret_minimum_length || this.secret2.length < this.shared_secret_minimum_length)
return this.reportError('EncryptedApi shared secrets must be at least ' + this.shared_secret_minimum_length + ' bytes long.');
if (this.arraysAreEqual(this.secret1, this.secret2))
return this.reportError('EncryptedApi shared secrets must not be equal to each other.');
if (this.arraysAreEqual(this.secret1.slice(0, this.shared_secret_minimum_length), this.secret2.slice(0, this.shared_secret_minimum_length)))
return this.reportError('EncryptedApi shared secrets first 32 bytes must not be equal to each other.');
}
/*
* Helper functions
*/
encryptedApi.prototype.checkBinHexFormat = function(value, length)
{
if (value.match(new RegExp('^[\\da-f]' + (typeof length !== 'undefined' ? '{' + parseInt(length) + '}' : '+') + '$')) === null)
return this.reportError('EncryptedApi invalid BinHex format or invalid length.');
}
encryptedApi.prototype.arraysAreEqual = function(arr1, arr2)
{
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;)
if(arr1[i] !== arr2[i])
return false;
return true;
}
encryptedApi.prototype.reportError = function(message)
{
if (this.error_handler instanceof Function) {
this.error_handler(message);
return true;
}
throw Error(message);
}
encryptedApi.prototype.parseJSON = function(json)
{
var result;
try {
result = JSON.parse(json);
} catch (error) {
return this.reportError('JSON parsing failed: ' + error.message);
}
return result;
}
if (typeof exports !== 'undefined') // node.js
module.exports = encryptedApi;
else if (typeof(define) === 'function' && define.amd) // RequireJS / AMD
define(encryptedApi);
else // web browsers
root.encryptedApi = encryptedApi;
})(this);