Skip to content

Commit e159ce8

Browse files
committed
crypto: add OID constants for EVP_PKEY_ types
The existence of the constant can be used to check for support for the key types, and the value is useful when encoding/decoding keys using ASN.1.
1 parent 9b6b567 commit e159ce8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/node_constants.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,11 +795,40 @@ void DefinePriorityConstants(Local<Object> target) {
795795
#endif
796796
}
797797

798+
// Convert nid's to the string representation of their OID. Non-reentrant, and
799+
// will abort if called with invalid nids (so only pass values from OpenSSL's
800+
// headers).
801+
static const char* OBJ_nid2oid(int nid, char (*buf)[128]) {
802+
ASN1_OBJECT* obj = OBJ_nid2obj(nid);
803+
CHECK_NOT_NULL(obj);
804+
CHECK_EQ(sizeof(*buf), 128);
805+
CHECK_LE(OBJ_obj2txt(*buf, 128, obj, 1), 128);
806+
return *buf;
807+
}
808+
798809
void DefineCryptoConstants(Local<Object> target) {
799810
#ifdef OPENSSL_VERSION_NUMBER
800811
NODE_DEFINE_CONSTANT(target, OPENSSL_VERSION_NUMBER);
801812
#endif
802813

814+
#define NID2OID(nid) do { \
815+
char buf[128]; \
816+
NODE_DEFINE_STRING_CONSTANT(target, #nid, OBJ_nid2oid(nid, &buf)); \
817+
} while (false)
818+
819+
NID2OID(EVP_PKEY_RSA);
820+
NID2OID(EVP_PKEY_RSA_PSS);
821+
NID2OID(EVP_PKEY_DSA);
822+
NID2OID(EVP_PKEY_DH);
823+
NID2OID(EVP_PKEY_EC);
824+
// Note for backporters: following are new in openssl 1.1.1.
825+
NID2OID(EVP_PKEY_ED25519);
826+
NID2OID(EVP_PKEY_ED448);
827+
NID2OID(EVP_PKEY_X25519);
828+
NID2OID(EVP_PKEY_X448);
829+
830+
#undef NID2OID
831+
803832
#ifdef SSL_OP_ALL
804833
NODE_DEFINE_CONSTANT(target, SSL_OP_ALL);
805834
#endif

test/parallel/test-crypto-oids.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const {
9+
constants,
10+
} = require('crypto');
11+
12+
assert.strictEqual(constants.EVP_PKEY_RSA, '1.2.840.113549.1.1.1');
13+
assert.strictEqual(constants.EVP_PKEY_RSA_PSS, '1.2.840.113549.1.1.10');
14+
assert.strictEqual(constants.EVP_PKEY_DSA, '1.2.840.10040.4.1');
15+
assert.strictEqual(constants.EVP_PKEY_DH, '1.2.840.113549.1.3.1');
16+
assert.strictEqual(constants.EVP_PKEY_EC, '1.2.840.10045.2.1');
17+
assert.strictEqual(constants.EVP_PKEY_ED25519, '1.3.101.112');
18+
assert.strictEqual(constants.EVP_PKEY_ED448, '1.3.101.113');
19+
assert.strictEqual(constants.EVP_PKEY_X25519, '1.3.101.110');
20+
assert.strictEqual(constants.EVP_PKEY_X448, '1.3.101.111');

0 commit comments

Comments
 (0)