rustdds 0.11.8

Native Rust DDS implementation with RTPS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
use ring::{digest, hmac};

use crate::{
  create_security_error_and_log,
  security::{
    access_control::{
      access_control_builtin::types::{
        BuiltinPluginEndpointSecurityAttributes, BuiltinPluginParticipantSecurityAttributes,
      },
      types::*,
    },
    cryptographic::cryptographic_builtin::*,
  },
};
use super::{aes_gcm_gmac::keygen, builtin_key::*, key_material::*};

impl CryptographicBuiltin {
  fn generate_crypto_handle(&mut self) -> CryptoHandle {
    self.crypto_handle_counter += 1;
    self.crypto_handle_counter
  }

  fn get_or_generate_matched_remote_endpoint_crypto_handle(
    &mut self,
    remote_participant_crypto_handle: ParticipantCryptoHandle,
    local_endpoint_crypto_handle: EndpointCryptoHandle,
  ) -> EndpointCryptoHandle {
    // If a corresponding handle exists, get and return
    if let Some(remote_endpoint_crypto_handle) = self
      .matched_remote_endpoint
      .get(&local_endpoint_crypto_handle)
      .and_then(|remote_participant_to_remote_endpoint| {
        remote_participant_to_remote_endpoint.get(&remote_participant_crypto_handle)
      })
    {
      *remote_endpoint_crypto_handle
    } else {
      // Otherwise generate a new handle
      let remote_endpoint_crypto_handle = self.generate_crypto_handle();
      // Associate it with the remote participant
      self.endpoint_to_participant.insert(
        remote_endpoint_crypto_handle,
        remote_participant_crypto_handle,
      );
      // Associate it with the local endpoint
      self
        .matched_local_endpoint
        .insert(remote_endpoint_crypto_handle, local_endpoint_crypto_handle);
      // Insert it to the HashMap corresponding to the local endpoint
      if let Some(remote_participant_to_remote_endpoint) = self
        .matched_remote_endpoint
        .get_mut(&local_endpoint_crypto_handle)
      {
        remote_participant_to_remote_endpoint.insert(
          remote_participant_crypto_handle,
          remote_endpoint_crypto_handle,
        );
      } else {
        // Create a new HashMap if one does not yet exist
        self.matched_remote_endpoint.insert(
          local_endpoint_crypto_handle,
          HashMap::from([(
            remote_participant_crypto_handle,
            remote_endpoint_crypto_handle,
          )]),
        );
      }
      // Return the generated handle
      remote_endpoint_crypto_handle
    }
  }

  fn is_volatile(properties: &[Property]) -> bool {
    properties
      .iter()
      .find(|property| {
        property
          .name
          .eq(VOLATILE_ENDPOINT_RECOGNITION_PROPERTY_NAME)
      })
      .is_some_and(|property| {
        property
          .value
          .eq(VOLATILE_WRITER_RECOGNITION_PROPERTY_VALUE)
          || property
            .value
            .eq(VOLATILE_READER_RECOGNITION_PROPERTY_VALUE)
      })
  }

  // 9.5.2.1.2
  fn derive_volatile_key_materials(
    SharedSecretHandle {
      shared_secret,
      challenge1,
      challenge2,
    }: &SharedSecretHandle,
    use_256_bit_key: bool,
  ) -> SecurityResult<KeyMaterial_AES_GCM_GMAC_seq> {
    let transformation_kind = if use_256_bit_key {
      BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES256_GCM
    } else {
      BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES128_GCM
    };

    let salt_cookie: &[u8] = b"keyexchange salt".as_ref(); // Not a typo
    let key_cookie: &[u8] = b"key exchange key".as_ref();

    let master_salt = Self::hash_shared_secret(
      [challenge1.as_ref(), salt_cookie, challenge2.as_ref()],
      shared_secret,
    );

    let master_sender_key = Self::hash_shared_secret(
      [challenge2.as_ref(), key_cookie, challenge1.as_ref()],
      shared_secret,
    );

    Ok(KeyMaterial_AES_GCM_GMAC_seq::Two(
      KeyMaterial_AES_GCM_GMAC {
        transformation_kind,
        master_salt,
        sender_key_id: CryptoTransformKeyId::ZERO, // 9.5.2.1.2
        master_sender_key,
        // Leave receiver-specific key empty
        receiver_specific_key_id: CryptoTransformKeyId::ZERO,
        master_receiver_specific_key: BuiltinKey::None,
      },
      // The volatile topic has no payload protection
      KeyMaterial_AES_GCM_GMAC {
        transformation_kind: BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_NONE,
        master_salt: BuiltinKey::None,
        sender_key_id: CryptoTransformKeyId::ZERO,
        master_sender_key: BuiltinKey::None,
        receiver_specific_key_id: CryptoTransformKeyId::ZERO,
        master_receiver_specific_key: BuiltinKey::None,
      },
    ))
  }

