#pragma once #include #include #include #include #include #include #define AT __func__ << ":" << __LINE__ << " " namespace { // Helper method to extract public key from the certificate. std::vector extractPubKey(const std::vector& cert_bytes) { const uint8_t* p = cert_bytes.data(); bssl::UniquePtr decoded_cert(d2i_X509(nullptr, &p, cert_bytes.size())); if (!decoded_cert) { LOG(INFO) << AT << "Could not decode the cert, trying decoding as PEM"; bssl::UniquePtr cert_bio(BIO_new_mem_buf(cert_bytes.data(), cert_bytes.size())); if (!cert_bio) { LOG(ERROR) << AT << "Failed to create BIO"; return {}; } decoded_cert = bssl::UniquePtr(PEM_read_bio_X509(cert_bio.get(), nullptr, nullptr, nullptr)); } if (!decoded_cert) { LOG(ERROR) << AT << "Could not decode the cert."; return {}; } bssl::UniquePtr pub_key(X509_get_pubkey(decoded_cert.get())); if (!pub_key) { LOG(ERROR) << AT << "Could not extract public key."; return {}; } bssl::UniquePtr pub_key_bio(BIO_new(BIO_s_mem())); if (!pub_key_bio || i2d_PUBKEY_bio(pub_key_bio.get(), pub_key.get()) <= 0) { LOG(ERROR) << AT << "Could not serialize public key."; return {}; } const uint8_t* pub_key_bytes; size_t pub_key_len; if (!BIO_mem_contents(pub_key_bio.get(), &pub_key_bytes, &pub_key_len)) { LOG(ERROR) << AT << "Could not get bytes from BIO."; return {}; } return {pub_key_bytes, pub_key_bytes + pub_key_len}; } } // namespace