Skip to content

Commit

Permalink
Update fabric-sdk-node with changes from master
Browse files Browse the repository at this point in the history
Need to sync up the bug fixes that occurred after
the code was split from master.  Includes change
https://gerrit.hyperledger.org/r/#/c/1011

Fixes https://jira.hyperledger.org/browse/FAB-659.

Change-Id: I818692960234ad9898441f134b6a19e514c9919f
Signed-off-by: Caroline Daughtrey <[email protected]>
  • Loading branch information
cdaughtr committed Oct 21, 2016
1 parent cca09d6 commit 57bf3a1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 35 deletions.
6 changes: 3 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module.exports.CryptoSuite = class {
*
* @returns {byte[]} the random number
*/
generateNonce() {}
static generateNonce() {}

/**
* Generate asymmetric key pair
Expand Down Expand Up @@ -183,8 +183,8 @@ module.exports.CryptoSuite = class {
eciesEncrypt(recipientPublicKey, msg) {}
hmac(key, bytes) {}
aesCBCPKCS7Decrypt(key, bytes) {}
aes256GCMDecrypt(key /*Buffer*/ , ct /*Buffer*/ ) {}
aesKeyGen() {}
static aes256GCMDecrypt(key /*Buffer*/ , ct /*Buffer*/ ) {}
static aesKeyGen() {}
hmacAESTruncated(key, bytes) {}

};
Expand Down
24 changes: 12 additions & 12 deletions lib/impl/CryptoSuite_ECDSA_SHA.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
* @ignore
*/
setHashAlgorithm(hashAlgorithm) {
this._checkHashFunction(hashAlgorithm);
CryptoSuite_ECDSA_SHA._checkHashFunction(hashAlgorithm);

this._hashAlgorithm = hashAlgorithm;
this._initialize();
}

generateNonce() {
static generateNonce() {
return crypto.randomBytes(NonceSize);
}

Expand Down Expand Up @@ -311,7 +311,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
return this.eciesEncryptECDSA(ecdsa.keyFromPublic(recipientPublicKey.pubKeyHex, 'hex'), msg);
}

