1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @fileoverview Implements a sign handler using USB gnubbies.
10 var CORRUPT_sign = false;
13 * @param {!SignHelperRequest} request The sign request.
15 * @implements {RequestHandler}
17 function UsbSignHandler(request) {
18 /** @private {!SignHelperRequest} */
19 this.request_ = request;
21 /** @private {boolean} */
22 this.notified_ = false;
23 /** @private {boolean} */
24 this.anyGnubbiesFound_ = false;
25 /** @private {!Array<!Gnubby>} */
26 this.notEnrolledGnubbies_ = [];
30 * Default timeout value in case the caller never provides a valid timeout.
33 UsbSignHandler.DEFAULT_TIMEOUT_MILLIS = 30 * 1000;
36 * Attempts to run this handler's request.
37 * @param {RequestHandlerCallback} cb Called with the result of the request and
38 * an optional source for the sign result.
39 * @return {boolean} whether this set of challenges was accepted.
41 UsbSignHandler.prototype.run = function(cb) {
43 // Can only handle one request.
46 /** @private {RequestHandlerCallback} */
48 if (!this.request_.signData || !this.request_.signData.length) {
49 // Fail a sign request with an empty set of challenges.
53 this.request_.timeoutSeconds ?
54 this.request_.timeoutSeconds * 1000 :
55 UsbSignHandler.DEFAULT_TIMEOUT_MILLIS;
56 /** @private {MultipleGnubbySigner} */
57 this.signer_ = new MultipleGnubbySigner(
58 false /* forEnroll */,
59 this.signerCompleted_.bind(this),
60 this.signerFoundGnubby_.bind(this),
62 this.request_.logMsgUrl);
63 return this.signer_.doSign(this.request_.signData);
68 * Called when a MultipleGnubbySigner completes.
69 * @param {boolean} anyPending Whether any gnubbies are pending.
72 UsbSignHandler.prototype.signerCompleted_ = function(anyPending) {
73 if (!this.anyGnubbiesFound_ || anyPending) {
74 this.notifyError_(DeviceStatusCodes.TIMEOUT_STATUS);
75 } else if (this.signerError_ !== undefined) {
76 this.notifyError_(this.signerError_);
78 // Do nothing: signerFoundGnubby_ will have returned results from other
84 * Called when a MultipleGnubbySigner finds a gnubby that has completed signing
86 * @param {MultipleSignerResult} signResult Signer result object
87 * @param {boolean} moreExpected Whether the signer expects to produce more
91 UsbSignHandler.prototype.signerFoundGnubby_ =
92 function(signResult, moreExpected) {
93 this.anyGnubbiesFound_ = true;
94 if (!signResult.code) {
95 var gnubby = signResult['gnubby'];
96 var challenge = signResult['challenge'];
97 var info = new Uint8Array(signResult['info']);
98 this.notifySuccess_(gnubby, challenge, info);
99 } else if (signResult.code == DeviceStatusCodes.WRONG_DATA_STATUS) {
100 var gnubby = signResult['gnubby'];
101 this.notEnrolledGnubbies_.push(gnubby);
102 this.sendBogusEnroll_(gnubby);
103 } else if (!moreExpected) {
104 // If the signer doesn't expect more results, return the error directly to
106 this.notifyError_(signResult.code);
108 // Record the last error, to report from the complete callback if no other
109 // eligible gnubbies are found.
110 /** @private {number} */
111 this.signerError_ = signResult.code;
116 UsbSignHandler.BOGUS_APP_ID_HASH = [
117 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
118 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
119 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
120 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
124 UsbSignHandler.BOGUS_CHALLENGE_V1 = [
125 0x04, 0xA2, 0x24, 0x7D, 0x5C, 0x0B, 0x76, 0xF1,
126 0xDC, 0xCD, 0x44, 0xAF, 0x91, 0x9A, 0xA2, 0x3F,
127 0x3F, 0xBA, 0x65, 0x9F, 0x06, 0x78, 0x82, 0xFB,
128 0x93, 0x4B, 0xBF, 0x86, 0x55, 0x95, 0x66, 0x46,
129 0x76, 0x90, 0xDC, 0xE1, 0xE8, 0x6C, 0x86, 0x86,
130 0xC3, 0x03, 0x4E, 0x65, 0x52, 0x4C, 0x32, 0x6F,
131 0xB6, 0x44, 0x0D, 0x50, 0xF9, 0x16, 0xC0, 0xA3,
132 0xDA, 0x31, 0x4B, 0xD3, 0x3F, 0x94, 0xA5, 0xF1,
137 UsbSignHandler.BOGUS_CHALLENGE_V2 = [
138 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
139 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
140 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
141 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42
145 * Sends a bogus enroll command to the not-enrolled gnubby, to force the user
146 * to tap the gnubby before revealing its state to the caller.
147 * @param {Gnubby} gnubby The gnubby to "enroll" on.
150 UsbSignHandler.prototype.sendBogusEnroll_ = function(gnubby) {
152 gnubby.version(function(rc, opt_data) {
154 self.notifyError_(rc);
158 var version = UTIL_BytesToString(new Uint8Array(opt_data || []));
161 enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V1;
164 enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V2;
167 self.notifyError_(DeviceStatusCodes.INVALID_DATA_STATUS);
170 /** @type {Array<number>} */ (enrollChallenge),
171 UsbSignHandler.BOGUS_APP_ID_HASH,
172 self.enrollCallback_.bind(self, gnubby));
177 * Called with the result of the (bogus, tap capturing) enroll command.
178 * @param {Gnubby} gnubby The gnubby "enrolled".
179 * @param {number} code The result of the enroll command.
180 * @param {ArrayBuffer=} infoArray Returned data.
183 UsbSignHandler.prototype.enrollCallback_ = function(gnubby, code, infoArray) {
187 case DeviceStatusCodes.WAIT_TOUCH_STATUS:
188 this.sendBogusEnroll_(gnubby);
191 case DeviceStatusCodes.OK_STATUS:
192 // Got a successful enroll => user tapped gnubby.
193 // Send a WRONG_DATA_STATUS finally. (The gnubby is implicitly closed
195 this.notifyError_(DeviceStatusCodes.WRONG_DATA_STATUS);
201 * Reports the result of a successful sign operation.
202 * @param {Gnubby} gnubby Gnubby instance
203 * @param {SignHelperChallenge} challenge Challenge signed
204 * @param {Uint8Array} info Result data
207 UsbSignHandler.prototype.notifySuccess_ = function(gnubby, challenge, info) {
210 this.notified_ = true;
212 gnubby.closeWhenIdle();
216 CORRUPT_sign = false;
217 info[info.length - 1] = info[info.length - 1] ^ 0xff;
220 'appIdHash': B64_encode(challenge['appIdHash']),
221 'challengeHash': B64_encode(challenge['challengeHash']),
222 'keyHandle': B64_encode(challenge['keyHandle']),
223 'signatureData': B64_encode(info)
226 'type': 'sign_helper_reply',
227 'code': DeviceStatusCodes.OK_STATUS,
228 'responseData': responseData
230 this.cb_(reply, 'USB');
234 * Reports error to the caller.
235 * @param {number} code error to report
238 UsbSignHandler.prototype.notifyError_ = function(code) {
241 this.notified_ = true;
244 'type': 'sign_helper_reply',
251 * Closes the MultipleGnubbySigner, if any.
253 UsbSignHandler.prototype.close = function() {
254 while (this.notEnrolledGnubbies_.length != 0) {
255 var gnubby = this.notEnrolledGnubbies_.shift();
256 gnubby.closeWhenIdle();
259 this.signer_.close();