Skip to content

Commit c4f069b

Browse files
committed
crypto: add X.509 generation with CSR support
Expose callback-based generateX509Certificate() and synchronous generateX509CertificateSync(), producing X509Certificate objects for self-signed and issuer-signed X.509 v3 certificates. Accept grouped distinguished names or exact subject DER, public keys, serial numbers, validity dates, and an explicit signing digest. Reuse crypto.sign() key inputs and RSA options, including OpenSSL STORE-backed keys. A PKCS#10 request can supply the subject and public key instead. Require a synchronous selectExtensions callback to accept, replace, or amend the requested extensions. Verify the request signature before signing the certificate, in the selected sync or async mode. Provide typed Basic Constraints, Key Usage, Extended Key Usage, SAN, SKI, and AKI, plus arbitrary raw extensions. Add none implicitly and leave raw extension semantics to the caller. Copy mutable inputs before queueing asynchronous work. Certificate trust and CA authorization remain the caller's responsibility. Fixes: nodejs#51169 Assisted-by: Codex Signed-off-by: Filip Skokan <[email protected]>
1 parent 7721ef9 commit c4f069b

18 files changed

Lines changed: 3657 additions & 23 deletions

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,16 +1707,13 @@ std::optional<X509NameInspection> InspectName(const X509_NAME* name) {
17071707
return output;
17081708
}
17091709

