22#include <openssl/ssl.h>
23#include <openssl/bio.h>
24#include <openssl/x509v3.h>
25#include <openssl/x509.h>
32using BIGNUM_ptr = std::unique_ptr<::BIGNUM,
decltype(&::BN_free)>;
63 X509_NAME_oneline(X509_get_subject_name(cert),
nullptr, 0),
65 { OPENSSL_free(p); });
67 return std::string(subject.get());
68 return std::string(
"");
74 if (subject_bio ==
nullptr)
76 return std::string(
"");
79 X509_NAME_print_ex(subject_bio.get(),
80 X509_get_subject_name(cert),
84 | ASN1_STRFLGS_UTF8_CONVERT
85 | ASN1_STRFLGS_ESC_CTRL);
86 if (BIO_eof(subject_bio.get()))
88 return std::string(
"");
91 BUF_MEM *subject_mem =
nullptr;
92 BIO_get_mem_ptr(subject_bio.get(), &subject_mem);
93 return std::string(subject_mem->data,
94 subject_mem->data + subject_mem->length);
100 BIO *bio = BIO_new(BIO_s_mem());
102 PEM_write_bio_X509(bio, cert);
103 size_t len = BIO_get_mem_data(bio, &data);
104 std::string certpem{data, len};
116 int nid = X509_get_signature_nid(cert);
117 const char *sig = OBJ_nid2sn(nid);
123 return "(error getting signature algorithm)";
140 static const char nullc =
'\0';
143 auto *x509_name = X509_get_subject_name(cert);
144 int i = X509_NAME_get_index_by_NID(x509_name, nid, -1);
147 const X509_NAME_ENTRY *ent = X509_NAME_get_entry(x509_name, i);
150 const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
152 buf = (
unsigned char *)1;
154 const int len = ASN1_STRING_to_UTF8(&buf, val);
157 if (std::strlen((
char *)buf) ==
static_cast<unsigned int>(len))
165 i = X509_get_ext_by_NID(cert, nid, -1);
170 auto *
ext = X509_get_ext(cert, i);
173 BIO *bio = BIO_new(BIO_s_mem());
176 if (X509V3_EXT_print(bio,
ext, 0, 0))
178 if (BIO_write(bio, &nullc, 1) == 1)
181 const long len = BIO_get_mem_data(bio, &str);
182 if (std::strlen(str) ==
static_cast<size_t>(len))
207 const ASN1_INTEGER *asn1_i = X509_get_serialNumber(cert);
214 char *openssl_serial = BN_bn2dec(bignum.get());
221 const std::string ret = openssl_serial;
222 OPENSSL_free(openssl_serial);
239 const ASN1_INTEGER *asn1_i = X509_get0_serialNumber(cert);
245 int numbytesoutput = BN_num_bytes(serial.get());
247 std::unique_ptr<unsigned char[]> buf(
new unsigned char[numbytesoutput]);
249 BN_bn2binpad(serial.get(), buf.get(), numbytesoutput);
261 return EVP_MD_size(EVP_sha256());
266 std::vector<uint8_t> fingerprint;
269 if (::X509_digest(cert, EVP_sha256(), fingerprint.data(), NULL) != 1)
static std::size_t x509_fingerprint_size()
static std::string x509_get_serial_hex(::X509 *cert)
static std::string x509_get_field(::X509 *cert, const int nid)
std::unique_ptr<::BIGNUM, decltype(&::BN_free)> BIGNUM_ptr
static std::string X509_get_pem_encoding(::X509 *cert)
static std::string x509_get_serial(::X509 *cert)
static std::vector< uint8_t > x509_get_fingerprint(const ::X509 *cert)
static std::string x509_get_subject(::X509 *cert, bool new_format=false)
static std::string x509_get_signature_algorithm(const ::X509 *cert)
std::unique_ptr< T, std::function< void(T *)> > unique_ptr_del
std::string render_hex_sep(const unsigned char *data, size_t size, const char sep, const bool caps=false)
void ext(const std::string &path)