JosiahWI commented on code in PR #13349:
URL: https://github.com/apache/trafficserver/pull/13349#discussion_r3494262540


##########
src/iocore/net/SSLKeyUtils.cc:
##########
@@ -0,0 +1,186 @@
+/** @file
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include "SSLKeyUtils.h"
+#include "P_SSLUtils.h"
+
+#include <tscore/Diags.h>
+#ifdef OPENSSL_IS_OPENSSL3
+#include <tscore/ink_assert.h>
+#else
+#include <tscore/ink_config.h>
+#endif
+
+#ifdef OPENSSL_IS_OPENSSL3
+#include <openssl/evp.h>
+#include <openssl/decoder.h>
+#include <openssl/params.h>
+#include <openssl/ssl.h>
+#else
+#include <openssl/bio.h>
+#include <openssl/bn.h>
+#include <openssl/pem.h>
+#include <openssl/ssl.h>
+#endif
+
+#ifdef OPENSSL_IS_OPENSSL3
+
+EVP_PKEY *
+gen_dh_2048_256_pkey()
+{
+  scoped_PKEY_CTX pctx{EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)};
+  if (!pctx) {
+    Error("failed to create OpenSSL pkey context");
+    return nullptr;
+  }
+
+  if (EVP_PKEY_keygen_init(pctx.get()) <= 0) {
+    Error("failed to initialize OpenSSL keygen");
+    return nullptr;
+  }
+
+  char             prime_group[]{"dh_2048_256"};
+  OSSL_PARAM const params[]{OSSL_PARAM_utf8_string("group", prime_group, 
sizeof(prime_group) - 1), OSSL_PARAM_END};
+
+  if (!EVP_PKEY_CTX_set_params(pctx.get(), params)) {
+    Error("SSL dhparams source returned invalid parameters");
+    return nullptr;
+  }
+
+  EVP_PKEY *pkey{};
+  EVP_PKEY_generate(pctx.get(), &pkey);
+
+  return pkey;
+}
+
+EVP_PKEY *
+load_dhparams_file(char const *dhparams_file)
+{
+  EVP_PKEY          *pkey{};
+  scoped_Decoder_CTX dctx{OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, 
"DH", OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, NULL, NULL)};
+  if (!dctx) {
+    Error("failed to create OpenSSL decoder context");
+    return nullptr;
+  }
+
+  ink_assert(OSSL_DECODER_CTX_get_num_decoders(dctx.get()) > 0);
+  scoped_BIO bio{BIO_new_file(dhparams_file, "r")};
+  if (!OSSL_DECODER_from_bio(dctx.get(), bio.get())) {
+    Error("SSL dhparams source returned invalid parameters");
+    return nullptr;
+  }
+
+  return pkey;
+}
+
+bool
+set_ctx_dh(SSL_CTX *ctx, dh_key_t *pkey)
+{
+  bool result{SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE) && 
SSL_CTX_set0_tmp_dh_pkey(ctx, pkey)};
+  if (!result) {
+    EVP_PKEY_free(pkey);
+  }
+  return result;
+}

Review Comment:
   Of special note: the OpenSSL 3.0 API takes ownership of the key, whereas the 
older API does not. Therefore, the control flow with respect to memory 
management is different between the two `set_ctx_dh` implementations, which 
could be surprising.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to