OpenDDS  Snapshot(2023/04/28-20:55)
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
OpenDDS::Security::SSL::PrivateKey Class Reference

#include <PrivateKey.h>

Public Types

typedef DCPS::unique_ptr< PrivateKeyunique_ptr
 

Public Member Functions

 PrivateKey (const std::string &uri, const std::string &password="")
 
 PrivateKey ()
 
virtual ~PrivateKey ()
 
void load (const std::string &uri, const std::string &password="")
 
int sign (const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst) const
 

Private Member Functions

 PrivateKey (const PrivateKey &)
 
PrivateKeyoperator= (const PrivateKey &)
 

Static Private Member Functions

static EVP_PKEYEVP_PKEY_from_pem (const std::string &path, const std::string &password="")
 
static EVP_PKEYEVP_PKEY_from_pem_data (const std::string &data, const std::string &password)
 

Private Attributes

EVP_PKEYk_
 

Friends

OpenDDS_Security_Export bool operator== (const PrivateKey &lhs, const PrivateKey &rhs)
 

Detailed Description

Definition at line 25 of file PrivateKey.h.

Member Typedef Documentation

◆ unique_ptr

Definition at line 28 of file PrivateKey.h.

Constructor & Destructor Documentation

◆ PrivateKey() [1/3]

OpenDDS::Security::SSL::PrivateKey::PrivateKey ( const std::string &  uri,
const std::string &  password = "" 
)
explicit

Definition at line 22 of file PrivateKey.cpp.

References load().

23  : k_(0)
24 {
25  load(uri, password);
26 }
void load(const std::string &uri, const std::string &password="")
Definition: PrivateKey.cpp:40

◆ PrivateKey() [2/3]

OpenDDS::Security::SSL::PrivateKey::PrivateKey ( )

Definition at line 28 of file PrivateKey.cpp.

29  : k_(0)
30 {
31 }

◆ ~PrivateKey()

OpenDDS::Security::SSL::PrivateKey::~PrivateKey ( )
virtual

Definition at line 33 of file PrivateKey.cpp.

References k_.

34 {
35  if (k_) {
36  EVP_PKEY_free(k_);
37  }
38 }

◆ PrivateKey() [3/3]

OpenDDS::Security::SSL::PrivateKey::PrivateKey ( const PrivateKey )
private

Member Function Documentation

◆ EVP_PKEY_from_pem()

EVP_PKEY * OpenDDS::Security::SSL::PrivateKey::EVP_PKEY_from_pem ( const std::string &  path,
const std::string &  password = "" 
)
staticprivate

Definition at line 164 of file PrivateKey.cpp.

References OPENDDS_SSL_LOG_ERR.

Referenced by load().

166 {
167  EVP_PKEY* result = 0;
168 
169  BIO* filebuf = BIO_new_file(path.c_str(), "r");
170  if (filebuf) {
171  result = PEM_read_bio_PrivateKey(filebuf, 0, 0,
172  password.empty() ? 0 : (void*)password.c_str());
173  if (!result) {
174  OPENDDS_SSL_LOG_ERR("PEM_read_bio_PrivateKey failed");
175  }
176 
177  BIO_free(filebuf);
178 
179  } else {
180  std::stringstream errmsg;
181  errmsg << "failed to read file '" << path << "' using BIO_new_file";
182  OPENDDS_SSL_LOG_ERR(errmsg.str().c_str());
183  }
184 
185  return result;
186 }
struct evp_pkey_st EVP_PKEY
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

◆ EVP_PKEY_from_pem_data()

EVP_PKEY * OpenDDS::Security::SSL::PrivateKey::EVP_PKEY_from_pem_data ( const std::string &  data,
const std::string &  password 
)
staticprivate

Definition at line 188 of file PrivateKey.cpp.

References OPENDDS_SSL_LOG_ERR.

Referenced by load().

