Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions net/security-context/test/test_cert_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#include <openssl/rsa.h>
#include <openssl/opensslv.h>

// X509_get_subject_name()'s return qualifier follows the OpenSSL headers in use --
// 1.1.1 hands back X509_NAME*, newer ones const X509_NAME* -- while the setters below
// take a mutable one. One const_cast covers both: on the old signature it removes no
// qualifier and is a no-op, so this needs no version macro to stay compatible with
// either. The certificate is ours and writable, which is what makes the cast sound.
inline X509_NAME* test_subject_name(X509* cert) {
return const_cast<X509_NAME*>(X509_get_subject_name(cert));
}

// Generate a self-signed cert at runtime, different from the hardcoded test cert.
// Used in negative tests where a valid-but-wrong CA is needed.
inline std::string generate_different_self_signed_cert() {
Expand All @@ -31,7 +40,7 @@ inline std::string generate_different_self_signed_cert() {
X509_gmtime_adj(X509_get_notAfter(x509), 365 * 24 * 3600);
X509_set_pubkey(x509, pkey);

auto name = X509_get_subject_name(x509);
X509_NAME* name = test_subject_name(x509);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
(unsigned char*)"WrongCA", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
Expand Down Expand Up @@ -95,7 +104,7 @@ inline std::string test_pem_of_key(EVP_PKEY* pkey) {
}

inline void test_set_cert_name(X509* cert, const char* cn) {
auto name = X509_get_subject_name(cert);
X509_NAME* name = test_subject_name(cert);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
(unsigned char*)"PhotonTest", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
Expand Down Expand Up @@ -123,7 +132,7 @@ inline TestCertChain generate_ca_signed_cert(const std::vector<std::string>& san
X509_gmtime_adj(X509_get_notAfter(ca), 365 * 24 * 3600);
X509_set_pubkey(ca, ca_key);
test_set_cert_name(ca, ca_cn);
X509_set_issuer_name(ca, X509_get_subject_name(ca));
X509_set_issuer_name(ca, test_subject_name(ca));
{
// CA:TRUE is mandatory: without basicConstraints the chain is rejected
// before any name check happens, which would mask what these tests mean
Expand All @@ -146,7 +155,7 @@ inline TestCertChain generate_ca_signed_cert(const std::vector<std::string>& san
X509_gmtime_adj(X509_get_notAfter(leaf), 365 * 24 * 3600);
X509_set_pubkey(leaf, leaf_key);
test_set_cert_name(leaf, cn);
X509_set_issuer_name(leaf, X509_get_subject_name(ca));
X509_set_issuer_name(leaf, test_subject_name(ca));
{
X509V3_CTX v3ctx;
X509V3_set_ctx_nodb(&v3ctx);
Expand Down
Loading