Пример #1
0
static IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE CloneDiagnosticPropertyData(const IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA* source)
{
    IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE result = NULL;
    if (source == NULL)
    {
        LogError("Invalid argument - source is NULL");
    }
    else
    {
        result = (IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA_HANDLE)malloc(sizeof(IOTHUB_MESSAGE_DIAGNOSTIC_PROPERTY_DATA));
        if (result == NULL)
        {
            LogError("malloc failed");
        }
        else
        {
            result->diagnosticCreationTimeUtc = NULL;
            result->diagnosticId = NULL;
            if (source->diagnosticCreationTimeUtc != NULL && mallocAndStrcpy_s(&result->diagnosticCreationTimeUtc, source->diagnosticCreationTimeUtc) != 0)
            {
                LogError("mallocAndStrcpy_s for diagnosticCreationTimeUtc failed");
                free(result);
                result = NULL;
            }
            else if (source->diagnosticId != NULL && mallocAndStrcpy_s(&result->diagnosticId, source->diagnosticId) != 0)
            {
                LogError("mallocAndStrcpy_s for diagnosticId failed");
                free(result->diagnosticCreationTimeUtc);
                free(result);
                result = NULL;
            }
        }
    }
    return result;
}
Пример #2
0
static int insertNewKeyValue(MAP_HANDLE_DATA* handleData, const char* key, const char* value)
{
    int result;
    if (Map_IncreaseStorageKeysValues(handleData) != 0) /*this increases handleData->count*/
    {
        result = __LINE__;
    }
    else
    {
        if (mallocAndStrcpy_s(&(handleData->keys[handleData->count - 1]), key) != 0)
        {
            Map_DecreaseStorageKeysValues(handleData);
            LogError("unable to mallocAndStrcpy_s\r\n");
            result = __LINE__;
        }
        else
        {
            if (mallocAndStrcpy_s(&(handleData->values[handleData->count - 1]), value) != 0)
            {
                free(handleData->keys[handleData->count - 1]);
                Map_DecreaseStorageKeysValues(handleData);
                LogError("unable to mallocAndStrcpy_s\r\n");
                result = __LINE__;
            }
            else
            {
                result = 0;
            }
        }
    }
    return result; 
}
static JVM_OPTIONS* options_copy(JVM_OPTIONS* options)
{
    //Copy the VECTOR_HANDLE
    JVM_OPTIONS* new_options = (JVM_OPTIONS*)malloc(sizeof(JVM_OPTIONS));
    if (new_options == NULL)
    {
        LogError("Failed to allocate memory for JVM_OPTIONS structure.");
    }
    else
    {
        new_options->additional_options = NULL;
        if (options->additional_options != NULL)
        {
            new_options->additional_options = VECTOR_copy(options->additional_options);
        }

        if (options->additional_options != NULL && new_options->additional_options == NULL)
        {
            LogError("Failed to copy additional_options VECTOR_HANDLE.");
            free(new_options);
            new_options = NULL;
        }
        else
        {
            //Copy the class path
            int status = mallocAndStrcpy_s((char**)(&new_options->class_path), options->class_path);
            if (status != 0)
            {
                LogError("Failed to allocate class_path.");
                VECTOR_destroy(new_options->additional_options);
                free(new_options);
                new_options = NULL;
            }
            else
            {
                //Copy the library path
                status = mallocAndStrcpy_s((char**)(&new_options->library_path), options->library_path);
                if (status != 0)
                {
                    LogError("Failed to allocate library_path.");
                    free((void*)new_options->class_path);
                    VECTOR_destroy(new_options->additional_options);
                    free(new_options);
                    new_options = NULL;
                }
                else
                {
                    new_options->debug = options->debug;
                    new_options->debug_port = options->debug_port;
                    new_options->verbose = options->verbose;
                    new_options->version = options->version;
                }
            }
        }
    }
    return new_options;
}
IOTHUB_AUTHORIZATION_HANDLE IoTHubClient_Auth_Create(const char* device_key, const char* device_id, const char* device_sas_token, const char *module_id)
{
    IOTHUB_AUTHORIZATION_DATA* result;
    /* Codes_SRS_IoTHub_Authorization_07_001: [if device_id is NULL IoTHubClient_Auth_Create, shall return NULL. ] */
    if (device_id == NULL)
    {
        LogError("Invalid Parameter device_id: %p", device_id);
        result = NULL;
    }
    else
    {
        result = initialize_auth_client(device_id, module_id);
        if (result == NULL)
        {
            LogError("Failure initializing auth client");
        }
        else if (device_key != NULL && mallocAndStrcpy_s(&result->device_key, device_key) != 0)
        {
            /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
            LogError("Failed allocating device_key");
            free(result->device_id);
            free(result->module_id);
            free(result);
            result = NULL;
        }
        else
        {
            if (device_key != NULL)
            {
                /* Codes_SRS_IoTHub_Authorization_07_003: [ IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY if the device_sas_token is NULL. ]*/
                result->cred_type = IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY;
            }
            else if (device_sas_token != NULL)
            {
                /* Codes_SRS_IoTHub_Authorization_07_020: [ else IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN. ] */
                result->cred_type = IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN;
                if (mallocAndStrcpy_s(&result->device_sas_token, device_sas_token) != 0)
                {
                    /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
                    LogError("Failed allocating device_key");
                    free(result->device_key);
                    free(result->device_id);
                    free(result->module_id);
                    free(result);
                    result = NULL;
                }
            }
            else
            {
                /* Codes_SRS_IoTHub_Authorization_07_024: [ if device_sas_token and device_key are NULL IoTHubClient_Auth_Create shall set the credential type to IOTHUB_CREDENTIAL_TYPE_UNKNOWN. ] */
                result->cred_type = IOTHUB_CREDENTIAL_TYPE_UNKNOWN;
            }
        }
    }
    /* Codes_SRS_IoTHub_Authorization_07_004: [ If successful IoTHubClient_Auth_Create shall return a IOTHUB_AUTHORIZATION_HANDLE value. ] */
    return result;
}
int IoTHubClient_Auth_Get_x509_info(IOTHUB_AUTHORIZATION_HANDLE handle, char** x509_cert, char** x509_key)
{
    int result;
    if (handle == NULL || x509_cert == NULL || x509_key == NULL)
    {
        LogError("Invalid Parameter handle: %p, x509_cert: %p, x509_key: %p", handle, x509_cert, x509_key);
        result = __FAILURE__;
    }
    else if (handle->cred_type != IOTHUB_CREDENTIAL_TYPE_X509_ECC)
    {
        LogError("Invalid credential types for this operation");
        result = __FAILURE__;
    }
    else
    {
#ifdef USE_PROV_MODULE
        CREDENTIAL_RESULT* cred_result = iothub_device_auth_generate_credentials(handle->device_auth_handle, NULL);
        if (cred_result == NULL)
        {
            LogError("Failure generating credentials");
            result = __FAILURE__;
        }
        else
        {
            if (mallocAndStrcpy_s(x509_cert, cred_result->auth_cred_result.x509_result.x509_cert) != 0)
            {
                LogError("Failure copying certificate");
                result = __FAILURE__;
            }
            else if (mallocAndStrcpy_s(x509_key, cred_result->auth_cred_result.x509_result.x509_alias_key) != 0)
            {
                LogError("Failure copying private key");
                result = __FAILURE__;
                free(*x509_cert);
            }
            else
            {
                result = 0;
            }
            free(cred_result);
        }
#else
        LogError("Failed HSM module is not supported");
        result = __FAILURE__;
#endif
    }
    return result;
}
 static void setup_dev_auth_emulator_generate_credentials_mocks(const char* token_scope)
 {
     STRICT_EXPECTED_CALL(STRING_new());
     STRICT_EXPECTED_CALL(get_time(IGNORED_PTR_ARG));
     STRICT_EXPECTED_CALL(STRING_construct(token_scope));
     STRICT_EXPECTED_CALL(STRING_construct(IGNORED_PTR_ARG))
         .IgnoreArgument_psz();
     STRICT_EXPECTED_CALL(SASToken_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG))
         .IgnoreArgument_scope()
         .IgnoreArgument_keyName()
         .IgnoreArgument_expiry()
         .IgnoreArgument_key();
     STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG))
         .IgnoreArgument_handle();
     STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
         .IgnoreArgument_destination()
         .IgnoreArgument_source();
     STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG))
         .IgnoreArgument_handle();
     STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG))
         .IgnoreArgument_handle();
     STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG))
         .IgnoreArgument_handle();
     STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG))
         .IgnoreArgument_handle();
 }
Пример #7
0
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetMessageId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* messageId)
{
    IOTHUB_MESSAGE_RESULT result;
    /* Codes_SRS_IOTHUBMESSAGE_07_012: [if any of the parameters are NULL then IoTHubMessage_SetMessageId shall return a IOTHUB_MESSAGE_INVALID_ARG value.] */
    if (iotHubMessageHandle == NULL || messageId == NULL)
    {
        LogError("invalid arg (NULL) passed to IoTHubMessage_SetMessageId\r\n");
        result = IOTHUB_MESSAGE_INVALID_ARG;
    }
    else
    {
        IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
        /* Codes_SRS_IOTHUBMESSAGE_07_013: [If the IOTHUB_MESSAGE_HANDLE messageId is not NULL, then the IOTHUB_MESSAGE_HANDLE messageId will be freed] */
        if (handleData->messageId != NULL)
        {
            free(handleData->messageId);
        }

        /* Codes_SRS_IOTHUBMESSAGE_07_014: [If the allocation or the copying of the messageId fails, then IoTHubMessage_SetMessageId shall return IOTHUB_MESSAGE_ERROR.] */
        if (mallocAndStrcpy_s(&handleData->messageId, messageId) != 0)
        {
            result = IOTHUB_MESSAGE_ERROR;
        }
        else
        {
            result = IOTHUB_MESSAGE_OK;
        }
    }
    return result;
}
Пример #8
0
int prov_auth_set_registration_id(PROV_AUTH_HANDLE handle, const char* registration_id)
{
    int result;
    if (handle == NULL || registration_id == NULL)
    {
        LogError("Invalid parameter handle: %p, registration_id: %p", handle, registration_id);
        result = MU_FAILURE;
    }
    else
    {
        if (handle->registration_id != NULL)
        {
            LogError("Registration_id has been previously set, registration can not be changed");
            result = MU_FAILURE;
        }
        else if (mallocAndStrcpy_s(&handle->registration_id, registration_id) != 0)
        {
            LogError("Failed allocating registration key");
            result = MU_FAILURE;
        }
        else
        {
            result = 0;
        }
    }
    return result;
}
int prov_transport_common_mqtt_set_trusted_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate)
{
    int result;
    if (handle == NULL || certificate == NULL)
    {
        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_030: [ If handle or certificate is NULL, prov_transport_common_mqtt_set_trusted_cert shall return a non-zero value. ] */
        LogError("Invalid parameter specified handle: %p, certificate: %p", handle, certificate);
        result = __FAILURE__;
    }
    else
    {
        PROV_TRANSPORT_MQTT_INFO* mqtt_info = (PROV_TRANSPORT_MQTT_INFO*)handle;

        if (mqtt_info->certificate != NULL)
        {
            free(mqtt_info->certificate);
            mqtt_info->certificate = NULL;
        }

        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_031: [ prov_transport_common_mqtt_set_trusted_cert shall copy the certificate value. ] */
        if (mallocAndStrcpy_s(&mqtt_info->certificate, certificate) != 0)
        {
            /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_032: [ On any failure prov_transport_common_mqtt_set_trusted_cert, shall return a non-zero value. ] */
            result = __FAILURE__;
            LogError("failure allocating certificate");
        }
        else
        {
            /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_033: [ On success prov_transport_common_mqtt_set_trusted_cert shall return a zero value. ] */
            result = 0;
        }
    }
    return result;
}
Пример #10
0
static char* encode_value(const uint8_t* msg_digest, size_t digest_len)
{
    char* result;

    char* encoded_hash = Base32_Encode_Bytes(msg_digest, digest_len);
    if (encoded_hash == NULL)
    {
        LogError("Failure encoded Base32");
        result = NULL;
    }
    else
    {
        size_t encode_len = strlen(encoded_hash);
        char* iterator = encoded_hash + encode_len - 1;

        while (iterator != encoded_hash)
        {
            if (*iterator != '=')
            {
                *(iterator+1) = '\0';
                break;
            }
            iterator--;
        }

        if (mallocAndStrcpy_s(&result, encoded_hash) != 0)
        {
            LogError("Failure allocating encoded base32 result");
            result = NULL;
        }
        free(encoded_hash);
    }
    return result;
}
STRING_TOKENIZER_HANDLE STRING_TOKENIZER_create(STRING_HANDLE handle)
{
    STRING_TOKEN *result;
    char* inputStringToMalloc;

    /* Codes_SRS_STRING_04_001: [STRING_TOKENIZER_create shall return an NULL STRING_TOKENIZER_HANDLE if parameter handle is NULL] */
    if (handle == NULL)
    {
        LogError("Invalid Argument. Handle cannot be NULL.\r\n");
        result = NULL;
    }
    /* Codes_SRS_STRING_04_002: [STRING_TOKENIZER_create shall allocate a new STRING_TOKENIZER_HANDLE having the content of the STRING_HANDLE copied and current position pointing at the beginning of the string] */
    else if((result = (STRING_TOKEN *)malloc(sizeof(STRING_TOKEN))) == NULL)
    {
        LogError("Memory Allocation failed. Cannot allocate STRING_TOKENIZER.\r\n");
    }
    else if ((mallocAndStrcpy_s(&inputStringToMalloc, STRING_c_str(handle))) != 0)
    {
        LogError("Memory Allocation Failed. Cannot allocate and copy string Content.\r\n");
        free(result);
        result = NULL;
    }
    else
    {
        result->inputString = inputStringToMalloc;
        result->currentPos = result->inputString; //Current Pos will point to the initial position of Token.
        result->sizeOfinputString = strlen(result->inputString); //Calculate Size of Current String
    }
    
    return (STRING_TOKENIZER_HANDLE)result;
}
Пример #12
0
IOTHUB_MESSAGE_RESULT IoTHubMessage_SetCorrelationId(IOTHUB_MESSAGE_HANDLE iotHubMessageHandle, const char* correlationId)
{
    IOTHUB_MESSAGE_RESULT result;
    /* Codes_SRS_IOTHUBMESSAGE_07_018: [if any of the parameters are NULL then IoTHubMessage_SetCorrelationId shall return a IOTHUB_MESSAGE_INVALID_ARG value.]*/
    if (iotHubMessageHandle == NULL || correlationId == NULL)
    {
        LogError("invalid arg (NULL) passed to IoTHubMessage_SetCorrelationId\r\n");
        result = IOTHUB_MESSAGE_INVALID_ARG;
    }
    else
    {
        IOTHUB_MESSAGE_HANDLE_DATA* handleData = iotHubMessageHandle;
        /* Codes_SRS_IOTHUBMESSAGE_07_019: [If the IOTHUB_MESSAGE_HANDLE correlationId is not NULL, then the IOTHUB_MESSAGE_HANDLE correlationId will be deallocated.] */
        if (handleData->correlationId != NULL)
        {
            free(handleData->correlationId);
        }

        if (mallocAndStrcpy_s(&handleData->correlationId, correlationId) != 0)
        {
            /* Codes_SRS_IOTHUBMESSAGE_07_020: [If the allocation or the copying of the correlationId fails, then IoTHubMessage_SetCorrelationId shall return IOTHUB_MESSAGE_ERROR.] */
            result = IOTHUB_MESSAGE_ERROR;
        }
        else
        {
            /* Codes_SRS_IOTHUBMESSAGE_07_021: [IoTHubMessage_SetCorrelationId finishes successfully it shall return IOTHUB_MESSAGE_OK.] */
            result = IOTHUB_MESSAGE_OK;
        }
    }
    return result;
}
int prov_transport_common_mqtt_x509_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate, const char* private_key)
{
    int result;
    if (handle == NULL || certificate == NULL)
    {
        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_026: [ If handle or certificate is NULL, prov_transport_common_mqtt_x509_cert shall return a non-zero value. ] */
        LogError("Invalid parameter specified handle: %p, certificate: %p", handle, certificate);
        result = __FAILURE__;
    }
    else
    {
        PROV_TRANSPORT_MQTT_INFO* mqtt_info = (PROV_TRANSPORT_MQTT_INFO*)handle;

        if (mqtt_info->x509_cert != NULL)
        {
            free(mqtt_info->x509_cert);
            mqtt_info->x509_cert = NULL;
        }
        if (mqtt_info->private_key != NULL)
        {
            free(mqtt_info->private_key);
            mqtt_info->private_key = NULL;
        }

        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_027: [ prov_transport_common_mqtt_x509_cert shall copy the certificate and private_key values. ] */
        if (mallocAndStrcpy_s(&mqtt_info->x509_cert, certificate) != 0)
        {
            /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_028: [ On any failure prov_transport_common_mqtt_x509_cert, shall return a non-zero value. ] */
            result = __FAILURE__;
            LogError("failure allocating certificate");
        }
        else if (mallocAndStrcpy_s(&mqtt_info->private_key, private_key) != 0)
        {
            /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_028: [ On any failure prov_transport_common_mqtt_x509_cert, shall return a non-zero value. ] */
            LogError("failure allocating certificate");
            free(mqtt_info->x509_cert);
            mqtt_info->x509_cert = NULL;
            result = __FAILURE__;
        }
        else
        {
            /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_029: [ On success prov_transport_common_mqtt_x509_cert shall return a zero value. ] */
            result = 0;
        }
    }
    return result;
}
Пример #14
0
static MODULE_HANDLE SimulatorModule_Create(BROKER_HANDLE broker, const void* configuration)
{
    SIMULATOR_MODULE_HANDLE * module;

    if ((broker == NULL) ||
        (configuration == NULL))
    {
        LogError("Simulator had a null input. broker: [%p], configuration: [%p]", broker, configuration);
        module = NULL;
    }
    else
    {
        SIMULATOR_MODULE_CONFIG * conf = (SIMULATOR_MODULE_CONFIG *)configuration;
        module = (SIMULATOR_MODULE_HANDLE*)malloc(sizeof(SIMULATOR_MODULE_HANDLE));
        if (module == NULL)
        {
            LogError("Could not allocate memory for module handle");
        }
        else
        {
            if (mallocAndStrcpy_s(&(module->device_id), conf->device_id) != 0)
            {
                LogError("could not allocate memory for deviceID string");
                free(module);
                module = NULL;
            }
            else
            {
                module->broker = broker;
                module->message_delay = conf->message_delay;
                module->message_size = conf->message_size;
                module->properties_count = conf->properties_count;
                module->properties_size = conf->properties_size;
                size_t max_buffer = std::max(module->properties_size, module->message_size);
                module->psuedo_random_buffer = (char*)malloc(max_buffer + 1);
                if (module->psuedo_random_buffer == NULL)
                {
                    LogError("could not allocate memory for deviceID string");
                    free(module->device_id);
                    free(module);
                    module = NULL;
                }
                else
                {
                    std::default_random_engine generator;
                    std::uniform_int_distribution<int> distribution(' ', 'z');
                    for (size_t i = 0; i < max_buffer; i++)
                    {
                        (module->psuedo_random_buffer)[i] = distribution(generator);
                    }
                    (module->psuedo_random_buffer)[max_buffer] = '\0';
                }
            }
        }
    }
    return (MODULE_HANDLE)module;
}
/* Codes_SRS_TLSIO_SSL_ESP8266_99_017: [ The tlsio_openssl_create shall receive the connection configuration (TLSIO_CONFIG). ]*/
CONCRETE_IO_HANDLE tlsio_openssl_create(void* io_create_parameters)
{
    //LogInfo("tlsio_openssl_create begin: %d", system_get_free_heap_size());
    /* Codes_SRS_TLSIO_SSL_ESP8266_99_005: [ The tlsio_ssl_esp8266 shall received the connection information using the TLSIO_CONFIG structure defined in `tlsio.h`. ]*/
    /* Codes_SRS_TLSIO_SSL_ESP8266_99_017: [ The tlsio_openssl_create shall receive the connection configuration (TLSIO_CONFIG). ]*/
    TLSIO_CONFIG* tls_io_config = (TLSIO_CONFIG*)io_create_parameters;
    TLS_IO_INSTANCE* result;

    /* Codes_SRS_TLSIO_SSL_ESP8266_99_013: [ The tlsio_openssl_create shall return NULL when io_create_parameters is NULL. ]*/
    if (tls_io_config == NULL)
    {
        result = NULL;
        LogError("NULL tls_io_config.");
    }
    else
    {
        /* Codes_SRS_TLSIO_SSL_ESP8266_99_011: [ The tlsio_openssl_create shall allocate memory to control the tlsio instance. ]*/
        result = (TLS_IO_INSTANCE*) malloc(sizeof(TLS_IO_INSTANCE));

        if (result == NULL)
        {
            /* Codes_SRS_TLSIO_SSL_ESP8266_99_012: [ If there is not enough memory to create the tlsio, the tlsio_openssl_create shall return NULL as the handle. ]*/
            LogError("Failed allocating TLSIO instance.");
        }
        else
        {
            memset(result, 0, sizeof(TLS_IO_INSTANCE));
            mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname);
            result->port = (int)tls_io_config->port;
            result->ssl_context = NULL;
            result->ssl = NULL;
            result->certificate = NULL;

            /* Codes_SRS_TLSIO_SSL_ESP8266_99_016: [ The tlsio_openssl_create shall initialize all callback pointers as NULL. ]*/
            result->on_bytes_received = NULL;
            result->on_bytes_received_context = NULL;

            result->on_io_open_complete = NULL;
            result->on_io_open_complete_context = NULL;

            result->on_io_close_complete = NULL;
            result->on_io_close_complete_context = NULL;

            result->on_io_error = NULL;
            result->on_io_error_context = NULL;

            /* Codes_SRS_TLSIO_SSL_ESP8266_99_020: [ If tlsio_openssl_create get success to create the tlsio instance, it shall set the tlsio state as TLSIO_STATE_NOT_OPEN. ]*/
            result->tlsio_state = TLSIO_STATE_NOT_OPEN;

            result->x509certificate = NULL;
            result->x509privatekey = NULL;
        }
    }

    /* Codes_SRS_TLSIO_SSL_ESP8266_99_010: [ The tlsio_openssl_create shall return a non-NULL handle on success. ]*/
    return (CONCRETE_IO_HANDLE)result;
}
Пример #16
0
/*this function will clone an option given by name and value*/
static void* tlsio_schannel_CloneOption(const char* name, const void* value)
{
    void* result;
    if (
        (name == NULL) || (value == NULL)
        )
    {
        LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value);
        result = NULL;
    }
    else
    {
        if (strcmp(name, "x509certificate") == 0)
        {
			if (mallocAndStrcpy_s((char**)&result, (const char *) value) != 0)
            {
                LogError("unable to mallocAndStrcpy_s x509certificate value");
                result = NULL;
            }
            else
            {
                /*return as is*/
            }
        }
        else if (strcmp(name, "x509privatekey") == 0)
        {
			if (mallocAndStrcpy_s((char**)&result, (const char *) value) != 0)
            {
                LogError("unable to mallocAndStrcpy_s x509privatekey value");
                result = NULL;
            }
            else
            {
                /*return as is*/
            }
        }
        else
        {
            LogError("not handled option : %s", name);
            result = NULL;
        }
    }
    return result;
}
Пример #17
0
/*
 * @brief	Duplicate and copy all fields in the IDENTITY_MAP_CONFIG struct.
 *			Forces MAC address to be all upper case (to guarantee comparisons are consistent).
 */