  // Creates a hmac key out of the challenges and cookie and uses it to hash the
  // secret according to 9.5.2.1.2
  fn hash_shared_secret(hmac_key_plain: [&[u8]; 3], shared_secret: &SharedSecret) -> BuiltinKey {
    let hmac_key = hmac::Key::new(
      hmac::HMAC_SHA256,
      digest::digest(&digest::SHA256, hmac_key_plain.concat().as_ref()).as_ref(),
    );
    let hashed_secret = hmac::sign(&hmac_key, shared_secret.as_ref());
    // from_bytes handles truncation. HMAC_SHA256 gives 256 bit output so this never
    // fails.
    BuiltinKey::from_bytes(KeyLength::AES256, hashed_secret.as_ref()).unwrap()
  }

  fn use_256_bit_key(properties: &[Property]) -> bool {
    properties
      .iter()
      .find(|property| property.name.eq("dds.sec.crypto.keysize"))
      .map_or(true, |property| !property.value.eq("128"))
  }

  fn transformation_kind(
    is_protected: bool,
    is_encrypted: bool,
    use_256_bit_key: bool,
  ) -> BuiltinCryptoTransformationKind {
    match (is_protected, is_encrypted, use_256_bit_key) {
      (false, _, _) => BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_NONE,
      (true, false, false) => {
        BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES128_GMAC
      }
      (true, false, true) => {
        BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES256_GMAC
      }
      (true, true, false) => BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES128_GCM,
      (true, true, true) => BuiltinCryptoTransformationKind::CRYPTO_TRANSFORMATION_KIND_AES256_GCM,
    }
  }

  fn generate_key_id(&mut self) -> CryptoTransformKeyId {
    loop {
      let candidate = CryptoTransformKeyId::random();
      if !self.used_local_key_ids.contains(&candidate) {
        return candidate;
      }
      // Else there was a collision, retry
    }
  }

  fn generate_key_material(
    &mut self,
    transformation_kind: BuiltinCryptoTransformationKind,
  ) -> KeyMaterial_AES_GCM_GMAC {
    let key_length = KeyLength::from(transformation_kind);
    KeyMaterial_AES_GCM_GMAC {
      transformation_kind,

      master_salt: BuiltinKey::generate_random(key_length),

      sender_key_id: self.generate_key_id(),
      master_sender_key: keygen(key_length),
      // Leave receiver-specific key empty initially
      receiver_specific_key_id: CryptoTransformKeyId::ZERO,
      master_receiver_specific_key: BuiltinKey::None,
    }
  }

  fn generate_receiver_specific_key(
    &mut self,
    key_materials: KeyMaterial_AES_GCM_GMAC_seq,
    origin_authentication: bool,
  ) -> KeyMaterial_AES_GCM_GMAC_seq {
    if origin_authentication {
      let master_receiver_specific_key =
        keygen(key_materials.key_material().transformation_kind.into());
      key_materials
        .add_master_receiver_specific_key(self.generate_key_id(), master_receiver_specific_key)
    } else {
      key_materials.add_master_receiver_specific_key(CryptoTransformKeyId::ZERO, BuiltinKey::None)
    }
  }

  fn unregister_endpoint(&mut self, endpoint_info: EndpointInfo) {
    let endpoint_crypto_handle = endpoint_info.crypto_handle;
    self
      .common_encode_key_materials
      .remove(&endpoint_crypto_handle);
    self
      .receiver_specific_encode_key_materials
      .remove(&endpoint_crypto_handle);
    self.decode_key_materials.remove(&endpoint_crypto_handle);
    self
      .endpoint_encrypt_options
      .remove(&endpoint_crypto_handle);
    if let Some(participant_crypto_handle) =
      self.endpoint_to_participant.remove(&endpoint_crypto_handle)
    {
      if let Some(endpoint_info_set) = self
        .participant_to_endpoint_info
        .get_mut(&participant_crypto_handle)
      {
        endpoint_info_set.remove(&endpoint_info);
      }

      // If the endpoint is remote remove the association to the corresponding local
      // endpoint
      if let Some(matched_local_endpoint_crypto_handle) =
        self.matched_local_endpoint.remove(&endpoint_crypto_handle)
      {
        if let Some(remote_participant_to_remote_endpoint) = self
          .matched_remote_endpoint
          .get_mut(&matched_local_endpoint_crypto_handle)
        {
          remote_participant_to_remote_endpoint.remove(&participant_crypto_handle);
        }
      }
      // If the endpoint is local, unregister all associated remote entities as they serve no
      // purpose on their own. TODO: should we do this or just sever the association?
      else if let Some(remote_participant_to_remote_endpoint) =
        self.matched_remote_endpoint.remove(&endpoint_crypto_handle)
      {
        for remote_endpoint_crypto_handle in remote_participant_to_remote_endpoint.values() {
          self.unregister_endpoint(EndpointInfo {
            crypto_handle: *remote_endpoint_crypto_handle,
            kind: endpoint_info.kind.opposite(),
          });
        }
      }
    }
  }
}

/// Builtin CryptoKeyFactory implementation from section 9.5.3.1 of the Security
/// specification (v. 1.1)
impl CryptoKeyFactory for CryptographicBuiltin {
  fn register_local_participant(
    &mut self,
    _participant_identity: IdentityHandle,
    _participant_permissions: PermissionsHandle,
    participant_properties: &[Property],
    participant_security_attributes: ParticipantSecurityAttributes,
  ) -> SecurityResult<ParticipantCryptoHandle> {
    //TODO: this is only a mock implementation
    let plugin_participant_security_attributes =
      BuiltinPluginParticipantSecurityAttributes::try_from(
        participant_security_attributes.plugin_participant_attributes,
      )?;
    let crypto_handle = self.generate_crypto_handle();

    let key_material = self.generate_key_material(Self::transformation_kind(
      participant_security_attributes.is_rtps_protected,
      plugin_participant_security_attributes.is_rtps_encrypted,
      Self::use_256_bit_key(participant_properties),
    ));
    self
      .insert_common_encode_key_materials(
        crypto_handle,
        CommonEncodeKeyMaterials::Some(KeyMaterial_AES_GCM_GMAC_seq::One(key_material)),
      )
      .and(self.insert_participant_attributes(crypto_handle, participant_security_attributes))
      .and(Ok(crypto_handle))
  }

  fn register_matched_remote_participant(
    &mut self,
    local_participant_crypto_handle: ParticipantCryptoHandle,
    _remote_participant_identity: IdentityHandle,
    _remote_participant_permissions: PermissionsHandle,
    _shared_secret: SharedSecretHandle,
  ) -> SecurityResult<ParticipantCryptoHandle> {
    //TODO: this is only a mock implementation

    let local_participant_key_materials = self
      .get_common_encode_key_materials(&local_participant_crypto_handle)
      .cloned()
      .and_then(
        |common_encode_key_materials| match common_encode_key_materials {
          CommonEncodeKeyMaterials::Some(value) => Ok(value),
          CommonEncodeKeyMaterials::Volatile(_) => Err(create_security_error_and_log!(
            "The local_participant_crypto_handle {} points to volatile, but a participant cannot \
             be volatile",
            local_participant_crypto_handle
          )),
        },
      )?;

    let is_rtps_origin_authenticated = self
      .participant_encrypt_options
      .get(&local_participant_crypto_handle)
      .ok_or_else(|| {
        create_security_error_and_log!(
          "Participant encrypt options not found for the ParticipantCryptoHandle {}",
          local_participant_crypto_handle
        )
      })
      .and_then(|participant_security_attributes| {
        BuiltinPluginParticipantSecurityAttributes::try_from(
          participant_security_attributes.plugin_participant_attributes,
        )
      })
      .map(|plugin_participant_attributes| {
        plugin_participant_attributes.is_rtps_origin_authenticated
      })?;

    let remote_participant_crypto_handle = self.generate_crypto_handle();

    let key_materials = self.generate_receiver_specific_key(
      local_participant_key_materials,
      is_rtps_origin_authenticated,
    );

    self.insert_receiver_specific_encode_key_materials(
      remote_participant_crypto_handle,
      key_materials,
    )?;

    Ok(remote_participant_crypto_handle)
  }

  fn register_local_datawriter(
    &mut self,
    participant_crypto: ParticipantCryptoHandle,
    datawriter_properties: &[Property],
    datawriter_security_attributes: EndpointSecurityAttributes,
  ) -> SecurityResult<DatawriterCryptoHandle> {
    //TODO: this is only a mock implementation
    let plugin_endpoint_security_attributes = BuiltinPluginEndpointSecurityAttributes::try_from(
      datawriter_security_attributes.plugin_endpoint_attributes,
    )?;

    let local_datawriter_crypto_handle = self.generate_crypto_handle();

    let use_256_bit_key = Self::use_256_bit_key(datawriter_properties);

    // The key material for volatile datawriter is derived from the shared secret in
    // register_matched_remote_datareader
    if Self::is_volatile(datawriter_properties) {
      self.insert_common_encode_key_materials(
        local_datawriter_crypto_handle,
        CommonEncodeKeyMaterials::Volatile(use_256_bit_key),
      )?;
    } else {
      let submessage_transformation_kind = Self::transformation_kind(
        datawriter_security_attributes.is_submessage_protected,
        plugin_endpoint_security_attributes.is_submessage_encrypted,
        use_256_bit_key,
      );
      let payload_transformation_kind = Self::transformation_kind(
        datawriter_security_attributes.is_payload_protected,
        plugin_endpoint_security_attributes.is_payload_encrypted,
        use_256_bit_key,
      );

      let submessage_key_material = self.generate_key_material(submessage_transformation_kind);
      // If the transformation kinds match, key reuse is possible: 9.5.3.1
      let key_materials = if submessage_transformation_kind == payload_transformation_kind
      /* && additional configurable condition? */
      {
        KeyMaterial_AES_GCM_GMAC_seq::One(submessage_key_material)
      } else {
        KeyMaterial_AES_GCM_GMAC_seq::Two(
          submessage_key_material,
          self.generate_key_material(payload_transformation_kind),
        )
      };
      self.insert_common_encode_key_materials(
        local_datawriter_crypto_handle,
        CommonEncodeKeyMaterials::Some(key_materials),
      )?;
    }

    self.insert_endpoint_attributes(
      local_datawriter_crypto_handle,
      datawriter_security_attributes,
    )?;

    self.insert_endpoint_info(
      participant_crypto,
      EndpointInfo {
        crypto_handle: local_datawriter_crypto_handle,
        kind: EndpointKind::DataWriter,
      },
    );
    self
      .endpoint_to_participant
      .insert(local_datawriter_crypto_handle, participant_crypto);

    SecurityResult::Ok(local_datawriter_crypto_handle)
  }

