OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Private Attributes | List of all members
OpenDDS::Security::SSL::ecdh_shared_secret_from_octets Class Reference
Collaboration diagram for OpenDDS::Security::SSL::ecdh_shared_secret_from_octets:
Collaboration graph
[legend]

Public Member Functions

 ecdh_shared_secret_from_octets (EVP_PKEY *pkey)
 
 ~ecdh_shared_secret_from_octets ()
 
int operator() (const DDS::OctetSeq &src, DDS::OctetSeq &dst)
 

Private Attributes

EC_Handle keypair
 
const EC_GROUP * group
 
EC_POINT * pubkey
 
BN_CTX * bignum_ctx
 

Detailed Description

Definition at line 543 of file DiffieHellman.cpp.

Constructor & Destructor Documentation

◆ ecdh_shared_secret_from_octets()

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

Definition at line 546 of file DiffieHellman.cpp.

References OPENDDS_SSL_LOG_ERR.

547  : keypair(pkey)
548 #ifdef OPENSSL_V_3_0
549  , ec_ctx(0)
550  , fd_ctx(0)
551  , peer(0)
552  , param_bld(0)
553  , params(0)
554 #else
555  , group(0)
556 #endif
557  , pubkey(0)
558  , bignum_ctx(0)
559  {
560  if (!keypair) {
561  OPENDDS_SSL_LOG_ERR("EVP_PKEY_get0_EC_KEY failed");
562  }
563  }
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

◆ ~ecdh_shared_secret_from_octets()

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

Definition at line 565 of file DiffieHellman.cpp.

566  {
567  EC_POINT_free(pubkey);
568  BN_CTX_free(bignum_ctx);
569 #ifdef OPENSSL_V_3_0
570  EVP_PKEY_CTX_free(ec_ctx);
571  EVP_PKEY_CTX_free(fd_ctx);
572  EVP_PKEY_free(peer);
573  OSSL_PARAM_BLD_free(param_bld);
574  OSSL_PARAM_free(params);
575 #endif
576  }

Member Function Documentation

◆ operator()()

int OpenDDS::Security::SSL::ecdh_shared_secret_from_octets::operator() ( const DDS::OctetSeq src,
DDS::OctetSeq dst 
)
inline

Definition at line 578 of file DiffieHellman.cpp.

References OPENDDS_SSL_LOG_ERR, and strcmp().

579  {
580  if (!keypair) return 1;
581 
582  if (0 == (bignum_ctx = BN_CTX_new())) {
583  OPENDDS_SSL_LOG_ERR("BN_CTX_new failed");
584  return 1;
585  }
586 #ifndef OPENSSL_V_3_0
587  if (0 == (group = EC_KEY_get0_group(keypair))) {
588  OPENDDS_SSL_LOG_ERR("EC_KEY_get0_group failed");
589  return 1;
590  }
591 
592  pubkey = EC_POINT_new(group);
593  if (1 != EC_POINT_oct2point(group, pubkey, src.get_buffer(),
594  src.length(), bignum_ctx)) {
595  OPENDDS_SSL_LOG_ERR("EC_POINT_point2oct failed");
596  return 1;
597  }
598 
599  const int numbits = EC_GROUP_get_degree(group);
600  dst.length((numbits + 7) / 8);
601 
602  const int len = ECDH_compute_key(dst.get_buffer(), dst.length(), pubkey,
603  keypair, 0);
604 
605  if (0 == len) {
606  OPENDDS_SSL_LOG_ERR("ECDH_compute_key failed");
607  return 1;
608  }
609 #else
610  const char* grp = 0;
611  if (EVP_PKEY_todata(keypair, EVP_PKEY_PUBLIC_KEY, &params) <= 0) {
612  OPENDDS_SSL_LOG_ERR("pkey to data failed");
613  return 1;
614  } else {
615  for (OSSL_PARAM* p = params; grp == 0 && p != 0 && p->key != 0; p++) {
616  if (strcmp(p->key, "group") == 0) {
617  grp = static_cast<const char*>(p->data);
618  }
619  }
620  if (grp == 0) {
621  OPENDDS_SSL_LOG_ERR("could not find group id");
622  return 1;
623  }
624  }
625 
626  if ((param_bld = OSSL_PARAM_BLD_new()) == 0) {
627  OPENDDS_SSL_LOG_ERR("OSSL_PARAM_BLD_new failed");
628  return 1;
629  }
630 
631  if ((OSSL_PARAM_BLD_push_utf8_string(param_bld, "group", grp, 0) == 0)) {
632  OPENDDS_SSL_LOG_ERR("Building prarms list failed");
633  return 1;
634  }
635 
636  if ((OSSL_PARAM_BLD_push_octet_string(param_bld, "pub", src.get_buffer(),src.length()) == 0)) {
637  OPENDDS_SSL_LOG_ERR("Building prarms list failed");
638  return 1;
639  }
640 
641  OSSL_PARAM* old_params = params;
642  params = OSSL_PARAM_BLD_to_param(param_bld);
643  OSSL_PARAM_free(old_params);
644 
645  if ((fd_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
646  OPENDDS_SSL_LOG_ERR("new ctx from name ECBH failed.");
647  return 1;
648  }
649 
650  EVP_PKEY_fromdata_init(fd_ctx);
651 
652  if (EVP_PKEY_fromdata(fd_ctx, &peer, EVP_PKEY_PUBLIC_KEY, params) != 1) {
653  OPENDDS_SSL_LOG_ERR("EVP_PKEY_fromdata Failed");
654  return 1;
655  }
656 
657  if ((ec_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
658  OPENDDS_SSL_LOG_ERR("new ctx from name ECBH failed.");
659  return 1;
660  }
661 
662  if (!EVP_PKEY_derive_init(ec_ctx)) {
663  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive_init failed");
664  return 1;
665  }
666 
667  if (EVP_PKEY_derive_set_peer(ec_ctx, peer) <= 0) {
668  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive_set peer failed");
669  return 1;
670  }
671 
672  size_t len = 0;
673  if (EVP_PKEY_derive(ec_ctx, 0, &len) <= 0) {
674  OPENDDS_SSL_LOG_ERR("DH compute_key error getting length");
675  return 1;
676  }
677  dst.length(static_cast<ACE_CDR::ULong>(len));
678  if (EVP_PKEY_derive(ec_ctx, dst.get_buffer(), &len) <= 0) {
679  OPENDDS_SSL_LOG_ERR("EVP_PKEY_derive failed");
680  dst.length(0u);
681  return 1;
682  }
683 #endif
684  return 0;
685  }
int strcmp(const char *s, const char *t)
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

Member Data Documentation

◆ bignum_ctx

BN_CTX* OpenDDS::Security::SSL::ecdh_shared_secret_from_octets::bignum_ctx
private

Definition at line 700 of file DiffieHellman.cpp.

◆ group

const EC_GROUP* OpenDDS::Security::SSL::ecdh_shared_secret_from_octets::group
private

Definition at line 690 of file DiffieHellman.cpp.

◆ keypair

EC_Handle OpenDDS::Security::SSL::ecdh_shared_secret_from_octets::keypair
private

Definition at line 689 of file DiffieHellman.cpp.

◆ pubkey

EC_POINT* OpenDDS::Security::SSL::ecdh_shared_secret_from_octets::pubkey
private

Definition at line 699 of file DiffieHellman.cpp.


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