Check-in [f486b04a4b]
Overview
Comment:Updated to support using the Chrome PIN dialog if doing a Chrome Certificate request
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f486b04a4b04933b565252dc65e0f18b54d4c1eb
User & Date: rkeene on 2019-06-05 20:40:48
Other Links: manifest | tags
Context
2019-06-05
21:35
Potentially better PIN handling check-in: e5a1e29b79 user: rkeene tags: trunk
20:40
Updated to support using the Chrome PIN dialog if doing a Chrome Certificate request check-in: f486b04a4b user: rkeene tags: trunk
2019-02-06
17:58
ChromeOS Release 11 check-in: 7288e80894 user: rkeene tags: trunk
Changes

Modified build/chrome/cackey.js from [d291048f9c] to [dc80ae3ba4].

175
176
177
178
179
180
181






















































182
183
184
185
186
187
188
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







				return;
			}

			cackeyCertificateToPINMapUpdateLastUsed(null);
		}, 900000);
	}
}

/*
 * Handle redispatching requests after receiving a PIN
 */
function cackeyPINPromptCompleted(pinValue) {
	var tmpMessageEvent;

	for (messageIdx = 0; messageIdx < cackeyMessagesToRetry.length; messageIdx++) {
		tmpMessageEvent = cackeyMessagesToRetry[messageIdx];

		if (pinValue == "") {
			if (goog.DEBUG) {
				console.log("[cackey] The PIN dialog was closed without gathering a PIN, treating it as a failure.");
			}

			tmpMessageEvent.data.status = "error";
			tmpMessageEvent.data.error = "PIN window closed without a PIN being provided";

			cackeyMessageIncoming(tmpMessageEvent);
		} else {
			tmpMessageEvent.data.originalrequest.pin = pinValue;

			cackeyCertificateToPINMap[cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate)] = {}
			cackeyCertificateToPINMap[cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate)].pin = pinValue;
			cackeyCertificateToPINMapUpdateLastUsed(cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate));

			chromeCallback = null;
			if (tmpMessageEvent.data.id) {
				if (cackeyOutstandingCallbacks) {
					chromeCallback = cackeyOutstandingCallbacks[tmpMessageEvent.data.id];
				}
			}

			cackeyInitPCSC(function() {
				cackeyHandle.postMessage(tmpMessageEvent.data.originalrequest);
			}, function() {
				if (chromeCallback) {
					chromeCallback();
				}

				if (tmpMessageEvent.data.id && cackeyOutstandingCallbacks[tmpMessageEvent.data.id]) {
					delete cackeyOutstandingCallbacks[tmpMessageEvent.data.id];
				}
			});
		}
	}

	/*
	 * Delete the existing handle and create a new one
	 */
	delete cackeyMessagesToRetry;

	cackeyMessagesToRetry = [];
}

