Skip to content

Commit d04a519

Browse files
patrickrbcanonrig
authored andcommitted
crypto: expose signatureAlgorithm on X509Certificate
Adds the `signatureAlgorithm` property to a X509Certificate allowing users to retrieve a string representing the algorithm used to sign the certificate. This string is defined by the OpenSSL library. Fixes: nodejs/node#59103 PR-URL: nodejs/node#59235 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 2b08c2a commit d04a519

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/ncrypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ class X509View final {
12111211
BIOPointer getInfoAccess() const;
12121212
BIOPointer getValidFrom() const;
12131213
BIOPointer getValidTo() const;
1214+
std::optional<std::string_view> getSignatureAlgorithm() const;
1215+
std::optional<std::string> getSignatureAlgorithmOID() const;
12141216
int64_t getValidFromTime() const;
12151217
int64_t getValidToTime() const;
12161218
DataPointer getSerialNumber() const;

src/ncrypto.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#endif
1717

1818
#include <algorithm>
19+
#include <array>
1920
#include <cstring>
21+
#include <string_view>
2022
#if OPENSSL_VERSION_MAJOR >= 3
2123
#include <openssl/core_names.h>
2224
#include <openssl/params.h>
@@ -1155,6 +1157,29 @@ BIOPointer X509View::getValidTo() const {
11551157
return bio;
11561158
}
11571159

1160+
std::optional<std::string_view> X509View::getSignatureAlgorithm() const {
1161+
if (cert_ == nullptr) return std::nullopt;
1162+
int nid = X509_get_signature_nid(cert_);
1163+
if (nid == NID_undef) return std::nullopt;
1164+
const char* ln = OBJ_nid2ln(nid);
1165+
if (ln == nullptr) return std::nullopt;
1166+
return std::string_view(ln);
1167+
}
1168+
1169+
std::optional<std::string> X509View::getSignatureAlgorithmOID() const {
1170+
if (cert_ == nullptr) return std::nullopt;
1171+
const X509_ALGOR* alg = nullptr;
1172+
X509_get0_signature(nullptr, &alg, cert_);
1173+
if (alg == nullptr) return std::nullopt;
1174+
const ASN1_OBJECT* obj = nullptr;
1175+
X509_ALGOR_get0(&obj, nullptr, nullptr, alg);
1176+
if (obj == nullptr) return std::nullopt;
1177+
std::array<char, 128> buf{};
1178+
int len = OBJ_obj2txt(buf.data(), buf.size(), obj, 1);
1179+
if (len < 0 || static_cast<size_t>(len) >= buf.size()) return std::nullopt;
1180+
return std::string(buf.data(), static_cast<size_t>(len));
1181+
}
1182+
11581183
int64_t X509View::getValidToTime() const {
11591184
#ifdef OPENSSL_IS_BORINGSSL
11601185
#ifndef NCRYPTO_NO_ASN1_TIME

0 commit comments

Comments
 (0)