|
| 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 | +//! Extended functionalities for s2n-tls |
| 16 | +
|
| 17 | +use s2n_tls::connection::Connection; |
| 18 | +use s2n_tls::error::Error; |
| 19 | + |
| 20 | +/// Export keying material from a TLS connection |
| 21 | +/// |
| 22 | +/// Derives keying material for application use in accordance with RFC 5705. |
| 23 | +/// |
| 24 | +/// Note: Currently only available with TLS 1.3 connections. |
| 25 | +/// |
| 26 | +/// See [tls_exporter](https://docs.rs/s2n-tls/latest/s2n_tls/connection/struct.Connection.html#method.tls_exporter). |
| 27 | +pub fn ssl_export_keying_material( |
| 28 | + conn: &Connection, |
| 29 | + out: &mut [u8], |
| 30 | + label: &str, |
| 31 | + context: Option<&[u8]>, |
| 32 | +) -> Result<(), Error> { |
| 33 | + let context_bytes = context.unwrap_or(&[]); |
| 34 | + conn.tls_exporter(label.as_bytes(), context_bytes, out) |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(test)] |
| 38 | +mod tests { |
| 39 | + use super::*; |
| 40 | + use s2n_tls::config::Builder; |
| 41 | + use s2n_tls::enums::Mode; |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn test_ssl_export_keying_material_exists() { |
| 45 | + // This test verifies that ssl_export_keying_material function exists |
| 46 | + // and has the correct signature. Actual functional testing requires |
| 47 | + // an established TLS connection. |
| 48 | + let config = Builder::new().build().unwrap(); |
| 49 | + let mut conn = s2n_tls::connection::Connection::new(Mode::Client); |
| 50 | + conn.set_config(config).unwrap(); |
| 51 | + let mut out = [0u8; 32]; |
| 52 | + |
| 53 | + // This will fail since there's no established connection, but verifies |
| 54 | + // the function signature is correct |
| 55 | + let _ = ssl_export_keying_material(&conn, &mut out, "test", None); |
| 56 | + } |
| 57 | +} |
0 commit comments