static IDENTITYMAP_RESULT IdentityMapConfig_CopyDeep(IDENTITY_MAP_CONFIG * dest, IDENTITY_MAP_CONFIG * source)
{
	IDENTITYMAP_RESULT result;
	int status;
	dest->macAddress = IdentityMapConfig_ToUpperCase(source->macAddress);
	if (dest->macAddress == NULL)
	{
		LogError("Unable to allocate macAddress");
		result = IDENTITYMAP_MEMORY;
	}
	else
	{
		char * temp;
		status = mallocAndStrcpy_s(&temp, source->deviceId);
		if (status != 0)
		{
			LogError("Unable to allocate deviceId");
			free((void*)dest->macAddress);
			dest->macAddress = NULL;
			result = IDENTITYMAP_MEMORY;
		}
		else
		{
			dest->deviceId = temp;
			status = mallocAndStrcpy_s(&temp, source->deviceKey);
			if (status != 0)
			{
				LogError("Unable to allocate deviceKey");
				free((void*)dest->deviceId);
				dest->deviceId = NULL;
				free((void*)dest->macAddress);
				dest->macAddress = NULL;
				result = IDENTITYMAP_MEMORY;
			}
			else
			{
				dest->deviceKey = temp;
				result = IDENTITYMAP_OK;
			}
		}
	}
	return result;
}
static JAVA_MODULE_HOST_CONFIG* config_copy(JAVA_MODULE_HOST_CONFIG* config)
{
    JAVA_MODULE_HOST_CONFIG* new_config = malloc(sizeof(JAVA_MODULE_HOST_CONFIG));
    if (new_config == NULL)
    {
        LogError("Failed to allocate memory for JAVA_MODULE_HOST_CONFIG structure.");
    }
    else
    {
        //Copy the class name
        int status = mallocAndStrcpy_s((char**)(&new_config->class_name), config->class_name);
        if (status != 0)
        {
            LogError("Failed to allocate class_name.");
            free(new_config);
            new_config = NULL;
        }
        else
        {
            //Copy the configuration
            status = mallocAndStrcpy_s((char**)(&new_config->configuration_json), config->configuration_json);
            if (status != 0)
            {
                LogError("Failed to allocate configuration_json.");
                free((void*)new_config->class_name);
                free(new_config);
                new_config = NULL;
            }
            else
            {
                new_config->options = options_copy(config->options);
                if (new_config->options == NULL)
                {
                    free((void*)new_config->class_name);
                    free((void*)new_config->configuration_json);
                    free(new_config);
                    new_config = NULL;
                }
            }
        }
    }
    return new_config;
}
Пример #19
0
static void iothub_prov_register_device(PROV_DEVICE_RESULT register_result, const char* iothub_uri, const char* device_id, void* user_context)
{
    if (user_context == NULL)
    {
        ASSERT_FAIL("User Context is NULL iothub_prov_register_device");
    }
    else
    {
        PROV_CLIENT_E2E_INFO* prov_info = (PROV_CLIENT_E2E_INFO*)user_context;
        if (register_result == PROV_DEVICE_RESULT_OK)
        {
            (void)mallocAndStrcpy_s(&prov_info->iothub_uri, iothub_uri);
            (void)mallocAndStrcpy_s(&prov_info->device_id, device_id);
            prov_info->reg_result = REG_RESULT_COMPLETE;
        }
        else
        {
            prov_info->reg_result = REG_RESULT_FAILED;
        }
    }
}
int prov_transport_common_mqtt_open(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* registration_id, BUFFER_HANDLE ek, BUFFER_HANDLE srk, PROV_DEVICE_TRANSPORT_REGISTER_CALLBACK data_callback, void* user_ctx, PROV_DEVICE_TRANSPORT_STATUS_CALLBACK status_cb, void* status_ctx)
{
    int result;
    PROV_TRANSPORT_MQTT_INFO* mqtt_info = (PROV_TRANSPORT_MQTT_INFO*)handle;
    if (mqtt_info == NULL || data_callback == NULL || status_cb == NULL || registration_id == NULL)
    {
        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_007: [ If handle, data_callback, or status_cb is NULL, prov_transport_common_mqtt_open shall return a non-zero value. ] */
        LogError("Invalid parameter specified handle: %p, data_callback: %p, status_cb: %p, registration_id: %p", handle, data_callback, status_cb, registration_id);
        result = __FAILURE__;
    }
    else if ( (mqtt_info->hsm_type == TRANSPORT_HSM_TYPE_TPM) && (ek == NULL || srk == NULL) )
    {
        /* Codes_PROV_TRANSPORT_MQTT_COMMON_07_008: [ If hsm_type is TRANSPORT_HSM_TYPE_TPM and ek or srk is NULL, prov_transport_common_mqtt_open shall return a non-zero value. ] */
        LogError("Invalid parameter specified ek: %p, srk: %p", ek, srk);
        result = __FAILURE__;
    }
    /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_009: [ prov_transport_common_mqtt_open shall clone the ek and srk values.] */
    else if (ek != NULL && (mqtt_info->ek = BUFFER_clone(ek)) == NULL)
    {
        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_041: [ If a failure is encountered, prov_transport_common_mqtt_open shall return a non-zero value. ] */
        LogError("Unable to allocate endorsement key");
        result = __FAILURE__;
    }
    else if (srk != NULL && (mqtt_info->srk = BUFFER_clone(srk)) == NULL)
    {
        /* Tests_PROV_TRANSPORT_MQTT_COMMON_07_041: [ If a failure is encountered, prov_transport_common_mqtt_open shall return a non-zero value. ] */
        LogError("Unable to allocate storage root key");
        BUFFER_delete(mqtt_info->ek);
        mqtt_info->ek = NULL;
        result = __FAILURE__;
    }
    else if (mallocAndStrcpy_s(&mqtt_info->registration_id, registration_id) != 0)
    {
        /* Codes_PROV_TRANSPORT_HTTP_CLIENT_07_003: [ If any error is encountered prov_transport_http_create shall return NULL. ] */
        LogError("failure constructing registration Id");
        BUFFER_delete(mqtt_info->ek);
        mqtt_info->ek = NULL;
        BUFFER_delete(mqtt_info->srk);
        mqtt_info->srk = NULL;
        result = __FAILURE__;
    }
    else
    {
        mqtt_info->register_data_cb = data_callback;
        mqtt_info->user_ctx = user_ctx;
        mqtt_info->status_cb = status_cb;
        mqtt_info->status_ctx = status_ctx;
        mqtt_info->mqtt_state = MQTT_STATE_DISCONNECTED;
        result = 0;
    }
    return result;
}
Пример #21
0
const char* IoTHubAccount_GetSharedAccessSignature(IOTHUB_ACCOUNT_INFO_HANDLE acctHandle)
{
    const char* result = NULL;
    IOTHUB_ACCOUNT_INFO* acctInfo = (IOTHUB_ACCOUNT_INFO*)acctHandle;
    if (acctInfo != NULL)
    {
        if (acctInfo->sharedAccessToken != NULL)
        {
            // Reuse the sharedAccessToken if it's been created already
            result = acctInfo->sharedAccessToken;
        }
        else
        {
        time_t currentTime = time(NULL);
        size_t expiry_time = (size_t)(currentTime + 3600);

        STRING_HANDLE accessKey = STRING_construct(acctInfo->sharedAccessKey);
        STRING_HANDLE iotName = STRING_construct(acctInfo->hostname);
        STRING_HANDLE keyName = STRING_construct(acctInfo->keyName);
        if (accessKey != NULL && iotName != NULL && keyName != NULL)
        {
            STRING_HANDLE sasHandle = SASToken_Create(accessKey, iotName, keyName, expiry_time);
            if (sasHandle == NULL)
            {

                result = NULL;
            }
            else
            {
                if (mallocAndStrcpy_s(&acctInfo->sharedAccessToken, STRING_c_str(sasHandle)) != 0)
                {
                    result = NULL;
                }
                else
                {
                    result = acctInfo->sharedAccessToken;
                }
                STRING_delete(sasHandle);
            }
        }
        STRING_delete(accessKey);
        STRING_delete(iotName);
        STRING_delete(keyName);
    }
    }
    else
    {
        result = NULL;
    }
    return result; 
}
static IOTHUB_AUTHORIZATION_DATA* initialize_auth_client(const char* device_id, const char* module_id)
{
    IOTHUB_AUTHORIZATION_DATA* result;

    /* Codes_SRS_IoTHub_Authorization_07_002: [IoTHubClient_Auth_Create shall allocate a IOTHUB_AUTHORIZATION_HANDLE that is needed for subsequent calls. ] */
    result = (IOTHUB_AUTHORIZATION_DATA*)malloc(sizeof(IOTHUB_AUTHORIZATION_DATA) );
    if (result == NULL)
    {
        /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
        LogError("Failed allocating IOTHUB_AUTHORIZATION_DATA");
        result = NULL;
    }
    else
    {
        memset(result, 0, sizeof(IOTHUB_AUTHORIZATION_DATA) );
        if (mallocAndStrcpy_s(&result->device_id, device_id) != 0)
        {
            /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
            LogError("Failed allocating device_key");
            free(result);
            result = NULL;
        }
        else if (module_id != NULL && mallocAndStrcpy_s(&result->module_id, module_id) != 0)
        {
            /* Codes_SRS_IoTHub_Authorization_07_019: [ On error IoTHubClient_Auth_Create shall return NULL. ] */
            LogError("Failed allocating module_id");
            free(result->device_id);
            free(result);
            result = NULL;
        }
        else
        {
            result->token_expiry_time_sec = DEFAULT_SAS_TOKEN_EXPIRY_TIME_SECS;
        }
    }
    return result;
}
Пример #23
0
char* prov_auth_get_registration_id(PROV_AUTH_HANDLE handle)
{
    char* result;
    if (handle == NULL)
    {
        /* Codes_SRS_PROV_AUTH_CLIENT_07_010: [ if handle is NULL prov_auth_get_registration_id shall return NULL. ] */
        LogError("Invalid handle parameter");
        result = NULL;
    }
    else
    {
        int load_result = 0;
        int reg_id_allocated = 0;
        if (handle->registration_id == NULL)
        {
            /* Codes_SRS_PROV_AUTH_CLIENT_07_011: [ prov_auth_get_registration_id shall load the registration id if it has not been previously loaded. ] */
            load_result = load_registration_id(handle);
            if (load_result == 0)
            {
                reg_id_allocated = 1;
            }
        }

        if (load_result != 0)
        {
            /* Codes_SRS_PROV_AUTH_CLIENT_07_012: [ If a failure is encountered, prov_auth_get_registration_id shall return NULL. ] */
            LogError("Failed loading registration key");
            result = NULL;
        }
        /* Codes_SRS_PROV_AUTH_CLIENT_07_013: [ Upon success prov_auth_get_registration_id shall return the registration id of the secure enclave. ] */
        else if (mallocAndStrcpy_s(&result, handle->registration_id) != 0)
        {
            /* Codes_SRS_PROV_AUTH_CLIENT_07_012: [ If a failure is encountered, prov_auth_get_registration_id shall return NULL. ] */
            LogError("Failed allocating registration key");
            if (reg_id_allocated == 1)
            {
                free(handle->registration_id);
                handle->registration_id = NULL;
            }
            result = NULL;
        }
    }
    return result;
}
Пример #24
0
char* secure_dev_tpm_get_storage_key(SEC_DEVICE_HANDLE handle)
{
    char* result;
    if (handle == NULL)
    {
        /* Codes_SRS_SECURE_DEVICE_TPM_07_016: [ If handle is NULL, secure_dev_tpm_get_storage_key shall return NULL. ] */
        LogError("Invalid handle value specified");
        result = NULL;
    }
    else if (handle->srk_pub.publicArea.unique.rsa.t.size == 0)
    {
        /* Codes_SRS_SECURE_DEVICE_TPM_07_017: [ If the srk_public value was not initialized, secure_dev_tpm_get_storage_key shall return NULL. ] */
        LogError("storage root key is invalid");
        result = NULL;
    }
    else
    {
        unsigned char data_bytes[1024];
        unsigned char* data_pos = data_bytes;
        /* Codes_SRS_SECURE_DEVICE_TPM_07_018: [ secure_dev_tpm_get_storage_key shall allocate and return the Storage Root Key. ] */
        uint32_t data_length = TPM2B_PUBLIC_Marshal(&handle->srk_pub, &data_pos, NULL);
        STRING_HANDLE encoded_srk = Azure_Base64_Encode_Bytes(data_bytes, data_length);
        if (encoded_srk == NULL)
        {
            /* Codes_SRS_SECURE_DEVICE_TPM_07_019: [ If any failure is encountered, secure_dev_tpm_get_storage_key shall return NULL. ] */
            LogError("Failure encoding storage root key");
            result = NULL;
        }
        else
        {
            if (mallocAndStrcpy_s(&result, STRING_c_str(encoded_srk)) != 0)
            {
                /* Codes_SRS_SECURE_DEVICE_TPM_07_019: [ If any failure is encountered, secure_dev_tpm_get_storage_key shall return NULL. ] */
                LogError("Failure allocating storage root key");
                result = NULL;
            }
            STRING_delete(encoded_srk);
        }
    }
    return result;
}
static void* NativeModuleHost_ParseConfigurationFromJson(const char* configuration)
{
    char* config_str;
    if (configuration == NULL)
    {
        /*Codes_SRS_NATIVEMODULEHOST_17_003: [ NativeModuleHost_ParseConfigurationFromJson shall return NULL if configuration is NULL. ]*/
        LogError("NULL configuration given, this isn't correct for Native Module Host.");
        config_str = NULL;
    }
    else
    {
        /*Codes_SRS_NATIVEMODULEHOST_17_004: [ On success, NativeModuleHost_ParseConfigurationFromJson shall return a valid copy of the configuration string given. ]*/
        if (mallocAndStrcpy_s(&config_str, configuration) != 0)
        {
            /*Codes_SRS_NATIVEMODULEHOST_17_005: [ NativeModuleHost_ParseConfigurationFromJson shall return NULL if any system call fails. ]*/
            LogError("Unable to copy the configuration, returning NULL");
            config_str = NULL;
        }
    }
    return config_str;
}
Пример #26
0
/*
 * @brief	duplicate a string and convert to upper case. New string must be released
 *			no longer needed.
 */
