OpenDDS  Snapshot(2023/04/28-20:55)
Classes | Functions | Variables
OpenDDS::Security::SSL Namespace Reference

Classes

class  Bio
 
struct  cache_dsign_algo_impl
 
class  Certificate
 
struct  deserialize_impl
 
class  DH_2048_MODP_256_PRIME
 
class  dh_constructor
 
struct  DH_Handle
 
class  dh_shared_secret
 
class  DHAlgorithm
 
class  DiffieHellman
 
struct  EC_Handle
 
class  ecdh_constructor
 
class  ECDH_PRIME_256_V1_CEUM
 
class  ecdh_pubkey_as_octets
 
class  ecdh_shared_secret_from_octets
 
class  hash_serialized_impl
 
class  PKCS7Doc
 
class  PrivateKey
 
class  sign_implementation
 
class  SignedDocument
 
class  StackOfX509
 
class  SubjectName
 
class  verify_implementation
 
class  X509Store
 

Functions

std::ostreamoperator<< (std::ostream &lhs, const Certificate &rhs)
 
bool operator== (const Certificate &lhs, const Certificate &rhs)
 
bool operator== (const PrivateKey &lhs, const PrivateKey &rhs)
 
int make_adjusted_guid (const OpenDDS::DCPS::GUID_t &src, OpenDDS::DCPS::GUID_t &dst, const Certificate &target)
 
template<size_t Bits>
int make_nonce (std::vector< unsigned char > &nonce)
 
int make_nonce_256 (std::vector< unsigned char > &nonce)
 
int make_nonce_256 (DDS::OctetSeq &nonce)
 
unsigned char offset_1bit (const unsigned char array[], size_t i)
 Gets byte from array as though it were shifted right one bit. More...
 
int hash (const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst)
 
int hash_serialized (const DDS::BinaryPropertySeq &src, DDS::OctetSeq &dst)
 
int sign_serialized (const DDS::BinaryPropertySeq &src, const PrivateKey &key, DDS::OctetSeq &dst)
 
int verify_serialized (const DDS::BinaryPropertySeq &src, const Certificate &key, const DDS::OctetSeq &signed_data)
 

Variables

const char DH_2048_MODP_256_PRIME_STR [] = "DH+MODP-2048-256"
 
const char ECDH_PRIME_256_V1_CEUM_STR [] = "ECDH+prime256v1-CEUM"
 

Function Documentation

◆ hash()

OpenDDS_Security_Export int OpenDDS::Security::SSL::hash ( const std::vector< const DDS::OctetSeq * > &  src,
DDS::OctetSeq dst 
)
Returns
int 0 on success; 1 on failure.

Definition at line 132 of file security/SSL/Utils.cpp.

References EVP_MD_CTX_free, EVP_MD_CTX_new, and OPENDDS_SSL_LOG_ERR.

Referenced by OpenDDS::DCPS::AddressCacheEntryProxy::addrs(), OpenDDS::Security::AuthenticationBuiltInImpl::begin_handshake_reply(), OpenDDS::Security::AuthenticationBuiltInImpl::begin_handshake_request(), OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL::hash_endpoint(), OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL::hash_endpoints(), hash_serialized(), OpenDDS::Security::SSL::DHAlgorithm::hash_shared_secret(), make_adjusted_guid(), OpenDDS::DCPS::NetworkAddress::operator<(), and OpenDDS::Security::AuthenticationBuiltInImpl::process_handshake_reply().

133 {
134  EVP_MD_CTX* hash_ctx = EVP_MD_CTX_new();
135  if (!hash_ctx) {
136  OPENDDS_SSL_LOG_ERR("EVP_MD_CTX_new failed");
137  return 1;
138  }
139 
140  EVP_DigestInit_ex(hash_ctx, EVP_sha256(), 0);
141 
142  unsigned char hash[EVP_MAX_MD_SIZE] = { 0 };
143  unsigned int len = 0u;
144 
145  std::vector<const DDS::OctetSeq*>::const_iterator i, n = src.end();
146  for (i = src.begin(); i != n; ++i) {
147  EVP_DigestUpdate(hash_ctx, (*i)->get_buffer(), (*i)->length());
148  }
149 
150  EVP_DigestFinal_ex(hash_ctx, hash, &len);
151 
152  dst.length(len);
153  std::memcpy(dst.get_buffer(), hash, len);
154 
155  EVP_MD_CTX_free(hash_ctx);
156 
157  return 0;
158 }
int hash(const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst)
#define EVP_MD_CTX_new
#define EVP_MD_CTX_free
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

