Skip to content

Commit 740954d

Browse files
committed
Allow optional DigestAlgorithm parameters.
RFC 3447 and RFC 8017 allow for optional `DigestAlgorithm` `NULL` parameters for `sha*` algorithms and require `NULL` paramters for `md2` and `md5` algorithms.
1 parent 56f4316 commit 740954d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Forge ChangeLog
22
===============
33

4+
## 1.3.1 - 2022-03-xx
5+
6+
### Fixes
7+
- RFC 3447 and RFC 8017 allow for optional `DigestAlgorithm` `NULL` parameters
8+
for `sha*` algorithms and require `NULL` paramters for `md2` and `md5`
9+
algorithms.
10+
411
## 1.3.0 - 2022-03-17
512

613
### Security

lib/rsa.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ var digestInfoValidator = {
286286
name: 'DigestInfo.DigestAlgorithm.parameters',
287287
tagClass: asn1.Class.UNIVERSAL,
288288
type: asn1.Type.NULL,
289+
// captured only to check existence for md2 and md5
290+
capture: 'parameters',
289291
optional: true,
290292
constructed: false
291293
}]
@@ -1188,6 +1190,16 @@ pki.setRsaPublicKey = pki.rsa.setPublicKey = function(n, e) {
11881190
throw error;
11891191
}
11901192

1193+
// special check for md2 and md5 that NULL parameters exist
1194+
if(oid === forge.oids.md2 || oid === forge.oids.md5) {
1195+
if(!('parameters' in capture)) {
1196+
throw new Error(
1197+
'ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 ' +
1198+
'DigestInfo value. ' +
1199+
'Missing algorithm identifer NULL parameters.');
1200+
}
1201+
}
1202+
11911203
// compare the given digest to the decrypted one
11921204
return digest === capture.digest;
11931205
}

tests/unit/rsa.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,15 @@ var UTIL = require('../../lib/util');
845845
/^Error: ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 DigestInfo value.$/);
846846
}
847847

848+
function _checkGoodDigestInfo(publicKey, S, skipTailingGarbage) {
849+
var md = MD.sha256.create();
850+
md.update(m);
851+
852+
ASSERT.ok(publicKey.verify(md.digest().getBytes(), S, undefined, {
853+
_parseAllDigestBytes: !skipTailingGarbage
854+
}));
855+
}
856+
848857
it('should check DigestInfo structure', function() {
849858
var publicKey = RSA.setPublicKey(N, e);
850859
// 0xff bytes stolen from padding
@@ -904,7 +913,7 @@ var UTIL = require('../../lib/util');
904913
'0bc1dd3f020cb1091af6b476416da3024ea046b09fbbbc4d2355da9a2bc6ddb9');
905914

906915
_checkBadTailingGarbage(publicKey, S);
907-
_checkBadDigestInfo(publicKey, S, true);
916+
_checkGoodDigestInfo(publicKey, S, true);
908917
});
909918

910919
it('should check tailing garbage and DigestInfo [2]', function() {

0 commit comments

Comments
 (0)