tls: add getter and setter for session ticket number. by mkrawczuk · Pull Request #34020 · nodejs/node · GitHub
Skip to content
Closed
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
28 changes: 28 additions & 0 deletions doc/api/tls.md
4 changes: 4 additions & 0 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ exports.createSecureContext = function createSecureContext(options) {
options.clientCertEngine);
}

if (options.numTickets) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This won't let you set it to 0.

c.context.setNumTickets(options.numTickets);
}

return c;
};

Expand Down
14 changes: 13 additions & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,9 @@ Server.prototype.setSecureContext = function(options) {
.slice(0, 32);
}

if (options.numTickets)
this.numTickets = options.numTickets;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditto, plus it introduces a performance gotcha in that it creates two hidden classes: one with the property, one without. Always set the property.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey @bnoordhuis could you please clarify how it creates two hidden classes: one with the property, one without?


this._sharedCreds = tls.createSecureContext({
pfx: this.pfx,
key: this.key,
Expand All @@ -1332,7 +1335,8 @@ Server.prototype.setSecureContext = function(options) {
secureOptions: this.secureOptions,
honorCipherOrder: this.honorCipherOrder,
crl: this.crl,
sessionIdContext: this.sessionIdContext
sessionIdContext: this.sessionIdContext,
numTickets: this.numTickets
});

if (this.sessionTimeout)
Expand Down Expand Up @@ -1366,6 +1370,14 @@ Server.prototype.setTicketKeys = function setTicketKeys(keys) {
this._sharedCreds.context.setTicketKeys(keys);
};

Server.prototype.getNumTickets = function getNumTickets() {
return this._sharedCreds.context.getNumTickets();
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm.. I thought I had left a review comment on this previously but I'm not seeing it now... Stylistically, I'd much prefer these to get regular getter/setters (e.g. server.numTickets = 1) rather than separate functions like this.



Server.prototype.setNumTickets = function setNumTickets(numTickets) {
this._sharedCreds.context.setNumTickets(numTickets);
Comment thread
mkrawczuk marked this conversation as resolved.
};

Server.prototype.setOptions = deprecate(function(options) {
this.requestCert = options.requestCert === true;
Expand Down
28 changes: 28 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
env->SetProtoMethod(t, "enableTicketKeyCallback", EnableTicketKeyCallback);
env->SetProtoMethodNoSideEffect(t, "getCertificate", GetCertificate<true>);
env->SetProtoMethodNoSideEffect(t, "getIssuer", GetCertificate<false>);
env->SetProtoMethodNoSideEffect(t, "getNumTickets", GetNumTickets);
env->SetProtoMethod(t, "setNumTickets", SetNumTickets);

#define SET_INTEGER_CONSTANTS(name, value) \
t->Set(FIXED_ONE_BYTE_STRING(env->isolate(), name), \
Expand Down Expand Up @@ -1558,6 +1560,32 @@ void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) {
}


void SecureContext::GetNumTickets(const FunctionCallbackInfo<Value>& args) {
SecureContext* sc;
ASSIGN_OR_RETURN_UNWRAP(&sc, args.Holder());

CHECK_EQ(args.Length(), 0);

uint32_t num_tickets = SSL_CTX_get_num_tickets(sc->ctx_.get());
args.GetReturnValue().Set(num_tickets);
}


void SecureContext::SetNumTickets(const FunctionCallbackInfo<Value>& args) {
SecureContext* sc;
ASSIGN_OR_RETURN_UNWRAP(&sc, args.Holder());

if (args.Length() != 1 || !args[0]->IsUint32()) {
return THROW_ERR_INVALID_ARG_TYPE(
sc->env(), "Number of tickets must be an unsigned 32-bit integer");
}

uint32_t numTickets = args[0].As<Uint32>()->Value();

CHECK(SSL_CTX_set_num_tickets(sc->ctx_.get(), numTickets));
Comment on lines +1583 to +1585

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
uint32_t numTickets = args[0].As<Uint32>()->Value();
CHECK(SSL_CTX_set_num_tickets(sc->ctx_.get(), numTickets));
uint32_t num_tickets = args[0].As<Uint32>()->Value();
CHECK_EQ(1, SSL_CTX_set_num_tickets(sc->ctx_.get(), num_tickets));

}


void SecureContext::SetFreeListLength(const FunctionCallbackInfo<Value>& args) {
}

Expand Down
2 changes: 2 additions & 0 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class SecureContext final : public BaseObject {
#endif // !OPENSSL_NO_ENGINE
static void GetTicketKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetTicketKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetNumTickets(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetNumTickets(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetFreeListLength(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void EnableTicketKeyCallback(
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-tls-num-tickets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'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');

const server = tls.createServer({
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
});

const expectedNumTickets = 1;
// 2 is the deafult value set by OpenSSL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change

assert.strictEqual(server.getNumTickets(), 2);
// Change the number of sent session tickets.
server.setNumTickets(expectedNumTickets);
// Ensure that it has indeed been changed.
assert.strictEqual(server.getNumTickets(), expectedNumTickets);
// Ensure that an error is thrown when attempting to set the number of tickets
// to a negative number.
assert.throws(
() => server.setNumTickets(-1),
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'Number of tickets must be an unsigned 32-bit integer'
}
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you check multiple values, e.g.:

for (const expectedNumTickets of [0, 1, 2, 42, 1337, 2 ** 32 - 1]) {
  // ...
}

Checking that 2 ** 32 throws would be good, too.


// Ensure that an error is thrown when attempting to set the number of tickets
// to Mongolian throat singing.
assert.throws(
() => server.setNumTickets('HURRMMMM'),
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'Number of tickets must be an unsigned 32-bit integer'
}
);