Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 033866e

Browse files
blockguardianrajat-dlt
authored andcommitted
Bump up the jsrsasign version from 8.0.24 to 10.4.1
* changed the unit test cases according to the jsrsasign package upgrade * Updated extensions as per the new jsrsasign package * made changes in PKCS11_ECDSA_KEY class as per changes in jsrsasign 10.4.1 * certificate request extensions are used and mapped to the new format to ensure backwards compatibility Co-Authored-by: Rajat Sharma <rajat.sharma@dltlabs.io> Co-Authored-by: Deepak Singh <deepak.singh2@dltlabs.io> Signed-off-by: Deepak Singh <91736795+blockguardian@users.noreply.github.com>
1 parent a4c448d commit 033866e

File tree

7 files changed

+70
-35
lines changed

7 files changed

+70
-35
lines changed

fabric-ca-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
},
2121
"types": "./types/index.d.ts",
2222
"dependencies": {
23+
"jsrsasign": "^10.4.1",
2324
"grpc": "1.24.11",
2425
"lodash.clone": "4.5.0",
25-
"jsrsasign": "^8.0.20",
2626
"url": "^0.11.0",
2727
"winston": "^2.4.0"
2828
},

fabric-client/lib/impl/ecdsa/key.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ module.exports = class ECDSA_KEY extends api.Key {
117117
throw new Error('A CSR cannot be generated from a public key');
118118
}
119119

