Line data Source code
1 : #include "CommonUtilities.h"
2 : #include "dds/DCPS/SafetyProfileStreams.h"
3 :
4 : #include <string>
5 : #include <cstdio>
6 : #include <vector>
7 :
8 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
9 :
10 : namespace OpenDDS {
11 : namespace Security {
12 : namespace CommonUtilities {
13 :
14 : using OpenDDS::DCPS::to_hex_dds_string;
15 :
16 308 : URI::URI(const std::string& src)
17 308 : : scheme(URI_UNKNOWN), everything_else("") //authority(), path(""), query(""), fragment("")
18 : {
19 : typedef std::vector<std::pair<std::string, Scheme> > uri_pattern_t;
20 :
21 308 : uri_pattern_t uri_patterns;
22 308 : uri_patterns.push_back(std::make_pair("file:", URI_FILE));
23 308 : uri_patterns.push_back(std::make_pair("data:", URI_DATA));
24 308 : uri_patterns.push_back(std::make_pair("pkcs11:", URI_PKCS11));
25 :
26 308 : for (uri_pattern_t::iterator i = uri_patterns.begin();
27 360 : i != uri_patterns.end(); ++i) {
28 360 : const std::string& pfx = i->first;
29 360 : size_t pfx_end = pfx.length();
30 :
31 360 : if (src.substr(0, pfx_end) == pfx) {
32 308 : everything_else = src.substr(pfx_end, std::string::npos);
33 308 : scheme = i->second;
34 308 : break;
35 : }
36 : }
37 308 : }
38 :
39 121 : 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 121 : if (next == 0) {
47 0 : ACE_ERROR((LM_ERROR, "(%P|%t) OpenDDS::Security::CommonUtilities::"
48 : "increment_handle ERROR - out of handles\n"));
49 0 : return 0;
50 : }
51 121 : const int h = next;
52 121 : if (next == LAST_POSITIVE_HANDLE) {
53 0 : next = FIRST_NEGATIVE_HANDLE;
54 : } else {
55 121 : ++next;
56 : }
57 121 : return h;
58 : }
59 :
60 87 : bool set_security_error(DDS::Security::SecurityException& ex,
61 : int code,
62 : int minor_code,
63 : const char* message)
64 : {
65 87 : ex.code = code;
66 87 : ex.minor_code = minor_code;
67 87 : ex.message = message;
68 87 : return false;
69 : }
70 :
71 1 : bool set_security_error(DDS::Security::SecurityException& ex,
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 1 : std::string full(message);
79 1 : const size_t i = full.size();
80 1 : full.resize(i + 25);
81 2 : std::sprintf(&full[i], " %.2x %.2x %.2x %.2x, %.2x %.2x %.2x %.2x",
82 1 : a1[0], a1[1], a1[2], a1[3], a2[0], a2[1], a2[2], a2[3]);
83 2 : return set_security_error(ex, code, minor_code, full.c_str());
84 1 : }
85 :
86 0 : const char* ctk_to_dds_string(const CryptoTransformKind& keyKind)
87 : {
88 0 : if (!keyKind[0] && !keyKind[1] && !keyKind[2]) {
89 0 : switch (keyKind[3]) {
90 0 : case CRYPTO_TRANSFORMATION_KIND_NONE:
91 0 : return "CRYPTO_TRANSFORMATION_KIND_NONE";
92 0 : case CRYPTO_TRANSFORMATION_KIND_AES128_GMAC:
93 0 : return "CRYPTO_TRANSFORMATION_KIND_AES128_GMAC";
94 0 : case CRYPTO_TRANSFORMATION_KIND_AES128_GCM:
95 0 : return "CRYPTO_TRANSFORMATION_KIND_AES128_GCM";
96 0 : case CRYPTO_TRANSFORMATION_KIND_AES256_GMAC:
97 0 : return "CRYPTO_TRANSFORMATION_KIND_AES256_GMAC";
98 0 : case CRYPTO_TRANSFORMATION_KIND_AES256_GCM:
99 0 : return "CRYPTO_TRANSFORMATION_KIND_AES256_GCM";
100 : }
101 : }
102 0 : 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 0 : return "Invalid CryptoTransformKind";
106 : }
107 :
108 0 : OPENDDS_STRING ctki_to_dds_string(const CryptoTransformKeyId& keyId)
109 : {
110 0 : return to_hex_dds_string(keyId, sizeof(keyId), ' ');
111 : }
112 :
113 0 : OPENDDS_STRING to_dds_string(const KeyOctetSeq& keyData)
114 : {
115 0 : if (keyData.length()) {
116 0 : return to_hex_dds_string(&keyData[0], keyData.length(), '\n', 8);
117 : }
118 0 : return "";
119 : }
120 :
121 0 : OPENDDS_STRING to_dds_string(const KeyMaterial_AES_GCM_GMAC& km)
122 : {
123 : return
124 0 : OPENDDS_STRING("transformation_kind: ") +
125 0 : ctk_to_dds_string(km.transformation_kind) +
126 0 : OPENDDS_STRING("\nmaster_salt:\n") +
127 0 : to_dds_string(km.master_salt) +
128 0 : OPENDDS_STRING("\nsender_key_id: ") +
129 0 : ctki_to_dds_string(km.sender_key_id) +
130 0 : OPENDDS_STRING("\nmaster_sender_key:\n") +
131 0 : to_dds_string(km.master_sender_key) +
132 0 : OPENDDS_STRING("\nreceiver_specific_key_id: ") +
133 0 : ctki_to_dds_string(km.receiver_specific_key_id) +
134 0 : OPENDDS_STRING("\nmaster_receiver_specific_key:\n") +
135 0 : to_dds_string(km.master_receiver_specific_key) +
136 0 : OPENDDS_STRING("\n");
137 : }
138 :
139 0 : OPENDDS_STRING to_dds_string(const CryptoTransformIdentifier& id)
140 : {
141 : return
142 0 : OPENDDS_STRING("transformation_kind: ") +
143 0 : ctk_to_dds_string(id.transformation_kind) +
144 0 : OPENDDS_STRING("\ntransformation_key_id: ") +
145 0 : ctki_to_dds_string(id.transformation_key_id) +
146 0 : OPENDDS_STRING("\n");
147 : }
148 :
149 : }
150 : }
151 : }
152 :
153 : OPENDDS_END_VERSIONED_NAMESPACE_DECL
|