byte * GetCBOREncoding(const cn_cbor * pControl, int * pcbEncoded) { const cn_cbor * pOutputs = cn_cbor_mapget_string(pControl, "output"); const cn_cbor * pCBOR; byte * pb = NULL; const byte * pb2; int i; if ((pOutputs == NULL) || (pOutputs->type != CN_CBOR_MAP)) { fprintf(stderr, "Invalid output\n"); exit(1); } pCBOR = cn_cbor_mapget_string(pOutputs, "cbor"); if ((pCBOR == NULL) || (pCBOR->type != CN_CBOR_TEXT)) { fprintf(stderr, "Invalid cbor object"); exit(1); } pb = malloc(pCBOR->length / 2); pb2 = pCBOR->v.bytes; for (i = 0; i < pCBOR->length; i += 2) { pb[i / 2] = fromHex(pb2[i]) * 16 + fromHex(pb2[i + 1]); } *pcbEncoded = (int) (pCBOR->length / 2); return pb; }
bool SetSendingAttributes(HCOSE hMsg, const cn_cbor * pIn, int base) { bool f = false; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "protected"), COSE_PROTECT_ONLY, base, true)) goto returnError; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unprotected"), COSE_UNPROTECT_ONLY, base, true)) goto returnError; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"), COSE_DONT_SEND, base, false)) goto returnError; cn_cbor * pExternal = cn_cbor_mapget_string(pIn, "external"); if (pExternal != NULL) { cn_cbor * pcn = cn_cbor_clone(pExternal, CBOR_CONTEXT_PARAM_COMMA NULL); if (pcn == NULL) goto returnError; switch (base) { #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_ENCRYPT case Attributes_Enveloped_protected: if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_MAC case Attributes_MAC_protected: if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_MAC0 case Attributes_MAC0_protected: if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_SIGN case Attributes_Signer_protected: if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_SIGN0 case Attributes_Sign0_protected: if (!COSE_Sign0_SetExternal((HCOSE_SIGN0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif } } f = true; returnError: return f; }
cn_cbor * _COSE_map_get_str(COSE * pcose, const char * key, int flags, cose_errback * perror) { cn_cbor * p = NULL; if (perror != NULL) perror->err = COSE_ERR_NONE; if ((pcose->m_protectedMap != NULL) && ((flags & COSE_PROTECT_ONLY) != 0)) { p = cn_cbor_mapget_string(pcose->m_protectedMap, key); if (p != NULL) return p; } if ((pcose->m_unprotectMap != NULL) && ((flags & COSE_UNPROTECT_ONLY) != 0)) { p = cn_cbor_mapget_string(pcose->m_unprotectMap, key); } if ((pcose->m_dontSendMap != NULL) && ((flags & COSE_DONT_SEND) != 0)) { p = cn_cbor_mapget_string(pcose->m_dontSendMap, key); } return p; }
void RunFileTest(const char * szFileName) { const cn_cbor * pControl = NULL; pControl = ParseJson(szFileName); // // If we are given a file name, then process the file name // if (pControl == NULL) { CFails += 1; return; } // To find out what we are doing we need to get the correct item const cn_cbor * pInput = cn_cbor_mapget_string(pControl, "input"); if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { fprintf(stderr, "No or bad input section"); exit(1); } if (cn_cbor_mapget_string(pInput, "mac") != NULL) { #if INCLUDE_MAC if (ValidateMAC(pControl)) { BuildMacMessage(pControl); } #endif } else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { #if INCLUDE_MAC0 if (ValidateMac0(pControl)) { BuildMac0Message(pControl); } #endif } else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { #if INCLUDE_ENCRYPT if (ValidateEnveloped(pControl)) { BuildEnvelopedMessage(pControl); } #endif } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN if (ValidateSigned(pControl)) { BuildSignedMessage(pControl); } #endif } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN0 if (ValidateSign0(pControl)) { BuildSign0Message(pControl); } #endif } else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { #if INCLUDE_ENCRYPT0 if (ValidateEncrypt(pControl)) { BuildEncryptMessage(pControl); } #endif } return; }
void RunMemoryTest(const char * szFileName) { #ifdef USE_CBOR_CONTEXT unsigned int iFail; const cn_cbor * pControl = ParseJson(szFileName); if (pControl == NULL) { CFails += 1; return; } // // To find out what we are doing we need to get the correct item const cn_cbor * pInput = cn_cbor_mapget_string(pControl, "input"); if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { fprintf(stderr, "No or bad input section"); exit(1); } // bool fValidateDone = false; bool fBuildDone = false; for (iFail = 0; !fValidateDone || !fBuildDone; iFail++) { context = CreateContext(iFail); if (cn_cbor_mapget_string(pInput, "mac") != NULL) { #if INCLUDE_MAC if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateMAC(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildMacMessage(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { #if INCLUDE_MAC0 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateMac0(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildMac0Message(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { #if INCLUDE_ENCRYPT0 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateEncrypt(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildEncryptMessage(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { #if INCLUDE_ENCRYPT if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateEnveloped(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildEnvelopedMessage(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateSigned(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildSignedMessage(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN0 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateSign0(pControl); if (CFails == 0) fValidateDone = true; } if (!fBuildDone) { context = CreateContext(iFail); CFails = 0; BuildSign0Message(pControl); if (CFails == 0) fBuildDone = true; } #else fValidateDone = true; fBuildDone = true; #endif } } CFails = 0; context = NULL; #else return; #endif }
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; }