Skip to content

Commit b76bf0d

Browse files
author
Leonardo Zanivan
committed
Add support for OAEP digest method is specified as SHA256 and SHA512
1 parent d1c20b1 commit b76bf0d

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

lib/xmlenc.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ function decryptKeyInfo(doc, options) {
238238
throw new Error('cant find encryption algorithm');
239239
}
240240

241+
const keyDigestMethod = xpath.select("//*[local-name(.)='KeyInfo']/*[local-name(.)='EncryptedKey']/*[local-name(.)='EncryptionMethod']/*[local-name(.)='DigestMethod']", doc)[0];
242+
if (keyDigestMethod) {
243+
const keyDigestMethodAlgorithm = keyDigestMethod.getAttribute('Algorithm');
244+
245+
switch (keyDigestMethodAlgorithm) {
246+
case 'http://www.w3.org/2000/09/xmldsig#sha1':
247+
options.oaepHash = 'sha1';
248+
break;
249+
case 'http://www.w3.org/2000/09/xmldsig#sha256':
250+
options.oaepHash = 'sha256';
251+
break;
252+
case 'http://www.w3.org/2000/09/xmldsig#sha512':
253+
options.oaepHash = 'sha512';
254+
break;
255+
default:
256+
throw new Error('key encryption digest algorithm ' + keyDigestMethodAlgorithm + ' not supported');
257+
}
258+
}
259+
241260
var keyEncryptionAlgorithm = keyEncryptionMethod.getAttribute('Algorithm');
242261
if (options.disallowDecryptionWithInsecureAlgorithm
243262
&& insecureAlgorithms.indexOf(keyEncryptionAlgorithm) >= 0) {
@@ -259,10 +278,10 @@ function decryptKeyInfo(doc, options) {
259278
}
260279

261280
function decryptKeyInfoWithScheme(encryptedKey, options, scheme) {
262-
var padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
263-
var key = Buffer.from(encryptedKey.textContent, 'base64');
281+
const padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
282+
const key = Buffer.from(encryptedKey.textContent, 'base64');
264283
const oaepHash = options.oaepHash || 'sha1';
265-
var decrypted = crypto.privateDecrypt({ key: options.key, oaepHash: oaepHash, padding: padding}, key);
284+
const decrypted = crypto.privateDecrypt({ key: options.key, oaepHash, padding}, key);
266285
return Buffer.from(decrypted, 'binary');
267286
}
268287

package-lock.json

+2-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xml-encryption",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"devDependencies": {
55
"mocha": "^7.1.2",
66
"should": "^11.2.1",
@@ -30,8 +30,5 @@
3030
],
3131
"scripts": {
3232
"test": "mocha"
33-
},
34-
"engines": {
35-
"node": ">=12 < 18"
3633
}
3734
}

test/xmlenc.encryptedkey.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var assert = require('assert');
22
var fs = require('fs');
3-
var should = require('should');
43
var sinon = require('sinon');
54
var xmlenc = require('../lib');
6-
var xpath = require('xpath');
75

86
describe('encrypt', function() {
97
let consoleSpy = null;
@@ -39,13 +37,21 @@ describe('encrypt', function() {
3937
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
4038
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
4139
}
42-
}, {
40+
},
41+
{
4342
name: 'aes-128-gcm with sha256',
4443
encryptionOptions: {
4544
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
4645
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
4746
oaepHash: 'sha256'
4847
}
48+
}, {
49+
name: 'aes-128-gcm with sha512',
50+
encryptionOptions: {
51+
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
52+
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
53+
oaepHash: 'sha512'
54+
}
4955
}, {
5056
name: 'des-ede3-cbc',
5157
encryptionOptions: {
@@ -79,6 +85,7 @@ describe('encrypt', function() {
7985

8086
xmlenc.encrypt(content, options, function(err, result) {
8187
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key'), warnInsecureAlgorithm: false}, function (err, decrypted) {
88+
if (err) return done(err);
8289
assert.equal(decrypted, content);
8390
done();
8491
});

0 commit comments

Comments
 (0)