third_party/openssl/platform/ssl_pm.c
/**
 * @brief create SSL low-level object
 */
@brief create SSL low-level object
@brief create SSL low - level object
int ssl_pm_new(SSL *ssl)
{
    struct ssl_pm *ssl_pm;
    int ret;
    const unsigned char pers[] = "OpenSSL PM";
    size_t pers_len = sizeof(pers);
    int endpoint;
    int version;
    const SSL_METHOD *method = ssl->method;
    if (ssl->ctx->read_buffer_len < OPENSSL_READ_BUFFER_LENGTH_MIN ||
        ssl->ctx->read_buffer_len > OPENSSL_READ_BUFFER_LENGTH_MAX)
        return -1;
     ssl_pm = ssl_mem_zalloc(sizeof(struct ssl_pm));
    if (!ssl_pm) {
        SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (ssl_pm)");
        goto no_mem;
    }
    max_content_len = ssl->ctx->read_buffer_len;
    mbedtls_net_init(&ssl_pm->fd);
    mbedtls_net_init(&ssl_pm->cl_fd);
    mbedtls_ssl_config_init(&ssl_pm->conf);
    mbedtls_ctr_drbg_init(&ssl_pm->ctr_drbg);
    mbedtls_entropy_init(&ssl_pm->entropy);
    mbedtls_ssl_init(&ssl_pm->ssl);
    ret = mbedtls_ctr_drbg_seed(&ssl_pm->ctr_drbg, mbedtls_entropy_func, &ssl_pm->entropy, pers, pers_len);
    if (ret) {
        SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ctr_drbg_seed() return -0x%x", -ret);
        goto mbedtls_err1;
    }
    if (method->endpoint) {
        endpoint = MBEDTLS_SSL_IS_SERVER;
    } else {
        endpoint = MBEDTLS_SSL_IS_CLIENT;
    }
    ret = mbedtls_ssl_config_defaults(&ssl_pm->conf, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
    if (ret) {
        SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_config_defaults() return -0x%x", -ret);
        goto mbedtls_err2;
    }
    if (TLS_ANY_VERSION != ssl->version) {
        if (TLS1_2_VERSION == ssl->version)
            version = MBEDTLS_SSL_MINOR_VERSION_3;
        else if (TLS1_1_VERSION == ssl->version)
            version = MBEDTLS_SSL_MINOR_VERSION_2;
        else if (TLS1_VERSION == ssl->version)
            version = MBEDTLS_SSL_MINOR_VERSION_1;
        else
            version = MBEDTLS_SSL_MINOR_VERSION_0;
        mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version);
        mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version);
    } else {
        mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
        mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0);
    }
    mbedtls_ssl_conf_rng(&ssl_pm->conf, mbedtls_ctr_drbg_random, &ssl_pm->ctr_drbg);
#ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
    mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL);
    mbedtls_ssl_conf_dbg(&ssl_pm->conf, ssl_platform_debug, NULL);
#else
    mbedtls_ssl_conf_dbg(&ssl_pm->conf, NULL, NULL);
#endif
    ret = mbedtls_ssl_setup(&ssl_pm->ssl, &ssl_pm->conf);
    if (ret) {
        SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_setup() return -0x%x", -ret);
        goto mbedtls_err2;
    }
    mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd, mbedtls_net_send, mbedtls_net_recv, NULL);
    ssl->ssl_pm = ssl_pm;
    return 0;
mbedtls_err2:
    mbedtls_ssl_config_free(&ssl_pm->conf);
    mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg);
mbedtls_err1:
    mbedtls_entropy_free(&ssl_pm->entropy);
    ssl_mem_free(ssl_pm);
