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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Implements a sign handler using USB gnubbies.
*/
'use strict';
var CORRUPT_sign = false;
/**
* @param {!SignHelperRequest} request The sign request.
* @constructor
* @implements {RequestHandler}
*/
function UsbSignHandler(request) {
/** @private {!SignHelperRequest} */
this.request_ = request;
/** @private {boolean} */
this.notified_ = false;
/** @private {boolean} */
this.anyGnubbiesFound_ = false;
/** @private {!Array<!Gnubby>} */
this.notEnrolledGnubbies_ = [];
}
/**
* Default timeout value in case the caller never provides a valid timeout.
* @const
*/
UsbSignHandler.DEFAULT_TIMEOUT_MILLIS = 30 * 1000;
/**
* Attempts to run this handler's request.
* @param {RequestHandlerCallback} cb Called with the result of the request and
* an optional source for the sign result.
* @return {boolean} whether this set of challenges was accepted.
*/
UsbSignHandler.prototype.run = function(cb) {
if (this.cb_) {
// Can only handle one request.
return false;
}
/** @private {RequestHandlerCallback} */
this.cb_ = cb;
if (!this.request_.signData || !this.request_.signData.length) {
// Fail a sign request with an empty set of challenges.
return false;
}
var timeoutMillis =
this.request_.timeoutSeconds ?
this.request_.timeoutSeconds * 1000 :
UsbSignHandler.DEFAULT_TIMEOUT_MILLIS;
/** @private {MultipleGnubbySigner} */
this.signer_ = new MultipleGnubbySigner(
false /* forEnroll */,
this.signerCompleted_.bind(this),
this.signerFoundGnubby_.bind(this),
timeoutMillis,
this.request_.logMsgUrl);
return this.signer_.doSign(this.request_.signData);
};
/**
* Called when a MultipleGnubbySigner completes.
* @param {boolean} anyPending Whether any gnubbies are pending.
* @private
*/
UsbSignHandler.prototype.signerCompleted_ = function(anyPending) {
if (!this.anyGnubbiesFound_ || anyPending) {
this.notifyError_(DeviceStatusCodes.TIMEOUT_STATUS);
} else if (this.signerError_ !== undefined) {
this.notifyError_(this.signerError_);
} else {
// Do nothing: signerFoundGnubby_ will have returned results from other
// gnubbies.
}
};
/**
* Called when a MultipleGnubbySigner finds a gnubby that has completed signing
* its challenges.
* @param {MultipleSignerResult} signResult Signer result object
* @param {boolean} moreExpected Whether the signer expects to produce more
* results.
* @private
*/
UsbSignHandler.prototype.signerFoundGnubby_ =
function(signResult, moreExpected) {
this.anyGnubbiesFound_ = true;
if (!signResult.code) {
var gnubby = signResult['gnubby'];
var challenge = signResult['challenge'];
var info = new Uint8Array(signResult['info']);
this.notifySuccess_(gnubby, challenge, info);
} else if (SingleGnubbySigner.signErrorIndicatesInvalidKeyHandle(
signResult.code)) {
var gnubby = signResult['gnubby'];
this.notEnrolledGnubbies_.push(gnubby);
this.sendBogusEnroll_(gnubby);
} else if (!moreExpected) {
// If the signer doesn't expect more results, return the error directly to
// the caller.
this.notifyError_(signResult.code);
} else {
// Record the last error, to report from the complete callback if no other
// eligible gnubbies are found.
/** @private {number} */
this.signerError_ = signResult.code;
}
};
/** @const */
UsbSignHandler.BOGUS_APP_ID_HASH = [
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
];
/** @const */
UsbSignHandler.BOGUS_CHALLENGE_V1 = [
0x04, 0xA2, 0x24, 0x7D, 0x5C, 0x0B, 0x76, 0xF1,
0xDC, 0xCD, 0x44, 0xAF, 0x91, 0x9A, 0xA2, 0x3F,
0x3F, 0xBA, 0x65, 0x9F, 0x06, 0x78, 0x82, 0xFB,
0x93, 0x4B, 0xBF, 0x86, 0x55, 0x95, 0x66, 0x46,
0x76, 0x90, 0xDC, 0xE1, 0xE8, 0x6C, 0x86, 0x86,
0xC3, 0x03, 0x4E, 0x65, 0x52, 0x4C, 0x32, 0x6F,
0xB6, 0x44, 0x0D, 0x50, 0xF9, 0x16, 0xC0, 0xA3,
0xDA, 0x31, 0x4B, 0xD3, 0x3F, 0x94, 0xA5, 0xF1,
0xD3
];
/** @const */
UsbSignHandler.BOGUS_CHALLENGE_V2 = [
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42
];
/**
* Sends a bogus enroll command to the not-enrolled gnubby, to force the user
* to tap the gnubby before revealing its state to the caller.
* @param {Gnubby} gnubby The gnubby to "enroll" on.
* @private
*/
UsbSignHandler.prototype.sendBogusEnroll_ = function(gnubby) {
var self = this;
gnubby.version(function(rc, opt_data) {
if (rc) {
self.notifyError_(rc);
return;
}
var enrollChallenge;
var version = UTIL_BytesToString(new Uint8Array(opt_data || []));
switch (version) {
case Gnubby.U2F_V1:
enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V1;
break;
case Gnubby.U2F_V2:
enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V2;
break;
default:
self.notifyError_(DeviceStatusCodes.INVALID_DATA_STATUS);
}
gnubby.enroll(
/** @type {Array<number>} */ (enrollChallenge),
UsbSignHandler.BOGUS_APP_ID_HASH,
self.enrollCallback_.bind(self, gnubby));
});
};
/**
* Called with the result of the (bogus, tap capturing) enroll command.
* @param {Gnubby} gnubby The gnubby "enrolled".
* @param {number} code The result of the enroll command.
* @param {ArrayBuffer=} infoArray Returned data.
* @private
*/
UsbSignHandler.prototype.enrollCallback_ = function(gnubby, code, infoArray) {
if (this.notified_)
return;
switch (code) {
case DeviceStatusCodes.WAIT_TOUCH_STATUS:
this.sendBogusEnroll_(gnubby);
return;
case DeviceStatusCodes.OK_STATUS:
// Got a successful enroll => user tapped gnubby.
// Send a WRONG_DATA_STATUS finally. (The gnubby is implicitly closed
// by notifyError_.)
this.notifyError_(DeviceStatusCodes.WRONG_DATA_STATUS);
return;
}
};
/**
* Reports the result of a successful sign operation.
* @param {Gnubby} gnubby Gnubby instance
* @param {SignHelperChallenge} challenge Challenge signed
* @param {Uint8Array} info Result data
* @private
*/
UsbSignHandler.prototype.notifySuccess_ = function(gnubby, challenge, info) {
if (this.notified_)
return;
this.notified_ = true;
gnubby.closeWhenIdle();
this.close();
if (CORRUPT_sign) {
CORRUPT_sign = false;
info[info.length - 1] = info[info.length - 1] ^ 0xff;
}
var responseData = {
'appIdHash': B64_encode(challenge['appIdHash']),
'challengeHash': B64_encode(challenge['challengeHash']),
'keyHandle': B64_encode(challenge['keyHandle']),
'signatureData': B64_encode(info)
};
var reply = {
'type': 'sign_helper_reply',
'code': DeviceStatusCodes.OK_STATUS,
'responseData': responseData
};
this.cb_(reply, 'USB');
};
/**
* Reports error to the caller.
* @param {number} code error to report
* @private
*/
UsbSignHandler.prototype.notifyError_ = function(code) {
if (this.notified_)
return;
this.notified_ = true;
this.close();
var reply = {
'type': 'sign_helper_reply',
'code': code
};
this.cb_(reply);
};
/**
* Closes the MultipleGnubbySigner, if any.
*/
UsbSignHandler.prototype.close = function() {
while (this.notEnrolledGnubbies_.length != 0) {
var gnubby = this.notEnrolledGnubbies_.shift();
gnubby.closeWhenIdle();
}
if (this.signer_) {
this.signer_.close();
this.signer_ = null;
}
};
|