Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XrdCrypto] Generate DH parameters on first call to XrdCryptosslCipher #1595

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 17 additions & 22 deletions src/XrdCrypto/XrdCryptosslCipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/* */
/* ************************************************************************** */
#include <cstring>
#include <assert.h>
#include <cassert>

#include "XrdSut/XrdSutRndm.hh"
#include "XrdCrypto/XrdCryptosslTrace.hh"
Expand Down Expand Up @@ -475,32 +475,27 @@ XrdCryptosslCipher::XrdCryptosslCipher(bool padded, int bits, char *pub,
cipher = 0;
deflength = 1;

static DH *dhparms = [] {
DH *dh = DH_new();
DEBUG("generate DH parameters");
DH_generate_parameters_ex(dh, kDHMINBITS, DH_GENERATOR_5, NULL);
DEBUG("generate DH parameters done");
return dh;
}();

assert(DH_get0_p(dhparms));

if (!pub) {
static DH *dhparms = [] {
DH *dh = DH_new();
DEBUG("generate DH parameters");
DH_generate_parameters_ex(dh, kDHMINBITS, DH_GENERATOR_5, NULL);
DEBUG("generate DH parameters done");
return dh;
}();

DEBUG("configure DH parameters");
// Set params for DH object
assert(DH_get0_p(dhparms));
fDH = DHparams_dup(dhparms);
if (fDH) {
int prc = 0;
DH_check(fDH,&prc);
if (prc == 0) {
//
// Generate DH key
if (DH_generate_key(fDH)) {
// Init context
ctx = EVP_CIPHER_CTX_new();
if (ctx)
valid = 1;
}
//
// Generate DH key
if (DH_generate_key(fDH)) {
// Init context
ctx = EVP_CIPHER_CTX_new();
if (ctx)
valid = 1;
}
}

Expand Down
54 changes: 26 additions & 28 deletions src/XrdCrypto/openssl3/XrdCryptosslCipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/* */
/* ************************************************************************** */
#include <cstring>
#include <cassert>

#include "XrdSut/XrdSutRndm.hh"
#include "XrdCrypto/XrdCryptosslTrace.hh"
Expand Down Expand Up @@ -487,7 +488,7 @@ XrdCryptosslCipher::XrdCryptosslCipher(bool padded, int bits, char *pub,
// Constructor for key agreement.
// If pub is not defined, generates a DH full key,
// the public part and parameters can be retrieved using Public().
// The number of random bits to be used in 'bits'.
// 'bits' is ignored (DH key is generated once)
// If pub is defined with the public part and parameters of the
// counterpart fully initialize a cipher with that information.
// Sets also the name to 't', if different from the default one.
Expand All @@ -503,35 +504,32 @@ XrdCryptosslCipher::XrdCryptosslCipher(bool padded, int bits, char *pub,
deflength = 1;

if (!pub) {
DEBUG("generate DH full key");
static EVP_PKEY *dhparms = [] {
DEBUG("generate DH parameters");
EVP_PKEY *dhParam = 0;
EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, 0);
EVP_PKEY_paramgen_init(pkctx);
EVP_PKEY_CTX_set_dh_paramgen_prime_len(pkctx, kDHMINBITS);
EVP_PKEY_CTX_set_dh_paramgen_generator(pkctx, 5);
EVP_PKEY_paramgen(pkctx, &dhParam);
EVP_PKEY_CTX_free(pkctx);
DEBUG("generate DH parameters done");
return dhParam;
}();

DEBUG("configure DH parameters");
//
// at least 128 bits
bits = (bits < kDHMINBITS) ? kDHMINBITS : bits;
//
// Generate params for DH object
EVP_PKEY *dhParam = 0;
EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, 0);
EVP_PKEY_paramgen_init(pkctx);
EVP_PKEY_CTX_set_dh_paramgen_prime_len(pkctx, bits);
EVP_PKEY_CTX_set_dh_paramgen_generator(pkctx, 5);
EVP_PKEY_paramgen(pkctx, &dhParam);
// Set params for DH object
assert(dhparms);
EVP_PKEY_CTX *pkctx = EVP_PKEY_CTX_new(dhparms, 0);
EVP_PKEY_keygen_init(pkctx);
EVP_PKEY_keygen(pkctx, &fDH);
EVP_PKEY_CTX_free(pkctx);
if (dhParam) {
if (XrdCheckDH(dhParam) == 1) {
//
// Generate DH key
pkctx = EVP_PKEY_CTX_new(dhParam, 0);
EVP_PKEY_keygen_init(pkctx);
EVP_PKEY_keygen(pkctx, &fDH);
EVP_PKEY_CTX_free(pkctx);
if (fDH) {
// Init context
ctx = EVP_CIPHER_CTX_new();
if (ctx)
valid = 1;
}
}
EVP_PKEY_free(dhParam);
if (fDH) {
// Init context
ctx = EVP_CIPHER_CTX_new();
if (ctx)
valid = 1;
}

} else {
Expand Down