Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / cryptotoken / usbsignhandler.js
blobd06b529708ec5e3f1ea677945cd0cf220ebfe8cb
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.
5 /**
6 * @fileoverview Implements a sign handler using USB gnubbies.
7 */
8 'use strict';
10 var CORRUPT_sign = false;
12 /**
13 * @param {!SignHelperRequest} request The sign request.
14 * @constructor
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_ = [];
29 /**
30 * Default timeout value in case the caller never provides a valid timeout.
31 * @const
33 UsbSignHandler.DEFAULT_TIMEOUT_MILLIS = 30 * 1000;
35 /**
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) {
42 if (this.cb_) {
43 // Can only handle one request.
44 return false;
46 /** @private {RequestHandlerCallback} */
47 this.cb_ = cb;
48 if (!this.request_.signData || !this.request_.signData.length) {
49 // Fail a sign request with an empty set of challenges.
50 return false;
52 var timeoutMillis =
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),
61 timeoutMillis,
62 this.request_.logMsgUrl);
63 return this.signer_.doSign(this.request_.signData);
67 /**
68 * Called when a MultipleGnubbySigner completes.
69 * @param {boolean} anyPending Whether any gnubbies are pending.
70 * @private
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_);
77 } else {
78 // Do nothing: signerFoundGnubby_ will have returned results from other
79 // gnubbies.
83 /**
84 * Called when a MultipleGnubbySigner finds a gnubby that has completed signing
85 * its challenges.
86 * @param {MultipleSignerResult} signResult Signer result object
87 * @param {boolean} moreExpected Whether the signer expects to produce more
88 * results.
89 * @private
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
105 // the caller.
106 this.notifyError_(signResult.code);
107 } else {
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;
115 /** @const */
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
123 /** @const */
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,
133 0xD3
136 /** @const */
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.
148 * @private
150 UsbSignHandler.prototype.sendBogusEnroll_ = function(gnubby) {
151 var self = this;
152 gnubby.version(function(rc, opt_data) {
153 if (rc) {
154 self.notifyError_(rc);
155 return;
157 var enrollChallenge;
158 var version = UTIL_BytesToString(new Uint8Array(opt_data || []));
159 switch (version) {
160 case Gnubby.U2F_V1:
161 enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V1;
162 break;
163 case Gnubby.U2F_V2:
164 enrollChallenge = UsbSignHandler.BOGUS_CHALLENGE_V2;
165 break;
166 default:
167 self.notifyError_(DeviceStatusCodes.INVALID_DATA_STATUS);
169 gnubby.enroll(
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.
181 * @private
183 UsbSignHandler.prototype.enrollCallback_ = function(gnubby, code, infoArray) {
184 if (this.notified_)
185 return;
186 switch (code) {
187 case DeviceStatusCodes.WAIT_TOUCH_STATUS:
188 this.sendBogusEnroll_(gnubby);
189 return;
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
194 // by notifyError_.)
195 this.notifyError_(DeviceStatusCodes.WRONG_DATA_STATUS);
196 return;
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
205 * @private
207 UsbSignHandler.prototype.notifySuccess_ = function(gnubby, challenge, info) {
208 if (this.notified_)
209 return;
210 this.notified_ = true;
212 gnubby.closeWhenIdle();
213 this.close();
215 if (CORRUPT_sign) {
216 CORRUPT_sign = false;
217 info[info.length - 1] = info[info.length - 1] ^ 0xff;
219 var responseData = {
220 'appIdHash': B64_encode(challenge['appIdHash']),
221 'challengeHash': B64_encode(challenge['challengeHash']),
222 'keyHandle': B64_encode(challenge['keyHandle']),
223 'signatureData': B64_encode(info)
225 var reply = {
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
236 * @private
238 UsbSignHandler.prototype.notifyError_ = function(code) {
239 if (this.notified_)
240 return;
241 this.notified_ = true;
242 this.close();
243 var reply = {
244 'type': 'sign_helper_reply',
245 'code': code
247 this.cb_(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();
258 if (this.signer_) {
259 this.signer_.close();
260 this.signer_ = null;