@@ -298,107 +298,29 @@ // If certificates were rejected by the API, log an error, for example. console.error(rejectedCerts.length + ' certificates were rejected.'); return; } +/* + * Handle for the CACKey NaCl Target + */ var cackeyHandle = null; - -function cackeyInitLoaded(messageEvent) { - console.log("Loaded CACKey PNaCl Module"); - - /* Register listeners with Chrome */ - chrome.certificateProvider.onCertificatesRequested.addListener(cackeyListCertificates); - chrome.certificateProvider.onSignDigestRequested.addListener(cackeySignMessage); -} - -function cackeyInit() { - var elementEmbed; - - /* Verify that we can register callbacks */ - if (!chrome.certificateProvider) { - if (!GoogleSmartCard.IS_DEBUG_BUILD) { - console.error("This extension only works on ChromeOS!"); - - return; - } else { - console.log("This extension only works on ChromeOS, but you appear to be debugging it -- trying anyway."); - } - } - - if (cackeyHandle != null) { - return; - } - - elementEmbed = document.createElement('embed'); - elementEmbed.type = "application/x-pnacl"; - elementEmbed.width = 0; - elementEmbed.height = 0; - elementEmbed.src = "cackey.nmf"; - elementEmbed.id = "cackeyModule"; - elementEmbed.addEventListener('error', function(messageEvent) { console.error("Error loading CACKey PNaCl Module: " + messageEvent.data); }, true); - elementEmbed.addEventListener('load', cackeyInitLoaded, true); - elementEmbed.addEventListener('message', function(messageEvent) { console.log("Start message"); console.log(messageEvent.data); console.log("End message"); }, true); - - cackeyHandle = elementEmbed; - - document.body.appendChild(cackeyHandle) - - new GoogleSmartCard.PcscNacl(cackeyHandle); -} - -function cackeyListCertificates(chromeCallback) { +var cackeyOutstandingCallback = null +var cackeyOutstandingCallbackId = -1; + +function cackeyMessageIncomingListCertificates(message, chromeCallback) { + var idx; var certificates = []; - certificates.push( - { - certificate: new Uint8Array([ - 0x30, 0x82, 0x02, 0x9a, 0x30, 0x82, 0x01, 0x82, 0x02, 0x09, 0x00, 0xf3, 0xa7, 0xc0, 0xfd, 0x81, - 0x0f, 0x11, 0x57, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, - 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, 0x54, - 0x65, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x34, 0x31, 0x38, 0x34, - 0x34, 0x31, 0x36, 0x5a, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x33, 0x30, 0x35, 0x31, 0x38, 0x34, 0x34, - 0x31, 0x36, 0x5a, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, - 0x54, 0x65, 0x73, 0x74, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, - 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, - 0x02, 0x82, 0x01, 0x01, 0x00, 0xc5, 0x25, 0xec, 0xac, 0x61, 0xa2, 0x01, 0xdf, 0x2e, 0x27, 0x49, - 0x67, 0xbb, 0xfd, 0xca, 0x1a, 0x35, 0xbe, 0xc2, 0xf0, 0x39, 0xfb, 0x7b, 0x14, 0x3a, 0x1f, 0x76, - 0xf4, 0x64, 0x75, 0x7b, 0xb0, 0x3c, 0xdf, 0xeb, 0x0a, 0xa0, 0xb9, 0xe7, 0x1f, 0x76, 0xcb, 0xa2, - 0x2a, 0xbd, 0x28, 0x5c, 0x96, 0xc6, 0x5d, 0x57, 0x8e, 0xb9, 0x9b, 0x33, 0x11, 0x7f, 0x2d, 0x3a, - 0x20, 0x9a, 0xa0, 0x45, 0x57, 0xda, 0xec, 0x66, 0x5c, 0x43, 0xf8, 0x69, 0xe6, 0xf7, 0x95, 0xcd, - 0x4b, 0x36, 0xc3, 0x9e, 0xe7, 0x68, 0xdb, 0xdf, 0xf9, 0x6a, 0x6a, 0xc0, 0x05, 0x89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, 0xc4, 0x69, 0x43, - 0x37, 0x93, 0xb4, 0x2f, 0x68, 0xc1, 0xc1, 0x5d, 0xfc, 0xe2, 0xac, 0xdc, 0x65, 0xdf, 0x08, 0x1c, - 0xc3, 0x56, 0x48, 0x6d, 0x92, 0xc4, 0x30, 0xc5, 0x96, 0x8c, 0x57, 0xae, 0x33, 0x2b, 0x88, 0x94, - 0x49, 0x3f, 0xd5, 0x6a, 0x29, 0x9a, 0x87, 0x03, 0x98, 0x71, 0x79, 0x3e, 0x27, 0x02, 0xbd, 0x8c, - 0x88, 0xff, 0xee, 0xe8, 0x10, 0x22, 0xd9, 0x8f, 0x8f, 0x6b, 0x8f, 0xe2, 0xdf, 0x5d, 0x9e, 0xe0, - 0xcb, 0x6a, 0xa0, 0x20, 0x28, 0xb6, 0x19, 0xa0, 0x13, 0xe4, 0xba, 0x4a, 0x51, 0x39 - ]).buffer, - supportedHashes: ['SHA1', 'SHA256'] - } - ); + for (idx = 0; idx < message.certificates.length; idx++) { + certificates.push( + { + certificate: message.certificates[idx], + supportedHashes: ['SHA1', 'SHA256'] + } + ); + } chromeCallback(certificates, function(rejectedCerts) { if (chrome.runtime.lastError) { return; @@ -409,29 +331,169 @@ } return; } ); +} + +function cackeyMessageIncomingSignMessage(message, chromeCallback) { + var payload; + + payload = message.signedData; + + chromeCallback(payload); +} + +function cackeyMessageIncoming(messageEvent) { + var nextFunction = null; + + if (messageEvent.data.target != "cackey") { + return; + } + + console.log("START MESSAGE"); + console.log(messageEvent.data); + console.log("END MESSAGE"); + + if (messageEvent.data.id != cackeyOutstandingCallbackId || cackeyOutstandingCallback == null) { + console.log("[cackey] Discarding outdated message"); + + return; + } + + if (messageEvent.data.status != "success") { + console.error("[cackey] Failed to execute command '" + messageEvent.data.command + "': " + messageEvent.data.error); + + cackeyOutstandingCallback(); + } else { + switch (messageEvent.data.command) { + case "listcertificates": + nextFunction = cackeyMessageIncomingListCertificates; + + break; + case "sign": + nextFunction = cackeyMessageIncomingSignMessage; + + break; + } + } + + if (nextFunction != null) { + nextFunction(messageEvent.data, cackeyOutstandingCallback); + } + + cackeyOutstandingCallback = null; + + return; +} + +/* + * Finish performing initialization that must wait until we have loaded the CACKey module + */ +function cackeyInitLoaded(messageEvent) { + console.log("[cackey] Loaded CACKey PNaCl Module"); + + /* Register listeners with Chrome */ + if (chrome.certificateProvider) { + chrome.certificateProvider.onCertificatesRequested.addListener(cackeyListCertificates); + chrome.certificateProvider.onSignDigestRequested.addListener(cackeySignMessage); + } + + return; +} + +function cackeyInit() { + var elementEmbed; + + /* Log that we are operational */ + console.log("[cackey] cackeyInit(): Called."); + + /* Verify that we can register callbacks */ + if (!chrome.certificateProvider) { + if (!GoogleSmartCard.IS_DEBUG_BUILD) { + console.error("[cackey] This extension only works on ChromeOS!"); + + return; + } else { + console.log("[cackey] This extension only works on ChromeOS, but you appear to be debugging it -- trying anyway."); + } + } + + if (cackeyHandle != null) { + console.log("[cackey] cackeyInit(): Already initialized. Returning."); + + return; + } + + elementEmbed = document.createElement('embed'); + elementEmbed.type = "application/x-pnacl"; + elementEmbed.width = 0; + elementEmbed.height = 0; + elementEmbed.src = "cackey.nmf"; + elementEmbed.id = "cackeyModule"; + elementEmbed.addEventListener('error', function(messageEvent) { console.error("Error loading CACKey PNaCl Module: " + messageEvent.data); }, true); + elementEmbed.addEventListener('load', cackeyInitLoaded, true); + elementEmbed.addEventListener('message', cackeyMessageIncoming, true); + + cackeyHandle = elementEmbed; + + /* + * Start the Google PCSC Interface + */ + new GoogleSmartCard.PcscNacl(cackeyHandle); + + document.body.appendChild(cackeyHandle) + + console.log("[cackey] cackeyInit(): Completed. Returning."); +} + +function cackeyListCertificates(chromeCallback) { + var callbackId; + + console.log("[cackey] Asked to provide a list of certificates -- throwing that request over to the NaCl side... "); + + callbackId = cackeyOutstandingCallbackId + 1; + + cackeyHandle.postMessage( + { + 'target': "cackey", + 'command': "listcertificates", + 'id': callbackId + } + ); + + cackeyOutstandingCallbackId = callbackId; + cackeyOutstandingCallback = chromeCallback; + + console.log("[cackey] Thrown."); return; } function cackeySignMessage(signRequest, chromeCallback) { - var key, rsaObj; - var payload; - - key = "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAxSXsrGGiAd8uJ0lnu/3KGjW+wvA5+3sUOh929GR1e7A83+sK\noLnnH3bLoiq9KFyWxl1XjrmbMxF/LTogmqBFV9rsZlxD+Gnm95XNSzbDnudo29/5\namrABYnQUOkeU+ENy3/kD8IkuX0M98mdb0/PRgQQWLrwxckiurYL4uazQzgrBq8w\nrs8idOz5rMWSB4hYJmyMV2XnTNaTFodIay439QjqRK+tattOsQDywa0won+vt0EG\n5l5JOgQlUFapIIJ6DU8XNvHmLnxncVjG9CDZbIOQm7/F92LJX23+VgHwn1b11CAp\nbQaTkKXQpEtBpyX8VaGt5nDffVEIOWWL+6uqWQIDAQABAoIBAQCQeLw5jsqECKLv\n01EbaF3kWyJSBGzZJ1apSUmIyt11KK9TgBCnu9PtMMcFLcC0hTVQZDK3urhK8jV6\nVTBzX6OxQBMg59g/gIvrP3HT/mvYVMh46GcEXvoclON9h3fcC/5p9GySR7lV3JfW\nNX8ThS7lpjC8dTHgHwc+QA3x1xHi3UnsamUmd42ENyKDLVbJTRU2Cw2AZLHkXqm+\nI8esn8236cF0UT1/Lws11k2wLZ8CO/ZGV57v7U2pSUr8HPduGKMyviwxf/eWD9zh\n7EyawKB9fXtzsSqaXyn/YVigfUm4XVGRXIMC2gNh4ps3b8udqitYp9BI3QqRdmyg\n8JDjF08hAoGBAPRh/3Sie6zbE0J3DZQ84QnKO9WjS4g1ffyw2vv/obH48Xk2yXOF\nR/m7TAb4C5k/lRMpx8icrra4llNUrL56anME8g9fQ4WP50Q1vW306r3S2OeVjngx\n1ByUWUAQtEAApH9dJ9Lz6rP+aGfebj1YAcfwJaZWHnJB0fL0SeXuEUh9AoGBAM6F\nG3GhajL7eAE0luZw24f0dVnW5iyYdQe32YieuVcGZ0jPHRaNvM+xpnIylKn9jCn6\neJuFDhgNKrgaAUDIfEwDDsDdMSYXTH1BKl8RaeGSiN7w0yqiyGFiyf0xMo6Mr3Iy\nXebxNYLzMKnjr+etRzshNLl4F+bDjlwShAULI6wNAoGAHT/td2RLMTE3aOke8vMm\nbCmMZ9GXpMHiRnBUhXzfOZH2NO/dxLnbF4TXYK4nlTwK97Q0eUpnju8g7vBfjJzS\nSNn6ruRUDJRgtcVI2vRJdFbcJzu7RZ+RJ6qJ1CUrrc1l4uifb5xKJyj4DhaNU0JJ\nVEjl+LauBjbijhW3EpIf93ECgYBIEV8wWhnLTMHl+conBH83hHFnNn0AVvuQA/Jf\noLQg0FU+rTgDZNbcy6u/8jK614YQKB3qd5YhgobD1B9X0h6+D9sYJpg+KKdClnkI\nz/TZCN/GnNez6zZt9aFzP7Xa1PFssnLeYpI/7kmvH4uFF4zh74aMqoH8v4VwT+3O\nLML4CQKBgAy5japY35yjgbLQEOd3b/t+3EBAqg7up2edLugfDCjAjhGHygseVclG\nY4RywzvDtbxz5j6BsSrMDFGPy34bp/5fQnWuucnXrGC3R6wYtbN1kKt0zvAC/kQL\nk+e8Hm6hN8kedKQpAn2jx0/19hDKsPgx1vjFhgiQGW9nLWujrzbA\n-----END RSA PRIVATE KEY-----\n"; - - rsaObj = new RSAKey(); - rsaObj.readPrivateKeyFromPEMString(key); - - payload = rsaObj.signWithMessageHash(BAtohex(new Uint8Array(signRequest.digest)), signRequest.hash.toLowerCase()); - - payload = new Uint8Array(stoBA(hextorstr(payload))).buffer; - - chromeCallback(payload); + var callbackId; + + console.log("[cackey] Asked to sign a message -- throwing that request over to the NaCl side... "); + + callbackId = cackeyOutstandingCallbackId + 1; + + cackeyHandle.postMessage( + { + 'target': "cackey", + 'command': "sign", + 'id': callbackId, + 'certificate': signRequest.certificate, + 'data': signRequest.digest /* XXX:TODO: This needs to be prefixed based on the signRequest.hash */ + } + ); + + cackeyOutstandingCallbackId = callbackId; + cackeyOutstandingCallback = chromeCallback; + + console.log("[cackey] Thrown."); + return; } /* Initialize CACKey */ cackeyInit();