7#include <qcbor/qcbor.h>
8#include <qcbor/qcbor_spiffy_decode.h>
11#include <t_cose/t_cose_common.h>
17 static constexpr int64_t PARAM_ALG = 1;
18 static constexpr int64_t PARAM_CONTENT_TYPE = 3;
19 static constexpr int64_t PARAM_KID = 4;
20 static constexpr int64_t PARAM_X5CHAIN = 33;
22 static constexpr auto CONTENT_TYPE_APPLICATION_JSON_VALUE =
28 static std::string qcbor_buf_to_string(
const UsefulBufC& buf)
30 return std::string(
reinterpret_cast<const char*
>(buf.ptr), buf.len);
33 static std::vector<uint8_t> qcbor_buf_to_byte_vector(
const UsefulBufC& buf)
35 auto ptr =
static_cast<const uint8_t*
>(buf.ptr);
36 return {ptr, ptr + buf.len};
39 static bool is_ecdsa_alg(int64_t cose_alg)
41 return cose_alg == T_COSE_ALGORITHM_ES256 ||
42 cose_alg == T_COSE_ALGORITHM_ES384 || cose_alg == T_COSE_ALGORITHM_ES512;
45 static bool is_rsa_alg(int64_t cose_alg)
47 return cose_alg == T_COSE_ALGORITHM_PS256 ||
48 cose_alg == T_COSE_ALGORITHM_PS384 || cose_alg == T_COSE_ALGORITHM_PS512;
59 std::runtime_error(msg)
63 static std::string tstring_to_string(QCBORItem& item)
66 static_cast<const char*
>(item.val.string.ptr),
67 static_cast<const char*
>(item.val.string.ptr) + item.val.string.len};
70 static std::pair<std::string , std::string >
71 extract_iss_sub_from_sig(
const std::vector<uint8_t>& cose_sign1)
73 QCBORError qcbor_result;
74 QCBORDecodeContext ctx;
76 QCBORDecode_Init(&ctx, buf, QCBOR_DECODE_MODE_NORMAL);
78 QCBORDecode_EnterArray(&ctx,
nullptr);
79 qcbor_result = QCBORDecode_GetError(&ctx);
80 if (qcbor_result != QCBOR_SUCCESS)
82 throw COSEDecodeError(
"Failed to parse COSE_Sign1 outer array");
85 uint64_t tag = QCBORDecode_GetNthTagOfLast(&ctx, 0);
86 if (tag != CBOR_TAG_COSE_SIGN1)
88 throw COSEDecodeError(
"COSE_Sign1 is not tagged");
91 QCBORDecode_EnterBstrWrapped(&ctx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
92 QCBORDecode_EnterMap(&ctx, NULL);
99 QCBORItem header_items[END_INDEX + 1];
101 header_items[CWT_CLAIMS_INDEX].label.int64 = crypto::COSE_PHEADER_KEY_CWT;
102 header_items[CWT_CLAIMS_INDEX].uLabelType = QCBOR_TYPE_INT64;
103 header_items[CWT_CLAIMS_INDEX].uDataType = QCBOR_TYPE_MAP;
105 header_items[END_INDEX].uLabelType = QCBOR_TYPE_NONE;
107 QCBORDecode_GetItemsInMap(&ctx, header_items);
109 qcbor_result = QCBORDecode_GetError(&ctx);
110 if (qcbor_result != QCBOR_SUCCESS)
112 throw COSEDecodeError(
113 fmt::format(
"Failed to decode protected header: {}", qcbor_result));
116 if (header_items[CWT_CLAIMS_INDEX].uDataType == QCBOR_TYPE_NONE)
118 throw COSEDecodeError(
"Missing CWT claims in COSE_Sign1");
121 QCBORDecode_EnterMapFromMapN(&ctx, crypto::COSE_PHEADER_KEY_CWT);
122 auto decode_error = QCBORDecode_GetError(&ctx);
123 if (decode_error != QCBOR_SUCCESS)
125 throw COSEDecodeError(
126 fmt::format(
"Failed to decode CWT claims: {}", decode_error));
135 QCBORItem cwt_items[CWT_END_INDEX + 1];
137 cwt_items[CWT_ISS_INDEX].label.int64 = crypto::COSE_PHEADER_KEY_ISS;
138 cwt_items[CWT_ISS_INDEX].uLabelType = QCBOR_TYPE_INT64;
139 cwt_items[CWT_ISS_INDEX].uDataType = QCBOR_TYPE_TEXT_STRING;
141 cwt_items[CWT_SUB_INDEX].label.int64 = crypto::COSE_PHEADER_KEY_SUB;
142 cwt_items[CWT_SUB_INDEX].uLabelType = QCBOR_TYPE_INT64;
143 cwt_items[CWT_SUB_INDEX].uDataType = QCBOR_TYPE_TEXT_STRING;
145 cwt_items[CWT_END_INDEX].uLabelType = QCBOR_TYPE_NONE;
147 QCBORDecode_GetItemsInMap(&ctx, cwt_items);
148 decode_error = QCBORDecode_GetError(&ctx);
149 if (decode_error != QCBOR_SUCCESS)
151 throw COSEDecodeError(
152 fmt::format(
"Failed to decode CWT claim contents: {}", decode_error));
156 cwt_items[CWT_ISS_INDEX].uDataType != QCBOR_TYPE_NONE &&
157 cwt_items[CWT_SUB_INDEX].uDataType != QCBOR_TYPE_NONE)
159 auto issuer = tstring_to_string(cwt_items[CWT_ISS_INDEX]);
160 auto subject = tstring_to_string(cwt_items[CWT_SUB_INDEX]);
161 return {issuer, subject};
165 throw COSEDecodeError(
166 "Missing issuer and subject values in CWT Claims in COSE_Sign1");
Definition cose_signatures_config_interface.h:12
std::span< const uint8_t > Signature
Definition cose_common.h:26
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
Definition cose_common.h:52
COSEDecodeError(const std::string &msg)
Definition cose_common.h:53
Definition cose_common.h:57
COSESignatureValidationError(const std::string &msg)
Definition cose_common.h:58