OpenDDS  Snapshot(2023/04/28-20:55)
Public Member Functions | Private Attributes | List of all members
OpenDDS::Security::SSL::dh_shared_secret Class Reference
Collaboration diagram for OpenDDS::Security::SSL::dh_shared_secret:
Collaboration graph
[legend]

Public Member Functions

 dh_shared_secret (EVP_PKEY *pkey)
 
 ~dh_shared_secret ()
 
int operator() (const DDS::OctetSeq &pub_key, DDS::OctetSeq &dst)
 

Private Attributes

DH_Handle keypair
 
BIGNUM * pubkey
 

Detailed Description

Definition at line 198 of file DiffieHellman.cpp.

Constructor & Destructor Documentation

◆ dh_shared_secret()

OpenDDS::Security::SSL::dh_shared_secret::dh_shared_secret ( EVP_PKEY pkey)
inlineexplicit

Definition at line 201 of file DiffieHellman.cpp.

References OPENDDS_SSL_LOG_ERR.

202  : keypair(pkey)
203 #ifdef OPENSSL_V_3_0
204  , dh_ctx(0)
205  , fd_ctx(0)
206  , peer(0)
207  , param_bld(0)
208  , params(0)
209  , glen(32)
210  , grp(new char[glen])
211 #endif
212  , pubkey(0)
213  {
214  if (!keypair) {
215 #ifndef OPENSSL_V_3_0
216  OPENDDS_SSL_LOG_ERR("EVP_PKEY_get0_DH failed");
217 #endif
218  }
219  }
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

◆ ~dh_shared_secret()

OpenDDS::Security::SSL::dh_shared_secret::~dh_shared_secret ( )
inline

Definition at line 221 of file DiffieHellman.cpp.

222  {
223  BN_free(pubkey);
224 #ifdef OPENSSL_V_3_0
225  EVP_PKEY_CTX_free(dh_ctx);
226  EVP_PKEY_CTX_free(fd_ctx);
227  EVP_PKEY_free(peer);
228  OSSL_PARAM_BLD_free(param_bld);
229  OSSL_PARAM_free(params);
230  delete [] grp;
231 #endif
232  }

Member Function Documentation

◆ operator()()

int OpenDDS::Security::SSL::dh_shared_secret::operator() ( const DDS::OctetSeq pub_key,
DDS::OctetSeq dst 
)
inline

Definition at line 234 of file DiffieHellman.cpp.

References OPENDDS_SSL_LOG_ERR.

235  {
236  if (!keypair) return 1;
237 
238  if (0 == (pubkey = BN_bin2bn(pub_key.get_buffer(), pub_key.length(), 0))) {
239  OPENDDS_SSL_LOG_ERR("BN_bin2bn failed");
240  return 1;
241  }
242 
243 #ifndef OPENSSL_V_3_0
244  int len = DH_size(keypair);
245  dst.length(len);
246  len = DH_compute_key(dst.get_buffer(), pubkey, keypair);
247  if (len < 0) {
248  OPENDDS_SSL_LOG_ERR("DH_compute_key failed");
249  dst.length(0u);
250  return 1;
251  }
252 #else
253  if (!EVP_PKEY_get_utf8_string_param(keypair, "group", grp, glen, &glen)) {
254  OPENDDS_SSL_LOG_ERR("Failed to find group name");
255  return 1;
256  }
257  OSSL_PARAM_free(params);
258  params = 0;
259 
260  if ((param_bld = OSSL_PARAM_BLD_new()) == 0) {
261  OPENDDS_SSL_LOG_ERR("OSSL_PARAM_BLD_new failed");
262  return 1;
263  }
264 
265  if ((OSSL_PARAM_BLD_push_utf8_string(param_bld, "group", grp, 0) == 0)) {
266  OPENDDS_SSL_LOG_ERR("Building prarms list failed");
267  return 1;
268  }
269 
270  if ((OSSL_PARAM_BLD_push_BN(param_bld, "pub", pubkey) == 0)) {
271  OPENDDS_SSL_LOG_ERR("Building prarms list failed");
272  return 1;
273  }
274  params = OSSL_PARAM_BLD_to_param(param_bld);
275 
276  if ((fd_ctx = EVP_PKEY_CTX_new(keypair, 0)) == 0) {
277  OPENDDS_SSL_LOG_ERR("new ctx failed.");
278  return 1;
279  }
280 
281  EVP_PKEY_fromdata_init(fd_ctx);
282 
283  if (EVP_PKEY_fromdata(fd_ctx, &peer, EVP_PKEY_PUBLIC_KEY, params) != 1) {
284  OPENDDS_SSL_LOG_ERR("EVP_PKEY_fromdata Failed");
285  return 1;
286  }
287 
288  if ((dh_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
289  OPENDDS_SSL_LOG_ERR("new ctx from name BH failed.");
290  return 1;
291  }
292 
293  if (!EVP_PKEY_derive_init(dh_ctx)) {
294  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive_init failed");
295  return 1;
296  }
297 
298  if (EVP_PKEY_derive_set_peer(dh_ctx, peer) <= 0) {
299  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive_set peer failed");
300  return 1;
301  }
302 
303  size_t len = 0;
304  if (EVP_PKEY_derive(dh_ctx, 0, &len) <= 0) {
305  OPENDDS_SSL_LOG_ERR("DH compute_key error getting length");
306  return 1;
307  }
308  dst.length(static_cast<ACE_CDR::ULong>(len));
309  if (EVP_PKEY_derive(dh_ctx, dst.get_buffer(), &len) <= 0) {
310  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive failed");
311  dst.length(0u);
312  return 1;
313  }
314 #endif
315  return 0;
316  }
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

Member Data Documentation

◆ keypair

DH_Handle OpenDDS::Security::SSL::dh_shared_secret::keypair
private

Definition at line 320 of file DiffieHellman.cpp.

◆ pubkey

BIGNUM* OpenDDS::Security::SSL::dh_shared_secret::pubkey
private

Definition at line 331 of file DiffieHellman.cpp.


The documentation for this class was generated from the following file: