OpenDDS  Snapshot(2023/04/28-20:55)
DiffieHellman.h
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.OpenDDS.org/license.html
4  */
5 
6 #ifndef OPENDDS_DCPS_SECURITY_SSL_DIFFIEHELLMAN_H
7 #define OPENDDS_DCPS_SECURITY_SSL_DIFFIEHELLMAN_H
8 
10 #include <dds/DCPS/unique_ptr.h>
11 
12 #include "dds/DdsDcpsCoreC.h"
13 
14 #include <openssl/evp.h>
15 
17 
18 namespace OpenDDS {
19 namespace Security {
20 namespace SSL {
21 
22 const char DH_2048_MODP_256_PRIME_STR[] = "DH+MODP-2048-256";
23 const char ECDH_PRIME_256_V1_CEUM_STR[] = "ECDH+prime256v1-CEUM";
24 
26 public:
28 
29  DHAlgorithm() : k_(0) {}
30 
31  virtual ~DHAlgorithm();
32 
33  virtual int init() = 0;
34  virtual int pub_key(DDS::OctetSeq& dst) = 0;
35 
36  virtual int gen_shared_secret(const DDS::OctetSeq& pub_key)
37  {
38  return compute_shared_secret(pub_key) || hash_shared_secret();
39  }
40 
41  virtual const DDS::OctetSeq& get_shared_secret() const
42  {
43  return shared_secret_;
44  }
45 
46  virtual bool cmp_shared_secret(const DHAlgorithm& other) const;
47  virtual const char* kagree_algo() const = 0;
48 
49  protected:
50  virtual int compute_shared_secret(const DDS::OctetSeq& pub_key) = 0;
51  int hash_shared_secret();
52 
55 };
56 
58 public:
61 
62  /**
63  * @return int 0 on success; 1 on failure.
64  */
65  int init();
66 
67  /**
68  * @return int 0 on success; 1 on failure.
69  */
70  int pub_key(DDS::OctetSeq& dst);
71 
72  /**
73  * @return int 0 on success; 1 on failure.
74  */
75  int compute_shared_secret(const DDS::OctetSeq& pub_key);
76 
77  const char* kagree_algo() const { return DH_2048_MODP_256_PRIME_STR; }
78 };
79 
81 public:
84 
85  /**
86  * @return int 0 on success; 1 on failure.
87  */
88  int init();
89 
90  /**
91  * @return int 0 on success; 1 on failure.
92  */
93  int pub_key(DDS::OctetSeq& dst);
94 
95  /**
96  * @return int 0 on success; 1 on failure.
97  */
98  int compute_shared_secret(const DDS::OctetSeq& pub_key);
99 
100  const char* kagree_algo() const { return ECDH_PRIME_256_V1_CEUM_STR; }
101 };
102 
104 public:
106 
107  static DiffieHellman* factory(const DDS::OctetSeq& kagree_algo);
108 
109  explicit DiffieHellman(DHAlgorithm* algorithm) : algo_(algorithm) {}
110 
112 
113  void load()
114  {
115  if (algo_) algo_->init();
116  }
117 
118  /**
119  * @return int 0 on success; 1 on failure.
120  */
121  int pub_key(DDS::OctetSeq& dst) { return algo_->pub_key(dst); }
122 
123  /**
124  * @return int 0 on success; 1 on failure.
125  */
126  int gen_shared_secret(const DDS::OctetSeq& pub_key)
127  {
128  return algo_->gen_shared_secret(pub_key);
129  }
130 
132  {
133  return algo_->get_shared_secret();
134  }
135 
136  bool cmp_shared_secret(const DiffieHellman& other)
137  {
138  return algo_->cmp_shared_secret(*other.algo_);
139  }
140 
141  const char* kagree_algo() const { return algo_->kagree_algo(); }
142 
143  private:
145 };
146 
147 } // namespace SSL
148 } // namespace Security
149 } // namespace OpenDDS
150 
152 
153 #endif
virtual int gen_shared_secret(const DDS::OctetSeq &pub_key)
Definition: DiffieHellman.h:36
bool(* factory)(CORBA::TCKind, TAO_InputCDR &, CORBA::TypeCode_ptr &, TC_Info_List &, TC_Info_List &)
DCPS::unique_ptr< DHAlgorithm > unique_ptr
Definition: DiffieHellman.h:27
const char DH_2048_MODP_256_PRIME_STR[]
Definition: DiffieHellman.h:22
DCPS::unique_ptr< DiffieHellman > unique_ptr
struct evp_pkey_st EVP_PKEY
bool cmp_shared_secret(const DiffieHellman &other)
int init(void)
sequence< octet > OctetSeq
Definition: DdsDcpsCore.idl:64
DiffieHellman(DHAlgorithm *algorithm)
const DDS::OctetSeq & get_shared_secret()
#define OpenDDS_Security_Export
int gen_shared_secret(const DDS::OctetSeq &pub_key)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
const char ECDH_PRIME_256_V1_CEUM_STR[]
Definition: DiffieHellman.h:23
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
virtual const DDS::OctetSeq & get_shared_secret() const
Definition: DiffieHellman.h:41