Skip to content

Commit f79c037

Browse files
committed
Add support for SSL_group_to_name and SSL_get_negotiated_group
1 parent 9f29412 commit f79c037

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

openssl-sys/src/handwritten/ssl.rs

+5
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,8 @@ extern "C" {
951951
#[cfg(any(ossl110, libressl360))]
952952
pub fn SSL_get_security_level(s: *const SSL) -> c_int;
953953
}
954+
955+
extern "C" {
956+
#[cfg(ossl300)]
957+
pub fn SSL_group_to_name(ssl: *const SSL, id: c_int) -> *const c_char;
958+
}

openssl-sys/src/ssl.rs

+6
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ pub const SSL_CTRL_GET_MIN_PROTO_VERSION: c_int = 130;
363363
pub const SSL_CTRL_GET_MAX_PROTO_VERSION: c_int = 131;
364364
#[cfg(ossl300)]
365365
pub const SSL_CTRL_GET_TMP_KEY: c_int = 133;
366+
#[cfg(ossl300)]
367+
pub const SSL_CTRL_GET_NEGOTIATED_GROUP: c_int = 134;
366368

367369
pub unsafe fn SSL_CTX_set_tmp_dh(ctx: *mut SSL_CTX, dh: *mut DH) -> c_long {
368370
SSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh as *mut c_void)
@@ -519,6 +521,10 @@ cfg_if! {
519521
pub unsafe fn SSL_get_tmp_key(ssl: *mut SSL, key: *mut *mut EVP_PKEY) -> c_long {
520522
SSL_ctrl(ssl, SSL_CTRL_GET_TMP_KEY, 0, key as *mut c_void)
521523
}
524+
525+
pub unsafe fn SSL_get_negotiated_group(ssl: *mut SSL) -> c_int {
526+
SSL_ctrl(ssl, SSL_CTRL_GET_NEGOTIATED_GROUP, 0, ptr::null_mut()) as c_int
527+
}
522528
}
523529
}
524530

openssl/src/ssl/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use crate::x509::store::{X509Store, X509StoreBuilderRef, X509StoreRef};
8282
#[cfg(any(ossl102, boringssl, libressl261))]
8383
use crate::x509::verify::X509VerifyParamRef;
8484
use crate::x509::{X509Name, X509Ref, X509StoreContextRef, X509VerifyResult, X509};
85-
use crate::{cvt, cvt_n, cvt_p, init};
85+
use crate::{cvt, cvt_n, cvt_p, cvt_p_const, init};
8686
use bitflags::bitflags;
8787
use cfg_if::cfg_if;
8888
use foreign_types::{ForeignType, ForeignTypeRef, Opaque};
@@ -3484,6 +3484,47 @@ impl SslRef {
34843484
}
34853485
}
34863486
}
3487+
3488+
/// Returns the NID of the negotiated group used for the handshake key
3489+
/// exchange process.
3490+
/// For TLSv1.3 connections this typically reflects the state of the
3491+
/// current connection, though in the case of PSK-only resumption, the
3492+
/// returned value will be from a previous connection.
3493+
/// For earlier TLS versions, when a session has been resumed, it always
3494+
/// reflects the group used for key exchange during the initial handshake
3495+
/// (otherwise it is from the current, non-resumption, connection).
3496+
/// This can be called by either client or server.
3497+
/// If the NID for the shared group is unknown then the value is set to the
3498+
/// bitwise OR of TLSEXT_nid_unknown (0x1000000) and the id of the group.
3499+
#[corresponds(SSL_get_negotiated_group)]
3500+
#[cfg(ossl300)]
3501+
pub fn negotiated_group(&self) -> Result<c_int, ErrorStack> {
3502+
unsafe { cvt(ffi::SSL_get_negotiated_group(self.as_ptr())) }
3503+
}
3504+
3505+
/// Return retrieve the TLS group name associated with a given TLS
3506+
/// group ID, as registered via built-in or external providers and as
3507+
/// returned by a call to SSL_get1_groups() or SSL_get_shared_group().
3508+
///
3509+
/// If non-NULL, SSL_group_to_name() returns the TLS group name
3510+
/// corresponding to the given id as a NUL-terminated string.
3511+
/// If SSL_group_to_name() returns NULL, an error occurred; possibly no
3512+
/// corresponding tlsname was registered during provider initialisation.
3513+
///
3514+
/// Note that the return value is valid only during the lifetime of the
3515+
/// SSL object ssl.
3516+
#[corresponds(SSL_group_to_name)]
3517+
#[cfg(ossl300)]
3518+
pub fn group_to_name<'s>(&'s self, id: c_int) -> Result<&'s str, ErrorStack> {
3519+
unsafe {
3520+
match cvt_p_const(ffi::SSL_group_to_name(self.as_ptr(), id)) {
3521+
Ok(constp) => Ok(CStr::from_ptr(constp)
3522+
.to_str()
3523+
.expect("Invalid UTF8 in input")),
3524+
Err(e) => Err(e),
3525+
}
3526+
}
3527+
}
34873528
}
34883529

34893530
/// An SSL stream midway through the handshake process.

0 commit comments

Comments
 (0)