Make WebCrypto optional with config.useWebCrypto

This commit is contained in:
Tankred Hase 2014-10-01 11:17:48 +02:00
parent e6f66b9039
commit 72cb1cfc49
3 changed files with 25 additions and 19 deletions

View File

@ -134,12 +134,13 @@ function RSA() {
// Generate a new random private key B bits long, using public expt E // Generate a new random private key B bits long, using public expt E
function generate(B, E, callback) { function generate(B, E, callback) {
var webCrypto = util.getWebCrypto();
// //
// Native RSA keygen using Web Crypto // Native RSA keygen using Web Crypto
// //
if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) { if (webCrypto) {
var Euint32 = new Uint32Array([parseInt(E, 16)]); // get integer of exponent var Euint32 = new Uint32Array([parseInt(E, 16)]); // get integer of exponent
var Euint8 = new Uint8Array(Euint32.buffer); // get bytes of exponent var Euint8 = new Uint8Array(Euint32.buffer); // get bytes of exponent
@ -152,7 +153,7 @@ function RSA() {
} }
}; };
var gen = window.crypto.subtle.generateKey(keyGenOpt, true, ['sign', 'verify']); var gen = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']);
gen.then(exportKey).then(decodeKey).catch(onError); gen.then(exportKey).then(decodeKey).catch(onError);
return; return;
@ -161,7 +162,7 @@ function RSA() {
function exportKey(key) { function exportKey(key) {
// export the generated keys as JsonWebKey (JWK) // export the generated keys as JsonWebKey (JWK)
// https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33 // https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33
return window.crypto.subtle.exportKey('jwk', key.privateKey); return webCrypto.exportKey('jwk', key.privateKey);
} }
function decodeKey(jwk) { function decodeKey(jwk) {

View File

@ -38,6 +38,7 @@ var armor = require('./encoding/armor.js'),
message = require('./message.js'), message = require('./message.js'),
cleartext = require('./cleartext.js'), cleartext = require('./cleartext.js'),
key = require('./key.js'), key = require('./key.js'),
util = require('./util'),
AsyncProxy = require('./worker/async_proxy.js'); AsyncProxy = require('./worker/async_proxy.js');
var asyncProxy; // instance of the asyncproxy var asyncProxy; // instance of the asyncproxy
@ -234,7 +235,7 @@ function generateKeyPair(options, callback) {
} }
// use web worker if web crypto apis are not supported // use web worker if web crypto apis are not supported
if (!useWebCrypto() && useWorker(callback)) { if (!util.getWebCrypto() && useWorker(callback)) {
asyncProxy.generateKeyPair(options, callback); asyncProxy.generateKeyPair(options, callback);
return; return;
} }
@ -273,13 +274,6 @@ function useWorker(callback) {
return true; return true;
} }
/**
* Check for WebCrypto support
*/
function useWebCrypto() {
return typeof window !== 'undefined' && window.crypto && window.crypto.subtle;
}
/** /**
* Command pattern that handles async calls gracefully * Command pattern that handles async calls gracefully
*/ */

View File

@ -1,16 +1,16 @@
// GPG4Browsers - An OpenPGP implementation in javascript // GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH // Copyright (C) 2011 Recurity Labs GmbH
// //
// This library is free software; you can redistribute it and/or // This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public // modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either // License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version. // version 3.0 of the License, or (at your option) any later version.
// //
// This library is distributed in the hope that it will be useful, // This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details. // Lesser General Public License for more details.
// //
// You should have received a copy of the GNU Lesser General Public // You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software // License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@ -193,7 +193,7 @@ module.exports = {
}, },
/** /**
* Convert a Uint8Array to a string. This currently functions * Convert a Uint8Array to a string. This currently functions
* the same as bin2str. * the same as bin2str.
* @function module:util.Uint8Array2str * @function module:util.Uint8Array2str
* @param {Uint8Array} bin An array of (binary) integers to convert * @param {Uint8Array} bin An array of (binary) integers to convert
@ -228,7 +228,7 @@ module.exports = {
}, },
/** /**
* Helper function to print a debug message. Debug * Helper function to print a debug message. Debug
* messages are only printed if * messages are only printed if
* @link module:config/config.debug is set to true. * @link module:config/config.debug is set to true.
* @param {String} str String of the debug message * @param {String} str String of the debug message
@ -240,7 +240,7 @@ module.exports = {
}, },
/** /**
* Helper function to print a debug message. Debug * Helper function to print a debug message. Debug
* messages are only printed if * messages are only printed if
* @link module:config/config.debug is set to true. * @link module:config/config.debug is set to true.
* Different than print_debug because will call hexstrdump iff necessary. * Different than print_debug because will call hexstrdump iff necessary.
@ -265,9 +265,9 @@ module.exports = {
/** /**
* Shifting a string to n bits right * Shifting a string to n bits right
* @param {String} value The string to shift * @param {String} value The string to shift
* @param {Integer} bitcount Amount of bits to shift (MUST be smaller * @param {Integer} bitcount Amount of bits to shift (MUST be smaller
* than 9) * than 9)
* @return {String} Resulting string. * @return {String} Resulting string.
*/ */
shiftRight: function (value, bitcount) { shiftRight: function (value, bitcount) {
var temp = util.str2bin(value); var temp = util.str2bin(value);
@ -305,5 +305,16 @@ module.exports = {
return "SHA224"; return "SHA224";
} }
return "unknown"; return "unknown";
},
getWebCrypto: function() {
if (config.useWebCrypto === false) {
// make web crypto optional
return;
}
if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) {
return window.crypto.subtle;
}
} }
}; };