◆ hash_serialized()

OpenDDS_Security_Export int OpenDDS::Security::SSL::hash_serialized ( const DDS::BinaryPropertySeq &  src,
DDS::OctetSeq dst 
)
Returns
int 0 on success; 1 on failure.

Definition at line 214 of file security/SSL/Utils.cpp.

References hash().

Referenced by OpenDDS::Security::CredentialHash::operator()().

215 {
216  hash_serialized_impl hash;
217  return hash(src, dst);
218 }
int hash(const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst)

◆ make_adjusted_guid()

OpenDDS_Security_Export int OpenDDS::Security::SSL::make_adjusted_guid ( const OpenDDS::DCPS::GUID_t src,
OpenDDS::DCPS::GUID_t dst,
const Certificate target 
)

Definition at line 41 of file security/SSL/Utils.cpp.

References OpenDDS::DCPS::GUID_t::entityId, EVP_MD_CTX_free, EVP_MD_CTX_new, OpenDDS::DCPS::GUID_UNKNOWN, hash(), offset_1bit(), and OpenDDS::Security::SSL::Certificate::subject_name_digest().

Referenced by OpenDDS::Security::AuthenticationBuiltInImpl::validate_local_identity().

44 {
46  dst.entityId = src.entityId;
47 
48  std::vector<unsigned char> hash;
49  int result = target.subject_name_digest(hash);
50 
51  if (result == 0 && hash.size() >= 6) {
52  unsigned char* bytes = reinterpret_cast<unsigned char*>(&dst);
53 
54  for (size_t i = 0; i < 6; ++i) { // First 6 bytes of guid prefix
55  bytes[i] = offset_1bit(&hash[0], i);
56  }
57 
58  bytes[0] |= 0x80;
59 
60  // Last 6 bytes of guid prefix = hash of src guid (candidate guid)
61 
62  unsigned char hash2[EVP_MAX_MD_SIZE] = {0};
63  unsigned int len = 0u;
64 
65  EVP_MD_CTX* hash_ctx = EVP_MD_CTX_new();
66  if (hash_ctx) {
67  EVP_DigestInit_ex(hash_ctx, EVP_sha256(), 0);
68  EVP_DigestUpdate(hash_ctx, &src, sizeof(OpenDDS::DCPS::GUID_t));
69  EVP_DigestFinal_ex(hash_ctx, hash2, &len);
70  if (len > 5) {
71  std::memcpy(bytes + 6, hash2, 6);
72  } else {
73  result = 1;
74  }
75 
76  EVP_MD_CTX_free(hash_ctx);
77  }
78  }
79 
80  return result;
81 }
int hash(const std::vector< const DDS::OctetSeq *> &src, DDS::OctetSeq &dst)
const GUID_t GUID_UNKNOWN
Nil value for GUID.
Definition: GuidUtils.h:59
unsigned char offset_1bit(const unsigned char array[], size_t i)
Gets byte from array as though it were shifted right one bit.
#define EVP_MD_CTX_new
#define EVP_MD_CTX_free
key EntityId_t entityId
Definition: DdsDcpsGuid.idl:59

◆ make_nonce()

template<size_t Bits>
int OpenDDS::Security::SSL::make_nonce ( std::vector< unsigned char > &  nonce)

Definition at line 84 of file security/SSL/Utils.cpp.

References ACE_ERROR, ACE_TEXT(), and LM_ERROR.

