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); }
char* querySpecification_serializeToJson(const PROVISIONING_QUERY_SPECIFICATION* query_spec) { char* result = NULL; char* serialized_string = NULL; JSON_Value* root_value = NULL; if (query_spec == NULL) { LogError("Invalid query specification"); } else if (query_spec->version != PROVISIONING_QUERY_SPECIFICATION_VERSION_1) { LogError("Invalid version"); } else if ((root_value = querySpecification_toJson(query_spec)) == NULL) { LogError("Creating json object failed"); } else if ((serialized_string = json_serialize_to_string(root_value)) == NULL) { LogError("Failed to serialize to JSON"); } else if (mallocAndStrcpy_s(&result, serialized_string) != 0) { LogError("Failed to copy serialized string"); } if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } if (serialized_string != NULL) { json_free_serialized_string(serialized_string); serialized_string = NULL; } return result; }
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; } }
static MODULE_HANDLE NativeModuleHost_Create(BROKER_HANDLE broker, const void* configuration) { MODULE_HOST * result; if (broker == NULL || configuration == NULL) { /*Codes_SRS_NATIVEMODULEHOST_17_008: [ NativeModuleHost_Create shall return NULL if broker is NULL. ]*/ /*Codes_SRS_NATIVEMODULEHOST_17_009: [ NativeModuleHost_Create shall return NULL if configuration does not contain valid JSON. ]*/ LogError("broker [%p] or configuration [%p] is NULL, both are required", broker, configuration); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_010: [ NativeModuleHost_Create shall intialize the Module_Loader. ]*/ if (ModuleLoader_Initialize() != MODULE_LOADER_SUCCESS) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("ModuleLoader_Initialize failed"); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_011: [ NativeModuleHost_Create shall parse the configuration JSON. ]*/ char * outprocess_module_args = (char *)configuration; JSON_Value *root_value = json_parse_string(outprocess_module_args); JSON_Object * module_host_args; if ((root_value == NULL) || ((module_host_args = json_value_get_object(root_value)) == NULL)) { if (root_value != NULL) { json_value_free(root_value); } LogError("NativeModuleHost_Create could not parse arguments as JSON"); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_035: [ If the "outprocess.loaders" array exists in the configuration JSON, NativeModuleHost_Create shall initialize the Module_Loader from this array. ]*/ JSON_Value * loaders_array = json_object_get_value(module_host_args, OOP_MODULE_LOADERS_ARRAY_KEY); if ((loaders_array != NULL) && (ModuleLoader_InitializeFromJson(loaders_array) != MODULE_LOADER_SUCCESS)) { LogError("NativeModuleHost_Create could not extract loaders array from module arguments."); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_012: [ NativeModuleHost_Create shall get the "outprocess.loader" object from the configuration JSON. ]*/ JSON_Object * loader_args = json_object_get_object(module_host_args, OOP_MODULE_LOADER_KEY); if (loader_args == NULL) { LogError("NativeModuleHost_Create could not get loader arguments."); result = NULL; } else { GATEWAY_MODULE_LOADER_INFO loader_info; if (parse_loader(loader_args, &loader_info) != 0) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("NativeModuleHost_Create could not extract loader information from loader arguments."); result = NULL; } else { // Have loader and entrypoint now, get module. result = (MODULE_HOST*)malloc(sizeof(MODULE_HOST)); if (result == NULL) { LogError("NativeModuleHost_Create could not allocate module."); result = NULL; } else { /*Codes_SRS_NATIVEMODULEHOST_17_018: [ NativeModuleHost_Create shall get the "module.args" object from the configuration JSON. ]*/ JSON_Value * module_args = json_object_get_value(module_host_args, OOP_MODULE_ARGS_KEY); char * module_args_string = json_serialize_to_string(module_args); if (module_create(result, broker, &loader_info, module_args_string) != 0) { /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ LogError("NativeModuleHost_Create could not load module."); free(result); result = NULL; } /*Codes_SRS_NATIVEMODULEHOST_17_024: [ NativeModuleHost_Create shall free all resources used during module loading. ]*/ json_free_serialized_string(module_args_string); } loader_info.loader->api->FreeEntrypoint(loader_info.loader, loader_info.entrypoint); } } } /*Codes_SRS_NATIVEMODULEHOST_17_024: [ NativeModuleHost_Create shall free all resources used during module loading. ]*/ json_value_free(root_value); } if (result == NULL) { // failed to create a module, give up entirely. /*Codes_SRS_NATIVEMODULEHOST_17_026: [ If any step above fails, then NativeModuleHost_Create shall free all resources allocated and return NULL. ]*/ ModuleLoader_Destroy(); } } } return result; }
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; }
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; }
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; }
static ACVP_RESULT execute_network_action(ACVP_CTX *ctx, ACVP_NET_ACTION action, char *url, char *data, int data_len, int *curl_code) { ACVP_RESULT result = 0; char *resp = NULL; char large_url[ACVP_ATTR_URL_MAX + 1] = {0}; int large_submission = 0; int resp_len = 0; int rc = 0; switch(action) { case ACVP_NET_GET: case ACVP_NET_GET_VS: case ACVP_NET_GET_VS_RESULT: case ACVP_NET_GET_VS_SAMPLE: rc = acvp_curl_http_get(ctx, url); break; case ACVP_NET_POST: case ACVP_NET_POST_LOGIN: case ACVP_NET_POST_REG: rc = acvp_curl_http_post(ctx, url, data, data_len); break; case ACVP_NET_POST_VS_RESP: resp = json_serialize_to_string(ctx->kat_resp, &resp_len); if (!resp) { ACVP_LOG_ERR("Faled to serialize JSON to string"); return ACVP_JSON_ERR; } json_value_free(ctx->kat_resp); ctx->kat_resp = NULL; if (ctx->post_size_constraint && resp_len > ctx->post_size_constraint) { /* Determine if this POST body goes over the "constraint" */ large_submission = 1; } if (large_submission) { /* * Need to tell the server about this large submission. * The server will supply us with a one-time "large" URL; */ result = acvp_notify_large(ctx, url, large_url, resp_len); if (result != ACVP_SUCCESS) goto end; rc = acvp_curl_http_post(ctx, large_url, resp, resp_len); } else { rc = acvp_curl_http_post(ctx, url, resp, resp_len); } break; default: ACVP_LOG_ERR("Unknown ACVP_NET_ACTION"); return ACVP_INVALID_ARG; } /* Peek at the HTTP code */ result = inspect_http_code(ctx, rc); if (result != ACVP_SUCCESS) { if (result == ACVP_JWT_EXPIRED && action != ACVP_NET_POST_LOGIN) { /* * Expired JWT * We are going to refresh the session * and try to obtain a new JWT! * This should not ever happen during "login"... * and we need to avoid an infinite loop (via acvp_refesh). */ ACVP_LOG_ERR("JWT authorization has timed out, curl rc=%d.\n" "Refreshing session...", rc); result = acvp_refresh(ctx); if (result != ACVP_SUCCESS) { ACVP_LOG_ERR("JWT refresh failed."); goto end; } /* Try action again after the refresh */ switch(action) { case ACVP_NET_GET: case ACVP_NET_GET_VS: case ACVP_NET_GET_VS_RESULT: case ACVP_NET_GET_VS_SAMPLE: rc = acvp_curl_http_get(ctx, url); break; case ACVP_NET_POST: case ACVP_NET_POST_REG: rc = acvp_curl_http_post(ctx, url, data, data_len); break; case ACVP_NET_POST_VS_RESP: if (large_submission) { rc = acvp_curl_http_post(ctx, large_url, resp, resp_len); } else { rc = acvp_curl_http_post(ctx, url, resp, resp_len); } break; case ACVP_NET_POST_LOGIN: ACVP_LOG_ERR("We should never be here!"); break; } result = inspect_http_code(ctx, rc); if (result != ACVP_SUCCESS) { ACVP_LOG_ERR("Refreshed + retried, HTTP transport fails. curl rc=%d\n", rc); goto end; } } else if (result == ACVP_JWT_INVALID) { /* * Invalid JWT */ ACVP_LOG_ERR("JWT invalid. curl rc=%d.\n", rc); goto end; } /* Generic error */ goto end; } result = ACVP_SUCCESS; end: if (resp) json_free_serialized_string(resp); *curl_code = rc; return result; }