1

我正在进行的项目涉及使用 EAF(外部附件框架)连接到 MFI(iPhone 制造)设备,并通过 USB 电缆来回传递数据。我想使用 TLS 来加密流量,但我无法让它工作,因为 TCP 在设备上不可用。

我正在使用以下代码来测试与 Web 服务器的 TLS 握手。它工作正常。我的问题是我不知道如何创建与设备的连接,因为 TCP 不可用。有什么建议吗?谢谢。

bool tlsHandshake( id param )
{

mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;

mbedtls_ssl_config conf;
mbedtls_ssl_context m_ssl;
mbedtls_net_context server_fd;
mbedtls_x509_crt cacert;

//initialise an SSL/TLS context
mbedtls_net_init( &server_fd );
mbedtls_ssl_init(&m_ssl);
mbedtls_ssl_config_init( &conf );

mbedtls_entropy_init( &entropy );
const char *pers = "BYOM2Plus";

mbedtls_debug_set_threshold( DEBUG_LEVEL );
int ret = 0;
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers));

if( ret != 0 ) {

    [param LogEvent:[NSString stringWithFormat:@"%@%@", @"failed!! mbedtls_ctr_drbg_seed returned 0x%x", [@(ret) stringValue]]];
    return false;
}
else{
    [param LogEvent:@"mbedtls_ctr_drbg_seed OK"];
}

//-------------------------- Initialize certificates --------------------------

[param LogEvent:@"Loading the CA root certificate"];
int ret2 = 0;
ret2 = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem, mbedtls_test_cas_pem_len );
if( ret2 < 0 )
{
     [param LogEvent:[NSString stringWithFormat:@"%@%@", @" failed\n  !  mbedtls_x509_crt_parse returned -", [@(ret2) stringValue] ]];
     return false;
}

[param LogEvent:[NSString stringWithFormat:@"%@%@", @"x509 cert ok ", [@(ret2) stringValue]]];

//---------------------------- Start the connection -----------------------------
[param LogEvent:[NSString stringWithFormat:@"%@%s%@%s",@"Connecting to tcp ", SERVER_NAME, @":", SERVER_PORT]];

int ret3 = mbedtls_net_connect( &server_fd, SERVER_NAME, SERVER_PORT, MBEDTLS_NET_PROTO_TCP );
if( ret3 != 0 )
{
    [param LogEvent:[NSString stringWithFormat:@"%@%@", @"Failed ! mbedtls_net_connect returned -", [@(ret3) stringValue]]];
    return false;
}

[param LogEvent:[NSString stringWithFormat:@"%@%@", @"Connection started: ", [@(ret3) stringValue]]];

//---------------------------- SSL setup ----------------------------
int ret4 = 0;
ret4 = mbedtls_ssl_config_defaults( &conf,
                                           MBEDTLS_SSL_IS_CLIENT,
                                           MBEDTLS_SSL_TRANSPORT_STREAM,
                                       MBEDTLS_SSL_PRESET_DEFAULT );
if( ret4 != 0 ) {
    [param LogEvent:[NSString stringWithFormat:@"%@%@", @"failed!! mbedtls_ssl_config_defaults returned 0x%0x", [@(ret4) stringValue]]];
    return false;
}
else{
    [param LogEvent:@"mbedtls_ssl_config_defaults OK"];
}

mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );

int ret5 = 0;
ret5 = mbedtls_ssl_setup( &m_ssl, &conf );
if( ret5 != 0 ) {
    [param LogEvent:[NSString stringWithFormat:@"%@%@", @"failed!! mbedtls_ssl_setup returned -", [@(ret5) stringValue]]];
    return false;
}

int ret6 = 0;
ret6 = mbedtls_ssl_set_hostname( &m_ssl, "mbed TLS Server 1" );
if( ret6 != 0 )
{
    [param LogEvent:[NSString stringWithFormat:@"%@%@", @" failed ! mbedtls_ssl_set_hostname returned -", [@(ret6) stringValue]]];
    return false;
}

mbedtls_ssl_set_bio( &m_ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );

[param LogEvent:@"Performing the SSL/TLS handshake"];
[param LogEvent:@"Waiting for Handshake"];

//#ifdef HANDSHAKING_TEST
//perform an SSL/TLS handshake
int ret7 = -1;
while(( ret7 = mbedtls_ssl_handshake( &m_ssl )) != 0 ) {
    if( ret7 != MBEDTLS_ERR_SSL_WANT_READ && ret7 != MBEDTLS_ERR_SSL_WANT_WRITE ) {
        [param LogEvent:[NSString stringWithFormat:@"%@%@", @"failed!! mbedtls_ssl_handshake returned ", [@(ret7) stringValue]]];
        return false;
    }
    else{
        [param LogEvent:@"SSL/TLS handshake succeeded"];
    }
}

[param LogEvent:[NSString stringWithFormat:@"%@%@", @"Handshake return value: ", [@(ret7) stringValue]]];

return true;
}
4

1 回答 1

0

即使这是一个老问题,我也会给出一些提示。

除了许多成员之外,struct mbedtls_ssl_context还有以下有趣的成员:

  • p_bio它指的是mbedtls_net_context(实际上是套接字/文件描述符的包装器)
  • f_send这是网络发送的回调方法
  • f_recv这是网络接收的回调方法

这些成员通常通过设置mbedtls_ssl_set_bio()。您的一种解决方案可能是提供您自己的set_bio()函数或直接设置结构成员。

文件参考:

  • include/mbedtls/ssl.h
  • library/ssl_tls.c

用于检查的版本:mbedtls-2.16.6

于 2020-06-01T15:36:47.610 回答