Debian Codegraph @

crt/s2n/bindings/rust/standard/tls-harness/src/openssl_extension.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! This module defines an "extension" trait to add our own bindings to the openssl
5 //! crate. Ideally all of this logic would live _in_ the openssl crate, but they
6 //! don't always accept PRs.
7
8 use openssl::ssl::{SslContextBuilder, SslRef, SslStream};
9 use openssl_sys::SSL_CTX;
10
11 use foreign_types_shared::ForeignTypeRef;
12
13 fn ssl_get_secure_renegotiation_support(ssl: *mut openssl_sys::SSL) -> std::ffi::c_long {
14 const SSL_CTRL_GET_RI_SUPPORT: std::ffi::c_int = 76;
15 unsafe { openssl_sys::SSL_ctrl(ssl, SSL_CTRL_GET_RI_SUPPORT, 0, std::ptr::null_mut()) }
16 }
17 extern "C" {
18 /// ```c
19 /// int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size);
20 /// ```
21 pub fn SSL_CTX_set_block_padding(ctx: *mut SSL_CTX, block_size: usize) -> std::ffi::c_int;
22
23 pub fn SSL_renegotiate_pending(ssl: *mut openssl_sys::SSL) -> std::ffi::c_int;
24 pub fn SSL_renegotiate(ssl: *mut openssl_sys::SSL) -> std::ffi::c_int;
25 }
26
27 pub trait SslContextExtension {
28 fn set_block_padding(&mut self, block_size: usize);
29 }
30
31 impl SslContextExtension for SslContextBuilder {
32 fn set_block_padding(&mut self, block_size: usize) {
33 unsafe {
34 // > The SSL_CTX_set_block_padding() and SSL_set_block_padding() functions
35 // > return 1 on success or 0 if block_size is too large.
36 // > https://docs.openssl.org/master/man3/SSL_CTX_set_record_padding_callback
37 let res = SSL_CTX_set_block_padding(self.as_ptr(), block_size as _);
38 assert_eq!(res, 1);
39 }
40 }
41 }
42
43 pub trait SslStreamExtension {
44 fn mut_ssl(&mut self) -> &mut SslRef;
45 }
46
47 impl<T> SslStreamExtension for SslStream<T> {
48 /// Required to obtain a mutable `SslRef` from `SslStream` for test-only
49 /// logic. The cast is safe under our usage and tracked by an
50 /// upstream PR: https://github.com/sfackler/rust-openssl/pull/2223
51 #[allow(invalid_reference_casting)]
52 fn mut_ssl(&mut self) -> &mut SslRef {
53 unsafe { &mut *(self.ssl() as *const openssl::ssl::SslRef as *mut openssl::ssl::SslRef) }
54 }
55 }
56
57 pub trait SslExtension {
58 /// Returns `true` if the peer is patched against insecure renegotiation.
59 fn secure_renegotiation_support(&self) -> bool;
60
61 fn renegotiate_pending(&self) -> bool;
62
63 /// Schedule a renegotiate request to be sent on the next io.
64 fn renegotiate(&mut self);
65 }
66
67 impl SslExtension for openssl::ssl::SslRef {
68 fn secure_renegotiation_support(&self) -> bool {
69 let result = ssl_get_secure_renegotiation_support(self.as_ptr());
70 match result {
71 1 => true,
72 0 => false,
73 _ => unreachable!("unexpected OpenSSL return value; expected 0 or 1"),
74 }
75 }
76
77 fn renegotiate_pending(&self) -> bool {
78 match unsafe { SSL_renegotiate_pending(self.as_ptr()) } {
79 1 => true,
80 0 => false,
81 _ => unreachable!("unexpected OpenSSL return value; expected 0 or 1"),
82 }
83 }
84
85 fn renegotiate(&mut self) {
86 // https://docs.openssl.org/3.3/man3/SSL_key_update/#return-values
87 let result = unsafe { SSL_renegotiate(self.as_ptr()) };
88 assert_eq!(result, 1);
89 }
90 }