Add SSL_get0_group_name() to get name of the group used for KEX
authorAlex Bozarth <ajbozart@us.ibm.com>
Fri, 19 May 2023 19:08:41 +0000 (14:08 -0500)
committerTomas Mraz <tomas@openssl.org>
Tue, 6 Jun 2023 15:03:41 +0000 (17:03 +0200)
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20866)

doc/build.info
doc/man3/SSL_get0_group_name.pod [new file with mode: 0644]
include/openssl/ssl.h.in
ssl/s3_lib.c
ssl/ssl_local.h
ssl/t1_lib.c
test/sslapitest.c
util/libssl.num

index 6846b552d8e4affa3fd0c02aef8ad3ebb5b4b9cc..e4a78e0d1444a58584713517a8f8a182623db201 100644 (file)
@@ -2495,6 +2495,10 @@ DEPEND[html/man3/SSL_get0_connection.html]=man3/SSL_get0_connection.pod
 GENERATE[html/man3/SSL_get0_connection.html]=man3/SSL_get0_connection.pod
 DEPEND[man/man3/SSL_get0_connection.3]=man3/SSL_get0_connection.pod
 GENERATE[man/man3/SSL_get0_connection.3]=man3/SSL_get0_connection.pod
+DEPEND[html/man3/SSL_get0_group_name.html]=man3/SSL_get0_group_name.pod
+GENERATE[html/man3/SSL_get0_group_name.html]=man3/SSL_get0_group_name.pod
+DEPEND[man/man3/SSL_get0_group_name.3]=man3/SSL_get0_group_name.pod
+GENERATE[man/man3/SSL_get0_group_name.3]=man3/SSL_get0_group_name.pod
 DEPEND[html/man3/SSL_get0_peer_rpk.html]=man3/SSL_get0_peer_rpk.pod
 GENERATE[html/man3/SSL_get0_peer_rpk.html]=man3/SSL_get0_peer_rpk.pod
 DEPEND[man/man3/SSL_get0_peer_rpk.3]=man3/SSL_get0_peer_rpk.pod
@@ -3523,6 +3527,7 @@ html/man3/SSL_export_keying_material.html \
 html/man3/SSL_extension_supported.html \
 html/man3/SSL_free.html \
 html/man3/SSL_get0_connection.html \
+html/man3/SSL_get0_group_name.html \
 html/man3/SSL_get0_peer_rpk.html \
 html/man3/SSL_get0_peer_scts.html \
 html/man3/SSL_get_SSL_CTX.html \
@@ -4160,6 +4165,7 @@ man/man3/SSL_export_keying_material.3 \
 man/man3/SSL_extension_supported.3 \
 man/man3/SSL_free.3 \
 man/man3/SSL_get0_connection.3 \
+man/man3/SSL_get0_group_name.3 \
 man/man3/SSL_get0_peer_rpk.3 \
 man/man3/SSL_get0_peer_scts.3 \
 man/man3/SSL_get_SSL_CTX.3 \
diff --git a/doc/man3/SSL_get0_group_name.pod b/doc/man3/SSL_get0_group_name.pod
new file mode 100644 (file)
index 0000000..a96340a
--- /dev/null
@@ -0,0 +1,43 @@
+=pod
+
+=head1 NAME
+
+SSL_get0_group_name - get name of the group that was used for the key
+agreement of the current TLS session establishment
+
+=head1 SYNOPSIS
+
+ #include <openssl/ssl.h>
+
+ const char *SSL_get0_group_name(SSL *s);
+
+=head1 DESCRIPTION
+
+SSL_get0_group_name() returns the name of the group that was used for
+the key agreement of the current TLS session establishment.
+
+
+=head1 RETURN VALUES
+
+If non-NULL, SSL_get0_group_name() returns the name of the group that was used for
+the key agreement of the current TLS session establishment.
+If SSL_get0_group_name() returns NULL, an error occurred; possibly no TLS session
+has been established.
+
+Note that the return value is valid only during the lifetime of the
+SSL object I<ssl>.
+
+=head1 SEE ALSO
+
+L<ssl(7)>
+
+=head1 COPYRIGHT
+
+Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
+
+Licensed under the Apache License 2.0 (the "License").  You may not use
+this file except in compliance with the License.  You can obtain a copy
+in the file LICENSE in the source distribution or at
+L<https://www.openssl.org/source/license.html>.
+
+=cut
index bf5ff7c06b34ab1585af4542b61bad1307ed2d6b..beedd8956d60b4c6f8348b449055967933a16905 100644 (file)
@@ -1504,6 +1504,7 @@ DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION)
 # define SSL_get_max_proto_version(s) \
         SSL_ctrl(s, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL)
 
+const char *SSL_get0_group_name(SSL *s);
 const char *SSL_group_to_name(SSL *s, int id);
 
 /* Backwards compatibility, original 1.1.0 names */
index 835af33fea396c18543cfe6857cab1e223d08163..10cff08eaeffbf95d891dc2ec76c290e157319f0 100644 (file)
@@ -5022,6 +5022,22 @@ int ssl_encapsulate(SSL_CONNECTION *s, EVP_PKEY *pubkey,
     return rv;
 }
 
+const char *SSL_get0_group_name(SSL *s)
+{
+    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
+    unsigned int id;
+
+    if (sc == NULL)
+        return NULL;
+
+    if (SSL_CONNECTION_IS_TLS13(sc) && sc->s3.did_kex)
+        id = sc->s3.group_id;
+    else
+        id = sc->session->kex_group;
+
+    return tls1_group_id2name(s->ctx, id);
+}
+
 const char *SSL_group_to_name(SSL *s, int nid) {
     int group_id = 0;
     const TLS_GROUP_INFO *cinf = NULL;
index 7ab84acc8064bd1e9731f1f049e203a4c7512982..decb02a207b71a4fff2ff3db2788821eed80e059 100644 (file)
@@ -2767,6 +2767,7 @@ __owur int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s);
 SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n);
 
 __owur const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t curve_id);
+__owur const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id);
 __owur int tls1_group_id2nid(uint16_t group_id, int include_unknown);
 __owur uint16_t tls1_nid2group_id(int nid);
 __owur int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id,
index 189f241f7af4652ab89ed8cd40a7b06510b241db..576c7a3271e5577aaa3114e06a02c5ebd52b4fba 100644 (file)
@@ -755,6 +755,16 @@ const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id)
     return NULL;
 }
 
+const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id)
+{
+    const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id);
+
+    if (tls_group_info == NULL)
+        return NULL;
+
+    return tls_group_info->tlsname;
+}
+
 int tls1_group_id2nid(uint16_t group_id, int include_unknown)
 {
     size_t i;
index be1d742021e080b64e1e13b12adb9eaf141f9551..d2c6c774f50aa2675d3ddcdbf6830b6fa625fe59 100644 (file)
@@ -5037,6 +5037,9 @@ static int test_key_exchange(int idx)
 
     /* We don't implement RFC 7919 named groups for TLS 1.2. */
     if (idx != 13) {
+        if (!TEST_str_eq(SSL_get0_group_name(serverssl), kexch_name0)
+            || !TEST_str_eq(SSL_get0_group_name(clientssl), kexch_name0))
+            goto end;
         if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
             goto end;
         if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
@@ -9495,6 +9498,10 @@ static int test_pluggable_group(int idx)
                      SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
         goto end;
 
+    if (!TEST_str_eq(group_name, SSL_get0_group_name(serverssl))
+        || !TEST_str_eq(group_name, SSL_get0_group_name(clientssl)))
+        goto end;
+
     testresult = 1;
 
  end:
index 7f7b763075182247566447415cf399ed49c1910f..8377ed1b5789a30740951fa4f13fb4c092f2f91b 100644 (file)
@@ -576,3 +576,4 @@ SSL_get_conn_close_info                 ?   3_2_0   EXIST::FUNCTION:
 SSL_set_incoming_stream_policy          ?      3_2_0   EXIST::FUNCTION:
 SSL_handle_events                       ?      3_2_0   EXIST::FUNCTION:
 SSL_get_event_timeout                   ?      3_2_0   EXIST::FUNCTION:
+SSL_get0_group_name                     ?      3_2_0   EXIST::FUNCTION: