Botan 3.14.1
Crypto and TLS for C&
certstor_macos.cpp
Go to the documentation of this file.
1/*
2* Certificate Store
3* (C) 1999-2019 Jack Lloyd
4* (C) 2019-2020 René Meusel
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/certstor_macos.h>
10
11#include <botan/assert.h>
12#include <botan/ber_dec.h>
13#include <botan/bigint.h>
14#include <botan/exceptn.h>
15#include <botan/pkix_types.h>
16#include <botan/internal/x509_cert_cache.h>
17
18#include <algorithm>
19#include <array>
20
21#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
22#include <CoreFoundation/CoreFoundation.h>
23#include <CoreServices/CoreServices.h>
24
25namespace Botan {
26
27namespace {
28
29/**
30 * Abstract RAII wrapper for CFTypeRef-style object handles
31 * All of those xxxRef types are eventually typedefs to void*
32 */
33template <typename T>
34class scoped_CFType {
35 public:
36 explicit scoped_CFType(T value) : m_value(value) {}
37
38 scoped_CFType(const scoped_CFType<T>& rhs) = delete;
39
40 scoped_CFType(scoped_CFType<T>&& rhs) : m_value(std::move(rhs.m_value)) { rhs.m_value = nullptr; }
41
42 ~scoped_CFType() {
43 if(m_value) {
44 CFRelease(m_value);
45 }
46 }
47
48 operator bool() const { return m_value != nullptr; }
49
50 void assign(T value) {
51 BOTAN_ASSERT(m_value == nullptr, "scoped_CFType was not set yet");
52 m_value = value;
53 }
54
55 T& get() { return m_value; }
56
57 const T& get() const { return m_value; }
58
59 private:
60 T m_value;
61};
62
63/**
64 * Apple's DN parser "normalizes" ASN1 'PrintableString' into upper-case values
65 * and strips leading, trailing as well as multiple white spaces.
66 * See: opensource.apple.com/source/Security/Security-55471/sec/Security/SecCertificate.c.auto.html
67 */
68X509_DN normalize(const X509_DN& dn) {
69 X509_DN result{};
70
71 // TODO use X509_DN::rdns here
72 const auto flat = dn.dn_info();
73 for(const auto& [oid, str] : flat) {
74 if(str.tagging() == ASN1_Type::PrintableString) {
75 std::string normalized;
76 normalized.reserve(str.value().size());
77
78 for(const char c : str.value()) {
79 if(c != ' ') {
80 // PrintableString is ASCII-only; locale-independent fold to upper case
81 const char up = (c >= 'a' && c <= 'z') ? static_cast<char>(c - ('a' - 'A')) : c;
82 normalized.push_back(up);
83 } else if(!normalized.empty() && normalized.back() != ' ') {
84 // remove leading and squash multiple white spaces
85 normalized.push_back(c);
86 }
87 }
88
89 if(!normalized.empty() && normalized.back() == ' ') {
90 // remove potential remaining single trailing white space char
91 normalized.pop_back();
92 }
93
94 result.add_attribute(oid, ASN1_String(normalized, str.tagging()));
95 } else {
96 result.add_attribute(oid, str);
97 }
98 }
99 return result;
100}
101
102std::vector<uint8_t> normalizeAndSerialize(const X509_DN& dn) {
103 return normalize(dn).DER_encode();
104}
105
106std::string to_string(const CFStringRef cfstring) {
107 const char* ccstr = CFStringGetCStringPtr(cfstring, kCFStringEncodingUTF8);
108
109 if(ccstr != nullptr) {
110 return std::string(ccstr);
111 }
112
113 auto utf16_pairs = CFStringGetLength(cfstring);
114 auto max_utf8_bytes = CFStringGetMaximumSizeForEncoding(utf16_pairs, kCFStringEncodingUTF8);
115
116 std::vector<char> cstr(max_utf8_bytes, '\0');
117 auto result = CFStringGetCString(cfstring, cstr.data(), cstr.size(), kCFStringEncodingUTF8);
118
119 return (result) ? std::string(cstr.data()) : std::string();
120}
121
122std::string to_string(const OSStatus status) {
123 scoped_CFType<CFStringRef> eCFString(SecCopyErrorMessageString(status, nullptr));
124 return to_string(eCFString.get());
125}
126
127void check_success(const OSStatus status, const std::string context) {
128 if(errSecSuccess == status) {
129 return;
130 }
131
132 throw Internal_Error(std::string("failed to " + context + ": " + to_string(status)));
133}
134
135template <typename T>
136void check_notnull(const T& value, const std::string context) {
137 if(value) {
138 return;
139 }
140
141 throw Internal_Error(std::string("failed to ") + context);
142}
143
144} // namespace
145
146/**
147 * Internal class implementation (i.e. Pimpl) to keep the required platform-
148 * dependent members of Certificate_Store_MacOS contained in this compilation
149 * unit.
150 */
151class Certificate_Store_MacOS_Impl {
152 private:
153 static constexpr const char* system_roots = "/System/Library/Keychains/SystemRootCertificates.keychain";
154 static constexpr const char* system_keychain = "/Library/Keychains/System.keychain";
155
156 // Same size as the Windows system certificate store uses; arbitrary but
157 // large enough that repeated lookups of the same roots hit the cache
158 static constexpr size_t SystemStore_CertCacheSize = 128;
159
160 public:
161 /**
162 * Wraps a list of search query parameters that are later passed into
163 * Apple's certificate store API. The class provides some convenience
164 * functionality and handles the query parameter's data lifetime.
165 */
166 class Query {
167 public:
168 Query() = default;
169 ~Query() = default;
170 Query(Query&& other) = default;
171 Query& operator=(Query&& other) = default;
172
173 Query(const Query& other) = delete;
174 Query& operator=(const Query& other) = delete;
175
176 public:
177 void addParameter(CFStringRef key, CFTypeRef value) {
178 m_keys.emplace_back(key);
179 m_values.emplace_back(value);
180 }
181
182 void addParameter(CFStringRef key, std::vector<uint8_t> value) {
183 const auto& data = m_data_store.emplace_back(std::move(value));
184
185 const auto& data_ref = m_data_refs.emplace_back(
186 CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, data.data(), data.size(), kCFAllocatorNull));
187 check_notnull(data_ref, "create CFDataRef of search object failed");
188
189 addParameter(key, data_ref.get());
190 }
191
192 /**
193 * Amends the user-provided search query with generic filter rules
194 * for the associated system keychains and transforms it into a
195 * representation that can be passed to the Apple keychain API.
196 */
197 scoped_CFType<CFDictionaryRef> prepare(const CFArrayRef& keychains, const SecPolicyRef& policy) {
198 addParameter(kSecClass, kSecClassCertificate);
199 addParameter(kSecReturnRef, kCFBooleanTrue);
200 addParameter(kSecMatchLimit, kSecMatchLimitAll);
201 addParameter(kSecMatchTrustedOnly, kCFBooleanTrue);
202 addParameter(kSecMatchSearchList, keychains);
203 addParameter(kSecMatchPolicy, policy);
204
205 BOTAN_ASSERT_EQUAL(m_keys.size(), m_values.size(), "valid key-value pairs");
206
207 auto query = scoped_CFType<CFDictionaryRef>(CFDictionaryCreate(kCFAllocatorDefault,
208 (const void**)m_keys.data(),
209 (const void**)m_values.data(),
210 m_keys.size(),
211 &kCFTypeDictionaryKeyCallBacks,
212 &kCFTypeDictionaryValueCallBacks));
213 check_notnull(query, "create search query");
214
215 return query;
216 }
217
218 private:
219 using Data = std::vector<std::vector<uint8_t>>;
220 using DataRefs = std::vector<scoped_CFType<CFDataRef>>;
221 using Keys = std::vector<CFStringRef>;
222 using Values = std::vector<CFTypeRef>;
223
224 Data m_data_store; //! makes sure that data parameters are kept alive
225 DataRefs m_data_refs; //! keeps track of CFDataRef objects referring into \p m_data_store
226 Keys m_keys; //! ordered list of search parameter keys
227 Values m_values; //! ordered list of search parameter values
228 };
229
230 public:
231 Certificate_Store_MacOS_Impl() :
232 m_policy(SecPolicyCreateBasicX509()),
233 m_system_roots(nullptr),
234 m_system_chain(nullptr),
235 m_keychains(nullptr),
236 m_cert_cache(SystemStore_CertCacheSize) {
239 // macOS 12.0 deprecates 'Custom keychain management', though the API still works.
240 // Ideas for a replacement can be found in the discussion of GH #3122:
241 // https://github.com/randombit/botan/pull/3122
242 check_success(SecKeychainOpen(system_roots, &m_system_roots.get()), "open system root certificates");
243 check_success(SecKeychainOpen(system_keychain, &m_system_chain.get()), "open system keychain");
245 check_notnull(m_system_roots, "open system root certificate chain");
246 check_notnull(m_system_chain, "open system certificate chain");
247
248 // m_keychains is merely a convenience list view into all open keychain
249 // objects. This list is required in prepareQuery().
250 std::array<const void*, 2> keychains{{m_system_roots.get(), m_system_chain.get()}};
251
252 m_keychains.assign(
253 CFArrayCreate(kCFAllocatorDefault, keychains.data(), keychains.size(), &kCFTypeArrayCallBacks));
254 check_notnull(m_keychains, "initialize keychain array");
255 }
256
257 std::optional<X509_Certificate> findOne(Query query) const {
258 query.addParameter(kSecMatchLimit, kSecMatchLimitOne);
259
260 scoped_CFType<CFTypeRef> result(nullptr);
261 search(std::move(query), &result.get());
262
263 if(result)
264 return readCertificate(result.get());
265 else
266 return std::nullopt;
267 }
268
269 std::vector<X509_Certificate> findAll(Query query) const {
270 query.addParameter(kSecMatchLimit, kSecMatchLimitAll);
271
272 scoped_CFType<CFArrayRef> result(nullptr);
273 search(std::move(query), (CFTypeRef*)&result.get());
274
275 std::vector<X509_Certificate> output;
276
277 if(result) {
278 const auto count = CFArrayGetCount(result.get());
279 BOTAN_ASSERT(count > 0, "certificate result list contains data");
280
281 for(unsigned int i = 0; i < count; ++i) {
282 auto cert = CFArrayGetValueAtIndex(result.get(), i);
283 output.emplace_back(readCertificate(cert));
284 }
285 }
286
287 return output;
288 }
289
290 protected:
291 void search(Query query, CFTypeRef* result) const {
292 scoped_CFType<CFDictionaryRef> fullQuery(query.prepare(keychains(), policy()));
293
294 auto status = SecItemCopyMatching(fullQuery.get(), result);
295
296 if(errSecItemNotFound == status) {
297 return; // no matches
298 }
299
300 check_success(status, "look up certificate");
301 check_notnull(result, "look up certificate (invalid result value)");
302 }
303
304 /**
305 * Convert a CFTypeRef object into a X509_Certificate
306 *
307 * The DER encoding is looked up in (or inserted into) the certificate
308 * cache, so repeated hits for the same keychain item share one parsed
309 * certificate instead of being parsed again on every lookup.
310 */
311 X509_Certificate readCertificate(CFTypeRef object) const {
312 if(!object || CFGetTypeID(object) != SecCertificateGetTypeID()) {
313 throw Internal_Error("cannot convert CFTypeRef to SecCertificateRef");
314 }
315
316 auto cert = static_cast<SecCertificateRef>(const_cast<void*>(object));
317
318 scoped_CFType<CFDataRef> derData(SecCertificateCopyData(cert));
319 check_notnull(derData, "read extracted certificate");
320
321 const auto data = CFDataGetBytePtr(derData.get());
322 const auto length = static_cast<size_t>(CFDataGetLength(derData.get()));
323
324 return m_cert_cache.find_or_insert({data, length});
325 }
326
327 CFArrayRef keychains() const { return m_keychains.get(); }
328
329 SecPolicyRef policy() const { return m_policy.get(); }
330
331 private:
332 scoped_CFType<SecPolicyRef> m_policy;
333 scoped_CFType<SecKeychainRef> m_system_roots;
334 scoped_CFType<SecKeychainRef> m_system_chain;
335 scoped_CFType<CFArrayRef> m_keychains;
336
337 // The cache has its own mutex and is used from const lookup functions;
338 // everything else in this class is immutable after construction.
339 mutable X509_Certificate_Cache m_cert_cache;
340};
341
342//
343// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
344//
345// Implementation of Certificate_Store interface ...
346//
347// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
348//
349
350Certificate_Store_MacOS::Certificate_Store_MacOS() : m_impl(std::make_shared<Certificate_Store_MacOS_Impl>()) {}
351
352std::vector<X509_DN> Certificate_Store_MacOS::all_subjects() const {
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}
368
369std::optional<X509_Certificate> Certificate_Store_MacOS::find_cert(const X509_DN& subject_dn,
370 const std::vector<uint8_t>& key_id) const {
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}
380
381std::vector<X509_Certificate> Certificate_Store_MacOS::find_all_certs(const X509_DN& subject_dn,
382 const std::vector<uint8_t>& key_id) const {
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}
392
394 const std::vector<uint8_t>& key_hash) const {
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}
404
406 const std::vector<uint8_t>& subject_hash) const {
407 BOTAN_UNUSED(subject_hash);
408 throw Not_Implemented("Certificate_Store_MacOS::find_cert_by_raw_subject_dn_sha256");
409}
410
412 const X509_DN& issuer_dn, std::span<const uint8_t> serial_number) const {
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}
449
450std::optional<X509_CRL> Certificate_Store_MacOS::find_crl_for(const X509_Certificate& subject) const {
451 BOTAN_UNUSED(subject);
452 return {};
453}
454
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}
477
478} // namespace Botan
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:128
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:125
#define BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
Definition api.h:126
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:95
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
std::optional< X509_Certificate > find_cert_by_pubkey_sha1(const std::vector< uint8_t > &key_hash) const override
std::optional< X509_Certificate > find_cert(const X509_DN &subject_dn, const std::vector< uint8_t > &key_id) const override
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_by_issuer_dn_and_serial_number(const X509_DN &issuer_dn, std::span< const uint8_t > serial_number) const override
std::vector< X509_DN > all_subjects() 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
X509_Certificate find_or_insert(std::span< const uint8_t > encoding)
std::span< const uint8_t, 32 > certificate_data_sha256() const
Definition x509cert.cpp:477
const std::vector< uint8_t > & subject_public_key_bitstring_sha1() const
Definition x509cert.cpp:418
static X509_Serial_Number from_bytes(std::span< const uint8_t > bytes)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13