Skip to main content

rama/cli/service/
tls.rs

1//! Negotiated TLS values displayed by the fingerprint and echo services.
2
3use crate::{
4    extensions::Extensions,
5    tls::{KeyExchangeKind, SupportedGroup, client::NegotiatedTlsParameters},
6};
7use serde::Serialize;
8
9/// A server's view of the negotiated parameters on the inbound connection.
10/// TLS identifiers include their wire values, including unrecognized groups.
11/// Optional fields serialize as null when unused or unreported by the TLS backend.
12#[derive(Debug, Serialize)]
13pub struct NegotiatedTlsInfo {
14    pub protocol_version: String,
15    pub alpn: Option<String>,
16    pub cipher_suite: Option<String>,
17    pub key_exchange_group: Option<String>,
18    pub key_exchange_kind: Option<KeyExchangeKind>,
19    pub resumed: Option<bool>,
20    pub certificate_compression: Option<String>,
21    pub client_signature_scheme: Option<String>,
22}
23
24impl NegotiatedTlsInfo {
25    /// Read negotiated values even when the ClientHello was not captured.
26    #[must_use]
27    pub fn from_extensions(ext: &Extensions) -> Option<Self> {
28        let params = ext.get_ref::<NegotiatedTlsParameters>()?;
29        let algorithms = params.algorithms;
30        Some(Self {
31            protocol_version: params.protocol_version.to_string(),
32            alpn: params
33                .application_layer_protocol
34                .as_ref()
35                .map(ToString::to_string),
36            cipher_suite: algorithms.cipher_suite.map(|suite| suite.to_string()),
37            key_exchange_group: algorithms.key_exchange_group.map(|group| group.to_string()),
38            key_exchange_kind: algorithms
39                .key_exchange_group
40                .map(SupportedGroup::key_exchange_kind),
41            resumed: params.resumed,
42            certificate_compression: algorithms
43                .certificate_compression
44                .map(|algorithm| algorithm.to_string()),
45            client_signature_scheme: algorithms
46                .peer_signature_scheme
47                .map(|scheme| scheme.to_string()),
48        })
49    }
50}