OpenDDS  Snapshot(2023/04/28-20:55)
CommonUtilities.cpp
Go to the documentation of this file.
1 #include "CommonUtilities.h"
3 
4 #include <string>
5 #include <cstdio>
6 #include <vector>
7 
9 
10 namespace OpenDDS {
11 namespace Security {
12 namespace CommonUtilities {
13 
15 
16 URI::URI(const std::string& src)
17  : scheme(URI_UNKNOWN), everything_else("") //authority(), path(""), query(""), fragment("")
18 {
19  typedef std::vector<std::pair<std::string, Scheme> > uri_pattern_t;
20 
21  uri_pattern_t uri_patterns;
22  uri_patterns.push_back(std::make_pair("file:", URI_FILE));
23  uri_patterns.push_back(std::make_pair("data:", URI_DATA));
24  uri_patterns.push_back(std::make_pair("pkcs11:", URI_PKCS11));
25 
26  for (uri_pattern_t::iterator i = uri_patterns.begin();
27  i != uri_patterns.end(); ++i) {
28  const std::string& pfx = i->first;
29  size_t pfx_end = pfx.length();
30 
31  if (src.substr(0, pfx_end) == pfx) {
32  everything_else = src.substr(pfx_end, std::string::npos);
33  scheme = i->second;
34  break;
35  }
36  }
37 }
38 
39 int increment_handle(int& next)
40 {
41  // handles are 32-bit signed values (int on all supported platforms)
42  // the only special value is 0 for HANDLE_NIL, 'next' starts at 1
43  // signed increment is not guaranteed to roll over so we implement our own
44  static const int LAST_POSITIVE_HANDLE(0x7fffffff);
45  static const int FIRST_NEGATIVE_HANDLE(-LAST_POSITIVE_HANDLE);
46  if (next == 0) {
47  ACE_ERROR((LM_ERROR, "(%P|%t) OpenDDS::Security::CommonUtilities::"
48  "increment_handle ERROR - out of handles\n"));
49  return 0;
50  }
51  const int h = next;
52  if (next == LAST_POSITIVE_HANDLE) {
53  next = FIRST_NEGATIVE_HANDLE;
54  } else {
55  ++next;
56  }
57  return h;
58 }
59 
61  int code,
62  int minor_code,
63  const char* message)
64 {
65  ex.code = code;
66  ex.minor_code = minor_code;
67  ex.message = message;
68  return false;
69 }
70 
72  int code,
73  int minor_code,
74  const char* message,
75  const unsigned char (&a1)[4],
76  const unsigned char (&a2)[4])
77 {
78  std::string full(message);
79  const size_t i = full.size();
80  full.resize(i + 25);
81  std::sprintf(&full[i], " %.2x %.2x %.2x %.2x, %.2x %.2x %.2x %.2x",
82  a1[0], a1[1], a1[2], a1[3], a2[0], a2[1], a2[2], a2[3]);
83  return set_security_error(ex, code, minor_code, full.c_str());
84 }
85 
86 const char* ctk_to_dds_string(const CryptoTransformKind& keyKind)
87 {
88  if (!keyKind[0] && !keyKind[1] && !keyKind[2]) {
89  switch (keyKind[3]) {
91  return "CRYPTO_TRANSFORMATION_KIND_NONE";
93  return "CRYPTO_TRANSFORMATION_KIND_AES128_GMAC";
95  return "CRYPTO_TRANSFORMATION_KIND_AES128_GCM";
97  return "CRYPTO_TRANSFORMATION_KIND_AES256_GMAC";
99  return "CRYPTO_TRANSFORMATION_KIND_AES256_GCM";
100  }
101  }
102  ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) ERROR: Security::CommonUtilities::ctk_to_dds_string: ")
103  ACE_TEXT("%C is either invalid or not recognized.\n"),
104  to_hex_dds_string(keyKind, sizeof(keyKind), ' ').c_str()));
105  return "Invalid CryptoTransformKind";
106 }
107 
109 {
110  return to_hex_dds_string(keyId, sizeof(keyId), ' ');
111 }
112 
114 {
115  if (keyData.length()) {
116  return to_hex_dds_string(&keyData[0], keyData.length(), '\n', 8);
117  }
118  return "";
119 }
120 
122 {
123  return
124  OPENDDS_STRING("transformation_kind: ") +
126  OPENDDS_STRING("\nmaster_salt:\n") +
128  OPENDDS_STRING("\nsender_key_id: ") +
130  OPENDDS_STRING("\nmaster_sender_key:\n") +
132  OPENDDS_STRING("\nreceiver_specific_key_id: ") +
134  OPENDDS_STRING("\nmaster_receiver_specific_key:\n") +
136  OPENDDS_STRING("\n");
137 }
138 
140 {
141  return
142  OPENDDS_STRING("transformation_kind: ") +
143  ctk_to_dds_string(id.transformation_kind) +
144  OPENDDS_STRING("\ntransformation_key_id: ") +
145  ctki_to_dds_string(id.transformation_key_id) +
146  OPENDDS_STRING("\n");
147 }
148 
149 }
150 }
151 }
152 
const octet CRYPTO_TRANSFORMATION_KIND_AES256_GCM
#define ACE_ERROR(X)
sequence< octet, 32 > KeyOctetSeq
OPENDDS_STRING to_dds_string(const KeyOctetSeq &keyData)
const octet CRYPTO_TRANSFORMATION_KIND_NONE
const octet CRYPTO_TRANSFORMATION_KIND_AES128_GCM
octet CryptoTransformKeyId[4]
Christopher Diggins *renamed files *fixing compilation errors *adding Visual C project file *removed make Max Lybbert *removed references to missing and unused as reported by Andy Elvey and Dan Kosecki *resynced with Christopher Diggins s branch as it exists in tree building code is back Christopher Diggins *resynced codebase with Chris s branch *removed tree building code
Definition: CHANGELOG.txt:8
const char * ctk_to_dds_string(const CryptoTransformKind &keyKind)
const octet CRYPTO_TRANSFORMATION_KIND_AES128_GMAC
#define OPENDDS_STRING
String to_hex_dds_string(const unsigned char *data, const size_t size, const char delim, const size_t delim_every)
octet CryptoTransformKind[4]
const octet CRYPTO_TRANSFORMATION_KIND_AES256_GMAC
ACE_TEXT("TCP_Factory")
OPENDDS_STRING ctki_to_dds_string(const CryptoTransformKeyId &keyId)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
bool set_security_error(DDS::Security::SecurityException &ex, int code, int minor_code, const char *message)
LM_ERROR
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28