| 1 | #include "group.h" |
| 2 | #include "openssl_common.h" |
| 3 | #include "rsa.h" |
| 4 | #include <hpke/certificate.h> |
| 5 | #include <hpke/signature.h> |
| 6 | #include <memory> |
| 7 | #include <openssl/err.h> |
| 8 | #include <openssl/pem.h> |
| 9 | #include <openssl/x509.h> |
| 10 | #include <openssl/x509v3.h> |
| 11 | #include <tls/compat.h> |
| 12 | |
| 13 | namespace mlspp::hpke { |
| 14 | /// |
| 15 | /// Utility functions |
| 16 | /// |
| 17 | |
| 18 | static std::optional<bytes> |
| 19 | asn1_octet_string_to_bytes(const ASN1_OCTET_STRING* octets) |
| 20 | { |
| 21 | if (octets == nullptr) { |
| 22 | return std::nullopt; |
| 23 | } |
| 24 | const auto* ptr = ASN1_STRING_get0_data(octets); |
| 25 | const auto len = ASN1_STRING_length(octets); |
| 26 | // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 27 | return std::vector<uint8_t>(ptr, ptr + len); |
| 28 | } |
| 29 | |
| 30 | static std::string |
| 31 | asn1_string_to_std_string(const ASN1_STRING* asn1_string) |
| 32 | { |
| 33 | const auto* data = ASN1_STRING_get0_data(asn1_string); |
| 34 | const auto data_size = static_cast<size_t>(ASN1_STRING_length(asn1_string)); |
| 35 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 36 | auto str = std::string(reinterpret_cast<const char*>(data)); |
| 37 | if (str.size() != data_size) { |
| 38 | throw std::runtime_error("Malformed ASN.1 string"); |
| 39 | } |
| 40 | return str; |
| 41 | } |
| 42 | |
| 43 | static std::chrono::system_clock::time_point |
| 44 | asn1_time_to_chrono(const ASN1_TIME* asn1_time) |
| 45 | { |
| 46 | auto epoch_chrono = std::chrono::system_clock::time_point(); |
| 47 | auto epoch_time_t = std::chrono::system_clock::to_time_t(epoch_chrono); |
| 48 | auto epoch_asn1 = make_typed_unique(ASN1_TIME_set(nullptr, epoch_time_t)); |
| 49 | if (!epoch_asn1) { |
| 50 | throw openssl_error(); |
| 51 | } |
| 52 | |
| 53 | auto secs = int(0); |
| 54 | auto days = int(0); |
| 55 | if (ASN1_TIME_diff(&days, &secs, epoch_asn1.get(), asn1_time) != 1) { |
| 56 | throw openssl_error(); |
| 57 | } |
| 58 | |
| 59 | auto delta = std::chrono::seconds(secs) + std::chrono::hours(24 * days); |
| 60 | return std::chrono::system_clock::time_point(delta); |
| 61 | } |
| 62 | |
| 63 | /// |
| 64 | /// ParsedCertificate |
| 65 | /// |
| 66 | |
| 67 | const int Certificate::NameType::organization = NID_organizationName; |
| 68 | const int Certificate::NameType::common_name = NID_commonName; |
| 69 | const int Certificate::NameType::organizational_unit = |
| 70 | NID_organizationalUnitName; |
| 71 | const int Certificate::NameType::country = NID_countryName; |
| 72 | const int Certificate::NameType::serial_number = NID_serialNumber; |
| 73 | const int Certificate::NameType::state_or_province_name = |
| 74 | NID_stateOrProvinceName; |
| 75 | |
| 76 | struct RFC822Name |
| 77 | { |
| 78 | std::string value; |
| 79 | }; |
| 80 | |
| 81 | struct DNSName |
| 82 | { |
| 83 | std::string value; |
| 84 | }; |
| 85 | |
| 86 | using GeneralName = tls::var::variant<RFC822Name, DNSName>; |
| 87 | |
| 88 | struct Certificate::ParsedCertificate |
| 89 | { |
| 90 | |
| 91 | static std::unique_ptr<ParsedCertificate> parse(const bytes& der) |
| 92 | { |
| 93 | const auto* buf = der.data(); |
| 94 | auto cert = |
| 95 | make_typed_unique(d2i_X509(nullptr, &buf, static_cast<int>(der.size()))); |
| 96 | if (cert == nullptr) { |
| 97 | throw openssl_error(); |
| 98 | } |
| 99 | |
| 100 | return std::make_unique<ParsedCertificate>(cert.release()); |
| 101 | } |
| 102 | |
| 103 | static bytes compute_digest(const X509* cert) |
| 104 | { |
| 105 | const auto* md = EVP_sha256(); |
| 106 | auto digest = bytes(EVP_MD_size(md)); |
| 107 | unsigned int out_size = 0; |
| 108 | if (1 != X509_digest(cert, md, digest.data(), &out_size)) { |
| 109 | throw openssl_error(); |
| 110 | } |
| 111 | return digest; |
| 112 | } |
| 113 | |
| 114 | // Note: This method does not implement total general name parsing. |
| 115 | // Duplicate entries are not supported; if they are present, the last one |
| 116 | // presented by OpenSSL is chosen. |
| 117 | static ParsedName parse_names(const X509_NAME* x509_name) |
| 118 | { |
| 119 | if (x509_name == nullptr) { |
| 120 | throw openssl_error(); |
| 121 | } |
| 122 | |
| 123 | ParsedName parsed_name; |
| 124 | |
| 125 | for (int i = X509_NAME_entry_count(x509_name) - 1; i >= 0; i--) { |
| 126 | auto* entry = X509_NAME_get_entry(x509_name, i); |
| 127 | if (entry == nullptr) { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | auto* oid = X509_NAME_ENTRY_get_object(entry); |
| 132 | auto* asn_str = X509_NAME_ENTRY_get_data(entry); |
| 133 | if (oid == nullptr || asn_str == nullptr) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | const int nid = OBJ_obj2nid(oid); |
| 138 | const std::string parsed_value = asn1_string_to_std_string(asn_str); |
| 139 | parsed_name[nid] = parsed_value; |
| 140 | } |
| 141 | |
| 142 | return parsed_name; |
| 143 | } |
| 144 | |
| 145 | // Parse Subject Key Identifier Extension |
| 146 | static std::optional<bytes> parse_skid(X509* cert) |
| 147 | { |
| 148 | return asn1_octet_string_to_bytes(X509_get0_subject_key_id(cert)); |
| 149 | } |
| 150 | |
| 151 | // Parse Authority Key Identifier |
| 152 | static std::optional<bytes> parse_akid(X509* cert) |
| 153 | { |
| 154 | return asn1_octet_string_to_bytes(X509_get0_authority_key_id(cert)); |
| 155 | } |
| 156 | |
| 157 | static std::vector<GeneralName> parse_san(X509* cert) |
| 158 | { |
| 159 | std::vector<GeneralName> names; |
| 160 | |
| 161 | #ifdef WITH_BORINGSSL |
| 162 | using san_names_nb_t = size_t; |
| 163 | #else |
| 164 | using san_names_nb_t = int; |
| 165 | #endif |
| 166 | |
| 167 | san_names_nb_t san_names_nb = 0; |
| 168 | |
| 169 | auto* ext_ptr = |
| 170 | X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr); |
| 171 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 172 | auto* san_ptr = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(ext_ptr); |
| 173 | const auto san_names = make_typed_unique(san_ptr); |
| 174 | san_names_nb = sk_GENERAL_NAME_num(san_names.get()); |
| 175 | |
| 176 | // Check each name within the extension |
| 177 | for (san_names_nb_t i = 0; i < san_names_nb; i++) { |
| 178 | auto* current_name = sk_GENERAL_NAME_value(san_names.get(), i); |
| 179 | if (current_name->type == GEN_DNS) { |
| 180 | const auto dns_name = |
| 181 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) |
| 182 | asn1_string_to_std_string(current_name->d.dNSName); |
| 183 | names.emplace_back(DNSName{ dns_name }); |
| 184 | } else if (current_name->type == GEN_EMAIL) { |
| 185 | const auto email = |
| 186 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access |
| 187 | asn1_string_to_std_string(current_name->d.rfc822Name); |
| 188 | names.emplace_back(RFC822Name{ email }); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return names; |
| 193 | } |
| 194 | |
| 195 | explicit ParsedCertificate(X509* x509_in) |
| 196 | : x509(x509_in, typed_delete<X509>) |
| 197 | , pub_key_id(public_key_algorithm(x509.get())) |
| 198 | , sig_algo(signature_algorithm(x509.get())) |
| 199 | , issuer_hash(X509_issuer_name_hash(x509.get())) |
| 200 | , subject_hash(X509_subject_name_hash(x509.get())) |
| 201 | , issuer(parse_names(X509_get_issuer_name(x509.get()))) |
| 202 | , subject(parse_names(X509_get_subject_name(x509.get()))) |
| 203 | , subject_key_id(parse_skid(x509.get())) |
| 204 | , authority_key_id(parse_akid(x509.get())) |
| 205 | , sub_alt_names(parse_san(x509.get())) |
| 206 | , is_ca(X509_check_ca(x509.get()) != 0) |
| 207 | , hash(compute_digest(x509.get())) |
| 208 | , not_before(asn1_time_to_chrono(X509_get0_notBefore(x509.get()))) |
| 209 | , not_after(asn1_time_to_chrono(X509_get0_notAfter(x509.get()))) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | ParsedCertificate(const ParsedCertificate& other) |
| 214 | : x509(nullptr, typed_delete<X509>) |
| 215 | , pub_key_id(public_key_algorithm(other.x509.get())) |
| 216 | , sig_algo(signature_algorithm(other.x509.get())) |
| 217 | , issuer_hash(other.issuer_hash) |
| 218 | , subject_hash(other.subject_hash) |
| 219 | , issuer(other.issuer) |
| 220 | , subject(other.subject) |
| 221 | , subject_key_id(other.subject_key_id) |
| 222 | , authority_key_id(other.authority_key_id) |
| 223 | , sub_alt_names(other.sub_alt_names) |
| 224 | , is_ca(other.is_ca) |
| 225 | , hash(other.hash) |
| 226 | , not_before(other.not_before) |
| 227 | , not_after(other.not_after) |
| 228 | { |
| 229 | if (1 != X509_up_ref(other.x509.get())) { |
| 230 | throw openssl_error(); |
| 231 | } |
| 232 | x509.reset(other.x509.get()); |
| 233 | } |
| 234 | |
| 235 | static Signature::ID public_key_algorithm(X509* x509) |
| 236 | { |
| 237 | #if WITH_BORINGSSL |
| 238 | const auto pub = make_typed_unique(X509_get_pubkey(x509)); |
| 239 | const auto* pub_ptr = pub.get(); |
| 240 | #else |
| 241 | const auto* pub_ptr = X509_get0_pubkey(x509); |
| 242 | #endif |
| 243 | |
| 244 | switch (EVP_PKEY_base_id(pub_ptr)) { |
| 245 | case EVP_PKEY_ED25519: |
| 246 | return Signature::ID::Ed25519; |
| 247 | #if !defined(WITH_BORINGSSL) |
| 248 | case EVP_PKEY_ED448: |
| 249 | return Signature::ID::Ed448; |
| 250 | #endif |
| 251 | case EVP_PKEY_EC: { |
| 252 | auto key_size = EVP_PKEY_bits(pub_ptr); |
| 253 | switch (key_size) { |
| 254 | case 256: |
| 255 | return Signature::ID::P256_SHA256; |
| 256 | case 384: |
| 257 | return Signature::ID::P384_SHA384; |
| 258 | case 521: |
| 259 | return Signature::ID::P521_SHA512; |
| 260 | default: |
| 261 | throw std::runtime_error("Unknown curve"); |
| 262 | } |
| 263 | } |
| 264 | case EVP_PKEY_RSA: |
| 265 | // RSA public keys are not specific to an algorithm |
| 266 | return Signature::ID::RSA_SHA256; |
| 267 | default: |
| 268 | break; |
| 269 | } |
| 270 | throw std::runtime_error("Unsupported public key algorithm"); |
| 271 | } |
| 272 | |
| 273 | static Signature::ID signature_algorithm(X509* cert) |
| 274 | { |
| 275 | auto nid = X509_get_signature_nid(cert); |
| 276 | switch (nid) { |
| 277 | case EVP_PKEY_ED25519: |
| 278 | return Signature::ID::Ed25519; |
| 279 | #if !defined(WITH_BORINGSSL) |
| 280 | case EVP_PKEY_ED448: |
| 281 | return Signature::ID::Ed448; |
| 282 | #endif |
| 283 | case NID_ecdsa_with_SHA256: |
| 284 | return Signature::ID::P256_SHA256; |
| 285 | case NID_ecdsa_with_SHA384: |
| 286 | return Signature::ID::P384_SHA384; |
| 287 | case NID_ecdsa_with_SHA512: |
| 288 | return Signature::ID::P521_SHA512; |
| 289 | case NID_sha1WithRSAEncryption: |
| 290 | // We fall through to SHA256 for SHA1 because we do not implement SHA-1. |
| 291 | case NID_sha256WithRSAEncryption: |
| 292 | return Signature::ID::RSA_SHA256; |
| 293 | case NID_sha384WithRSAEncryption: |
| 294 | return Signature::ID::RSA_SHA384; |
| 295 | case NID_sha512WithRSAEncryption: |
| 296 | return Signature::ID::RSA_SHA512; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | throw std::runtime_error("Unsupported signature algorithm"); |
| 302 | } |
| 303 | |
| 304 | typed_unique_ptr<EVP_PKEY> public_key() const |
| 305 | { |
| 306 | return make_typed_unique<EVP_PKEY>(X509_get_pubkey(x509.get())); |
| 307 | } |
| 308 | |
| 309 | Certificate::ExpirationStatus expiration_status() const |
| 310 | { |
| 311 | auto now = std::chrono::system_clock::now(); |
| 312 | |
| 313 | if (now < not_before) { |
| 314 | return Certificate::ExpirationStatus::inactive; |
| 315 | } |
| 316 | |
| 317 | if (now > not_after) { |
| 318 | return Certificate::ExpirationStatus::expired; |
| 319 | } |
| 320 | |
| 321 | return Certificate::ExpirationStatus::active; |
| 322 | } |
| 323 | |
| 324 | bytes raw() const |
| 325 | { |
| 326 | auto out = bytes(i2d_X509(x509.get(), nullptr)); |
| 327 | auto* ptr = out.data(); |
| 328 | i2d_X509(x509.get(), &ptr); |
| 329 | return out; |
| 330 | } |
| 331 | |
| 332 | typed_unique_ptr<X509> x509; |
| 333 | const Signature::ID pub_key_id; |
| 334 | const Signature::ID sig_algo; |
| 335 | const uint64_t issuer_hash; |
| 336 | const uint64_t subject_hash; |
| 337 | const ParsedName issuer; |
| 338 | const ParsedName subject; |
| 339 | const std::optional<bytes> subject_key_id; |
| 340 | const std::optional<bytes> authority_key_id; |
| 341 | const std::vector<GeneralName> sub_alt_names; |
| 342 | const bool is_ca; |
| 343 | const bytes hash; |
| 344 | const std::chrono::system_clock::time_point not_before; |
| 345 | const std::chrono::system_clock::time_point not_after; |
| 346 | }; |
| 347 | |
| 348 | /// |
| 349 | /// Certificate |
| 350 | /// |
| 351 | |
| 352 | static std::unique_ptr<Signature::PublicKey> |
| 353 | signature_key(EVP_PKEY* pkey) |
| 354 | { |
| 355 | switch (EVP_PKEY_base_id(pkey)) { |
| 356 | case EVP_PKEY_RSA: |
| 357 | return std::make_unique<RSASignature::PublicKey>(pkey); |
| 358 | |
| 359 | case EVP_PKEY_ED448: |
| 360 | case EVP_PKEY_ED25519: |
| 361 | case EVP_PKEY_EC: |
| 362 | return std::make_unique<EVPGroup::PublicKey>(pkey); |
| 363 | |
| 364 | default: |
| 365 | throw std::runtime_error("Unsupported algorithm"); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | Certificate::Certificate(std::unique_ptr<ParsedCertificate>&& parsed_cert_in) |
| 370 | : parsed_cert(std::move(parsed_cert_in)) |
| 371 | , public_key(signature_key(parsed_cert->public_key().release())) |
| 372 | , raw(parsed_cert->raw()) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | Certificate::Certificate(const bytes& der) |
| 377 | : parsed_cert(ParsedCertificate::parse(der)) |
| 378 | , public_key(signature_key(parsed_cert->public_key().release())) |
| 379 | , raw(der) |
| 380 | { |
| 381 | } |
| 382 | |
| 383 | Certificate::Certificate(const Certificate& other) |
| 384 | : parsed_cert(std::make_unique<ParsedCertificate>(*other.parsed_cert)) |
| 385 | , public_key(signature_key(parsed_cert->public_key().release())) |
| 386 | , raw(other.raw) |
| 387 | { |
| 388 | } |
| 389 | |
| 390 | Certificate::~Certificate() = default; |
| 391 | |
| 392 | std::vector<Certificate> |
| 393 | Certificate::parse_pem(const bytes& pem) |
| 394 | { |
| 395 | auto size_int = static_cast<int>(pem.size()); |
| 396 | auto bio = make_typed_unique<BIO>(BIO_new_mem_buf(pem.data(), size_int)); |
| 397 | if (!bio) { |
| 398 | throw openssl_error(); |
| 399 | } |
| 400 | |
| 401 | auto certs = std::vector<Certificate>(); |
| 402 | while (true) { |
| 403 | auto x509 = make_typed_unique<X509>( |
| 404 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 405 | if (!x509) { |
| 406 | // NOLINTNEXTLINE(hicpp-signed-bitwise) |
| 407 | auto err = ERR_GET_REASON(ERR_peek_last_error()); |
| 408 | if (err == PEM_R_NO_START_LINE) { |
| 409 | // No more objects to read |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | throw openssl_error(); |
| 414 | } |
| 415 | |
| 416 | auto parsed = std::make_unique<ParsedCertificate>(x509.release()); |
| 417 | certs.emplace_back(std::move(parsed)); |
| 418 | } |
| 419 | |
| 420 | return certs; |
| 421 | } |
| 422 | |
| 423 | bool |
| 424 | Certificate::valid_from(const Certificate& parent) const |
| 425 | { |
| 426 | auto pub = parent.parsed_cert->public_key(); |
| 427 | return (1 == X509_verify(parsed_cert->x509.get(), pub.get())); |
| 428 | } |
| 429 | |
| 430 | uint64_t |
| 431 | Certificate::issuer_hash() const |
| 432 | { |
| 433 | return parsed_cert->issuer_hash; |
| 434 | } |
| 435 | |
| 436 | uint64_t |
| 437 | Certificate::subject_hash() const |
| 438 | { |
| 439 | return parsed_cert->subject_hash; |
| 440 | } |
| 441 | |
| 442 | Certificate::ParsedName |
| 443 | Certificate::subject() const |
| 444 | { |
| 445 | return parsed_cert->subject; |
| 446 | } |
| 447 | |
| 448 | Certificate::ParsedName |
| 449 | Certificate::issuer() const |
| 450 | { |
| 451 | return parsed_cert->issuer; |
| 452 | } |
| 453 | |
| 454 | bool |
| 455 | Certificate::is_ca() const |
| 456 | { |
| 457 | return parsed_cert->is_ca; |
| 458 | } |
| 459 | |
| 460 | Certificate::ExpirationStatus |
| 461 | Certificate::expiration_status() const |
| 462 | { |
| 463 | return parsed_cert->expiration_status(); |
| 464 | } |
| 465 | |
| 466 | std::optional<bytes> |
| 467 | Certificate::subject_key_id() const |
| 468 | { |
| 469 | return parsed_cert->subject_key_id; |
| 470 | } |
| 471 | |
| 472 | std::optional<bytes> |
| 473 | Certificate::authority_key_id() const |
| 474 | { |
| 475 | return parsed_cert->authority_key_id; |
| 476 | } |
| 477 | |
| 478 | std::vector<std::string> |
| 479 | Certificate::email_addresses() const |
| 480 | { |
| 481 | std::vector<std::string> emails; |
| 482 | for (const auto& name : parsed_cert->sub_alt_names) { |
| 483 | if (tls::var::holds_alternative<RFC822Name>(name)) { |
| 484 | emails.emplace_back(tls::var::get<RFC822Name>(name).value); |
| 485 | } |
| 486 | } |
| 487 | return emails; |
| 488 | } |
| 489 | |
| 490 | std::vector<std::string> |
| 491 | Certificate::dns_names() const |
| 492 | { |
| 493 | std::vector<std::string> domains; |
| 494 | for (const auto& name : parsed_cert->sub_alt_names) { |
| 495 | if (tls::var::holds_alternative<DNSName>(name)) { |
| 496 | domains.emplace_back(tls::var::get<DNSName>(name).value); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return domains; |
| 501 | } |
| 502 | |
| 503 | bytes |
| 504 | Certificate::hash() const |
| 505 | { |
| 506 | return parsed_cert->hash; |
| 507 | } |
| 508 | |
| 509 | std::chrono::system_clock::time_point |
| 510 | Certificate::not_before() const |
| 511 | { |
| 512 | return parsed_cert->not_before; |
| 513 | } |
| 514 | |
| 515 | std::chrono::system_clock::time_point |
| 516 | Certificate::not_after() const |
| 517 | { |
| 518 | return parsed_cert->not_after; |
| 519 | } |
| 520 | |
| 521 | Signature::ID |
| 522 | Certificate::public_key_algorithm() const |
| 523 | { |
| 524 | return parsed_cert->pub_key_id; |
| 525 | } |
| 526 | |
| 527 | Signature::ID |
| 528 | Certificate::signature_algorithm() const |
| 529 | { |
| 530 | return parsed_cert->sig_algo; |
| 531 | } |
| 532 | |
| 533 | bool |
| 534 | operator==(const Certificate& lhs, const Certificate& rhs) |
| 535 | { |
| 536 | return lhs.raw == rhs.raw; |
| 537 | } |
| 538 | |
| 539 | } // namespace mlspp::hpke |