1710+
template <typename GetExtension>
17101711
std::optional<std::vector<X509RawExtension>> InspectExtensions(
1711-
const X509* certificate) {
1712-
if (certificate == nullptr) return std::nullopt;
1713-
const int count = X509_get_ext_count(certificate);
1714-
if (count < 0) return std::nullopt;
1715-
1712+
OPENSSL_SIZE_T count, GetExtension get_extension) {
17161713
std::vector<X509RawExtension> output;
17171714
output.reserve(static_cast<size_t>(count));
1718-
for (int i = 0; i < count; i++) {
1719-
const X509_EXTENSION* extension = X509_get_ext(certificate, i);
1715+
for (OPENSSL_SIZE_T i = 0; i < count; i++) {
1716+
const X509_EXTENSION* extension = get_extension(i);
17201717
if (extension == nullptr) return std::nullopt;
17211718

17221719
auto* mutable_extension = const_cast<X509_EXTENSION*>(extension);
@@ -1735,6 +1732,15 @@ std::optional<std::vector<X509RawExtension>> InspectExtensions(
17351732
}
17361733
} // namespace
17371734

1735+
std::optional<std::vector<X509RawExtension>> inspectX509Extensions(
1736+
const STACK_OF(X509_EXTENSION) * extensions) {
1737+
if (extensions == nullptr) return std::nullopt;
1738+
return InspectExtensions(sk_X509_EXTENSION_num(extensions),
1739+
[extensions](OPENSSL_SIZE_T index) {
1740+
return sk_X509_EXTENSION_value(extensions, index);
1741+
});
1742+
}
1743+
17381744
std::optional<int64_t> X509View::getVersion() const {
17391745
MarkPopErrorOnReturn mark_pop_error_on_return;
17401746
if (cert_ == nullptr) return std::nullopt;
@@ -1758,7 +1764,13 @@ std::optional<X509NameInspection> X509View::inspectIssuerName() const {
17581764
std::optional<std::vector<X509RawExtension>> X509View::inspectExtensions()
17591765
const {
17601766
MarkPopErrorOnReturn mark_pop_error_on_return;
1761-
return InspectExtensions(cert_);
1767+
if (cert_ == nullptr) return std::nullopt;
1768+
const int count = X509_get_ext_count(cert_);
1769+
if (count < 0) return std::nullopt;
1770+
return InspectExtensions(
1771+
static_cast<OPENSSL_SIZE_T>(count), [this](OPENSSL_SIZE_T index) {
1772+
return X509_get_ext(cert_, static_cast<int>(index));
1773+
});
17621774
}
17631775

17641776
bool X509View::isIssuedBy(const X509View& issuer) const {
@@ -1768,10 +1780,39 @@ bool X509View::isIssuedBy(const X509View& issuer) const {
17681780
const_cast<X509*>(cert_)) == X509_V_OK;
17691781
}
17701782

1771-
bool X509View::checkPrivateKey(const EVPKeyPointer& pkey) const {
1772-
ClearErrorOnReturn clearErrorOnReturn;
1773-
if (cert_ == nullptr || pkey == nullptr) return false;
1774-
return X509_check_private_key(const_cast<X509*>(cert_), pkey.get()) == 1;
1783+
X509View::CheckKeyResult X509View::checkPrivateKey(
1784+
const EVPKeyPointer& pkey) const {
1785+
MarkPopErrorOnReturn mark_pop_error_on_return;
1786+
if (cert_ == nullptr || pkey == nullptr) {
1787+
return CheckKeyResult::OPERATION_FAILED;
1788+
}
1789+
1790+
EVPKeyPointer certificate_key(X509_get_pubkey(const_cast<X509*>(cert_)));
1791+
if (!certificate_key) return CheckKeyResult::OPERATION_FAILED;
1792+
#if NCRYPTO_USE_BORINGSSL
1793+
if (EVP_PKEY_is_opaque(certificate_key.get()) ||
1794+
EVP_PKEY_is_opaque(pkey.get())) {
1795+
return CheckKeyResult::UNSUPPORTED_OPERATION;
1796+
}
1797+
#endif
1798+
#if NCRYPTO_USE_OPENSSL_PROVIDER
1799+
const int result = EVP_PKEY_eq(certificate_key.get(), pkey.get());
1800+
#elif NCRYPTO_USE_BORINGSSL
1801+
const int result = EVP_PKEY_cmp(certificate_key.get(), pkey.get());
1802+
#else
1803+
#error "Unsupported crypto backend"
1804+
#endif
1805+
switch (result) {
1806+
case 1:
1807+
return CheckKeyResult::MATCH;
1808+
case 0:
1809+
case -1:
1810+
return CheckKeyResult::NO_MATCH;
1811+
case -2:
1812+
return CheckKeyResult::UNSUPPORTED_OPERATION;
1813+
default:
1814+
return CheckKeyResult::OPERATION_FAILED;
1815+
}
17751816
}
17761817

17771818
bool X509View::checkPublicKey(const EVPKeyPointer& pkey) const {

‎deps/ncrypto/ncrypto.h‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@
6868
#define NCRYPTO_HAS_ASN1_BIT_STRING_GET_LENGTH 0
6969
#endif
7070

71+
// BoringSSL's X.509 signer can encode only its restricted RSA-PSS parameter
72+
// profile. Other supported backends accept the full runtime signing profile.
73+
#if NCRYPTO_USE_BORINGSSL
74+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 0
75+
#else
76+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 1
77+
#endif
78+
7179
#if NCRYPTO_USE_OPENSSL_PROVIDER
7280
#include <openssl/core_names.h>
7381
#include <openssl/encoder.h>
@@ -1641,8 +1649,19 @@ struct X509RawExtension final {
16411649
std::vector<unsigned char> value;
16421650
};
16431651

1652+
std::optional<std::vector<X509RawExtension>> inspectX509Extensions(
1653+
const STACK_OF(X509_EXTENSION) * extensions);
1654+
16441655
class X509View final {
16451656
public:
1657+
enum class CheckKeyResult {
1658+
MATCH, // The requested comparison passed.
1659+
NO_MATCH, // It completed and produced an ordinary mismatch.
1660+
INVALID_CERTIFICATE, // Reserved for certificate verification.
1661+
UNSUPPORTED_OPERATION, // The backend cannot compare these keys.
1662+
OPERATION_FAILED, // The backend failed for another reason.
1663+
};
1664+
16461665
static X509View From(const SSLPointer& ssl);
16471666
static X509View From(const SSLCtxPointer& ctx);
16481667

@@ -1685,7 +1704,7 @@ class X509View final {
16851704

16861705
bool isCA() const;
16871706
bool isIssuedBy(const X509View& other) const;
1688-
bool checkPrivateKey(const EVPKeyPointer& pkey) const;
1707+
CheckKeyResult checkPrivateKey(const EVPKeyPointer& pkey) const;
16891708
bool checkPublicKey(const EVPKeyPointer& pkey) const;
16901709

16911710
std::optional<std::string> getFingerprint(const Digest& method) const;

0 commit comments

Comments
 (0)