GeezerVison is showing github.com — tls: allow obvious key/passphrase combinations · nodejs/node@df9d8ee
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/tls.md +7-6Lines changed: 7 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -891,12 +891,13 @@ added: v0.11.13 | 891 | 891 | individually. PFX is usually encrypted, if it is, `passphrase` will be used | 892 | 892 | to decrypt it. | 893 | 893 | * `key` {string|string[]|Buffer|Buffer[]|Object[]} Optional private keys in | 894 | | - PEM format. Single keys will be decrypted with `passphrase` if necessary. | 895 | | - Multiple keys, probably using different algorithms, can be provided either | 896 | | - as an array of unencrypted key strings or buffers, or an array of objects in | 897 | | - the form `{pem: <string|buffer>, passphrase: <string>}`. The object form can | 898 | | - only occur in an array, and it _must_ include a passphrase, even if key is | 899 | | - not encrypted. | | 894 | + PEM format. PEM allows the option of private keys being encrypted. Encrypted | | 895 | + keys will be decrypted with `options.passphrase`. Multiple keys using | | 896 | + different algorithms can be provided either as an array of unencrypted key | | 897 | + strings or buffers, or an array of objects in the form `{pem: | | 898 | + <string|buffer>[, passphrase: <string>]}`. The object form can only occur in | | 899 | + an array. `object.passphrase` is optional. Encrypted keys will be decrypted | | 900 | + with `object.passphrase` if provided, or `options.passphrase` if it is not. | 900 | 901 | * `passphrase` {string} Optional shared passphrase used for a single private | 901 | 902 | key and/or a PFX. | 902 | 903 | * `cert` {string|string[]|Buffer|Buffer[]} Optional cert chains in PEM format. | |
Collapse file Copy file name to clipboardExpand all lines: lib/_tls_common.js +3-9Lines changed: 3 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,17 +78,11 @@ exports.createSecureContext = function createSecureContext(options, context) { | 78 | 78 | if (Array.isArray(options.key)) { | 79 | 79 | for (i = 0; i < options.key.length; i++) { | 80 | 80 | const key = options.key[i]; | 81 | | - if (key.passphrase) | 82 | | - c.context.setKey(key.pem, key.passphrase); | 83 | | - else | 84 | | - c.context.setKey(key); | | 81 | + const passphrase = key.passphrase || options.passphrase; | | 82 | + c.context.setKey(key.pem || key, passphrase); | 85 | 83 | } | 86 | 84 | } else { | 87 | | - if (options.passphrase) { | 88 | | - c.context.setKey(options.key, options.passphrase); | 89 | | - } else { | 90 | | - c.context.setKey(options.key); | 91 | | - } | | 85 | + c.context.setKey(options.key, options.passphrase); | 92 | 86 | } | 93 | 87 | } | 94 | 88 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -446,7 +446,10 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) { | 446 | 446 | } | 447 | 447 |
| 448 | 448 | if (len == 2) { | 449 | | - THROW_AND_RETURN_IF_NOT_STRING(args[1], "Pass phrase"); | | 449 | + if (args[1]->IsUndefined() || args[1]->IsNull()) | | 450 | + len = 1; | | 451 | + else | | 452 | + THROW_AND_RETURN_IF_NOT_STRING(args[1], "Pass phrase"); | 450 | 453 | } | 451 | 454 |
| 452 | 455 | BIO *bio = LoadBIO(env, args[0]); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-tls-passphrase.js +83-14Lines changed: 83 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,21 +51,19 @@ server.listen(0, common.mustCall(function() { | 51 | 51 | tls.connect({ | 52 | 52 | port: this.address().port, | 53 | 53 | key: rawKey, | 54 | | - passphrase: 'passphrase', // Ignored. | | 54 | + passphrase: 'ignored', | 55 | 55 | cert: cert, | 56 | 56 | rejectUnauthorized: false | 57 | 57 | }, common.mustCall(function() {})); | 58 | 58 |
| 59 | 59 | // Buffer[] | 60 | | - /* XXX(sam) Should work, but its unimplemented ATM. | 61 | 60 | tls.connect({ | 62 | 61 | port: this.address().port, | 63 | 62 | key: [passKey], | 64 | 63 | passphrase: 'passphrase', | 65 | 64 | cert: [cert], | 66 | 65 | rejectUnauthorized: false | 67 | 66 | }, common.mustCall(function() {})); | 68 | | - */ | 69 | 67 |
| 70 | 68 | tls.connect({ | 71 | 69 | port: this.address().port, | @@ -77,7 +75,7 @@ server.listen(0, common.mustCall(function() { | 77 | 75 | tls.connect({ | 78 | 76 | port: this.address().port, | 79 | 77 | key: [rawKey], | 80 | | - passphrase: 'passphrase', // Ignored. | | 78 | + passphrase: 'ignored', | 81 | 79 | cert: [cert], | 82 | 80 | rejectUnauthorized: false | 83 | 81 | }, common.mustCall(function() {})); | @@ -101,21 +99,19 @@ server.listen(0, common.mustCall(function() { | 101 | 99 | tls.connect({ | 102 | 100 | port: this.address().port, | 103 | 101 | key: rawKey.toString(), | 104 | | - passphrase: 'passphrase', // Ignored. | | 102 | + passphrase: 'ignored', | 105 | 103 | cert: cert.toString(), | 106 | 104 | rejectUnauthorized: false | 107 | 105 | }, common.mustCall(function() {})); | 108 | 106 |
| 109 | 107 | // String[] | 110 | | - /* XXX(sam) Should work, but its unimplemented ATM. | 111 | 108 | tls.connect({ | 112 | 109 | port: this.address().port, | 113 | 110 | key: [passKey.toString()], | 114 | 111 | passphrase: 'passphrase', | 115 | 112 | cert: [cert.toString()], | 116 | 113 | rejectUnauthorized: false | 117 | 114 | }, common.mustCall(function() {})); | 118 | | - */ | 119 | 115 |
| 120 | 116 | tls.connect({ | 121 | 117 | port: this.address().port, | @@ -127,7 +123,7 @@ server.listen(0, common.mustCall(function() { | 127 | 123 | tls.connect({ | 128 | 124 | port: this.address().port, | 129 | 125 | key: [rawKey.toString()], | 130 | | - passphrase: 'passphrase', // Ignored. | | 126 | + passphrase: 'ignored', | 131 | 127 | cert: [cert.toString()], | 132 | 128 | rejectUnauthorized: false | 133 | 129 | }, common.mustCall(function() {})); | @@ -140,6 +136,22 @@ server.listen(0, common.mustCall(function() { | 140 | 136 | rejectUnauthorized: false | 141 | 137 | }, common.mustCall(function() {})); | 142 | 138 |
| | 139 | + tls.connect({ | | 140 | + port: this.address().port, | | 141 | + key: [{pem: passKey, passphrase: 'passphrase'}], | | 142 | + passphrase: 'ignored', | | 143 | + cert: cert, | | 144 | + rejectUnauthorized: false | | 145 | + }, common.mustCall(function() {})); | | 146 | + | | 147 | + tls.connect({ | | 148 | + port: this.address().port, | | 149 | + key: [{pem: passKey}], | | 150 | + passphrase: 'passphrase', | | 151 | + cert: cert, | | 152 | + rejectUnauthorized: false | | 153 | + }, common.mustCall(function() {})); | | 154 | + | 143 | 155 | tls.connect({ | 144 | 156 | port: this.address().port, | 145 | 157 | key: [{pem: passKey.toString(), passphrase: 'passphrase'}], | @@ -149,31 +161,30 @@ server.listen(0, common.mustCall(function() { | 149 | 161 |
| 150 | 162 | tls.connect({ | 151 | 163 | port: this.address().port, | 152 | | - key: [{pem: rawKey, passphrase: 'passphrase'}], | | 164 | + key: [{pem: rawKey, passphrase: 'ignored'}], | 153 | 165 | cert: cert, | 154 | 166 | rejectUnauthorized: false | 155 | 167 | }, common.mustCall(function() {})); | 156 | 168 |
| 157 | 169 | tls.connect({ | 158 | 170 | port: this.address().port, | 159 | | - key: [{pem: rawKey.toString(), passphrase: 'passphrase'}], | | 171 | + key: [{pem: rawKey.toString(), passphrase: 'ignored'}], | 160 | 172 | cert: cert, | 161 | 173 | rejectUnauthorized: false | 162 | 174 | }, common.mustCall(function() {})); | 163 | 175 |
| 164 | | - /* XXX(sam) Should work, but unimplemented ATM | 165 | 176 | tls.connect({ | 166 | 177 | port: this.address().port, | 167 | 178 | key: [{pem: rawKey}], | 168 | | - passphrase: 'passphrase', | | 179 | + passphrase: 'ignored', | 169 | 180 | cert: cert, | 170 | 181 | rejectUnauthorized: false | 171 | 182 | }, common.mustCall(function() {})); | 172 | 183 |
| 173 | 184 | tls.connect({ | 174 | 185 | port: this.address().port, | 175 | 186 | key: [{pem: rawKey.toString()}], | 176 | | - passphrase: 'passphrase', | | 187 | + passphrase: 'ignored', | 177 | 188 | cert: cert, | 178 | 189 | rejectUnauthorized: false | 179 | 190 | }, common.mustCall(function() {})); | @@ -191,9 +202,37 @@ server.listen(0, common.mustCall(function() { | 191 | 202 | cert: cert, | 192 | 203 | rejectUnauthorized: false | 193 | 204 | }, common.mustCall(function() {})); | 194 | | - */ | 195 | 205 | })).unref(); | 196 | 206 |
| | 207 | +// Missing passphrase | | 208 | +assert.throws(function() { | | 209 | + tls.connect({ | | 210 | + port: server.address().port, | | 211 | + key: passKey, | | 212 | + cert: cert, | | 213 | + rejectUnauthorized: false | | 214 | + }); | | 215 | +}, /bad password read/); | | 216 | + | | 217 | +assert.throws(function() { | | 218 | + tls.connect({ | | 219 | + port: server.address().port, | | 220 | + key: [passKey], | | 221 | + cert: cert, | | 222 | + rejectUnauthorized: false | | 223 | + }); | | 224 | +}, /bad password read/); | | 225 | + | | 226 | +assert.throws(function() { | | 227 | + tls.connect({ | | 228 | + port: server.address().port, | | 229 | + key: [{pem: passKey}], | | 230 | + cert: cert, | | 231 | + rejectUnauthorized: false | | 232 | + }); | | 233 | +}, /bad password read/); | | 234 | + | | 235 | +// Invalid passphrase | 197 | 236 | assert.throws(function() { | 198 | 237 | tls.connect({ | 199 | 238 | port: server.address().port, | @@ -203,3 +242,33 @@ assert.throws(function() { | 203 | 242 | rejectUnauthorized: false | 204 | 243 | }); | 205 | 244 | }, /bad decrypt/); | | 245 | + | | 246 | +assert.throws(function() { | | 247 | + tls.connect({ | | 248 | + port: server.address().port, | | 249 | + key: [passKey], | | 250 | + passphrase: 'invalid', | | 251 | + cert: cert, | | 252 | + rejectUnauthorized: false | | 253 | + }); | | 254 | +}, /bad decrypt/); | | 255 | + | | 256 | +assert.throws(function() { | | 257 | + tls.connect({ | | 258 | + port: server.address().port, | | 259 | + key: [{pem: passKey}], | | 260 | + passphrase: 'invalid', | | 261 | + cert: cert, | | 262 | + rejectUnauthorized: false | | 263 | + }); | | 264 | +}, /bad decrypt/); | | 265 | + | | 266 | +assert.throws(function() { | | 267 | + tls.connect({ | | 268 | + port: server.address().port, | | 269 | + key: [{pem: passKey, passphrase: 'invalid'}], | | 270 | + passphrase: 'passphrase', // Valid but unused | | 271 | + cert: cert, | | 272 | + rejectUnauthorized: false | | 273 | + }); | | 274 | +}, /bad decrypt/); |
|