Skip to content

Commit 92f61d0

Browse files
panvaaduh95
authored andcommitted
crypto: add X509Certificate inspection getters
Expose version, subjectName, issuerName, extensions, tbs, and signature without requiring callers to parse display strings or certificate DER. Names include complete DER and grouped, ordered RDN attributes. Extensions retain their order, duplicate and unknown OIDs, criticality, and raw values. Expose the exact signed TBSCertificate bytes and the signature octets, algorithm OID, and DER parameters. Return fresh objects, arrays, and Buffers from the new getters, leaving existing getter caching unchanged. Report inspection failures as ERR_CRYPTO_INVALID_X509_CERTIFICATE. Fixes: #48730 Assisted-by: Codex Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #67082 Backport-PR-URL: #67442 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent e76a35c commit 92f61d0

13 files changed

Lines changed: 1208 additions & 0 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
5656
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
5757
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>;
5858

59+
struct ASN1SequenceDeleter final {
60+
void operator()(ASN1_SEQUENCE_ANY* sequence) const {
61+
sk_ASN1_TYPE_pop_free(sequence, ASN1_TYPE_free);
62+
}
63+
};
64+
65+
using ASN1SequencePointer =
66+
std::unique_ptr<ASN1_SEQUENCE_ANY, ASN1SequenceDeleter>;
67+
5968
#if NCRYPTO_USE_OPENSSL3_PROVIDER
6069
using X509PubKeyPointer = DeleteFnPtr<X509_PUBKEY, X509_PUBKEY_free>;
6170
// OSSL_STORE_close() returns int, so it needs a void-returning adapter to be
@@ -1618,6 +1627,165 @@ bool X509View::isCA() const {
16181627
return X509_check_ca(const_cast<X509*>(cert_)) == 1;
16191628
}
16201629

