CCF
Loading...
Searching...
No Matches
ca.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/crypto/pem.h"
7
8#include <exception>
9
10using namespace ccf::crypto::OpenSSL;
11
12namespace tls
13{
14 class CA
15 {
16 private:
17 std::vector<Unique_X509> cas;
18 bool partial_ok = false;
19
20 void append_cert(const std::string& ca_string)
21 {
22 if (!ca_string.empty())
23 {
24 Unique_BIO bio(ca_string.data(), ca_string.size());
25 Unique_X509 ca;
26 if (!(ca = Unique_X509(bio, true)))
27 {
28 throw std::runtime_error(
29 "Could not parse CA: " + error_string(ERR_get_error()));
30 }
31 cas.push_back(std::move(ca));
32 }
33 }
34
35 public:
36 CA(const std::string& ca, bool partial_ok_ = false) :
37 partial_ok(partial_ok_)
38 {
39 append_cert(ca);
40 }
41
42 CA(const std::vector<std::string>& ca_strings, bool partial_ok_ = false) :
43 partial_ok(partial_ok_)
44 {
45 for (const auto& ca_string : ca_strings)
46 {
47 append_cert(ca_string);
48 }
49 }
50
51 CA(const std::vector<ccf::crypto::Pem>& ca_pems, bool partial_ok_ = false) :
52 partial_ok(partial_ok_)
53 {
54 for (const auto& ca_pem : ca_pems)
55 {
56 append_cert(ca_pem.str());
57 }
58 }
59
60 ~CA() = default;
61
62 void use(SSL_CTX* ssl_ctx)
63 {
64 X509_STORE* store = X509_STORE_new();
65 if (partial_ok)
66 {
67 CHECK1(X509_STORE_set_flags(store, X509_V_FLAG_PARTIAL_CHAIN));
68 }
69 for (const auto& ca : cas)
70 {
71 CHECK1(X509_STORE_add_cert(store, ca));
72 }
73 SSL_CTX_set_cert_store(ssl_ctx, store);
74 }
75 };
76}
Definition ca.h:15
CA(const std::vector< std::string > &ca_strings, bool partial_ok_=false)
Definition ca.h:42
~CA()=default
void use(SSL_CTX *ssl_ctx)
Definition ca.h:62
CA(const std::string &ca, bool partial_ok_=false)
Definition ca.h:36
CA(const std::vector< ccf::crypto::Pem > &ca_pems, bool partial_ok_=false)
Definition ca.h:51
Definition openssl_wrappers.h:29
void CHECK1(int rc)
Throws if rc is not 1 and has error.
Definition openssl_wrappers.h:58
Definition key_exchange.h:18
std::string error_string(int ec)
Definition tls.h:32
Definition openssl_wrappers.h:161
Definition openssl_wrappers.h:277