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
29 changes: 29 additions & 0 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,40 @@ void DefinePriorityConstants(Local<Object> target) {
#endif
}

// Convert nid's to the string representation of their OID. Non-reentrant, and
// will abort if called with invalid nids (so only pass values from OpenSSL's
// headers).
static const char* OBJ_nid2oid(int nid, char (*buf)[128]) {
ASN1_OBJECT* obj = OBJ_nid2obj(nid);
CHECK_NOT_NULL(obj);
CHECK_EQ(sizeof(*buf), 128);
CHECK_LE(OBJ_obj2txt(*buf, 128, obj, 1), 128);
return *buf;
}

void DefineCryptoConstants(Local<Object> target) {
#ifdef OPENSSL_VERSION_NUMBER
NODE_DEFINE_CONSTANT(target, OPENSSL_VERSION_NUMBER);
#endif

#define NID2OID(nid) do { \
char buf[128]; \
NODE_DEFINE_STRING_CONSTANT(target, #nid, OBJ_nid2oid(nid, &buf)); \
} while (false)

NID2OID(EVP_PKEY_RSA);
NID2OID(EVP_PKEY_RSA_PSS);
NID2OID(EVP_PKEY_DSA);
NID2OID(EVP_PKEY_DH);
NID2OID(EVP_PKEY_EC);
// Note for backporters: following are new in openssl 1.1.1.
NID2OID(EVP_PKEY_ED25519);
Copy link
Member

Choose a reason for hiding this comment

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

Wrap these in #ifdef EVP_PKEY_ED25519 etc.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

master doesn't support anything earlier than openssl 1.1.1, and I didn't want to introduce the possibility that someone might try and get a superficially working build. This is constructed this way (without the #define EVPS(V) V( EVP_PKEY_DH) idiom so that when this backports to 11.x or 10.x, it will be trivial to put in the macros.

NID2OID(EVP_PKEY_ED448);
NID2OID(EVP_PKEY_X25519);
NID2OID(EVP_PKEY_X448);

#undef NID2OID

#ifdef SSL_OP_ALL
NODE_DEFINE_CONSTANT(target, SSL_OP_ALL);
#endif
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-crypto-oids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const {
constants,
} = require('crypto');

assert.strictEqual(constants.EVP_PKEY_RSA, '1.2.840.113549.1.1.1');
assert.strictEqual(constants.EVP_PKEY_RSA_PSS, '1.2.840.113549.1.1.10');
assert.strictEqual(constants.EVP_PKEY_DSA, '1.2.840.10040.4.1');
assert.strictEqual(constants.EVP_PKEY_DH, '1.2.840.113549.1.3.1');
assert.strictEqual(constants.EVP_PKEY_EC, '1.2.840.10045.2.1');
assert.strictEqual(constants.EVP_PKEY_ED25519, '1.3.101.112');
assert.strictEqual(constants.EVP_PKEY_ED448, '1.3.101.113');
assert.strictEqual(constants.EVP_PKEY_X25519, '1.3.101.110');
assert.strictEqual(constants.EVP_PKEY_X448, '1.3.101.111');