Skip to content

Commit 515f1bc

Browse files
kraemvctz
authored andcommitted
Added: TLSGroupInfo
Added: New Entrypoint to build Added: New NIDs Added: Test group_to_name in tests/constants.c
1 parent 3ecd9e5 commit 515f1bc

6 files changed

Lines changed: 97 additions & 7 deletions

File tree

MATRIX.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
| `SSL_get_version` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
385385
| `SSL_get_wbio` | | :white_check_mark: | :white_check_mark: | :white_check_mark: |
386386
| `SSL_get_wfd` | | | | |
387-
| `SSL_group_to_name` | | | | |
387+
| `SSL_group_to_name` | | | | :white_check_mark: |
388388
| `SSL_has_matching_session_id` | | | | |
389389
| `SSL_has_pending` | | | | :white_check_mark: |
390390
| `SSL_in_before` | | | | :white_check_mark: |

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ const ENTRYPOINTS: &[&str] = &[
193193
"SSL_get_verify_result",
194194
"SSL_get_version",
195195
"SSL_get_wbio",
196+
"SSL_group_to_name",
196197
"SSL_has_pending",
197198
"SSL_in_before",
198199
"SSL_in_init",

src/constants.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,45 @@ pub fn sig_scheme_to_type_nid(scheme: SignatureScheme) -> Option<c_int> {
120120
}
121121
}
122122

123+
pub fn named_group_to_tls_name(id: NamedGroup) -> Option<&'static CStr> {
124+
Some(match id {
125+
NamedGroup::secp256r1 => c"secp256r1",
126+
NamedGroup::secp384r1 => c"secp384r1",
127+
NamedGroup::secp521r1 => c"secp521r1",
128+
NamedGroup::X25519 => c"x25519",
129+
NamedGroup::X448 => c"x448",
130+
NamedGroup::FFDHE2048 => c"ffdhe2048",
131+
NamedGroup::FFDHE3072 => c"ffdhe3072",
132+
NamedGroup::FFDHE4096 => c"ffdhe4096",
133+
NamedGroup::FFDHE6144 => c"ffdhe6144",
134+
NamedGroup::FFDHE8192 => c"ffdhe8192",
135+
NamedGroup::MLKEM512 => c"MLKEM512",
136+
NamedGroup::MLKEM768 => c"MLKEM768",
137+
NamedGroup::MLKEM1024 => c"MLKEM1024",
138+
NamedGroup::X25519MLKEM768 => c"X25519MLKEM768",
139+
NamedGroup::secp256r1MLKEM768 => c"SecP256r1MLKEM768",
140+
_ => return None,
141+
})
142+
}
143+
123144
pub fn named_group_to_nid(group: NamedGroup) -> Option<c_int> {
124145
use NamedGroup::*;
125146

147+
// See TLSEXT_nid_unknown from tls1.h - openssl-sys does not
148+
// have a constant for this to import.
149+
const TLSEXT_NID_UNKNOWN: c_int = 0x1000000;
126150
// See NID_ffhdhe* from obj_mac.h - openssl-sys does not have
127151
// constants for these to import.
128152
const NID_FFDHE2048: c_int = 1126;
129153
const NID_FFDHE3072: c_int = 1127;
130154
const NID_FFDHE4096: c_int = 1128;
131155
const NID_FFDHE6144: c_int = 1129;
132156
const NID_FFDHE8192: c_int = 1130;
133-
134-
// See TLSEXT_nid_unknown from tls1.h - openssl-sys does not
135-
// have a constant for this to import.
136-
const TLSEXT_NID_UNKNOWN: c_int = 0x1000000;
157+
// See NID_ML_KEM_* from obj_mac.h - openssl-sys does not have
158+
// constants for these to import.
159+
const NID_ML_KEM_512: c_int = 1454;
160+
const NID_ML_KEM_768: c_int = 1455;
161+
const NID_ML_KEM_1024: c_int = 1456;
137162

138163
match group {
139164
secp256r1 => Some(NID_X9_62_prime256v1),
@@ -146,6 +171,9 @@ pub fn named_group_to_nid(group: NamedGroup) -> Option<c_int> {
146171
FFDHE4096 => Some(NID_FFDHE4096),
147172
FFDHE6144 => Some(NID_FFDHE6144),
148173
FFDHE8192 => Some(NID_FFDHE8192),
174+
MLKEM512 => Some(NID_ML_KEM_512),
175+
MLKEM768 => Some(NID_ML_KEM_768),
176+
MLKEM1024 => Some(NID_ML_KEM_1024),
149177
other => Some(TLSEXT_NID_UNKNOWN | u16::from(other) as c_int),
150178
}
151179
}

src/entry.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
2222

2323
use crate::bio::{Bio, BIO, BIO_METHOD};
2424
use crate::callbacks::SslCallbackContext;
25-
use crate::constants::{named_group_to_nid, sig_scheme_to_type_nid};
25+
use crate::constants::{named_group_to_nid, named_group_to_tls_name, sig_scheme_to_type_nid};
2626
use crate::error::{ffi_panic_boundary, Error, MysteriouslyOppositeReturnValue};
2727
use crate::evp_pkey::EvpPkey;
2828
use crate::ex_data::ExData;
@@ -1475,6 +1475,22 @@ entry! {
14751475
}
14761476
}
14771477

1478+
entry! {
1479+
pub fn _SSL_group_to_name(ssl: *const SSL, id: c_int) -> *const c_char {
1480+
try_clone_arc!(ssl)
1481+
.get()
1482+
.get_groups()
1483+
.iter()
1484+
.find_map(|group| match named_group_to_nid(group.name()) {
1485+
Some(nid) if nid == id => {
1486+
named_group_to_tls_name(group.name()).map(|info| info.as_ptr())
1487+
}
1488+
_ => None,
1489+
})
1490+
.unwrap_or_else(ptr::null)
1491+
}
1492+
}
1493+
14781494
entry! {
14791495
pub fn _SSL_version(ssl: *const SSL) -> c_int {
14801496
try_clone_arc!(ssl)

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use openssl_sys::{
1111
EVP_PKEY, SSL_ERROR_NONE, SSL_ERROR_SSL, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, X509,
1212
X509_STORE, X509_V_ERR_UNSPECIFIED,
1313
};
14+
1415
use rustls::client::Resumption;
1516
use rustls::crypto::{aws_lc_rs as provider, SupportedKxGroup};
1617
use rustls::pki_types::{CertificateDer, ServerName};
@@ -459,6 +460,7 @@ pub struct SslContext {
459460
info_callback: callbacks::InfoCallbackConfig,
460461
client_hello_callback: callbacks::ClientHelloCallbackConfig,
461462
auth_keys: sign::CertifiedKeySet,
463+
groups: Vec<&'static dyn SupportedKxGroup>,
462464
max_early_data: u32,
463465
}
464466

@@ -491,6 +493,7 @@ impl SslContext {
491493
info_callback: callbacks::InfoCallbackConfig::default(),
492494
client_hello_callback: callbacks::ClientHelloCallbackConfig::default(),
493495
auth_keys: sign::CertifiedKeySet::default(),
496+
groups: provider::default_provider().kx_groups.clone(),
494497
max_early_data: 0,
495498
}
496499
}
@@ -521,6 +524,10 @@ impl SslContext {
521524
self.raw_options
522525
}
523526

527+
fn get_groups(&self) -> &[&'static dyn SupportedKxGroup] {
528+
&self.groups
529+
}
530+
524531
fn get_num_tickets(&self) -> usize {
525532
self.num_tickets
526533
}
@@ -880,6 +887,10 @@ impl Ssl {
880887
self.raw_options
881888
}
882889

890+
fn get_groups(&self) -> &[&'static dyn SupportedKxGroup] {
891+
self.ctx.get().get_groups()
892+
}
893+
883894
fn get_num_tickets(&self) -> usize {
884895
self.num_tickets
885896
}

tests/constants.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@
33
*/
44

55
#include <stdio.h>
6+
#include <stdlib.h>
67

8+
#include <openssl/obj_mac.h>
79
#include <openssl/ssl.h>
810

9-
int main(void) {
11+
void print_group_to_name() {
12+
// secp{256, 384}r1, x25519
13+
int supported_nids[] = {NID_X9_62_prime256v1, NID_secp384r1, NID_X25519};
14+
SSL_CTX *ctx = SSL_CTX_new(TLS_method());
15+
if (!ctx) {
16+
printf("Failed allocating SSL context\n");
17+
return;
18+
}
19+
SSL *ssl_inst = SSL_new(ctx);
20+
if (!ssl_inst) {
21+
SSL_CTX_free(ctx);
22+
printf("Failed allocating SSL struct\n");
23+
return;
24+
}
25+
26+
for (size_t i = 0; i < sizeof(supported_nids) / sizeof(int); i += 1) {
27+
const char *group_name = SSL_group_to_name(ssl_inst, supported_nids[i]);
28+
if (group_name)
29+
printf("%d: '%s'\n", supported_nids[i], group_name);
30+
else
31+
printf("Unknown: %d\n", supported_nids[i]);
32+
}
33+
34+
SSL_free(ssl_inst);
35+
SSL_CTX_free(ctx);
36+
}
37+
38+
void print_alert_desc_string() {
1039
for (int i = -1; i < 260; i++) {
1140
printf("%d: '%s' '%s'\n", i, SSL_alert_desc_string(i),
1241
SSL_alert_desc_string_long(i));
1342
}
43+
}
44+
45+
int main(void) {
46+
print_alert_desc_string();
47+
print_group_to_name();
1448
return 0;
1549
}

0 commit comments

Comments
 (0)