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

#include <SignedDocument.h>

Collaboration diagram for OpenDDS::Security::SSL::SignedDocument:
Collaboration graph
[legend]

Public Member Functions

 SignedDocument (const DDS::OctetSeq &src)
 
 SignedDocument ()
 
virtual ~SignedDocument ()
 
bool load (const std::string &uri, DDS::Security::SecurityException &ex)
 
bool verify (const Certificate &ca)
 
const DDS::OctetSeqoriginal () const
 
const std::string & content () const
 
bool verified () const
 
const std::string & filename () const
 
bool operator== (const SignedDocument &other) const
 

Private Member Functions

void load_file (const std::string &path)
 

Private Attributes

DDS::OctetSeq original_
 
std::string content_
 
bool verified_
 
std::string filename_
 

Detailed Description

Definition at line 25 of file SignedDocument.h.

Constructor & Destructor Documentation

◆ SignedDocument() [1/2]

OpenDDS::Security::SSL::SignedDocument::SignedDocument ( const DDS::OctetSeq src)
explicit

Definition at line 42 of file SignedDocument.cpp.

◆ SignedDocument() [2/2]

OpenDDS::Security::SSL::SignedDocument::SignedDocument ( )

Definition at line 34 of file SignedDocument.cpp.

◆ ~SignedDocument()

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

Definition at line 50 of file SignedDocument.cpp.

51 {
52 }

Member Function Documentation

◆ content()

const std::string& OpenDDS::Security::SSL::SignedDocument::content ( ) const
inline

◆ filename()

const std::string& OpenDDS::Security::SSL::SignedDocument::filename ( void  ) const
inline

◆ load()

bool OpenDDS::Security::SSL::SignedDocument::load ( const std::string &  uri,
DDS::Security::SecurityException ex 
)

Definition at line 54 of file SignedDocument.cpp.

References ACE_ERROR, content_, OpenDDS::Security::CommonUtilities::URI::everything_else, filename_, LM_WARNING, load_file(), original_, OpenDDS::Security::CommonUtilities::URI::scheme, OpenDDS::Security::CommonUtilities::set_security_error(), OpenDDS::Security::CommonUtilities::URI::URI_DATA, OpenDDS::Security::CommonUtilities::URI::URI_FILE, OpenDDS::Security::CommonUtilities::URI::URI_PKCS11, OpenDDS::Security::CommonUtilities::URI::URI_UNKNOWN, and verified_.

Referenced by OpenDDS::Security::LocalAccessCredentialData::load().

55 {
56  using namespace CommonUtilities;
57 
58  original_.length(0);
59  content_.clear();
60  verified_ = false;
61  filename_ = default_filename;
62 
63  URI uri_info(uri);
64 
65  switch (uri_info.scheme) {
66  case URI::URI_FILE: {
67  load_file(uri_info.everything_else);
68  break;
69  }
70  case URI::URI_DATA:
71  original_.length(static_cast<unsigned int>(uri_info.everything_else.length() + 1));
72  std::memcpy(original_.get_buffer(), uri_info.everything_else.c_str(), uri_info.everything_else.length() + 1);
73  filename_ = data_filename;
74  break;
75 
76  case URI::URI_PKCS11:
77  case URI::URI_UNKNOWN:
78  default:
79  ACE_ERROR((LM_WARNING,
80  "(%P|%t) SSL::SignedDocument::load: WARNING: Unsupported URI scheme\n"));
81  break;
82  }
83 
84  if (original_.length() == 0) {
85  std::stringstream msg;
86  msg << "SSL::SignedDocument::load: WARNING: Failed to load document supplied "
87  "with URI '" << uri << "'";
88  set_security_error(ex, -1, 0, msg.str().c_str());
89  return false;
90  }
91 
92  return true;
93 }
#define ACE_ERROR(X)
void load_file(const std::string &path)
bool set_security_error(DDS::Security::SecurityException &ex, int code, int minor_code, const char *message)

◆ load_file()

void OpenDDS::Security::SSL::SignedDocument::load_file ( const std::string &  path)
private

Definition at line 305 of file SignedDocument.cpp.

References ACE_ERROR, ACE_OS::fclose(), filename_, ACE_OS::fopen(), ACE_OS::fread(), LM_WARNING, ACE_OS::memcpy(), and original_.

Referenced by load().

306 {
307  filename_ = path;
308 
309 #ifdef ACE_ANDROID
310  CORBA::Octet *buffer;
311 
312  char b[1024];
313  FILE* fp = ACE_OS::fopen(path.c_str(), "rb");
314 
315  int n;
316  int i = 0;
317  while (!feof(fp)) {
318  n = ACE_OS::fread(&b, 1, 1024, fp);
319  i += n;
320 
321  original_.length(i + 1); // +1 for null byte at end of cert
322  buffer = original_.get_buffer();
323  ACE_OS::memcpy(buffer + i - n, b, n);
324  }
325 
326  ACE_OS::fclose(fp);
327 
328  // To appease the other DDS security implementations which
329  // append a null byte at the end of the cert.
330  buffer[i + 1] = 0u;
331 
332 #else
333  std::ifstream in(path.c_str(), std::ios::binary);
334 
335  if (!in) {
336  ACE_ERROR((LM_WARNING,
337  "(%P|%t) SignedDocument::PKCS7_from_SMIME_file:"
338  "WARNING: Failed to load file '%C'; '%m'\n",
339  path.c_str()));
340  return;
341  }
342 
343  const std::ifstream::pos_type begin = in.tellg();
344  in.seekg(0, std::ios::end);
345  const std::ifstream::pos_type end = in.tellg();
346  in.seekg(0, std::ios::beg);
347 
348  original_.length(static_cast<CORBA::ULong>(end - begin + 1));
349  in.read(reinterpret_cast<char*>(original_.get_buffer()), end - begin);
350 
351  if (!in) {
352  ACE_ERROR((LM_WARNING,
353  "(%P|%t) SignedDocument::PKCS7_from_SMIME_file:"
354  "WARNING: Failed to load file '%C'; '%m'\n",
355  path.c_str()));
356  return;
357  }
358 
359  // To appease the other DDS security implementations
360  original_[original_.length() - 1] = 0u;
361 #endif
362 }
int fclose(FILE *fp)
#define ACE_ERROR(X)
void * memcpy(void *t, const void *s, size_t len)
FILE * fopen(const char *filename, const char *mode)
size_t fread(void *ptr, size_t size, size_t nelems, FILE *fp)
ACE_CDR::Octet Octet

◆ operator==()

bool OpenDDS::Security::SSL::SignedDocument::operator== ( const SignedDocument other) const

Definition at line 364 of file SignedDocument.cpp.

References content_, OPENDDS_END_VERSIONED_NAMESPACE_DECL, original_, and verified_.

365 {
366  return original_ == other.original_ && content_ == other.content_ && verified_ == other.verified_;
367 }

◆ original()

const DDS::OctetSeq& OpenDDS::Security::SSL::SignedDocument::original ( ) const
inline

◆ verified()

bool OpenDDS::Security::SSL::SignedDocument::verified ( ) const
inline

Definition at line 37 of file SignedDocument.h.

◆ verify()

bool OpenDDS::Security::SSL::SignedDocument::verify ( const Certificate ca)

Definition at line 254 of file SignedDocument.cpp.

References OpenDDS::Security::SSL::Bio::bio(), content(), content_, OpenDDS::Security::SSL::Bio::get_mem_data(), OpenDDS::Security::SSL::Bio::new_mem(), OPENDDS_SSL_LOG_ERR, original_, OpenDDS::Security::SSL::StackOfX509::push(), verified_, OpenDDS::Security::SSL::PKCS7Doc::verify(), and OpenDDS::Security::SSL::Bio::write().

Referenced by OpenDDS::Security::LocalAccessCredentialData::verify().

255 {
256  content_.clear();
257  verified_ = false;
258 
259  StackOfX509 certs;
260  if (!certs) {
261  return false;
262  }
263 
264  if (!certs.push(ca)) {
265  return false;
266  }
267 
268  Bio filebuf;
269  if (!filebuf.new_mem()) {
270  return false;
271  }
272 
273  if (!filebuf.write(original_.get_buffer(), original_.length())) {
274  return false;
275  }
276 
277  Bio bcont;
278  PKCS7Doc doc(SMIME_read_PKCS7(filebuf.bio(), &bcont.bio()));
279  if (!doc) {
280  OPENDDS_SSL_LOG_ERR("SMIME_read_PKCS7 failed");
281  return false;
282  }
283 
284  Bio content;
285  if (!content.new_mem()) {
286  return false;
287  }
288 
289  if (!doc.verify(&certs, 0, bcont, content, PKCS7_TEXT | PKCS7_NOVERIFY | PKCS7_NOINTERN)) {
290  return false;
291  }
292 
293  char* p = 0;
294  long size = content.get_mem_data(&p);
295  if (size < 0) {
296  return false;
297  }
298 
299  content_ = std::string(p, size);
300  verified_ = true;
301 
302  return verified_;
303 }
const std::string & content() const
#define OPENDDS_SSL_LOG_ERR(MSG)
Definition: Err.h:12

Member Data Documentation

◆ content_

std::string OpenDDS::Security::SSL::SignedDocument::content_
private

Definition at line 46 of file SignedDocument.h.

Referenced by load(), operator==(), and verify().

◆ filename_

std::string OpenDDS::Security::SSL::SignedDocument::filename_
private

Definition at line 48 of file SignedDocument.h.

Referenced by load(), and load_file().

◆ original_

DDS::OctetSeq OpenDDS::Security::SSL::SignedDocument::original_
private

Definition at line 45 of file SignedDocument.h.

Referenced by load(), load_file(), operator==(), and verify().

◆ verified_

bool OpenDDS::Security::SSL::SignedDocument::verified_
private

Definition at line 47 of file SignedDocument.h.

Referenced by load(), operator==(), and verify().


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