Botan 3.14.1
Crypto and TLS for C&
Botan::Certificate_Store_MacOS Class Referencefinal

#include <certstor_macos.h>

Inheritance diagram for Botan::Certificate_Store_MacOS:
Botan::Certificate_Store

Public Member Functions

std::vector< X509_DN > all_subjects () const override
bool certificate_known (const X509_Certificate &cert) const
 Certificate_Store_MacOS ()
 Certificate_Store_MacOS (Certificate_Store_MacOS &&)=default
 Certificate_Store_MacOS (const Certificate_Store_MacOS &)=default
bool contains (const X509_Certificate &cert) const override
std::vector< X509_Certificate > find_all_certs (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::optional< X509_Certificate > find_cert (const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
std::optional< X509_Certificate > find_cert_by_issuer_dn_and_serial_number (const X509_DN &issuer_dn, std::span< const uint8_t > serial_number) const override
std::optional< X509_Certificate > find_cert_by_pubkey_sha1 (const std::vector< uint8_t > &key_hash) const override
std::optional< X509_Certificate > find_cert_by_raw_subject_dn_sha256 (const std::vector< uint8_t > &subject_hash) const override
std::optional< X509_CRL > find_crl_for (const X509_Certificate &subject) const override
Certificate_Store_MacOS & operator= (Certificate_Store_MacOS &&)=default
Certificate_Store_MacOS & operator= (const Certificate_Store_MacOS &)=default

Detailed Description

Certificate Store that is backed by the system trust store on macOS. This opens a handle to the macOS keychain and serves certificate queries directly from there.

Definition at line 27 of file certstor_macos.h.

Constructor & Destructor Documentation

◆ Certificate_Store_MacOS() [1/3]

Botan::Certificate_Store_MacOS::Certificate_Store_MacOS ( )

Definition at line 350 of file certstor_macos.cpp.

350: m_impl(std::make_shared<Certificate_Store_MacOS_Impl>()) {}

Referenced by Certificate_Store_MacOS(), Certificate_Store_MacOS(), operator=(), and operator=().

◆ Certificate_Store_MacOS() [2/3]

Botan::Certificate_Store_MacOS::Certificate_Store_MacOS ( const Certificate_Store_MacOS & )
default

◆ Certificate_Store_MacOS() [3/3]

Botan::Certificate_Store_MacOS::Certificate_Store_MacOS ( Certificate_Store_MacOS && )
default

Member Function Documentation

◆ all_subjects()

std::vector< X509_DN > Botan::Certificate_Store_MacOS::all_subjects ( ) const
overridevirtual
Returns
DNs for all certificates managed by the store

Implements Botan::Certificate_Store.

Definition at line 352 of file certstor_macos.cpp.

352 {
353 // Note: This fetches and parses all certificates in the trust store.
354 // Apple's API provides SecCertificateCopyNormalizedSubjectSequence
355 // which facilitates reading the certificate DN without parsing the
356 // entire certificate via X509_Certificate. However, this
357 // function applies the same DN "normalization" as stated above.
358 const auto certificates = m_impl->findAll({});
359
360 std::vector<X509_DN> output;
361 std::transform(certificates.cbegin(),
362 certificates.cend(),
363 std::back_inserter(output),
364 [](const std::optional<X509_Certificate> cert) { return cert->subject_dn(); });
365
366 return output;
367}

◆ certificate_known()

bool Botan::Certificate_Store::certificate_known ( const X509_Certificate & cert) const
inherited

Old version of contains

Definition at line 24 of file certstor.cpp.

24 {
25 return contains(cert);
26}
virtual bool contains(const X509_Certificate &cert) const
Definition certstor.cpp:28

References contains().

Referenced by find_cert_by_issuer_dn_and_serial_number().

◆ contains()

bool Botan::Certificate_Store_MacOS::contains ( const X509_Certificate & cert) const
overridevirtual

Check whether the system trust store contains this exact certificate. Looks up the certificates with the same public key in the keychain and compares their encoding, instead of the generic subject DN lookup.

Reimplemented from Botan::Certificate_Store.

Definition at line 455 of file certstor_macos.cpp.

455 {
456 /*
457 The keychain cannot be searched by a hash of the whole certificate (unlike
458 CERT_FIND_SHA1_HASH on Windows). The most selective attribute the keychain
459 indexes is the SHA-1 hash of the public key, which is derived from the key
460 itself (see GH #2779) and shared only between certificates for the same
461 key, e.g. cross-signed variants of a root. Fetch those candidates and
462 compare their full encoding, so that, as in the generic implementation,
463 only a binary identical certificate counts.
464 */
465 Certificate_Store_MacOS_Impl::Query query;
466 query.addParameter(kSecAttrPublicKeyHash, cert.subject_public_key_bitstring_sha1());
467
468 const auto sha256 = cert.certificate_data_sha256();
469 for(const auto& candidate : m_impl->findAll(std::move(query))) {
470 if(std::ranges::equal(candidate.certificate_data_sha256(), sha256)) {
471 return true;
472 }
473 }
474
475 return false;
476}

References Botan::X509_Certificate::certificate_data_sha256(), and Botan::X509_Certificate::subject_public_key_bitstring_sha1().

◆ find_all_certs()

std::vector< X509_Certificate > Botan::Certificate_Store_MacOS::find_all_certs ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
overridevirtual

Find all certificates with a given Subject DN. Subject DN and even the key identifier might not be unique.

Implements Botan::Certificate_Store.

Definition at line 381 of file certstor_macos.cpp.

382 {
383 Certificate_Store_MacOS_Impl::Query query;
384 query.addParameter(kSecAttrSubject, normalizeAndSerialize(subject_dn));
385
386 if(!key_id.empty()) {
387 query.addParameter(kSecAttrSubjectKeyID, key_id);
388 }
389
390 return m_impl->findAll(std::move(query));
391}

◆ find_cert()

std::optional< X509_Certificate > Botan::Certificate_Store_MacOS::find_cert ( const X509_DN & subject_dn,
const std::vector< uint8_t > & key_id ) const
overridevirtual

Find a certificate by Subject DN and (optionally) key identifier

Returns
the first certificate that matches

Reimplemented from Botan::Certificate_Store.

Definition at line 369 of file certstor_macos.cpp.

370 {
371 Certificate_Store_MacOS_Impl::Query query;
372 query.addParameter(kSecAttrSubject, normalizeAndSerialize(subject_dn));
373
374 if(!key_id.empty()) {
375 query.addParameter(kSecAttrSubjectKeyID, key_id);
376 }
377
378 return m_impl->findOne(std::move(query));
379}

◆ find_cert_by_issuer_dn_and_serial_number()

std::optional< X509_Certificate > Botan::Certificate_Store_MacOS::find_cert_by_issuer_dn_and_serial_number ( const X509_DN & issuer_dn,
std::span< const uint8_t > serial_number ) const
overridevirtual

Find a certificate by searching for one with a matching issuer DN and serial number. Used for CMS or PKCS#7.

Parameters
issuer_dnthe distinguished name of the issuer
serial_numberthe certificate's serial number
Returns
a matching certificate or nullopt otherwise

TODO(Botan4) change this to use X509_Serial_Number

Implements Botan::Certificate_Store.

Definition at line 411 of file certstor_macos.cpp.

412 {
413 /*
414 The keychain stores kSecAttrSerialNumber as the content octets of the DER
415 INTEGER, i.e. with a leading zero octet if the top bit of the magnitude is
416 set and as a single zero octet for serial number zero. The interface passes
417 the unsigned magnitude (as X509_Certificate::serial_number returns it), so
418 it has to be re-encoded before it can be used in the query.
419 */
420 const auto serial = X509_Serial_Number::from_bytes(serial_number);
421
422 const auto lookup = [&](const X509_Serial_Number& s) {
423 Certificate_Store_MacOS_Impl::Query query;
424 query.addParameter(kSecAttrIssuer, normalizeAndSerialize(issuer_dn));
425
426 const auto contents = s.der_contents();
427 query.addParameter(kSecAttrSerialNumber, std::vector<uint8_t>(contents.begin(), contents.end()));
428
429 return m_impl->findOne(std::move(query));
430 };
431
432 if(auto cert = lookup(serial)) {
433 return cert;
434 }
435
436 /*
437 The other certificate stores compare the magnitude only, so they also find
438 a (non-conforming) certificate whose serial number is negative. Retry with
439 the negative encoding of the same magnitude to behave the same way.
440
441 TODO(Botan4) remove this behavior when negative serial number support is dropped
442 */
443 if(!serial.is_zero()) {
444 return lookup(X509_Serial_Number(-serial.to_bigint()));
445 }
446
447 return std::nullopt;
448}
static X509_Serial_Number from_bytes(std::span< const uint8_t > bytes)
std::string lookup(const OID &oid)
Definition oids.h:87

References Botan::X509_Serial_Number::from_bytes().

◆ find_cert_by_pubkey_sha1()

std::optional< X509_Certificate > Botan::Certificate_Store_MacOS::find_cert_by_pubkey_sha1 ( const std::vector< uint8_t > & key_hash) const
overridevirtual

Find a certificate by searching for one with a matching SHA-1 hash of public key.

Returns
a matching certificate or nullptr otherwise

Implements Botan::Certificate_Store.

Definition at line 393 of file certstor_macos.cpp.

394 {
395 if(key_hash.size() != 20) {
396 throw Invalid_Argument("Certificate_Store_MacOS::find_cert_by_pubkey_sha1 invalid hash");
397 }
398
399 Certificate_Store_MacOS_Impl::Query query;
400 query.addParameter(kSecAttrPublicKeyHash, key_hash);
401
402 return m_impl->findOne(std::move(query));
403}

◆ find_cert_by_raw_subject_dn_sha256()

std::optional< X509_Certificate > Botan::Certificate_Store_MacOS::find_cert_by_raw_subject_dn_sha256 ( const std::vector< uint8_t > & subject_hash) const
overridevirtual
Exceptions
Not_Implementedas this functionality is not available in the macOS certificate interface

Implements Botan::Certificate_Store.

Definition at line 405 of file certstor_macos.cpp.

406 {
407 BOTAN_UNUSED(subject_hash);
408 throw Not_Implemented("Certificate_Store_MacOS::find_cert_by_raw_subject_dn_sha256");
409}
#define BOTAN_UNUSED
Definition assert.h:151

References BOTAN_UNUSED.

◆ find_crl_for()

std::optional< X509_CRL > Botan::Certificate_Store_MacOS::find_crl_for ( const X509_Certificate & subject) const
overridevirtual

Fetching CRLs is not supported by the keychain on macOS. This will always return an empty list.

Reimplemented from Botan::Certificate_Store.

Definition at line 450 of file certstor_macos.cpp.

450 {
451 BOTAN_UNUSED(subject);
452 return {};
453}

References BOTAN_UNUSED.

◆ operator=() [1/2]

Certificate_Store_MacOS & Botan::Certificate_Store_MacOS::operator= ( Certificate_Store_MacOS && )
default

◆ operator=() [2/2]

Certificate_Store_MacOS & Botan::Certificate_Store_MacOS::operator= ( const Certificate_Store_MacOS & )
default

The documentation for this class was generated from the following files: