forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tls: accept SecureContext object in server.addContext()
Do not call tls.createSecureContext() if the context provided is already an instance of tls.SecureContext. Fixes: nodejs#47408 PR-URL: nodejs#47570 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
1ad41cd
commit 9fc9c85
Showing
3 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
const tls = require('tls'); | ||
|
||
function loadPEM(n) { | ||
return fixtures.readKey(`${n}.pem`); | ||
} | ||
|
||
const serverOptions = { | ||
key: loadPEM('agent2-key'), | ||
cert: loadPEM('agent2-cert'), | ||
ca: [ loadPEM('ca2-cert') ], | ||
requestCert: true, | ||
rejectUnauthorized: false, | ||
}; | ||
|
||
let connections = 0; | ||
|
||
const server = tls.createServer(serverOptions, (c) => { | ||
if (++connections === 3) { | ||
server.close(); | ||
} | ||
if (c.servername === 'unknowncontext') { | ||
assert.strictEqual(c.authorized, false); | ||
return; | ||
} | ||
assert.strictEqual(c.authorized, true); | ||
}); | ||
|
||
const secureContext = { | ||
key: loadPEM('agent1-key'), | ||
cert: loadPEM('agent1-cert'), | ||
ca: [ loadPEM('ca1-cert') ], | ||
}; | ||
server.addContext('context1', secureContext); | ||
server.addContext('context2', tls.createSecureContext(secureContext)); | ||
|
||
const clientOptionsBase = { | ||
key: loadPEM('agent1-key'), | ||
cert: loadPEM('agent1-cert'), | ||
ca: [ loadPEM('ca1-cert') ], | ||
rejectUnauthorized: false, | ||
}; | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const client1 = tls.connect({ | ||
...clientOptionsBase, | ||
port: server.address().port, | ||
servername: 'context1', | ||
}, common.mustCall(() => { | ||
client1.end(); | ||
})); | ||
|
||
const client2 = tls.connect({ | ||
...clientOptionsBase, | ||
port: server.address().port, | ||
servername: 'context2', | ||
}, common.mustCall(() => { | ||
client2.end(); | ||
})); | ||
|
||
const client3 = tls.connect({ | ||
...clientOptionsBase, | ||
port: server.address().port, | ||
servername: 'unknowncontext', | ||
}, common.mustCall(() => { | ||
client3.end(); | ||
})); | ||
})); |