Skip to content

Commit

Permalink
Merge pull request #1936 from adecaro/issue1892
Browse files Browse the repository at this point in the history
Fix Issue 1892. Client-sdk: setting invalid security level/hash function family leave the chain object in a corrupted state
  • Loading branch information
srderson committed Jun 27, 2016
2 parents 0587761 + 2879022 commit af7a610
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 27 deletions.
4 changes: 2 additions & 2 deletions sdk/node/lib/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export declare class Crypto {
hmac(key: any, bytes: any): any[];
hmacAESTruncated(key: any, bytes: any): any[];
hash(bytes: any): any;
private checkSecurityLevel();
private checkHashFunction();
private checkSecurityLevel(securityLevel);
private checkHashFunction(hashAlgorithm);
private initialize();
/** HKDF with the specified hash function.
* @param {bitArray} ikm The input keying material.
Expand Down
22 changes: 12 additions & 10 deletions sdk/node/lib/crypto.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/node/lib/crypto.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sdk/node/lib/hlc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export interface TransactionRequest {
*/
export interface DeployRequest extends TransactionRequest {
chaincodePath: string;
chaincodeName: string;
}
/**
* Invoke or query request.
Expand Down
6 changes: 3 additions & 3 deletions sdk/node/lib/hlc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/node/lib/hlc.js.map

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions sdk/node/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class Crypto {
* @params securityLevel The security level
*/
setSecurityLevel(securityLevel:number):void {
this.checkSecurityLevel(securityLevel);

this.securityLevel = securityLevel;
this.initialize();
}
Expand All @@ -113,6 +115,8 @@ export class Crypto {
* @params hashAlgorithm The hash algorithm ('SHA2' or 'SHA3')
*/
setHashAlgorithm(hashAlgorithm:string):void {
this.checkHashFunction(hashAlgorithm);

this.hashAlgorithm = hashAlgorithm;
this.initialize();
}
Expand Down Expand Up @@ -385,23 +389,23 @@ export class Crypto {
return this.hashFunction(bytes);
}

private checkSecurityLevel() {
if (this.securityLevel != 256 && this.securityLevel != 384)
private checkSecurityLevel(securityLevel:number) {
if (securityLevel != 256 && securityLevel != 384)
throw new Error("Illegal level: " + this.securityLevel + " - must be either 256 or 384");
}

private checkHashFunction() {
if (!_isString(this.hashAlgorithm))
throw new Error("Illegal Hash function family: " + this.hashAlgorithm + " - must be either SHA2 or SHA3");
private checkHashFunction(hashAlgorithm: string) {
if (!_isString(hashAlgorithm))
throw new Error("Illegal Hash function family: " + hashAlgorithm + " - must be either SHA2 or SHA3");

this.hashAlgorithm = this.hashAlgorithm.toUpperCase();
if (this.hashAlgorithm != SHA2 && this.hashAlgorithm != SHA3)
throw new Error("Illegal Hash function family: " + this.hashAlgorithm + " - must be either SHA2 or SHA3");
hashAlgorithm = hashAlgorithm.toUpperCase();
if (hashAlgorithm != SHA2 && hashAlgorithm != SHA3)
throw new Error("Illegal Hash function family: " + hashAlgorithm + " - must be either SHA2 or SHA3");
}

private initialize() {
this.checkSecurityLevel();
this.checkHashFunction();
this.checkSecurityLevel(this.securityLevel);
this.checkHashFunction(this.hashAlgorithm);

this.suite = this.hashAlgorithm.toLowerCase() + '-' + this.securityLevel;
if (this.securityLevel == CURVE_P_256_Size) {
Expand Down
34 changes: 34 additions & 0 deletions sdk/node/test/unit/chain-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,40 @@ function fail(t, msg, err) {
t.end(err);
}

//
// Set Invalid security level and hash algorithm.
//

test('Set Invalid security level and hash algorithm.', function (t) {
t.plan(2);

var securityLevel = chain.getMemberServices().getSecurityLevel();
try {
chain.getMemberServices().setSecurityLevel(128);
t.fail("Setting an invalid security level should fail. Allowed security levels are '256' and '384'.")
} catch (err) {
if (securityLevel != chain.getMemberServices().getSecurityLevel()) {
t.fail("Chain is using an invalid security level.")
}

t.pass("Setting an invalid security level failed as expected.")
}

var hashAlgorithm = chain.getMemberServices().getHashAlgorithm();
try {
chain.getMemberServices().setHashAlgorithm('SHA');
t.fail("Setting an invalid hash algorithm should fail. Allowed hash algorithm are 'SHA2' and 'SHA3'.")
} catch (err) {
if (hashAlgorithm != chain.getMemberServices().getHashAlgorithm()) {
t.fail("Chain is using an invalid hash algorithm.")
}

t.pass("Setting an invalid hash algorithm failed as expected.")
}

});


//
// Enroll the WebAppAdmin member. WebAppAdmin member is already registered
// manually by being included inside the membersrvc.yaml file.
Expand Down

0 comments on commit af7a610

Please sign in to comment.