190 {
191  DDS::OctetSeq original_bytes;
192 
193  // The minus 1 is because path contains a comma in element 0 and that
194  // comma is not included in the cert string
195  original_bytes.length(static_cast<unsigned int>(data.size() - 1));
196  std::memcpy(original_bytes.get_buffer(), &data[1],
197  original_bytes.length());
198 
199  // To appease the other DDS security implementations which
200  // append a null byte at the end of the cert.
201  original_bytes.length(original_bytes.length() + 1);
202  original_bytes[original_bytes.length() - 1] = 0;
203 
204  EVP_PKEY* result = 0;
205  BIO* filebuf = BIO_new(BIO_s_mem());
206 
207  if (filebuf) {
208  if (0 >= BIO_write(filebuf, original_bytes.get_buffer(),
209  original_bytes.length())) {
210  OPENDDS_SSL_LOG_ERR("BIO_write failed");
211  }
212 
213  result = PEM_read_bio_PrivateKey(filebuf, 0, 0,
214  password.empty() ? 0 : (void*)password.c_str());
215 
216  if (!result) {
217  OPENDDS_SSL_LOG_ERR("PEM_read_bio_PrivateKey failed");
218  }
219 
220  BIO_free(filebuf);
221 
222  } else {
223  std::stringstream errmsg;
224  errmsg << "failed to create data '" << data << "' using BIO_new";
225  OPENDDS_SSL_LOG_ERR(errmsg.str().c_str());
226  }
227 
228  return result;
229 }
struct evp_pkey_st EVP_PKEY
sequence< octet > OctetSeq
Definition: DdsDcpsCore.idl:64
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

◆ load()

void OpenDDS::Security::SSL::PrivateKey::load ( const std::string &  uri,
const std::string &  password = "" 
)

Definition at line 40 of file PrivateKey.cpp.

References ACE_ERROR, ACE_TEXT(), OpenDDS::Security::CommonUtilities::URI::everything_else, EVP_PKEY_from_pem(), EVP_PKEY_from_pem_data(), k_, LM_WARNING, OpenDDS::Security::CommonUtilities::URI::scheme, OpenDDS::Security::CommonUtilities::URI::URI_DATA, OpenDDS::Security::CommonUtilities::URI::URI_FILE, OpenDDS::Security::CommonUtilities::URI::URI_PKCS11, and OpenDDS::Security::CommonUtilities::URI::URI_UNKNOWN.

Referenced by PrivateKey().

41 {
42  using namespace CommonUtilities;
43 
44  if (k_) return;
45 
46  URI uri_info(uri);
47 
48  switch (uri_info.scheme) {
49  case URI::URI_FILE:
50  k_ = EVP_PKEY_from_pem(uri_info.everything_else, password);
51  break;
52 
53  case URI::URI_DATA:
54  k_ = EVP_PKEY_from_pem_data(uri_info.everything_else, password);
55  break;
56 
57  case URI::URI_PKCS11:
58  case URI::URI_UNKNOWN:
59  default:
60  ACE_ERROR((LM_WARNING,
61  ACE_TEXT("(%P|%t) SSL::PrivateKey::load: WARNING: Unsupported URI scheme in cert path '%C'\n"),
62  uri.c_str()));
63  break;
64  }
65 }
#define ACE_ERROR(X)
static EVP_PKEY * EVP_PKEY_from_pem(const std::string &path, const std::string &password="")
Definition: PrivateKey.cpp:164
ACE_TEXT("TCP_Factory")
static EVP_PKEY * EVP_PKEY_from_pem_data(const std::string &data, const std::string &password)
Definition: PrivateKey.cpp:188

◆ operator=()

PrivateKey& OpenDDS::Security::SSL::PrivateKey::operator= ( const PrivateKey )
private

◆ sign()

int OpenDDS::Security::SSL::PrivateKey::sign ( const std::vector< const DDS::OctetSeq *> &  src,
DDS::OctetSeq dst 
) const

Definition at line 157 of file PrivateKey.cpp.

References k_.

Referenced by OpenDDS::Security::SSL::sign_serialized().

159 {
160  sign_implementation sign(k_);
161  return sign(src, dst);
162 }
int sign(const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst) const
Definition: PrivateKey.cpp:157

Friends And Related Function Documentation

◆ operator==

OpenDDS_Security_Export bool operator== ( const PrivateKey lhs,
const PrivateKey rhs 
)
friend

Definition at line 231 of file PrivateKey.cpp.

232 {
233  if (lhs.k_ && rhs.k_) {
234 #ifdef OPENSSL_V_3_0
235  return 1 == EVP_PKEY_eq(lhs.k_, rhs.k_);
236 #else
237  return 1 == EVP_PKEY_cmp(lhs.k_, rhs.k_);
238 #endif
239  }
240  return lhs.k_ == rhs.k_;
241 }

Member Data Documentation

◆ k_

EVP_PKEY* OpenDDS::Security::SSL::PrivateKey::k_
private

Definition at line 54 of file PrivateKey.h.

Referenced by load(), OpenDDS::Security::SSL::operator==(), sign(), and ~PrivateKey().


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