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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// 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 A multiple gnubby signer wraps the process of opening a number
* of gnubbies, signing each challenge in an array of challenges until a
* success condition is satisfied, and yielding each succeeding gnubby.
*/
'use strict';
/**
* @typedef {{
* code: number,
* gnubbyId: llGnubbyDeviceId,
* challenge: (SignHelperChallenge|undefined),
* info: (ArrayBuffer|undefined)
* }}
*/
var MultipleSignerResult;
/**
* Creates a new sign handler that manages signing with all the available
* gnubbies.
* @param {!GnubbyFactory} gnubbyFactory Used to create and open the gnubbies.
* @param {!CountdownFactory} timerFactory Used to create timers to reenumerate
* gnubbies.
* @param {boolean} forEnroll Whether this signer is signing for an attempted
* enroll operation.
* @param {function(boolean)} allCompleteCb Called when this signer completes
* sign attempts, i.e. no further results will be produced. The parameter
* indicates whether any gnubbies are present that have not yet produced a
* final result.
* @param {function(MultipleSignerResult, boolean)} gnubbyCompleteCb
* Called with each gnubby/challenge that yields a final result, along with
* whether this signer expects to produce more results. The boolean is a
* hint rather than a promise: it's possible for this signer to produce
* further results after saying it doesn't expect more, or to fail to
* produce further results after saying it does.
* @param {number} timeoutMillis A timeout value, beyond whose expiration the
* signer will not attempt any new operations, assuming the caller is no
* longer interested in the outcome.
* @param {string=} opt_logMsgUrl A URL to post log messages to.
* @constructor
*/
function MultipleGnubbySigner(gnubbyFactory, timerFactory, forEnroll,
allCompleteCb, gnubbyCompleteCb, timeoutMillis, opt_logMsgUrl) {
/** @private {!GnubbyFactory} */
this.gnubbyFactory_ = gnubbyFactory;
/** @private {!CountdownFactory} */
this.timerFactory_ = timerFactory;
/** @private {boolean} */
this.forEnroll_ = forEnroll;
/** @private {function(boolean)} */
this.allCompleteCb_ = allCompleteCb;
/** @private {function(MultipleSignerResult, boolean)} */
this.gnubbyCompleteCb_ = gnubbyCompleteCb;
/** @private {string|undefined} */
this.logMsgUrl_ = opt_logMsgUrl;
/** @private {Array.<SignHelperChallenge>} */
this.challenges_ = [];
/** @private {boolean} */
this.challengesSet_ = false;
/** @private {boolean} */
this.complete_ = false;
/** @private {number} */
this.numComplete_ = 0;
/** @private {!Object.<string, GnubbyTracker>} */
this.gnubbies_ = {};
/** @private {Countdown} */
this.timer_ = timerFactory.createTimer(
timeoutMillis, this.timeout_.bind(this));
/** @private {Countdown} */
this.reenumerateTimer_ = timerFactory.createTimer(timeoutMillis);
}
/**
* @typedef {{
* index: string,
* signer: SingleGnubbySigner,
* stillGoing: boolean,
* errorStatus: number
* }}
*/
var GnubbyTracker;
/**
* Closes this signer's gnubbies, if any are open.
*/
MultipleGnubbySigner.prototype.close = function() {
for (var k in this.gnubbies_) {
this.gnubbies_[k].signer.close();
}
this.reenumerateTimer_.clearTimeout();
this.timer_.clearTimeout();
if (this.reenumerateIntervalTimer_) {
this.reenumerateIntervalTimer_.clearTimeout();
}
};
/**
* Begins signing the given challenges.
* @param {Array.<SignHelperChallenge>} challenges The challenges to sign.
* @return {boolean} whether the challenges were successfully added.
*/
MultipleGnubbySigner.prototype.doSign = function(challenges) {
if (this.challengesSet_) {
// Can't add new challenges once they're finalized.
return false;
}
if (challenges) {
for (var i = 0; i < challenges.length; i++) {
var decodedChallenge = {};
var challenge = challenges[i];
decodedChallenge['challengeHash'] =
B64_decode(challenge['challengeHash']);
decodedChallenge['appIdHash'] = B64_decode(challenge['appIdHash']);
decodedChallenge['keyHandle'] = B64_decode(challenge['keyHandle']);
if (challenge['version']) {
decodedChallenge['version'] = challenge['version'];
}
this.challenges_.push(decodedChallenge);
}
}
this.challengesSet_ = true;
this.enumerateGnubbies_();
return true;
};
/**
* Enumerates gnubbies.
* @private
*/
MultipleGnubbySigner.prototype.enumerateGnubbies_ = function() {
this.gnubbyFactory_.enumerate(this.enumerateCallback_.bind(this));
};
/**
* Called with the result of enumerating gnubbies.
* @param {number} rc The return code from enumerating.
* @param {Array.<llGnubbyDeviceId>} ids The gnubbies enumerated.
* @private
*/
MultipleGnubbySigner.prototype.enumerateCallback_ = function(rc, ids) {
if (this.complete_) {
return;
}
if (rc || !ids || !ids.length) {
this.maybeReEnumerateGnubbies_(true);
return;
}
for (var i = 0; i < ids.length; i++) {
this.addGnubby_(ids[i]);
}
this.maybeReEnumerateGnubbies_(false);
};
/**
* How frequently to reenumerate gnubbies when none are found, in milliseconds.
* @const
*/
MultipleGnubbySigner.ACTIVE_REENUMERATE_INTERVAL_MILLIS = 200;
/**
* How frequently to reenumerate gnubbies when some are found, in milliseconds.
* @const
*/
MultipleGnubbySigner.PASSIVE_REENUMERATE_INTERVAL_MILLIS = 3000;
/**
* Reenumerates gnubbies if there's still time.
* @param {boolean} activeScan Whether to poll more aggressively, e.g. if
* there are no devices present.
* @private
*/
MultipleGnubbySigner.prototype.maybeReEnumerateGnubbies_ =
function(activeScan) {
if (this.reenumerateTimer_.expired()) {
// If the timer is expired, timeout_ will be called, and there's no
// additional work to do here.
return;
}
// Reenumerate more aggressively if there are no gnubbies present than if
// there are any.
var reenumerateTimeoutMillis;
if (activeScan) {
reenumerateTimeoutMillis =
MultipleGnubbySigner.ACTIVE_REENUMERATE_INTERVAL_MILLIS;
} else {
reenumerateTimeoutMillis =
MultipleGnubbySigner.PASSIVE_REENUMERATE_INTERVAL_MILLIS;
}
if (reenumerateTimeoutMillis >
this.reenumerateTimer_.millisecondsUntilExpired()) {
reenumerateTimeoutMillis =
this.reenumerateTimer_.millisecondsUntilExpired();
}
/** @private {Countdown} */
this.reenumerateIntervalTimer_ =
this.timerFactory_.createTimer(reenumerateTimeoutMillis,
this.enumerateGnubbies_.bind(this));
};
/**
* Adds a new gnubby to this signer's list of gnubbies. (Only possible while
* this signer is still signing: without this restriction, the completed
* callback could be called more than once, in violation of its contract.)
* If this signer has challenges to sign, begins signing on the new gnubby with
* them.
* @param {llGnubbyDeviceId} gnubbyId The id of the gnubby to add.
* @return {boolean} Whether the gnubby was added successfully.
* @private
*/
MultipleGnubbySigner.prototype.addGnubby_ = function(gnubbyId) {
var index = JSON.stringify(gnubbyId);
if (this.gnubbies_.hasOwnProperty(index)) {
// Can't add the same gnubby twice.
return false;
}
var tracker = {
index: index,
errorStatus: 0,
stillGoing: false,
signer: null
};
tracker.signer = new SingleGnubbySigner(
this.gnubbyFactory_,
gnubbyId,
this.forEnroll_,
this.signCompletedCallback_.bind(this, tracker),
this.timer_.clone(),
this.logMsgUrl_);
this.gnubbies_[index] = tracker;
this.gnubbies_[index].stillGoing =
tracker.signer.doSign(this.challenges_);
if (!this.gnubbies_[index].errorStatus) {
this.gnubbies_[index].errorStatus = 0;
}
return true;
};
/**
* Called by a SingleGnubbySigner upon completion.
* @param {GnubbyTracker} tracker The tracker object of the gnubby whose result
* this is.
* @param {SingleSignerResult} result The result of the sign operation.
* @private
*/
MultipleGnubbySigner.prototype.signCompletedCallback_ =
function(tracker, result) {
console.log(
UTIL_fmt(result.code ? 'failure.' : 'success!' +
' gnubby ' + tracker.index +
' got code ' + result.code.toString(16)));
if (!tracker.stillGoing) {
console.log(UTIL_fmt('gnubby ' + tracker.index + ' no longer running!'));
// Shouldn't ever happen? Disregard.
return;
}
tracker.stillGoing = false;
tracker.errorStatus = result.code;
var moreExpected = this.tallyCompletedGnubby_();
switch (result.code) {
case DeviceStatusCodes.GONE_STATUS:
// Squelch removed gnubbies: the caller can't act on them.
break;
default:
// Report any other results directly to the caller.
this.notifyGnubbyComplete_(tracker, result, moreExpected);
break;
}
};
/**
* Counts another gnubby has having completed, and returns whether more results
* are expected.
* @return {boolean} Whether more gnubbies are still running.
* @private
*/
MultipleGnubbySigner.prototype.tallyCompletedGnubby_ = function() {
this.numComplete_++;
return this.anyPending_();
};
/**
* @return {boolean} Whether more gnubbies are still running.
* @private
*/
MultipleGnubbySigner.prototype.anyPending_ = function() {
return this.numComplete_ < Object.keys(this.gnubbies_).length;
};
/**
* Called upon timeout.
* @private
*/
MultipleGnubbySigner.prototype.timeout_ = function() {
if (this.complete_) return;
this.complete_ = true;
// Defer notifying the caller that all are complete, in case the caller is
// doing work in response to a gnubbyFound callback and has an inconsistent
// view of the state of this signer.
var self = this;
var anyPending = this.anyPending_();
window.setTimeout(function() {
self.allCompleteCb_(anyPending);
}, 0);
};
/**
* @param {GnubbyTracker} tracker The tracker object of the gnubby whose result
* this is.
* @param {SingleSignerResult} result Result object.
* @param {boolean} moreExpected Whether more gnubbies may still produce an
* outcome.
* @private
*/
MultipleGnubbySigner.prototype.notifyGnubbyComplete_ =
function(tracker, result, moreExpected) {
console.log(UTIL_fmt('gnubby ' + tracker.index + ' complete (' +
result.code.toString(16) + ')'));
var signResult = {
'code': result.code,
'gnubby': result.gnubby,
'gnubbyId': tracker.signer.getDeviceId()
};
if (result['challenge'])
signResult['challenge'] = result['challenge'];
if (result['info'])
signResult['info'] = result['info'];
this.gnubbyCompleteCb_(signResult, moreExpected);
};
|