1630+
namespace {
1631+
bool CopyStringBytes(const ASN1_STRING* value,
1632+
std::vector<unsigned char>* output) {
1633+
if (value == nullptr) return false;
1634+
const int length = ASN1_STRING_length(value);
1635+
const unsigned char* data = ASN1_STRING_get0_data(value);
1636+
if (length < 0 || (length > 0 && data == nullptr)) return false;
1637+
if (length == 0) {
1638+
output->clear();
1639+
} else {
1640+
output->assign(data, data + static_cast<size_t>(length));
1641+
}
1642+
return true;
1643+
}
1644+
1645+
bool CanConvertNameValueToUtf8(const ASN1_STRING* value) {
1646+
if (value == nullptr) return false;
1647+
switch (ASN1_STRING_type(value)) {
1648+
case V_ASN1_UTF8STRING:
1649+
case V_ASN1_NUMERICSTRING:
1650+
case V_ASN1_PRINTABLESTRING:
1651+
case V_ASN1_T61STRING:
1652+
case V_ASN1_IA5STRING:
1653+
case V_ASN1_UTCTIME:
1654+
case V_ASN1_GENERALIZEDTIME:
1655+
case V_ASN1_ISO64STRING:
1656+
case V_ASN1_UNIVERSALSTRING:
1657+
case V_ASN1_BMPSTRING:
1658+
return true;
1659+
default:
1660+
return false;
1661+
}
1662+
}
1663+
1664+
std::optional<size_t> GetEncodedRdnCount(const X509_NAME* name) {
1665+
const unsigned char* der = nullptr;
1666+
size_t der_length = 0;
1667+
if (X509_NAME_get0_der(const_cast<X509_NAME*>(name), &der, &der_length) !=
1668+
1 ||
1669+
der == nullptr || der_length > static_cast<size_t>(LONG_MAX)) {
1670+
return std::nullopt;
1671+
}
1672+
1673+
const unsigned char* cursor = der;
1674+
ASN1SequencePointer rdns(d2i_ASN1_SEQUENCE_ANY(
1675+
nullptr, &cursor, static_cast<long>(der_length))); // NOLINT(runtime/int)
1676+
if (!rdns || cursor != der + der_length) return std::nullopt;
1677+
1678+
const OPENSSL_SIZE_T count = sk_ASN1_TYPE_num(rdns.get());
1679+
for (OPENSSL_SIZE_T i = 0; i < count; i++) {
1680+
const ASN1_TYPE* rdn = sk_ASN1_TYPE_value(rdns.get(), i);
1681+
if (rdn == nullptr || ASN1_TYPE_get(rdn) != V_ASN1_SET) {
1682+
return std::nullopt;
1683+
}
1684+
}
1685+
return static_cast<size_t>(count);
1686+
}
1687+
1688+
std::optional<X509NameInspection> InspectName(const X509_NAME* name) {
1689+
if (name == nullptr) return std::nullopt;
1690+
const int count = X509_NAME_entry_count(name);
1691+
if (count < 0) return std::nullopt;
1692+
const auto encoded_rdn_count = GetEncodedRdnCount(name);
1693+
if (!encoded_rdn_count.has_value()) return std::nullopt;
1694+
1695+
X509NameInspection output;
1696+
output.reserve(static_cast<size_t>(count));
1697+
int previous_set = -1;
1698+
for (int i = 0; i < count; i++) {
1699+
const X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, i);
1700+
if (entry == nullptr) return std::nullopt;
1701+
1702+
const ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(entry);
1703+
const ASN1_STRING* value = X509_NAME_ENTRY_get_data(entry);
1704+
auto oid = ObjectToText(object, true);
1705+
const int set = X509_NAME_ENTRY_set(entry);
1706+
if (!oid.has_value() || value == nullptr || set < 0 ||
1707+
(set != previous_set &&
1708+
(previous_set == INT_MAX || set != previous_set + 1))) {
1709+
return std::nullopt;
1710+
}
1711+
previous_set = set;
1712+
1713+
X509NameAttribute attribute{
1714+
.set = set,
1715+
.oid = std::move(oid.value()),
1716+
};
1717+
if (CanConvertNameValueToUtf8(value)) {
1718+
unsigned char* data = nullptr;
1719+
const int length = ASN1_STRING_to_UTF8(&data, value);
1720+
DataPointer owned(data, length > 0 ? static_cast<size_t>(length) : 0);
1721+
if (length < 0 || (length > 0 && !owned)) return std::nullopt;
1722+
attribute.value =
1723+
length == 0 ? std::string()
1724+
: std::string(reinterpret_cast<const char*>(owned.get()),
1725+
static_cast<size_t>(length));
1726+
}
1727+
output.push_back(std::move(attribute));
1728+
}
1729+
const size_t inspected_rdn_count =
1730+
previous_set < 0 ? 0 : static_cast<size_t>(previous_set) + 1;
1731+
if (inspected_rdn_count != encoded_rdn_count.value()) return std::nullopt;
1732+
return output;
1733+
}
1734+
1735+
std::optional<std::vector<X509RawExtension>> InspectExtensions(
1736+
const X509* certificate) {
1737+
if (certificate == nullptr) return std::nullopt;
1738+
const int count = X509_get_ext_count(certificate);
1739+
if (count < 0) return std::nullopt;
1740+
1741+
std::vector<X509RawExtension> output;
1742+
output.reserve(static_cast<size_t>(count));
1743+
for (int i = 0; i < count; i++) {
1744+
const X509_EXTENSION* extension = X509_get_ext(certificate, i);
1745+
if (extension == nullptr) return std::nullopt;
1746+
1747+
auto* mutable_extension = const_cast<X509_EXTENSION*>(extension);
1748+
auto oid = ObjectToText(X509_EXTENSION_get_object(mutable_extension), true);
1749+
const ASN1_OCTET_STRING* value = X509_EXTENSION_get_data(mutable_extension);
1750+
if (!oid.has_value() || value == nullptr) return std::nullopt;
1751+
1752+
X509RawExtension inspected{
1753+
.oid = std::move(oid.value()),
1754+
.critical = X509_EXTENSION_get_critical(mutable_extension) != 0,
1755+
};
1756+
if (!CopyStringBytes(value, &inspected.value)) return std::nullopt;
1757+
output.push_back(std::move(inspected));
1758+
}
1759+
return output;
1760+
}
1761+
} // namespace
1762+
1763+
std::optional<int64_t> X509View::getVersion() const {
1764+
MarkPopErrorOnReturn mark_pop_error_on_return;
1765+
if (cert_ == nullptr) return std::nullopt;
1766+
const long version = X509_get_version(cert_); // NOLINT(runtime/int)
1767+
if (version < 0 || version > 2) return std::nullopt;
1768+
return static_cast<int64_t>(version) + 1;
1769+
}
1770+
1771+
std::optional<X509NameInspection> X509View::inspectSubjectName() const {
1772+
MarkPopErrorOnReturn mark_pop_error_on_return;
1773+
if (cert_ == nullptr) return std::nullopt;
1774+
return InspectName(X509_get_subject_name(cert_));
1775+
}
1776+
1777+
std::optional<X509NameInspection> X509View::inspectIssuerName() const {
1778+
MarkPopErrorOnReturn mark_pop_error_on_return;
1779+
if (cert_ == nullptr) return std::nullopt;
1780+
return InspectName(X509_get_issuer_name(cert_));
1781+
}
1782+
1783+
std::optional<std::vector<X509RawExtension>> X509View::inspectExtensions()
1784+
const {
1785+
MarkPopErrorOnReturn mark_pop_error_on_return;
1786+
return InspectExtensions(cert_);
1787+
}
1788+
16211789
bool X509View::isIssuedBy(const X509View& issuer) const {
16221790
MarkPopErrorOnReturn mark_pop_error_on_return;
16231791
if (cert_ == nullptr || issuer.cert_ == nullptr) return false;

‎deps/ncrypto/ncrypto.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@
7373
#define NCRYPTO_USE_LEGACY_KEY_TYPES 0
7474
#endif
7575

76+
// OpenSSL 4 makes ASN1_STRING opaque and provides a checked BIT STRING length
77+
// accessor. Earlier OpenSSL releases and pinned BoringSSL expose the decoded
78+
// unused-bit count through ASN1_STRING::flags.
79+
#if !NCRYPTO_USE_BORINGSSL && OPENSSL_VERSION_PREREQ(4, 0)
80+
#define NCRYPTO_HAS_ASN1_BIT_STRING_GET_LENGTH 1
81+
#else
82+
#define NCRYPTO_HAS_ASN1_BIT_STRING_GET_LENGTH 0
83+
#endif
84+
7685
#if NCRYPTO_USE_OPENSSL3_PROVIDER
7786
#include <openssl/core_names.h>
7887
#include <openssl/encoder.h>
@@ -1642,6 +1651,20 @@ class X509Name final {
16421651
int total_;
16431652
};
16441653

1654+
struct X509NameAttribute final {
1655+
int set = 0;
1656+
std::string oid;
1657+
std::optional<std::string> value;
1658+
};
1659+
1660+
using X509NameInspection = std::vector<X509NameAttribute>;
1661+
1662+
struct X509RawExtension final {
1663+
std::string oid;
1664+
bool critical = false;
1665+
std::vector<unsigned char> value;
1666+
};
1667+
16451668
class X509View final {
16461669
public:
16471670
static X509View From(const SSLPointer& ssl);
@@ -1679,6 +1702,11 @@ class X509View final {
16791702
Result<EVPKeyPointer, int> getPublicKey() const;
16801703
StackOfASN1 getKeyUsage() const;
16811704

1705+
std::optional<int64_t> getVersion() const;
1706+
std::optional<X509NameInspection> inspectSubjectName() const;
1707+
std::optional<X509NameInspection> inspectIssuerName() const;
1708+
std::optional<std::vector<X509RawExtension>> inspectExtensions() const;
1709+
16821710
bool isCA() const;
16831711
bool isIssuedBy(const X509View& other) const;
16841712
bool checkPrivateKey(const EVPKeyPointer& pkey) const;

0 commit comments

Comments
 (0)