|
| 1 | +use fluxheim_config::{TlsClientAuthMode, TlsConfig}; |
| 2 | +use openssl::ssl::{SslAcceptorBuilder, SslVerifyMode}; |
| 3 | +use openssl::stack::Stack; |
| 4 | +use openssl::x509::store::X509StoreBuilder; |
| 5 | +use openssl::x509::{X509, X509Name}; |
| 6 | + |
| 7 | +use super::OpenSslDownstreamAcceptorError; |
| 8 | +use crate::tls_input::{MAX_CA_BUNDLE_BYTES, MAX_CA_CERTIFICATES, read_bounded_file}; |
| 9 | + |
| 10 | +pub(super) fn apply_client_auth( |
| 11 | + builder: &mut SslAcceptorBuilder, |
| 12 | + tls: &TlsConfig, |
| 13 | +) -> Result<(), OpenSslDownstreamAcceptorError> { |
| 14 | + if tls.client_auth.mode == TlsClientAuthMode::Off { |
| 15 | + return Ok(()); |
| 16 | + } |
| 17 | + let ca_path = tls |
| 18 | + .client_auth |
| 19 | + .ca_path |
| 20 | + .as_deref() |
| 21 | + .ok_or(OpenSslDownstreamAcceptorError::MissingClientAuthCa)?; |
| 22 | + let verify = match tls.client_auth.mode { |
| 23 | + TlsClientAuthMode::Off => SslVerifyMode::NONE, |
| 24 | + TlsClientAuthMode::Optional => SslVerifyMode::PEER, |
| 25 | + TlsClientAuthMode::Required => SslVerifyMode::PEER | SslVerifyMode::FAIL_IF_NO_PEER_CERT, |
| 26 | + }; |
| 27 | + builder.set_verify(verify); |
| 28 | + let bytes = read_bounded_file(ca_path, MAX_CA_BUNDLE_BYTES).map_err(|source| { |
| 29 | + OpenSslDownstreamAcceptorError::ReadClientAuthCa { |
| 30 | + path: ca_path.to_path_buf(), |
| 31 | + source, |
| 32 | + } |
| 33 | + })?; |
| 34 | + let certificates = X509::stack_from_pem(&bytes).map_err(|source| { |
| 35 | + OpenSslDownstreamAcceptorError::ParseClientAuthCa { |
| 36 | + path: ca_path.to_path_buf(), |
| 37 | + source, |
| 38 | + } |
| 39 | + })?; |
| 40 | + if certificates.is_empty() { |
| 41 | + return Err(OpenSslDownstreamAcceptorError::EmptyClientAuthCa { |
| 42 | + path: ca_path.to_path_buf(), |
| 43 | + }); |
| 44 | + } |
| 45 | + if certificates.len() > MAX_CA_CERTIFICATES { |
| 46 | + return Err(OpenSslDownstreamAcceptorError::TooManyClientAuthCa { |
| 47 | + path: ca_path.to_path_buf(), |
| 48 | + count: certificates.len(), |
| 49 | + maximum: MAX_CA_CERTIFICATES, |
| 50 | + }); |
| 51 | + } |
| 52 | + let mut store = X509StoreBuilder::new().map_err(|source| { |
| 53 | + OpenSslDownstreamAcceptorError::ApplyClientAuthCa { |
| 54 | + path: ca_path.to_path_buf(), |
| 55 | + source, |
| 56 | + } |
| 57 | + })?; |
| 58 | + let mut names = Stack::<X509Name>::new().map_err(|source| { |
| 59 | + OpenSslDownstreamAcceptorError::ApplyClientAuthCaList { |
| 60 | + path: ca_path.to_path_buf(), |
| 61 | + source, |
| 62 | + } |
| 63 | + })?; |
| 64 | + for certificate in certificates { |
| 65 | + names |
| 66 | + .push(certificate.subject_name().to_owned().map_err(|source| { |
| 67 | + OpenSslDownstreamAcceptorError::ApplyClientAuthCaList { |
| 68 | + path: ca_path.to_path_buf(), |
| 69 | + source, |
| 70 | + } |
| 71 | + })?) |
| 72 | + .map_err( |
| 73 | + |source| OpenSslDownstreamAcceptorError::ApplyClientAuthCaList { |
| 74 | + path: ca_path.to_path_buf(), |
| 75 | + source, |
| 76 | + }, |
| 77 | + )?; |
| 78 | + store.add_cert(certificate).map_err(|source| { |
| 79 | + OpenSslDownstreamAcceptorError::ApplyClientAuthCa { |
| 80 | + path: ca_path.to_path_buf(), |
| 81 | + source, |
| 82 | + } |
| 83 | + })?; |
| 84 | + } |
| 85 | + builder.set_cert_store(store.build()); |
| 86 | + builder.set_client_ca_list(names); |
| 87 | + Ok(()) |
| 88 | +} |
0 commit comments