Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: support reading multiple cas from one input #4099

Merged
merged 2 commits into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,26 +453,6 @@ static BIO* LoadBIO(Environment* env, Local<Value> v) {
}


// Takes a string or buffer and loads it into an X509
// Caller responsible for X509_free-ing the returned object.
static X509* LoadX509(Environment* env, Local<Value> v) {
HandleScope scope(env->isolate());

BIO *bio = LoadBIO(env, v);
if (!bio)
return nullptr;

X509 * x509 = PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr);
if (!x509) {
BIO_free_all(bio);
return nullptr;
}

BIO_free_all(bio);
return x509;
}


void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -668,16 +648,19 @@ void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
newCAStore = true;
}

X509* x509 = LoadX509(env, args[0]);
if (!x509)
return;

X509_STORE_add_cert(sc->ca_store_, x509);
SSL_CTX_add_client_CA(sc->ctx_, x509);

X509_free(x509);
unsigned cert_count = 0;
if (BIO* bio = LoadBIO(env, args[0])) {
while (X509* x509 = // NOLINT(whitespace/if-one-line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do while (true) here, and break in loop if x509 === nullptr. I don't think that this deserves disabling lint rules.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NOLINT works around what I suspect is a bug in the lint rule. If you put the while on a single line (and s/nullptr/0/ to keep it < 80 columns) it won't trigger.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, it doesn't look like a good code style to me, splitting while's condition between lines. That's just my opinion, though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @trevnorris - you can be the arbitrator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the following not possible?

  if (BIO* bio = LoadBIO(env, args[0])) {
    X509* x509;
    while (x509 = PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr)) {
     // ...

If not, I'd say how it is now isn't the prettiest but personally find it easier to logically follow. Which wins for me.

PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr)) {
X509_STORE_add_cert(sc->ca_store_, x509);
SSL_CTX_add_client_CA(sc->ctx_, x509);
X509_free(x509);
cert_count += 1;
}
BIO_free_all(bio);
}

if (newCAStore) {
if (cert_count > 0 && newCAStore) {
SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-tls-two-cas-one-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const common = require('../common');
const tls = require('tls');
const fs = require('fs');

const ca1 =
fs.readFileSync(`${common.fixturesDir}/keys/ca1-cert.pem`, `utf8`);
const ca2 =
fs.readFileSync(`${common.fixturesDir}/keys/ca2-cert.pem`, `utf8`);
const cert =
fs.readFileSync(`${common.fixturesDir}/keys/agent3-cert.pem`, `utf8`);
const key =
fs.readFileSync(`${common.fixturesDir}/keys/agent3-key.pem`, `utf8`);

function test(ca, next) {
const server = tls.createServer({ ca, cert, key }, function(conn) {
this.close();
conn.end();
});

server.addContext('agent3', { ca, cert, key });

const host = common.localhostIPv4;
const port = common.PORT;
server.listen(port, host, function() {
tls.connect({ servername: 'agent3', host, port, ca });
});

server.once('close', next);
}

const array = [ca1, ca2];
const string = ca1 + '\n' + ca2;
test(array, () => test(string, () => {}));
1 change: 1 addition & 0 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
'whitespace/comments',
'whitespace/end_of_line',
'whitespace/ending_newline',
'whitespace/if-one-line',
'whitespace/indent',
'whitespace/labels',
'whitespace/line_length',
Expand Down