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 std::optional<std::string> issuer = std::nullopt;
31
32 bool operator==(const JsonWebKey&) const = default;
33 };
37
39 {
40 P256 = 0,
41 P256K1 = 1,
42 P384 = 2,
43 P521 = 3
44 };
47 {{JsonWebKeyECCurve::P256, "P-256"},
49 "secp256k1"}, // As per
50 // https://www.rfc-editor.org/rfc/rfc8812#name-jose-and-cose-secp256k1-cur
51 {JsonWebKeyECCurve::P384, "P-384"},
52 {JsonWebKeyECCurve::P521, "P-521"}});
53
54 static JsonWebKeyECCurve curve_id_to_jwk_curve(CurveID curve_id)
55 {
56 switch (curve_id)
57 {
64 default:
65 throw std::logic_error(fmt::format("Unknown curve {}", curve_id));
66 }
67 }
68
69 static CurveID jwk_curve_to_curve_id(JsonWebKeyECCurve jwk_curve)
70 {
71 switch (jwk_curve)
72 {
74 return CurveID::SECP384R1;
76 return CurveID::SECP256R1;
78 return CurveID::SECP256K1;
79 default:
80 throw std::logic_error(fmt::format("Unknown JWK curve {}", jwk_curve));
81 }
82 }
83
85 {
86 ED25519 = 0,
87 X25519 = 1
88 };
92 {JsonWebKeyEdDSACurve::X25519, "X25519"}});
93
94 static JsonWebKeyEdDSACurve curve_id_to_jwk_eddsa_curve(CurveID curve_id)
95 {
96 switch (curve_id)
97 {
100 case CurveID::X25519:
102 default:
103 throw std::logic_error(fmt::format("Unknown EdDSA curve {}", curve_id));
104 }
105 }
106
108 {
110 std::string x; // base64url
111 std::string y; // base64url
112
113 bool operator==(const JsonWebKeyECPublic&) const = default;
114 };
117
119 {
120 std::string d; // base64url
121
122 bool operator==(const JsonWebKeyECPrivate&) const = default;
123 };
126
128 {
129 std::string n; // base64url
130 std::string e; // base64url
131
132 bool operator==(const JsonWebKeyRSAPublic&) const = default;
133 };
136
138 {
139 std::string d; // base64url
140 std::string p; // base64url
141 std::string q; // base64url
142 std::string dp; // base64url
143 std::string dq; // base64url
144 std::string qi; // base64url
145
146 bool operator==(const JsonWebKeyRSAPrivate&) const = default;
147 };
150
152 {
154 std::string x; // base64url
155
156 bool operator==(const JsonWebKeyEdDSAPublic&) const = default;
157 };
160
162 {
163 std::string d; // base64url
164
165 bool operator==(const JsonWebKeyEdDSAPrivate&) const = default;
166 };
169}
#define DECLARE_JSON_TYPE_WITH_BASE(TYPE, BASE)
Definition json.h:663
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:712
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:688
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:784
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:835
Definition base64.h:9
JsonWebKeyECCurve
Definition jwk.h:39
JsonWebKeyEdDSACurve
Definition jwk.h:85
JsonWebKeyType
Definition jwk.h:14
CurveID
Definition curve.h:18
@ SECP384R1
The SECP384R1 curve.
@ SECP256K1
The SECP256K1 curve.
@ CURVE25519
The CURVE25519 curve.
@ SECP256R1
The SECP256R1 curve.
bool operator==(const JsonWebKeyECPrivate &) const =default
std::string d
Definition jwk.h:120
std::string x
Definition jwk.h:110
JsonWebKeyECCurve crv
Definition jwk.h:109
std::string y
Definition jwk.h:111
bool operator==(const JsonWebKeyECPublic &) const =default
bool operator==(const JsonWebKeyEdDSAPrivate &) const =default
std::string d
Definition jwk.h:163
JsonWebKeyEdDSACurve crv
Definition jwk.h:153
std::string x
Definition jwk.h:154
bool operator==(const JsonWebKeyEdDSAPublic &) const =default
std::string q
Definition jwk.h:141
std::string qi
Definition jwk.h:144
std::string p
Definition jwk.h:140
bool operator==(const JsonWebKeyRSAPrivate &) const =default
std::string dq
Definition jwk.h:143
std::string dp
Definition jwk.h:142
std::string d
Definition jwk.h:139
bool operator==(const JsonWebKeyRSAPublic &) const =default
std::string e
Definition jwk.h:130
std::string n
Definition jwk.h:129
Definition jwk.h:26
std::optional< std::string > kid
Definition jwk.h:28
std::optional< std::string > issuer
Definition jwk.h:30
JsonWebKeyType kty
Definition jwk.h:27
bool operator==(const JsonWebKey &) const =default
std::optional< std::vector< std::string > > x5c
Definition jwk.h:29