|
1 | 1 | var crypto = require('crypto'); |
2 | | -var async = require('async'); |
3 | 2 | var xmldom = require('xmldom'); |
4 | 3 | var xpath = require('xpath'); |
5 | 4 | var utils = require('./utils'); |
@@ -59,60 +58,83 @@ function encrypt(content, options, callback) { |
59 | 58 |
|
60 | 59 | options.input_encoding = options.input_encoding || 'utf8'; |
61 | 60 |
|
62 | | - async.waterfall([ |
63 | | - function generate_symmetric_key(cb) { |
64 | | - switch (options.encryptionAlgorithm) { |
65 | | - case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc': |
66 | | - crypto.randomBytes(16, cb); // generate a symmetric random key 16 bytes length |
67 | | - break; |
68 | | - case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc': |
69 | | - crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length |
70 | | - break; |
71 | | - case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc': |
72 | | - crypto.randomBytes(24, cb); // generate a symmetric random key 24 bytes (192 bits) length |
73 | | - break; |
74 | | - default: |
75 | | - crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length |
76 | | - } |
77 | | - }, |
78 | | - function encrypt_content(symmetricKey, cb) { |
79 | | - switch (options.encryptionAlgorithm) { |
80 | | - case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc': |
81 | | - encryptWithAlgorithm('aes-128-cbc', symmetricKey, 16, content, options.input_encoding, function (err, encryptedContent) { |
82 | | - if (err) return cb(err); |
83 | | - cb(null, symmetricKey, encryptedContent); |
84 | | - }); |
85 | | - break; |
86 | | - case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc': |
87 | | - encryptWithAlgorithm('aes-256-cbc', symmetricKey, 16, content, options.input_encoding, function (err, encryptedContent) { |
88 | | - if (err) return cb(err); |
89 | | - cb(null, symmetricKey, encryptedContent); |
90 | | - }); |
91 | | - break; |
92 | | - case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc': |
93 | | - encryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, content, options.input_encoding, function (err, encryptedContent) { |
94 | | - if (err) return cb(err); |
95 | | - cb(null, symmetricKey, encryptedContent); |
96 | | - }); |
97 | | - break; |
98 | | - default: |
99 | | - cb(new Error('encryption algorithm not supported')); |
100 | | - } |
101 | | - }, |
102 | | - function encrypt_key(symmetricKey, encryptedContent, cb) { |
103 | | - encryptKeyInfo(symmetricKey, options, function(err, keyInfo) { |
104 | | - if (err) return cb(err); |
105 | | - |
106 | | - var result = utils.renderTemplate('encrypted-key', { |
107 | | - encryptedContent: encryptedContent.toString('base64'), |
108 | | - keyInfo: keyInfo, |
109 | | - contentEncryptionMethod: options.encryptionAlgorithm |
| 61 | + function generate_symmetric_key(cb) { |
| 62 | + switch (options.encryptionAlgorithm) { |
| 63 | + case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc': |
| 64 | + crypto.randomBytes(16, cb); // generate a symmetric random key 16 bytes length |
| 65 | + break; |
| 66 | + case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc': |
| 67 | + crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length |
| 68 | + break; |
| 69 | + case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc': |
| 70 | + crypto.randomBytes(24, cb); // generate a symmetric random key 24 bytes (192 bits) length |
| 71 | + break; |
| 72 | + default: |
| 73 | + crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + function encrypt_content(symmetricKey, cb) { |
| 78 | + switch (options.encryptionAlgorithm) { |
| 79 | + case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc': |
| 80 | + encryptWithAlgorithm('aes-128-cbc', symmetricKey, 16, content, options.input_encoding, function (err, encryptedContent) { |
| 81 | + if (err) return cb(err); |
| 82 | + cb(null, encryptedContent); |
| 83 | + }); |
| 84 | + break; |
| 85 | + case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc': |
| 86 | + encryptWithAlgorithm('aes-256-cbc', symmetricKey, 16, content, options.input_encoding, function (err, encryptedContent) { |
| 87 | + if (err) return cb(err); |
| 88 | + cb(null, encryptedContent); |
110 | 89 | }); |
| 90 | + break; |
| 91 | + case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc': |
| 92 | + encryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, content, options.input_encoding, function (err, encryptedContent) { |
| 93 | + if (err) return cb(err); |
| 94 | + cb(null, encryptedContent); |
| 95 | + }); |
| 96 | + break; |
| 97 | + default: |
| 98 | + cb(new Error('encryption algorithm not supported')); |
| 99 | + } |
| 100 | + } |
111 | 101 |
|
112 | | - cb(null, result); |
| 102 | + function encrypt_key(symmetricKey, encryptedContent, cb) { |
| 103 | + encryptKeyInfo(symmetricKey, options, function(err, keyInfo) { |
| 104 | + if (err) return cb(err); |
| 105 | + |
| 106 | + var result = utils.renderTemplate('encrypted-key', { |
| 107 | + encryptedContent: encryptedContent.toString('base64'), |
| 108 | + keyInfo: keyInfo, |
| 109 | + contentEncryptionMethod: options.encryptionAlgorithm |
113 | 110 | }); |
| 111 | + |
| 112 | + cb(null, result); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + generate_symmetric_key(function (genKeyError, symmetricKey) { |
| 118 | + if (genKeyError) { |
| 119 | + return callback(genKeyError); |
114 | 120 | } |
115 | | - ], callback); |
| 121 | + |
| 122 | + encrypt_content(symmetricKey, function(encryptContentError, encryptedContent) { |
| 123 | + if (encryptContentError) { |
| 124 | + return callback(encryptContentError); |
| 125 | + } |
| 126 | + |
| 127 | + encrypt_key(symmetricKey, encryptedContent, function (encryptKeyError, result) { |
| 128 | + if (encryptKeyError) { |
| 129 | + return callback(encryptKeyError); |
| 130 | + } |
| 131 | + |
| 132 | + callback(null, result); |
| 133 | + }); |
| 134 | + |
| 135 | + }); |
| 136 | + |
| 137 | + }); |
116 | 138 | } |
117 | 139 |
|
118 | 140 | function decrypt(xml, options, callback) { |
|
0 commit comments