Debian Codegraph @

src/security/core/tests/common/cryptography_wrapper.c

raw
1 /*
2 * Copyright(c) 2006 to 2021 ZettaScale Technology and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7 * v. 1.0 which is available at
8 * http://www.eclipse.org/org/documents/edl-v10.php.
9 *
10 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11 */
12 #include <string.h>
13 #include <stdio.h>
14 #include "CUnit/Test.h"
15 #include "dds/dds.h"
16 #include "dds/ddsrt/circlist.h"
17 #include "dds/ddsrt/heap.h"
18 #include "dds/ddsrt/sync.h"
19 #include "dds/ddsrt/string.h"
20 #include "dds/ddsi/ddsi_domaingv.h"
21 #include "dds/security/dds_security_api.h"
22 #include "dds/security/dds_security_api_defs.h"
23 #include "dds/security/core/dds_security_utils.h"
24 #include "crypto_tokens.h"
25 #include "cryptography_wrapper.h"
26
27 int32_t init_crypto(const char *argument, void **context, struct ddsi_domaingv *gv);
28 int32_t finalize_crypto(void *context);
29
30 enum crypto_plugin_mode {
31 PLUGIN_MODE_ALL_OK,
32 PLUGIN_MODE_MISSING_FUNC,
33 PLUGIN_MODE_WRAPPED,
34 PLUGIN_MODE_TOKEN_LOG,
35 PLUGIN_MODE_PLAIN_DATA
36 };
37
38 struct dds_security_crypto_key_exchange_impl {
39 struct dds_security_crypto_key_exchange base;
40 struct dds_security_crypto_key_exchange *instance;
41 struct dds_security_cryptography_impl *parent;
42 };
43
44 struct dds_security_crypto_key_factory_impl {
45 struct dds_security_crypto_key_factory base;
46 struct dds_security_crypto_key_factory *instance;
47 struct dds_security_cryptography_impl *parent;
48 };
49
50 struct dds_security_crypto_transform_impl {
51 struct dds_security_crypto_transform base;
52 struct dds_security_crypto_transform *instance;
53 struct dds_security_cryptography_impl *parent;
54 };
55
56 struct dds_security_cryptography_impl {
57 struct dds_security_cryptography base;
58 struct dds_security_cryptography *instance;
59 struct dds_security_crypto_transform_impl transform_wrap;
60 struct dds_security_crypto_key_factory_impl factory_wrap;
61 struct dds_security_crypto_key_exchange_impl exchange_wrap;
62 enum crypto_plugin_mode mode;
63 bool protection_kinds_set;
64 bool disc_protection_kinds_set;
65 DDS_Security_ProtectionKind rtps_protection_kind;
66 DDS_Security_ProtectionKind metadata_protection_kind;
67 DDS_Security_BasicProtectionKind payload_protection_kind;
68 DDS_Security_ProtectionKind disc_protection_kind;
69 DDS_Security_ProtectionKind liveliness_protection_kind;
70 const char * pp_secret;
71 const char * groupdata_secret;
72 const char * ep_secret;
73 const char * encrypted_secret;
74 ddsrt_mutex_t token_data_lock;
75 struct ddsrt_circlist token_data_list;
76 ddsrt_mutex_t encode_decode_log_lock;
77 struct ddsrt_circlist encode_decode_log;
78 bool force_plain_rtps;
79 bool force_plain_submsg;
80 bool force_plain_payload;
81 DDS_Security_DatawriterCryptoHandle force_plain_sender_handle;
82 };
83
84 static DDS_Security_ParticipantCryptoHandle g_local_participant_handle = 0;
85 static ddsrt_once_t lock_inited = DDSRT_ONCE_INIT;
86 static ddsrt_mutex_t g_print_token_lock;
87
88 void set_protection_kinds(
89 struct dds_security_cryptography_impl * impl,
90 DDS_Security_ProtectionKind rtps_protection_kind,
91 DDS_Security_ProtectionKind metadata_protection_kind,
92 DDS_Security_BasicProtectionKind payload_protection_kind)
93 {
94 assert(impl);
95 impl->rtps_protection_kind = rtps_protection_kind;
96 impl->metadata_protection_kind = metadata_protection_kind;
97 impl->payload_protection_kind = payload_protection_kind;
98 impl->protection_kinds_set = true;
99 }
100
101 void set_encrypted_secret(struct dds_security_cryptography_impl * impl, const char * secret)
102 {
103 assert(impl);
104 impl->encrypted_secret = secret;
105 }
106
107 void set_disc_protection_kinds(
108 struct dds_security_cryptography_impl * impl,
109 DDS_Security_ProtectionKind disc_protection_kind,
110 DDS_Security_ProtectionKind liveliness_protection_kind)
111 {
112 assert(impl);
113 impl->disc_protection_kind = disc_protection_kind;
114 impl->liveliness_protection_kind = liveliness_protection_kind;
115 impl->disc_protection_kinds_set = true;
116 }
117
118 void set_entity_data_secret(struct dds_security_cryptography_impl * impl, const char * pp_secret, const char * groupdata_secret, const char * ep_secret)
119 {
120 assert(impl);
121 impl->pp_secret = pp_secret;
122 impl->groupdata_secret = groupdata_secret;
123 impl->ep_secret = ep_secret;
124 }
125
126 void set_force_plain_data(struct dds_security_cryptography_impl * impl, DDS_Security_DatawriterCryptoHandle handle, bool plain_rtps, bool plain_submsg, bool plain_payload)
127 {
128 assert (impl);
129 assert (impl->mode == PLUGIN_MODE_PLAIN_DATA);
130 impl->force_plain_rtps = plain_rtps;
131 impl->force_plain_submsg = plain_submsg;
132 impl->force_plain_payload = plain_payload;
133 impl->force_plain_sender_handle = handle;
134 }
135
136 static bool check_crypto_tokens(const DDS_Security_DataHolderSeq *tokens)
137 {
138 bool result = true;
139
140 if (tokens->_length == 0 || tokens->_buffer == NULL)
141 return false;
142
143 for (uint32_t i = 0; result && (i < tokens->_length); i++)
144 {
145 result = (tokens->_buffer[i].class_id != NULL &&
146 strcmp(DDS_CRYPTOTOKEN_CLASS_ID, tokens->_buffer[i].class_id) == 0 &&
147 tokens->_buffer[i].binary_properties._length == 1 &&
148 tokens->_buffer[i].binary_properties._buffer != NULL &&
149 tokens->_buffer[i].binary_properties._buffer[0].name != NULL &&
150 strcmp(DDS_CRYPTOTOKEN_PROP_KEYMAT, tokens->_buffer[i].binary_properties._buffer[0].name) == 0 &&
151 tokens->_buffer[i].binary_properties._buffer[0].value._length > 0 &&
152 tokens->_buffer[i].binary_properties._buffer[0].value._buffer != NULL);
153 }
154 return result;
155 }
156
157 const char *get_crypto_token_type_str (enum crypto_tokens_type type)
158 {
159 switch (type)
160 {
161 case LOCAL_PARTICIPANT_TOKENS: return "LOCAL_PARTICIPANT_TOKENS";
162 case LOCAL_WRITER_TOKENS: return "LOCAL_WRITER_TOKENS";
163 case LOCAL_READER_TOKENS: return "LOCAL_READER_TOKENS";
164 case REMOTE_PARTICIPANT_TOKENS: return "REMOTE_PARTICIPANT_TOKENS";
165 case REMOTE_WRITER_TOKENS: return "REMOTE_WRITER_TOKENS";
166 case REMOTE_READER_TOKENS: return "REMOTE_READER_TOKENS";
167 default: assert (0); return "";
168 }
169 }
170
171 static void print_tokens (enum crypto_tokens_type type, const DDS_Security_ParticipantCryptoHandle lch, const DDS_Security_ParticipantCryptoHandle rch,
172 const DDS_Security_ParticipantCryptoTokenSeq *tokens)
173 {
174 ddsrt_mutex_lock (&g_print_token_lock);
175 printf ("Token type %s, local %"PRIx64" / remote %"PRIx64", count: %u\n", get_crypto_token_type_str (type), lch, rch, tokens->_length);
176 for (uint32_t i = 0; i < tokens->_length; i++)
177 {
178 printf ("- token: ");
179 for (uint32_t j = 0; j < tokens->_buffer[i].binary_properties._buffer[0].value._length && j < 32; j++)
180 printf ("%02x", tokens->_buffer[i].binary_properties._buffer[0].value._buffer[j]);
181 printf ("\n");
182 }
183 printf ("\n");
184 ddsrt_mutex_unlock (&g_print_token_lock);
185 }
186
187 static void add_tokens (struct ddsrt_circlist *list, enum crypto_tokens_type type,
188 const DDS_Security_ParticipantCryptoHandle lch, const DDS_Security_ParticipantCryptoHandle rch,
189 const DDS_Security_ParticipantCryptoTokenSeq *tokens)
190 {
191 struct crypto_token_data *token_data = ddsrt_malloc (sizeof (*token_data));
192 token_data->type = type;
193 token_data->local_handle = lch;
194 token_data->remote_handle = rch;
195 token_data->n_tokens = tokens->_length;
196 assert (tokens->_length <= CRYPTO_TOKEN_MAXCOUNT);
197 for (uint32_t i = 0; i < tokens->_length; i++)
198 {
199 size_t len = tokens->_buffer[i].binary_properties._buffer[0].value._length;
200 assert (len <= CRYPTO_TOKEN_SIZE);
201 memcpy (token_data->data[i], tokens->_buffer[i].binary_properties._buffer[0].value._buffer, len);
202 token_data->data_len[i] = len;
203 }
204 ddsrt_circlist_append(list, &token_data->e);
205 }
206
207 static void store_tokens (struct dds_security_crypto_key_exchange_impl *impl, enum crypto_tokens_type type, const DDS_Security_ParticipantCryptoHandle lch,
208 const DDS_Security_ParticipantCryptoHandle rch, const DDS_Security_ParticipantCryptoTokenSeq *tokens)
209 {
210 if (!check_crypto_tokens ((const DDS_Security_DataHolderSeq *) tokens))
211 {
212 printf ("%d ERROR\n", type);
213 return;
214 }
215
216 ddsrt_mutex_lock (&impl->parent->token_data_lock);
217 add_tokens (&impl->parent->token_data_list, type, lch, rch, tokens);
218 ddsrt_mutex_unlock (&impl->parent->token_data_lock);
219
220 print_tokens (type, lch, rch, tokens);
221 }
222
223 struct ddsrt_circlist * get_crypto_tokens (struct dds_security_cryptography_impl * impl)
224 {
225 struct ddsrt_circlist *tokens = ddsrt_malloc (sizeof (*tokens));
226 ddsrt_circlist_init (tokens);
227
228 ddsrt_mutex_lock (&impl->token_data_lock);
229 if (ddsrt_circlist_isempty (&impl->encode_decode_log))
230 {
231 ddsrt_mutex_unlock (&impl->token_data_lock);
232 return tokens;
233 }
234
235 struct ddsrt_circlist_elem *elem0 = ddsrt_circlist_oldest (&impl->token_data_list), *elem = elem0;
236 do
237 {
238 struct crypto_token_data *elem_data = DDSRT_FROM_CIRCLIST (struct crypto_token_data, e, elem);
239 struct crypto_token_data *token_data = ddsrt_malloc (sizeof (*token_data));
240 memcpy (token_data, elem_data, sizeof (*token_data));
241 ddsrt_circlist_append (tokens, &token_data->e);
242 elem = elem->next;
243 } while (elem != elem0);
244 ddsrt_mutex_unlock (&impl->token_data_lock);
245
246 return tokens;
247 }
248
249 struct crypto_token_data * find_crypto_token (struct dds_security_cryptography_impl * impl, enum crypto_tokens_type type, unsigned char * data, size_t data_len)
250 {
251 assert (data_len <= CRYPTO_TOKEN_SIZE);
252 ddsrt_mutex_lock (&impl->token_data_lock);
253 if (ddsrt_circlist_isempty (&impl->encode_decode_log))
254 {
255 ddsrt_mutex_unlock (&impl->token_data_lock);
256 return NULL;
257 }
258 struct ddsrt_circlist_elem *elem0 = ddsrt_circlist_oldest (&impl->token_data_list), *elem = elem0;
259 do
260 {
261 struct crypto_token_data *elem_data = DDSRT_FROM_CIRCLIST (struct crypto_token_data, e, elem);
262 if (elem_data->type == type)
263 {
264 for (uint32_t i = 0; i < elem_data->n_tokens; i++)
265 {
266 size_t len = elem_data->data_len[i];
267 assert (len <= CRYPTO_TOKEN_SIZE);
268 if (!memcmp (data, elem_data->data[i], data_len < len ? data_len : len))
269 {
270 ddsrt_mutex_unlock (&impl->token_data_lock);
271 return elem_data;
272 }
273 }
274 }
275 elem = elem->next;
276 } while (elem != elem0);
277 ddsrt_mutex_unlock (&impl->token_data_lock);
278 return NULL;
279 }
280
281 static void log_encode_decode (struct dds_security_cryptography_impl * impl, enum crypto_encode_decode_fn function, DDS_Security_long_long handle)
282 {
283 ddsrt_mutex_lock (&impl->encode_decode_log_lock);
284 if (!ddsrt_circlist_isempty (&impl->encode_decode_log))
285 {
286 struct ddsrt_circlist_elem *elem0 = ddsrt_circlist_oldest (&impl->encode_decode_log), *elem = elem0;
287 do
288 {
289 struct crypto_encode_decode_data *data = DDSRT_FROM_CIRCLIST (struct crypto_encode_decode_data, e, elem);
290 if (data->function == function && data->handle == handle)
291 {
292 data->count++;
293 ddsrt_mutex_unlock (&impl->encode_decode_log_lock);
294 return;
295 }
296 elem = elem->next;
297 } while (elem != elem0);
298 }
299 /* add new entry */
300 struct crypto_encode_decode_data *new_data = ddsrt_malloc (sizeof (*new_data));
301 new_data->function = function;
302 new_data->handle = handle;
303 new_data->count = 1;
304 ddsrt_circlist_append(&impl->encode_decode_log, &new_data->e);
305 ddsrt_mutex_unlock (&impl->encode_decode_log_lock);
306 }
307
308 struct crypto_encode_decode_data * get_encode_decode_log (struct dds_security_cryptography_impl * impl, enum crypto_encode_decode_fn function, DDS_Security_long_long handle)
309 {
310 ddsrt_mutex_lock (&impl->encode_decode_log_lock);
311 if (!ddsrt_circlist_isempty (&impl->encode_decode_log))
312 {
313 struct ddsrt_circlist_elem *elem0 = ddsrt_circlist_oldest (&impl->encode_decode_log), *elem = elem0;
314 do
315 {
316 struct crypto_encode_decode_data *data = DDSRT_FROM_CIRCLIST (struct crypto_encode_decode_data, e, elem);
317 if (data->function == function && data->handle == handle)
318 {
319 struct crypto_encode_decode_data *result = ddsrt_malloc (sizeof (*result));
320 memcpy (result, data, sizeof (*result));
321 ddsrt_mutex_unlock (&impl->encode_decode_log_lock);
322 return result;
323 }
324 elem = elem->next;
325 } while (elem != elem0);
326 }
327 ddsrt_mutex_unlock (&impl->encode_decode_log_lock);
328 return NULL;
329 }
330
331 static unsigned char * find_buffer_match(const unsigned char *input, size_t input_len, const unsigned char *match, size_t match_len)
332 {
333 if (match_len <= input_len && match_len > 0 && input_len > 0)
334 {
335 const unsigned char *match_end = match + match_len;
336 unsigned char *i = (unsigned char *) input;
337 while (i <= input + input_len - match_len)
338 {
339 unsigned char *m = (unsigned char *) match, *j = i;
340 while (*m == *j && j < input + input_len)
341 {
342 j++;
343 if (++m == match_end)
344 return i;
345 }
346 i++;
347 }
348 }
349 return NULL;
350 }
351
352 static bool check_buffers(const DDS_Security_OctetSeq *encoded_buffer, const DDS_Security_OctetSeq *plain_buffer, bool expect_encrypted, DDS_Security_SecurityException *ex)
353 {
354 unsigned char *m = find_buffer_match (encoded_buffer->_buffer, encoded_buffer->_length,
355 plain_buffer->_buffer, plain_buffer->_length);
356 if ((m == NULL) != expect_encrypted)
357 {
358 ex->code = 1;
359 ex->message = ddsrt_strdup (expect_encrypted ?
360 "Expect encryption, but clear payload found after encoding." : "Expect only signature, but clear payload was not found in source after decoding.");
361 return false;
362 }
363 return true;
364 }
365
366 static DDS_Security_long_long check_handle(DDS_Security_long_long handle)
367 {
368 /* Assume that handle, which actually is a pointer, has a value that is likely to be
369 a valid memory address and not a value returned by the mock implementation. */
370 CU_ASSERT_FATAL (handle == 0 || handle > 4096);
371 return handle;
372 }
373
374 static bool expect_encrypted_buffer (DDS_Security_ProtectionKind pk)
375 {
376 return pk == DDS_SECURITY_PROTECTION_KIND_ENCRYPT || pk == DDS_SECURITY_PROTECTION_KIND_ENCRYPT_WITH_ORIGIN_AUTHENTICATION;
377 }
378
379 static void copy_octetseq(DDS_Security_OctetSeq *encoded_submsg, const DDS_Security_OctetSeq *plain_submsg)
380 {
381 encoded_submsg->_length = encoded_submsg->_maximum = plain_submsg->_length;
382 if (plain_submsg->_length > 0)
383 {
384 encoded_submsg->_buffer = ddsrt_malloc(encoded_submsg->_length);
385 memcpy(encoded_submsg->_buffer, plain_submsg->_buffer, encoded_submsg->_length);
386 }
387 else
388 {
389 encoded_submsg->_buffer = NULL;
390 }
391 }
392
393 /**
394 * Crypto key exchange
395 */
396 static DDS_Security_boolean create_local_participant_crypto_tokens(
397 dds_security_crypto_key_exchange *instance,
398 DDS_Security_ParticipantCryptoTokenSeq *local_participant_crypto_tokens,
399 const DDS_Security_ParticipantCryptoHandle local_participant_crypto,
400 const DDS_Security_ParticipantCryptoHandle remote_participant_crypto,
401 DDS_Security_SecurityException *ex)
402 {
403 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
404 switch (impl->parent->mode)
405 {
406 case PLUGIN_MODE_WRAPPED:
407 case PLUGIN_MODE_TOKEN_LOG:
408 case PLUGIN_MODE_PLAIN_DATA:
409 {
410 DDS_Security_boolean ret = impl->instance->create_local_participant_crypto_tokens (impl->instance, local_participant_crypto_tokens,
411 local_participant_crypto, remote_participant_crypto, ex);
412 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
413 store_tokens (impl, LOCAL_PARTICIPANT_TOKENS, local_participant_crypto, remote_participant_crypto, local_participant_crypto_tokens);
414 return ret;
415 }
416 default:
417 return true;
418 }
419 }
420
421 static DDS_Security_boolean set_remote_participant_crypto_tokens(
422 dds_security_crypto_key_exchange *instance,
423 const DDS_Security_ParticipantCryptoHandle local_participant_crypto,
424 const DDS_Security_ParticipantCryptoHandle remote_participant_crypto,
425 const DDS_Security_ParticipantCryptoTokenSeq *remote_participant_tokens,
426 DDS_Security_SecurityException *ex)
427 {
428 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
429 switch (impl->parent->mode)
430 {
431 case PLUGIN_MODE_WRAPPED:
432 case PLUGIN_MODE_TOKEN_LOG:
433 case PLUGIN_MODE_PLAIN_DATA:
434 {
435 DDS_Security_boolean ret = impl->instance->set_remote_participant_crypto_tokens (impl->instance, check_handle (local_participant_crypto),
436 check_handle (remote_participant_crypto), remote_participant_tokens, ex);
437 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
438 store_tokens (impl, REMOTE_PARTICIPANT_TOKENS, local_participant_crypto, remote_participant_crypto, remote_participant_tokens);
439 return ret;
440 }
441 default:
442 return true;
443 }
444 }
445
446 static DDS_Security_boolean create_local_datawriter_crypto_tokens(
447 dds_security_crypto_key_exchange *instance,
448 DDS_Security_DatawriterCryptoTokenSeq *local_datawriter_crypto_tokens,
449 const DDS_Security_DatawriterCryptoHandle local_datawriter_crypto,
450 const DDS_Security_DatareaderCryptoHandle remote_datareader_crypto,
451 DDS_Security_SecurityException *ex)
452 {
453 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
454 switch (impl->parent->mode)
455 {
456 case PLUGIN_MODE_WRAPPED:
457 case PLUGIN_MODE_TOKEN_LOG:
458 case PLUGIN_MODE_PLAIN_DATA:
459 {
460 DDS_Security_boolean ret = impl->instance->create_local_datawriter_crypto_tokens (impl->instance, local_datawriter_crypto_tokens,
461 check_handle (local_datawriter_crypto), check_handle (remote_datareader_crypto), ex);
462 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
463 store_tokens (impl, LOCAL_WRITER_TOKENS, local_datawriter_crypto, remote_datareader_crypto, local_datawriter_crypto_tokens);
464 return ret;
465 }
466 default:
467 return true;
468 }
469 }
470
471 static DDS_Security_boolean set_remote_datawriter_crypto_tokens(
472 dds_security_crypto_key_exchange *instance,
473 const DDS_Security_DatareaderCryptoHandle local_datareader_crypto,
474 const DDS_Security_DatawriterCryptoHandle remote_datawriter_crypto,
475 const DDS_Security_DatawriterCryptoTokenSeq *remote_datawriter_tokens,
476 DDS_Security_SecurityException *ex)
477 {
478 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
479 switch (impl->parent->mode)
480 {
481 case PLUGIN_MODE_WRAPPED:
482 case PLUGIN_MODE_TOKEN_LOG:
483 case PLUGIN_MODE_PLAIN_DATA:
484 {
485 DDS_Security_boolean ret = impl->instance->set_remote_datawriter_crypto_tokens (impl->instance, check_handle (local_datareader_crypto),
486 check_handle (remote_datawriter_crypto), remote_datawriter_tokens, ex);
487 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
488 store_tokens (impl, REMOTE_WRITER_TOKENS, local_datareader_crypto, remote_datawriter_crypto, remote_datawriter_tokens);
489 return ret;
490 }
491 default:
492 return true;
493 }
494 }
495
496 static DDS_Security_boolean create_local_datareader_crypto_tokens(
497 dds_security_crypto_key_exchange *instance,
498 DDS_Security_DatareaderCryptoTokenSeq *local_datareader_cryto_tokens,
499 const DDS_Security_DatareaderCryptoHandle local_datareader_crypto,
500 const DDS_Security_DatawriterCryptoHandle remote_datawriter_crypto,
501 DDS_Security_SecurityException *ex)
502 {
503 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
504 switch (impl->parent->mode)
505 {
506 case PLUGIN_MODE_WRAPPED:
507 case PLUGIN_MODE_TOKEN_LOG:
508 case PLUGIN_MODE_PLAIN_DATA:
509 {
510 DDS_Security_boolean ret = impl->instance->create_local_datareader_crypto_tokens (impl->instance, local_datareader_cryto_tokens,
511 check_handle (local_datareader_crypto), check_handle (remote_datawriter_crypto), ex);
512 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
513 store_tokens (impl, LOCAL_READER_TOKENS, local_datareader_crypto, remote_datawriter_crypto, local_datareader_cryto_tokens);
514 return ret;
515 }
516 default:
517 return true;
518 }
519 }
520
521 static DDS_Security_boolean set_remote_datareader_crypto_tokens(
522 dds_security_crypto_key_exchange *instance,
523 const DDS_Security_DatawriterCryptoHandle local_datawriter_crypto,
524 const DDS_Security_DatareaderCryptoHandle remote_datareader_crypto,
525 const DDS_Security_DatareaderCryptoTokenSeq *remote_datareader_tokens,
526 DDS_Security_SecurityException *ex)
527 {
528 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
529 switch (impl->parent->mode)
530 {
531 case PLUGIN_MODE_WRAPPED:
532 case PLUGIN_MODE_TOKEN_LOG:
533 case PLUGIN_MODE_PLAIN_DATA:
534 {
535 DDS_Security_boolean ret = impl->instance->set_remote_datareader_crypto_tokens (impl->instance, check_handle (local_datawriter_crypto),
536 check_handle (remote_datareader_crypto), remote_datareader_tokens, ex);
537 if (ret && impl->parent->mode == PLUGIN_MODE_TOKEN_LOG)
538 store_tokens (impl, REMOTE_READER_TOKENS, local_datawriter_crypto, remote_datareader_crypto, remote_datareader_tokens);
539 return ret;
540 }
541 default:
542 return true;
543 }
544 }
545
546 static DDS_Security_boolean return_crypto_tokens(
547 dds_security_crypto_key_exchange *instance,
548 DDS_Security_CryptoTokenSeq *crypto_tokens,
549 DDS_Security_SecurityException *ex)
550 {
551 struct dds_security_crypto_key_exchange_impl *impl = (struct dds_security_crypto_key_exchange_impl *)instance;
552 switch (impl->parent->mode)
553 {
554 case PLUGIN_MODE_WRAPPED:
555 case PLUGIN_MODE_TOKEN_LOG:
556 case PLUGIN_MODE_PLAIN_DATA:
557 return impl->instance->return_crypto_tokens (impl->instance, crypto_tokens, ex);
558 default:
559 return true;
560 }
561 }
562
563 /**
564 * Crypto key factory
565 */
566 static DDS_Security_ParticipantCryptoHandle register_local_participant(
567 dds_security_crypto_key_factory *instance,
568 const DDS_Security_IdentityHandle participant_identity,
569 const DDS_Security_PermissionsHandle participant_permissions,
570 const DDS_Security_PropertySeq *participant_properties,
571 const DDS_Security_ParticipantSecurityAttributes *participant_security_attributes,
572 DDS_Security_SecurityException *ex)
573 {
574 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
575 switch (impl->parent->mode)
576 {
577 case PLUGIN_MODE_WRAPPED:
578 case PLUGIN_MODE_TOKEN_LOG:
579 case PLUGIN_MODE_PLAIN_DATA:
580 return check_handle (impl->instance->register_local_participant (impl->instance, check_handle (participant_identity),
581 check_handle (participant_permissions), participant_properties, participant_security_attributes, ex));
582 default:
583 return ++g_local_participant_handle;
584 }
585 }
586
587 static DDS_Security_ParticipantCryptoHandle register_matched_remote_participant(
588 dds_security_crypto_key_factory *instance,
589 const DDS_Security_ParticipantCryptoHandle local_participant_crypto_handle,
590 const DDS_Security_IdentityHandle remote_participant_identity,
591 const DDS_Security_PermissionsHandle remote_participant_permissions,
592 const DDS_Security_SharedSecretHandle shared_secret,
593 DDS_Security_SecurityException *ex)
594 {
595 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
596 switch (impl->parent->mode)
597 {
598 case PLUGIN_MODE_WRAPPED:
599 case PLUGIN_MODE_TOKEN_LOG:
600 case PLUGIN_MODE_PLAIN_DATA:
601 return check_handle (impl->instance->register_matched_remote_participant (impl->instance, local_participant_crypto_handle,
602 remote_participant_identity, remote_participant_permissions, shared_secret, ex));
603 default:
604 return 0;
605 }
606 }
607
608 static DDS_Security_DatawriterCryptoHandle register_local_datawriter(
609 dds_security_crypto_key_factory *instance,
610 const DDS_Security_ParticipantCryptoHandle participant_crypto,
611 const DDS_Security_PropertySeq *datawriter_properties,
612 const DDS_Security_EndpointSecurityAttributes *datawriter_security_attributes,
613 DDS_Security_SecurityException *ex)
614 {
615 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
616 switch (impl->parent->mode)
617 {
618 case PLUGIN_MODE_WRAPPED:
619 case PLUGIN_MODE_TOKEN_LOG:
620 case PLUGIN_MODE_PLAIN_DATA:
621 return check_handle (impl->instance->register_local_datawriter (impl->instance, check_handle (participant_crypto),
622 datawriter_properties, datawriter_security_attributes, ex));
623 default:
624 return 0;
625 }
626 }
627
628 static DDS_Security_DatareaderCryptoHandle register_matched_remote_datareader(
629 dds_security_crypto_key_factory *instance,
630 const DDS_Security_DatawriterCryptoHandle local_datawriter_crypto_handle,
631 const DDS_Security_ParticipantCryptoHandle remote_participant_crypto,
632 const DDS_Security_SharedSecretHandle shared_secret,
633 const DDS_Security_boolean relay_only,
634 DDS_Security_SecurityException *ex)
635 {
636 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
637 switch (impl->parent->mode)
638 {
639 case PLUGIN_MODE_WRAPPED:
640 case PLUGIN_MODE_TOKEN_LOG:
641 case PLUGIN_MODE_PLAIN_DATA:
642 return check_handle (impl->instance->register_matched_remote_datareader (impl->instance, check_handle (local_datawriter_crypto_handle),
643 check_handle (remote_participant_crypto), check_handle (shared_secret), relay_only, ex));
644 default:
645 return 0;
646 }
647 }
648
649 static DDS_Security_DatareaderCryptoHandle register_local_datareader(
650 dds_security_crypto_key_factory *instance,
651 const DDS_Security_ParticipantCryptoHandle participant_crypto,
652 const DDS_Security_PropertySeq *datareader_properties,
653 const DDS_Security_EndpointSecurityAttributes *datareader_security_attributes,
654 DDS_Security_SecurityException *ex)
655 {
656 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
657 switch (impl->parent->mode)
658 {
659 case PLUGIN_MODE_WRAPPED:
660 case PLUGIN_MODE_TOKEN_LOG:
661 case PLUGIN_MODE_PLAIN_DATA:
662 return check_handle (impl->instance->register_local_datareader (impl->instance, check_handle (participant_crypto),
663 datareader_properties, datareader_security_attributes, ex));
664 default:
665 return 0;
666 }
667 }
668
669 static DDS_Security_DatawriterCryptoHandle register_matched_remote_datawriter(
670 dds_security_crypto_key_factory *instance,
671 const DDS_Security_DatareaderCryptoHandle local_datareader_crypto_handle,
672 const DDS_Security_ParticipantCryptoHandle remote_participant_crypt,
673 const DDS_Security_SharedSecretHandle shared_secret,
674 DDS_Security_SecurityException *ex)
675 {
676 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
677 switch (impl->parent->mode)
678 {
679 case PLUGIN_MODE_WRAPPED:
680 case PLUGIN_MODE_TOKEN_LOG:
681 case PLUGIN_MODE_PLAIN_DATA:
682 return check_handle (impl->instance->register_matched_remote_datawriter (impl->instance, check_handle (local_datareader_crypto_handle),
683 check_handle (remote_participant_crypt), shared_secret, ex));
684 default:
685 return 1;
686 }
687 }
688
689 static DDS_Security_boolean unregister_participant(
690 dds_security_crypto_key_factory *instance,
691 const DDS_Security_ParticipantCryptoHandle participant_crypto_handle,
692 DDS_Security_SecurityException *ex)
693 {
694 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
695 switch (impl->parent->mode)
696 {
697 case PLUGIN_MODE_WRAPPED:
698 case PLUGIN_MODE_TOKEN_LOG:
699 case PLUGIN_MODE_PLAIN_DATA:
700 return impl->instance->unregister_participant (impl->instance, check_handle (participant_crypto_handle), ex);
701 default:
702 return true;
703 }
704 }
705
706 static DDS_Security_boolean unregister_datawriter(
707 dds_security_crypto_key_factory *instance,
708 const DDS_Security_DatawriterCryptoHandle datawriter_crypto_handle,
709 DDS_Security_SecurityException *ex)
710 {
711 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
712 switch (impl->parent->mode)
713 {
714 case PLUGIN_MODE_WRAPPED:
715 case PLUGIN_MODE_TOKEN_LOG:
716 case PLUGIN_MODE_PLAIN_DATA:
717 return impl->instance->unregister_datawriter (impl->instance, check_handle (datawriter_crypto_handle), ex);
718 default:
719 return true;
720 }
721 }
722
723 static DDS_Security_boolean unregister_datareader(
724 dds_security_crypto_key_factory *instance,
725 const DDS_Security_DatareaderCryptoHandle datareader_crypto_handle,
726 DDS_Security_SecurityException *ex)
727 {
728 struct dds_security_crypto_key_factory_impl *impl = (struct dds_security_crypto_key_factory_impl *)instance;
729 switch (impl->parent->mode)
730 {
731 case PLUGIN_MODE_WRAPPED:
732 case PLUGIN_MODE_TOKEN_LOG:
733 case PLUGIN_MODE_PLAIN_DATA:
734 return impl->instance->unregister_datareader (impl->instance, check_handle (datareader_crypto_handle), ex);
735 default:
736 return true;
737 }
738 }
739
740 /**
741 * Crypto transform
742 */
743 static DDS_Security_boolean encode_serialized_payload(
744 dds_security_crypto_transform *instance,
745 DDS_Security_OctetSeq *encoded_buffer,
746 DDS_Security_OctetSeq *extra_inline_qos,
747 const DDS_Security_OctetSeq *plain_buffer,
748 const DDS_Security_DatawriterCryptoHandle sending_datawriter_crypto,
749 DDS_Security_SecurityException *ex)
750 {
751 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
752 switch (impl->parent->mode)
753 {
754 case PLUGIN_MODE_PLAIN_DATA:
755 if (impl->parent->force_plain_payload && impl->parent->force_plain_sender_handle == sending_datawriter_crypto)
756 {
757 copy_octetseq (encoded_buffer, plain_buffer);
758 return true;
759 }
760 /* fall through */
761 case PLUGIN_MODE_WRAPPED:
762 case PLUGIN_MODE_TOKEN_LOG:
763 {
764 if (!impl->instance->encode_serialized_payload (impl->instance, encoded_buffer,
765 extra_inline_qos, plain_buffer, check_handle (sending_datawriter_crypto), ex))
766 return false;
767 if (impl->parent->protection_kinds_set
768 && impl->parent->encrypted_secret
769 && (impl->parent->payload_protection_kind == DDS_SECURITY_BASICPROTECTION_KIND_ENCRYPT
770 || expect_encrypted_buffer (impl->parent->metadata_protection_kind)
771 || expect_encrypted_buffer (impl->parent->rtps_protection_kind)))
772 {
773 if (find_buffer_match (encoded_buffer->_buffer, encoded_buffer->_length, (const unsigned char *) impl->parent->encrypted_secret, strlen (impl->parent->encrypted_secret)) != NULL)
774 {
775 ex->code = 1;
776 ex->message = ddsrt_strdup ("Expect encryption, but found secret in payload after encoding");
777 return false;
778 }
779 }
780 return !impl->parent->protection_kinds_set || check_buffers (encoded_buffer, plain_buffer, impl->parent->payload_protection_kind == DDS_SECURITY_BASICPROTECTION_KIND_ENCRYPT, ex);
781 }
782 default:
783 return true;
784 }
785 }
786
787 static DDS_Security_boolean check_buffer_submsg(
788 struct dds_security_crypto_transform_impl *impl,
789 DDS_Security_OctetSeq *encoded_rtps_submessage,
790 const DDS_Security_OctetSeq *plain_rtps_submessage,
791 DDS_Security_SecurityException *ex)
792 {
793 bool exp_enc = impl->parent->protection_kinds_set && (expect_encrypted_buffer (impl->parent->metadata_protection_kind) || expect_encrypted_buffer (impl->parent->rtps_protection_kind));
794 if (exp_enc && impl->parent->encrypted_secret && find_buffer_match (encoded_rtps_submessage->_buffer, encoded_rtps_submessage->_length, (const unsigned char *) impl->parent->encrypted_secret, strlen (impl->parent->encrypted_secret)) != NULL)
795 {
796 ex->code = 1;
797 ex->message = ddsrt_strdup ("Expect encryption, but found secret in submessage after encoding");
798 return false;
799 }
800
801 return impl->parent->protection_kinds_set && expect_encrypted_buffer (impl->parent->metadata_protection_kind) ? check_buffers (encoded_rtps_submessage, plain_rtps_submessage, true, ex) : true;
802 }
803
804 static DDS_Security_boolean encode_datawriter_submessage(
805 dds_security_crypto_transform *instance,
806 DDS_Security_OctetSeq *encoded_rtps_submessage,
807 const DDS_Security_OctetSeq *plain_rtps_submessage,
808 const DDS_Security_DatawriterCryptoHandle sending_datawriter_crypto,
809 const DDS_Security_DatareaderCryptoHandleSeq *receiving_datareader_crypto_list,
810 int32_t *receiving_datareader_crypto_list_index,
811 DDS_Security_SecurityException *ex)
812 {
813 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
814 switch (impl->parent->mode)
815 {
816 case PLUGIN_MODE_PLAIN_DATA:
817 if (impl->parent->force_plain_submsg && impl->parent->force_plain_sender_handle == sending_datawriter_crypto)
818 {
819 copy_octetseq (encoded_rtps_submessage, plain_rtps_submessage);
820 assert (receiving_datareader_crypto_list->_length <= INT32_MAX);
821 *receiving_datareader_crypto_list_index = (int32_t) receiving_datareader_crypto_list->_length;
822 return true;
823 }
824 /* fall through */
825 case PLUGIN_MODE_WRAPPED:
826 log_encode_decode (impl->parent, ENCODE_DATAWRITER_SUBMESSAGE, sending_datawriter_crypto);
827 /* fall-through */
828 case PLUGIN_MODE_TOKEN_LOG:
829 if (!impl->instance->encode_datawriter_submessage (impl->instance, encoded_rtps_submessage,
830 plain_rtps_submessage, check_handle (sending_datawriter_crypto), receiving_datareader_crypto_list, receiving_datareader_crypto_list_index, ex))
831 return false;
832 if (!check_buffer_submsg(impl, encoded_rtps_submessage, plain_rtps_submessage, ex))
833 return false;
834
835 if (impl->parent->disc_protection_kinds_set && expect_encrypted_buffer (impl->parent->disc_protection_kind))
836 {
837 if (impl->parent->pp_secret && find_buffer_match (plain_rtps_submessage->_buffer, plain_rtps_submessage->_length, (const unsigned char *) impl->parent->pp_secret, strlen (impl->parent->pp_secret)) != NULL)
838 {
839 ex->code = 1;
840 ex->message = ddsrt_strdup ("Expect discovery encryption, but found participant userdata secret in submessage after encoding");
841 return false;
842 }
843 if (impl->parent->groupdata_secret && find_buffer_match (plain_rtps_submessage->_buffer, plain_rtps_submessage->_length, (const unsigned char *) impl->parent->groupdata_secret, strlen (impl->parent->groupdata_secret)) != NULL)
844 {
845 ex->code = 1;
846 ex->message = ddsrt_strdup ("Expect discovery encryption, but found publisher/subscriber groupdata secret in submessage after encoding");
847 return false;
848 }
849 if (impl->parent->ep_secret && find_buffer_match (plain_rtps_submessage->_buffer, plain_rtps_submessage->_length, (const unsigned char *) impl->parent->ep_secret, strlen (impl->parent->ep_secret)) != NULL)
850 {
851 ex->code = 1;
852 ex->message = ddsrt_strdup ("Expect discovery encryption, but found reader/writer userdata secret in submessage after encoding");
853 return false;
854 }
855 }
856 return true;
857
858
859 default:
860 return true;
861 }
862 }
863
864 static DDS_Security_boolean encode_datareader_submessage(
865 dds_security_crypto_transform *instance,
866 DDS_Security_OctetSeq *encoded_rtps_submessage,
867 const DDS_Security_OctetSeq *plain_rtps_submessage,
868 const DDS_Security_DatareaderCryptoHandle sending_datareader_crypto,
869 const DDS_Security_DatawriterCryptoHandleSeq *receiving_datawriter_crypto_list,
870 DDS_Security_SecurityException *ex)
871 {
872 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
873 switch (impl->parent->mode)
874 {
875 case PLUGIN_MODE_PLAIN_DATA:
876 if (impl->parent->force_plain_submsg && impl->parent->force_plain_sender_handle == sending_datareader_crypto)
877 {
878 copy_octetseq (encoded_rtps_submessage, plain_rtps_submessage);
879 return true;
880 }
881 /* fall through */
882 case PLUGIN_MODE_WRAPPED:
883 log_encode_decode (impl->parent, ENCODE_DATAREADER_SUBMESSAGE, sending_datareader_crypto);
884 /* fall-through */
885 case PLUGIN_MODE_TOKEN_LOG:
886 if (!impl->instance->encode_datareader_submessage (impl->instance, encoded_rtps_submessage,
887 plain_rtps_submessage, check_handle (sending_datareader_crypto), receiving_datawriter_crypto_list, ex))
888 return false;
889 return check_buffer_submsg(impl, encoded_rtps_submessage, plain_rtps_submessage, ex);
890 default:
891 return true;
892 }
893 }
894
895 static DDS_Security_boolean encode_rtps_message(
896 dds_security_crypto_transform *instance,
897 DDS_Security_OctetSeq *encoded_rtps_message,
898 const DDS_Security_OctetSeq *plain_rtps_message,
899 const DDS_Security_ParticipantCryptoHandle sending_participant_crypto,
900 const DDS_Security_ParticipantCryptoHandleSeq *receiving_participant_crypto_list,
901 int32_t *receiving_participant_crypto_list_index,
902 DDS_Security_SecurityException *ex)
903 {
904 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
905 switch (impl->parent->mode)
906 {
907 case PLUGIN_MODE_PLAIN_DATA:
908 if (impl->parent->force_plain_rtps)
909 {
910 copy_octetseq (encoded_rtps_message, plain_rtps_message);
911 assert (receiving_participant_crypto_list->_length <= INT32_MAX);
912 *receiving_participant_crypto_list_index = (int32_t) receiving_participant_crypto_list->_length;
913 return true;
914 }
915 /* fall through */
916 case PLUGIN_MODE_WRAPPED:
917 case PLUGIN_MODE_TOKEN_LOG:
918 if (!impl->instance->encode_rtps_message (impl->instance, encoded_rtps_message,
919 plain_rtps_message, check_handle (sending_participant_crypto), receiving_participant_crypto_list, receiving_participant_crypto_list_index, ex))
920 return false;
921 if (impl->parent->protection_kinds_set
922 && impl->parent->encrypted_secret
923 && expect_encrypted_buffer (impl->parent->rtps_protection_kind)
924 && find_buffer_match (encoded_rtps_message->_buffer, encoded_rtps_message->_length, (const unsigned char *) impl->parent->encrypted_secret, strlen (impl->parent->encrypted_secret)) != NULL)
925 {
926 ex->code = 1;
927 ex->message = ddsrt_strdup ("Expect encryption, but found secret in RTPS message after encoding");
928 return false;
929 }
930 return impl->parent->protection_kinds_set && expect_encrypted_buffer (impl->parent->rtps_protection_kind) ?
931 check_buffers (encoded_rtps_message, plain_rtps_message, true, ex) : true;
932 default:
933 return true;
934 }
935 }
936
937 static DDS_Security_boolean decode_rtps_message(
938 dds_security_crypto_transform *instance,
939 DDS_Security_OctetSeq *plain_buffer,
940 const DDS_Security_OctetSeq *encoded_buffer,
941 const DDS_Security_ParticipantCryptoHandle receiving_participant_crypto,
942 const DDS_Security_ParticipantCryptoHandle sending_participant_crypto,
943 DDS_Security_SecurityException *ex)
944 {
945 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
946 switch (impl->parent->mode)
947 {
948 case PLUGIN_MODE_WRAPPED:
949 case PLUGIN_MODE_TOKEN_LOG:
950 case PLUGIN_MODE_PLAIN_DATA:
951 return impl->instance->decode_rtps_message (impl->instance, plain_buffer, encoded_buffer,
952 check_handle (receiving_participant_crypto), check_handle (sending_participant_crypto), ex);
953 default:
954 return true;
955 }
956 }
957
958 static DDS_Security_boolean preprocess_secure_submsg(
959 dds_security_crypto_transform *instance,
960 DDS_Security_DatawriterCryptoHandle *datawriter_crypto,
961 DDS_Security_DatareaderCryptoHandle *datareader_crypto,
962 DDS_Security_SecureSubmessageCategory_t *secure_submessage_category,
963 const DDS_Security_OctetSeq *encoded_rtps_submessage,
964 const DDS_Security_ParticipantCryptoHandle receiving_participant_crypto,
965 const DDS_Security_ParticipantCryptoHandle sending_participant_crypto,
966 DDS_Security_SecurityException *ex)
967 {
968 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
969 switch (impl->parent->mode)
970 {
971 case PLUGIN_MODE_WRAPPED:
972 case PLUGIN_MODE_TOKEN_LOG:
973 case PLUGIN_MODE_PLAIN_DATA:
974 return impl->instance->preprocess_secure_submsg (impl->instance, datawriter_crypto, datareader_crypto,
975 secure_submessage_category, encoded_rtps_submessage, check_handle (receiving_participant_crypto), check_handle (sending_participant_crypto), ex);
976 default:
977 return true;
978 }
979 }
980
981 static DDS_Security_boolean decode_datawriter_submessage(
982 dds_security_crypto_transform *instance,
983 DDS_Security_OctetSeq *plain_rtps_submessage,
984 const DDS_Security_OctetSeq *encoded_rtps_submessage,
985 const DDS_Security_DatareaderCryptoHandle receiving_datareader_crypto,
986 const DDS_Security_DatawriterCryptoHandle sending_datawriter_crypto,
987 DDS_Security_SecurityException *ex)
988 {
989 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
990 switch (impl->parent->mode)
991 {
992 case PLUGIN_MODE_PLAIN_DATA:
993 case PLUGIN_MODE_WRAPPED:
994 log_encode_decode (impl->parent, DECODE_DATAWRITER_SUBMESSAGE, receiving_datareader_crypto);
995 /* fall-through */
996 case PLUGIN_MODE_TOKEN_LOG:
997 return impl->instance->decode_datawriter_submessage (impl->instance, plain_rtps_submessage,
998 encoded_rtps_submessage, check_handle (receiving_datareader_crypto), check_handle (sending_datawriter_crypto), ex);
999 default:
1000 return true;
1001 }
1002 }
1003
1004 static DDS_Security_boolean decode_datareader_submessage(
1005 dds_security_crypto_transform *instance,
1006 DDS_Security_OctetSeq *plain_rtps_submessage,
1007 const DDS_Security_OctetSeq *encoded_rtps_submessage,
1008 const DDS_Security_DatawriterCryptoHandle receiving_datawriter_crypto,
1009 const DDS_Security_DatareaderCryptoHandle sending_datareader_crypto,
1010 DDS_Security_SecurityException *ex)
1011 {
1012 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
1013 switch (impl->parent->mode)
1014 {
1015 case PLUGIN_MODE_PLAIN_DATA:
1016 case PLUGIN_MODE_WRAPPED:
1017 log_encode_decode (impl->parent, DECODE_DATAREADER_SUBMESSAGE, receiving_datawriter_crypto);
1018 /* fall-through */
1019 case PLUGIN_MODE_TOKEN_LOG:
1020 return impl->instance->decode_datareader_submessage (impl->instance, plain_rtps_submessage,
1021 encoded_rtps_submessage, check_handle (receiving_datawriter_crypto), check_handle (sending_datareader_crypto), ex);
1022 default:
1023 return true;
1024 }
1025 }
1026
1027 static DDS_Security_boolean decode_serialized_payload(
1028 dds_security_crypto_transform *instance,
1029 DDS_Security_OctetSeq *plain_buffer,
1030 const DDS_Security_OctetSeq *encoded_buffer,
1031 const DDS_Security_OctetSeq *inline_qos,
1032 const DDS_Security_DatareaderCryptoHandle receiving_datareader_crypto,
1033 const DDS_Security_DatawriterCryptoHandle sending_datawriter_crypto,
1034 DDS_Security_SecurityException *ex)
1035 {
1036 struct dds_security_crypto_transform_impl *impl = (struct dds_security_crypto_transform_impl *)instance;
1037 switch (impl->parent->mode)
1038 {
1039 case PLUGIN_MODE_WRAPPED:
1040 case PLUGIN_MODE_TOKEN_LOG:
1041 case PLUGIN_MODE_PLAIN_DATA:
1042 return impl->instance->decode_serialized_payload(impl->instance, plain_buffer, encoded_buffer,
1043 inline_qos, check_handle (receiving_datareader_crypto), check_handle (sending_datawriter_crypto), ex);
1044 default:
1045 return true;
1046 }
1047 }
1048
1049 /**
1050 * Init and finalize functions
1051 */
1052 static struct dds_security_cryptography_impl * init_test_cryptography_common(const char *argument, bool wrapped, struct ddsi_domaingv *gv)
1053 {
1054 struct dds_security_cryptography_impl *impl = (struct dds_security_cryptography_impl*) ddsrt_malloc(sizeof(*impl));
1055 memset(impl, 0, sizeof(*impl));
1056
1057 if (wrapped)
1058 {
1059 if (init_crypto(argument, (void **)&impl->instance, gv) != DDS_SECURITY_SUCCESS)
1060 return NULL;
1061
1062 impl->transform_wrap.instance = impl->instance->crypto_transform;
1063 impl->factory_wrap.instance = impl->instance->crypto_key_factory;
1064 impl->exchange_wrap.instance = impl->instance->crypto_key_exchange;
1065 }
1066
1067 impl->base.crypto_transform = (dds_security_crypto_transform *)&impl->transform_wrap;
1068 impl->base.crypto_key_factory = (dds_security_crypto_key_factory *)&impl->factory_wrap;
1069 impl->base.crypto_key_exchange = (dds_security_crypto_key_exchange *)&impl->exchange_wrap;
1070
1071 impl->transform_wrap.parent = impl;
1072 impl->factory_wrap.parent = impl;
1073 impl->exchange_wrap.parent = impl;
1074
1075 impl->factory_wrap.base.register_local_participant = &register_local_participant;
1076 impl->factory_wrap.base.register_matched_remote_participant = &register_matched_remote_participant;
1077 impl->factory_wrap.base.register_local_datawriter = &register_local_datawriter;
1078 impl->factory_wrap.base.register_matched_remote_datareader = &register_matched_remote_datareader;
1079 impl->factory_wrap.base.register_local_datareader = &register_local_datareader;
1080 impl->factory_wrap.base.register_matched_remote_datawriter = &register_matched_remote_datawriter;
1081 impl->factory_wrap.base.unregister_participant = &unregister_participant;
1082 impl->factory_wrap.base.unregister_datawriter = &unregister_datawriter;
1083 impl->factory_wrap.base.unregister_datareader = &unregister_datareader;
1084
1085 impl->exchange_wrap.base.create_local_participant_crypto_tokens = &create_local_participant_crypto_tokens;
1086 impl->exchange_wrap.base.set_remote_participant_crypto_tokens = &set_remote_participant_crypto_tokens;
1087 impl->exchange_wrap.base.create_local_datawriter_crypto_tokens = &create_local_datawriter_crypto_tokens;
1088 impl->exchange_wrap.base.set_remote_datawriter_crypto_tokens = &set_remote_datawriter_crypto_tokens;
1089 impl->exchange_wrap.base.create_local_datareader_crypto_tokens = &create_local_datareader_crypto_tokens;
1090 impl->exchange_wrap.base.set_remote_datareader_crypto_tokens = &set_remote_datareader_crypto_tokens;
1091 impl->exchange_wrap.base.return_crypto_tokens = &return_crypto_tokens;
1092
1093 impl->transform_wrap.base.encode_datawriter_submessage = &encode_datawriter_submessage;
1094 impl->transform_wrap.base.encode_datareader_submessage = &encode_datareader_submessage;
1095 impl->transform_wrap.base.encode_rtps_message = &encode_rtps_message;
1096 impl->transform_wrap.base.decode_rtps_message = &decode_rtps_message;
1097 impl->transform_wrap.base.preprocess_secure_submsg = &preprocess_secure_submsg;
1098 impl->transform_wrap.base.decode_datawriter_submessage = &decode_datawriter_submessage;
1099 impl->transform_wrap.base.decode_datareader_submessage = &decode_datareader_submessage;
1100 impl->transform_wrap.base.decode_serialized_payload = &decode_serialized_payload;
1101 impl->transform_wrap.base.encode_serialized_payload = &encode_serialized_payload;
1102
1103 return impl;
1104 }
1105
1106 static int finalize_test_cryptography_common(struct dds_security_cryptography_impl * impl, bool wrapped)
1107 {
1108 int ret;
1109 if (wrapped && (ret = finalize_crypto(impl->instance)) != DDS_SECURITY_SUCCESS)
1110 return ret;
1111 ddsrt_free(impl);
1112 return DDS_SECURITY_SUCCESS;
1113 }
1114
1115 int init_test_cryptography_all_ok(const char *argument, void **context, struct ddsi_domaingv *gv)
1116 {
1117 struct dds_security_cryptography_impl *impl = init_test_cryptography_common(argument, false, gv);
1118 if (!impl)
1119 return DDS_SECURITY_FAILED;
1120 impl->mode = PLUGIN_MODE_ALL_OK;
1121 *context = impl;
1122 return DDS_SECURITY_SUCCESS;
1123 }
1124
1125 int finalize_test_cryptography_all_ok(void *context)
1126 {
1127 struct dds_security_cryptography_impl* impl = (struct dds_security_cryptography_impl*) context;
1128 assert(impl->mode == PLUGIN_MODE_ALL_OK);
1129 return finalize_test_cryptography_common(impl, false);
1130 }
1131
1132 int init_test_cryptography_missing_func(const char *argument, void **context, struct ddsi_domaingv *gv)
1133 {
1134 struct dds_security_cryptography_impl *impl = init_test_cryptography_common(argument, false, gv);
1135 if (!impl)
1136 return DDS_SECURITY_FAILED;
1137 impl->base.crypto_key_exchange->set_remote_participant_crypto_tokens = NULL;
1138 impl->mode = PLUGIN_MODE_MISSING_FUNC;
1139 *context = impl;
1140 return DDS_SECURITY_SUCCESS;
1141 }
1142
1143 int finalize_test_cryptography_missing_func(void *context)
1144 {
1145 struct dds_security_cryptography_impl* impl = (struct dds_security_cryptography_impl*) context;
1146 assert(impl->mode == PLUGIN_MODE_MISSING_FUNC);
1147 return finalize_test_cryptography_common(impl, false);
1148 }
1149
1150 static void init_encode_decode_log(struct dds_security_cryptography_impl *impl)
1151 {
1152 ddsrt_mutex_init (&impl->encode_decode_log_lock);
1153 ddsrt_circlist_init (&impl->encode_decode_log);
1154 }
1155
1156 static void fini_encode_decode_log(struct dds_security_cryptography_impl *impl)
1157 {
1158 ddsrt_mutex_lock (&impl->encode_decode_log_lock);
1159 while (!ddsrt_circlist_isempty (&impl->encode_decode_log))
1160 {
1161 struct ddsrt_circlist_elem *list_elem = ddsrt_circlist_oldest (&impl->encode_decode_log);
1162 ddsrt_circlist_remove (&impl->encode_decode_log, list_elem);
1163 ddsrt_free (list_elem);
1164 }
1165 ddsrt_mutex_unlock (&impl->encode_decode_log_lock);
1166 ddsrt_mutex_destroy (&impl->encode_decode_log_lock);
1167 }
1168
1169 int init_test_cryptography_wrapped(const char *argument, void **context, struct ddsi_domaingv *gv)
1170 {
1171 struct dds_security_cryptography_impl *impl = init_test_cryptography_common(argument, true, gv);
1172 if (!impl)
1173 return DDS_SECURITY_FAILED;
1174 impl->mode = PLUGIN_MODE_WRAPPED;
1175 init_encode_decode_log(impl);
1176 *context = impl;
1177 return DDS_SECURITY_SUCCESS;
1178 }
1179
1180 int finalize_test_cryptography_wrapped(void *context)
1181 {
1182 struct dds_security_cryptography_impl* impl = (struct dds_security_cryptography_impl*) context;
1183 assert(impl->mode == PLUGIN_MODE_WRAPPED);
1184 fini_encode_decode_log(impl);
1185 return finalize_test_cryptography_common(impl, true);
1186 }
1187
1188 static void init_print_lock(void)
1189 {
1190 ddsrt_mutex_init (&g_print_token_lock);
1191 }
1192
1193 int32_t init_test_cryptography_store_tokens(const char *argument, void **context, struct ddsi_domaingv *gv)
1194 {
1195 struct dds_security_cryptography_impl *impl = init_test_cryptography_common(argument, true, gv);
1196 if (!impl)
1197 return DDS_SECURITY_FAILED;
1198 impl->mode = PLUGIN_MODE_TOKEN_LOG;
1199 ddsrt_once (&lock_inited, &init_print_lock);
1200 ddsrt_mutex_init (&impl->token_data_lock);
1201 ddsrt_circlist_init (&impl->token_data_list);
1202 *context = impl;
1203 return DDS_SECURITY_SUCCESS;
1204 }
1205
1206 int32_t finalize_test_cryptography_store_tokens(void *context)
1207 {
1208 struct dds_security_cryptography_impl* impl = (struct dds_security_cryptography_impl*) context;
1209 assert(impl->mode == PLUGIN_MODE_TOKEN_LOG);
1210 ddsrt_mutex_lock (&impl->token_data_lock);
1211 while (!ddsrt_circlist_isempty (&impl->token_data_list))
1212 {
1213 struct ddsrt_circlist_elem *list_elem = ddsrt_circlist_oldest (&impl->token_data_list);
1214 ddsrt_circlist_remove (&impl->token_data_list, list_elem);
1215 ddsrt_free (list_elem);
1216 }
1217 ddsrt_mutex_unlock (&impl->token_data_lock);
1218 ddsrt_mutex_destroy (&impl->token_data_lock);
1219
1220 /* don't detroy g_print_token_lock as this will result in multiple
1221 calls to mutex_destroy for this lock in case of multiple domains */
1222
1223 return finalize_test_cryptography_common(impl, true);
1224 }
1225
1226 int init_test_cryptography_plain_data(const char *argument, void **context, struct ddsi_domaingv *gv)
1227 {
1228 struct dds_security_cryptography_impl *impl = init_test_cryptography_common(argument, true, gv);
1229 if (!impl)
1230 return DDS_SECURITY_FAILED;
1231 impl->mode = PLUGIN_MODE_PLAIN_DATA;
1232 init_encode_decode_log(impl);
1233 *context = impl;
1234 return DDS_SECURITY_SUCCESS;
1235 }
1236
1237 int finalize_test_cryptography_plain_data(void *context)
1238 {
1239 struct dds_security_cryptography_impl* impl = (struct dds_security_cryptography_impl*) context;
1240 assert(impl->mode == PLUGIN_MODE_PLAIN_DATA);
1241 fini_encode_decode_log(impl);
1242 return finalize_test_cryptography_common(impl, true);
1243 }
1244