Skip to content

Commit

Permalink
FAB-3829 Remove storekey param in cryptoSuite importKey
Browse files Browse the repository at this point in the history
The storekey param is extraneous because the "opts" param
already has the "ephemeral" that means the same thing.

Also added tests for ephemeral true.

Change-Id: Ic8ea871666373c0e9d539e06238f984dcb24dd52
Signed-off-by: cdaughtr <cdaughtr@us.ibm.com>
  • Loading branch information
cdaughtr committed May 22, 2017
1 parent 8f148f3 commit 6778314
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
12 changes: 7 additions & 5 deletions fabric-client/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ module.exports.CryptoSuite = class {
* @param {Object} opts
* <br>`type`: type of information that 'raw' represents: x509 certificate,
* <br>`algorithm`: an identifier for the algorithm to be used
* <br>`ephemeral`: true if the key to generate has to be ephemeral
* @storeKey {boolean} store the key in persistent key store- when false the key will
* the key will be returned without a Promise
* @returns {Key} Promise of an instance of the Key class wrapping the raw key bytes
* <br>`ephemeral`: {boolean} Optional. If true, the key to import will not be persisted
* and the key will be returned without a Promise. If not set or false, defaults to
* saving the key in persistent key store.
* @returns {Key} or {Promise} If `ephemeral` is true, the Key class wrapping the raw bytes.
* If `ephemeral' not set or false, a Promise of an instance of the
* Key class wrapping the raw key bytes.
*/
importKey(raw, opts, storeKey) {}
importKey(raw, opts) {}

/**
* Returns the key this CSP associates to the Subject Key Identifier ski.
Expand Down
13 changes: 6 additions & 7 deletions fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var CryptoSuite_ECDSA_AES = class extends api.CryptoSuite {
var pair = KEYUTIL.generateKeypair('EC', this._curveName);

if (typeof opts !== 'undefined' && typeof opts.ephemeral !== 'undefined' && opts.ephemeral === true) {
logger.debug('generateKey, ephemeral true, Promise resolved');
return Promise.resolve(new ECDSAKey(pair.prvKeyObj));
} else {
if (!this._cryptoKeyStore) {
Expand Down Expand Up @@ -168,17 +169,15 @@ var CryptoSuite_ECDSA_AES = class extends api.CryptoSuite {

/**
* This is an implementation of {@link module:api.CryptoSuite#importKey}
*/
importKey(raw, opts, storeKey) {
**/
importKey(raw, opts) {
logger.debug('importKey - start');
var store_key = true; //default
// if storing is not required and therefore a promise will not be returned
// then storeKey must be set to false;
if(typeof storeKey === 'boolean') {
store_key = storeKey;
if (typeof opts !== 'undefined' && typeof opts.ephemeral !== 'undefined' && opts.ephemeral === true) {
store_key = false;
}
if (!!store_key && !this._cryptoKeyStore) {
throw new Error('importKey storeKey is true, which requires CryptoKeyStore to be set.');
throw new Error('importKey opts.ephemeral is false, which requires CryptoKeyStore to be set.');
}

var self = this;
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/lib/msp/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ var MSP = class {
var cert = sid.getIdBytes().toBinary();
logger.debug('Encoded cert from deserialized identity: %s', cert);
if(!store_key) {
var publicKey =this.cryptoSuite.importKey(cert, { algorithm: api.CryptoAlgorithms.X509Certificate }, false);
var publicKey =this.cryptoSuite.importKey(cert, { algorithm: api.CryptoAlgorithms.X509Certificate, ephemeral: true });
var sdk_identity = new Identity(cert, publicKey, this.getId(), this.cryptoSuite);
return sdk_identity;
}
Expand Down
27 changes: 26 additions & 1 deletion test/unit/cryptosuite-ecdsa-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ test('\n\n ** CryptoSuite_ECDSA_AES - error tests **\n\n', function (t) {
() => {
cryptoUtils.importKey(TEST_CERT_PEM);
},
/importKey storeKey is true, which requires CryptoKeyStore to be set./,
/importKey opts.ephemeral is false, which requires CryptoKeyStore to be set./,
'Test missing cryptoKeyStore: cryptoSuite.importKey'
);
t.throws(
Expand All @@ -180,6 +180,31 @@ test('\n\n ** CryptoSuite_ECDSA_AES - error tests **\n\n', function (t) {
t.end();
});

test('\n\n ** CryptoSuite_ECDSA_AES - ephemeral true tests **\n\n', function (t) {
testutil.resetDefaults();
var cryptoUtils = utils.newCryptoSuite();
var key = cryptoUtils.importKey(TEST_KEY_PRIVATE_PEM, {ephemeral: true});
if (key && key._key && key._key.type === 'EC') {
t.pass('importKey returned key using ephemeral true');
} else {
t.fail('importKey did not return key using ephemeral true');
}

return cryptoUtils.generateKey({ephemeral: true})
.then(function (key) {
if (key && key._key && key._key.type === 'EC') {
t.pass('generateKey returned key using ephemeral true');
t.end();
} else {
t.fail('generateKey did not return key using ephemeral true');
t.end();
}
},(err) => {
t.fail('Failed to generateKey. Can not progress any further. Exiting. ' + err.stack ? err.stack : err);
t.end();
});
});

test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
testutil.resetDefaults();

Expand Down

0 comments on commit 6778314

Please sign in to comment.