  fn register_matched_remote_datareader(
    &mut self,
    local_datawriter_crypto_handle: DatawriterCryptoHandle,
    remote_participant_crypto_handle: ParticipantCryptoHandle,
    shared_secret: SharedSecretHandle,
    _relay_only: bool,
  ) -> SecurityResult<DatareaderCryptoHandle> {
    //TODO: this is only a mock implementation
    let common_encode_key_materials = self
      .get_common_encode_key_materials(&local_datawriter_crypto_handle)
      .cloned()?;

    // Find a handle for the remote datareader corresponding to the (remote
    // participant, local datawriter) pair, or generate a new one
    let remote_datareader_crypto_handle = self
      .get_or_generate_matched_remote_endpoint_crypto_handle(
        remote_participant_crypto_handle,
        local_datawriter_crypto_handle,
      );

    let receiver_specific_encode_key_materials = match common_encode_key_materials {
      CommonEncodeKeyMaterials::Volatile(use_256_bit_key) => {
        let volatile_key_materials =
          Self::derive_volatile_key_materials(&shared_secret, use_256_bit_key)?;

        // Instead of sending keys over the network like in other topics, the same key
        // material is used for decoding
        self.insert_decode_key_materials(
          remote_datareader_crypto_handle,
          volatile_key_materials.clone(),
        )?;
        volatile_key_materials
      }
      CommonEncodeKeyMaterials::Some(common_encode_key_materials) => {
        let is_submessage_origin_authenticated = self
          .endpoint_encrypt_options
          .get(&local_datawriter_crypto_handle)
          .ok_or_else(|| {
            create_security_error_and_log!(
              "Datawriter encrypt options not found for the DatawriterCryptoHandle {}",
              local_datawriter_crypto_handle
            )
          })
          .and_then(|datawriter_security_attributes| {
            BuiltinPluginEndpointSecurityAttributes::try_from(
              datawriter_security_attributes.plugin_endpoint_attributes,
            )
          })
          .map(|plugin_datawriter_attributes| {
            plugin_datawriter_attributes.is_submessage_origin_authenticated
          })?;

        self.generate_receiver_specific_key(
          common_encode_key_materials,
          is_submessage_origin_authenticated,
        )
      }
    };

    self.insert_receiver_specific_encode_key_materials(
      remote_datareader_crypto_handle,
      receiver_specific_encode_key_materials,
    )?;

    // Add endpoint info
    self.insert_endpoint_info(
      remote_participant_crypto_handle,
      EndpointInfo {
        crypto_handle: remote_datareader_crypto_handle,
        kind: EndpointKind::DataReader,
      },
    );

    // Copy the attributes
    if let Some(attributes) = self
      .endpoint_encrypt_options
      .get(&local_datawriter_crypto_handle)
      .cloned()
    {
      self
        .endpoint_encrypt_options
        .insert(remote_datareader_crypto_handle, attributes);
    }

    Ok(remote_datareader_crypto_handle)
  }

  fn register_local_datareader(
    &mut self,
    participant_crypto_handle: ParticipantCryptoHandle,
    datareader_properties: &[Property],
    datareader_security_attributes: EndpointSecurityAttributes,
  ) -> SecurityResult<DatareaderCryptoHandle> {
    //TODO: this is only a mock implementation
    let plugin_endpoint_security_attributes = BuiltinPluginEndpointSecurityAttributes::try_from(
      datareader_security_attributes.plugin_endpoint_attributes,
    )?;

    let local_datareader_crypto_handle = self.generate_crypto_handle();

    let use_256_bit_key = Self::use_256_bit_key(datareader_properties);
    // The key material for volatile datareader is derived from the shared secret in
    // register_matched_remote_datawriter
    if Self::is_volatile(datareader_properties) {
      self.insert_common_encode_key_materials(
        local_datareader_crypto_handle,
        CommonEncodeKeyMaterials::Volatile(use_256_bit_key),
      )?;
    } else {
      let key_material = self.generate_key_material(Self::transformation_kind(
        datareader_security_attributes.is_submessage_protected,
        plugin_endpoint_security_attributes.is_submessage_encrypted,
        use_256_bit_key,
      ));
      self.insert_common_encode_key_materials(
        local_datareader_crypto_handle,
        CommonEncodeKeyMaterials::Some(KeyMaterial_AES_GCM_GMAC_seq::One(key_material)),
      )?;
    }
    self.insert_endpoint_attributes(
      local_datareader_crypto_handle,
      datareader_security_attributes,
    )?;

    self.insert_endpoint_info(
      participant_crypto_handle,
      EndpointInfo {
        crypto_handle: local_datareader_crypto_handle,
        kind: EndpointKind::DataReader,
      },
    );
    self
      .endpoint_to_participant
      .insert(local_datareader_crypto_handle, participant_crypto_handle);
    SecurityResult::Ok(local_datareader_crypto_handle)
  }

