pingora_core/protocols/tls/
mod.rs

1// Copyright 2025 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The TLS layer implementations
16
17pub mod digest;
18pub use digest::*;
19
20#[cfg(feature = "openssl_derived")]
21mod boringssl_openssl;
22
23#[cfg(feature = "openssl_derived")]
24pub use boringssl_openssl::*;
25
26#[cfg(feature = "rustls")]
27mod rustls;
28
29#[cfg(feature = "rustls")]
30pub use rustls::*;
31
32#[cfg(not(feature = "any_tls"))]
33pub mod noop_tls;
34
35#[cfg(not(feature = "any_tls"))]
36pub use noop_tls::*;
37
38/// The protocol for Application-Layer Protocol Negotiation
39#[derive(Hash, Clone, Debug)]
40pub enum ALPN {
41    /// Prefer HTTP/1.1 only
42    H1,
43    /// Prefer HTTP/2 only
44    H2,
45    /// Prefer HTTP/2 over HTTP/1.1
46    H2H1,
47}
48
49impl std::fmt::Display for ALPN {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            ALPN::H1 => write!(f, "H1"),
53            ALPN::H2 => write!(f, "H2"),
54            ALPN::H2H1 => write!(f, "H2H1"),
55        }
56    }
57}
58
59impl ALPN {
60    /// Create a new ALPN according to the `max` and `min` version constraints
61    pub fn new(max: u8, min: u8) -> Self {
62        if max == 1 {
63            ALPN::H1
64        } else if min == 2 {
65            ALPN::H2
66        } else {
67            ALPN::H2H1
68        }
69    }
70
71    /// Return the max http version this [`ALPN`] allows
72    pub fn get_max_http_version(&self) -> u8 {
73        match self {
74            ALPN::H1 => 1,
75            _ => 2,
76        }
77    }
78
79    /// Return the min http version this [`ALPN`] allows
80    pub fn get_min_http_version(&self) -> u8 {
81        match self {
82            ALPN::H2 => 2,
83            _ => 1,
84        }
85    }
86
87    #[cfg(feature = "openssl_derived")]
88    pub(crate) fn to_wire_preference(&self) -> &[u8] {
89        // https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_alpn_select_cb.html
90        // "vector of nonempty, 8-bit length-prefixed, byte strings"
91        match self {
92            Self::H1 => b"\x08http/1.1",
93            Self::H2 => b"\x02h2",
94            Self::H2H1 => b"\x02h2\x08http/1.1",
95        }
96    }
97
98    #[cfg(feature = "any_tls")]
99    pub(crate) fn from_wire_selected(raw: &[u8]) -> Option<Self> {
100        match raw {
101            b"http/1.1" => Some(Self::H1),
102            b"h2" => Some(Self::H2),
103            _ => None,
104        }
105    }
106
107    #[cfg(feature = "rustls")]
108    pub(crate) fn to_wire_protocols(&self) -> Vec<Vec<u8>> {
109        match self {
110            ALPN::H1 => vec![b"http/1.1".to_vec()],
111            ALPN::H2 => vec![b"h2".to_vec()],
112            ALPN::H2H1 => vec![b"h2".to_vec(), b"http/1.1".to_vec()],
113        }
114    }
115}