Diff

Differences From Artifact [d4659c8b69]:

To Artifact [f7f7a0c55f]:


130
131
132
133
134
135
136
137
138
139
140
141
























142
143
144
145
146
147
148
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
...
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
...
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
...
393
394
395
396
397
398
399






400
401
402
403
404
405
406
407
408
409



410
411
412
413
414
415
416
...
427
428
429
430
431
432
433
434
435


436
437

438
439





440
441








442
443
444
445
446
447
448
...
458
459
460
461
462
463
464

465
466
467
468
469
470
471
...
505
506
507
508
509
510
511

512





513
514
515
516
517
518
519
}

function cackeySSHAgentEncodeArray(input) {
	var result;

	result = cackeySSHAgentEncodeInt(input.length);
	input.forEach(function(element) {
		result = result.concat(element);
	});

	return(result);
}

























function cackeySSHAgentEncodeToUTF8Array(str) {
	var utf8 = [];

	if (typeof(str) === "string") {
		str = str.split("").map(function(c) {
			return(c.charCodeAt(0));
................................................................................
			buffer = buffer.join("");
			break;
	}

	return(buffer);
}

function cackeySSHAgentEncodeCertToKeyAndID(cert, keyType) {
	var result = null, resultKey = null;
	var certObj, certBytes;
	var publicKey;

	certObj = new X509;
	if (!certObj) {
		return(result);
................................................................................

	certBytes = Array.from(new Uint8Array(cert));

	certObj.readCertHex(cackeySSHAgentEncodeBinaryToHex(certBytes));

	publicKey = certObj.getPublicKey();

	switch (keyType) {
		case "ssh":
			switch (publicKey.type) {
				case "RSA":
					resultKey = cackeySSHAgentEncodeString("ssh-rsa");
					resultKey = resultKey.concat(cackeySSHAgentEncodeBigInt(publicKey.e));
					resultKey = resultKey.concat(cackeySSHAgentEncodeBigInt(publicKey.n));
					break;
				default:
					console.log("[cackeySSH] Unsupported public key type:", keyType, "/", publicKey.type, "-- ignoring.");
					break;
			}
			break;
		case "x509v3-sign":
			resultKey = certBytes;
			break;
		case "x509v3-ssh":
................................................................................
				case "RSA":
					resultKey = cackeySSHAgentEncodeString("x509v3-ssh-rsa");

					/*
					 * Array of certificates
					 */
					resultKey = resultKey.concat(cackeySSHAgentEncodeArray([
						cackeySSHAgentEncodeLV(certBytes)
					]));

					/*
					 * Array of OCSP responses
					 */
					resultKey = resultKey.concat(cackeySSHAgentEncodeArray([]));
					break;
				default:
					console.log("[cackeySSH] Unsupported public key type:", keyType, "/", publicKey.type, "-- ignoring.");
					break;
			}
			break;
		default:
			console.log("[cackeySSH] Unsupported SSH key type:", keyType, "-- ignoring.");
			break;
	}

	if (resultKey) {




























		result = {
			id: certObj.getSubjectString(),

			publicKeyType: publicKey.type,
			sshKeyType: keyType,
			key: resultKey
		};
	}

	return(result);
}




/*
 * Command Handlers
 */
async function cackeySSHAgentCommandRequestIdentity(request) {
	var response;
	var certs = [];
................................................................................
	 */
	response = [];

	response.push(cackeySSHAgentMessage.SSH_AGENT_IDENTITIES_ANSWER);
	response = response.concat(cackeySSHAgentEncodeInt(keys.length));
	keys.forEach(function(key) {
		response = response.concat(cackeySSHAgentEncodeLV(key.key));
		response = response.concat(cackeySSHAgentEncodeString("CACKey: " + key.id));
	});

	return(response);
}

async function cackeySSHAgentCommandSignRequest(request) {
	var keyInfo, data, flags;
	var certs, certToUse, certToUseType;
	var hashMethod, signedData, signedDataHeader, signRequest;

	var response;
	var flagMeaning = {
		SSH_AGENT_RSA_SHA2_256: 2,
		SSH_AGENT_RSA_SHA2_512: 4


	};






	/*
	 * Strip off the command
	 */
	request = request.slice(1);

	/*
	 * Get certificate to sign using
................................................................................
		case "RSA":
			if ((flags & flagMeaning.SSH_AGENT_RSA_SHA2_512) == flagMeaning.SSH_AGENT_RSA_SHA2_512) {
				hashMethod = "SHA512";
				data = await crypto.subtle.digest("SHA-512", new Uint8Array(data));
			} else if ((flags & flagMeaning.SSH_AGENT_RSA_SHA2_256) == flagMeaning.SSH_AGENT_RSA_SHA2_256) {
				hashMethod = "SHA256";
				data = await crypto.subtle.digest("SHA-256", new Uint8Array(data));






			} else if (flags == 0) {
				hashMethod = "SHA1";
				data = await crypto.subtle.digest("SHA-1", new Uint8Array(data));
			} else {
				console.info("[cackeySSH] Sign request with flags set to", flags, "which is unsupported, failing the request.");

				return(null);
			}

			switch (hashMethod) {



				case "SHA1":
					signedDataHeader = cackeySSHAgentEncodeString("ssh-rsa");
					break;
				case "SHA256":
					signedDataHeader = cackeySSHAgentEncodeString("rsa-sha2-256");
					break;
				case "SHA512":
................................................................................
			console.info("[cackeySSH] Unsupported public key type:", certToUseType, "failing the request.");

			return(null);
			break;
	}

	/*
	 * Sign the data
	 */


	signRequest = {
		hash: hashMethod,

		digest: new Uint8Array(data)
	};





	signedData = await cackeySignMessage(signRequest);
	signedData = Array.from(new Uint8Array(signedData));









	/*
	 * Encode signature
	 */
	signedData = signedDataHeader.concat(cackeySSHAgentEncodeLV(signedData));

	/*
................................................................................

/*
 * Session handling
 */
async function cackeySSHAgentHandleMessage(socket, request) {
	var sshRequestID, sshRequest, response, sshResponse;
	var sshHandlerError;


	if (!request.type || request.type !== "auth-agent@openssh.com") {
		return;
	}

	if (!request.data || request.data.length < 1) {
		return;
................................................................................
		data: response
	};

	if (goog.DEBUG) {
		console.log("[cackeySSH] Response: ", sshResponse);
	}


	socket.postMessage(sshResponse);






	return;
}

function cackeySSHAgentAcceptConnection(socket) {
	if (!socket) {
		return;







|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|








|







 







|








|




|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
>

|






>
>
>







 







|









>
|


|
>
>


>
>
>
>
>







 







>
>
>
>
>
>










>
>
>







 







|

>
>
|
|
>
|
|
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>







 







>







 







>
|
>
>
>
>
>







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
...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
...
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
...
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
...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
}

function cackeySSHAgentEncodeArray(input) {
	var result;

	result = cackeySSHAgentEncodeInt(input.length);
	input.forEach(function(element) {
		result = result.concat(cackeySSHAgentEncodeLV(element));
	});

	return(result);
}

function cackeySSHAgentDecodeArray(input) {
	var items, info;
	var itemCount;

	info = cackeySSHAgentDecodeInt(input);
	input = info.output;
	itemCount = info.value;

	items = [];
	while (itemCount > 0) {
		itemCount--;

		info = cackeySSHAgentDecodeLV(input);
		input = info.output;
		items.push(info.value);
	}

	return({
		value: items,
		output: input
	});
}


function cackeySSHAgentEncodeToUTF8Array(str) {
	var utf8 = [];

	if (typeof(str) === "string") {
		str = str.split("").map(function(c) {
			return(c.charCodeAt(0));
................................................................................
			buffer = buffer.join("");
			break;
	}

	return(buffer);
}

function cackeySSHAgentEncodeCertToKeyAndID(cert, sshKeyType) {
	var result = null, resultKey = null;
	var certObj, certBytes;
	var publicKey;

	certObj = new X509;
	if (!certObj) {
		return(result);
................................................................................

	certBytes = Array.from(new Uint8Array(cert));

	certObj.readCertHex(cackeySSHAgentEncodeBinaryToHex(certBytes));

	publicKey = certObj.getPublicKey();

	switch (sshKeyType) {
		case "ssh":
			switch (publicKey.type) {
				case "RSA":
					resultKey = cackeySSHAgentEncodeString("ssh-rsa");
					resultKey = resultKey.concat(cackeySSHAgentEncodeBigInt(publicKey.e));
					resultKey = resultKey.concat(cackeySSHAgentEncodeBigInt(publicKey.n));
					break;
				default:
					console.log("[cackeySSH] Unsupported public key type:", sshKeyType, "/", publicKey.type, "-- ignoring.");
					break;
			}
			break;
		case "x509v3-sign":
			resultKey = certBytes;
			break;
		case "x509v3-ssh":
................................................................................
				case "RSA":
					resultKey = cackeySSHAgentEncodeString("x509v3-ssh-rsa");

					/*
					 * Array of certificates
					 */
					resultKey = resultKey.concat(cackeySSHAgentEncodeArray([
						certBytes
					]));

					/*
					 * Array of OCSP responses
					 */
					resultKey = resultKey.concat(cackeySSHAgentEncodeArray([]));
					break;
				default:
					console.log("[cackeySSH] Unsupported public key type:", sshKeyType, "/", publicKey.type, "-- ignoring.");
					break;
			}
			break;
		default:
			console.log("[cackeySSH] Unsupported SSH key type:", sshKeyType, "-- ignoring.");
			break;
	}

	if (resultKey) {
		var certLabel;
		var certSAN;
		var ignoreException;

		/*
		 * Set a default label
		 */
		certLabel = certObj.getSubjectString();

		/*
		 * Try to find a better label from the certificate's
		 * Subject Alternative Name (SAN) extensions
		 */
		try {
			certSAN = certObj.getExtSubjectAltName2();
			certSAN.forEach(function(itemPair) {
				var itemType, itemValue;

				itemType = itemPair[0];
				itemValue = itemPair[1];

				if (itemType === "MAIL") {
					certLabel = itemValue;
				}
			});
		} catch (ignoreException) {
		}

		result = {

			label: certLabel,
			publicKeyType: publicKey.type,
			sshKeyType: sshKeyType,
			key: resultKey
		};
	}

	return(result);
}

function cackeySSHAgentDecodeCert(requestArray) {
}

/*
 * Command Handlers
 */
async function cackeySSHAgentCommandRequestIdentity(request) {
	var response;
	var certs = [];
................................................................................
	 */
	response = [];

	response.push(cackeySSHAgentMessage.SSH_AGENT_IDENTITIES_ANSWER);
	response = response.concat(cackeySSHAgentEncodeInt(keys.length));
	keys.forEach(function(key) {
		response = response.concat(cackeySSHAgentEncodeLV(key.key));
		response = response.concat(cackeySSHAgentEncodeString(key.label));
	});

	return(response);
}

async function cackeySSHAgentCommandSignRequest(request) {
	var keyInfo, data, flags;
	var certs, certToUse, certToUseType;
	var hashMethod, signedData, signedDataHeader, signRequest;
	var decryptedData, decryptRequest;
	var operation, response;
	var flagMeaning = {
		SSH_AGENT_RSA_SHA2_256: 2,
		SSH_AGENT_RSA_SHA2_512: 4,
		SSH_AGENT_RSA_RAW:      0x40000000,
		SSH_AGENT_RSA_DECRYPT:  0x80000000
	};

	/*
	 * Default mode is signing
	 */
	operation = "sign";

	/*
	 * Strip off the command
	 */
	request = request.slice(1);

	/*
	 * Get certificate to sign using
................................................................................
		case "RSA":
			if ((flags & flagMeaning.SSH_AGENT_RSA_SHA2_512) == flagMeaning.SSH_AGENT_RSA_SHA2_512) {
				hashMethod = "SHA512";
				data = await crypto.subtle.digest("SHA-512", new Uint8Array(data));
			} else if ((flags & flagMeaning.SSH_AGENT_RSA_SHA2_256) == flagMeaning.SSH_AGENT_RSA_SHA2_256) {
				hashMethod = "SHA256";
				data = await crypto.subtle.digest("SHA-256", new Uint8Array(data));
			} else if (flags == (flagMeaning.SSH_AGENT_RSA_RAW | flagMeaning.SSH_AGENT_RSA_DECRYPT)) {
				operation = "decrypt";
				data = new Uint8Array(data);
			} else if (flags == flagMeaning.SSH_AGENT_RSA_RAW) {
				hashMethod = "RAW";
				data = new Uint8Array(data);
			} else if (flags == 0) {
				hashMethod = "SHA1";
				data = await crypto.subtle.digest("SHA-1", new Uint8Array(data));
			} else {
				console.info("[cackeySSH] Sign request with flags set to", flags, "which is unsupported, failing the request.");

				return(null);
			}

			switch (hashMethod) {
				case "RAW":
					signedDataHeader = cackeySSHAgentEncodeString("rsa");
					break;
				case "SHA1":
					signedDataHeader = cackeySSHAgentEncodeString("ssh-rsa");
					break;
				case "SHA256":
					signedDataHeader = cackeySSHAgentEncodeString("rsa-sha2-256");
					break;
				case "SHA512":
................................................................................
			console.info("[cackeySSH] Unsupported public key type:", certToUseType, "failing the request.");

			return(null);
			break;
	}

	/*
	 * Sign or decrypt the data
	 */
	switch (operation) {
		case "sign":
			signRequest = {
				hash: hashMethod,
				certificate: certToUse.certificate,
				digest: new Uint8Array(data)
			};

			if (goog.DEBUG) {
				console.log("[cackeySSH] Requesting CACKey sign message:", signRequest);
			}

			signedData = await cackeySignMessage(signRequest);
			signedData = Array.from(new Uint8Array(signedData));
			break;
		case "decrypt":
			/* XXX:TODO: Incomplete ! */
			decryptRequest = {
				data: data
			}
			break;
	}

	/*
	 * Encode signature
	 */
	signedData = signedDataHeader.concat(cackeySSHAgentEncodeLV(signedData));

	/*
................................................................................

/*
 * Session handling
 */
async function cackeySSHAgentHandleMessage(socket, request) {
	var sshRequestID, sshRequest, response, sshResponse;
	var sshHandlerError;
	var postMessageException;

	if (!request.type || request.type !== "auth-agent@openssh.com") {
		return;
	}

	if (!request.data || request.data.length < 1) {
		return;
................................................................................
		data: response
	};

	if (goog.DEBUG) {
		console.log("[cackeySSH] Response: ", sshResponse);
	}

	try {
		socket.postMessage(sshResponse);
	} catch (postMessageException) {
		if (goog.DEBUG) {
			console.log("[cackeySSH] Failed to send response", postMessageException);
		}
	}

	return;
}

function cackeySSHAgentAcceptConnection(socket) {
	if (!socket) {
		return;