Skip to content

Commit 1d847d1

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 fa5f6da commit 1d847d1

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
@@ -1718,16 +1718,13 @@ std::optional<X509NameInspection> InspectName(const X509_NAME* name) {
17181718
return output;
17191719
}
17201720

1721+
template <typename GetExtension>
17211722
std::optional<std::vector<X509RawExtension>> InspectExtensions(
1722-
const X509* certificate) {
1723-
if (certificate == nullptr) return std::nullopt;
1724-
const int count = X509_get_ext_count(certificate);
1725-
if (count < 0) return std::nullopt;
1726-
1723+
OPENSSL_SIZE_T count, GetExtension get_extension) {
17271724
std::vector<X509RawExtension> output;
17281725
output.reserve(static_cast<size_t>(count));
1729-
for (int i = 0; i < count; i++) {
1730-
const X509_EXTENSION* extension = X509_get_ext(certificate, i);
1726+
for (OPENSSL_SIZE_T i = 0; i < count; i++) {
1727+
const X509_EXTENSION* extension = get_extension(i);
17311728
if (extension == nullptr) return std::nullopt;
17321729

17331730
auto* mutable_extension = const_cast<X509_EXTENSION*>(extension);
@@ -1746,6 +1743,15 @@ std::optional<std::vector<X509RawExtension>> InspectExtensions(
17461743
}
17471744
} // namespace
17481745

1746+
std::optional<std::vector<X509RawExtension>> inspectX509Extensions(
1747+
const STACK_OF(X509_EXTENSION) * extensions) {
1748+
if (extensions == nullptr) return std::nullopt;
1749+
return InspectExtensions(sk_X509_EXTENSION_num(extensions),
1750+
[extensions](OPENSSL_SIZE_T index) {
1751+
return sk_X509_EXTENSION_value(extensions, index);
1752+
});
1753+
}
1754+
17491755
std::optional<int64_t> X509View::getVersion() const {
17501756
MarkPopErrorOnReturn mark_pop_error_on_return;
17511757
if (cert_ == nullptr) return std::nullopt;
@@ -1769,7 +1775,13 @@ std::optional<X509NameInspection> X509View::inspectIssuerName() const {
17691775
std::optional<std::vector<X509RawExtension>> X509View::inspectExtensions()
17701776
const {
17711777
MarkPopErrorOnReturn mark_pop_error_on_return;
1772-
return InspectExtensions(cert_);
1778+
if (cert_ == nullptr) return std::nullopt;
1779+
const int count = X509_get_ext_count(cert_);
1780+
if (count < 0) return std::nullopt;
1781+
return InspectExtensions(
1782+
static_cast<OPENSSL_SIZE_T>(count), [this](OPENSSL_SIZE_T index) {
1783+
return X509_get_ext(cert_, static_cast<int>(index));
1784+
});
17731785
}
17741786

17751787
bool X509View::isIssuedBy(const X509View& issuer) const {
@@ -1779,10 +1791,39 @@ bool X509View::isIssuedBy(const X509View& issuer) const {
17791791
const_cast<X509*>(cert_)) == X509_V_OK;
17801792
}
17811793

1782-
bool X509View::checkPrivateKey(const EVPKeyPointer& pkey) const {
1783-
ClearErrorOnReturn clearErrorOnReturn;
1784-
if (cert_ == nullptr || pkey == nullptr) return false;
1785-
return X509_check_private_key(const_cast<X509*>(cert_), pkey.get()) == 1;
1794+
X509View::CheckKeyResult X509View::checkPrivateKey(
1795+
const EVPKeyPointer& pkey) const {
1796+
MarkPopErrorOnReturn mark_pop_error_on_return;
1797+
if (cert_ == nullptr || pkey == nullptr) {
1798+
return CheckKeyResult::OPERATION_FAILED;
1799+
}
1800+
1801+
EVPKeyPointer certificate_key(X509_get_pubkey(const_cast<X509*>(cert_)));
1802+
if (!certificate_key) return CheckKeyResult::OPERATION_FAILED;
1803+
#if NCRYPTO_USE_BORINGSSL
1804+
if (EVP_PKEY_is_opaque(certificate_key.get()) ||
1805+
EVP_PKEY_is_opaque(pkey.get())) {
1806+
return CheckKeyResult::UNSUPPORTED_OPERATION;
1807+
}
1808+
#endif
1809+
#if NCRYPTO_USE_OPENSSL3_PROVIDER
1810+
const int result = EVP_PKEY_eq(certificate_key.get(), pkey.get());
1811+
#elif NCRYPTO_USE_BORINGSSL || NCRYPTO_USE_LEGACY_OPENSSL
1812+
const int result = EVP_PKEY_cmp(certificate_key.get(), pkey.get());
1813+
#else
1814+
#error "Unsupported crypto backend"
1815+
#endif
1816+
switch (result) {
1817+
case 1:
1818+
return CheckKeyResult::MATCH;
1819+
case 0:
1820+
case -1:
1821+
return CheckKeyResult::NO_MATCH;
1822+
case -2:
1823+
return CheckKeyResult::UNSUPPORTED_OPERATION;
1824+
default:
1825+
return CheckKeyResult::OPERATION_FAILED;
1826+
}
17861827
}
17871828

17881829
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
@@ -82,6 +82,14 @@
8282
#define NCRYPTO_HAS_ASN1_BIT_STRING_GET_LENGTH 0
8383
#endif
8484

85+
// BoringSSL's X.509 signer can encode only its restricted RSA-PSS parameter
86+
// profile. Other supported backends accept the full runtime signing profile.
87+
#if NCRYPTO_USE_BORINGSSL
88+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 0
89+
#else
90+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 1
91+
#endif
92+
8593
#if NCRYPTO_USE_OPENSSL3_PROVIDER
8694
#include <openssl/core_names.h>
8795
#include <openssl/encoder.h>
@@ -1665,8 +1673,19 @@ struct X509RawExtension final {
16651673
std::vector<unsigned char> value;
16661674
};
16671675

1676+
std::optional<std::vector<X509RawExtension>> inspectX509Extensions(
1677+
const STACK_OF(X509_EXTENSION) * extensions);
1678+
16681679
class X509View final {
16691680
public:
1681+
enum class CheckKeyResult {
1682+
MATCH, // The requested comparison passed.
1683+
NO_MATCH, // It completed and produced an ordinary mismatch.
1684+
INVALID_CERTIFICATE, // Reserved for certificate verification.
1685+
UNSUPPORTED_OPERATION, // The backend cannot compare these keys.
1686+
OPERATION_FAILED, // The backend failed for another reason.
1687+
};
1688+
16701689
static X509View From(const SSLPointer& ssl);
16711690
static X509View From(const SSLCtxPointer& ctx);
16721691

@@ -1709,7 +1728,7 @@ class X509View final {
17091728

17101729
bool isCA() const;
17111730
bool isIssuedBy(const X509View& other) const;
1712-
bool checkPrivateKey(const EVPKeyPointer& pkey) const;
1731+
CheckKeyResult checkPrivateKey(const EVPKeyPointer& pkey) const;
17131732
bool checkPublicKey(const EVPKeyPointer& pkey) const;
17141733

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

0 commit comments

Comments
 (0)