CCF
Loading...
Searching...
No Matches
cose_sign.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
6
7#include <openssl/ossl_typ.h>
8#include <span>
9#include <string>
10#include <t_cose/t_cose_sign1_sign.h>
11#include <unordered_map>
12
13namespace ccf::crypto
14{
15 // Standardised field: algorithm used to sign.
16 static constexpr int64_t COSE_PHEADER_KEY_ALG = 1;
17 // Standardised: hash of the signing key.
18 static constexpr int64_t COSE_PHEADER_KEY_ID = 4;
19 // Standardised: CWT claims map.
20 static constexpr int64_t COSE_PHEADER_KEY_CWT = 15;
21 // Standardised: verifiable data structure.
22 static constexpr int64_t COSE_PHEADER_KEY_VDS = 395;
23 // Standardised: issued at CWT claim. Value is **PLAIN INTEGER**, as per
24 // https://www.rfc-editor.org/rfc/rfc8392#section-2. Quote:
25 /* The "NumericDate" term in this specification has the same meaning and
26 * processing rules as the JWT "NumericDate" term defined in Section 2 of
27 * [RFC7519], except that it is represented as a CBOR numericdate (from
28 * Section 2.4.1 of [RFC7049]) instead of a JSON number. The encoding is
29 * modified so that the leading tag 1 (epoch-based date/time) MUST be
30 * omitted.
31 */
32 static constexpr int64_t COSE_PHEADER_KEY_IAT = 6;
33 // Standardised: issuer CWT claim.
34 // https://www.iana.org/assignments/cose/cose.xhtml#header-parameters
35 /* The "iss" (issuer) claim identifies the principal that issued the CWT.
36 * The "iss" value is a case-sensitive string containing a StringOrURI value.
37 */
38 static constexpr int64_t COSE_PHEADER_KEY_ISS = 1;
39 // Standardised: subject CWT claim.
40 // https://www.iana.org/assignments/cose/cose.xhtml#header-parameters
41 /* The "sub" (subject) claim identifies the principal that is the subject of
42 * the CWT. The claims in a CWT are normally statements about the subject.
43 * The "sub" value is a case-sensitive string containing a StringOrURI value.
44 */
45 static constexpr int64_t COSE_PHEADER_KEY_SUB = 2;
46 // CCF headers nested map key.
47 static const std::string COSE_PHEADER_KEY_CCF = "ccf.v1";
48 // CCF-specific: last signed TxID.
49 static const std::string COSE_PHEADER_KEY_TXID = "txid";
50 // CCF-specific: first TX in the range.
51 static const std::string COSE_PHEADER_KEY_RANGE_BEGIN = "epoch.start.txid";
52 // CCF-specific: last TX included in the range.
53 static const std::string COSE_PHEADER_KEY_RANGE_END = "epoch.end.txid";
54 // CCF-specific: last signed Merkle root hash in the range.
55 static const std::string COSE_PHEADER_KEY_EPOCH_LAST_MERKLE_ROOT =
56 "epoch.end.merkle.root";
57
59 {
60 public:
61 virtual void apply(QCBOREncodeContext* ctx) const = 0;
62 virtual size_t estimated_size() const = 0;
63
64 virtual ~COSEMapKey() = default;
65 };
66
68 {
69 public:
70 COSEMapIntKey(int64_t key_);
71 ~COSEMapIntKey() override = default;
72
73 void apply(QCBOREncodeContext* ctx) const override;
74 size_t estimated_size() const override;
75
76 private:
77 int64_t key;
78 };
79
81 {
82 public:
83 COSEMapStringKey(std::string key_);
84 ~COSEMapStringKey() override = default;
85
86 void apply(QCBOREncodeContext* ctx) const override;
87 size_t estimated_size() const override;
88
89 private:
90 std::string key;
91 };
92
94 {
95 public:
96 virtual void apply(QCBOREncodeContext* ctx) const = 0;
97 virtual size_t estimated_size() const = 0;
98
99 virtual ~COSEParametersFactory() = default;
100 };
101
103 {
104 public:
106 std::shared_ptr<COSEMapKey> key_,
107 const std::vector<std::shared_ptr<COSEParametersFactory>>& factories_);
108
109 void apply(QCBOREncodeContext* ctx) const override;
110 size_t estimated_size() const override;
111
112 virtual ~COSEParametersMap() = default;
113
114 private:
115 std::shared_ptr<COSEMapKey> key;
116 std::vector<std::shared_ptr<COSEParametersFactory>> factories{};
117 };
118
119 std::shared_ptr<COSEParametersFactory> cose_params_int_int(
120 int64_t key, int64_t value);
121
122 std::shared_ptr<COSEParametersFactory> cose_params_int_string(
123 int64_t key, const std::string& value);
124
125 std::shared_ptr<COSEParametersFactory> cose_params_string_int(
126 const std::string& key, int64_t value);
127
128 std::shared_ptr<COSEParametersFactory> cose_params_string_string(
129 const std::string& key, const std::string& value);
130
131 std::shared_ptr<COSEParametersFactory> cose_params_int_bytes(
132 int64_t key, std::span<const uint8_t> value);
133
134 std::shared_ptr<COSEParametersFactory> cose_params_string_bytes(
135 const std::string& key, std::span<const uint8_t> value);
136
138 {
139 public:
140 template <typename Callable>
141 COSEParametersPair(Callable&& impl, size_t args_size) :
142 impl(std::forward<Callable>(impl)),
143 args_size{args_size}
144 {}
145
146 virtual ~COSEParametersPair() = default;
147
148 void apply(QCBOREncodeContext* ctx) const override
149 {
150 impl(ctx);
151 }
152
153 size_t estimated_size() const override
154 {
155 return args_size;
156 }
157
158 private:
159 std::function<void(QCBOREncodeContext*)> impl{};
160 size_t args_size{};
161 };
162
164 std::vector<std::shared_ptr<ccf::crypto::COSEParametersFactory>>;
165
166 struct COSESignError : public std::runtime_error
167 {
168 COSESignError(const std::string& msg) : std::runtime_error(msg) {}
169 };
170
171 std::optional<int> key_to_cose_alg_id(
173
174 /* Sign a cose_sign1 payload with custom protected headers as strings, where
175 - key: integer label to be assigned in a COSE value
176 - value: string behind the label.
177
178 Labels have to be unique. For standardised labels list check
179 https://www.iana.org/assignments/cose/cose.xhtml#header-parameters.
180 */
181 std::vector<uint8_t> cose_sign1(
182 const KeyPair_OpenSSL& key,
183 const std::vector<std::shared_ptr<COSEParametersFactory>>&
184 protected_headers,
185 std::span<const uint8_t> payload,
186 bool detached_payload = true);
187}
Definition cose_sign.h:68
size_t estimated_size() const override
Definition cose_sign.cpp:106
void apply(QCBOREncodeContext *ctx) const override
Definition cose_sign.cpp:101
~COSEMapIntKey() override=default
Definition cose_sign.h:59
virtual ~COSEMapKey()=default
virtual void apply(QCBOREncodeContext *ctx) const =0
virtual size_t estimated_size() const =0
Definition cose_sign.h:81
void apply(QCBOREncodeContext *ctx) const override
Definition cose_sign.cpp:113
~COSEMapStringKey() override=default
size_t estimated_size() const override
Definition cose_sign.cpp:118
Definition cose_sign.h:94
virtual ~COSEParametersFactory()=default
virtual size_t estimated_size() const =0
virtual void apply(QCBOREncodeContext *ctx) const =0
Definition cose_sign.h:103
size_t estimated_size() const override
Definition cose_sign.cpp:141
virtual ~COSEParametersMap()=default
void apply(QCBOREncodeContext *ctx) const override
Definition cose_sign.cpp:130
Definition cose_sign.h:138
COSEParametersPair(Callable &&impl, size_t args_size)
Definition cose_sign.h:141
void apply(QCBOREncodeContext *ctx) const override
Definition cose_sign.h:148
size_t estimated_size() const override
Definition cose_sign.h:153
virtual ~COSEParametersPair()=default
Definition public_key.h:16
Definition base64.h:10
std::shared_ptr< COSEParametersFactory > cose_params_string_bytes(const std::string &key, std::span< const uint8_t > value)
Definition cose_sign.cpp:217
std::shared_ptr< COSEParametersFactory > cose_params_int_int(int64_t key, int64_t value)
Definition cose_sign.cpp:154
std::shared_ptr< COSEParametersFactory > cose_params_int_bytes(int64_t key, std::span< const uint8_t > value)
Definition cose_sign.cpp:204
std::shared_ptr< COSEParametersFactory > cose_params_int_string(int64_t key, const std::string &value)
Definition cose_sign.cpp:166
std::optional< int > key_to_cose_alg_id(const ccf::crypto::PublicKey_OpenSSL &key)
Definition cose_sign.cpp:84
std::shared_ptr< COSEParametersFactory > cose_params_string_int(const std::string &key, int64_t value)
Definition cose_sign.cpp:178
std::shared_ptr< COSEParametersFactory > cose_params_string_string(const std::string &key, const std::string &value)
Definition cose_sign.cpp:191
std::vector< uint8_t > cose_sign1(const KeyPair_OpenSSL &key, const std::vector< std::shared_ptr< COSEParametersFactory > > &protected_headers, std::span< const uint8_t > payload, bool detached_payload)
Definition cose_sign.cpp:231
std::vector< std::shared_ptr< ccf::crypto::COSEParametersFactory > > COSEHeadersArray
Definition cose_sign.h:164
STL namespace.
Definition cose_sign.h:167
COSESignError(const std::string &msg)
Definition cose_sign.h:168