LS_API bool tube_data(tube *t, uint8_t *data, size_t len, ls_err *err) { // max size for CBOR preamble 19 bytes: // 1(map|27) 8(length) 1(key:0) 1(bstr|27) 8(length) //uint8_t preamble[19]; cn_cbor *map; cn_cbor *cdata; bool ret = false; cn_cbor_context ctx; assert(t); if (len == 0) { return tube_send(t, SPUD_DATA, false, false, NULL, 0, 0, err); } if (!_map_create(&ctx, &map, err)) { return false; } // TODO: the whole point of the iov system is so that we don't have to copy // the data here. Which we just did. Please fix. if (!(cdata = cn_cbor_data_create(data, len, &ctx, NULL)) || !cn_cbor_mapput_int(map, 0, cdata, &ctx, NULL)) { LS_ERROR(err, LS_ERR_NO_MEMORY); goto cleanup; } ret = tube_send_cbor(t, SPUD_DATA, false, false, map, err); cleanup: ls_pool_destroy((ls_pool*)ctx.context); return ret; }
bool _COSE_map_put(COSE * pCose, int key, cn_cbor * value, int flags, cose_errback * perr) { #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pCose->m_allocContext; #endif cn_cbor_errback error; bool f; if ((flags & COSE_BOTH) == COSE_BOTH) { if (perr != NULL) perr->err = COSE_ERR_INVALID_PARAMETER; errorReturn: return false; } if (perr != NULL) perr->err = COSE_ERR_NONE; switch (flags) { case COSE_PROTECT_ONLY: f = cn_cbor_mapput_int(pCose->m_protectedMap, key, value, CBOR_CONTEXT_PARAM_COMMA &error); break; case COSE_UNPROTECT_ONLY: f = cn_cbor_mapput_int(pCose->m_unprotectMap, key, value, CBOR_CONTEXT_PARAM_COMMA &error); break; case COSE_DONT_SEND: if (pCose->m_dontSendMap == NULL) { pCose->m_dontSendMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA &error); CHECK_CONDITION(pCose->m_dontSendMap != NULL, COSE_ERR_OUT_OF_MEMORY); } f = cn_cbor_mapput_int(pCose->m_dontSendMap, key, value, CBOR_CONTEXT_PARAM_COMMA &error); break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); break; } CHECK_CONDITION(f, _MapFromCBOR(error)); return f; }
cn_cbor * BuildKey(const cn_cbor * pKeyIn, bool fPublicKey) { cn_cbor * pKeyOut = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA NULL); cn_cbor * pKty = cn_cbor_mapget_string(pKeyIn, "kty"); cn_cbor * p; cn_cbor * pKey; cn_cbor * pValue; int i; int kty; unsigned char * pb; size_t cb; if (pKeyOut == NULL) return NULL; if ((pKty == NULL) || (pKty->type != CN_CBOR_TEXT)) return NULL; if (pKty->length == 2) { if (strncmp(pKty->v.str, "EC", 2) == 0) kty = 2; else return NULL; } else if (pKty->length == 3) { if (strncmp(pKty->v.str, "oct", 3) == 0) kty = 4; else return NULL; } else return NULL; p = cn_cbor_int_create(kty, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, 1, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; for (pKey = pKeyIn->first_child; pKey != NULL; pKey = pKey->next->next) { pValue = pKey->next; if (pKey->type == CN_CBOR_TEXT) { for (i = 0; i < 7; i++) { if ((pKey->length == strlen(RgStringKeys[i].szKey)) && (strncmp(pKey->v.str, RgStringKeys[i].szKey, strlen(RgStringKeys[i].szKey)) == 0) && ((RgStringKeys[i].kty == 0) || (RgStringKeys[i].kty == kty))) { switch (RgStringKeys[i].operation) { case OPERATION_NONE: p = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; case OPERATION_BASE64: if ((strcmp(pKey->v.str, "d") == 0) && fPublicKey) continue; pb = base64_decode(pValue->v.str, pValue->length, &cb); p = cn_cbor_data_create(pb, (int)cb, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; case OPERATION_STRING: p = cn_cbor_int_create(MapName(pValue, RgCurveNames, _countof(RgCurveNames)), CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; } i = 99; } } } } return pKeyOut; }
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; }