HCOSE_MAC0 COSE_Mac0_Init(CBOR_CONTEXT_COMMA cose_errback * perr) { COSE_Mac0Message * pobj = (COSE_Mac0Message *)COSE_CALLOC(1, sizeof(COSE_Mac0Message), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init(&pobj->m_message, COSE_mac0_object, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } return (HCOSE_MAC0)pobj; errorReturn: if (pobj != NULL) COSE_Mac0_Free((HCOSE_MAC0)pobj); return NULL; }
HCOSE_ENCRYPT COSE_Encrypt_Init(CBOR_CONTEXT_COMMA cose_errback * perror) { COSE_Encrypt * pobj = (COSE_Encrypt *)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); if (pobj == NULL) { if (perror != NULL) perror->err = COSE_ERR_OUT_OF_MEMORY; return NULL; } if (!_COSE_Init(&pobj->m_message, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA perror)) { COSE_Encrypt_Free((HCOSE_ENCRYPT)pobj); return NULL; } _COSE_InsertInList(&EncryptRoot, &pobj->m_message); return (HCOSE_ENCRYPT) pobj; }
/*! * @brief Allocate and initialize an object for creation of an Enveloped message object * * Allocate and initialize the object used to create a COSE Enveloped message object. * Supported flags are: * COSE_INIT_FLAG_DETACHED_CONTENT - content is not part of the message * COSE_INIT_NO_CBOR_FLAG - Do not emit the leading CBOR tag on the message. * * See the notes on the memory model for the use of the context variable. * Applications need to free the returned handle before deallocating the * memory block that it was wrapped in for correct handle checking. * * @param flags Set of initialization flags from the COSE_INIT_FLAGS enum * @param context CN_CBOR context allocator struture * @param perr Location to return error specific information * @returns handle to the newly allocated object */ HCOSE_ENVELOPED COSE_Enveloped_Init(COSE_INIT_FLAGS flags, CBOR_CONTEXT_COMMA cose_errback * perr) { COSE_Enveloped * pobj = (COSE_Enveloped *)COSE_CALLOC(1, sizeof(COSE_Enveloped), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init(flags,&pobj->m_message, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA perr)) { _COSE_Enveloped_Release(pobj); COSE_FREE(pobj, context); return NULL; } _COSE_InsertInList(&EnvelopedRoot, &pobj->m_message); return (HCOSE_ENVELOPED) pobj; errorReturn: return NULL; }
HCOSE_ENCRYPT COSE_Encrypt_Init(COSE_INIT_FLAGS flags, CBOR_CONTEXT_COMMA cose_errback * perr) { CHECK_CONDITION(flags == COSE_INIT_FLAGS_NONE, COSE_ERR_INVALID_PARAMETER); COSE_Encrypt * pobj = (COSE_Encrypt *)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init(flags, &pobj->m_message, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA perr)) { _COSE_Encrypt_Release(pobj); COSE_FREE(pobj, context); return NULL; } _COSE_InsertInList(&EncryptRoot, &pobj->m_message); return (HCOSE_ENCRYPT) pobj; errorReturn: return NULL; }
HCOSE_RECIPIENT COSE_Mac_add_shared_secret(HCOSE_MAC hcose, COSE_Algorithms alg, byte * rgbKey, int cbKey, byte * rgbKid, int cbKid, cose_errback * perr) { #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif // USE_CBOR_CONTEXT COSE_RecipientInfo * pobj; COSE_MacMessage * pcose = (COSE_MacMessage *)hcose; cn_cbor * cn_Temp = NULL; cn_cbor * pRecipients = NULL; cn_cbor * pRecipientsNew = NULL; byte * pbKey = NULL; byte * pbTemp = NULL; cn_cbor * cnTemp = NULL; cn_cbor_errback cbor_error; CHECK_CONDITION(IsValidMacHandle(hcose) && (rgbKey != NULL), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif // USE_CBOR_CONTEXT switch (alg) { case COSE_Algorithm_Direct: break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } pobj = (COSE_RecipientInfo *)COSE_CALLOC(1, sizeof(COSE_RecipientInfo), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init(&pobj->m_encrypt.m_message, COSE_unknown_object, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } cn_Temp = cn_cbor_int_create(alg, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cn_Temp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_mapput_int(pobj->m_encrypt.m_message.m_unprotectMap, COSE_Header_Algorithm, cn_Temp, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); cn_Temp = NULL; if (cbKid > 0) { pbTemp = (byte *)COSE_CALLOC(cbKid, 1, context); CHECK_CONDITION(pbTemp != NULL, COSE_ERR_OUT_OF_MEMORY); memcpy(pbTemp, rgbKid, cbKid); cnTemp = cn_cbor_data_create(pbTemp, cbKid, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cnTemp != NULL, cbor_error); pbTemp = NULL; CHECK_CONDITION_CBOR(cn_cbor_mapput_int(pobj->m_encrypt.m_message.m_unprotectMap, COSE_Header_KID, cnTemp, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); } pobj->m_encrypt.pbKey = pbKey = (byte *)COSE_CALLOC(cbKey, 1, context); CHECK_CONDITION(pobj->m_encrypt.pbKey != NULL, COSE_ERR_OUT_OF_MEMORY); memcpy(pbKey, rgbKey, cbKey); pobj->m_encrypt.cbKey = cbKey; pobj->m_recipientNext = pcose->m_recipientFirst; pcose->m_recipientFirst = pobj; pRecipients = _COSE_arrayget_int(&pcose->m_message, INDEX_MAC_RECIPIENTS); if (pRecipients == NULL) { pRecipients = pRecipientsNew = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(pRecipients != NULL, cbor_error); pRecipientsNew = NULL; CHECK_CONDITION_CBOR(_COSE_array_replace(&pcose->m_message, pRecipients, INDEX_MAC_RECIPIENTS, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); } CHECK_CONDITION_CBOR(cn_cbor_array_append(pRecipients, pobj->m_encrypt.m_message.m_cbor, &cbor_error), cbor_error); pobj->m_encrypt.m_message.m_flags |= 1; return (HCOSE_RECIPIENT)pobj; errorReturn: if (cn_Temp != NULL) CN_CBOR_FREE(cn_Temp, context); if (pRecipientsNew != NULL) CN_CBOR_FREE(pRecipientsNew, context); // if (pobj != NULL) COSE_Recipient_Free(pobj); return NULL; }