diff --git a/src/node_constants.cc b/src/node_constants.cc index bdbef2bfbc54d4..2f98c619ada939 100644 --- a/src/node_constants.cc +++ b/src/node_constants.cc @@ -795,11 +795,40 @@ void DefinePriorityConstants(Local 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 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); + 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 diff --git a/test/parallel/test-crypto-oids.js b/test/parallel/test-crypto-oids.js new file mode 100644 index 00000000000000..aa72ef51abef9d --- /dev/null +++ b/test/parallel/test-crypto-oids.js @@ -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');