/*
 * Handle an incoming message from the NaCl side and pass it off to
 * one of the handlers above for actual formatting and passing to
 * the callback
 *
 * If an error occured, invoke the callback with no arguments.
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
336
337
338
339
340
341
342
343
344
345
346
347
348

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370






































371
372
373
374
375
376
377
312
313
314
315
316
317
318
319






































320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359













































360






















361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405







+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








			/*
			 * Set the handle to an invalid (but non-null) value until the window
			 * is created in case we are invoked again soon.
			 */
			pinWindowPreviousHandle = "invalid";

			if (messageEvent.data.originalrequest.signRequestId === null) {
			chrome.app.window.create("pin.html", {
				"id": "cackeyPINEntry",
				"resizable": false,
				"alwaysOnTop": true,
				"focused": true,
				"visibleOnAllWorkspaces": true,
				"innerBounds": {
					"width": 350,
					"minWidth": 350,
					"height": 135,
					"minHeight": 135
				}
			}, function(pinWindow) {
				/*
				 * Set the PIN value to blank
				 */
				pinWindowPINValue = "";

				if (!pinWindow) {
					console.error("[cackey] No window was provided for PIN entry, this will not go well.");

					return;
				}

				pinWindowPreviousHandle = pinWindow;

				pinWindow.drawAttention();
				pinWindow.focus();

				/*
				 * Register a handler to handle the window being closed without
				 * having sent anything
				 */
				pinWindow.onClosed.addListener(function() {
					var messageIdx;
					var chromeCallback;

					pinWindowPreviousHandle = null;
				chrome.app.window.create("pin.html", {
					"id": "cackeyPINEntry",
					"resizable": false,
					"alwaysOnTop": true,
					"focused": true,
					"visibleOnAllWorkspaces": true,
					"innerBounds": {
						"width": 350,
						"minWidth": 350,
						"height": 135,
						"minHeight": 135
					}
				}, function(pinWindow) {
					/*
					 * Set the PIN value to blank
					 */
					pinWindowPINValue = "";
	
					if (!pinWindow) {
						console.error("[cackey] No window was provided for PIN entry, this will not go well.");
	
						return;
					}
	
					pinWindowPreviousHandle = pinWindow;
	
					pinWindow.drawAttention();
					pinWindow.focus();
	
					/*
					 * Register a handler to handle the window being closed without
					 * having sent anything
					 */
					pinWindow.onClosed.addListener(function() {
						var messageIdx;
						var chromeCallback;
						var pinValue;
	
						pinWindowPreviousHandle = null;

					for (messageIdx = 0; messageIdx < cackeyMessagesToRetry.length; messageIdx++) {
						var tmpMessageEvent;

						tmpMessageEvent = cackeyMessagesToRetry[messageIdx];

						if (pinWindowPINValue == "") {
							if (goog.DEBUG) {
								console.log("[cackey] The PIN dialog was closed without gathering a PIN, treating it as a failure.");
							}

							tmpMessageEvent.data.status = "error";
							tmpMessageEvent.data.error = "PIN window closed without a PIN being provided";

							cackeyMessageIncoming(tmpMessageEvent);
						} else {
							tmpMessageEvent.data.originalrequest.pin = pinWindowPINValue;

							cackeyCertificateToPINMap[cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate)] = {}
							cackeyCertificateToPINMap[cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate)].pin = pinWindowPINValue;

							cackeyCertificateToPINMapUpdateLastUsed(cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate));

							chromeCallback = null;
							if (tmpMessageEvent.data.id) {
								if (cackeyOutstandingCallbacks) {
									chromeCallback = cackeyOutstandingCallbacks[tmpMessageEvent.data.id];
								}
							}

							cackeyInitPCSC(function() {
								cackeyHandle.postMessage(tmpMessageEvent.data.originalrequest);
							}, function() {
								if (chromeCallback) {
									chromeCallback();
								}

								if (tmpMessageEvent.data.id && cackeyOutstandingCallbacks[tmpMessageEvent.data.id]) {
									delete cackeyOutstandingCallbacks[tmpMessageEvent.data.id];
								}
							});
						}
					}


					/*
						/*
					 * Delete the existing handle and create a new one
					 */
					delete cackeyMessagesToRetry;

					cackeyMessagesToRetry = [];

					/*
					 * We are done fetching the user PIN, clear the value
					 */
					pinWindowPINValue = "";

					return;
				})

				/*
				 * Pass this message off to the other window so that it may resubmit the request.
				 */
				pinWindow.contentWindow.parentWindow = window;
				pinWindow.contentWindow.messageEvent = messageEvent;

				return;
			});
						 * We are done fetching the user PIN, clear the value
						 */
						pinValue = pinWindowPINValue;
						pinWindowPINValue = "";

						cackeyPINPromptCompleted(pinValue);
	
						return;
					})
	
					/*
					 * Pass this message off to the other window so that it may resubmit the request.
					 */
					pinWindow.contentWindow.parentWindow = window;
					pinWindow.contentWindow.messageEvent = messageEvent;
	
					return;
				});
			} else {
				chrome.certificateProvider.requestPin({
					signRequestId: messageEvent.data.originalrequest.signRequestId,
					requestType: "PIN"
				}, function(userInfo) {
					chrome.certificateProvider.stopPinRequest({
						signRequestId: messageEvent.data.originalrequest.signRequestId
					}, function() {
						var pinValue = "";

						pinWindowPreviousHandle = null;

						if (userInfo && userInfo.userInput) {
							pinValue = userInfo.userInput;
						}

						return(cackeyPINPromptCompleted(pinValue));
					});
				});
			}

			/*
			 * We return here instead of break to avoid deleting the callback
			 * entry.
			 */
			return;
		case "success":
484
485
486
487
488
489
490




491
492
493
494
495
496
497
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529







+
+
+
+







 */
function cackeySignMessage(signRequest, chromeCallback) {
	var callbackId;
	var command;
	var certificateId;
	var digest, digestHeader;
	var promiseHandle = null, promiseResolve, promiseReject;

	if (signRequest.signRequestId === undefined || signRequest.signRequestId === null) {
		signRequest.signRequestId = null;
	}

	if (!chromeCallback) {
		/*
		 * If no callback supplied, arrange for a promise to be returned instead
		 */
		promiseHandle = new Promise(function(resolve, reject) {
			promiseResolve = resolve;
543
544
545
546
547
548
549

550
551
552
553
554
555
556
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589







+







	}

	callbackId = ++cackeyOutstandingCallbackCounter;

	command = {
		'target': "cackey",
		'command': "sign",
		'signRequestId': signRequest.signRequestId,
		'id': callbackId,
		'certificate': signRequest.certificate,
		'data': digest.buffer
	};

	certificateId = cackeyCertificateToPINID(command.certificate);