@@ -126,15 +126,15 @@ function decrypt(xml, options, callback) {
126
126
var decrypted ;
127
127
128
128
try {
129
- var doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
129
+ var doc = typeof xml === 'string' ? new xmldom . DOMParser ( ) . parseFromString ( xml ) : xml ;
130
130
131
131
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 ] ;
133
133
var encryptionAlgorithm = encryptionMethod . getAttribute ( 'Algorithm' ) ;
134
134
135
135
var decipher ;
136
136
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 ] ;
138
138
139
139
var encrypted = new Buffer ( encryptedContent . textContent , 'base64' ) ;
140
140
@@ -190,10 +190,24 @@ function decrypt(xml, options, callback) {
190
190
function decryptKeyInfo ( doc , options ) {
191
191
if ( typeof doc === 'string' ) doc = new xmldom . DOMParser ( ) . parseFromString ( doc ) ;
192
192
193
+ var keyRetrievalMethodUri ;
193
194
var keyInfo = xpath . select ( "//*[local-name(.)='KeyInfo' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" , doc ) [ 0 ] ;
194
195
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
+
195
207
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 ] ;
197
211
198
212
switch ( keyEncryptionAlgorighm ) {
199
213
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' :
0 commit comments