10 #include <openssl/evp.h> 11 #include <openssl/dh.h> 12 #if OPENSSL_VERSION_NUMBER >= 0x30000000L 13 #include <openssl/core_names.h> 14 #include <openssl/param_build.h> 16 #include "../OpenSSL_legacy.h" 30 int compare(
const DDS::OctetSeq& s1,
const char* s2,
const size_t s2_len) {
31 return s1.length() != s2_len ? -1 : std::memcmp(s1.get_buffer(), s2, s2_len);
39 #if defined OPENSSL_V_1_0 || defined OPENSSL_V_3_0 40 :
dh_(EVP_PKEY_get1_DH(key))
42 :
dh_(EVP_PKEY_get0_DH(key))
45 operator DH*() {
return dh_; }
48 #if defined OPENSSL_V_1_0 || defined OPENSSL_V_3_0 63 return (0 == std::memcmp(shared_secret_.get_buffer(),
65 shared_secret_.length()));
71 std::vector<const DDS::OctetSeq*> hash_data;
72 hash_data.push_back(&tmp);
73 return SSL::hash(hash_data, shared_secret_);
87 EVP_PKEY_free(params);
88 EVP_PKEY_CTX_free(paramgen_ctx);
89 EVP_PKEY_CTX_free(keygen_ctx);
96 #if OPENSSL_VERSION_NUMBER < 0x10002000L 97 OPENDDS_SSL_LOG_ERR(
"RFC 5114 2.3 - 2048-bit MODP Group with 256-bit Prime Order Subgroup - not provided by this OpenSSL library");
100 if (0 == (params = EVP_PKEY_new())) {
105 if (0 == (paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DHX, 0))) {
110 if (1 != EVP_PKEY_paramgen_init(paramgen_ctx)) {
115 if (1 != EVP_PKEY_CTX_set_dh_rfc5114(paramgen_ctx, 3)) {
120 if ((1 != EVP_PKEY_paramgen(paramgen_ctx, ¶ms)) || params == 0) {
125 if (0 == (keygen_ctx = EVP_PKEY_CTX_new(params, 0))) {
130 if (1 != EVP_PKEY_keygen_init(keygen_ctx)) {
135 if (1 != EVP_PKEY_keygen(keygen_ctx, &result)) {
164 #ifndef OPENSSL_V_3_0 167 const BIGNUM *pubkey = 0, *privkey = 0;
170 dst.length(BN_num_bytes(pubkey));
171 if (0 < BN_bn2bin(pubkey, dst.get_buffer())) {
182 if (EVP_PKEY_get_bn_param(k_, OSSL_PKEY_PARAM_PUB_KEY, &pubkey)) {
183 dst.length(BN_num_bytes(pubkey));
184 if (0 < BN_bn2bin(pubkey, dst.get_buffer())) {
210 , grp(new char[glen])
215 #ifndef OPENSSL_V_3_0 225 EVP_PKEY_CTX_free(dh_ctx);
226 EVP_PKEY_CTX_free(fd_ctx);
228 OSSL_PARAM_BLD_free(param_bld);
229 OSSL_PARAM_free(params);
236 if (!keypair)
return 1;
238 if (0 == (pubkey = BN_bin2bn(pub_key.get_buffer(), pub_key.length(), 0))) {
243 #ifndef OPENSSL_V_3_0 244 int len = DH_size(keypair);
246 len = DH_compute_key(dst.get_buffer(), pubkey, keypair);
253 if (!EVP_PKEY_get_utf8_string_param(keypair,
"group", grp, glen, &glen)) {
257 OSSL_PARAM_free(params);
260 if ((param_bld = OSSL_PARAM_BLD_new()) == 0) {
265 if ((OSSL_PARAM_BLD_push_utf8_string(param_bld,
"group", grp, 0) == 0)) {
270 if ((OSSL_PARAM_BLD_push_BN(param_bld,
"pub", pubkey) == 0)) {
274 params = OSSL_PARAM_BLD_to_param(param_bld);
276 if ((fd_ctx = EVP_PKEY_CTX_new(keypair, 0)) == 0) {
281 EVP_PKEY_fromdata_init(fd_ctx);
283 if (EVP_PKEY_fromdata(fd_ctx, &peer, EVP_PKEY_PUBLIC_KEY, params) != 1) {
288 if ((dh_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
293 if (!EVP_PKEY_derive_init(dh_ctx)) {
298 if (EVP_PKEY_derive_set_peer(dh_ctx, peer) <= 0) {
304 if (EVP_PKEY_derive(dh_ctx, 0, &len) <= 0) {
308 dst.length(static_cast<ACE_CDR::ULong>(len));
309 if (EVP_PKEY_derive(dh_ctx, dst.get_buffer(), &len) <= 0) {
319 #ifndef OPENSSL_V_3_0 323 EVP_PKEY_CTX* dh_ctx;
324 EVP_PKEY_CTX* fd_ctx;
326 OSSL_PARAM_BLD* param_bld;
337 return secret(pub_key, shared_secret_);
340 #ifndef OPENSSL_V_3_0 344 #if defined OPENSSL_V_1_0 || defined OPENSSL_V_3_0 345 : ec_(EVP_PKEY_get1_EC_KEY(key))
347 : ec_(EVP_PKEY_get0_EC_KEY(key))
350 operator EC_KEY*() {
return ec_; }
353 #if defined OPENSSL_V_1_0 || defined OPENSSL_V_3_0 371 EVP_PKEY_free(params);
372 EVP_PKEY_CTX_free(paramgen_ctx);
373 EVP_PKEY_CTX_free(keygen_ctx);
380 if (0 == (params = EVP_PKEY_new())) {
385 if (0 == (paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, 0))) {
390 if (1 != EVP_PKEY_paramgen_init(paramgen_ctx)) {
395 if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(paramgen_ctx, NID_X9_62_prime256v1)) {
400 if (1 != EVP_PKEY_paramgen(paramgen_ctx, ¶ms) || params == 0) {
405 if (0 == (keygen_ctx = EVP_PKEY_CTX_new(params, 0))) {
410 if (1 != EVP_PKEY_keygen_init(keygen_ctx)) {
415 if (1 != EVP_PKEY_keygen(keygen_ctx, &result)) {
453 OSSL_PARAM_free(params);
459 if (!keypair)
return 1;
461 #ifndef OPENSSL_V_3_0 468 const EC_POINT* pubkey = EC_KEY_get0_public_key(keypair_ecdh);
474 size_t len = EC_POINT_point2oct(EC_KEY_get0_group(keypair_ecdh), pubkey,
475 EC_KEY_get_conv_form(keypair_ecdh), 0,
482 dst.length(static_cast<unsigned int>(len));
483 if (0 == EC_POINT_point2oct(EC_KEY_get0_group(keypair_ecdh), pubkey,
484 EC_KEY_get_conv_form(keypair_ecdh),
485 dst.get_buffer(), len, 0)) {
490 if (EVP_PKEY_todata(keypair, EVP_PKEY_KEYPAIR, ¶ms) <= 0) {
494 const char* gname = 0;
495 const unsigned char* pubbuf = 0;
496 size_t pubbuflen = 0;
497 for (OSSL_PARAM* p = params; p != 0 && p->key != 0; ++p) {
499 gname =
static_cast<const char*
>(p->data);
501 pubbuf =
static_cast<const unsigned char*
>(p->data);
502 pubbuflen = p->data_size;
506 const int nid = OBJ_txt2nid(gname);
511 EC_GROUP*
const ecg = EC_GROUP_new_by_curve_name(nid);
512 const point_conversion_form_t cf = EC_GROUP_get_point_conversion_form(ecg);
513 EC_POINT*
const ec = EC_POINT_new(ecg);
514 if (!EC_POINT_oct2point(ecg, ec, pubbuf, pubbuflen, 0)) {
520 const size_t eclen = EC_POINT_point2oct(ecg, ec, cf, 0, 0u, 0);
521 dst.length(static_cast<ACE_CDR::ULong>(eclen));
522 EC_POINT_point2oct(ecg, ec, cf, dst.get_buffer(), eclen, 0);
567 EC_POINT_free(pubkey);
568 BN_CTX_free(bignum_ctx);
570 EVP_PKEY_CTX_free(ec_ctx);
571 EVP_PKEY_CTX_free(fd_ctx);
573 OSSL_PARAM_BLD_free(param_bld);
574 OSSL_PARAM_free(params);
580 if (!keypair)
return 1;
582 if (0 == (bignum_ctx = BN_CTX_new())) {
586 #ifndef OPENSSL_V_3_0 587 if (0 == (group = EC_KEY_get0_group(keypair))) {
592 pubkey = EC_POINT_new(group);
593 if (1 != EC_POINT_oct2point(group, pubkey, src.get_buffer(),
594 src.length(), bignum_ctx)) {
599 const int numbits = EC_GROUP_get_degree(group);
600 dst.length((numbits + 7) / 8);
602 const int len = ECDH_compute_key(dst.get_buffer(), dst.length(), pubkey,
611 if (EVP_PKEY_todata(keypair, EVP_PKEY_PUBLIC_KEY, ¶ms) <= 0) {
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);
626 if ((param_bld = OSSL_PARAM_BLD_new()) == 0) {
631 if ((OSSL_PARAM_BLD_push_utf8_string(param_bld,
"group", grp, 0) == 0)) {
636 if ((OSSL_PARAM_BLD_push_octet_string(param_bld,
"pub", src.get_buffer(),src.length()) == 0)) {
641 OSSL_PARAM* old_params = params;
642 params = OSSL_PARAM_BLD_to_param(param_bld);
643 OSSL_PARAM_free(old_params);
645 if ((fd_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
650 EVP_PKEY_fromdata_init(fd_ctx);
652 if (EVP_PKEY_fromdata(fd_ctx, &peer, EVP_PKEY_PUBLIC_KEY, params) != 1) {
657 if ((ec_ctx = EVP_PKEY_CTX_new(keypair,0)) == 0) {
662 if (!EVP_PKEY_derive_init(ec_ctx)) {
667 if (EVP_PKEY_derive_set_peer(ec_ctx, peer) <= 0) {
673 if (EVP_PKEY_derive(ec_ctx, 0, &len) <= 0) {
677 dst.length(static_cast<ACE_CDR::ULong>(len));
678 if (EVP_PKEY_derive(ec_ctx, dst.get_buffer(), &len) <= 0) {
688 #ifndef OPENSSL_V_3_0 693 EVP_PKEY_CTX* ec_ctx;
694 EVP_PKEY_CTX* fd_ctx;
696 OSSL_PARAM_BLD* param_bld;
706 return secret(pub_key, shared_secret_);
static DiffieHellman * factory(const DDS::OctetSeq &kagree_algo)
int operator()(DDS::OctetSeq &dst)
int hash(const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst)
EVP_PKEY_CTX * keygen_ctx
int compute_shared_secret(const DDS::OctetSeq &pub_key)
void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
const char DH_2048_MODP_256_PRIME_STR[]
int operator()(const DDS::OctetSeq &pub_key, DDS::OctetSeq &dst)
struct evp_pkey_st EVP_PKEY
ecdh_pubkey_as_octets(EVP_PKEY *pkey)
int compute_shared_secret(const DDS::OctetSeq &pub_key)
~ecdh_shared_secret_from_octets()
~DH_2048_MODP_256_PRIME()
~ECDH_PRIME_256_V1_CEUM()
ecdh_shared_secret_from_octets(EVP_PKEY *pkey)
EVP_PKEY_CTX * paramgen_ctx
sequence< octet > OctetSeq
int strcmp(const char *s, const char *t)
int pub_key(DDS::OctetSeq &dst)
EVP_PKEY_CTX * keygen_ctx
int strcasecmp(const char *s, const char *t)
EVP_PKEY_CTX * paramgen_ctx
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
virtual bool cmp_shared_secret(const DHAlgorithm &other) const
dh_shared_secret(EVP_PKEY *pkey)
const char ECDH_PRIME_256_V1_CEUM_STR[]
int pub_key(DDS::OctetSeq &dst)
The Internal API and Implementation of OpenDDS.
#define OPENDDS_SSL_LOG_ERR(MSG)
int operator()(const DDS::OctetSeq &src, DDS::OctetSeq &dst)
virtual const DDS::OctetSeq & get_shared_secret() const