  fn register_matched_remote_datawriter(
    &mut self,
    local_datareader_crypto_handle: DatareaderCryptoHandle,
    remote_participant_crypto_handle: ParticipantCryptoHandle,
    shared_secret: SharedSecretHandle,
  ) -> SecurityResult<DatawriterCryptoHandle> {
    //TODO: this is only a mock implementation
    let common_encode_key_materials = self
      .get_common_encode_key_materials(&local_datareader_crypto_handle)
      .cloned()?;

    // Find a handle for the remote datawriter corresponding to the (remote
    // participant, local datareader) pair, or generate a new one
    let remote_datawriter_crypto_handle: DatareaderCryptoHandle = self
      .get_or_generate_matched_remote_endpoint_crypto_handle(
        remote_participant_crypto_handle,
        local_datareader_crypto_handle,
      );

    let receiver_specific_encode_key_materials = match common_encode_key_materials {
      CommonEncodeKeyMaterials::Volatile(use_256_bit_key) => {
        let volatile_key_materials =
          Self::derive_volatile_key_materials(&shared_secret, use_256_bit_key)?;

        // Instead of sending keys over the network like in other topics, the same key
        // material is used for decoding
        self.insert_decode_key_materials(
          remote_datawriter_crypto_handle,
          volatile_key_materials.clone(),
        )?;
        volatile_key_materials
      }
      CommonEncodeKeyMaterials::Some(common_encode_key_materials) => {
        let is_submessage_origin_authenticated = self
          .endpoint_encrypt_options
          .get(&local_datareader_crypto_handle)
          .ok_or_else(|| {
            create_security_error_and_log!(
              "Datareader encrypt options not found for the handle {}",
              local_datareader_crypto_handle
            )
          })
          .and_then(|datareader_security_attributes| {
            BuiltinPluginEndpointSecurityAttributes::try_from(
              datareader_security_attributes.plugin_endpoint_attributes,
            )
          })
          .map(|plugin_datareader_attributes| {
            plugin_datareader_attributes.is_submessage_origin_authenticated
          })?;
        self.generate_receiver_specific_key(
          common_encode_key_materials,
          is_submessage_origin_authenticated,
        )
      }
    };
    self.insert_receiver_specific_encode_key_materials(
      remote_datawriter_crypto_handle,
      receiver_specific_encode_key_materials,
    )?;

    // Add endpoint info
    self.insert_endpoint_info(
      remote_participant_crypto_handle,
      EndpointInfo {
        crypto_handle: remote_datawriter_crypto_handle,
        kind: EndpointKind::DataWriter,
      },
    );

    // Copy the attributes
    if let Some(attributes) = self
      .endpoint_encrypt_options
      .get(&local_datareader_crypto_handle)
      .cloned()
    {
      self
        .endpoint_encrypt_options
        .insert(remote_datawriter_crypto_handle, attributes);
    }

    Ok(remote_datawriter_crypto_handle)
  }

  fn unregister_participant(
    &mut self,
    participant_crypto_handle: ParticipantCryptoHandle,
  ) -> SecurityResult<()> {
    //TODO: this is only a mock implementation
    self
      .participant_encrypt_options
      .remove(&participant_crypto_handle);
    if let Some(endpoint_info_set) = self
      .participant_to_endpoint_info
      .remove(&participant_crypto_handle)
    {
      for endpoint_info in endpoint_info_set {
        self.unregister_endpoint(endpoint_info);
      }
    }
    self
      .common_encode_key_materials
      .remove(&participant_crypto_handle);
    self
      .receiver_specific_encode_key_materials
      .remove(&participant_crypto_handle);
    self.decode_key_materials.remove(&participant_crypto_handle);
    Ok(())
  }

  fn unregister_datawriter(
    &mut self,
    datawriter_crypto_handle: DatawriterCryptoHandle,
  ) -> SecurityResult<()> {
    //TODO: this is only a mock implementation
    self.unregister_endpoint(EndpointInfo {
      crypto_handle: datawriter_crypto_handle,
      kind: EndpointKind::DataWriter,
    });
    Ok(())
  }

  fn unregister_datareader(
    &mut self,
    datareader_crypto_handle: DatareaderCryptoHandle,
  ) -> SecurityResult<()> {
    //TODO: this is only a mock implementation
    self.unregister_endpoint(EndpointInfo {
      crypto_handle: datareader_crypto_handle,
      kind: EndpointKind::DataReader,
    });
    Ok(())
  }
}