CCF
Loading...
Searching...
No Matches
jwk.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"
6#include "ccf/ds/json.h"
7#include "ccf/ds/logger.h"
8
9#include <string>
10
11namespace ccf::crypto
12{
13 enum class JsonWebKeyType : uint8_t
14 {
15 EC = 0,
16 RSA = 1,
17 OKP = 2
18 };
21 {{JsonWebKeyType::EC, "EC"},
22 {JsonWebKeyType::RSA, "RSA"},
23 {JsonWebKeyType::OKP, "OKP"}});
24
26 {
28 std::optional<std::string> kid = std::nullopt;
29 std::optional<std::vector<std::string>> x5c = std::nullopt;
30
31 bool operator==(const JsonWebKey&) const = default;
32 };
36
37 enum class JsonWebKeyECCurve : uint8_t
38 {
39 P256 = 0,
40 P384 = 1,
41 P521 = 2
42 };
45 {{JsonWebKeyECCurve::P256, "P-256"},
46 {JsonWebKeyECCurve::P384, "P-384"},
47 {JsonWebKeyECCurve::P521, "P-521"}});
48
50 {
52 std::optional<std::string> kid = std::nullopt;
53 std::optional<std::vector<std::string>> x5c = std::nullopt;
54 std::optional<std::string> n = std::nullopt;
55 std::optional<std::string> e = std::nullopt;
56 std::optional<std::string> x = std::nullopt;
57 std::optional<std::string> y = std::nullopt;
58 std::optional<JsonWebKeyECCurve> crv = std::nullopt;
59 std::optional<std::string> issuer = std::nullopt;
60
61 bool operator==(const JsonWebKeyData&) const = default;
62 };
66 JsonWebKeyData, kid, x5c, n, e, x, y, crv, issuer);
67
68 static JsonWebKeyECCurve curve_id_to_jwk_curve(CurveID curve_id)
69 {
70 switch (curve_id)
71 {
72 case CurveID::NONE:
74 case CurveID::X25519:
75 throw std::logic_error(
76 fmt::format("Invalid JWK EC CurveId {}", curve_id));
81 default:
82 throw std::logic_error(fmt::format("Unknown curve {}", curve_id));
83 }
84 }
85
86 static CurveID jwk_curve_to_curve_id(JsonWebKeyECCurve jwk_curve)
87 {
88 switch (jwk_curve)
89 {
91 throw std::logic_error(
92 fmt::format("Unsupported JWK curve {}", jwk_curve));
94 return CurveID::SECP384R1;
96 return CurveID::SECP256R1;
97 default:
98 throw std::logic_error(fmt::format("Unknown JWK curve {}", jwk_curve));
99 }
100 }
101
102 enum class JsonWebKeyEdDSACurve : std::uint8_t
103 {
104 ED25519 = 0,
105 X25519 = 1
106 };
109 {{JsonWebKeyEdDSACurve::ED25519, "Ed25519"},
110 {JsonWebKeyEdDSACurve::X25519, "X25519"}});
111
112 static JsonWebKeyEdDSACurve curve_id_to_jwk_eddsa_curve(CurveID curve_id)
113 {
114 switch (curve_id)
115 {
116 case CurveID::NONE:
119 throw std::logic_error(fmt::format("Invalid EdDSA curve {}", curve_id));
122 case CurveID::X25519:
124 default:
125 throw std::logic_error(fmt::format("Unknown EdDSA curve {}", curve_id));
126 }
127 }
128
130 {
132 std::string x; // base64url
133 std::string y; // base64url
134
135 bool operator==(const JsonWebKeyECPublic&) const = default;
136 };
139
141 {
142 std::string d; // base64url
143
144 bool operator==(const JsonWebKeyECPrivate&) const = default;
145 };
148
150 {
151 std::string n; // base64url
152 std::string e; // base64url
153
154 bool operator==(const JsonWebKeyRSAPublic&) const = default;
155 };
158
160 {
161 std::string d; // base64url
162 std::string p; // base64url
163 std::string q; // base64url
164 std::string dp; // base64url
165 std::string dq; // base64url
166 std::string qi; // base64url
167
168 bool operator==(const JsonWebKeyRSAPrivate&) const = default;
169 };
172
174 {
176 std::string x; // base64url
177
178 bool operator==(const JsonWebKeyEdDSAPublic&) const = default;
179 };
182
184 {
185 std::string d; // base64url
186
187 bool operator==(const JsonWebKeyEdDSAPrivate&) const = default;
188 };
191}
#define DECLARE_JSON_TYPE_WITH_BASE(TYPE, BASE)
Definition json.h:687
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:736
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:712
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:811
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:864
Definition base64.h:11
JsonWebKeyType
Definition jwk.h:14
JsonWebKeyEdDSACurve
Definition jwk.h:103
JsonWebKeyECCurve
Definition jwk.h:38
CurveID
Definition curve.h:18
@ SECP384R1
The SECP384R1 curve.
@ CURVE25519
The CURVE25519 curve.
@ SECP256R1
The SECP256R1 curve.
Definition jwk.h:50
std::optional< std::string > n
Definition jwk.h:54
std::optional< std::string > x
Definition jwk.h:56
std::optional< std::vector< std::string > > x5c
Definition jwk.h:53
std::optional< JsonWebKeyECCurve > crv
Definition jwk.h:58
std::optional< std::string > kid
Definition jwk.h:52
std::optional< std::string > issuer
Definition jwk.h:59
std::optional< std::string > e
Definition jwk.h:55
JsonWebKeyType kty
Definition jwk.h:51
std::optional< std::string > y
Definition jwk.h:57
bool operator==(const JsonWebKeyData &) const =default
bool operator==(const JsonWebKeyECPrivate &) const =default
std::string d
Definition jwk.h:142
std::string x
Definition jwk.h:132
JsonWebKeyECCurve crv
Definition jwk.h:131
std::string y
Definition jwk.h:133
bool operator==(const JsonWebKeyECPublic &) const =default
bool operator==(const JsonWebKeyEdDSAPrivate &) const =default
std::string d
Definition jwk.h:185
JsonWebKeyEdDSACurve crv
Definition jwk.h:175
std::string x
Definition jwk.h:176
bool operator==(const JsonWebKeyEdDSAPublic &) const =default
std::string q
Definition jwk.h:163
std::string qi
Definition jwk.h:166
std::string p
Definition jwk.h:162
bool operator==(const JsonWebKeyRSAPrivate &) const =default
std::string dq
Definition jwk.h:165
std::string dp
Definition jwk.h:164
std::string d
Definition jwk.h:161
bool operator==(const JsonWebKeyRSAPublic &) const =default
std::string e
Definition jwk.h:152
std::string n
Definition jwk.h:151
Definition jwk.h:26
std::optional< std::string > kid
Definition jwk.h:28
JsonWebKeyType kty
Definition jwk.h:27
bool operator==(const JsonWebKey &) const =default
std::optional< std::vector< std::string > > x5c
Definition jwk.h:29