static GString *gmime_message_to_json(GMimeMessage *message, guint content_option) { MessageData *mdata = convert_message(message, content_option); JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); json_object_set_value(root_object, "from", addresses_list_to_json(mdata->from)); json_object_set_value(root_object, "to", addresses_list_to_json(mdata->to)); json_object_set_value(root_object, "replyTo", addresses_list_to_json(mdata->reply_to)); json_object_set_value(root_object, "cc", addresses_list_to_json(mdata->cc)); json_object_set_value(root_object, "bcc", addresses_list_to_json(mdata->bcc)); json_object_set_string(root_object, "messageId", mdata->message_id); json_object_set_string(root_object, "subject", mdata->subject); json_object_set_string(root_object, "date", mdata->date); json_object_set_value(root_object, "inReplyTo", references_to_json(mdata->in_reply_to)); json_object_set_value(root_object, "references", references_to_json(mdata->references)); json_object_set_value(root_object, "text", message_body_to_json(mdata->text)); json_object_set_value(root_object, "html", message_body_to_json(mdata->html)); json_object_set_value(root_object, "attachments", message_attachments_list_to_json(mdata->attachments)); free_message_data(mdata); gchar *serialized_string = json_serialize_to_string(root_value); json_value_free(root_value); GString *json_string = g_string_new(serialized_string); g_free(serialized_string); return json_string; }
static JSON_Value* querySpecification_toJson(const PROVISIONING_QUERY_SPECIFICATION* query_spec) { JSON_Value* root_value = NULL; JSON_Object* root_object = NULL; //Setup if ((root_value = json_value_init_object()) == NULL) { LogError("json_value_init_object failed"); } else if ((root_object = json_value_get_object(root_value)) == NULL) { LogError("json_value_get_object failed"); json_value_free(root_value); root_value = NULL; } //Set data else if (json_object_set_string(root_object, QUERY_SPECIFICATION_JSON_KEY_QUERY, query_spec->query_string) != JSONSuccess) { LogError("Failed to set %s in JSON string", QUERY_SPECIFICATION_JSON_KEY_QUERY); json_value_free(root_value); root_value = NULL; } return root_value; }
static JSON_Value *address_to_json(Address *addr) { if (!addr) return NULL; JSON_Value *address_value = json_value_init_object(); JSON_Object *address_object = json_value_get_object(address_value); json_object_set_string(address_object, "name", addr->name); json_object_set_string(address_object, "address", addr->address); return address_value; }
void test_suite_7(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *schema = json_value_init_object(); JSON_Object *schema_obj = json_value_get_object(schema); json_object_set_string(schema_obj, "first", ""); json_object_set_string(schema_obj, "last", ""); json_object_set_number(schema_obj, "age", 0); json_object_set_null(schema_obj, "favorites"); TEST(json_validate(schema, val_from_file) == JSONSuccess); json_object_set_string(schema_obj, "age", ""); TEST(json_validate(schema, val_from_file) == JSONFailure); }
static JSON_Value *message_body_to_json(MessageBody *mbody) { if (!mbody) return NULL; JSON_Value *body_value = json_value_init_object(); JSON_Object *body_object = json_value_get_object(body_value); json_object_set_string(body_object, "type", mbody->content_type); json_object_set_string(body_object, "content", mbody->content->str); json_object_set_number(body_object, "size", mbody->size); return body_value; }
void serialization_example(void) { JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); char *serialized_string = NULL; json_object_set_string(root_object, "name", "John Smith"); json_object_set_number(root_object, "age", 25); json_object_dotset_string(root_object, "address.city", "Cupertino"); json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"[email protected]\", \"[email protected]\"]")); serialized_string = json_serialize_to_string(root_value); puts(serialized_string); json_free_serialized_string(serialized_string); json_value_free(root_value); }
//get message - string void getMessage(jsonMessage_t jsonMessage,String* message){ *message=NULL; //init JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); json_object_set_string(root_object, "type", jsonMessage.type); json_object_set_string(root_object, "address", jsonMessage.address); json_object_set_string(root_object, "replyAddress", jsonMessage.replyAddress); json_object_set_value(root_object, "headers", jsonMessage.headers); json_object_set_value(root_object, "body", jsonMessage.body); //to string *message=json_serialize_to_string_pretty(root_value); //free json_value_free(root_value); }
char *djcs_export_public_key(djcs_public_key *pk) { char *buffer; char *retstr; JSON_Value *root = json_value_init_object(); JSON_Object *obj = json_value_get_object(root); buffer = mpz_get_str(NULL, HCS_INTERNAL_BASE, pk->n[0]); json_object_set_number(obj, "s", pk->s); json_object_set_string(obj, "n", buffer); retstr = json_serialize_to_string(root); json_value_free(root); free(buffer); return retstr; }
void persistence_example(void) { JSON_Value *schema = json_parse_string("{\"name\":\"\"}"); JSON_Value *user_data = json_parse_file("user_data.json"); char buf[256]; const char *name = NULL; if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) { puts("Enter your name:"); scanf("%s", buf); user_data = json_value_init_object(); json_object_set_string(json_object(user_data), "name", buf); json_serialize_to_file(user_data, "user_data.json"); } name = json_object_get_string(json_object(user_data), "name"); printf("Hello, %s.", name); json_value_free(schema); json_value_free(user_data); return; }
static JSON_Value *message_attachments_list_to_json(MessageAttachmentsList *matts) { if (!matts) return NULL; JSON_Value *attachments_value = json_value_init_array(); JSON_Array *attachments_array = json_value_get_array(attachments_value); guint i; for (i = 0; i < matts->len; i++) { MessageAttachment *att = message_attachments_list_get(matts, i); JSON_Value *attachment_value = json_value_init_object(); JSON_Object *attachment_object = json_value_get_object(attachment_value); json_object_set_number(attachment_object, "partId", att->part_id); json_object_set_string(attachment_object, "type", att->content_type); json_object_set_string(attachment_object, "filename", att->filename); json_object_set_number(attachment_object, "size", att->size); json_array_append_value(attachments_array, attachment_value); } return attachments_value; }
void test_suite_5(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *val = json_value_init_object(); JSON_Object *obj = json_value_get_object(val); TEST(json_object_set_string(obj, "first", "John") == JSONSuccess); TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess); TEST(json_object_set_number(obj, "age", 25) == JSONSuccess); TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess); TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess); TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Writing") == JSONSuccess); TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Mountain Biking") == JSONSuccess); TEST(json_array_replace_string(json_object_get_array(obj, "interests"), 0, "Reading") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess); TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess); TEST(json_object_set_string(obj, "utf string", "\\u006corem\\u0020ipsum") == JSONSuccess); TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess); TEST(json_object_set_string(obj, "surrogate string", "lorem\\uD834\\uDD1Eipsum\\uD834\\uDF67lorem") == JSONSuccess); TEST(json_value_equals(val_from_file, val)); }
char *djcs_export_private_key(djcs_private_key *vk) { char *buffer; char *retstr; /* Allocate space for largest buffer output value used */ size_t buffer_size = mpz_sizeinbase((mpz_cmp(vk->n[0], vk->d) >= 0 ? vk->n[0] : vk->d), HCS_INTERNAL_BASE) + 2; buffer = malloc(buffer_size); JSON_Value *root = json_value_init_object(); JSON_Object *obj = json_value_get_object(root); json_object_set_number(obj, "s", vk->s); mpz_get_str(buffer, HCS_INTERNAL_BASE, vk->n[0]); json_object_set_string(obj, "n", buffer); mpz_get_str(buffer, HCS_INTERNAL_BASE, vk->d); json_object_set_string(obj, "d", buffer); retstr = json_serialize_to_string(root); json_value_free(root); free(buffer); return retstr; }
ACVP_RESULT acvp_kdf135_ssh_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) { unsigned int tc_id; JSON_Value *groupval; JSON_Object *groupobj = NULL; JSON_Value *testval; JSON_Object *testobj = NULL; JSON_Array *groups; JSON_Array *tests; JSON_Value *reg_arry_val = NULL; JSON_Object *reg_obj = NULL; JSON_Array *reg_arry = NULL; int i, g_cnt; int j, t_cnt; JSON_Value *r_vs_val = NULL; JSON_Object *r_vs = NULL; JSON_Array *r_tarr = NULL, *r_garr = NULL; /* Response testarray, grouparray */ JSON_Value *r_tval = NULL, *r_gval = NULL; /* Response testval, groupval */ JSON_Object *r_tobj = NULL, *r_gobj = NULL; /* Response testobj, groupobj */ ACVP_CAPS_LIST *cap; ACVP_KDF135_SSH_TC stc; ACVP_TEST_CASE tc; ACVP_RESULT rv; ACVP_CIPHER alg_id; const char *alg_str = NULL; const char *mode_str = NULL; const char *cipher_str = NULL; const char *shared_secret_str = NULL; const char *session_id_str = NULL; const char *hash_str = NULL; char *json_result; if (!ctx) { ACVP_LOG_ERR("No ctx for handler operation"); return ACVP_NO_CTX; } if (!obj) { ACVP_LOG_ERR("No obj for handler operation"); return ACVP_MALFORMED_JSON; } alg_str = json_object_get_string(obj, "algorithm"); if (!alg_str) { ACVP_LOG_ERR("unable to parse 'algorithm' from JSON"); return ACVP_MALFORMED_JSON; } mode_str = json_object_get_string(obj, "mode"); if (!mode_str) { ACVP_LOG_ERR("unable to parse 'mode' from JSON"); return ACVP_MALFORMED_JSON; } alg_id = acvp_lookup_cipher_w_mode_index(alg_str, mode_str); if (alg_id != ACVP_KDF135_SSH) { ACVP_LOG_ERR("Server JSON invalid 'algorithm' or 'mode'"); return ACVP_INVALID_ARG; } /* * Get a reference to the abstracted test case */ tc.tc.kdf135_ssh = &stc; /* * Get the crypto module handler for this hash algorithm */ cap = acvp_locate_cap_entry(ctx, alg_id); if (!cap) { ACVP_LOG_ERR("ACVP server requesting unsupported capability %s : %d.", alg_str, alg_id); return ACVP_UNSUPPORTED_OP; } /* * Create ACVP array for response */ rv = acvp_create_array(®_obj, ®_arry_val, ®_arry); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to create JSON response struct. "); return rv; } /* * Start to build the JSON response */ rv = acvp_setup_json_rsp_group(&ctx, ®_arry_val, &r_vs_val, &r_vs, alg_str, &r_garr); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to setup json response"); return rv; } groups = json_object_get_array(obj, "testGroups"); if (!groups) { ACVP_LOG_ERR("Failed to include testGroups. "); rv = ACVP_MISSING_ARG; goto err; } g_cnt = json_array_get_count(groups); for (i = 0; i < g_cnt; i++) { int tgId = 0; int diff = 1; unsigned int e_key_len = 0, i_key_len = 0, hash_len = 0, iv_len = 0; ACVP_HASH_ALG sha_type = 0; const char *sha_str = NULL; groupval = json_array_get_value(groups, i); groupobj = json_value_get_object(groupval); /* * Create a new group in the response with the tgid * and an array of tests */ r_gval = json_value_init_object(); r_gobj = json_value_get_object(r_gval); tgId = json_object_get_number(groupobj, "tgId"); if (!tgId) { ACVP_LOG_ERR("Missing tgid from server JSON groub obj"); rv = ACVP_MALFORMED_JSON; goto err; } json_object_set_number(r_gobj, "tgId", tgId); json_object_set_value(r_gobj, "tests", json_value_init_array()); r_tarr = json_object_get_array(r_gobj, "tests"); // Get the expected (user will generate) key and iv lengths cipher_str = json_object_get_string(groupobj, "cipher"); if (!cipher_str) { ACVP_LOG_ERR("Failed to include cipher. "); rv = ACVP_MISSING_ARG; goto err; } sha_str = json_object_get_string(groupobj, "hashAlg"); if (!sha_str) { ACVP_LOG_ERR("Failed to include hashAlg. "); rv = ACVP_MISSING_ARG; goto err; } sha_type = acvp_lookup_hash_alg(sha_str); if (sha_type == ACVP_SHA1) { i_key_len = hash_len = ACVP_SHA1_BYTE_LEN; } else if (sha_type == ACVP_SHA224) { i_key_len = hash_len = ACVP_SHA224_BYTE_LEN; } else if (sha_type == ACVP_SHA256) { i_key_len = hash_len = ACVP_SHA256_BYTE_LEN; } else if (sha_type == ACVP_SHA384) { i_key_len = hash_len = ACVP_SHA384_BYTE_LEN; } else if (sha_type == ACVP_SHA512) { i_key_len = hash_len = ACVP_SHA512_BYTE_LEN; } else { ACVP_LOG_ERR("ACVP server requesting invalid hashAlg"); rv = ACVP_NO_CAP; goto err; } /* * Determine the encrypt key_len, inferred from cipher. */ strcmp_s(ACVP_MODE_TDES, 4, cipher_str, &diff); if (!diff) { e_key_len = ACVP_KEY_LEN_TDES; iv_len = ACVP_BLOCK_LEN_TDES; } strcmp_s(ACVP_MODE_AES_128, 7, cipher_str, &diff); if (!diff) { e_key_len = ACVP_KEY_LEN_AES128; iv_len = ACVP_BLOCK_LEN_AES128; } strcmp_s(ACVP_MODE_AES_192, 7, cipher_str, &diff); if (!diff) { e_key_len = ACVP_KEY_LEN_AES192; iv_len = ACVP_BLOCK_LEN_AES192; } strcmp_s(ACVP_MODE_AES_256, 7, cipher_str, &diff); if (!diff) { e_key_len = ACVP_KEY_LEN_AES256; iv_len = ACVP_BLOCK_LEN_AES256; } if (!e_key_len || !iv_len) { ACVP_LOG_ERR("Unsupported cipher type"); rv = ACVP_NO_CAP; goto err; } /* * Log Test Group information... */ ACVP_LOG_INFO(" Test group: %d", i); ACVP_LOG_INFO(" cipher: %s", cipher_str); ACVP_LOG_INFO(" hashAlg: %s", sha_str); tests = json_object_get_array(groupobj, "tests"); if (!tests) { ACVP_LOG_ERR("Failed to include tests. "); rv = ACVP_MISSING_ARG; goto err; } t_cnt = json_array_get_count(tests); if (!t_cnt) { ACVP_LOG_ERR("Failed to include tests in array. "); rv = ACVP_MISSING_ARG; goto err; } for (j = 0; j < t_cnt; j++) { ACVP_LOG_INFO("Found new KDF SSH test vector..."); testval = json_array_get_value(tests, j); testobj = json_value_get_object(testval); tc_id = (unsigned int)json_object_get_number(testobj, "tcId"); if (!tc_id) { ACVP_LOG_ERR("Failed to include tc_id. "); rv = ACVP_MISSING_ARG; goto err; } shared_secret_str = json_object_get_string(testobj, "k"); if (!shared_secret_str) { ACVP_LOG_ERR("Failed to include k. "); rv = ACVP_MISSING_ARG; goto err; } hash_str = json_object_get_string(testobj, "h"); if (!hash_str) { ACVP_LOG_ERR("Failed to include h. "); rv = ACVP_MISSING_ARG; goto err; } session_id_str = json_object_get_string(testobj, "sessionId"); if (!session_id_str) { ACVP_LOG_ERR("Failed to include sessionId. "); rv = ACVP_MISSING_ARG; goto err; } ACVP_LOG_INFO(" Test case: %d", j); ACVP_LOG_INFO(" tcId: %d", tc_id); ACVP_LOG_INFO(" k: %s", shared_secret_str); ACVP_LOG_INFO(" h: %s", hash_str); ACVP_LOG_INFO(" session_id: %s", session_id_str); /* * Create a new test case in the response */ r_tval = json_value_init_object(); r_tobj = json_value_get_object(r_tval); json_object_set_number(r_tobj, "tcId", tc_id); /* * Setup the test case data that will be passed down to * the crypto module. */ rv = acvp_kdf135_ssh_init_tc(ctx, &stc, tc_id, alg_id, sha_type, e_key_len, i_key_len, iv_len, hash_len, shared_secret_str, hash_str, session_id_str); if (rv != ACVP_SUCCESS) { acvp_kdf135_ssh_release_tc(&stc); json_value_free(r_tval); goto err; } /* Process the current test vector... */ if ((cap->crypto_handler)(&tc)) { ACVP_LOG_ERR("crypto module failed the KDF SSH operation"); acvp_kdf135_ssh_release_tc(&stc); rv = ACVP_CRYPTO_MODULE_FAIL; json_value_free(r_tval); goto err; } /* * Output the test case results using JSON */ rv = acvp_kdf135_ssh_output_tc(ctx, &stc, r_tobj); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("JSON output failure in hash module"); acvp_kdf135_ssh_release_tc(&stc); json_value_free(r_tval); goto err; } /* * Release all the memory associated with the test case */ acvp_kdf135_ssh_release_tc(&stc); /* Append the test response value to array */ json_array_append_value(r_tarr, r_tval); } json_array_append_value(r_garr, r_gval); } json_array_append_value(reg_arry, r_vs_val); json_result = json_serialize_to_string_pretty(ctx->kat_resp, NULL); if (ctx->debug == ACVP_LOG_LVL_VERBOSE) { printf("\n\n%s\n\n", json_result); } else { ACVP_LOG_INFO("\n\n%s\n\n", json_result); } json_free_serialized_string(json_result); rv = ACVP_SUCCESS; err: if (rv != ACVP_SUCCESS) { acvp_release_json(r_vs_val, r_gval); } return rv; }
void APP2_Tasks ( void ) { /* Update the application state machine based * on the current state */ switch(app2Data.state) { case APP2_STATE_INIT: /* Open the device layer */ app2Data.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(app2Data.deviceHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(app2Data.deviceHandle, APP_USBDeviceEventHandler, 0); app2Data.state = APP2_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP2_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device was configured */ if(app2Data.isConfigured) { app2Data.state = APP2_STATE_SCHEDULE_READ; } break; case APP2_STATE_SCHEDULE_READ: if(APP_StateReset()) { break; } /* If a read is complete, then schedule a read * else wait for the current read to complete */ app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE; if(app2Data.isReadComplete == true) { app2Data.isReadComplete = false; app2Data.readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0, &app2Data.readTransferHandle, app2Data.readBuffer, APP_READ_BUFFER_SIZE); if(app2Data.readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID) { app2Data.state = APP2_STATE_ERROR; break; } } break; case APP2_STATE_WAIT_FOR_READ_COMPLETE: if(APP_StateReset()) { break; } if(app2Data.isReadComplete == true) { // Check for delimiter int res = APP_CheckForMessage(app2Data.readBuffer); if(res == MESSAGE_BAD_POINTER) { // Do something break; } else if(res == MESSAGE_NO_DELIMITER) { strcat(messageBuffer, app2Data.readBuffer); memset(app2Data.readBuffer, '\0', APP_READ_BUFFER_SIZE); app2Data.state = APP2_STATE_SCHEDULE_READ; break; } strcat(messageBuffer, app2Data.readBuffer); // Parse for the command being sent so we can handle it int command = APP_ParseCommand(messageBuffer); // Lets build our response message // First initialize the JSON value and object from parson library JSON_Value *rootValue = json_value_init_object(); JSON_Object *rootObject = json_value_get_object(rootValue); char *serializedString = NULL; // Build response and handle the command switch(command) { // If hello command, respond with discovery packet case COMMAND_HELLO: json_object_dotset_string(rootObject, "message.command", "discovery"); json_object_dotset_string(rootObject, "message.discovery_object.title", APP_TITLE); json_object_dotset_string(rootObject, "message.discovery_object.part_number", APP_PART_NUMBER); json_object_dotset_string(rootObject, "message.discovery_object.mac_address", appData.macAddress); json_object_dotset_string(rootObject, "message.discovery_object.firmware_version", APP_FIRMWARE_VERSION); json_object_dotset_string(rootObject, "message.discovery_object.harmony_version", SYS_VERSION_STR); json_object_dotset_boolean(rootObject, "message.discovery_object.is_commissioned", appData.isCommissioned); break; // If configuration command, respond with ACK/NACK case COMMAND_CONFIGURE: { if(appData.isCommissioned == false) { JSON_Value *messageRoot = json_parse_string(messageBuffer); if(json_value_get_type(messageRoot) != JSONObject) { BuildAckNackMessage(rootObject, "nack", "Invalid JSON Object"); break; } else { JSON_Object *messageObject = json_value_get_object(messageRoot); if( json_object_dotget_string(messageObject, "message.configuration_object.aws_iot_endpoint_address") != NULL && json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate") != NULL && json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate_private_key") != NULL ) { json_object_dotset_string(rootObject, "message.command", "ack"); json_object_dotset_string(rootObject, "message.ack_nack_message", "Writing commission parameters to flash"); sprintf((char *)appData.host, json_object_dotget_string(messageObject, "message.configuration_object.aws_iot_endpoint_address")); sprintf((char *)appData.clientCert, json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate")); sprintf((char *)appData.clientKey, json_object_dotget_string(messageObject, "message.configuration_object.aws_certificate_private_key")); appData.isCommissioned = true; appData.writeToNVM = true; } else { BuildAckNackMessage(rootObject, "nack", "Invalid commission parameters"); break; } } } else { BuildAckNackMessage(rootObject, "nack", "Already commissioned"); } break; } case COMMAND_DEBUG_SET: { JSON_Value *messageRoot = json_parse_string(messageBuffer); if(json_value_get_type(messageRoot) != JSONObject) { BuildAckNackMessage(rootObject, "nack", "Invalid JSON Object"); break; } JSON_Object *messageObject = json_value_get_object(messageRoot); if(messageObject == NULL) { BuildAckNackMessage(rootObject, "nack", "NULL Pointer"); break; } BuildAckNackMessage(rootObject, "ack", "Received debug set"); int DebugMessage = DEBUG_UPDATE; xQueueSendToFront(app2Data.debugQueue, &DebugMessage, 1); if(json_object_dotget_boolean(messageObject, "message.debug_set_object.set")) appData.debugSet = true; else appData.debugSet = false; break; } case COMMAND_BAD_JSON: BuildAckNackMessage(rootObject, "nack", "Bad JSON"); break; case COMMAND_INVALID: BuildAckNackMessage(rootObject, "nack", "Command Invalid"); break; default: BuildAckNackMessage(rootObject, "nack", "Something went wrong"); break; } memset(app2Data.writeBuffer, '\0', APP_WRITE_BUFFER_SIZE); // With our response built, serialize the response into a string serializedString = json_serialize_to_string(rootValue); strcpy(app2Data.writeBuffer, serializedString); // Find length of string and add delimiter to end app2Data.writeBuffer[strlen(app2Data.writeBuffer)] = '\r'; json_free_serialized_string(serializedString); // Reset string buffers memset(app2Data.readBuffer, '\0', APP_READ_BUFFER_SIZE); memset(messageBuffer, '\0', APP_MESSAGE_BUFFER_SIZE); app2Data.state = APP2_STATE_SCHEDULE_WRITE; } else { app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE; if( uxQueueMessagesWaiting( app2Data.debugQueue ) > 0 ) { int debugMessageNumber; xQueueReceive( app2Data.debugQueue, &debugMessageNumber, 1 ); sprintf(app2Data.writeBuffer, "{" "\"message\":{" "\"command\":\"debug\"," "\"debug_object\":{" "\"board_ip_address\":\"%d.%d.%d.%d\"," "\"aws_iot_endpoint\":\"%s\"," "\"mac_address\":\"%s\"," "\"socket_connected\":%s," "\"mqtt_connected\":%s," "\"raw_message\":\"%s\"" "}}}\r", appData.board_ipAddr.v4Add.v[0], appData.board_ipAddr.v4Add.v[1], appData.board_ipAddr.v4Add.v[2], appData.board_ipAddr.v4Add.v[3], appData.host, appData.macAddress, (appData.socket_connected ? "true" : "false"), (appData.mqtt_connected ? "true" : "false"), APP_ReturnDebugCodeToString(debugMessageNumber)); app2Data.state = APP2_STATE_SCHEDULE_DEBUG_WRITE; } } break; case APP2_STATE_SCHEDULE_WRITE: if(APP_StateReset()) { break; } app2Data.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; app2Data.isWriteComplete = false; app2Data.state = APP2_STATE_WAIT_FOR_WRITE_COMPLETE; USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0, &app2Data.writeTransferHandle, app2Data.writeBuffer, strlen(app2Data.writeBuffer), USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); break; case APP2_STATE_WAIT_FOR_WRITE_COMPLETE: if(APP_StateReset()) { break; } /* Check if message sent. The isWriteComplete * flag gets updated in the CDC event handler */ if(app2Data.isWriteComplete == true) { app2Data.state = APP2_STATE_SCHEDULE_READ; } break; case APP2_STATE_SCHEDULE_DEBUG_WRITE: if(APP_StateReset()) { break; } app2Data.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; app2Data.isWriteComplete = false; app2Data.state = APP2_STATE_WAIT_FOR_DEBUG_WRITE_COMPLETE; USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0, &app2Data.writeTransferHandle, app2Data.writeBuffer, strlen(app2Data.writeBuffer), USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE); break; case APP2_STATE_WAIT_FOR_DEBUG_WRITE_COMPLETE: if(APP_StateReset()) { break; } /* Check if message sent. The isWriteComplete * flag gets updated in the CDC event handler */ if(app2Data.isWriteComplete == true) { app2Data.state = APP2_STATE_WAIT_FOR_READ_COMPLETE; } break; case APP2_STATE_ERROR: break; default: break; } }
ACVP_RESULT acvp_kdf135_ikev1_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) { unsigned int tc_id; JSON_Value *groupval; JSON_Object *groupobj = NULL; JSON_Value *testval; JSON_Object *testobj = NULL; JSON_Array *groups; JSON_Array *tests; JSON_Value *reg_arry_val = NULL; JSON_Object *reg_obj = NULL; JSON_Array *reg_arry = NULL; int i, g_cnt; int j, t_cnt; JSON_Value *r_vs_val = NULL; JSON_Object *r_vs = NULL; JSON_Array *r_tarr = NULL, *r_garr = NULL; /* Response testarray, grouparray */ JSON_Value *r_tval = NULL, *r_gval = NULL; /* Response testval, groupval */ JSON_Object *r_tobj = NULL, *r_gobj = NULL; /* Response testobj, groupobj */ ACVP_CAPS_LIST *cap; ACVP_KDF135_IKEV1_TC stc; ACVP_TEST_CASE tc; ACVP_RESULT rv; const char *alg_str = json_object_get_string(obj, "algorithm"); const char *mode_str = NULL; ACVP_CIPHER alg_id; char *json_result; ACVP_HASH_ALG hash_alg = 0; ACVP_KDF135_IKEV1_AUTH_METHOD auth_method = 0; const char *hash_alg_str = NULL, *auth_method_str = NULL; char *init_ckey = NULL, *resp_ckey = NULL, *gxy = NULL, *psk = NULL, *init_nonce = NULL, *resp_nonce = NULL; int init_nonce_len = 0, resp_nonce_len = 0, dh_secret_len = 0, psk_len = 0; if (!ctx) { ACVP_LOG_ERR("No ctx for handler operation"); return ACVP_NO_CTX; } if (!alg_str) { ACVP_LOG_ERR("unable to parse 'algorithm' from JSON."); return ACVP_MALFORMED_JSON; } mode_str = json_object_get_string(obj, "mode"); if (!mode_str) { ACVP_LOG_ERR("unable to parse 'mode' from JSON."); return ACVP_MALFORMED_JSON; } alg_id = acvp_lookup_cipher_w_mode_index(alg_str, mode_str); if (alg_id != ACVP_KDF135_IKEV1) { ACVP_LOG_ERR("Server JSON invalid 'algorithm' or 'mode'"); return ACVP_INVALID_ARG; } /* * Get a reference to the abstracted test case */ tc.tc.kdf135_ikev1 = &stc; stc.cipher = alg_id; cap = acvp_locate_cap_entry(ctx, alg_id); if (!cap) { ACVP_LOG_ERR("ACVP server requesting unsupported capability %s : %d.", alg_str, alg_id); return ACVP_UNSUPPORTED_OP; } /* * Create ACVP array for response */ rv = acvp_create_array(®_obj, ®_arry_val, ®_arry); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to create JSON response struct. "); return rv; } /* * Start to build the JSON response */ rv = acvp_setup_json_rsp_group(&ctx, ®_arry_val, &r_vs_val, &r_vs, alg_str, &r_garr); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to setup json response"); return rv; } groups = json_object_get_array(obj, "testGroups"); g_cnt = json_array_get_count(groups); for (i = 0; i < g_cnt; i++) { int tgId = 0; groupval = json_array_get_value(groups, i); groupobj = json_value_get_object(groupval); /* * Create a new group in the response with the tgid * and an array of tests */ r_gval = json_value_init_object(); r_gobj = json_value_get_object(r_gval); tgId = json_object_get_number(groupobj, "tgId"); if (!tgId) { ACVP_LOG_ERR("Missing tgid from server JSON groub obj"); rv = ACVP_MALFORMED_JSON; goto err; } json_object_set_number(r_gobj, "tgId", tgId); json_object_set_value(r_gobj, "tests", json_value_init_array()); r_tarr = json_object_get_array(r_gobj, "tests"); hash_alg_str = json_object_get_string(groupobj, "hashAlg"); if (!hash_alg_str) { ACVP_LOG_ERR("Failed to include hashAlg"); rv = ACVP_MISSING_ARG; goto err; } hash_alg = acvp_lookup_hash_alg(hash_alg_str); if (hash_alg != ACVP_SHA1 && hash_alg != ACVP_SHA224 && hash_alg != ACVP_SHA256 && hash_alg != ACVP_SHA384 && hash_alg != ACVP_SHA512) { ACVP_LOG_ERR("ACVP server requesting invalid hashAlg"); rv = ACVP_INVALID_ARG; goto err; } auth_method_str = json_object_get_string(groupobj, "authenticationMethod"); if (!auth_method_str) { ACVP_LOG_ERR("Failed to include authenticationMethod"); rv = ACVP_MISSING_ARG; goto err; } auth_method = read_auth_method(auth_method_str); if (!auth_method) { ACVP_LOG_ERR("ACVP server requesting invalid authenticationMethod"); rv = ACVP_INVALID_ARG; goto err; } init_nonce_len = json_object_get_number(groupobj, "nInitLength"); if (!(init_nonce_len >= ACVP_KDF135_IKEV1_INIT_NONCE_BIT_MIN && init_nonce_len <= ACVP_KDF135_IKEV1_INIT_NONCE_BIT_MAX)) { ACVP_LOG_ERR("nInitLength incorrect, %d", init_nonce_len); rv = ACVP_INVALID_ARG; goto err; } resp_nonce_len = json_object_get_number(groupobj, "nRespLength"); if (!(resp_nonce_len >= ACVP_KDF135_IKEV1_RESP_NONCE_BIT_MIN && resp_nonce_len <= ACVP_KDF135_IKEV1_RESP_NONCE_BIT_MAX)) { ACVP_LOG_ERR("nRespLength incorrect, %d", resp_nonce_len); rv = ACVP_INVALID_ARG; goto err; } dh_secret_len = json_object_get_number(groupobj, "dhLength"); if (!(dh_secret_len >= ACVP_KDF135_IKEV1_DH_SHARED_SECRET_BIT_MIN && dh_secret_len <= ACVP_KDF135_IKEV1_DH_SHARED_SECRET_BIT_MAX)) { ACVP_LOG_ERR("dhLength incorrect, %d", dh_secret_len); rv = ACVP_INVALID_ARG; goto err; } if (auth_method == ACVP_KDF135_IKEV1_AMETH_PSK) { /* Only for PSK authentication method */ psk_len = json_object_get_number(groupobj, "preSharedKeyLength"); if (!(psk_len >= ACVP_KDF135_IKEV1_PSK_BIT_MIN && psk_len <= ACVP_KDF135_IKEV1_PSK_BIT_MAX)) { ACVP_LOG_ERR("preSharedKeyLength incorrect, %d", psk_len); rv = ACVP_INVALID_ARG; goto err; } } ACVP_LOG_INFO("\n Test group: %d", i); ACVP_LOG_INFO(" hash alg: %s", hash_alg_str); ACVP_LOG_INFO(" auth method: %s", auth_method_str); ACVP_LOG_INFO(" init nonce len: %d", init_nonce_len); ACVP_LOG_INFO(" resp nonce len: %d", resp_nonce_len); ACVP_LOG_INFO(" dh secret len: %d", dh_secret_len); ACVP_LOG_INFO(" psk len: %d", psk_len); tests = json_object_get_array(groupobj, "tests"); t_cnt = json_array_get_count(tests); for (j = 0; j < t_cnt; j++) { ACVP_LOG_INFO("Found new KDF IKEv1 test vector..."); testval = json_array_get_value(tests, j); testobj = json_value_get_object(testval); tc_id = (unsigned int)json_object_get_number(testobj, "tcId"); init_nonce = (char *)json_object_get_string(testobj, "nInit"); if (!init_nonce) { ACVP_LOG_ERR("Failed to include nInit"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)init_nonce, ACVP_KDF135_IKEV1_INIT_NONCE_STR_MAX + 1) != ((init_nonce_len + 7) / 8) * 2) { ACVP_LOG_ERR("nInit length(%d) incorrect, expected(%d)", strnlen_s((char *)init_nonce, ACVP_KDF135_IKEV1_INIT_NONCE_STR_MAX + 1), ((init_nonce_len + 7) / 8) * 2); rv = ACVP_INVALID_ARG; goto err; } resp_nonce = (char *)json_object_get_string(testobj, "nResp"); if (!resp_nonce) { ACVP_LOG_ERR("Failed to include nResp"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)resp_nonce, ACVP_KDF135_IKEV1_RESP_NONCE_STR_MAX + 1) != ((resp_nonce_len + 7) / 8) * 2) { ACVP_LOG_ERR("nResp length(%d) incorrect, expected(%d)", strnlen_s((char *)resp_nonce, ACVP_KDF135_IKEV1_RESP_NONCE_STR_MAX + 1), ((resp_nonce_len + 7) / 8) * 2); rv = ACVP_INVALID_ARG; goto err; } init_ckey = (char *)json_object_get_string(testobj, "ckyInit"); if (!init_ckey) { ACVP_LOG_ERR("Failed to include ckyInit"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)init_ckey, ACVP_KDF135_IKEV1_COOKIE_STR_MAX + 1) > ACVP_KDF135_IKEV1_COOKIE_STR_MAX) { ACVP_LOG_ERR("ckyInit too long, max allowed=(%d)", ACVP_KDF135_IKEV1_COOKIE_STR_MAX); rv = ACVP_INVALID_ARG; goto err; } resp_ckey = (char *)json_object_get_string(testobj, "ckyResp"); if (!resp_ckey) { ACVP_LOG_ERR("Failed to include ckyResp"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)resp_ckey, ACVP_KDF135_IKEV1_COOKIE_STR_MAX + 1) > ACVP_KDF135_IKEV1_COOKIE_STR_MAX) { ACVP_LOG_ERR("ckyResp too long, max allowed=(%d)", ACVP_KDF135_IKEV1_COOKIE_STR_MAX); rv = ACVP_INVALID_ARG; goto err; } gxy = (char *)json_object_get_string(testobj, "gxy"); if (!gxy) { ACVP_LOG_ERR("Failed to include gxy"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)gxy, ACVP_KDF135_IKEV1_DH_SHARED_SECRET_STR_MAX + 1) > ACVP_KDF135_IKEV1_DH_SHARED_SECRET_STR_MAX) { ACVP_LOG_ERR("gxy too long, max allowed=(%d)", ACVP_KDF135_IKEV1_DH_SHARED_SECRET_STR_MAX); rv = ACVP_INVALID_ARG; goto err; } if (auth_method == ACVP_KDF135_IKEV1_AMETH_PSK) { /* Only for PSK authentication method */ psk = (char *)json_object_get_string(testobj, "preSharedKey"); if (!psk) { ACVP_LOG_ERR("Failed to include preSharedKey"); rv = ACVP_MISSING_ARG; goto err; } if (strnlen_s((char *)psk, ACVP_KDF135_IKEV1_PSK_STR_MAX + 1) > ACVP_KDF135_IKEV1_PSK_STR_MAX) { ACVP_LOG_ERR("preSharedKey too long, max allowed=(%d)", ACVP_KDF135_IKEV1_PSK_STR_MAX); rv = ACVP_INVALID_ARG; goto err; } } ACVP_LOG_INFO(" Test case: %d", j); ACVP_LOG_INFO(" tcId: %d", tc_id); /* * Create a new test case in the response */ r_tval = json_value_init_object(); r_tobj = json_value_get_object(r_tval); json_object_set_number(r_tobj, "tcId", tc_id); /* * Setup the test case data that will be passed down to * the crypto module2 */ rv = acvp_kdf135_ikev1_init_tc(ctx, &stc, tc_id, hash_alg, auth_method, init_nonce_len, resp_nonce_len, dh_secret_len, psk_len, init_nonce, resp_nonce, init_ckey, resp_ckey, gxy, psk); if (rv != ACVP_SUCCESS) { acvp_kdf135_ikev1_release_tc(&stc); json_value_free(r_tval); goto err; } /* Process the current test vector... */ if ((cap->crypto_handler)(&tc)) { ACVP_LOG_ERR("crypto module failed the KDF IKEv1 operation"); acvp_kdf135_ikev1_release_tc(&stc); rv = ACVP_CRYPTO_MODULE_FAIL; json_value_free(r_tval); goto err; } /* * Output the test case results using JSON */ rv = acvp_kdf135_ikev1_output_tc(ctx, &stc, r_tobj); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("JSON output failure in hash module"); acvp_kdf135_ikev1_release_tc(&stc); json_value_free(r_tval); goto err; } /* * Release all the memory associated with the test case */ acvp_kdf135_ikev1_release_tc(&stc); /* Append the test response value to array */ json_array_append_value(r_tarr, r_tval); } json_array_append_value(r_garr, r_gval); } json_array_append_value(reg_arry, r_vs_val); json_result = json_serialize_to_string_pretty(ctx->kat_resp, NULL); if (ctx->debug == ACVP_LOG_LVL_VERBOSE) { printf("\n\n%s\n\n", json_result); } else { ACVP_LOG_INFO("\n\n%s\n\n", json_result); } json_free_serialized_string(json_result); rv = ACVP_SUCCESS; err: if (rv != ACVP_SUCCESS) { acvp_release_json(r_vs_val, r_gval); } return rv; }
void test_suite_5(void) { JSON_Value *val_from_file = json_parse_file("tests/test_5.txt"); JSON_Value *val = NULL; JSON_Object *obj = NULL; JSON_Array *interests_arr = NULL; val = json_value_init_object(); TEST(val != NULL); obj = json_value_get_object(val); TEST(obj != NULL); TEST(json_object_set_string(obj, "first", "John") == JSONSuccess); TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess); TEST(json_object_set_number(obj, "age", 25) == JSONSuccess); TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess); TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess); interests_arr = json_object_get_array(obj, "interests"); TEST(interests_arr != NULL); TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess); TEST(json_array_append_string(interests_arr, "Mountain Biking") == JSONSuccess); TEST(json_array_replace_string(interests_arr, 0, "Reading") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess); TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess); TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess); TEST(json_object_set_string(obj, "utf string", "lorem ipsum") == JSONSuccess); TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess); TEST(json_object_set_string(obj, "surrogate string", "lorem𝄞ipsum𝍧lorem") == JSONSuccess); TEST(json_object_set_string(obj, "windows path", "C:\\Windows\\Path") == JSONSuccess); TEST(json_value_equals(val_from_file, val)); TEST(json_object_set_string(obj, NULL, "") == JSONFailure); TEST(json_object_set_string(obj, "last", NULL) == JSONFailure); TEST(json_object_set_string(obj, NULL, NULL) == JSONFailure); TEST(json_object_set_value(obj, NULL, NULL) == JSONFailure); TEST(json_object_dotset_string(obj, NULL, "") == JSONFailure); TEST(json_object_dotset_string(obj, "favorites.color", NULL) == JSONFailure); TEST(json_object_dotset_string(obj, NULL, NULL) == JSONFailure); TEST(json_object_dotset_value(obj, NULL, NULL) == JSONFailure); TEST(json_array_append_string(NULL, "lorem") == JSONFailure); TEST(json_array_append_value(interests_arr, NULL) == JSONFailure); TEST(json_array_append_value(NULL, NULL) == JSONFailure); TEST(json_array_remove(NULL, 0) == JSONFailure); TEST(json_array_replace_value(interests_arr, 0, NULL) == JSONFailure); TEST(json_array_replace_string(NULL, 0, "lorem") == JSONFailure); TEST(json_array_replace_string(interests_arr, 100, "not existing") == JSONFailure); TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure); /* UTF-8 tests */ TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 1", "\xed\x9f\xbf") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 2", "\xee\x80\x80") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 3", "\xef\xbf\xbd") == JSONSuccess); TEST(json_object_set_string(obj, "boundary 4", "\xf4\x8f\xbf\xbf") == JSONSuccess); TEST(json_object_set_string(obj, "first continuation byte", "\x80") == JSONFailure); TEST(json_object_set_string(obj, "last continuation byte", "\xbf") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 1", "\xfe") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 2", "\xff") == JSONFailure); TEST(json_object_set_string(obj, "impossible sequence 3", "\xfe\xfe\xff\xff") == JSONFailure); TEST(json_object_set_string(obj, "overlong 1", "\xc0\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 2", "\xc1\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 3", "\xe0\x80\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 4", "\xe0\x9f\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 5", "\xf0\x80\x80\xaf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 6", "\xf0\x8f\xbf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong 7", "\xf0\x8f\xbf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 1", "\xc0\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 2", "\xe0\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 3", "\xf0\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 4", "\xf8\x80\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "overlong null 5", "\xfc\x80\x80\x80\x80\x80") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 1", "\xed\xa0\x80") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 2", "\xed\xaf\xbf") == JSONFailure); TEST(json_object_set_string(obj, "single surrogate 3", "\xed\xbf\xbf") == JSONFailure); }
ACVP_RESULT acvp_cmac_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) { unsigned int tc_id, msglen, keyLen = 0, keyingOption = 0, maclen, verify = 0; char *msg = NULL, *key1 = NULL, *key2 = NULL, *key3 = NULL, *mac = NULL; JSON_Value *groupval; JSON_Object *groupobj = NULL; JSON_Value *testval; JSON_Object *testobj = NULL; JSON_Array *groups; JSON_Array *tests; JSON_Value *reg_arry_val = NULL; JSON_Object *reg_obj = NULL; JSON_Array *reg_arry = NULL; int i, g_cnt; int j, t_cnt; JSON_Value *r_vs_val = NULL; JSON_Object *r_vs = NULL; JSON_Array *r_tarr = NULL, *r_garr = NULL; /* Response testarray, grouparray */ JSON_Value *r_tval = NULL, *r_gval = NULL; /* Response testval, groupval */ JSON_Object *r_tobj = NULL, *r_gobj = NULL; /* Response testobj, groupobj */ ACVP_CAPS_LIST *cap; ACVP_CMAC_TC stc; ACVP_TEST_CASE tc; ACVP_RESULT rv; const char *alg_str = json_object_get_string(obj, "algorithm"); ACVP_CIPHER alg_id; char *json_result, *direction = NULL; int key1_len, key2_len, key3_len, json_msglen; if (!ctx) { ACVP_LOG_ERR("No ctx for handler operation"); return ACVP_NO_CTX; } if (!obj) { ACVP_LOG_ERR("No obj for handler operation"); return ACVP_MALFORMED_JSON; } if (!alg_str) { ACVP_LOG_ERR("ERROR: unable to parse 'algorithm' from JSON"); return ACVP_MALFORMED_JSON; } /* * Get a reference to the abstracted test case */ tc.tc.cmac = &stc; /* * Get the crypto module handler for this hash algorithm */ alg_id = acvp_lookup_cipher_index(alg_str); if (alg_id < ACVP_CIPHER_START) { ACVP_LOG_ERR("ERROR: unsupported algorithm (%s)", alg_str); return ACVP_UNSUPPORTED_OP; } cap = acvp_locate_cap_entry(ctx, alg_id); if (!cap) { ACVP_LOG_ERR("ERROR: ACVP server requesting unsupported capability"); return ACVP_UNSUPPORTED_OP; } /* * Create ACVP array for response */ rv = acvp_create_array(®_obj, ®_arry_val, ®_arry); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("ERROR: Failed to create JSON response struct. "); return rv; } /* * Start to build the JSON response */ rv = acvp_setup_json_rsp_group(&ctx, ®_arry_val, &r_vs_val, &r_vs, alg_str, &r_garr); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to setup json response"); return rv; } groups = json_object_get_array(obj, "testGroups"); g_cnt = json_array_get_count(groups); for (i = 0; i < g_cnt; i++) { int tgId = 0; int diff = 0; groupval = json_array_get_value(groups, i); groupobj = json_value_get_object(groupval); /* * Create a new group in the response with the tgid * and an array of tests */ r_gval = json_value_init_object(); r_gobj = json_value_get_object(r_gval); tgId = json_object_get_number(groupobj, "tgId"); if (!tgId) { ACVP_LOG_ERR("Missing tgid from server JSON groub obj"); rv = ACVP_MALFORMED_JSON; goto err; } json_object_set_number(r_gobj, "tgId", tgId); json_object_set_value(r_gobj, "tests", json_value_init_array()); r_tarr = json_object_get_array(r_gobj, "tests"); if (alg_id == ACVP_CMAC_AES) { keyLen = (unsigned int)json_object_get_number(groupobj, "keyLen"); if (!keyLen) { ACVP_LOG_ERR("keylen missing from cmac aes json"); rv = ACVP_MISSING_ARG; goto err; } } else if (alg_id == ACVP_CMAC_TDES) { keyingOption = (unsigned int)json_object_get_number(groupobj, "keyingOption"); if (keyingOption <= ACVP_CMAC_TDES_KEYING_OPTION_MIN || keyingOption >= ACVP_CMAC_TDES_KEYING_OPTION_MAX) { ACVP_LOG_ERR("keyingOption missing or wrong from cmac tdes json"); rv = ACVP_INVALID_ARG; goto err; } } direction = (char *)json_object_get_string(groupobj, "direction"); if (!direction) { ACVP_LOG_ERR("Unable to parse 'direction' from JSON."); rv = ACVP_MALFORMED_JSON; goto err; } strcmp_s("ver", 3, direction, &diff); if (!diff) { verify = 1; } else { strcmp_s("gen", 3, direction, &diff); if (diff) { ACVP_LOG_ERR("'direction' should be 'gen' or 'ver'"); rv = ACVP_UNSUPPORTED_OP; goto err; } } msglen = (unsigned int)json_object_get_number(groupobj, "msgLen") / 8; maclen = (unsigned int)json_object_get_number(groupobj, "macLen") / 8; if (!maclen) { ACVP_LOG_ERR("Server JSON missing 'macLen'"); rv = ACVP_MISSING_ARG; goto err; } ACVP_LOG_INFO("\n\n Test group: %d", i); tests = json_object_get_array(groupobj, "tests"); t_cnt = json_array_get_count(tests); for (j = 0; j < t_cnt; j++) { ACVP_LOG_INFO("Found new cmac test vector..."); testval = json_array_get_value(tests, j); testobj = json_value_get_object(testval); tc_id = (unsigned int)json_object_get_number(testobj, "tcId"); msg = (char *)json_object_get_string(testobj, "message"); /* msg can be null if msglen is 0 */ if (msg) { json_msglen = strnlen_s(msg, ACVP_CMAC_MSGLEN_MAX_STR + 1); if (json_msglen > ACVP_CMAC_MSGLEN_MAX_STR) { ACVP_LOG_ERR("'msg' too long"); rv = ACVP_INVALID_ARG; goto err; } if (!msglen && json_msglen > 0) { ACVP_LOG_ERR("Server JSON missing 'msgLen'"); rv = ACVP_MISSING_ARG; goto err; } } else if (msglen) { ACVP_LOG_ERR("msglen is nonzero, expected 'msg' in json"); rv = ACVP_MISSING_ARG; goto err; } if (alg_id == ACVP_CMAC_AES) { key1 = (char *)json_object_get_string(testobj, "key"); if (!key1) { ACVP_LOG_ERR("Server JSON missing 'key'"); rv = ACVP_MISSING_ARG; goto err; } key1_len = strnlen_s(key1, ACVP_CMAC_KEY_MAX + 1); if (key1_len > ACVP_CMAC_KEY_MAX) { ACVP_LOG_ERR("Invalid length for 'key' attribute in CMAC-AES test"); rv = ACVP_INVALID_ARG; goto err; } } else if (alg_id == ACVP_CMAC_TDES) { key1 = (char *)json_object_get_string(testobj, "key1"); key2 = (char *)json_object_get_string(testobj, "key2"); key3 = (char *)json_object_get_string(testobj, "key3"); if (!key1 || !key2 || !key3) { ACVP_LOG_ERR("Server JSON missing 'key(1,2,3)' value"); rv = ACVP_MISSING_ARG; goto err; } key1_len = strnlen_s(key1, ACVP_CMAC_KEY_MAX + 1); key2_len = strnlen_s(key2, ACVP_CMAC_KEY_MAX + 1); key3_len = strnlen_s(key3, ACVP_CMAC_KEY_MAX + 1); if (key1_len > ACVP_CMAC_KEY_MAX || key2_len > ACVP_CMAC_KEY_MAX || key3_len > ACVP_CMAC_KEY_MAX) { ACVP_LOG_ERR("Invalid length for 'key(1|2|3)' attribute in CMAC-TDES test"); rv = ACVP_INVALID_ARG; goto err; } } if (verify) { mac = (char *)json_object_get_string(testobj, "mac"); if (!mac) { ACVP_LOG_ERR("Server JSON missing 'mac'"); rv = ACVP_MISSING_ARG; goto err; } } ACVP_LOG_INFO("\n Test case: %d", j); ACVP_LOG_INFO(" tcId: %d", tc_id); ACVP_LOG_INFO(" direction: %s", direction); ACVP_LOG_INFO(" msgLen: %d", msglen); ACVP_LOG_INFO(" msg: %s", msg); if (alg_id == ACVP_CMAC_AES) { ACVP_LOG_INFO(" keyLen: %d", keyLen); ACVP_LOG_INFO(" key: %s", key1); } else if (alg_id == ACVP_CMAC_TDES) { ACVP_LOG_INFO(" keyingOption: %d", keyingOption); ACVP_LOG_INFO(" key1: %s", key1); ACVP_LOG_INFO(" key2: %s", key2); ACVP_LOG_INFO(" key3: %s", key3); } if (verify) { ACVP_LOG_INFO(" mac: %s", mac); } /* * Create a new test case in the response */ r_tval = json_value_init_object(); r_tobj = json_value_get_object(r_tval); json_object_set_number(r_tobj, "tcId", tc_id); /* * Setup the test case data that will be passed down to * the crypto module. */ rv = acvp_cmac_init_tc(ctx, &stc, tc_id, msg, msglen, keyLen, key1, key2, key3, verify, mac, maclen, alg_id); if (rv != ACVP_SUCCESS) { acvp_cmac_release_tc(&stc); json_value_free(r_tval); goto err; } /* Process the current test vector... */ if ((cap->crypto_handler)(&tc)) { ACVP_LOG_ERR("ERROR: crypto module failed the operation"); acvp_cmac_release_tc(&stc); rv = ACVP_CRYPTO_MODULE_FAIL; json_value_free(r_tval); goto err; } /* * Output the test case results using JSON */ rv = acvp_cmac_output_tc(ctx, &stc, r_tobj); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("ERROR: JSON output failure in hash module"); acvp_cmac_release_tc(&stc); json_value_free(r_tval); goto err; } /* * Release all the memory associated with the test case */ acvp_cmac_release_tc(&stc); /* Append the test response value to array */ json_array_append_value(r_tarr, r_tval); } json_array_append_value(r_garr, r_gval); } json_array_append_value(reg_arry, r_vs_val); json_result = json_serialize_to_string_pretty(ctx->kat_resp, NULL); if (ctx->debug == ACVP_LOG_LVL_VERBOSE) { printf("\n\n%s\n\n", json_result); } else { ACVP_LOG_INFO("\n\n%s\n\n", json_result); } json_free_serialized_string(json_result); rv = ACVP_SUCCESS; err: if (rv != ACVP_SUCCESS) { acvp_release_json(r_vs_val, r_gval); } return rv; }
ACVP_RESULT acvp_kdf135_srtp_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) { unsigned int tc_id; JSON_Value *groupval; JSON_Object *groupobj = NULL; JSON_Value *testval; JSON_Object *testobj = NULL; JSON_Array *groups; JSON_Array *tests; JSON_Value *reg_arry_val = NULL; JSON_Object *reg_obj = NULL; JSON_Array *reg_arry = NULL; int i, g_cnt; int j, t_cnt; JSON_Value *r_vs_val = NULL; JSON_Object *r_vs = NULL; JSON_Array *r_tarr = NULL, *r_garr = NULL; /* Response testarray, grouparray */ JSON_Value *r_tval = NULL, *r_gval = NULL; /* Response testval, groupval */ JSON_Object *r_tobj = NULL, *r_gobj = NULL; /* Response testobj, groupobj */ ACVP_CAPS_LIST *cap; ACVP_KDF135_SRTP_TC stc; ACVP_TEST_CASE tc; ACVP_RESULT rv; const char *alg_str = json_object_get_string(obj, "algorithm"); const char *mode_str = NULL; ACVP_CIPHER alg_id; char *json_result; int aes_key_length; char *kdr = NULL, *master_key = NULL, *master_salt = NULL, *index = NULL, *srtcp_index = NULL; if (!ctx) { ACVP_LOG_ERR("No ctx for handler operation"); return ACVP_NO_CTX; } if (!alg_str) { ACVP_LOG_ERR("unable to parse 'algorithm' from JSON."); return ACVP_MALFORMED_JSON; } mode_str = json_object_get_string(obj, "mode"); if (!mode_str) { ACVP_LOG_ERR("unable to parse 'mode' from JSON."); return ACVP_MALFORMED_JSON; } alg_id = acvp_lookup_cipher_w_mode_index(alg_str, mode_str); if (alg_id != ACVP_KDF135_SRTP) { ACVP_LOG_ERR("Server JSON invalid 'algorithm' or 'mode'"); return ACVP_INVALID_ARG; } /* * Get a reference to the abstracted test case */ tc.tc.kdf135_srtp = &stc; stc.cipher = alg_id; cap = acvp_locate_cap_entry(ctx, alg_id); if (!cap) { ACVP_LOG_ERR("ACVP server requesting unsupported capability %s : %d.", alg_str, alg_id); return ACVP_UNSUPPORTED_OP; } /* * Create ACVP array for response */ rv = acvp_create_array(®_obj, ®_arry_val, ®_arry); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to create JSON response struct. "); return rv; } /* * Start to build the JSON response */ rv = acvp_setup_json_rsp_group(&ctx, ®_arry_val, &r_vs_val, &r_vs, alg_str, &r_garr); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("Failed to setup json response"); goto err; } groups = json_object_get_array(obj, "testGroups"); g_cnt = json_array_get_count(groups); for (i = 0; i < g_cnt; i++) { int tgId = 0; groupval = json_array_get_value(groups, i); groupobj = json_value_get_object(groupval); /* * Create a new group in the response with the tgid * and an array of tests */ r_gval = json_value_init_object(); r_gobj = json_value_get_object(r_gval); tgId = json_object_get_number(groupobj, "tgId"); if (!tgId) { ACVP_LOG_ERR("Missing tgid from server JSON groub obj"); rv = ACVP_MALFORMED_JSON; goto err; } json_object_set_number(r_gobj, "tgId", tgId); json_object_set_value(r_gobj, "tests", json_value_init_array()); r_tarr = json_object_get_array(r_gobj, "tests"); aes_key_length = (unsigned int)json_object_get_number(groupobj, "aesKeyLength"); if (!aes_key_length) { ACVP_LOG_ERR("aesKeyLength incorrect, %d", aes_key_length); rv = ACVP_INVALID_ARG; goto err; } kdr = (char *)json_object_get_string(groupobj, "kdr"); if (!kdr) { ACVP_LOG_ERR("Failed to include kdr"); rv = ACVP_MISSING_ARG; goto err; } ACVP_LOG_INFO("\n Test group: %d", i); ACVP_LOG_INFO(" kdr: %s", kdr); ACVP_LOG_INFO(" key length: %d", aes_key_length); tests = json_object_get_array(groupobj, "tests"); t_cnt = json_array_get_count(tests); for (j = 0; j < t_cnt; j++) { ACVP_LOG_INFO("Found new KDF SRTP test vector..."); testval = json_array_get_value(tests, j); testobj = json_value_get_object(testval); tc_id = (unsigned int)json_object_get_number(testobj, "tcId"); master_key = (char *)json_object_get_string(testobj, "masterKey"); if (!master_key) { ACVP_LOG_ERR("Failed to include JSON key:\"masterKey\""); rv = ACVP_MISSING_ARG; goto err; } master_salt = (char *)json_object_get_string(testobj, "masterSalt"); if (!master_salt) { ACVP_LOG_ERR("Failed to include JSON key:\"masterSalt\""); rv = ACVP_MISSING_ARG; goto err; } index = (char *)json_object_get_string(testobj, "index"); if (!index) { ACVP_LOG_ERR("Failed to include JSON key:\"index\""); rv = ACVP_MISSING_ARG; goto err; } srtcp_index = (char *)json_object_get_string(testobj, "srtcpIndex"); if (!srtcp_index) { ACVP_LOG_ERR("Failed to include JSON key:\"srtcpIndex\""); rv = ACVP_MISSING_ARG; goto err; } ACVP_LOG_INFO(" Test case: %d", j); ACVP_LOG_INFO(" tcId: %d", tc_id); ACVP_LOG_INFO(" masterKey: %s", master_key); ACVP_LOG_INFO(" masterSalt: %s", master_salt); ACVP_LOG_INFO(" index: %s", index); ACVP_LOG_INFO(" srtcpIndex: %s", srtcp_index); /* * Create a new test case in the response */ r_tval = json_value_init_object(); r_tobj = json_value_get_object(r_tval); json_object_set_number(r_tobj, "tcId", tc_id); /* * Setup the test case data that will be passed down to * the crypto module. */ rv = acvp_kdf135_srtp_init_tc(ctx, &stc, tc_id, aes_key_length, kdr, master_key, master_salt, index, srtcp_index); if (rv != ACVP_SUCCESS) { acvp_kdf135_srtp_release_tc(&stc); json_value_free(r_tval); goto err; } /* Process the current test vector... */ if ((cap->crypto_handler)(&tc)) { ACVP_LOG_ERR("crypto module failed"); acvp_kdf135_srtp_release_tc(&stc); rv = ACVP_CRYPTO_MODULE_FAIL; json_value_free(r_tval); goto err; } /* * Output the test case results using JSON */ rv = acvp_kdf135_srtp_output_tc(ctx, &stc, r_tobj); if (rv != ACVP_SUCCESS) { ACVP_LOG_ERR("JSON output failure"); acvp_kdf135_srtp_release_tc(&stc); json_value_free(r_tval); goto err; } /* * Release all the memory associated with the test case */ acvp_kdf135_srtp_release_tc(&stc); /* Append the test response value to array */ json_array_append_value(r_tarr, r_tval); } json_array_append_value(r_garr, r_gval); } json_array_append_value(reg_arry, r_vs_val); json_result = json_serialize_to_string_pretty(ctx->kat_resp, NULL); if (ctx->debug == ACVP_LOG_LVL_VERBOSE) { printf("\n\n%s\n\n", json_result); } else { ACVP_LOG_INFO("\n\n%s\n\n", json_result); } json_free_serialized_string(json_result); rv = ACVP_SUCCESS; err: if (rv != ACVP_SUCCESS) { acvp_release_json(r_vs_val, r_gval); } return rv; }