no_mem:
    return -1;
}
int ssl_pm_new ( SSL * ssl ) { struct ssl_pm * ssl_pm ; int ret ; const unsigned char pers [ ] = " " ; size_t pers_len = sizeof ( pers ) ; int endpoint ; int version ; const SSL_METHOD * method = ssl -> method ; if ( ssl -> ctx -> read_buffer_len < OPENSSL_READ_BUFFER_LENGTH_MIN || ssl -> ctx -> read_buffer_len > OPENSSL_READ_BUFFER_LENGTH_MAX ) return -1 ; ssl_pm = ssl_mem_zalloc ( sizeof ( struct ssl_pm ) ) ; if ( ! ssl_pm ) { SSL_DEBUG ( SSL_PLATFORM_ERROR_LEVEL , " " ) ; goto no_mem ; } max_content_len = ssl -> ctx -> read_buffer_len ; mbedtls_net_init ( & ssl_pm -> fd ) ; mbedtls_net_init ( & ssl_pm -> cl_fd ) ; mbedtls_ssl_config_init ( & ssl_pm -> conf ) ; mbedtls_ctr_drbg_init ( & ssl_pm -> ctr_drbg ) ; mbedtls_entropy_init ( & ssl_pm -> entropy ) ; mbedtls_ssl_init ( & ssl_pm -> ssl ) ; ret = mbedtls_ctr_drbg_seed ( & ssl_pm -> ctr_drbg , mbedtls_entropy_func , & ssl_pm -> entropy , pers , pers_len ) ; if ( ret ) { SSL_DEBUG ( SSL_PLATFORM_ERROR_LEVEL , " " , - ret ) ; goto mbedtls_err1 ; } if ( method -> endpoint ) { endpoint = MBEDTLS_SSL_IS_SERVER ; } else { endpoint = MBEDTLS_SSL_IS_CLIENT ; } ret = mbedtls_ssl_config_defaults ( & ssl_pm -> conf , endpoint , MBEDTLS_SSL_TRANSPORT_STREAM , MBEDTLS_SSL_PRESET_DEFAULT ) ; if ( ret ) { SSL_DEBUG ( SSL_PLATFORM_ERROR_LEVEL , " " , - ret ) ; goto mbedtls_err2 ; } if ( TLS_ANY_VERSION != ssl -> version ) { if ( TLS1_2_VERSION == ssl -> version ) version = MBEDTLS_SSL_MINOR_VERSION_3 ; else if ( TLS1_1_VERSION == ssl -> version ) version = MBEDTLS_SSL_MINOR_VERSION_2 ; else if ( TLS1_VERSION == ssl -> version ) version = MBEDTLS_SSL_MINOR_VERSION_1 ; else version = MBEDTLS_SSL_MINOR_VERSION_0 ; mbedtls_ssl_conf_max_version ( & ssl_pm -> conf , MBEDTLS_SSL_MAJOR_VERSION_3 , version ) ; mbedtls_ssl_conf_min_version ( & ssl_pm -> conf , MBEDTLS_SSL_MAJOR_VERSION_3 , version ) ; } else { mbedtls_ssl_conf_max_version ( & ssl_pm -> conf , MBEDTLS_SSL_MAJOR_VERSION_3 , MBEDTLS_SSL_MINOR_VERSION_3 ) ; mbedtls_ssl_conf_min_version ( & ssl_pm -> conf , MBEDTLS_SSL_MAJOR_VERSION_3 , MBEDTLS_SSL_MINOR_VERSION_0 ) ; } mbedtls_ssl_conf_rng ( & ssl_pm -> conf , mbedtls_ctr_drbg_random , & ssl_pm -> ctr_drbg ) ; #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG mbedtls_debug_set_threshold ( MBEDTLS_DEBUG_LEVEL ) ; mbedtls_ssl_conf_dbg ( & ssl_pm -> conf , ssl_platform_debug , NULL ) ; #else mbedtls_ssl_conf_dbg ( & ssl_pm -> conf , NULL , NULL ) ; #endif ret = mbedtls_ssl_setup ( & ssl_pm -> ssl , & ssl_pm -> conf ) ; if ( ret ) { SSL_DEBUG ( SSL_PLATFORM_ERROR_LEVEL , " " , - ret ) ; goto mbedtls_err2 ; } mbedtls_ssl_set_bio ( & ssl_pm -> ssl , & ssl_pm -> fd , mbedtls_net_send , mbedtls_net_recv , NULL ) ; ssl -> ssl_pm = ssl_pm ; return 0 ; mbedtls_err2 : mbedtls_ssl_config_free ( & ssl_pm -> conf ) ; mbedtls_ctr_drbg_free ( & ssl_pm -> ctr_drbg ) ; mbedtls_err1 : mbedtls_entropy_free ( & ssl_pm -> entropy ) ; ssl_mem_free ( ssl_pm ) ; no_mem : return -1 ; }
@brief create SSL low-level object
@brief create SSL low - level object