CCF
Loading...
Searching...
No Matches
san.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#include "ccf/ds/nonstd.h"
7
8#define FMT_HEADER_ONLY
9#include <fmt/format.h>
10#include <string>
11
12namespace ccf::crypto
13{
14 static const std::string IP_ADDRESS_PREFIX = "iPAddress:";
15 static const std::string DNS_NAME_PREFIX = "dNSName:";
16
18 {
19 std::string san;
20 bool is_ip;
21
22 bool operator==(const SubjectAltName& other) const = default;
23 bool operator!=(const SubjectAltName& other) const = default;
24 };
27
28 static SubjectAltName san_from_string(const std::string& str)
29 {
30 if (str.starts_with(IP_ADDRESS_PREFIX))
31 {
32 return {str.substr(IP_ADDRESS_PREFIX.size()), true};
33 }
34 else if (str.starts_with(DNS_NAME_PREFIX))
35 {
36 return {str.substr(DNS_NAME_PREFIX.size()), false};
37 }
38 else
39 {
40 throw std::logic_error(fmt::format(
41 "SAN could not be parsed: {}, must be (iPAddress|dNSName):VALUE", str));
42 }
43 }
44
45 static std::vector<SubjectAltName> sans_from_string_list(
46 const std::vector<std::string>& list)
47 {
48 std::vector<SubjectAltName> sans = {};
49 for (const auto& l : list)
50 {
51 sans.push_back(san_from_string(l));
52 }
53 return sans;
54 }
55}
56
57FMT_BEGIN_NAMESPACE
58template <>
59struct formatter<ccf::crypto::SubjectAltName>
60{
61 template <typename ParseContext>
62 constexpr auto parse(ParseContext& ctx)
63 {
64 return ctx.begin();
65 }
66
67 template <typename FormatContext>
68 auto format(const ccf::crypto::SubjectAltName& san, FormatContext& ctx) const
69 -> decltype(ctx.out())
70 {
71 std::string prefix;
72 if (san.is_ip)
73 {
74 prefix = "IP";
75 }
76 else
77 {
78 prefix = "DNS";
79 }
80 return format_to(ctx.out(), "{}:{}", prefix, san.san);
81 }
82};
83FMT_END_NAMESPACE
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:714
#define DECLARE_JSON_TYPE(TYPE)
Definition json.h:663
Definition base64.h:10
Definition app_interface.h:14
Definition san.h:18
bool operator!=(const SubjectAltName &other) const =default
std::string san
Definition san.h:19
bool operator==(const SubjectAltName &other) const =default
bool is_ip
Definition san.h:20
auto format(const ccf::crypto::SubjectAltName &san, FormatContext &ctx) const -> decltype(ctx.out())
Definition san.h:68
constexpr auto parse(ParseContext &ctx)
Definition san.h:62