|
| 1 | +use alloc::vec; |
| 2 | +use alloc::{boxed::Box, sync::Arc, vec::Vec}; |
| 3 | +use core::fmt::Debug; |
| 4 | +use mbedtls::pk::ECDSA_MAX_LEN; |
| 5 | +use std::sync::Mutex; |
| 6 | +use utils::error::mbedtls_err_into_rustls_err; |
| 7 | +use utils::hash::{buffer_for_hash_type, rustls_signature_scheme_to_mbedtls_hash_type}; |
| 8 | +use utils::pk::{rustls_signature_scheme_to_mbedtls_pk_options, rustls_signature_scheme_to_mbedtls_pk_type}; |
| 9 | + |
| 10 | +/// |
| 11 | +struct MbedTlsSigner(Arc<Mutex<mbedtls::pk::Pk>>, rustls::SignatureScheme); |
| 12 | + |
| 13 | +impl Debug for MbedTlsSigner { |
| 14 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 15 | + f.debug_tuple("MbedTlsSigner") |
| 16 | + .field(&"Arc<Mutex<mbedtls::pk::Pk>>") |
| 17 | + .field(&self.1) |
| 18 | + .finish() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl rustls::sign::Signer for MbedTlsSigner { |
| 23 | + fn sign(&self, message: &[u8]) -> Result<Vec<u8>, rustls::Error> { |
| 24 | + let hash_type = rustls_signature_scheme_to_mbedtls_hash_type(self.1); |
| 25 | + let mut hash = buffer_for_hash_type(hash_type).ok_or_else(|| rustls::Error::General("unexpected hash type".into()))?; |
| 26 | + let hash_size = mbedtls::hash::Md::hash(hash_type, message, &mut hash).map_err(mbedtls_err_into_rustls_err)?; |
| 27 | + |
| 28 | + let mut pk = self |
| 29 | + .0 |
| 30 | + .lock() |
| 31 | + .expect("poisoned PK lock!"); |
| 32 | + if let Some(opts) = rustls_signature_scheme_to_mbedtls_pk_options(self.1) { |
| 33 | + pk.set_options(opts); |
| 34 | + } |
| 35 | + |
| 36 | + fn sig_len_for_pk(pk: &mbedtls::pk::Pk) -> usize { |
| 37 | + match pk.pk_type() { |
| 38 | + mbedtls::pk::Type::Eckey | mbedtls::pk::Type::EckeyDh | mbedtls::pk::Type::Ecdsa => ECDSA_MAX_LEN, |
| 39 | + _ => pk.len() / 8, |
| 40 | + } |
| 41 | + } |
| 42 | + let mut sig = vec![0; sig_len_for_pk(&pk)]; |
| 43 | + let sig_len = pk |
| 44 | + .sign( |
| 45 | + hash_type, |
| 46 | + &hash[..hash_size], |
| 47 | + &mut sig, |
| 48 | + &mut crate::rng::rng_new().ok_or(rustls::Error::FailedToGetRandomBytes)?, |
| 49 | + ) |
| 50 | + .map_err(mbedtls_err_into_rustls_err)?; |
| 51 | + sig.truncate(sig_len); |
| 52 | + Ok(sig) |
| 53 | + } |
| 54 | + |
| 55 | + fn scheme(&self) -> rustls::SignatureScheme { |
| 56 | + self.1 |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// A [`SigningKey`] implemented by using [`mbedtls`] |
| 61 | +/// |
| 62 | +/// [`SigningKey`]: rustls::sign::SigningKey |
| 63 | +pub struct MbedTlsPkSigningKey(pub Arc<Mutex<mbedtls::pk::Pk>>); |
| 64 | + |
| 65 | +impl Debug for MbedTlsPkSigningKey { |
| 66 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 67 | + f.debug_tuple("MbedTlsPkSigningKey") |
| 68 | + .field(&"Arc<Mutex<mbedtls::pk::Pk>>") |
| 69 | + .finish() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl rustls::sign::SigningKey for MbedTlsPkSigningKey { |
| 74 | + fn choose_scheme(&self, offered: &[rustls::SignatureScheme]) -> Option<Box<dyn rustls::sign::Signer>> { |
| 75 | + for scheme in offered { |
| 76 | + let scheme_type = rustls_signature_scheme_to_mbedtls_pk_type(scheme); |
| 77 | + if let Some(scheme_type) = scheme_type { |
| 78 | + if scheme_type |
| 79 | + == self |
| 80 | + .0 |
| 81 | + .lock() |
| 82 | + .expect("poisoned pk lock") |
| 83 | + .pk_type() |
| 84 | + { |
| 85 | + let signer = MbedTlsSigner(self.0.clone(), *scheme); |
| 86 | + return Some(Box::new(signer)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + None |
| 91 | + } |
| 92 | + |
| 93 | + fn algorithm(&self) -> rustls::SignatureAlgorithm { |
| 94 | + use rustls::SignatureAlgorithm; |
| 95 | + match self |
| 96 | + .0 |
| 97 | + .lock() |
| 98 | + .expect("poisoned pk lock") |
| 99 | + .pk_type() |
| 100 | + { |
| 101 | + mbedtls::pk::Type::Rsa => SignatureAlgorithm::RSA, |
| 102 | + mbedtls::pk::Type::Ecdsa => SignatureAlgorithm::ECDSA, |
| 103 | + mbedtls::pk::Type::RsassaPss => SignatureAlgorithm::RSA, |
| 104 | + mbedtls::pk::Type::Eckey => SignatureAlgorithm::ECDSA, |
| 105 | + mbedtls::pk::Type::RsaAlt => SignatureAlgorithm::DSA, |
| 106 | + mbedtls::pk::Type::EckeyDh => SignatureAlgorithm::Anonymous, |
| 107 | + mbedtls::pk::Type::Custom => SignatureAlgorithm::Anonymous, |
| 108 | + mbedtls::pk::Type::None => SignatureAlgorithm::Anonymous, |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments