@@ -10,13 +10,13 @@ function encryptKeyInfoWithScheme(symmetricKey, options, scheme, callback) {
10
10
var rsa_pub = pki . publicKeyFromPem ( options . rsa_pub ) ;
11
11
var encrypted = rsa_pub . encrypt ( symmetricKey . toString ( 'binary' ) , scheme ) ;
12
12
var base64EncodedEncryptedKey = new Buffer ( encrypted , 'binary' ) . toString ( 'base64' ) ;
13
-
13
+
14
14
var params = {
15
15
encryptedKey : base64EncodedEncryptedKey ,
16
16
encryptionPublicCert : '<X509Data><X509Certificate>' + utils . pemToCert ( options . pem . toString ( ) ) + '</X509Certificate></X509Data>' ,
17
17
keyEncryptionMethod : options . keyEncryptionAlgorighm
18
18
} ;
19
-
19
+
20
20
var result = utils . renderTemplate ( 'keyinfo' , params ) ;
21
21
callback ( null , result ) ;
22
22
} catch ( e ) {
@@ -31,7 +31,7 @@ function encryptKeyInfo(symmetricKey, options, callback) {
31
31
return callback ( new Error ( 'must provide options.rsa_pub with public key RSA' ) ) ;
32
32
if ( ! options . pem )
33
33
return callback ( new Error ( 'must provide options.pem with certificate' ) ) ;
34
-
34
+
35
35
if ( ! options . keyEncryptionAlgorighm )
36
36
return callback ( new Error ( 'encryption without encrypted key is not supported yet' ) ) ;
37
37
@@ -122,7 +122,7 @@ function decrypt(xml, options, callback) {
122
122
return callback ( new Error ( 'must provide XML to encrypt' ) ) ;
123
123
if ( ! options . key )
124
124
return callback ( new Error ( 'key option is mandatory and you should provide a valid RSA private key' ) ) ;
125
-
125
+
126
126
var decrypted ;
127
127
128
128
try {
@@ -132,58 +132,48 @@ function decrypt(xml, options, callback) {
132
132
var encryptionMethod = xpath . select ( "//*[local-name(.)='EncryptedData']/*[local-name(.)='EncryptionMethod']" , doc ) [ 0 ] ;
133
133
var encryptionAlgorithm = encryptionMethod . getAttribute ( 'Algorithm' ) ;
134
134
135
- var decipher ;
136
- var padding ;
135
+ var algorithm ;
136
+ var ivLength ;
137
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
+
141
141
switch ( encryptionAlgorithm ) {
142
142
case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' :
143
- decipher = crypto . createDecipheriv ( 'aes-128-cbc' , symmetricKey , encrypted . slice ( 0 , 16 ) ) ;
144
-
145
- decipher . setAutoPadding ( false ) ;
146
- decrypted = decipher . update ( encrypted . slice ( 16 ) , null , 'binary' ) + decipher . final ( 'binary' ) ;
147
-
148
- // Remove padding bytes equal to the value of the last byte of the returned data.
149
- padding = decrypted . charCodeAt ( decrypted . length - 1 ) ;
150
- if ( 1 <= padding && padding <= 16 ) {
151
- decrypted = decrypted . substr ( 0 , decrypted . length - padding ) ;
152
- } else {
153
- callback ( new Error ( 'padding length invalid' ) ) ;
154
- return ;
155
- }
156
-
157
- decrypted = new Buffer ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
143
+ algorithm = 'aes-128-cbc' ;
144
+ ivLength = 16 ;
158
145
break ;
159
146
case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc' :
160
- decipher = crypto . createDecipheriv ( 'aes-256-cbc' , symmetricKey , encrypted . slice ( 0 , 16 ) ) ;
161
-
162
- decipher . setAutoPadding ( false ) ;
163
- decrypted = decipher . update ( encrypted . slice ( 16 ) , null , 'binary' ) + decipher . final ( 'binary' ) ;
164
-
165
- // Remove padding bytes equal to the value of the last byte of the returned data.
166
- padding = decrypted . charCodeAt ( decrypted . length - 1 ) ;
167
- if ( 1 <= padding && padding <= 16 ) {
168
- decrypted = decrypted . substr ( 0 , decrypted . length - padding ) ;
169
- } else {
170
- callback ( new Error ( 'padding length invalid' ) ) ;
171
- return ;
172
- }
173
- decrypted = new Buffer ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
147
+ algorithm = 'aes-256-cbc' ;
148
+ ivLength = 16 ;
174
149
break ;
175
150
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' :
176
- decipher = crypto . createDecipheriv ( 'des-ede3-cbc' , symmetricKey , encrypted . slice ( 0 , 8 ) ) ;
177
- decrypted = decipher . update ( encrypted . slice ( 8 ) , null , 'binary' ) + decipher . final ( 'binary' ) ;
178
- decrypted = new Buffer ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
151
+ algorithm = 'des-ede3-cbc' ;
152
+ ivLength = 8 ;
179
153
break ;
180
154
default :
181
155
return callback ( new Error ( 'encryption algorithm ' + encryptionAlgorithm + ' not supported' ) ) ;
182
156
}
157
+
158
+ var decipher = crypto . createDecipheriv ( algorithm , symmetricKey , encrypted . slice ( 0 , ivLength ) ) ;
159
+ decipher . setAutoPadding ( false ) ;
160
+
161
+ decrypted = decipher . update ( encrypted . slice ( ivLength ) , null , 'binary' ) + decipher . final ( 'binary' ) ;
162
+
163
+ // Remove padding bytes equal to the value of the last byte of the returned data.
164
+ var padding = decrypted . charCodeAt ( decrypted . length - 1 ) ;
165
+ if ( 1 <= padding && padding <= ivLength ) {
166
+ decrypted = decrypted . substr ( 0 , decrypted . length - padding ) ;
167
+ } else {
168
+ callback ( new Error ( 'padding length invalid' ) ) ;
169
+ return ;
170
+ }
171
+
172
+ decrypted = new Buffer ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
183
173
} catch ( e ) {
184
174
return callback ( e ) ;
185
175
}
186
-
176
+
187
177
callback ( null , decrypted ) ;
188
178
}
189
179
@@ -193,7 +183,7 @@ function decryptKeyInfo(doc, options) {
193
183
var keyRetrievalMethodUri ;
194
184
var keyInfo = xpath . select ( "//*[local-name(.)='KeyInfo' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" , doc ) [ 0 ] ;
195
185
var keyEncryptionMethod = xpath . select ( "//*[local-name(.)='KeyInfo']/*[local-name(.)='EncryptedKey']/*[local-name(.)='EncryptionMethod']" , doc ) [ 0 ] ;
196
-
186
+
197
187
if ( ! keyEncryptionMethod ) { // try with EncryptedData->KeyInfo->RetrievalMethod
198
188
var keyRetrievalMethod = xpath . select ( "//*[local-name(.)='EncryptedData']/*[local-name(.)='KeyInfo']/*[local-name(.)='RetrievalMethod']" , doc ) [ 0 ] ;
199
189
keyRetrievalMethodUri = keyRetrievalMethod ? keyRetrievalMethod . getAttribute ( 'URI' ) : null ;
@@ -235,7 +225,7 @@ function encryptWithAlgorithm(algorithm, symmetricKey, ivLength, content, encodi
235
225
// create a random iv for algorithm
236
226
crypto . randomBytes ( ivLength , function ( err , iv ) {
237
227
if ( err ) return callback ( err ) ;
238
-
228
+
239
229
var cipher = crypto . createCipheriv ( algorithm , symmetricKey , iv ) ;
240
230
// encrypted content
241
231
var encrypted = cipher . update ( content , encoding , 'binary' ) + cipher . final ( 'binary' ) ;
0 commit comments