static const char * IdentityMapConfig_ToUpperCase(const char * string)
{
	char * result;
	int status;
	status = mallocAndStrcpy_s(&result, string);
	if (status != 0) // failure
	{
		result = NULL;
	}
	else
	{
		char * temp = result;
		while (*temp)
		{
			*temp = toupper(*temp);
			temp++;
		}
	}

	return result;
}
Пример #27
0
/*obs: value is already cloned at the time of calling this function */
static int createOrUpdateOption(HTTPAPIEX_HANDLE_DATA* handleData, const char* optionName, const void* value)
{
    /*this function is called after the option value has been saved (cloned)*/
    int result;
    
    /*decide bwtween update or create*/
    HTTPAPIEX_SAVED_OPTION* whereIsIt = VECTOR_find_if(handleData->savedOptions, sameName, optionName);
    if (whereIsIt != NULL)
    {
        free((void*)(whereIsIt->value));
        whereIsIt->value = value;
        result = 0;
    }
    else
    {
        HTTPAPIEX_SAVED_OPTION newOption;
        if (mallocAndStrcpy_s((char**)&(newOption.optionName), optionName) != 0)
        {
            free((void*)value);
            result = __LINE__;
        }
        else
        {
            newOption.value = value;
            if (VECTOR_push_back(handleData->savedOptions, &newOption, 1) != 0)
            {
                LogError("unable to VECTOR_push_back\r\n");
                free((void*)newOption.optionName);
                free((void*)value);
                result = __LINE__;
            }
            else
            {
                result = 0;
            }
        }
    }
    
    return result;
}
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;
}
Пример #29
0
DNS_ASYNC_HANDLE dns_async_create(const char* hostname, DNS_ASYNC_OPTIONS* options)
{
    /* Codes_SRS_DNS_ASYNC_30_012: [ The optional options parameter shall be ignored. ]*/
    DNS_ASYNC_INSTANCE* result;
    (void)options;
    if (hostname == NULL)
    {
        /* Codes_SRS_DNS_ASYNC_30_011: [ If the hostname parameter is NULL, dns_async_create shall log an error and return NULL. ]*/
        LogError("NULL hostname");
        result = NULL;
    }
    else
    {
        result = malloc(sizeof(DNS_ASYNC_INSTANCE));
        if (result == NULL)
        {
            /* Codes_SRS_DNS_ASYNC_30_014: [ On any failure, dns_async_create shall log an error and return NULL. ]*/
            LogError("malloc instance failed");
            result = NULL;
        }
        else
        {
			int ms_result;
            result->is_complete = false;
            result->is_failed = false;
            result->ip_v4 = 0;
            /* Codes_SRS_DNS_ASYNC_30_010: [ dns_async_create shall make a copy of the hostname parameter to allow immediate deletion by the caller. ]*/
            ms_result = mallocAndStrcpy_s(&result->hostname, hostname);
            if (ms_result != 0)
            {
                /* Codes_SRS_DNS_ASYNC_30_014: [ On any failure, dns_async_create shall log an error and return NULL. ]*/
                free(result);
                result = NULL;
            }
        }
    }
    return result;
}
static void expected_calls_deviceRegistrationState_fromJson()
{
    STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_REGISTRATION_ID); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_CREATED_DATE_TIME_UTC); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_DEVICE_ID); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(REGISTRATION_STATUS_JSON_VALUE_ASSIGNED); //can't "fail"
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_UPDATED_DATE_TIME_UTC); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_ERROR_MESSAGE); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_string(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_ETAG); //can't "fail"
    STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
    STRICT_EXPECTED_CALL(json_object_get_number(IGNORED_PTR_ARG, IGNORED_PTR_ARG)).SetReturn(DUMMY_ERROR_CODE); //can't "fail"
}