Skip to content

Commit 2fb9995

Browse files
committed
Add helper to create signature digest.
- Reduce duplicate code. - Fix style nit. - Update changelog.
1 parent 03d3ed7 commit 2fb9995

File tree

2 files changed

+69
-130
lines changed

2 files changed

+69
-130
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ Forge ChangeLog
55
- [x509] 'Expected' and 'Actual' issuers were backwards in verification failure
66
message.
77

8+
### Added
9+
- [oid,x509]: Added OID `1.3.14.3.2.29 / sha1WithRSASignature` for sha1 with
10+
RSA. Considered a deprecated equivalent to `1.2.840.113549.1.1.5 /
11+
sha1WithRSAEncryption`. See [discussion and
12+
links](https://github.com/digitalbazaar/forge/issues/825).
13+
14+
### Changed
15+
- [x509]: Reduce duplicate code with a helper function to create a signature
16+
digest given an signature algorithm OID.
17+
818
## 1.1.0 - 2022-01-06
919

1020
### Fixed

lib/x509.js

Lines changed: 59 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,44 @@ var _readSignatureParameters = function(oid, obj, fillDefaults) {
689689
return params;
690690
};
691691

692+
/**
693+
* Create signature digest for OID.
694+
*
695+
* @param options
696+
* signatureOid: the OID specifying the signature algorithm.
697+
* type: a human readable type for error messages
698+
* @return a created md instance. throws if unknown oid.
699+
*/
700+
var _createSignatureDigest = function(options) {
701+
switch(oids[options.signatureOid]) {
702+
case 'sha1WithRSAEncryption':
703+
case 'sha1WithRSASignature':
704+
return forge.md.sha1.create();
705+
break;
706+
case 'md5WithRSAEncryption':
707+
return forge.md.md5.create();
708+
break;
709+
case 'sha256WithRSAEncryption':
710+
return forge.md.sha256.create();
711+
break;
712+
case 'sha384WithRSAEncryption':
713+
return forge.md.sha384.create();
714+
break;
715+
case 'sha512WithRSAEncryption':
716+
return forge.md.sha512.create();
717+
break;
718+
case 'RSASSA-PSS':
719+
return forge.md.sha256.create();
720+
break;
721+
default:
722+
var error = new Error(
723+
'Could not compute ' + options.type + ' digest. ' +
724+
'Unknown signature OID.');
725+
error.signatureOid = options.signatureOid;
726+
throw error;
727+
}
728+
};
729+
692730
/**
693731
* Converts an X.509 certificate from PEM format.
694732
*
@@ -1076,37 +1114,11 @@ pki.createCertificate = function() {
10761114

10771115
var md = child.md;
10781116
if(md === null) {
1079-
// check signature OID for supported signature types
1080-
if(child.signatureOid in oids) {
1081-
var oid = oids[child.signatureOid];
1082-
switch(oid) {
1083-
case 'sha1WithRSAEncryption':
1084-
case 'sha1WithRSASignature':
1085-
md = forge.md.sha1.create();
1086-
break;
1087-
case 'md5WithRSAEncryption':
1088-
md = forge.md.md5.create();
1089-
break;
1090-
case 'sha256WithRSAEncryption':
1091-
md = forge.md.sha256.create();
1092-
break;
1093-
case 'sha384WithRSAEncryption':
1094-
md = forge.md.sha384.create();
1095-
break;
1096-
case 'sha512WithRSAEncryption':
1097-
md = forge.md.sha512.create();
1098-
break;
1099-
case 'RSASSA-PSS':
1100-
md = forge.md.sha256.create();
1101-
break;
1102-
}
1103-
}
1104-
if(md === null) {
1105-
var error = new Error('Could not compute certificate digest. ' +
1106-
'Unknown signature OID.');
1107-
error.signatureOid = child.signatureOid;
1108-
throw error;
1109-
}
1117+
// create digest for OID signature types
1118+
md = _createSignatureDigest({
1119+
signatureOid: child.signatureOid,
1120+
type: 'certificate'
1121+
});
11101122

11111123
// produce DER formatted TBSCertificate and digest it
11121124
var tbsCertificate = child.tbsCertificate || pki.getTBSCertificate(child);
@@ -1120,8 +1132,8 @@ pki.createCertificate = function() {
11201132
switch(child.signatureOid) {
11211133
case oids.sha1WithRSAEncryption:
11221134
case oids.sha1WithRSASignature:
1123-
scheme = undefined; /* use PKCS#1 v1.5 padding scheme */
1124-
break;
1135+
scheme = undefined; /* use PKCS#1 v1.5 padding scheme */
1136+
break;
11251137
case oids['RSASSA-PSS']:
11261138
var hash, mgf;
11271139

@@ -1335,38 +1347,11 @@ pki.certificateFromAsn1 = function(obj, computeHash) {
13351347
cert.tbsCertificate = capture.tbsCertificate;
13361348

13371349
if(computeHash) {
1338-
// check signature OID for supported signature types
1339-
cert.md = null;
1340-
if(cert.signatureOid in oids) {
1341-
var oid = oids[cert.signatureOid];
1342-
switch(oid) {
1343-
case 'sha1WithRSAEncryption':
1344-
case 'sha1WithRSASignature':
1345-
cert.md = forge.md.sha1.create();
1346-
break;
1347-
case 'md5WithRSAEncryption':
1348-
cert.md = forge.md.md5.create();
1349-
break;
1350-
case 'sha256WithRSAEncryption':
1351-
cert.md = forge.md.sha256.create();
1352-
break;
1353-
case 'sha384WithRSAEncryption':
1354-
cert.md = forge.md.sha384.create();
1355-
break;
1356-
case 'sha512WithRSAEncryption':
1357-
cert.md = forge.md.sha512.create();
1358-
break;
1359-
case 'RSASSA-PSS':
1360-
cert.md = forge.md.sha256.create();
1361-
break;
1362-
}
1363-
}
1364-
if(cert.md === null) {
1365-
var error = new Error('Could not compute certificate digest. ' +
1366-
'Unknown signature OID.');
1367-
error.signatureOid = cert.signatureOid;
1368-
throw error;
1369-
}
1350+
// create digest for OID signature type
1351+
cert.md = _createSignatureDigest({
1352+
signatureOid: cert.signatureOid,
1353+
type: 'certificate'
1354+
});
13701355

13711356
// produce DER formatted TBSCertificate and digest it
13721357
var bytes = asn1.toDer(cert.tbsCertificate);
@@ -1684,38 +1669,11 @@ pki.certificationRequestFromAsn1 = function(obj, computeHash) {
16841669
csr.certificationRequestInfo = capture.certificationRequestInfo;
16851670

16861671
if(computeHash) {
1687-
// check signature OID for supported signature types
1688-
csr.md = null;
1689-
if(csr.signatureOid in oids) {
1690-
var oid = oids[csr.signatureOid];
1691-
switch(oid) {
1692-
case 'sha1WithRSAEncryption':
1693-
case 'sha1WithRSASignature':
1694-
csr.md = forge.md.sha1.create();
1695-
break;
1696-
case 'md5WithRSAEncryption':
1697-
csr.md = forge.md.md5.create();
1698-
break;
1699-
case 'sha256WithRSAEncryption':
1700-
csr.md = forge.md.sha256.create();
1701-
break;
1702-
case 'sha384WithRSAEncryption':
1703-
csr.md = forge.md.sha384.create();
1704-
break;
1705-
case 'sha512WithRSAEncryption':
1706-
csr.md = forge.md.sha512.create();
1707-
break;
1708-
case 'RSASSA-PSS':
1709-
csr.md = forge.md.sha256.create();
1710-
break;
1711-
}
1712-
}
1713-
if(csr.md === null) {
1714-
var error = new Error('Could not compute certification request digest. ' +
1715-
'Unknown signature OID.');
1716-
error.signatureOid = csr.signatureOid;
1717-
throw error;
1718-
}
1672+
// create digest for OID signature type
1673+
csr.md = _createSignatureDigest({
1674+
signatureOid: csr.signatureOid,
1675+
type: 'certification request'
1676+
});
17191677

17201678
// produce DER formatted CertificationRequestInfo and digest it
17211679
var bytes = asn1.toDer(csr.certificationRequestInfo);
@@ -1855,39 +1813,10 @@ pki.createCertificationRequest = function() {
18551813

18561814
var md = csr.md;
18571815
if(md === null) {
1858-
// check signature OID for supported signature types
1859-
if(csr.signatureOid in oids) {
1860-
// TODO: create DRY `OID to md` function
1861-
var oid = oids[csr.signatureOid];
1862-
switch(oid) {
1863-
case 'sha1WithRSAEncryption':
1864-
case 'sha1WithRSASignature':
1865-
md = forge.md.sha1.create();
1866-
break;
1867-
case 'md5WithRSAEncryption':
1868-
md = forge.md.md5.create();
1869-
break;
1870-
case 'sha256WithRSAEncryption':
1871-
md = forge.md.sha256.create();
1872-
break;
1873-
case 'sha384WithRSAEncryption':
1874-
md = forge.md.sha384.create();
1875-
break;
1876-
case 'sha512WithRSAEncryption':
1877-
md = forge.md.sha512.create();
1878-
break;
1879-
case 'RSASSA-PSS':
1880-
md = forge.md.sha256.create();
1881-
break;
1882-
}
1883-
}
1884-
if(md === null) {
1885-
var error = new Error(
1886-
'Could not compute certification request digest. ' +
1887-
'Unknown signature OID.');
1888-
error.signatureOid = csr.signatureOid;
1889-
throw error;
1890-
}
1816+
md = _createSignatureDigest({
1817+
signatureOid: csr.signatureOid,
1818+
type: 'certification request'
1819+
});
18911820

18921821
// produce DER formatted CertificationRequestInfo and digest it
18931822
var cri = csr.certificationRequestInfo ||

0 commit comments

Comments
 (0)