aesKeyGen() {
static aesKeyGen() {
return crypto.randomBytes(AESKeyLength);
}

Expand Down Expand Up @@ -342,13 +342,13 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {

var decryptedBytes, unpaddedBytes;

decryptedBytes = this._CBCDecrypt(key, bytes);
unpaddedBytes = this._PKCS7UnPadding(decryptedBytes);
decryptedBytes = CryptoSuite_ECDSA_SHA._CBCDecrypt(key, bytes);
unpaddedBytes = CryptoSuite_ECDSA_SHA._PKCS7UnPadding(decryptedBytes);

return unpaddedBytes;
}

aes256GCMDecrypt(key, ct) {
static aes256GCMDecrypt(key, ct) {
var decipher = crypto.createDecipheriv('aes-256-gcm', key, ct.slice(0, GCMStandardNonceSize));
decipher.setAuthTag(ct.slice(ct.length - GCMTagSize));
var dec = decipher.update(
Expand All @@ -367,7 +367,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
if (!info)
info = '';

var key = this._hkdf2(bytesToBits(new Buffer(ikm)), keyBitLength, bytesToBits(salt), info, this._hashFunctionKeyDerivation);
var key = CryptoSuite_ECDSA_SHA._hkdf2(bytesToBits(new Buffer(ikm)), keyBitLength, bytesToBits(salt), info, this._hashFunctionKeyDerivation);

return bitsToBytes(key);

Expand Down Expand Up @@ -400,7 +400,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
throw new Error('Illegal level: ' + securityLevel + ' - must be either 256 or 384');
}

_checkHashFunction(hashAlgorithm) {
static _checkHashFunction(hashAlgorithm) {
if (!_isString(hashAlgorithm))
throw new Error('Illegal Hash function family: ' + hashAlgorithm + ' - must be either SHA2 or SHA3');

Expand All @@ -411,7 +411,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {

_initialize() {
this._checkSecurityLevel(this._securityLevel);
this._checkHashFunction(this._hashAlgorithm);
CryptoSuite_ECDSA_SHA._checkHashFunction(this._hashAlgorithm);

this._suite = this._hashAlgorithm.toLowerCase() + '-' + this._securityLevel;
if (this._securityLevel == CURVE_P_256_Size) {
Expand Down Expand Up @@ -463,7 +463,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
* @return {bitArray} derived key.
* @ignore
*/
_hkdf2(ikm, keyBitLength, salt, info, Hash) {
static _hkdf2(ikm, keyBitLength, salt, info, Hash) {
var hmac, key, i, hashLen, loops, curOut, ret = [];

// Hash = Hash || sjcl.hash.sha256;
Expand Down Expand Up @@ -505,7 +505,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {
return sjcl.bitArray.clamp(ret, keyBitLength);
}

_CBCDecrypt(key, bytes) {
static _CBCDecrypt(key, bytes) {
debug('key length: ', key.length);
debug('bytes length: ', bytes.length);
var iv = bytes.slice(0, BlockSize);
Expand Down Expand Up @@ -547,7 +547,7 @@ var CryptoSuite_ECDSA_SHA = class extends api.CryptoSuite {

}

_PKCS7UnPadding(bytes) {
static _PKCS7UnPadding(bytes) {

//last byte is the number of padded bytes
var padding = bytes.readUInt8(bytes.length - 1);
Expand Down
117 changes: 97 additions & 20 deletions test/unit/headless-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
var test = require('tape');
var path = require('path');
var util = require('util');
var hfc = require(path.join(__dirname,'../..'));
var hfc = require('../..');
var fs = require('fs');
var execSync = require('child_process').execSync;
var utils = require('../../lib/utils.js');
Expand All @@ -36,6 +36,8 @@ var keyValStorePath = path.join(getUserHome(), 'kvsTemp');
//windows relative path starts with '/'
var keyValStorePath1 = 'tmp/keyValStore1';
var keyValStorePath2 = '/tmp/keyValStore2';
var keyValStorePath3 = '/tmp/keyValStore3';
var keyValStorePath4 = '/tmp/keyValStore4';
var testKey = 'keyValFileStoreName';
var testValue = 'secretKeyValue';
var store1 = '';
Expand Down Expand Up @@ -64,11 +66,6 @@ var memberCfg = {'enrollmentID': enrollmentID ,
var cryptoUtils = null;
// End: CryptoSuite_ECDSA_SHA tests /////

// Peer tests ////////
// var Peer = require('../../lib/Peer.js');
// var EventEmitter = require('events');
// End: Peer tests ////////


//
// Run the FileKeyValueStore test
Expand Down Expand Up @@ -154,6 +151,7 @@ test('FileKeyValueStore constructor test', function(t){
else
t.fail('FileKeyValueStore constructor test: Failed to create new directory: ' + keyValStorePath2);


t.end();
});

Expand All @@ -172,7 +170,7 @@ test('FileKeyValueStore setValue test', function(t) {
// Log the fulfillment value
function(val) {
if (val != testValue) {
t.fail('FileKeyValueStore store1 getValue test: '+ val + ' does not equal testValue of ' + testValue + 'for FileKeyValueStore read and write test');
t.fail('FileKeyValueStore store1 getValue test: '+ val + ' does not equal testValue of ' + testValue);
} else {
t.pass('FileKeyValueStore store1 getValue test: Successfully retrieved value');
}
Expand Down Expand Up @@ -206,7 +204,7 @@ test('FileKeyValueStore setValue test', function(t) {
// Log the fulfillment value
function(val) {
if (val != testValue)
t.fail('FileKeyValueStore store2 getValue test: '+ val + ' does not equal testValue of ' + testValue + 'for FileKeyValueStore read and write test');
t.fail('FileKeyValueStore store2 getValue test: '+ val + ' does not equal testValue of ' + testValue);
else
t.pass('FileKeyValueStore store2 getValue test: Successfully retrieved value');
})
Expand All @@ -227,6 +225,87 @@ test('FileKeyValueStore setValue test', function(t) {
t.end();
});

test('FileKeyValueStore error check tests', function(t){

t.throws(
function() {
store3 = new FileKeyValueStore();
},
/^Error: Must provide the path/,
'FileKeyValueStore error check tests: new FileKeyValueStore with no options should throw '+
'"Must provide the path to the directory to hold files for the store."'
);

t.throws(
function() {
store3 = new FileKeyValueStore({dir: getRelativePath(keyValStorePath3)});
},
/^Error: Must provide the path/,
'FileKeyValueStore error check tests: new FileKeyValueStore with no options.path should throw '+
'"Must provide the path to the directory to hold files for the store."'
);

cleanupFileKeyValueStore(keyValStorePath3);
var store3 = new FileKeyValueStore({path: getRelativePath(keyValStorePath3)});
store3.setValue(testKey, testValue)
.then(
function(result) {
if (result) {
t.comment('FileKeyValueStore error check tests: Delete store & getValue test. Successfully set value');

var exists = utils.existsSync(getAbsolutePath(keyValStorePath3), testKey);
if (exists) {
t.comment('FileKeyValueStore error check tests: Delete store & getValue test. Verified the file for key ' + testKey + ' does exist');
cleanupFileKeyValueStore(keyValStorePath3);
exists = utils.existsSync(getAbsolutePath(keyValStorePath3), testKey);
t.comment('FileKeyValueStore error check tests: Delete store & getValue test. Deleted store, exists: '+exists);
store3.getValue(testKey)
.then(
// Log the fulfillment value
function(val) {
if (val === null) {
t.pass('FileKeyValueStore error check tests: Delete store & getValue test. getValue is null');
} else {
t.fail('FileKeyValueStore error check tests: Delete store & getValue test. getValue successfully retrieved value: '+val);
}
})
.catch(
// Log the rejection reason
function(reason) {
t.fail('FileKeyValueStore error check tests: Delete store & getValue test. getValue caught unexpected error: '+reason);
});
} else {
t.fail('FileKeyValueStore error check tests: Delete store & getValue test. Failed to create file for key ' + testKey);
}
}
})
.catch(
function(reason) {
t.fail('FileKeyValueStore error check tests: Delete store & getValue test. Failed to set value: '+reason);
});

cleanupFileKeyValueStore(keyValStorePath4);
var store4 = new FileKeyValueStore({path: getRelativePath(keyValStorePath4)});
cleanupFileKeyValueStore(keyValStorePath4);
var exists = utils.existsSync(getAbsolutePath(keyValStorePath4));
t.comment('FileKeyValueStore error check tests: Delete store & setValue test. Deleted store, exists: '+exists);
store4.setValue(testKey, testValue)
.then(
function(result) {
if (result) {
t.fail('FileKeyValueStore error check tests: Delete store & setValue test. Successfully set value');
}
})
.catch(
function(reason) {
t.pass('FileKeyValueStore error check tests: Delete store & setValue test. Failed to set value: '+reason);
});


t.end();
});


// Chain tests /////////////
test('Chain constructor test', function(t) {
_chain = new Chain(chainName);
Expand Down Expand Up @@ -502,12 +581,12 @@ test('CryptoSuite_ECDSA_SHA function tests', function(t) {
'CryptoSuite_ECDSA_SHA function tests: setHashAlgorithm("SHA5") should throw Illegal Hash function family'
);

var nonce1 = cryptoUtils.generateNonce();
var nonce1 = cryptoSuiteReq.generateNonce();
if (t.equal(24, nonce1.length,
'CryptoSuite_ECDSA_SHA function tests: generateNonce length'));

var nonce2 = cryptoUtils.generateNonce();
var nonce3 = cryptoUtils.generateNonce();
var nonce2 = cryptoSuiteReq.generateNonce();
var nonce3 = cryptoSuiteReq.generateNonce();
if (nonce1 != nonce2 && nonce2 != nonce3)
t.pass('CryptoSuite_ECDSA_SHA function tests: verify generateNonce buffers are different');
else
Expand Down Expand Up @@ -553,9 +632,7 @@ test('CryptoSuite_ECDSA_SHA function tests', function(t) {
cryptoUtils.setSecurityLevel(256);
cryptoUtils.setHashAlgorithm('SHA2');
keyPair = cryptoUtils.generateKeyPair();
//console.log('keyPair.prvKeyObj.prvKeyHex: '+keyPair.prvKeyObj.prvKeyHex);
var kps = cryptoUtils.getKeyPairForSigning(keyPair.prvKeyObj.prvKeyHex, 'hex');
//console.log(util.inspect(keyPair, false, null))
t.equal(keyPair.prvKeyObj.prvKeyHex.toString(16, 2), kps.priv.toString(16, 2),
'CryptoSuite_ECDSA_SHA function tests: getKeyPairForSigning prvKeyHex == priv');

Expand Down Expand Up @@ -602,7 +679,7 @@ test('Logging utility tests - built-in logger', function(t) {

// test 2: custom logging levels for console logging
var output = '';
process.env.HFC_LOGGING = "{'debug': 'console'}"; // expected json format so this is invalid
process.env.HFC_LOGGING = '{\'debug\': \'console\'}'; // expected json format so this is invalid
// internal call. clearing the cached logger.
global.hfc.logger = undefined;

Expand Down Expand Up @@ -708,7 +785,7 @@ test('Logging utility tests - built-in logger', function(t) {
t.fail('Failed to validate content in debug log file');
}

var data = fs.readFileSync(errorPath);
data = fs.readFileSync(errorPath);

if (data.indexOf('Test logger - error') > 0) {
if (data.indexOf('Test logger - warn') > 0 ||
Expand Down Expand Up @@ -753,11 +830,11 @@ test('Logging utility tests - test setting an invalid external logger', function
t.fail('Should not have allowed an invalid logger to be set');
t.end();
} catch(err) {
err = err.toString();
if (err.indexOf('debug()') > 0 &&
err.indexOf('info()') > 0 &&
err.indexOf('warn()') > 0 &&
err.indexOf('error()') > 0) {
var er1 = err.toString();
if (er1.indexOf('debug()') > 0 &&
er1.indexOf('info()') > 0 &&
er1.indexOf('warn()') > 0 &&
er1.indexOf('error()') > 0) {
t.pass('Successfully tested thrown error for an invalid logger to set on the HFC SDK');
t.end();
} else {
Expand Down

0 comments on commit 57bf3a1

Please sign in to comment.