Skip to content

Commit 390fbaa

Browse files
committed
net.mbedtls: make errors more verbose, to ease remote diagnosis of issues
1 parent 3386036 commit 390fbaa

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

vlib/net/mbedtls/ssl_connection.c.v

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,24 @@ pub fn new_sslcerts_in_memory(verify string, cert string, cert_key string) !&SSL
5151
if verify != '' {
5252
ret := C.mbedtls_x509_crt_parse(&certs.cacert, verify.str, verify.len + 1)
5353
if ret != 0 {
54-
return error_with_code('mbedtls_x509_crt_parse error', ret)
54+
return error_with_code('net.mbedtls new_sslcerts_in_memory, mbedtls_x509_crt_parse error 1 ret: ${ret}',
55+
ret)
5556
}
5657
}
5758
if cert != '' {
5859
ret := C.mbedtls_x509_crt_parse(&certs.client_cert, cert.str, cert.len + 1)
5960
if ret != 0 {
60-
return error_with_code('mbedtls_x509_crt_parse error', ret)
61+
return error_with_code('net.mbedtls new_sslcerts_in_memory, mbedtls_x509_crt_parse error 2 ret: ${ret}',
62+
ret)
6163
}
6264
}
6365
if cert_key != '' {
6466
unsafe {
6567
ret := C.mbedtls_pk_parse_key(&certs.client_key, cert_key.str, cert_key.len + 1,
6668
0, 0, C.mbedtls_ctr_drbg_random, &ctr_drbg)
6769
if ret != 0 {
68-
return error_with_code('v error', ret)
70+
return error_with_code('net.mbedtls new_sslcerts_in_memory, mbedtls_pk_parse_key error ret: ${ret}',
71+
ret)
6972
}
7073
}
7174
}
@@ -78,21 +81,24 @@ pub fn new_sslcerts_from_file(verify string, cert string, cert_key string) !&SSL
7881
if verify != '' {
7982
ret := C.mbedtls_x509_crt_parse_file(&certs.cacert, &char(verify.str))
8083
if ret != 0 {
81-
return error_with_code('mbedtls_x509_crt_parse error', ret)
84+
return error_with_code('net.mbedtls new_sslcerts_from_file, mbedtls_x509_crt_parse_file error 1 ret: ${ret}',
85+
ret)
8286
}
8387
}
8488
if cert != '' {
8589
ret := C.mbedtls_x509_crt_parse_file(&certs.client_cert, &char(cert.str))
8690
if ret != 0 {
87-
return error_with_code('mbedtls_x509_crt_parse error', ret)
91+
return error_with_code('net.mbedtls new_sslcerts_from_file, mbedtls_x509_crt_parse_file error 2 ret: ${ret}',
92+
ret)
8893
}
8994
}
9095
if cert_key != '' {
9196
unsafe {
9297
ret := C.mbedtls_pk_parse_keyfile(&certs.client_key, &char(cert_key.str),
9398
0, C.mbedtls_ctr_drbg_random, &ctr_drbg)
9499
if ret != 0 {
95-
return error_with_code('v error', ret)
100+
return error_with_code('net.mbedtls new_sslcerts_from_file, mbedtls_pk_parse_keyfile error ret: ${ret}',
101+
ret)
96102
}
97103
}
98104
}
@@ -171,10 +177,10 @@ fn (mut l SSLListener) init() ! {
171177

172178
lhost, lport := net.split_address(l.saddr)!
173179
if l.config.cert == '' || l.config.cert_key == '' {
174-
return error('No certificate or key provided')
180+
return error('net.mbedtls SSLListener.init, no certificate or key provided')
175181
}
176182
if l.config.validate && l.config.verify == '' {
177-
return error('No root CA provided')
183+
return error('net.mbedtls SSLListener.init, no root CA provided')
178184
}
179185
C.mbedtls_net_init(&l.server_fd)
180186
C.mbedtls_ssl_init(&l.ssl)
@@ -195,11 +201,11 @@ fn (mut l SSLListener) init() ! {
195201

196202
if l.config.in_memory_verification {
197203
l.certs = new_sslcerts_in_memory(l.config.verify, l.config.cert, l.config.cert_key) or {
198-
return error('Cert failure')
204+
return error('net.mbedtls SSLListener.init, cert failure 1, err: ${err}')
199205
}
200206
} else {
201207
l.certs = new_sslcerts_from_file(l.config.verify, l.config.cert, l.config.cert_key) or {
202-
return error('Cert failure')
208+
return error('net.mbedtls SSLListener.init, cert failure 2, err: ${err}')
203209
}
204210
}
205211

@@ -216,26 +222,28 @@ fn (mut l SSLListener) init() ! {
216222
ret = C.mbedtls_net_bind(&l.server_fd, bind_ip, voidptr(bind_port.str), C.MBEDTLS_NET_PROTO_TCP)
217223

218224
if ret != 0 {
219-
return error_with_code("can't bind to ${l.saddr}", ret)
225+
return error_with_code("net.mbedtls SSLListener.init, mbedtls_net_bind can't bind to ${l.saddr} error ret: ${ret}",
226+
ret)
220227
}
221228

222229
ret = C.mbedtls_ssl_config_defaults(&l.conf, C.MBEDTLS_SSL_IS_SERVER, C.MBEDTLS_SSL_TRANSPORT_STREAM,
223230
C.MBEDTLS_SSL_PRESET_DEFAULT)
224231
if ret != 0 {
225-
return error_with_code("can't to set config defaults", ret)
232+
return error_with_code("net.mbedtls SSLListener.init, mbedtls_ssl_config_defaults can't set config defaults ret: ${ret}",
233+
ret)
226234
}
227235

228236
C.mbedtls_ssl_conf_ca_chain(&l.conf, &l.certs.cacert, unsafe { nil })
229237
ret = C.mbedtls_ssl_conf_own_cert(&l.conf, &l.certs.client_cert, &l.certs.client_key)
230-
231238
if ret != 0 {
232-
return error_with_code("can't load certificate", ret)
239+
return error_with_code("net.mbedtls SSLListener.init, mbedtls_ssl_conf_own_cert can't load certificate ret: ${ret}",
240+
ret)
233241
}
234242

235243
ret = C.mbedtls_ssl_setup(&l.ssl, &l.conf)
236-
237244
if ret != 0 {
238-
return error_with_code("can't setup ssl", ret)
245+
return error_with_code("net.mbedtls SSLListener.init, mbedtls_ssl_setup can't setup ssl ret: ${ret}",
246+
ret)
239247
}
240248

241249
if get_cert_callback := l.config.get_certificate {
@@ -270,7 +278,8 @@ pub fn (mut l SSLListener) accept() !&SSLConn {
270278

271279
mut ret := C.mbedtls_net_accept(&l.server_fd, &conn.server_fd, &ip, 16, &iplen)
272280
if ret != 0 {
273-
return error_with_code("can't accept connection", ret)
281+
return error_with_code("net.mbedtls SSLListener.accept, mbedtls_net_accept can't accept connection ret: ${ret}",
282+
ret)
274283
}
275284
conn.handle = conn.server_fd.fd
276285
conn.owns_socket = true
@@ -281,9 +290,9 @@ pub fn (mut l SSLListener) accept() !&SSLConn {
281290
C.mbedtls_ssl_init(&conn.ssl)
282291
C.mbedtls_ssl_config_init(&conn.conf)
283292
ret = C.mbedtls_ssl_setup(&conn.ssl, &l.conf)
284-
285293
if ret != 0 {
286-
return error_with_code('SSL setup failed', ret)
294+
return error_with_code('net.mbedtls SSLListener.accept, mbedtls_ssl_setup SSL setup failed ret: ${ret}',
295+
ret)
287296
}
288297

289298
C.mbedtls_ssl_set_bio(&conn.ssl, &conn.server_fd, C.mbedtls_net_send, C.mbedtls_net_recv,
@@ -297,7 +306,8 @@ pub fn (mut l SSLListener) accept() !&SSLConn {
297306
eprintln('${@METHOD} shutdown ---> res: ${err}')
298307
}
299308
}
300-
return error_with_code('SSL handshake failed', ret)
309+
return error_with_code('net.mbedtls SSLListener.accept, mbedtls_ssl_handshake failed 1; handshake ret: ${ret}',
310+
ret)
301311
}
302312
ret = C.mbedtls_ssl_handshake(&conn.ssl)
303313
}
@@ -326,7 +336,7 @@ pub fn new_ssl_conn(config SSLConnectConfig) !&SSLConn {
326336
mut conn := &SSLConn{
327337
config: config
328338
}
329-
conn.init() or { return err }
339+
conn.init()!
330340
return conn
331341
}
332342

@@ -348,7 +358,7 @@ pub fn (mut s SSLConn) shutdown() ! {
348358
eprintln(@METHOD)
349359
}
350360
if !s.opened {
351-
return error('ssl connection not open')
361+
return error('net.mbedtls SSLConn.shutdown, connection was not open')
352362
}
353363
if unsafe { s.certs != nil } {
354364
C.mbedtls_x509_crt_free(&s.certs.cacert)
@@ -375,7 +385,8 @@ fn (mut s SSLConn) init() ! {
375385
ret = C.mbedtls_ssl_config_defaults(&s.conf, C.MBEDTLS_SSL_IS_CLIENT, C.MBEDTLS_SSL_TRANSPORT_STREAM,
376386
C.MBEDTLS_SSL_PRESET_DEFAULT)
377387
if ret != 0 {
378-
return error_with_code('Failed to set SSL configuration', ret)
388+
return error_with_code('net.mbedtls SSLConn.init, mbedtls_ssl_config_defaults failed to set SSL configuration ret: ${ret}',
389+
ret)
379390
}
380391
$if trace_mbedtls_timeouts ? {
381392
dump(mbedtls_client_read_timeout_ms)
@@ -423,7 +434,8 @@ fn (mut s SSLConn) init() ! {
423434
}
424435
}
425436
if ret < 0 {
426-
return error_with_code('Failed to set certificates', ret)
437+
return error_with_code('net.mbedtls SSLConn.init, failed to set certificates, ret: ${ret}',
438+
ret)
427439
}
428440

429441
if unsafe { s.certs != nil } {
@@ -439,7 +451,8 @@ fn (mut s SSLConn) init() ! {
439451

440452
ret = C.mbedtls_ssl_setup(&s.ssl, &s.conf)
441453
if ret != 0 {
442-
return error_with_code('Failed to setup SSL connection', ret)
454+
return error_with_code('net.mbedtls SSLConn.init, mbedtls_ssl_setup failed to setup SSL connection ret: ${ret}',
455+
ret)
443456
}
444457
}
445458

@@ -449,26 +462,23 @@ pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ! {
449462
eprintln('${@METHOD} hostname: ${hostname}')
450463
}
451464
if s.opened {
452-
return error('ssl connection already open')
465+
return error('net.mbedtls SSLConn.connect, ssl connection was already open')
453466
}
454467
s.handle = tcp_conn.sock.handle
455468
s.duration = 30 * time.second
456-
457469
mut ret := C.mbedtls_ssl_set_hostname(&s.ssl, &char(hostname.str))
458470
if ret != 0 {
459-
return error_with_code('Failed to set hostname', ret)
471+
return error_with_code('net.mbedtls SSLConn.connect, mbedtls_ssl_set_hostname failed to set hostname',
472+
ret)
460473
}
461-
462474
s.server_fd.fd = s.handle
463-
464475
C.mbedtls_ssl_set_bio(&s.ssl, &s.server_fd, C.mbedtls_net_send, C.mbedtls_net_recv,
465476
C.mbedtls_net_recv_timeout)
466-
467477
ret = C.mbedtls_ssl_handshake(&s.ssl)
468478
if ret != 0 {
469-
return error_with_code('SSL handshake failed', ret)
479+
return error_with_code('net.mbedtls SSLConn.connect, mbedtls_ssl_handshake failed 2; ret: ${ret}',
480+
ret)
470481
}
471-
472482
s.opened = true
473483
}
474484

@@ -479,32 +489,30 @@ pub fn (mut s SSLConn) dial(hostname string, port int) ! {
479489
}
480490
s.owns_socket = true
481491
if s.opened {
482-
return error('ssl connection already open')
492+
return error('net.mbedtls SSLConn.dial, the ssl connection was already open')
483493
}
484494
s.duration = 30 * time.second
485495

486496
mut ret := C.mbedtls_ssl_set_hostname(&s.ssl, &char(hostname.str))
487497
if ret != 0 {
488-
return error_with_code('Failed to set hostname', ret)
498+
return error_with_code('net.mbedtls SSLConn.dial, failed to set hostname', ret)
489499
}
490500

491501
port_str := port.str()
492502
ret = C.mbedtls_net_connect(&s.server_fd, &char(hostname.str), &char(port_str.str),
493503
C.MBEDTLS_NET_PROTO_TCP)
494504
if ret != 0 {
495-
return error_with_code('Failed to connect to host', ret)
505+
return error_with_code('net.mbedtls SSLConn.dial, failed to connect to host',
506+
ret)
496507
}
497-
498508
C.mbedtls_ssl_set_bio(&s.ssl, &s.server_fd, C.mbedtls_net_send, C.mbedtls_net_recv,
499509
C.mbedtls_net_recv_timeout)
500-
501510
s.handle = s.server_fd.fd
502-
503511
ret = C.mbedtls_ssl_handshake(&s.ssl)
504512
if ret != 0 {
505-
return error_with_code('SSL handshake failed', ret)
513+
return error_with_code('net.mbedtls SSLConn.dial, mbedtls_ssl_handshake failed 3; ret: ${ret}',
514+
ret)
506515
}
507-
508516
s.opened = true
509517
}
510518

@@ -568,14 +576,14 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int {
568576
$if trace_ssl ? {
569577
eprintln('${@METHOD} ---> res: could not read using SSL')
570578
}
571-
return error_with_code('Could not read using SSL', res)
579+
return error_with_code('net.mbedtls SSLConn.socket_read_into_ptr, could not read using SSL',
580+
res)
572581
}
573582
}
574583
}
575584
}
576-
577-
// Dead code, for the compiler to pass
578-
return error('Unknown error')
585+
// Dead code, just to satisfy the compiler:
586+
return error('net.mbedtls SSLConn.socket_read_into_ptr, unknown error')
579587
}
580588

581589
// read reads data from the ssl connection into `buffer`
@@ -616,7 +624,8 @@ pub fn (mut s SSLConn) write_ptr(bytes &u8, len int) !int {
616624
$if trace_ssl ? {
617625
eprintln('${@METHOD} ---> res: could not write SSL, sent: ${sent}')
618626
}
619-
return error_with_code('Could not write using SSL', sent)
627+
return error_with_code('net.mbedtls SSLConn.write_ptr, could not write using SSL',
628+
sent)
620629
}
621630
}
622631
}
@@ -682,7 +691,7 @@ fn select(handle int, test Select, timeout time.Duration) !bool {
682691
remaining_time = (deadline - time.now()).milliseconds()
683692
continue
684693
}
685-
return error_with_code('Select failed: ${res}', C.errno)
694+
return error_with_code('net.mbedtls select, failed, res: ${res}', C.errno)
686695
} else if res == 0 {
687696
return net.err_timed_out
688697
}

0 commit comments

Comments
 (0)