int iothub_security_init(IOTHUB_SECURITY_TYPE sec_type)
{
    int result;
    g_security_type = sec_type;
    SECURE_DEVICE_TYPE device_type = prov_dev_security_get_type();
    if (device_type == SECURE_DEVICE_TYPE_UNKNOWN)
    {
        result = prov_dev_security_init(g_security_type == IOTHUB_SECURITY_TYPE_SAS ? SECURE_DEVICE_TYPE_TPM : SECURE_DEVICE_TYPE_X509);
    }
    else
    {
        // Make sure that the types are compatible
        if (device_type == SECURE_DEVICE_TYPE_TPM)
        {
            if (g_security_type != IOTHUB_SECURITY_TYPE_SAS)
            {
                result = __FAILURE__;
            }
            else
            {
                result = 0;
            }
        }
        else
        {
            if (g_security_type != IOTHUB_SECURITY_TYPE_X509)
            {
                result = __FAILURE__;
            }
            else
            {
                result = 0;
            }
        }
    }
    if (result == 0)
    {
        result = initialize_hsm_system();
    }
    return result;
}
int main()
{
    int result;

    if (platform_init() == 0 && initialize_hsm_system() == 0)
    {
        HSM_CLIENT_HANDLE hsm_handle;
        if ((hsm_handle = hsm_client_riot_create()) == NULL)
        {
            (void)printf("Failure getting common name\r\n");
            result = __LINE__;
        }
        else
        {
            char* enroll_type = get_user_input("Would you like to do Individual (i) or Group (g) enrollments: ", 1);
            if (enroll_type != NULL)
            {
                if (enroll_type[0] == 'g' || enroll_type[0] == 'G')
                {
                    if (ca_root_certificate_info(hsm_handle) != 0)
                    {
                        result = __LINE__;
                    }
                    else
                    {
                        result = 0;
                    }
                }
                else
                {
                    if (alias_certificate_info(hsm_handle) != 0)
                    {
                        result = __LINE__;
                    }
                    else
                    {
                        result = 0;
                    }
                }
                free(enroll_type);
            }
            else
            {
                (void)printf("Invalid option specified\r\n");
                result = __LINE__;
            }
            hsm_client_riot_destroy(hsm_handle);
        }
        deinitialize_hsm_system();
        platform_deinit();
    }
    else
    {
        (void)printf("Failed calling platform_init\r\n");
        result = __LINE__;
    }

    (void)printf("Press any key to continue:\r\n");
    (void)getchar();
    return result;
}