85 {
86  nonce.clear();
87 
88  unsigned char tmp[Bits / 8] = { 0 };
89 
90  if (RAND_bytes(tmp, sizeof tmp) == 1) {
91  nonce.insert(nonce.begin(), tmp, tmp + sizeof tmp);
92 
93  return 0;
94 
95  } else {
96  unsigned long err = ERR_get_error();
97  char msg[256] = { 0 };
98  ERR_error_string_n(err, msg, sizeof(msg));
99 
100  ACE_ERROR((LM_ERROR,
101  ACE_TEXT("(%P|%t) SSL::make_nonce: ERROR '%C' returned by RAND_bytes(...)\n"),
102  msg));
103  }
104 
105  return 1;
106 }
#define ACE_ERROR(X)
ACE_TEXT("TCP_Factory")

◆ make_nonce_256() [1/2]

OpenDDS_Security_Export int OpenDDS::Security::SSL::make_nonce_256 ( std::vector< unsigned char > &  nonce)

◆ make_nonce_256() [2/2]

OpenDDS_Security_Export int OpenDDS::Security::SSL::make_nonce_256 ( DDS::OctetSeq nonce)
Returns
int 0 on success; 1 on failure.

Definition at line 113 of file security/SSL/Utils.cpp.

114 {
115  /* A bit slower but the impl. for vectors is already complete */
116  std::vector<unsigned char> tmp;
117  int err = make_nonce<256>(tmp);
118  if (!err) {
119  nonce.length(static_cast<unsigned int>(tmp.size()));
120  for (size_t i = 0; i < tmp.size(); ++i) {
121  nonce[static_cast<unsigned int>(i)] = tmp[i];
122  }
123  }
124  return err;
125 }

◆ offset_1bit()

OpenDDS_Security_Export unsigned char OpenDDS::Security::SSL::offset_1bit ( const unsigned char  array[],
size_t  i 
)

Gets byte from array as though it were shifted right one bit.

Definition at line 127 of file security/SSL/Utils.cpp.

Referenced by make_adjusted_guid(), and OpenDDS::Security::validate_topic_data_guid().

128 {
129  return (array[i] >> 1) | (i == 0 ? 0 : ((array[i - 1] & 1) ? 0x80 : 0));
130 }

◆ operator<<()

OpenDDS_Security_Export std::ostream & OpenDDS::Security::SSL::operator<< ( std::ostream lhs,
const Certificate rhs 
)

Definition at line 644 of file Certificate.cpp.

References OpenDDS::Security::SSL::Certificate::x_.

645 {
646  if (rhs.x_) {
647  lhs << "Certificate: { is_ca? '"
648  << (X509_check_ca(rhs.x_) ? "yes" : "no") << "'; }";
649 
650  } else {
651  lhs << "NULL";
652  }
653  return lhs;
654 }

◆ operator==() [1/2]

OpenDDS_Security_Export bool OpenDDS::Security::SSL::operator== ( const PrivateKey lhs,
const PrivateKey rhs 
)

Definition at line 231 of file PrivateKey.cpp.

References OpenDDS::Security::SSL::PrivateKey::k_, and OPENDDS_END_VERSIONED_NAMESPACE_DECL.

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 }

◆ operator==() [2/2]

OpenDDS_Security_Export bool OpenDDS::Security::SSL::operator== ( const Certificate lhs,
const Certificate rhs 
)

Definition at line 656 of file Certificate.cpp.

References OPENDDS_END_VERSIONED_NAMESPACE_DECL, OpenDDS::Security::SSL::Certificate::original_bytes_, and OpenDDS::Security::SSL::Certificate::x_.

Referenced by OpenDDS::Security::SSL::SignedDocument::filename().

657 {
658  if (lhs.x_ && rhs.x_) {
659  return (0 == X509_cmp(lhs.x_, rhs.x_)) &&
660  (lhs.original_bytes_ == rhs.original_bytes_);
661  }
662  return (lhs.x_ == rhs.x_) &&
663  (lhs.original_bytes_ == rhs.original_bytes_);
664 }

◆ sign_serialized()

OpenDDS_Security_Export int OpenDDS::Security::SSL::sign_serialized ( const DDS::BinaryPropertySeq &  src,
const PrivateKey key,
DDS::OctetSeq dst 
)
Returns
int 0 on success; 1 on failure.

Definition at line 220 of file security/SSL/Utils.cpp.

