Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jose_jwk.h
1#ifndef SOURCEMETA_CORE_JOSE_JWK_H_
2#define SOURCEMETA_CORE_JOSE_JWK_H_
3
4#ifndef SOURCEMETA_CORE_JOSE_EXPORT
5#include <sourcemeta/core/jose_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/jose_algorithm.h>
10#include <sourcemeta/core/jose_error.h>
11// NOLINTEND(misc-include-cleaner)
12
13#include <sourcemeta/core/crypto.h>
14#include <sourcemeta/core/json.h>
15
16#include <cstdint> // std::uint8_t
17#include <optional> // std::optional, std::nullopt
18#include <string> // std::string
19#include <string_view> // std::string_view
20
21namespace sourcemeta::core {
22
40class SOURCEMETA_CORE_JOSE_EXPORT JWK {
41public:
43 enum class Type : std::uint8_t {
45 RSA,
49 OctetKeyPair,
51 Octet
52 };
53
55 explicit JWK(const JSON &value);
56
58 explicit JWK(JSON &&value);
59
61 JWK(JWK &&other) noexcept = default;
62 auto operator=(JWK &&other) noexcept -> JWK & = default;
63 JWK(const JWK &) = delete;
64 auto operator=(const JWK &) -> JWK & = delete;
65
68 [[nodiscard]] static auto from(const JSON &value) -> std::optional<JWK>;
69
72 [[nodiscard]] static auto from(JSON &&value) -> std::optional<JWK>;
73
87 [[nodiscard]] static auto from_octets(const std::string_view secret) -> JWK;
88
90 [[nodiscard]] auto type() const noexcept -> Type { return this->type_; }
91
93 [[nodiscard]] auto key_id() const noexcept
94 -> std::optional<std::string_view> {
95 if (this->key_id_.has_value()) {
96 return std::string_view{*this->key_id_};
97 }
98
99 return std::nullopt;
100 }
101
103 [[nodiscard]] auto algorithm() const noexcept -> std::optional<JWSAlgorithm> {
104 return this->algorithm_;
105 }
106
107 // Elliptic curve keys (RFC 7518 Section 6.2) and octet key pairs (RFC 8037
108 // Section 2) carry a curve name, which the elliptic curve algorithms pin to
109 // exactly one curve
111 [[nodiscard]] auto curve() const noexcept -> std::string_view {
112 return this->curve_;
113 }
114
115 // The parsed platform key, built once from the decoded material so that
116 // verification reuses it rather than reconstructing it per signature. It is
117 // null when the material could not be turned into a key
120 [[nodiscard]] auto public_key() const noexcept -> const PublicKey * {
121 return this->public_key_.has_value() ? &*this->public_key_ : nullptr;
122 }
123
124 // Symmetric keys carry their raw secret rather than a parsed platform key
125 // (RFC 7518 Section 6.4), and knowing the secret is required for both
126 // producing and checking an HMAC, so the public key class carries it
128 [[nodiscard]] auto secret() const noexcept -> std::string_view {
129 return this->secret_;
130 }
131
134 [[nodiscard]] auto public_jwk() const -> std::optional<JSON>;
135
138 [[nodiscard]] auto thumbprint() const -> std::optional<std::string>;
139
140private:
141 JWK() = default;
142 static auto parse(const JSON &value, JWK &result) -> bool;
143
144#if defined(_MSC_VER)
145#pragma warning(disable : 4251)
146#endif
147 Type type_{Type::RSA};
148 std::optional<std::string> key_id_;
149 std::optional<JWSAlgorithm> algorithm_;
150 std::string curve_;
151 std::optional<PublicKey> public_key_;
152 std::string secret_;
153 std::string modulus_;
154 std::string exponent_;
155 std::string coordinate_x_;
156 std::string coordinate_y_;
157 std::string public_point_;
158#if defined(_MSC_VER)
159#pragma warning(default : 4251)
160#endif
161};
162
163} // namespace sourcemeta::core
164
165#endif
Definition crypto_verify.h:62
EllipticCurve
Definition crypto_verify.h:28
Type
The family of key material a key holds.
Definition jose_jwk.h:43
@ RSA
The RSA key type.
Definition jose_jwk.h:45
auto public_jwk() const -> std::optional< JSON >
JWK(JSON &&value)
Parse a JSON Web Key from a JSON value, throwing on invalid input.
static auto from_octets(const std::string_view secret) -> JWK
static auto from(JSON &&value) -> std::optional< JWK >
auto key_id() const noexcept -> std::optional< std::string_view >
The key identifier used to select this key, if present.
Definition jose_jwk.h:93
auto secret() const noexcept -> std::string_view
The raw symmetric secret, empty for asymmetric keys.
Definition jose_jwk.h:128
auto type() const noexcept -> Type
The family of key material this key holds.
Definition jose_jwk.h:90
static auto from(const JSON &value) -> std::optional< JWK >
auto thumbprint() const -> std::optional< std::string >
auto algorithm() const noexcept -> std::optional< JWSAlgorithm >
The algorithm this key is intended for, if present.
Definition jose_jwk.h:103
JWK(JWK &&other) noexcept=default
A key exclusively owns its parsed public key, so it is move-only.
auto public_key() const noexcept -> const PublicKey *
Definition jose_jwk.h:120
JWK(const JSON &value)
Parse a JSON Web Key from a JSON value, throwing on invalid input.
auto curve() const noexcept -> std::string_view
The curve this key is pinned to, empty when it carries none.
Definition jose_jwk.h:111
JWSAlgorithm
Definition jose_algorithm.h:21
Definition json_value.h:39