-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: fix handling of root_cert_store.
SecureContext::AddRootCerts only parses the root certificates once and keeps the result in root_cert_store, a global X509_STORE. This change addresses the following issues: 1. SecureContext::AddCACert would add certificates to whatever X509_STORE was being used, even if that happened to be root_cert_store. Thus adding a CA certificate to a SecureContext would also cause it to be included in unrelated SecureContexts. 2. AddCRL would crash if neither AddRootCerts nor AddCACert had been called first. 3. Calling AddCACert without calling AddRootCerts first, and with an input that didn't contain any certificates, would leak an X509_STORE. 4. AddCRL would add the CRL to whatever X509_STORE was being used. Thus, like AddCACert, unrelated SecureContext objects could be affected. The following, non-obvious behaviour remains: calling AddRootCerts doesn't /add/ them, rather it sets the CA certs to be the root set and overrides any previous CA certificates. Points 1–3 are probably unimportant because the SecureContext is typically configured by `createSecureContext` in `lib/_tls_common.js`. This function either calls AddCACert or AddRootCerts and only calls AddCRL after setting up CA certificates. Point four could still apply in the unlikely case that someone configures a CRL without explicitly configuring the CAs.
- Loading branch information
Showing
4 changed files
with
160 additions
and
68 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
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,62 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fs = require('fs'); | ||
|
||
if (!common.hasCrypto) { | ||
common.skip('missing crypto'); | ||
return; | ||
} | ||
const tls = require('tls'); | ||
|
||
function filenamePEM(n) { | ||
return require('path').join(common.fixturesDir, 'keys', n + '.pem'); | ||
} | ||
|
||
function loadPEM(n) { | ||
return fs.readFileSync(filenamePEM(n)); | ||
} | ||
|
||
const caCert = loadPEM('ca1-cert'); | ||
const contextWithoutCert = tls.createSecureContext({}); | ||
const contextWithCert = tls.createSecureContext({}); | ||
// Adding a CA certificate to contextWithCert should not also add it to | ||
// contextWithoutCert. This is tested by trying to connect to a server that | ||
// depends on that CA using contextWithoutCert. | ||
contextWithCert.context.addCACert(caCert); | ||
|
||
const serverOptions = { | ||
key: loadPEM('agent1-key'), | ||
cert: loadPEM('agent1-cert'), | ||
}; | ||
const server = tls.createServer(serverOptions, function() {}); | ||
|
||
const clientOptions = { | ||
port: undefined, | ||
ca: [caCert], | ||
servername: 'agent1', | ||
rejectUnauthorized: true, | ||
}; | ||
|
||
function startTest() { | ||
// This client should fail to connect because it doesn't trust the CA | ||
// certificate. | ||
clientOptions.secureContext = contextWithoutCert; | ||
clientOptions.port = server.address().port; | ||
const client = tls.connect(clientOptions, common.fail); | ||
client.on('error', common.mustCall(() => { | ||
client.destroy(); | ||
|
||
// This time it should connect because contextWithCert includes the needed | ||
// CA certificate. | ||
clientOptions.secureContext = contextWithCert; | ||
const client2 = tls.connect(clientOptions, common.mustCall(() => { | ||
client2.destroy(); | ||
server.close(); | ||
})); | ||
client2.on('error', (e) => { | ||
console.log(e); | ||
}); | ||
})); | ||
} | ||
|
||
server.listen(0, startTest); |