LCOV - code coverage report
Current view: top level - DCPS/security/SSL - DiffieHellman.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 17 94.1 %
Date: 2023-04-30 01:32:43 Functions: 11 12 91.7 %

          Line data    Source code
       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             : 
       9             : #include <dds/DCPS/security/OpenDDS_Security_Export.h>
      10             : #include <dds/DCPS/unique_ptr.h>
      11             : 
      12             : #include "dds/DdsDcpsCoreC.h"
      13             : 
      14             : #include <openssl/evp.h>
      15             : 
      16             : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
      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             : 
      25             : class OpenDDS_Security_Export DHAlgorithm {
      26             : public:
      27             :   typedef DCPS::unique_ptr<DHAlgorithm> unique_ptr;
      28             : 
      29          22 :   DHAlgorithm() : k_(0) {}
      30             : 
      31             :   virtual ~DHAlgorithm();
      32             : 
      33             :   virtual int init() = 0;
      34             :   virtual int pub_key(DDS::OctetSeq& dst) = 0;
      35             : 
      36          14 :   virtual int gen_shared_secret(const DDS::OctetSeq& pub_key)
      37             :   {
      38          14 :     return compute_shared_secret(pub_key) || hash_shared_secret();
      39             :   }
      40             : 
      41          14 :   virtual const DDS::OctetSeq& get_shared_secret() const
      42             :   {
      43          14 :     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             : 
      53             :   EVP_PKEY* k_;
      54             :   DDS::OctetSeq shared_secret_;
      55             : };
      56             : 
      57             : class OpenDDS_Security_Export DH_2048_MODP_256_PRIME : public DHAlgorithm {
      58             : public:
      59             :   DH_2048_MODP_256_PRIME();
      60             :   ~DH_2048_MODP_256_PRIME();
      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           0 :   const char* kagree_algo() const { return DH_2048_MODP_256_PRIME_STR; }
      78             : };
      79             : 
      80             : class OpenDDS_Security_Export ECDH_PRIME_256_V1_CEUM : public DHAlgorithm {
      81             : public:
      82             :   ECDH_PRIME_256_V1_CEUM();
      83             :   ~ECDH_PRIME_256_V1_CEUM();
      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          40 :   const char* kagree_algo() const { return ECDH_PRIME_256_V1_CEUM_STR; }
     101             : };
     102             : 
     103             : class OpenDDS_Security_Export DiffieHellman {
     104             : public:
     105             :   typedef DCPS::unique_ptr<DiffieHellman> unique_ptr;
     106             : 
     107             :   static DiffieHellman* factory(const DDS::OctetSeq& kagree_algo);
     108             : 
     109          22 :   explicit DiffieHellman(DHAlgorithm* algorithm) : algo_(algorithm) {}
     110             : 
     111          22 :   ~DiffieHellman() {}
     112             : 
     113             :   void load()
     114             :   {
     115             :     if (algo_) algo_->init();
     116             :   }
     117             : 
     118             :   /**
     119             :    * @return int 0 on success; 1 on failure.
     120             :    */
     121          18 :   int pub_key(DDS::OctetSeq& dst) { return algo_->pub_key(dst); }
     122             : 
     123             :   /**
     124             :    * @return int 0 on success; 1 on failure.
     125             :    */
     126          14 :   int gen_shared_secret(const DDS::OctetSeq& pub_key)
     127             :   {
     128          14 :     return algo_->gen_shared_secret(pub_key);
     129             :   }
     130             : 
     131          10 :   const DDS::OctetSeq& get_shared_secret()
     132             :   {
     133          10 :     return algo_->get_shared_secret();
     134             :   }
     135             : 
     136           2 :   bool cmp_shared_secret(const DiffieHellman& other)
     137             :   {
     138           2 :     return algo_->cmp_shared_secret(*other.algo_);
     139             :   }
     140             : 
     141          40 :   const char* kagree_algo() const { return algo_->kagree_algo(); }
     142             : 
     143             :  private:
     144             :   DHAlgorithm::unique_ptr algo_;
     145             : };
     146             : 
     147             : }  // namespace SSL
     148             : }  // namespace Security
     149             : }  // namespace OpenDDS
     150             : 
     151             : OPENDDS_END_VERSIONED_NAMESPACE_DECL
     152             : 
     153             : #endif

Generated by: LCOV version 1.16