diff --git a/deps/ncrypto/ncrypto.cc b/deps/ncrypto/ncrypto.cc index 6d7bee31c5..e1c2da6969 100644 --- a/deps/ncrypto/ncrypto.cc +++ b/deps/ncrypto/ncrypto.cc @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #if OPENSSL_VERSION_MAJOR >= 3 #include #include @@ -1094,6 +1096,29 @@ BIOPointer X509View::getValidTo() const { return bio; } +std::optional X509View::getSignatureAlgorithm() const { + if (cert_ == nullptr) return std::nullopt; + int nid = X509_get_signature_nid(cert_); + if (nid == NID_undef) return std::nullopt; + const char* ln = OBJ_nid2ln(nid); + if (ln == nullptr) return std::nullopt; + return std::string_view(ln); +} + +std::optional X509View::getSignatureAlgorithmOID() const { + if (cert_ == nullptr) return std::nullopt; + const X509_ALGOR* alg = nullptr; + X509_get0_signature(nullptr, &alg, cert_); + if (alg == nullptr) return std::nullopt; + const ASN1_OBJECT* obj = nullptr; + X509_ALGOR_get0(&obj, nullptr, nullptr, alg); + if (obj == nullptr) return std::nullopt; + std::array buf{}; + int len = OBJ_obj2txt(buf.data(), buf.size(), obj, 1); + if (len < 0 || static_cast(len) >= buf.size()) return std::nullopt; + return std::string(buf.data(), static_cast(len)); +} + int64_t X509View::getValidToTime() const { #ifdef OPENSSL_IS_BORINGSSL // Boringssl does not implement ASN1_TIME_to_tm in a public way, diff --git a/deps/ncrypto/ncrypto.h b/deps/ncrypto/ncrypto.h index bee96ba783..175ec8ba0f 100644 --- a/deps/ncrypto/ncrypto.h +++ b/deps/ncrypto/ncrypto.h @@ -1191,6 +1191,8 @@ class X509View final { BIOPointer getInfoAccess() const; BIOPointer getValidFrom() const; BIOPointer getValidTo() const; + std::optional getSignatureAlgorithm() const; + std::optional getSignatureAlgorithmOID() const; int64_t getValidFromTime() const; int64_t getValidToTime() const; DataPointer getSerialNumber() const; diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 32e28e652c..d5041cf26a 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2971,6 +2971,26 @@ added: The date/time until which this certificate is valid, encapsulated in a `Date` object. +### `x509.signatureAlgorithm` + + + +* Type: {string|undefined} + +The algorithm used to sign the certificate or `undefined` if the signature algorithm is unknown by OpenSSL. + +### `x509.signatureAlgorithmOid` + + + +* Type: {string} + +The OID of the algorithm used to sign the certificate. + ### `x509.verify(publicKey)`