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 bool operator==(const Pem& rhs) const
32 {
33 return s == rhs.s;
34 }
35
36 bool operator!=(const Pem& rhs) const
37 {
38 return !(*this == rhs);
39 }
40
41 bool operator<(const Pem& rhs) const
42 {
43 return s < rhs.s;
44 }
45
46 [[nodiscard]] const std::string& str() const
47 {
48 return s;
49 }
50
51 uint8_t* data()
52 {
53 return reinterpret_cast<uint8_t*>(s.data());
54 }
55
56 [[nodiscard]] const uint8_t* data() const
57 {
58 return reinterpret_cast<const uint8_t*>(s.data());
59 }
60
61 [[nodiscard]] size_t size() const
62 {
63 return s.size();
64 }
65
66 [[nodiscard]] bool empty() const
67 {
68 return s.empty();
69 }
70
71 [[nodiscard]] 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([[maybe_unused]] const Pem* 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(
108 nlohmann::json& schema, [[maybe_unused]] const Pem* pem)
109 {
110 schema["type"] = "string";
111 }
112}
113
114namespace std
115{
116 template <>
117 struct hash<ccf::crypto::Pem>
118 {
119 size_t operator()(const ccf::crypto::Pem& pem) const
120 {
121 return std::hash<std::string>()(pem.str());
122 }
123 };
124}
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 *pem)
Definition pem.h:107
std::string schema_name(const Pem *pem)
Definition pem.h:99
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
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:119