Skip to content

Commit d64c93c

Browse files
committed
tls: Add subjectPublicKeyInfo to certificate info
Added `subjectPublicKeyInfo` attribute to the peer certificate object. The attribute is a buffer containing the DER-encoded value of the SubjectPublicKeyInfo structure as described in RFC 5280. This will allow applications to retrieve the SPKI information necessary for validating HTTP public key pins according to RFC 7469. https://tools.ietf.org/html/rfc7469
1 parent 7d90c90 commit d64c93c

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/api/tls.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ Example:
725725
valid_from: 'Nov 11 09:52:22 2009 GMT',
726726
valid_to: 'Nov 6 09:52:22 2029 GMT',
727727
fingerprint: '2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:5A:71:38:52:EC:8A:DF',
728-
serialNumber: 'B9B0D332A1AA5635' }
728+
serialNumber: 'B9B0D332A1AA5635',
729+
subjectPublicKeyInfo: < SPKI DER buffer > }
729730

730731
If the peer does not provide a certificate, it returns `null` or an empty
731732
object.

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ namespace node {
193193
V(status_string, "status") \
194194
V(stdio_string, "stdio") \
195195
V(subject_string, "subject") \
196+
V(subject_public_key_info_string, "subjectPublicKeyInfo") \
196197
V(subjectaltname_string, "subjectaltname") \
197198
V(sys_string, "sys") \
198199
V(syscall_string, "syscall") \

src/node_crypto.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,17 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
12171217

12181218
EVP_PKEY* pkey = X509_get_pubkey(cert);
12191219
RSA* rsa = nullptr;
1220-
if (pkey != nullptr)
1220+
if (pkey != nullptr) {
12211221
rsa = EVP_PKEY_get1_RSA(pkey);
12221222

1223+
int pkey_size = i2d_PUBKEY(pkey, nullptr);
1224+
Local<Object> pkey_buff = Buffer::New(env, pkey_size);
1225+
unsigned char* pkey_serialized = reinterpret_cast<unsigned char*>(
1226+
Buffer::Data(pkey_buff));
1227+
i2d_PUBKEY(pkey, &pkey_serialized);
1228+
info->Set(env->subject_public_key_info_string(), pkey_buff);
1229+
}
1230+
12231231
if (rsa != nullptr) {
12241232
BN_print(bio, rsa->n);
12251233
BIO_get_mem_ptr(bio, &mem);

test/parallel/test-tls-peer-certificate.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ var options = {
1919
};
2020
var verified = false;
2121

22+
var expectedBase64SubjectPublicKeyInfo = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB' +
23+
'iQKBgQC46zeFbysX7vHHmIH3COYiB34dOpEVR4rEb6ZZXfkeXoDe7NgZfBbOeqw6iavhr' +
24+
'9SRmvFs8ankDCpr2DvY0X3uDdLKyrYNbhrfJxdYB5hhwdKVHGokZdOPH68b/ScMJcsGGg' +
25+
'Mo7TTMRxx2MZLzESOOJ5BCv4p4BKYibSRCa43lhwIDAQAB';
26+
2227
var server = tls.createServer(options, function(cleartext) {
2328
cleartext.end('World');
2429
});
@@ -37,6 +42,8 @@ server.listen(common.PORT, function() {
3742
common.debug(util.inspect(peerCert));
3843
assert.equal(peerCert.subject.emailAddress, '[email protected]');
3944
assert.equal(peerCert.serialNumber, '9A84ABCFB8A72AC0');
45+
assert.equal(peerCert.subjectPublicKeyInfo.toString('base64'),
46+
expectedBase64SubjectPublicKeyInfo);
4047
assert.deepEqual(peerCert.infoAccess['OCSP - URI'],
4148
[ 'http://ocsp.nodejs.org/' ]);
4249

0 commit comments

Comments
 (0)