Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jose_jwk_private.h
1#ifndef SOURCEMETA_CORE_JOSE_JWK_PRIVATE_H_
2#define SOURCEMETA_CORE_JOSE_JWK_PRIVATE_H_
3
4#ifndef SOURCEMETA_CORE_JOSE_EXPORT
5#include <sourcemeta/core/jose_export.h>
6#endif
7
8#include <sourcemeta/core/jose_algorithm.h>
9
10#include <sourcemeta/core/crypto.h>
11#include <sourcemeta/core/json.h>
12
13#include <cstdint> // std::uint8_t
14#include <optional> // std::optional, std::nullopt
15#include <string> // std::string
16#include <string_view> // std::string_view
17
18namespace sourcemeta::core {
19
35class SOURCEMETA_CORE_JOSE_EXPORT JWKPrivate {
36public:
38 enum class Type : std::uint8_t {
40 RSA,
44 OctetKeyPair,
46 Octet
47 };
48
50 JWKPrivate(JWKPrivate &&other) noexcept = default;
51 auto operator=(JWKPrivate &&other) noexcept -> JWKPrivate & = default;
52 JWKPrivate(const JWKPrivate &) = delete;
53 auto operator=(const JWKPrivate &) -> JWKPrivate & = delete;
54 ~JWKPrivate() = default;
55
59 [[nodiscard]] static auto from(const JSON &value)
60 -> std::optional<JWKPrivate>;
61
64 [[nodiscard]] static auto from(JSON &&value) -> std::optional<JWKPrivate>;
65
69 [[nodiscard]] static auto from_pem(const std::string_view pem)
70 -> std::optional<JWKPrivate>;
71
86 [[nodiscard]] static auto from_octets(const std::string_view secret)
87 -> JWKPrivate;
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. A key parsed from a PEM document carries it once its
110 // public part has been recovered, and it stays empty for a key that carries
111 // no curve, such as an RSA or symmetric key
113 [[nodiscard]] auto curve() const noexcept -> std::string_view {
114 return this->curve_;
115 }
116
117 // The parsed platform key, built once from the decoded material so that
118 // signing reuses it rather than reconstructing it per signature. It is null
119 // when the material could not be turned into a key
121 [[nodiscard]] auto private_key() const noexcept -> const PrivateKey * {
122 return this->private_key_.has_value() ? &*this->private_key_ : nullptr;
123 }
124
125 // Symmetric keys carry their raw secret rather than a parsed platform key
126 // (RFC 7518 Section 6.4), and knowing the secret is required for both
127 // producing and checking an HMAC, so the private key class carries it
129 [[nodiscard]] auto secret() const noexcept -> std::string_view {
130 return this->secret_;
131 }
132
136 [[nodiscard]] auto public_jwk() const -> std::optional<JSON>;
137
141 [[nodiscard]] auto thumbprint() const -> std::optional<std::string>;
142
143private:
144 JWKPrivate() = default;
145 static auto parse(const JSON &value, JWKPrivate &result) -> bool;
146
147#if defined(_MSC_VER)
148#pragma warning(disable : 4251)
149#endif
150 Type type_{Type::RSA};
151 std::optional<std::string> key_id_;
152 std::optional<JWSAlgorithm> algorithm_;
153 std::string curve_;
154 std::optional<PrivateKey> private_key_;
155 std::string secret_;
156 std::string modulus_;
157 std::string exponent_;
158 std::string coordinate_x_;
159 std::string coordinate_y_;
160 std::string public_point_;
161#if defined(_MSC_VER)
162#pragma warning(default : 4251)
163#endif
164};
165
166} // namespace sourcemeta::core
167
168#endif
Definition crypto_sign.h:33
EllipticCurve
Definition crypto_verify.h:28
JWKPrivate(JWKPrivate &&other) noexcept=default
A key exclusively owns its parsed private key, so it is move-only.
auto key_id() const noexcept -> std::optional< std::string_view >
The key identifier used to select this key, if present.
Definition jose_jwk_private.h:93
static auto from_pem(const std::string_view pem) -> std::optional< JWKPrivate >
static auto from(const JSON &value) -> std::optional< JWKPrivate >
static auto from_octets(const std::string_view secret) -> JWKPrivate
auto secret() const noexcept -> std::string_view
The raw symmetric secret, empty for asymmetric keys.
Definition jose_jwk_private.h:129
auto private_key() const noexcept -> const PrivateKey *
The parsed private key, or null when the material could not be decoded.
Definition jose_jwk_private.h:121
Type
The family of key material a key holds.
Definition jose_jwk_private.h:38
@ RSA
The RSA key type.
Definition jose_jwk_private.h:40
auto public_jwk() const -> std::optional< JSON >
auto type() const noexcept -> Type
The family of key material this key holds.
Definition jose_jwk_private.h:90
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_private.h:103
auto curve() const noexcept -> std::string_view
The curve this key is pinned to, empty when it carries none.
Definition jose_jwk_private.h:113
static auto from(JSON &&value) -> std::optional< JWKPrivate >
JWSAlgorithm
Definition jose_algorithm.h:21
Definition json_value.h:39