53 : mpProtectedHeader(nullptr),
54 mpUnprotectedHeader(nullptr),
67 : mpProtectedHeader(nullptr),
68 mpUnprotectedHeader(nullptr),
70 mpSignature(nullptr) {
71 mReadSuccess = _read(buf);
88 mReadSuccess = _read(buf);
108 return mRoot._getBytes(mpProtectedHeader);
117 return mRoot._getMap(mpUnprotectedHeader);
136 return mRoot._getBytes(mpPayload);
145 return mRoot._getBytes(mpSignature);
157 auto alg = pHeader.
getInt(COSEHeaderLabel::Algorithm);
159 return alg != 0 && signature.size() > 0;
179 mRawPayload = payload;
180 return cn_cbor_data_update(mpPayload, mRawPayload.raw(), mRawPayload.size());
191 if (alg != COSEAlgorithm::EdDSA) {
197 pHeader.
put(COSEHeaderLabel::Algorithm, alg);
198 _setProtectedHeader(pHeader);
200 auto toSign = _toBeSigned(external);
201 auto signature = signer.
sign(toSign);
202 _setSignature(signature);
214 auto alg = pHeader.
getInt(COSEHeaderLabel::Algorithm);
215 if (alg != COSEAlgorithm::EdDSA) {
220 auto toVerify = _toBeSigned();
224 case COSEAlgorithm::EdDSA:
225 return Ed25519::verify(signature.raw(), publicKey.
raw(), toVerify.raw(), toVerify.size());
237 return mRoot.build();
254 auto root = cn_cbor_array_create(mRoot._errback());
255 cn_cbor_array_append(root, mpProtectedHeader = cn_cbor_data_create(
nullptr, 0, mRoot._errback()), mRoot._errback());
256 cn_cbor_array_append(root, mpUnprotectedHeader = cn_cbor_map_create(mRoot._errback()), mRoot._errback());
257 cn_cbor_array_append(root, mpPayload = cn_cbor_data_create(
nullptr, 0, mRoot._errback()), mRoot._errback());
258 cn_cbor_array_append(root, mpSignature = cn_cbor_data_create(
nullptr, 0, mRoot._errback()), mRoot._errback());
259 mRoot.mpMapNode = cn_cbor_tag_create(COSETag::Sign1, root, mRoot._errback());
269 bool _read(
const Bytes &buf) {
272 if (mRoot.mErr.err != CN_CBOR_NO_ERROR) {
276 if (mRoot.mpMapNode->type != CN_CBOR_TAG) {
280 if (mRoot.mpMapNode->v.sint != COSETag::Sign1) {
284 auto rootArray = mRoot.mpMapNode->first_child;
289 if (rootArray->type != CN_CBOR_ARRAY) {
293 auto protectedHeader = cn_cbor_index(rootArray, 0);
294 if (!protectedHeader) {
298 if (protectedHeader->type != CN_CBOR_BYTES) {
302 auto unprotectedHeader = cn_cbor_index(rootArray, 1);
303 if (!unprotectedHeader) {
307 if (unprotectedHeader->type != CN_CBOR_MAP) {
311 auto payload = cn_cbor_index(rootArray, 2);
316 if (payload->type != CN_CBOR_BYTES) {
320 auto signature = cn_cbor_index(rootArray, 3);
325 if (signature->type != CN_CBOR_BYTES) {
330 mpProtectedHeader = protectedHeader;
331 mpUnprotectedHeader = unprotectedHeader;
333 mpSignature = signature;
343 mpProtectedHeader =
nullptr;
344 mpUnprotectedHeader =
nullptr;
346 mpSignature =
nullptr;
348 mRawProtectedHeader.clean();
350 mRawSignature.clean();
360 bool _setProtectedHeader(
const CBORObject &pHeader) {
361 mRawProtectedHeader = pHeader.build();
362 return cn_cbor_data_update(mpProtectedHeader, mRawProtectedHeader.raw(), mRawProtectedHeader.size());
372 bool _setSignature(
const Bytes &signature) {
373 mRawSignature = signature;
374 return cn_cbor_data_update(mpSignature, mRawSignature.raw(), mRawSignature.size());
383 Bytes _toBeSigned(
const Bytes &external = Bytes()) {
387 auto sigStruct = cn_cbor_array_create(mRoot._errback());
388 cn_cbor_array_append(sigStruct, cn_cbor_string_create(
"Signature1", mRoot._errback()), mRoot._errback());
389 cn_cbor_array_append(sigStruct, cn_cbor_data_create(protectedHeader.raw(), protectedHeader.size(), mRoot._errback()), mRoot._errback());
390 cn_cbor_array_append(sigStruct, cn_cbor_data_create(external.raw(), external.size(), mRoot._errback()), mRoot._errback());
391 cn_cbor_array_append(sigStruct, cn_cbor_data_create(payload.raw(), payload.size(), mRoot._errback()), mRoot._errback());
392 auto rawSigStruct = mRoot._build(sigStruct);
393 cn_cbor_free(sigStruct);
399 cn_cbor *mpProtectedHeader;
400 cn_cbor *mpUnprotectedHeader;
402 cn_cbor *mpSignature;
404 Bytes mRawProtectedHeader;
const uint8_t * raw() const
Gets a const pointer to the raw byte array.
Definition Bytes.h:235
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:303
Definition CBORObject.h:40
long getInt(int key) const
Get an integer value at a specific integer key.
Definition CBORObject.h:399
Bytes getBytes(int key) const
Get binary data at a specific integer key.
Definition CBORObject.h:461
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
void read(const Bytes &buf)
Read CBOR data from a buffer.
Definition CBORObject.h:480
bool verify(const Bytes &publicKey)
Verifies the signature of the message using the provided public key.
Definition COSEMessage.h:212
bool setPayload(const Bytes &payload)
Sets the payload of the message.
Definition COSEMessage.h:178
CBORObject getUnprotectedHeader()
Gets the unprotected header as a CBORObject.
Definition COSEMessage.h:116
Bytes getSignature()
Gets the signature of the message.
Definition COSEMessage.h:144
void operator=(COSEMessage const &)=delete
Assignment operator disabled.
bool read(const Bytes &buf)
Reads a CBOR-encoded COSE_Sign1 message.
Definition COSEMessage.h:86
bool wasReadSuccessful() const
Checks if the message was read successfully.
Definition COSEMessage.h:98
virtual ~COSEMessage()
Destructor.
Definition COSEMessage.h:75
void clean()
Resets the message to its initial empty state.
Definition COSEMessage.h:243
Bytes getProtectedHeader()
Gets the protected header as a byte string.
Definition COSEMessage.h:107
COSEMessage()
Default constructor.
Definition COSEMessage.h:52
Bytes build() const
Builds the CBOR representation of the message.
Definition COSEMessage.h:236
COSEMessage(Bytes buf)
Constructs a message from existing CBOR data.
Definition COSEMessage.h:66
Bytes getPayload()
Gets the payload of the message.
Definition COSEMessage.h:135
Bytes getUnprotectedKid()
Gets the key identifier from the unprotected header.
Definition COSEMessage.h:126
void sign(const ICOSESigner &signer, const Bytes &external=Bytes())
Signs the message using the provided signer.
Definition COSEMessage.h:189
bool isSigned()
Checks if the message has a valid signature structure.
Definition COSEMessage.h:154
void setUnprotectedKid(const Bytes &kid)
Sets the key identifier in the unprotected header.
Definition COSEMessage.h:167
COSEMessage(COSEMessage const &)=delete
Copy constructor disabled.
Interface for CBOR Object Signing and Encryption (COSE) signing operations.
Definition ICOSESigner.h:35
virtual Bytes sign(const Bytes &data) const =0
Signs the provided data using the implementation's cryptographic algorithm.
virtual COSEAlgorithm signerAlgorithm() const =0
Gets the COSE algorithm identifier used by this signer.
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
Contains all classes and functions related to the Uniot Core.