120-
const csr = asn1.csr.CSRUtil.newCSRPEM({
120+
const csr = new asn1.csr.CertificationRequest({
121121
subject: {str: asn1.x509.X500Name.ldapToOneline(subjectDN)},
122122
sbjpubkey: this.getPublicKey()._key,
123123
sigalg: 'SHA256withECDSA',
124124
sbjprvkey: this._key
125125
});
126-
return csr;
126+
return csr.getPEM();
127127
}
128128

129129
/**
@@ -156,18 +156,17 @@ module.exports = class ECDSA_KEY extends api.Key {
156156
sbjpubkey: this.getPublicKey()._key,
157157
ext: [
158158
{
159-
basicConstraints: {
160-
cA: false,
161-
critical: true
162-
}
159+
extname: 'basicConstraints',
160+
cA: false,
161+
critical: true
163162
},
164163
{
165-
keyUsage: {bin: '11'}
164+
extname: 'keyUsage',
165+
bin: '11'
166166
},
167167
{
168-
extKeyUsage: {
169-
array: [{name: 'clientAuth'}]
170-
}
168+
extname: 'extKeyUsage',
169+
array: [{name: 'clientAuth'}]
171170
}
172171
],
173172
cakey: this._key

fabric-client/lib/impl/ecdsa/pkcs11_key.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
const api = require('../../api.js');
1111
const jsrsa = require('jsrsasign');
1212
const asn1 = jsrsa.asn1;
13+
const Utils = require('../../utils');
1314

1415
const elliptic = require('elliptic');
1516
const EC = elliptic.ec;
@@ -82,12 +83,13 @@ const PKCS11_ECDSA_KEY = class extends api.Key {
8283
csr.asn1SignatureAlg =
8384
new asn1.x509.AlgorithmIdentifier({'name': sigAlgName});
8485

85-
const digest = this._cryptoSuite.hash(Buffer.from(csr.asn1CSRInfo.getEncodedHex(), 'hex'));
86+
const csri = new asn1.csr.CertificationRequestInfo(csr.params);
87+
const digest = this._cryptoSuite.hash(Buffer.from(csri.getEncodedHex(), 'hex'));
8688
const sig = this._cryptoSuite.sign(this, Buffer.from(digest, 'hex'));
87-
csr.hexSig = sig.toString('hex');
89+
csr.params.sighex = sig.toString('hex');
8890

89-
csr.asn1Sig = new asn1.DERBitString({'hex': '00' + csr.hexSig});
90-
const seq = new asn1.DERSequence({'array': [csr.asn1CSRInfo, csr.asn1SignatureAlg, csr.asn1Sig]});
91+
csr.asn1Sig = new asn1.DERBitString({'hex': '00' + csr.params.sighex});
92+
const seq = new asn1.DERSequence({'array': [csri, csr.asn1SignatureAlg, csr.asn1Sig]});
9193
csr.hTLV = seq.getEncodedHex();
9294
csr.isModified = false;
9395
}
@@ -108,21 +110,17 @@ const PKCS11_ECDSA_KEY = class extends api.Key {
108110
}
109111
const ecdsa = new EC(this._cryptoSuite._ecdsaCurve);
110112
const pubKey = ecdsa.keyFromPublic(this._pub._ecpt);
111-
const csri = new _KJUR_asn1_csr.CertificationRequestInfo();
112-
csri.setSubjectByParam(param.subject);
113-
csri.setSubjectPublicKeyByGetKey({xy: pubKey.getPublic('hex'), curve: 'secp256r1'});
114-
if (param.ext !== undefined && param.ext.length !== undefined) {
115-
for (const ext of param.ext) {
116-
for (const key in ext) {
117-
csri.appendExtensionByName(key, ext[key]);
118-
}
119-
}
120-
}
121-
122-
const csr = new _KJUR_asn1_csr.CertificationRequest({'csrinfo': csri});
123-
this.signCSR(csr, param.sigalg);
113+
const extreq = Utils.mapCSRExtensions(param.ext);
114+
const sigAlgName = param.sigalg;
115+
const csr = new _KJUR_asn1_csr.CertificationRequest({
116+
subject: param.subject,
117+
sbjpubkey: {xy: pubKey.getPublic('hex'), curve: 'secp256r1'},
118+
sigalg: sigAlgName,
119+
extreq: extreq
120+
});
121+
this.signCSR(csr, sigAlgName);
124122

125-
const pem = csr.getPEMString();
123+
const pem = csr.getPEM();
126124
return pem;
127125

128126
}

fabric-client/lib/utils.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,42 @@ module.exports.convertBytetoString = (buffer_array, encoding) => {
573573

574574
return result;
575575
};
576+
/**
577+
* Map CSRUtil.newCSRPEM style extensions:
578+
* ```
579+
* {
580+
* subjectAltName: {
581+
* array: [...],
582+
* },
583+
* }
584+
* ```
585+
*
586+
* to CertificationRequest style extensions:
587+
* ```
588+
* {
589+
* extname: 'subjectAltName',
590+
* array: [...],
591+
* }
592+
* ```
593+
* @private
594+
*/
595+
module.exports.mapCSRExtensions = (extensions) => {
596+
if (!Array.isArray(extensions)) {
597+
return extensions;
598+
}
599+
600+
const results = [];
601+
extensions.forEach(extension => {
602+
const isCertificationRequestExtension = typeof extension.extname === 'string';
603+
if (isCertificationRequestExtension) {
604+
results.push(extension);
605+
} else {
606+
Object.entries(extension).forEach(([extname, props]) => {
607+
const extensionRequest = Object.assign({}, props, {extname});
608+
results.push(extensionRequest);
609+
});
610+
}
611+
});
612+
613+
return results;
614+
};

fabric-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"ignore-walk": "^3.0.0",
3232
"js-sha3": "^0.7.0",
3333
"js-yaml": "^3.9.0",
34-
"jsrsasign": "^8.0.20",
34+
"jsrsasign": "^10.4.1",
3535
"klaw": "^4.0.1",
3636
"lodash.clone": "4.5.0",
3737
"long": "^4.0.0",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"ink-docstrap": "^1.3.2",
6161
"intercept-stdout": "^0.1.2",
6262
"jsdoc": "^3.6.3",
63-
"jsrsasign": "^8.0.20",
63+
"jsrsasign": "^10.4.1",
6464
"log4js": "^6.1.1",
6565
"mocha": "^7.1.2",
6666
"mock-couch": "^0.1.11",

test/unit/ecdsa-key.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,14 @@ test('\n\n ** ECDSA Key Impl tests **\n\n', (t) => {
133133
const subjectDN = 'CN=dummy';
134134
try {
135135
csrPEM = key3.generateCSR(subjectDN);
136-
csrObject = asn1.csr.CSRUtil.getInfo(csrPEM);
136+
csrObject = asn1.csr.CSRUtil.getParam(csrPEM);
137137
} catch (err) {
138138
t.fail('Failed to generate a CSR: ' + err.stack ? err.stack : err);
139139
}
140-
141-
t.equal(asn1.x509.X500Name.onelineToLDAP(csrObject.subject.name), subjectDN,
140+
t.equal(asn1.x509.X500Name.onelineToLDAP(csrObject.subject.str), subjectDN,
142141
'Checking CSR subject matches subject from request');
143142

144-
t.equal(csrObject.pubkey.obj.pubKeyHex, key3.getPublicKey()._key.pubKeyHex,
143+
t.equal(KEYUTIL.getKeyFromCSRPEM(csrPEM).pubKeyHex, key3.getPublicKey()._key.pubKeyHex,
145144
'Checking CSR public key matches requested public key');
146145

147146
// test X509 generation

0 commit comments

Comments
 (0)