-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.js
More file actions
318 lines (287 loc) · 11.6 KB
/
wrapper.js
File metadata and controls
318 lines (287 loc) · 11.6 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
console.debug('navigator.credentials.get wrapper')
let WAD = {
lastGetRequest: null,
lastGetResponse: null,
lastCreateRequest: null,
lastCreateResponse: null,
base64ToArrayBuffer: function (base64) {
if(!base64){
return null
}
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
},
arrayBufferToBase64: async function (arrayBuff) {
if(!arrayBuff){
return null
}
var reader = new FileReader()
return new Promise(resolve => {
if (!arrayBuff){
resolve(null)
}
reader.onload = function() {
var dataUrl = reader.result
var base64 = dataUrl.split(',')[1]
resolve(base64);
}
reader.readAsDataURL(new Blob([arrayBuff]));
});
},
deserializeAssertion: function (assertion){
deserializeAssertion(assertion)
},
serializeAssertion: async function (assertion){
try{
if (!(
assertion.type &&
assertion.id &&
assertion.rawId &&
assertion.response &&
assertion.response.clientDataJSON)){
throw new Error('Invalid assertion: ', assertion)
}
let assertionJson = {
type: assertion.type,
id: assertion.id,
rawId: await arrayBufferToBase64(assertion.rawId),
response:{
clientDataJSON: JSON.parse(base64Decode(assertion.response.clientDataJSON)),
}
}
if (assertion.response.userHandle)
assertionJson.response.userHandle = await arrayBufferToBase64(assertion.response.userHandle)
if (assertion.response.authenticatorData)
assertionJson.response.authenticatorData = await arrayBufferToBase64(assertion.response.authenticatorData)
if (assertion.response.attestationObject)
assertionJson.response.attestationObject = CBOR.decode(assertion.response.attestationObject)
return assertionJson
}catch(e){
console.debug('Assertion: ', assertion)
console.error(e)
return null
}
},
decodeAndPrintAttestationResponse: async function (attestationResponse){
console.debug("Attestation Response: ", attestationResponse)
let decodedObject =JSON.parse(JSON.stringify(attestationResponse))
decodedObject.response.clientDataJSON = JSON.parse(base64Decode(attestationResponse.response.clientDataJSON))
const attestationBuffer = attestationResponse.response.attestationObject
decodedObject.response.attestationObject = CBOR.decode(attestationBuffer);
const authData = decodedObject.response.attestationObject.authData;
let authDataDecoded = {}
authDataDecoded.rpIdHash = bytesToHex(authData.slice(0, 32));
authDataDecoded.counter = new DataView(authData.slice(33, 37).buffer).getUint32(0, false); // Big-endian
const flags = authData[32]
const flagDetails = {
userPresent: (flags & 0x01) !== 0,
userVerified: (flags & 0x04) !== 0,
backupElegibility: (flags & 0x18) >> 3,
backupState: (flags & 0x20) !== 0,
attestationDataPesent: (flags & 0x40) !== 0,
extensionDataIncluded: (flags & 0x80) !== 0,
reserved1: (flags & 0x02) !== 0,
reserved2: (flags & 0x38) !== 0,
};
authDataDecoded.flags = flagDetails;
if (flagDetails.attestationDataPesent){
let attestedCredentialData = {}
attestedCredentialData.aaguid = bytesToHex(authData.slice(37, 37 + 16));
const credentialIdLength = new DataView(authData.slice(53, 53 + 2).buffer).getUint16(0, false);
attestedCredentialData.credentialId = bytesToHex(authData.slice(55, 55 + credentialIdLength));
const credentialPublicKey = CBOR.decode(authData.slice(55 + credentialIdLength).buffer);
attestedCredentialData.publicKey = parseCosePublicKey(credentialPublicKey);
authDataDecoded.attestedCredentialData = attestedCredentialData;
}
decodedObject.response.attestationObject.authData = authDataDecoded
console.debug("Attestation Response Decoded: ", decodedObject)
}
}
let originalNavCredGet=navigator.credentials.get
let originalNavCredCreate=navigator.credentials.create
function swapNavCred(){
navigator.credentials.get=navCredGetWrapper
navigator.credentials.create=navCredCreateWrapper
setTimeout(swapNavCred,500)
}
swapNavCred()
async function navCredGetExecuteAndSwap(webauthnReq){
navigator.credentials.get = originalNavCredGet
let promise = navigator.credentials.get(webauthnReq)
navigator.credentials.get=navCredGetWrapper
return promise
}
async function navCredCreateExecuteAndSwap(webauthnReq){
navigator.credentials.create = originalNavCredCreate
let promise = navigator.credentials.create(webauthnReq)
navigator.credentials.create=navCredCreateWrapper
return promise
}
async function navCredGetWrapper(webauthnReq){
console.debug("Get Request: ", webauthnReq)
WAD.lastGetRequest = webauthnReq
let response = await navCredGetExecuteAndSwap(webauthnReq)
//let assertionJson = await serializeAssertion(assertion)
WAD.lastGetResponse = response
console.debug('Get Response:', response)
return response
}
async function navCredCreateWrapper(webauthnReq){
console.debug("Create Request: ", webauthnReq)
WAD.lastCreateRequest = webauthnReq
let response = await navCredCreateExecuteAndSwap(webauthnReq)
//let assertionJson = await serializeAssertion(assertion)
WAD.lastCreateResponse = response
WAD.decodeAndPrintAttestationResponse (response)
return response
}
function parseCosePublicKey(publicKey) {
let parsedPublicKey = {};
parsedPublicKey.keyType = publicKey[1]; // kty (key type)
parsedPublicKey.algorithm = publicKey[3]; // alg (algorithm)
if (parsedPublicKey.keyType === 2) { // kty == 2 means EC2 key
parsedPublicKey.curve = publicKey[-1]; // crv (curve identifier)
parsedPublicKey.x = publicKey[-2]; // x-coordinate
parsedPublicKey.y = publicKey[-3]; // y-coordinate
} else if (parsedPublicKey.keyType === 3) { // RSA Key (kty == 3)
parsedPublicKey.modulus = publicKey[-1]; // n (modulus)
parsedPublicKey.exponent = publicKey[-2]; // e (exponent)
}
return parsedPublicKey;
}
async function serializeWebauthnRequest(request){
let jsonRequest = JSON.parse(JSON.stringify(request));
try{
if (!(request && request.publicKey && request.publicKey.challenge)){
throw new Error('Invalid webauthn request')
}
jsonRequest.publicKey.challenge = await arrayBufferToBase64(request.publicKey.challenge)
if (request.publicKey.allowCredentials && Array.isArray(request.publicKey.allowCredentials)){
for (let i=0; i<request.publicKey.allowCredentials.length; i++){
jsonRequest.publicKey.allowCredentials[i].id = await arrayBufferToBase64(request.publicKey.allowCredentials[i].id)
}
}
return jsonRequest
}catch(e){
console.error(e)
return null
}
}
function deserializeWebauthnRequest(request){
try{
if (!(request && request.publicKey && request.publicKey.challenge)){
throw new Error('Invalid webauthn request')
}
request.publicKey.challenge = base64ToArrayBuffer(request.publicKey.challenge)
if (request.publicKey.allowCredentials && Array.isArray(request.publicKey.allowCredentials)){
for (let i = 0; i < request.publicKey.allowCredentials.length; i++) {
request.publicKey.allowCredentials[i].id = base64ToArrayBuffer(request.publicKey.allowCredentials[i].id)
}
}
return request
}catch(e){
console.error(e)
return null
}
}
async function serializeAssertion(assertion){
try{
if (!(
assertion.type &&
assertion.id &&
assertion.rawId &&
assertion.response &&
assertion.response.clientDataJSON)){
throw new Error('Invalid assertion: ', assertion)
}
let assertionJson = {
type: assertion.type,
id: assertion.id,
rawId: await arrayBufferToBase64(assertion.rawId),
response:{
clientDataJSON: await arrayBufferToBase64(assertion.response.clientDataJSON),
userHandle: await arrayBufferToBase64(assertion.response.userHandle)
}
}
if (assertion.response.authenticatorData)
assertion.authenticatorData = await arrayBufferToBase64(assertion.response.authenticatorData)
if (assertion.response.attestationObject)
assertion.attestationObject = await arrayBufferToBase64(assertion.response.attestationObject)
return assertionJson
}catch(e){
console.debug('Assertion: ', assertion)
console.error(e)
return null
}
}
function deserializeAssertion(assertion){
try{
if (!(assertion.type &&
assertion.id &&
assertion.rawId &&
assertion.response &&
assertion.response.authenticatorData &&
assertion.response.clientDataJSON &&
assertion.response.signature)){
throw new Error('Invalid assertion: ', assertion)
}
assertion.response.authenticatorData = base64ToArrayBuffer(assertion.response.authenticatorData)
assertion.response.clientDataJSON = base64ToArrayBuffer(assertion.response.clientDataJSON)
assertion.response.signature = base64ToArrayBuffer(assertion.response.signature)
assertion.response.userHandle = base64ToArrayBuffer(assertion.response.userHandle)
assertion.rawId = base64ToArrayBuffer(assertion.rawId)
return assertion
}catch(e){
console.error(e)
return null
}
}
function base64ToArrayBuffer(base64) {
if(!base64){
return null
}
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
async function arrayBufferToBase64(arrayBuff) {
if(!arrayBuff){
return null
}
var reader = new FileReader()
return new Promise(resolve => {
if (!arrayBuff){
resolve(null)
}
reader.onload = function() {
var dataUrl = reader.result
var base64 = dataUrl.split(',')[1]
resolve(base64);
}
reader.readAsDataURL(new Blob([arrayBuff]));
});
}
function base64Decode(buffer){
if (buffer)
return String.fromCharCode.apply(null, new Uint8Array(buffer))
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
}
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
function bytesToHex(bytes) {
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
}