test ecdsa support, spaces for whitespace

This commit is contained in:
AJ ONeal 2018-11-09 21:21:40 -07:00
parent 2cc5a41268
commit a7462db2c8
1 changed files with 56 additions and 30 deletions

View File

@ -14,6 +14,32 @@
var PromiseA = window.Promise; var PromiseA = window.Promise;
var crypto = window.crypto; var crypto = window.crypto;
function testEcdsaSupport() {
var opts = {
type: 'ECDSA'
, bitlength: '256'
};
return BACME.accounts.generateKeypair(opts).then(function (jwk) {
return crypto.subtle.importKey(
"jwk"
, jwk
, { name: "ECDSA", namedCurve: "P-256" }
, true
, ["sign"]
).then(function (privateKey) {
return window.crypto.subtle.exportKey("pkcs8", privateKey);
});
});
}
testEcdsaSupport().then(function () {
console.log("supports ECDSA");
BROWSER_SUPPORTS_ECDSA = true;
}).catch(function () {
console.log("DOES NOT supports ECDSA");
BROWSER_SUPPORTS_ECDSA = false;
});
// TODO test RSA support
var apiUrl = 'https://acme-{{env}}.api.letsencrypt.org/directory'; var apiUrl = 'https://acme-{{env}}.api.letsencrypt.org/directory';
function updateApiType() { function updateApiType() {
console.log("type updated"); console.log("type updated");
@ -497,36 +523,36 @@
"data:text/octet-stream;base64," + window.btoa(certs); "data:text/octet-stream;base64," + window.btoa(certs);
// https://stackoverflow.com/questions/40314257/export-webcrypto-key-to-pem-format // https://stackoverflow.com/questions/40314257/export-webcrypto-key-to-pem-format
function spkiToPEM(keydata){ function spkiToPEM(keydata){
var keydataS = arrayBufferToString(keydata); var keydataS = arrayBufferToString(keydata);
var keydataB64 = window.btoa(keydataS); var keydataB64 = window.btoa(keydataS);
var keydataB64Pem = formatAsPem(keydataB64); var keydataB64Pem = formatAsPem(keydataB64);
return keydataB64Pem; return keydataB64Pem;
} }
function arrayBufferToString( buffer ) { function arrayBufferToString( buffer ) {
var binary = ''; var binary = '';
var bytes = new Uint8Array( buffer ); var bytes = new Uint8Array( buffer );
var len = bytes.byteLength; var len = bytes.byteLength;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] ); binary += String.fromCharCode( bytes[ i ] );
} }
return binary; return binary;
} }
function formatAsPem(str) { function formatAsPem(str) {
var finalString = '-----BEGIN ' + pemName + ' PRIVATE KEY-----\n'; var finalString = '-----BEGIN ' + pemName + ' PRIVATE KEY-----\n';
while(str.length > 0) { while(str.length > 0) {
finalString += str.substring(0, 64) + '\n'; finalString += str.substring(0, 64) + '\n';
str = str.substring(64); str = str.substring(64);
} }
finalString = finalString + '-----END ' + pemName + ' PRIVATE KEY-----'; finalString = finalString + '-----END ' + pemName + ' PRIVATE KEY-----';
return finalString; return finalString;
} }
var wcOpts; var wcOpts;
var pemName; var pemName;
@ -543,23 +569,23 @@
, namedCurve: "P-256" , namedCurve: "P-256"
}; };
} }
return crypto.subtle.importKey( return crypto.subtle.importKey(
"jwk" "jwk"
, info.serverJwk , info.serverJwk
, wcOpts , wcOpts
, true , true
, ["sign"] , ["sign"]
).then(function (privateKey) { ).then(function (privateKey) {
return window.crypto.subtle.exportKey("pkcs8", privateKey); return window.crypto.subtle.exportKey("pkcs8", privateKey);
}).then (function (keydata) { }).then (function (keydata) {
var pem = spkiToPEM(keydata); var pem = spkiToPEM(keydata);
$qs('#js-privkey').innerHTML = pem; $qs('#js-privkey').innerHTML = pem;
$qs("#js-download-privkey-link").href = $qs("#js-download-privkey-link").href =
"data:text/octet-stream;base64," + window.btoa(pem); "data:text/octet-stream;base64," + window.btoa(pem);
steps[i](); steps[i]();
}).catch(function(err){ }).catch(function(err){
console.error(err.toString()); console.error(err.toString());
}); });
}); });
}); });
}; };