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 ca = Unique_X509(bio, true);
27 if (ca == nullptr)
28 {
29 throw std::runtime_error(
30 "Could not parse CA: " + error_string(ERR_get_error()));
31 }
32 cas.push_back(std::move(ca));
33 }
34 }
35
36 public:
37 CA(const std::string& ca, bool partial_ok_ = false) :
38 partial_ok(partial_ok_)
39 {
40 append_cert(ca);
41 }
42
43 CA(const std::vector<std::string>& ca_strings, bool partial_ok_ = false) :
44 partial_ok(partial_ok_)
45 {
46 for (const auto& ca_string : ca_strings)
47 {
48 append_cert(ca_string);
49 }
50 }
51
52 CA(const std::vector<ccf::crypto::Pem>& ca_pems, bool partial_ok_ = false) :
53 partial_ok(partial_ok_)
54 {
55 for (const auto& ca_pem : ca_pems)
56 {
57 append_cert(ca_pem.str());
58 }
59 }
60
61 ~CA() = default;
62
63 void use(SSL_CTX* ssl_ctx)
64 {
65 X509_STORE* store = X509_STORE_new();
66 if (partial_ok)
67 {
68 CHECK1(X509_STORE_set_flags(store, X509_V_FLAG_PARTIAL_CHAIN));
69 }
70 for (const auto& ca : cas)
71 {
72 CHECK1(X509_STORE_add_cert(store, ca));
73 }
74 SSL_CTX_set_cert_store(ssl_ctx, store);
75 }
76 };
77}
Definition ca.h:15
CA(const std::vector< std::string > &ca_strings, bool partial_ok_=false)
Definition ca.h:43
~CA()=default
void use(SSL_CTX *ssl_ctx)
Definition ca.h:63
CA(const std::string &ca, bool partial_ok_=false)
Definition ca.h:37
CA(const std::vector< ccf::crypto::Pem > &ca_pems, bool partial_ok_=false)
Definition ca.h:52
Definition openssl_wrappers.h:27
void CHECK1(int rc)
Throws if rc is not 1 and has error.
Definition openssl_wrappers.h:54
Definition key_exchange.h:18
std::string error_string(int ec)
Definition tls.h:32
Definition openssl_wrappers.h:159
Definition openssl_wrappers.h:273