@@ -11,21 +11,24 @@ const constants = require('crypto').constants;
11
11
const crypto = require ( 'crypto' ) ;
12
12
13
13
// Test certificates
14
- var certPem = fs . readFileSync ( common . fixturesDir + '/test_cert.pem' , 'ascii' ) ;
15
- var keyPem = fs . readFileSync ( common . fixturesDir + '/test_key.pem' , 'ascii' ) ;
16
- var rsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_pubkey.pem' ,
17
- 'ascii' ) ;
18
- var rsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_privkey.pem' ,
19
- 'ascii' ) ;
20
- var rsaKeyPemEncrypted = fs . readFileSync (
14
+ const certPem = fs . readFileSync ( common . fixturesDir + '/test_cert.pem' , 'ascii' ) ;
15
+ const keyPem = fs . readFileSync ( common . fixturesDir + '/test_key.pem' , 'ascii' ) ;
16
+ const rsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_pubkey.pem' ,
17
+ 'ascii' ) ;
18
+ const rsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_rsa_privkey.pem' ,
19
+ 'ascii' ) ;
20
+ const rsaKeyPemEncrypted = fs . readFileSync (
21
21
common . fixturesDir + '/test_rsa_privkey_encrypted.pem' , 'ascii' ) ;
22
- var dsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_dsa_pubkey.pem' ,
23
- 'ascii' ) ;
24
- var dsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_dsa_privkey.pem' ,
25
- 'ascii' ) ;
26
- var dsaKeyPemEncrypted = fs . readFileSync (
22
+ const dsaPubPem = fs . readFileSync ( common . fixturesDir + '/test_dsa_pubkey.pem' ,
23
+ 'ascii' ) ;
24
+ const dsaKeyPem = fs . readFileSync ( common . fixturesDir + '/test_dsa_privkey.pem' ,
25
+ 'ascii' ) ;
26
+ const dsaKeyPemEncrypted = fs . readFileSync (
27
27
common . fixturesDir + '/test_dsa_privkey_encrypted.pem' , 'ascii' ) ;
28
28
29
+ const decryptError = new RegExp ( '^Error: error:06065064:digital envelope ' +
30
+ 'routines:EVP_DecryptFinal_ex:bad decrypt$' ) ;
31
+
29
32
// Test RSA encryption/decryption
30
33
{
31
34
const input = 'I AM THE WALRUS' ;
@@ -34,13 +37,13 @@ var dsaKeyPemEncrypted = fs.readFileSync(
34
37
let encryptedBuffer = crypto . publicEncrypt ( rsaPubPem , bufferToEncrypt ) ;
35
38
36
39
let decryptedBuffer = crypto . privateDecrypt ( rsaKeyPem , encryptedBuffer ) ;
37
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
40
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
38
41
39
42
let decryptedBufferWithPassword = crypto . privateDecrypt ( {
40
43
key : rsaKeyPemEncrypted ,
41
44
passphrase : 'password'
42
45
} , encryptedBuffer ) ;
43
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
46
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
44
47
45
48
encryptedBuffer = crypto . publicEncrypt ( {
46
49
key : rsaKeyPemEncrypted ,
@@ -51,7 +54,7 @@ var dsaKeyPemEncrypted = fs.readFileSync(
51
54
key : rsaKeyPemEncrypted ,
52
55
passphrase : 'password'
53
56
} , encryptedBuffer ) ;
54
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
57
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
55
58
56
59
encryptedBuffer = crypto . privateEncrypt ( {
57
60
key : rsaKeyPemEncrypted ,
@@ -62,116 +65,114 @@ var dsaKeyPemEncrypted = fs.readFileSync(
62
65
key : rsaKeyPemEncrypted ,
63
66
passphrase : Buffer . from ( 'password' )
64
67
} , encryptedBuffer ) ;
65
- assert . strictEqual ( input , decryptedBufferWithPassword . toString ( ) ) ;
68
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
66
69
67
70
encryptedBuffer = crypto . publicEncrypt ( certPem , bufferToEncrypt ) ;
68
71
69
72
decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
70
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
73
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
71
74
72
75
encryptedBuffer = crypto . publicEncrypt ( keyPem , bufferToEncrypt ) ;
73
76
74
77
decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
75
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
78
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
76
79
77
80
encryptedBuffer = crypto . privateEncrypt ( keyPem , bufferToEncrypt ) ;
78
81
79
82
decryptedBuffer = crypto . publicDecrypt ( keyPem , encryptedBuffer ) ;
80
- assert . strictEqual ( input , decryptedBuffer . toString ( ) ) ;
83
+ assert . strictEqual ( decryptedBuffer . toString ( ) , input ) ;
81
84
82
- assert . throws ( function ( ) {
85
+ assert . throws ( ( ) => {
83
86
crypto . privateDecrypt ( {
84
87
key : rsaKeyPemEncrypted ,
85
88
passphrase : 'wrong'
86
89
} , bufferToEncrypt ) ;
87
- } ) ;
90
+ } , decryptError ) ;
88
91
89
- assert . throws ( function ( ) {
92
+ assert . throws ( ( ) => {
90
93
crypto . publicEncrypt ( {
91
94
key : rsaKeyPemEncrypted ,
92
95
passphrase : 'wrong'
93
96
} , encryptedBuffer ) ;
94
- } ) ;
97
+ } , decryptError ) ;
95
98
96
99
encryptedBuffer = crypto . privateEncrypt ( {
97
100
key : rsaKeyPemEncrypted ,
98
101
passphrase : Buffer . from ( 'password' )
99
102
} , bufferToEncrypt ) ;
100
103
101
- assert . throws ( function ( ) {
104
+ assert . throws ( ( ) => {
102
105
crypto . publicDecrypt ( {
103
106
key : rsaKeyPemEncrypted ,
104
107
passphrase : [ ] . concat . apply ( [ ] , Buffer . from ( 'password' ) )
105
108
} , encryptedBuffer ) ;
106
- } ) ;
109
+ } , decryptError ) ;
107
110
}
108
111
109
112
function test_rsa ( padding ) {
110
- var input = Buffer . allocUnsafe ( padding === 'RSA_NO_PADDING' ? 1024 / 8 : 32 ) ;
111
- for ( var i = 0 ; i < input . length ; i ++ )
113
+ const size = ( padding === 'RSA_NO_PADDING' ) ? 1024 / 8 : 32 ;
114
+ const input = Buffer . allocUnsafe ( size ) ;
115
+ for ( let i = 0 ; i < input . length ; i ++ )
112
116
input [ i ] = ( i * 7 + 11 ) & 0xff ;
113
- var bufferToEncrypt = Buffer . from ( input ) ;
117
+ const bufferToEncrypt = Buffer . from ( input ) ;
114
118
115
119
padding = constants [ padding ] ;
116
120
117
- var encryptedBuffer = crypto . publicEncrypt ( {
121
+ const encryptedBuffer = crypto . publicEncrypt ( {
118
122
key : rsaPubPem ,
119
123
padding : padding
120
124
} , bufferToEncrypt ) ;
121
125
122
- var decryptedBuffer = crypto . privateDecrypt ( {
126
+ const decryptedBuffer = crypto . privateDecrypt ( {
123
127
key : rsaKeyPem ,
124
128
padding : padding
125
129
} , encryptedBuffer ) ;
126
- assert . equal ( input , decryptedBuffer . toString ( ) ) ;
130
+ assert . deepStrictEqual ( decryptedBuffer , input ) ;
127
131
}
128
132
129
133
test_rsa ( 'RSA_NO_PADDING' ) ;
130
134
test_rsa ( 'RSA_PKCS1_PADDING' ) ;
131
135
test_rsa ( 'RSA_PKCS1_OAEP_PADDING' ) ;
132
136
133
137
// Test RSA key signing/verification
134
- var rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
135
- var rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
138
+ let rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
139
+ let rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
136
140
assert . ok ( rsaSign ) ;
137
141
assert . ok ( rsaVerify ) ;
138
142
143
+ const expectedSignature =
144
+ '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
145
+ '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
146
+ 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
147
+ '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
148
+ '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ;
149
+
139
150
rsaSign . update ( rsaPubPem ) ;
140
- var rsaSignature = rsaSign . sign ( rsaKeyPem , 'hex' ) ;
141
- assert . equal ( rsaSignature ,
142
- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
143
- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
144
- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
145
- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
146
- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
151
+ let rsaSignature = rsaSign . sign ( rsaKeyPem , 'hex' ) ;
152
+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
147
153
148
154
rsaVerify . update ( rsaPubPem ) ;
149
155
assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
150
156
151
157
// Test RSA key signing/verification with encrypted key
152
158
rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
153
159
rsaSign . update ( rsaPubPem ) ;
154
- assert . doesNotThrow ( function ( ) {
160
+ assert . doesNotThrow ( ( ) => {
155
161
var signOptions = { key : rsaKeyPemEncrypted , passphrase : 'password' } ;
156
162
rsaSignature = rsaSign . sign ( signOptions , 'hex' ) ;
157
163
} ) ;
158
- assert . equal ( rsaSignature ,
159
- '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' +
160
- '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' +
161
- 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' +
162
- '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' +
163
- '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6' ) ;
164
+ assert . strictEqual ( rsaSignature , expectedSignature ) ;
164
165
165
166
rsaVerify = crypto . createVerify ( 'RSA-SHA1' ) ;
166
167
rsaVerify . update ( rsaPubPem ) ;
167
168
assert . strictEqual ( rsaVerify . verify ( rsaPubPem , rsaSignature , 'hex' ) , true ) ;
168
169
169
170
rsaSign = crypto . createSign ( 'RSA-SHA1' ) ;
170
171
rsaSign . update ( rsaPubPem ) ;
171
- assert . throws ( function ( ) {
172
+ assert . throws ( ( ) => {
172
173
var signOptions = { key : rsaKeyPemEncrypted , passphrase : 'wrong' } ;
173
174
rsaSign . sign ( signOptions , 'hex' ) ;
174
- } ) ;
175
+ } , decryptError ) ;
175
176
176
177
//
177
178
// Test RSA signing and verification
@@ -196,7 +197,7 @@ assert.throws(function() {
196
197
sign . update ( input ) ;
197
198
198
199
const output = sign . sign ( privateKey , 'hex' ) ;
199
- assert . strictEqual ( output , signature ) ;
200
+ assert . strictEqual ( signature , output ) ;
200
201
201
202
const verify = crypto . createVerify ( 'RSA-SHA256' ) ;
202
203
verify . update ( input ) ;
@@ -232,9 +233,9 @@ const input = 'I AM THE WALRUS';
232
233
{
233
234
const sign = crypto . createSign ( 'DSS1' ) ;
234
235
sign . update ( input ) ;
235
- assert . throws ( function ( ) {
236
+ assert . throws ( ( ) => {
236
237
sign . sign ( { key : dsaKeyPemEncrypted , passphrase : 'wrong' } , 'hex' ) ;
237
- } ) ;
238
+ } , decryptError ) ;
238
239
}
239
240
240
241
{
@@ -244,7 +245,7 @@ const input = 'I AM THE WALRUS';
244
245
sign . update ( input ) ;
245
246
246
247
let signature ;
247
- assert . doesNotThrow ( function ( ) {
248
+ assert . doesNotThrow ( ( ) => {
248
249
const signOptions = { key : dsaKeyPemEncrypted , passphrase : 'password' } ;
249
250
signature = sign . sign ( signOptions , 'hex' ) ;
250
251
} ) ;
0 commit comments