Skip to content

Commit fecf6cc

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 3b209b2 commit fecf6cc

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

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

17341731
auto* mutable_extension = const_cast<X509_EXTENSION*>(extension);
@@ -1747,6 +1744,15 @@ std::optional<std::vector<X509RawExtension>> InspectExtensions(
17471744
}
17481745
} // namespace
17491746

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

17761788
bool X509View::isIssuedBy(const X509View& issuer) const {
@@ -1780,10 +1792,39 @@ bool X509View::isIssuedBy(const X509View& issuer) const {
17801792
const_cast<X509*>(cert_)) == X509_V_OK;
17811793
}
17821794

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

17891830
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
@@ -70,6 +70,14 @@
7070
#define NCRYPTO_HAS_ASN1_BIT_STRING_GET_LENGTH 0
7171
#endif
7272

73+
// BoringSSL's X.509 signer can encode only its restricted RSA-PSS parameter
74+
// profile. Other supported backends accept the full runtime signing profile.
75+
#if NCRYPTO_USE_BORINGSSL
76+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 0
77+
#else
78+
#define NCRYPTO_HAS_X509_RSA_PSS_FLEXIBLE_PARAMETERS 1
79+
#endif
80+
7381
#if NCRYPTO_USE_OPENSSL_PROVIDER
7482
#include <openssl/core_names.h>
7583
#include <openssl/encoder.h>
@@ -1671,8 +1679,19 @@ struct X509RawExtension final {
16711679
std::vector<unsigned char> value;
16721680
};
16731681

1682+
std::optional<std::vector<X509RawExtension>> inspectX509Extensions(
1683+
const STACK_OF(X509_EXTENSION) * extensions);
1684+
16741685
class X509View final {
16751686
public:
1687+
enum class CheckKeyResult {
1688+
MATCH, // The requested comparison passed.
1689+
NO_MATCH, // It completed and produced an ordinary mismatch.
1690+
INVALID_CERTIFICATE, // Reserved for certificate verification.
1691+
UNSUPPORTED_OPERATION, // The backend cannot compare these keys.
1692+
OPERATION_FAILED, // The backend failed for another reason.
1693+
};
1694+
16761695
static X509View From(const SSLPointer& ssl);
16771696
static X509View From(const SSLCtxPointer& ctx);
16781697

@@ -1715,7 +1734,7 @@ class X509View final {
17151734

17161735
bool isCA() const;
17171736
bool isIssuedBy(const X509View& other) const;
1718-
bool checkPrivateKey(const EVPKeyPointer& pkey) const;
1737+
CheckKeyResult checkPrivateKey(const EVPKeyPointer& pkey) const;
17191738
bool checkPublicKey(const EVPKeyPointer& pkey) const;
17201739

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

0 commit comments

Comments
 (0)