TLSIO_OPTIONS_RESULT tlsio_options_set()

in pal/tlsio_options.c [137:226]


TLSIO_OPTIONS_RESULT tlsio_options_set(TLSIO_OPTIONS* options,
    const char* optionName, const void* value)
{
    TLSIO_OPTIONS_RESULT result;
    char* copied_value = NULL;

    if (options == NULL || optionName == NULL || value == NULL)
    {
        LogError("NULL parameter: options: %p, optionName: %p, value: %p",
            options, optionName, value);
        result = TLSIO_OPTIONS_RESULT_ERROR;
    }
    else if (!is_supported_string_option(optionName))
    {
        result = TLSIO_OPTIONS_RESULT_NOT_HANDLED;
    }
    else if(mallocAndStrcpy_s(&copied_value, value) != 0)
    {
        LogError("unable to mallocAndStrcpy_s option value");
        result = TLSIO_OPTIONS_RESULT_ERROR;
    }
    else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
    {
        if ((options->supported_options & TLSIO_OPTION_BIT_TRUSTED_CERTS) == 0)
        {
            LogError("Trusted certs option not supported");
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else if (options->trusted_certs != NULL)
        {
            LogError("unable to set trusted cert option more than once");
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else
        {
            options->trusted_certs = copied_value;
            result = TLSIO_OPTIONS_RESULT_SUCCESS;
        }
    }
    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
    {
        TLSIO_OPTIONS_x509_TYPE this_type = (strcmp(SU_OPTION_X509_CERT, optionName) == 0) ? TLSIO_OPTIONS_x509_TYPE_RSA : TLSIO_OPTIONS_x509_TYPE_ECC;
        if (options->x509_cert != NULL)
        {
            LogError("unable to set x509 cert more than once");
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else if (set_and_validate_x509_type(options, this_type) != 0)
        {
            // Error logged by helper
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else
        {
            options->x509_cert = copied_value;
            result = TLSIO_OPTIONS_RESULT_SUCCESS;
        }
    }
    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
    {
        TLSIO_OPTIONS_x509_TYPE this_type = (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0) ? TLSIO_OPTIONS_x509_TYPE_RSA : TLSIO_OPTIONS_x509_TYPE_ECC;
        if (options->x509_key != NULL)
        {
            LogError("unable to set x509 key more than once");
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else if (set_and_validate_x509_type(options, this_type) != 0)
        {
            // Error logged by helper
            result = TLSIO_OPTIONS_RESULT_ERROR;
        }
        else
        {
            options->x509_key = copied_value;
            result = TLSIO_OPTIONS_RESULT_SUCCESS;
        }
    }
    else
    {
        // This is logically impossible due to earlier tests, so just quiet the compiler
        result = TLSIO_OPTIONS_RESULT_ERROR;
    }

    if (result != TLSIO_OPTIONS_RESULT_SUCCESS)
    {
        free(copied_value);
    }

    return result;
}