References ACE_ERROR, ACE_TEXT(), OpenDDS::STUN::encoding(), LM_ERROR, OpenDDS::DCPS::serialized_size(), and OpenDDS::Security::SSL::PrivateKey::sign().

Referenced by OpenDDS::Security::AuthenticationBuiltInImpl::begin_handshake_reply(), and OpenDDS::Security::AuthenticationBuiltInImpl::process_handshake_reply().

222 {
223  const Encoding encoding = get_common_encoding();
224  size_t size = 0;
225  serialized_size(encoding, size, src);
226 
227  DDS::OctetSeq tmp;
228  tmp.length(static_cast<unsigned int>(size));
229  ACE_Message_Block buffer(reinterpret_cast<const char*>(tmp.get_buffer()),
230  tmp.length());
231  Serializer serializer(&buffer, encoding);
232  if (!(serializer << src)) {
233  ACE_ERROR((LM_ERROR,
234  ACE_TEXT("(%P|%t) SSL::sign_serialized: ERROR, failed to serialize "
235  "binary-property-sequence\n")));
236 
237  return 1;
238  }
239 
240  std::vector<const DDS::OctetSeq*> sign_these;
241  sign_these.push_back(&tmp);
242 
243  return key.sign(sign_these, dst);
244 }
#define ACE_ERROR(X)
sequence< octet > key
void serialized_size(const Encoding &encoding, size_t &size, const SequenceNumber &)
sequence< octet > OctetSeq
Definition: DdsDcpsCore.idl:64
ACE_TEXT("TCP_Factory")
const DCPS::Encoding encoding(DCPS::Encoding::KIND_UNALIGNED_CDR, DCPS::ENDIAN_BIG)

◆ verify_serialized()

OpenDDS_Security_Export int OpenDDS::Security::SSL::verify_serialized ( const DDS::BinaryPropertySeq &  src,
const Certificate key,
const DDS::OctetSeq signed_data 
)
Returns
int 0 on success; 1 on failure.

Definition at line 246 of file security/SSL/Utils.cpp.

References ACE_ERROR, ACE_TEXT(), OpenDDS::STUN::encoding(), LM_ERROR, OPENDDS_END_VERSIONED_NAMESPACE_DECL, OpenDDS::DCPS::serialized_size(), and OpenDDS::Security::SSL::Certificate::verify_signature().

Referenced by OpenDDS::Security::AuthenticationBuiltInImpl::process_final_handshake(), and OpenDDS::Security::AuthenticationBuiltInImpl::process_handshake_reply().

249 {
250  const Encoding encoding = get_common_encoding();
251  size_t size = 0;
252  serialized_size(encoding, size, src);
253 
254  DDS::OctetSeq tmp;
255  tmp.length(static_cast<unsigned int>(size));
256  ACE_Message_Block buffer(reinterpret_cast<const char*>(tmp.get_buffer()),
257  tmp.length());
258  Serializer serializer(&buffer, encoding);
259  if (!(serializer << src)) {
260  ACE_ERROR((LM_ERROR,
261  ACE_TEXT("(%P|%t) SSL::verify_serialized: ERROR, failed to serialize binary-property-sequence\n")));
262 
263  return 1;
264  }
265 
266  std::vector<const DDS::OctetSeq*> verify_these;
267  verify_these.push_back(&tmp);
268 
269  return key.verify_signature(signed_data, verify_these);
270 }
#define ACE_ERROR(X)
sequence< octet > key
void serialized_size(const Encoding &encoding, size_t &size, const SequenceNumber &)
sequence< octet > OctetSeq
Definition: DdsDcpsCore.idl:64
ACE_TEXT("TCP_Factory")
const DCPS::Encoding encoding(DCPS::Encoding::KIND_UNALIGNED_CDR, DCPS::ENDIAN_BIG)

Variable Documentation

◆ DH_2048_MODP_256_PRIME_STR

const char OpenDDS::Security::SSL::DH_2048_MODP_256_PRIME_STR[] = "DH+MODP-2048-256"

◆ ECDH_PRIME_256_V1_CEUM_STR

const char OpenDDS::Security::SSL::ECDH_PRIME_256_V1_CEUM_STR[] = "ECDH+prime256v1-CEUM"