CCF
Loading...
Searching...
No Matches
identity.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
5#include "ccf/crypto/curve.h"
7#include "crypto/certs.h"
9
10#include <openssl/crypto.h>
11#include <string>
12#include <vector>
13
14namespace ccf
15{
16 enum class IdentityType
17 {
19 SPLIT
20 };
21
23 {
26 std::optional<IdentityType> type = IdentityType::REPLICATED;
27 std::string subject_name = "CN=CCF Service";
28
29 bool operator==(const NetworkIdentity& other) const
30 {
31 return cert == other.cert && priv_key == other.priv_key &&
32 type == other.type && subject_name == other.subject_name;
33 }
34
35 NetworkIdentity(const std::string& subject_name_) :
37 subject_name(subject_name_)
38 {}
39 NetworkIdentity() = default;
40
42 const std::string& valid_from, size_t validity_period_days)
43 {
44 return {};
45 }
46
47 virtual void set_certificate(const ccf::crypto::Pem& certificate) {}
48
49 virtual ~NetworkIdentity() {}
50 };
51
53 {
54 public:
56
58 const std::string& subject_name_,
59 ccf::crypto::CurveID curve_id,
60 const std::string& valid_from,
61 size_t validity_period_days) :
62 NetworkIdentity(subject_name_)
63 {
64 auto identity_key_pair =
65 std::make_shared<ccf::crypto::KeyPair_OpenSSL>(curve_id);
66 priv_key = identity_key_pair->private_key_pem();
67
68 cert = ccf::crypto::create_self_signed_cert(
69 identity_key_pair,
71 {} /* SAN */,
72 valid_from,
73 validity_period_days);
74 }
75
77 NetworkIdentity(ccf::crypto::get_subject_name(other.cert))
78 {
79 if (type != other.type)
80 {
81 throw std::runtime_error("invalid identity type conversion");
82 }
83 priv_key = other.priv_key;
84 cert = other.cert;
85 }
86
88 const std::string& valid_from, size_t validity_period_days) override
89 {
90 auto identity_key_pair =
91 std::make_shared<ccf::crypto::KeyPair_OpenSSL>(priv_key);
92
93 return ccf::crypto::create_self_signed_cert(
94 identity_key_pair,
96 {} /* SAN */,
97 valid_from,
98 validity_period_days);
99 }
100
101 virtual void set_certificate(const ccf::crypto::Pem& new_cert) override
102 {
103 cert = new_cert;
104 }
105
107 {
108 OPENSSL_cleanse(priv_key.data(), priv_key.size());
109 }
110 };
111}
Definition identity.h:53
ReplicatedNetworkIdentity(const std::string &subject_name_, ccf::crypto::CurveID curve_id, const std::string &valid_from, size_t validity_period_days)
Definition identity.h:57
virtual ccf::crypto::Pem issue_certificate(const std::string &valid_from, size_t validity_period_days) override
Definition identity.h:87
~ReplicatedNetworkIdentity() override
Definition identity.h:106
ReplicatedNetworkIdentity(const NetworkIdentity &other)
Definition identity.h:76
virtual void set_certificate(const ccf::crypto::Pem &new_cert) override
Definition identity.h:101
Definition pem.h:18
size_t size() const
Definition pem.h:61
uint8_t * data()
Definition pem.h:51
CurveID
Definition curve.h:18
Definition app_interface.h:15
IdentityType
Definition identity.h:17
Definition identity.h:23
virtual ccf::crypto::Pem issue_certificate(const std::string &valid_from, size_t validity_period_days)
Definition identity.h:41
NetworkIdentity()=default
NetworkIdentity(const std::string &subject_name_)
Definition identity.h:35
std::string subject_name
Definition identity.h:27
ccf::crypto::Pem cert
Definition identity.h:25
ccf::crypto::Pem priv_key
Definition identity.h:24
virtual void set_certificate(const ccf::crypto::Pem &certificate)
Definition identity.h:47
std::optional< IdentityType > type
Definition identity.h:26
bool operator==(const NetworkIdentity &other) const
Definition identity.h:29
virtual ~NetworkIdentity()
Definition identity.h:49