CCF
Loading...
Searching...
No Matches
pem.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/ds/json.h"
6
7#include <cstring>
8#include <exception>
9#include <memory>
10#include <span>
11#include <string_view>
12#include <vector>
13
14namespace ccf::crypto
15{
16 // Convenience class ensuring null termination of PEM-encoded certificates
17 class Pem
18 {
19 private:
20 std::string s;
21 void check_pem_format();
22
23 public:
24 Pem() = default;
25 Pem(std::string pem_string);
26 Pem(const uint8_t* data, size_t size);
27
28 explicit Pem(std::span<const uint8_t> s) : Pem(s.data(), s.size()) {}
29 explicit Pem(const std::vector<uint8_t>& v) : Pem(v.data(), v.size()) {}
30
31 inline bool operator==(const Pem& rhs) const
32 {
33 return s == rhs.s;
34 }
35
36 inline bool operator!=(const Pem& rhs) const
37 {
38 return !(*this == rhs);
39 }
40
41 inline bool operator<(const Pem& rhs) const
42 {
43 return s < rhs.s;
44 }
45
46 inline const std::string& str() const
47 {
48 return s;
49 }
50
51 inline uint8_t* data()
52 {
53 return reinterpret_cast<uint8_t*>(s.data());
54 }
55
56 inline const uint8_t* data() const
57 {
58 return reinterpret_cast<const uint8_t*>(s.data());
59 }
60
61 inline size_t size() const
62 {
63 return s.size();
64 }
65
66 inline bool empty() const
67 {
68 return s.empty();
69 }
70
71 inline std::vector<uint8_t> raw() const
72 {
73 return {data(), data() + size()};
74 }
75 };
76
77 inline void to_json(nlohmann::json& j, const Pem& p)
78 {
79 j = p.str();
80 }
81
82 inline void from_json(const nlohmann::json& j, Pem& p)
83 {
84 if (j.is_string())
85 {
86 p = Pem(j.get<std::string>());
87 }
88 else if (j.is_array())
89 {
90 p = Pem(j.get<std::vector<uint8_t>>());
91 }
92 else
93 {
94 throw std::runtime_error(
95 fmt::format("Unable to parse pem from this JSON: {}", j.dump()));
96 }
97 }
98
99 inline std::string schema_name(const Pem*)
100 {
101 return "Pem";
102 }
103
104 std::vector<ccf::crypto::Pem> split_x509_cert_bundle(
105 const std::string_view& pem);
106
107 inline void fill_json_schema(nlohmann::json& schema, const Pem*)
108 {
109 schema["type"] = "string";
110 }
111}
112
113namespace std
114{
115 template <>
116 struct hash<ccf::crypto::Pem>
117 {
118 size_t operator()(const ccf::crypto::Pem& pem) const
119 {
120 return std::hash<std::string>()(pem.str());
121 }
122 };
123}
Definition pem.h:18
size_t size() const
Definition pem.h:61
bool empty() const
Definition pem.h:66
bool operator<(const Pem &rhs) const
Definition pem.h:41
bool operator!=(const Pem &rhs) const
Definition pem.h:36
Pem(const std::vector< uint8_t > &v)
Definition pem.h:29
const std::string & str() const
Definition pem.h:46
std::vector< uint8_t > raw() const
Definition pem.h:71
Pem(std::span< const uint8_t > s)
Definition pem.h:28
bool operator==(const Pem &rhs) const
Definition pem.h:31
uint8_t * data()
Definition pem.h:51
const uint8_t * data() const
Definition pem.h:56
Definition base64.h:10
void fill_json_schema(nlohmann::json &schema, const Pem *)
Definition pem.h:107
void to_json(nlohmann::json &j, const Pem &p)
Definition pem.h:77
std::vector< ccf::crypto::Pem > split_x509_cert_bundle(const std::string_view &pem)
Definition pem.cpp:37
std::string schema_name(const Pem *)
Definition pem.h:99
void from_json(const nlohmann::json &j, Pem &p)
Definition pem.h:82
Definition app_interface.h:14
STL namespace.
size_t operator()(const ccf::crypto::Pem &pem) const
Definition pem.h:118