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
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
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 {
76 default:
77 throw std::logic_error(fmt::format("Unknown curve {}", curve_id));
78 }
79 }
80
81 static CurveID jwk_curve_to_curve_id(JsonWebKeyECCurve jwk_curve)
82 {
83 switch (jwk_curve)
84 {
86 return CurveID::SECP384R1;
88 return CurveID::SECP256R1;
89 default:
90 throw std::logic_error(fmt::format("Unknown JWK curve {}", jwk_curve));
91 }
92 }
93
95 {
96 ED25519 = 0,
97 X25519 = 1
98 };
101 {{JsonWebKeyEdDSACurve::ED25519, "Ed25519"},
102 {JsonWebKeyEdDSACurve::X25519, "X25519"}});
103
104 static JsonWebKeyEdDSACurve curve_id_to_jwk_eddsa_curve(CurveID curve_id)
105 {
106 switch (curve_id)
107 {
110 case CurveID::X25519:
112 default:
113 throw std::logic_error(fmt::format("Unknown EdDSA curve {}", curve_id));
114 }
115 }
116
118 {
120 std::string x; // base64url
121 std::string y; // base64url
122
123 bool operator==(const JsonWebKeyECPublic&) const = default;
124 };
127
129 {
130 std::string d; // base64url
131
132 bool operator==(const JsonWebKeyECPrivate&) const = default;
133 };
136
138 {
139 std::string n; // base64url
140 std::string e; // base64url
141
142 bool operator==(const JsonWebKeyRSAPublic&) const = default;
143 };
146
148 {
149 std::string d; // base64url
150 std::string p; // base64url
151 std::string q; // base64url
152 std::string dp; // base64url
153 std::string dq; // base64url
154 std::string qi; // base64url
155
156 bool operator==(const JsonWebKeyRSAPrivate&) const = default;
157 };
160
162 {
164 std::string x; // base64url
165
166 bool operator==(const JsonWebKeyEdDSAPublic&) const = default;
167 };
170
172 {
173 std::string d; // base64url
174
175 bool operator==(const JsonWebKeyEdDSAPrivate&) const = default;
176 };
179}
#define DECLARE_JSON_TYPE_WITH_BASE(TYPE, BASE)
Definition json.h:665
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:714
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:690
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:786
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:837
Definition base64.h:10
JsonWebKeyECCurve
Definition jwk.h:38
JsonWebKeyEdDSACurve
Definition jwk.h:95
JsonWebKeyType
Definition jwk.h:14
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:130
std::string x
Definition jwk.h:120
JsonWebKeyECCurve crv
Definition jwk.h:119
std::string y
Definition jwk.h:121
bool operator==(const JsonWebKeyECPublic &) const =default
bool operator==(const JsonWebKeyEdDSAPrivate &) const =default
std::string d
Definition jwk.h:173
JsonWebKeyEdDSACurve crv
Definition jwk.h:163
std::string x
Definition jwk.h:164
bool operator==(const JsonWebKeyEdDSAPublic &) const =default
std::string q
Definition jwk.h:151
std::string qi
Definition jwk.h:154
std::string p
Definition jwk.h:150
bool operator==(const JsonWebKeyRSAPrivate &) const =default
std::string dq
Definition jwk.h:153
std::string dp
Definition jwk.h:152
std::string d
Definition jwk.h:149
bool operator==(const JsonWebKeyRSAPublic &) const =default
std::string e
Definition jwk.h:140
std::string n
Definition jwk.h:139
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