我正在进行的项目涉及使用EAF(外部附件框架)连接到MFI(Made for 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;
}
答案 0 :(得分: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