forked from webauthn-open-source/fido2-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
303 lines (250 loc) · 7.03 KB
/
utils.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"use strict";
var psl = require("psl");
var { URL } = require("url");
class Fido2LibError extends Error {
constructor(message, type) {
super();
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = type;
}
}
/* istanbul ignore next */
function printHex(msg, buf) {
// if the buffer was a TypedArray (e.g. Uint8Array), grab its buffer and use that
if (ArrayBuffer.isView(buf) && buf.buffer instanceof ArrayBuffer) {
buf = buf.buffer;
}
// check the arguments
if ((typeof msg != "string") ||
(typeof buf != "object")) {
console.log("Bad args to printHex"); // eslint-disable-line no-console
return;
}
if (!(buf instanceof ArrayBuffer)) {
console.log("Attempted printHex with non-ArrayBuffer:", buf); // eslint-disable-line no-console
return;
}
// print the buffer as a 16 byte long hex string
var arr = new Uint8Array(buf);
var len = buf.byteLength;
var i, str = "";
console.log(msg, `(${buf.byteLength} bytes)`); // eslint-disable-line no-console
for (i = 0; i < len; i++) {
var hexch = arr[i].toString(16);
hexch = (hexch.length == 1) ? ("0" + hexch) : hexch;
str += hexch.toUpperCase() + " ";
if (i && !((i + 1) % 16)) {
console.log(str); // eslint-disable-line no-console
str = "";
}
}
// print the remaining bytes
if ((i) % 16) {
console.log(str); // eslint-disable-line no-console
}
}
function coerceToBase64(thing, name) {
if (!name) {
throw new TypeError("name not specified in coerceToBase64");
}
// Array to Uint8Array
if (Array.isArray(thing)) {
thing = Uint8Array.from(thing);
}
// Uint8Array, etc. to ArrayBuffer
if (typeof thing === "object" &&
thing.buffer instanceof ArrayBuffer &&
!(thing instanceof Buffer)) {
thing = thing.buffer;
}
// ArrayBuffer to Buffer
if (thing instanceof ArrayBuffer && !(thing instanceof Buffer)) {
thing = new Buffer(thing);
}
// Buffer to base64 string
if (thing instanceof Buffer) {
thing = thing.toString("base64");
}
if (typeof thing !== "string") {
throw new Error(`could not coerce '${name}' to string`);
}
return thing;
}
function coerceToBase64Url(thing, name) {
thing = coerceToBase64(thing, name);
// base64 to base64url
// NOTE: "=" at the end of challenge is optional, strip it off here so that it's compatible with client
thing = thing.replace(/\+/g, "-").replace(/\//g, "_").replace(/=*$/g, "");
return thing;
}
function coerceToArrayBuffer(buf, name) {
if (!name) {
throw new TypeError("name not specified in coerceToArrayBuffer");
}
if (typeof buf === "string") {
// base64url to base64
buf = buf.replace(/-/g, "+").replace(/_/g, "/");
// base64 to Buffer
buf = Buffer.from(buf, "base64");
}
// Buffer or Array to Uint8Array
if (buf instanceof Buffer || Array.isArray(buf)) {
buf = new Uint8Array(buf);
}
// Uint8Array to ArrayBuffer
if (buf instanceof Uint8Array) {
buf = buf.buffer;
}
// error if none of the above worked
if (!(buf instanceof ArrayBuffer)) {
throw new TypeError(`could not coerce '${name}' to ArrayBuffer`);
}
return buf;
}
function ab2str(buf) {
var str = "";
new Uint8Array(buf).forEach((ch) => {
str += String.fromCharCode(ch);
});
return str;
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function isBase64Url(str) {
return !!str.match(/^[A-Za-z0-9\-_]+={0,2}$/);
}
function checkOrigin(str) {
var originUrl = new URL(str);
var origin = originUrl.origin;
if (origin !== str) {
throw new Error("origin was malformatted");
}
if (originUrl.protocol !== "https:") {
throw new Error("origin should be https");
}
if (!psl.isValid(originUrl.hostname) && originUrl.hostname !== "localhost") {
throw new Error("origin is not a valid eTLD+1");
}
return origin;
}
function bufEqual(a, b) {
var len = a.length;
if (len !== b.length) {
return false;
}
for (var i = 0; i < len; i++) {
if (a.readUInt8(i) !== b.readUInt8(i)) {
return false;
}
}
return true;
}
function abEqual(a, b) {
var len = a.byteLength;
if (len !== b.byteLength) {
return false;
}
a = new Uint8Array(a);
b = new Uint8Array(b);
for (let i = 0; i < len; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
function isPem(pem) {
if (typeof pem !== "string") {
return false;
}
var pemRegex = /^-----BEGIN .+-----$\n([A-Za-z0-9+/=]|\n)*^-----END .+-----$/m;
return !!pem.match(pemRegex);
}
function pemToBase64(pem) {
if (!isPem(pem)) {
throw new Error("expected PEM string as input");
}
var pemArr = pem.split("\n");
// remove first and last lines
pemArr = pemArr.slice(1, pemArr.length - 2);
return pemArr.join("");
}
function isPositiveInteger(n) {
return n >>> 0 === parseFloat(n);
}
function abToBuf(ab) {
return Buffer.from(new Uint8Array(ab));
}
function abToPem(type, ab) {
if (typeof type !== "string") {
throw new Error("abToPem expected 'type' to be string like 'CERTIFICATE', got: " + type);
}
var str = coerceToBase64(ab, "pem buffer");
return [
`-----BEGIN ${type}-----\n`,
...str.match(/.{1,64}/g).map((s) => s + "\n"),
`-----END ${type}-----\n`
].join("");
}
function arrayBufferEquals(b1, b2) {
if (!(b1 instanceof ArrayBuffer) ||
!(b2 instanceof ArrayBuffer)) {
console.log("not array buffers");
return false;
}
if (b1.byteLength !== b2.byteLength) {
console.log("not same length");
return false;
}
b1 = new Uint8Array(b1);
b2 = new Uint8Array(b2);
for (let i = 0; i < b1.byteLength; i++) {
if (b1[i] !== b2[i]) return false;
}
return true;
}
function abToInt(ab) {
if (!(ab instanceof ArrayBuffer)) {
throw new Error("abToInt: expected ArrayBuffer");
}
var buf = new Uint8Array(ab);
var cnt = ab.byteLength - 1;
var ret = 0;
buf.forEach((byte) => {
ret |= (byte << (cnt * 8));
cnt--;
});
return ret;
}
function b64ToJsObject(b64, desc) {
return JSON.parse(ab2str(coerceToArrayBuffer(b64, desc)));
}
module.exports = {
printHex,
Fido2LibError,
coerceToBase64,
coerceToBase64Url,
coerceToArrayBuffer,
ab2str,
str2ab,
isBase64Url,
checkOrigin,
bufEqual,
abEqual,
isPem,
pemToBase64,
isPositiveInteger,
abToBuf,
abToPem,
abToInt,
arrayBufferEquals,
b64ToJsObject
};