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