Skip to content

Commit 8278c8e

Browse files
committed
support RetrievalMethod
1 parent 664ebff commit 8278c8e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/xmlenc.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ function decrypt(xml, options, callback) {
126126
var decrypted;
127127

128128
try {
129-
var doc = new xmldom.DOMParser().parseFromString(xml);
129+
var doc = typeof xml === 'string' ? new xmldom.DOMParser().parseFromString(xml) : xml;
130130

131131
var symmetricKey = decryptKeyInfo(doc, options);
132-
var encryptionMethod = xpath.select("/*[local-name(.)='EncryptedData']/*[local-name(.)='EncryptionMethod']", doc)[0];
132+
var encryptionMethod = xpath.select("//*[local-name(.)='EncryptedData']/*[local-name(.)='EncryptionMethod']", doc)[0];
133133
var encryptionAlgorithm = encryptionMethod.getAttribute('Algorithm');
134134

135135
var decipher;
136136
var padding;
137-
var encryptedContent = xpath.select("/*[local-name(.)='EncryptedData']/*[local-name(.)='CipherData']/*[local-name(.)='CipherValue']", doc)[0];
137+
var encryptedContent = xpath.select("//*[local-name(.)='EncryptedData']/*[local-name(.)='CipherData']/*[local-name(.)='CipherValue']", doc)[0];
138138

139139
var encrypted = new Buffer(encryptedContent.textContent, 'base64');
140140

@@ -190,10 +190,24 @@ function decrypt(xml, options, callback) {
190190
function decryptKeyInfo(doc, options) {
191191
if (typeof doc === 'string') doc = new xmldom.DOMParser().parseFromString(doc);
192192

193+
var keyRetrievalMethodUri;
193194
var keyInfo = xpath.select("//*[local-name(.)='KeyInfo' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", doc)[0];
194195
var keyEncryptionMethod = xpath.select("//*[local-name(.)='KeyInfo']/*[local-name(.)='EncryptedKey']/*[local-name(.)='EncryptionMethod']", doc)[0];
196+
197+
if (!keyEncryptionMethod) { // try with EncryptedData->KeyInfo->RetrievalMethod
198+
var keyRetrievalMethod = xpath.select("//*[local-name(.)='EncryptedData']/*[local-name(.)='KeyInfo']/*[local-name(.)='RetrievalMethod']", doc)[0];
199+
keyRetrievalMethodUri = keyRetrievalMethod ? keyRetrievalMethod.getAttribute('URI') : null;
200+
keyEncryptionMethod = keyRetrievalMethodUri ? xpath.select("//*[local-name(.)='EncryptedKey' and @Id='" + keyRetrievalMethodUri.substring(1) + "']/*[local-name(.)='EncryptionMethod']", doc)[0] : null;
201+
}
202+
203+
if (!keyEncryptionMethod) {
204+
throw new Error('cant find encryption algorithm');
205+
}
206+
195207
var keyEncryptionAlgorighm = keyEncryptionMethod.getAttribute('Algorithm');
196-
var encryptedKey = xpath.select("//*[local-name(.)='CipherValue']", keyInfo)[0];
208+
var encryptedKey = keyRetrievalMethodUri ?
209+
xpath.select("//*[local-name(.)='EncryptedKey' and @Id='" + keyRetrievalMethodUri.substring(1) + "']/*[local-name(.)='CipherData']/*[local-name(.)='CipherValue']", keyInfo)[0] :
210+
xpath.select("//*[local-name(.)='CipherValue']", keyInfo)[0];
197211

198212
switch (keyEncryptionAlgorighm) {
199213
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p':

0 commit comments

Comments
 (0)