Skip to content

Commit 9b92319

Browse files
Add client authentication with certificates
1 parent ec7fc84 commit 9b92319

File tree

2 files changed

+447
-0
lines changed

2 files changed

+447
-0
lines changed

esp-mbedtls/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ pub mod asynch {
355355
ssl_context: *mut mbedtls_ssl_context,
356356
ssl_config: *mut mbedtls_ssl_config,
357357
crt: *mut mbedtls_x509_crt,
358+
client_crt: *mut mbedtls_x509_crt,
359+
private_key: *mut mbedtls_pk_context,
358360
eof: bool,
359361
tx_buffer: BufferedBytes<BUFFER_SIZE>,
360362
rx_buffer: BufferedBytes<BUFFER_SIZE>,
@@ -367,6 +369,8 @@ pub mod asynch {
367369
mode: Mode,
368370
min_version: TlsVersion,
369371
certs: Option<&str>,
372+
client_cert: Option<&str>,
373+
client_key: Option<&str>,
370374
) -> Result<Self, TlsError> {
371375
unsafe {
372376
error_checked!(psa_crypto_init())?;
@@ -391,6 +395,22 @@ pub mod asynch {
391395
return Err(TlsError::OutOfMemory);
392396
}
393397

398+
let client_crt =
399+
calloc(1, size_of::<mbedtls_x509_crt>() as u32) as *mut mbedtls_x509_crt;
400+
if client_crt.is_null() {
401+
free(ssl_context as *const _);
402+
free(ssl_config as *const _);
403+
return Err(TlsError::OutOfMemory);
404+
}
405+
406+
let private_key =
407+
calloc(1, size_of::<mbedtls_pk_context>() as u32) as *mut mbedtls_pk_context;
408+
if private_key.is_null() {
409+
free(ssl_context as *const _);
410+
free(ssl_config as *const _);
411+
return Err(TlsError::OutOfMemory);
412+
}
413+
394414
mbedtls_ssl_init(ssl_context);
395415
mbedtls_ssl_config_init(ssl_config);
396416
(*ssl_config).private_f_dbg = Some(dbg_print);
@@ -430,6 +450,11 @@ pub mod asynch {
430450

431451
mbedtls_x509_crt_init(crt);
432452

453+
// Init client certificate
454+
mbedtls_x509_crt_init(client_crt);
455+
// Initialize private key
456+
mbedtls_pk_init(private_key);
457+
433458
if let Some(certs) = certs {
434459
error_checked!(mbedtls_x509_crt_parse(
435460
crt,
@@ -438,14 +463,38 @@ pub mod asynch {
438463
))?;
439464
}
440465

466+
if let Some(client_cert) = client_cert {
467+
error_checked!(mbedtls_x509_crt_parse(
468+
client_crt,
469+
client_cert.as_ptr(),
470+
client_cert.len() as u32,
471+
))?;
472+
}
473+
474+
if let Some(client_key) = client_key {
475+
error_checked!(mbedtls_pk_parse_key(
476+
private_key,
477+
client_key.as_ptr(),
478+
client_key.len() as u32,
479+
core::ptr::null(),
480+
0,
481+
None,
482+
core::ptr::null_mut(),
483+
))?;
484+
}
485+
441486
mbedtls_ssl_conf_ca_chain(ssl_config, crt, core::ptr::null_mut());
442487

488+
mbedtls_ssl_conf_own_cert(ssl_config, client_crt, private_key);
489+
443490
#[cfg(feature = "async")]
444491
return Ok(Self {
445492
stream,
446493
ssl_context,
447494
ssl_config,
448495
crt,
496+
client_crt,
497+
private_key,
449498
eof: false,
450499
tx_buffer: Default::default(),
451500
rx_buffer: Default::default(),
@@ -469,6 +518,8 @@ pub mod asynch {
469518
free(self.ssl_config as *const _);
470519
free(self.ssl_context as *const _);
471520
free(self.crt as *const _);
521+
free(self.client_crt as *const _);
522+
free(self.private_key as *const _);
472523
}
473524
}
474525
}

0 commit comments

Comments
 (0)