forked from auth0/node-xml-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlenc.encryptedkey.js
251 lines (225 loc) · 9.32 KB
/
xmlenc.encryptedkey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var assert = require('assert');
var fs = require('fs');
var sinon = require('sinon');
var xmlenc = require('../lib');
describe('encrypt', function() {
let consoleSpy = null;
beforeEach(function() {
consoleSpy = sinon.spy(console, 'warn');
});
afterEach(function() {
consoleSpy.restore();
});
var algorithms = [{
name: 'aes-256-cbc',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
}, {
name: 'aes-128-cbc',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes128-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
}, {
name: 'aes-256-gcm',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes256-gcm',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
}, {
name: 'aes-128-gcm',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
},
{
name: 'aes-128-gcm with sha256',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
keyEncryptionDigest: 'sha256'
}
}, {
name: 'aes-128-gcm with sha512',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
keyEncryptionDigest: 'sha512'
}
}, {
name: 'des-ede3-cbc',
encryptionOptions: {
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
}
}];
algorithms.forEach(function (algorithm) {
describe(algorithm.name, function () {
it('should encrypt and decrypt xml', function (done) {
_shouldEncryptAndDecrypt('content to encrypt', algorithm.encryptionOptions, done);
});
it('should encrypt and decrypt xml with utf8 chars', function (done) {
_shouldEncryptAndDecrypt('Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge Gnügge', algorithm.encryptionOptions, done);
});
});
});
function _shouldEncryptAndDecrypt(content, options, done) {
// cert created with:
// openssl req -x509 -new -newkey rsa:2048 -nodes -subj '/CN=auth0.auth0.com/O=Auth0 LLC/C=US/ST=Washington/L=Redmond' -keyout auth0.key -out auth0.pem
// pub key extracted from (only the RSA public key between BEGIN PUBLIC KEY and END PUBLIC KEY)
// openssl x509 -in "test-auth0.pem" -pubkey
options.rsa_pub = fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
options.pem = fs.readFileSync(__dirname + '/test-auth0.pem'),
options.key = fs.readFileSync(__dirname + '/test-auth0.key'),
options.warnInsecureAlgorithm = false;
xmlenc.encrypt(content, options, function(err, result) {
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key'), warnInsecureAlgorithm: false}, function (err, decrypted) {
if (err) return done(err);
assert.equal(decrypted, content);
done();
});
});
}
describe('des-ede3-cbc fails', function() {
it('should fail encryption when disallowEncryptionWithInsecureAlgorithm is set', function(done) {
const options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
key: fs.readFileSync(__dirname + '/test-auth0.key'),
disallowEncryptionWithInsecureAlgorithm: true,
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
xmlenc.encrypt('encrypt me', options, function(err, result) {
assert(err);
assert(!result);
//should not pop up warns due to options.warnInsecureAlgorithm = false;
assert.equal(consoleSpy.called, false);
done();
});
});
it('should fail decryption when disallowDecryptionWithInsecureAlgorithm is set', function(done) {
const options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
key: fs.readFileSync(__dirname + '/test-auth0.key'),
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
}
xmlenc.encrypt('encrypt me', options, function(err, result) {
xmlenc.decrypt(result,
{ key: fs.readFileSync(__dirname + '/test-auth0.key'),
disallowDecryptionWithInsecureAlgorithm: true},
function (err, decrypted) {
assert(err);
assert(!decrypted);
done();
});
});
});
});
describe('rsa-1.5 fails', function() {
it('should fail encryption when disallowEncryptionWithInsecureAlgorithm is set', function(done) {
const options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
key: fs.readFileSync(__dirname + '/test-auth0.key'),
disallowEncryptionWithInsecureAlgorithm: true,
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
}
xmlenc.encrypt('encrypt me', options, function(err, result) {
assert(err);
assert(!result);
done();
});
});
it('should fail decryption when disallowDecryptionWithInsecureAlgorithm is set', function(done) {
const options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
key: fs.readFileSync(__dirname + '/test-auth0.key'),
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
}
xmlenc.encrypt('encrypt me', options, function(err, result) {
xmlenc.decrypt(result,
{ key: fs.readFileSync(__dirname + '/test-auth0.key'),
disallowDecryptionWithInsecureAlgorithm: true},
function (err, decrypted) {
assert(err);
assert(!decrypted);
done();
});
});
});
});
it('should encrypt and decrypt keyinfo', function (done) {
var options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p'
};
var plaintext = 'The quick brown fox jumps over the lazy dog';
xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
if (err) return done(err);
var decryptedKeyInfo = xmlenc.decryptKeyInfo(
encryptedKeyInfo,
{key: fs.readFileSync(__dirname + '/test-auth0.key')}
);
assert.equal(decryptedKeyInfo.toString(), plaintext);
done();
});
});
it('should decrypt xml with odd padding (aes256-cbc)', function (done) {
var encryptedContent = fs.readFileSync(__dirname + '/test-cbc256-padding.xml').toString()
xmlenc.decrypt(encryptedContent, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function(err, decrypted) {
assert.ifError(err);
assert.equal(decrypted, 'content');
done();
});
});
it('should catch error if padding length > 16', function (done) {
var encryptedContent = fs.readFileSync(__dirname + '/test-padding-length.xml').toString();
xmlenc.decrypt(encryptedContent, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function(err, decrypted) {
assert(err);
done();
});
});
it('should fail encrypt when disallowEncryptionWithInsecureAlgorithm is set', function (done) {
var options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5',
disallowEncryptionWithInsecureAlgorithm: true
};
var plaintext = 'The quick brown fox jumps over the lazy dog';
xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
assert(err);
done();
});
});
it('should encrypt and fail decrypt due to insecure algorithms', function (done) {
var options = {
rsa_pub: fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
pem: fs.readFileSync(__dirname + '/test-auth0.pem'),
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
};
var plaintext = 'The quick brown fox jumps over the lazy dog';
xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
if (err) return done(err);
assert.equal(consoleSpy.called, true);
assert.throws(
function(){xmlenc.decryptKeyInfo(
encryptedKeyInfo,
{key: fs.readFileSync(__dirname + '/test-auth0.key'),
disallowDecryptionWithInsecureAlgorithm: true})},
Error,
"Error thrown due to disallowing insecure algorithms.");
done();
});
});
});