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: verifiable data structure
20 static constexpr int64_t COSE_PHEADER_KEY_VDS = 395;
21 // CCF-specific: last signed TxID
22 static constexpr const char* COSE_PHEADER_KEY_TXID = "ccf.txid";
23
25 {
26 public:
27 template <typename Callable>
28 COSEParametersFactory(Callable&& impl, size_t args_size) :
29 impl(std::forward<Callable>(impl)),
30 args_size{args_size}
31 {}
32
33 void apply(QCBOREncodeContext* ctx) const
34 {
35 impl(ctx);
36 }
37
38 size_t estimated_size() const
39 {
40 return args_size;
41 }
42
43 private:
44 std::function<void(QCBOREncodeContext*)> impl{};
45 size_t args_size{};
46 };
47
48 COSEParametersFactory cose_params_int_int(int64_t key, int64_t value);
49
50 COSEParametersFactory cose_params_int_string(
51 int64_t key, const std::string& value);
52
53 COSEParametersFactory cose_params_string_int(
54 const std::string& key, int64_t value);
55
56 COSEParametersFactory cose_params_string_string(
57 const std::string& key, const std::string& value);
58
59 COSEParametersFactory cose_params_int_bytes(
60 int64_t key, const std::vector<uint8_t>& value);
61
62 struct COSESignError : public std::runtime_error
63 {
64 COSESignError(const std::string& msg) : std::runtime_error(msg) {}
65 };
66
68
69 /* Sign a cose_sign1 payload with custom protected headers as strings, where
70 - key: integer label to be assigned in a COSE value
71 - value: string behind the label.
72
73 Labels have to be unique. For standardised labels list check
74 https://www.iana.org/assignments/cose/cose.xhtml#header-parameters.
75 */
76 std::vector<uint8_t> cose_sign1(
77 KeyPair_OpenSSL& key,
78 const std::vector<COSEParametersFactory>& protected_headers,
79 std::span<const uint8_t> payload);
80}
Definition cose_sign.h:25
COSEParametersFactory(Callable &&impl, size_t args_size)
Definition cose_sign.h:28
size_t estimated_size() const
Definition cose_sign.h:38
void apply(QCBOREncodeContext *ctx) const
Definition cose_sign.h:33
Definition public_key.h:16
Definition base64.h:9
std::vector< uint8_t > cose_sign1(KeyPair_OpenSSL &key, const std::vector< COSEParametersFactory > &protected_headers, std::span< const uint8_t > payload)
Definition cose_sign.cpp:161
COSEParametersFactory cose_params_string_string(const std::string &key, const std::string &value)
Definition cose_sign.cpp:135
COSEParametersFactory cose_params_int_bytes(int64_t key, const std::vector< uint8_t > &value)
Definition cose_sign.cpp:148
std::optional< int > key_to_cose_alg_id(ccf::crypto::PublicKey_OpenSSL &key)
Definition cose_sign.cpp:85
COSEParametersFactory cose_params_int_int(int64_t key, int64_t value)
Definition cose_sign.cpp:99
COSEParametersFactory cose_params_int_string(int64_t key, const std::string &value)
Definition cose_sign.cpp:110
COSEParametersFactory cose_params_string_int(const std::string &key, int64_t value)
Definition cose_sign.cpp:122
STL namespace.
Definition cose_sign.h:63
COSESignError(const std::string &msg)
Definition cose_sign.h:64