bp7 0.10.7

Rust implementation of dtn Bundle Protocol Version 7 ([RFC 9171]
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
//use std::convert::TryInto;
use std::fmt;

use super::bundle::ByteBuffer;
use super::flags::BlockControlFlags;
//use super::flags::BlockControlFlagsType;
use super::primary::PrimaryBlock;
use super::*;

use bitflags::bitflags;
use thiserror::Error;

// use aes_gcm::aead::{
//     generic_array::{typenum, GenericArray},
//     Aead, KeyInit, Payload,
// };
// use aes_gcm::aes::{Aes128, Aes256};
// use aes_gcm::AesGcm;

// use aes_kw::Kek;

use hmac::{Hmac, Mac};
use sha2::{Sha256, Sha384, Sha512};

use serde::de::{SeqAccess, Visitor};
use serde::ser::{SerializeSeq, Serializer};
use serde::{de, Deserialize, Deserializer, Serialize};

// https://www.rfc-editor.org/rfc/rfc9172.html#BlockType
pub const INTEGRITY_BLOCK: CanonicalBlockType = 11;
pub const CONFIDENTIALITY_BLOCK: CanonicalBlockType = 12;

// SHA Variant
// https://www.rfc-editor.org/rfc/rfc9173.html#name-sha-variant
pub type ShaVariantType = u16;
pub const HMAC_SHA_256: ShaVariantType = 5;
pub const HMAC_SHA_384: ShaVariantType = 6; // default
pub const HMAC_SHA_512: ShaVariantType = 7;

// Security Context Id
// https://www.rfc-editor.org/rfc/rfc9173.html#name-security-context-identifier
// https://www.rfc-editor.org/rfc/rfc9172.html#SecCtx
pub type SecurityContextId = i16;
pub const BIB_HMAC_SHA2_ID: SecurityContextId = 1; // BIB-HMAC-SHA2
pub const BCB_AES_GCM_ID: SecurityContextId = 2; // BCB-AES-GCM

// Security Context Flags
//
pub type SecurityContextFlag = u8;
pub const SEC_CONTEXT_ABSENT: SecurityContextFlag = 0; // Security context parameters should be empty
pub const SEC_CONTEXT_PRESENT: SecurityContextFlag = 1; // Security context parameters are defined

// AES Variant
// https://www.rfc-editor.org/rfc/rfc9173.html#name-aes-gcm
pub type AesVariantType = u16;
pub const AES_128_GCM: AesVariantType = 1;
pub const AES_256_GCM: AesVariantType = 3; // default

pub type SecurityBlockHeader = (CanonicalBlockType, u64, flags::BlockControlFlagsType);

/// IntegrityProtectedPlaintext Builder. See IntegrityProtectedPlaintext Doc for usage.
#[derive(Debug, Clone, PartialEq)]
pub struct IpptBuilder {
    scope_flags: IntegrityScopeFlagsType,
    // canonical forms
    primary_block: Option<PrimaryBlock>,
    security_header: Option<SecurityBlockHeader>,
    security_target_contents: Vec<u8>,
}

impl IpptBuilder {
    pub fn new() -> IpptBuilder {
        IpptBuilder {
            scope_flags: 0x0007, // default value
            primary_block: None,
            security_header: None,
            security_target_contents: Vec::new(),
        }
    }
    pub fn scope_flags(mut self, scope_flags: IntegrityScopeFlagsType) -> Self {
        self.scope_flags = scope_flags;
        self
    }
    pub fn primary_block(mut self, primary_block: PrimaryBlock) -> Self {
        self.primary_block = Some(primary_block);
        self
    }
    pub fn security_header(mut self, security_header: SecurityBlockHeader) -> Self {
        self.security_header = Some(security_header);
        self
    }
    pub fn security_target_contents(mut self, security_target_contents: Vec<u8>) -> Self {
        self.security_target_contents = security_target_contents;
        self
    }
    pub fn build(self) -> IntegrityProtectedPlaintext {
        IntegrityProtectedPlaintext {
            scope_flags: self.scope_flags,
            primary_block: self.primary_block,
            security_header: self.security_header,
            security_target_contents: self.security_target_contents,
        }
    }
}

/// Structure to hold the Integrity Protected Plaintext. The content
/// of the IPPT is constructed as the concatenation of information
/// whose integrity is being preserved. Can optionally protect the integrity of
/// the primary block, the payload block header, the security block header.
/// The payload of the security target itself is always protected.
///
/// To function correctly the scope_flags have to be set accordingly.  
/// The default value is 0x0007, which means all flags are set. The
/// other options for the scope_flags are:  <br/>
/// Bit 0 (the low-order bit, 0x0001): Include primary block flag  <br/>
/// Bit 1 (0x0002): Include target header flag  <br/>
/// Bit 2 (0x0004): Include security header flag  <br/>
/// Bits 3-15: Unassigned. Do NOT set.  <br/>
///
/// # Fields
/// * `scope_flags` - Bit field
/// * `primary_block` - A reference to the primary block
/// * `security_header` - A tuple with the values of the block_type,
///     the block_number and the block_control_flags
/// * `security_target_contents` - A Vector with the result values
///
/// # RFC references
/// [IPPT](https://www.rfc-editor.org/rfc/rfc9173.html#name-scope)
///
/// # Results
///
/// # Example
/// TODO: example
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct IntegrityProtectedPlaintext {
    scope_flags: IntegrityScopeFlagsType,
    // canonical forms
    primary_block: Option<PrimaryBlock>,
    security_header: Option<SecurityBlockHeader>,
    security_target_contents: Vec<u8>,
}

impl IntegrityProtectedPlaintext {
    pub fn new() -> IntegrityProtectedPlaintext {
        IntegrityProtectedPlaintext {
            scope_flags: 0x0007, // default value
            primary_block: None,
            security_header: None,
            security_target_contents: Vec::new(),
        }
    }

    pub fn create(&mut self, payload_block: &CanonicalBlock) -> ByteBuffer {
        // If header data is not none and corresponding flag is set, include in MAC
        let mut optional_ippt_data = Vec::<u8>::new();

        if self
            .scope_flags
            .contains(IntegrityScopeFlags::INTEGRITY_PRIMARY_HEADER)
        {
            if let Some(pb) = &self.primary_block {
                optional_ippt_data.append(
                    serde_cbor::to_vec(pb)
                        .expect("Error creating canonical form of primary block")
                        .as_mut(),
                );
            } else {
                eprintln!("Primary header flag set but no primary header given!")
            }
        }
        if self
            .scope_flags
            .contains(IntegrityScopeFlags::INTEGRITY_PAYLOAD_HEADER)
        {
            optional_ippt_data.append(
                self.construct_payload_header(payload_block)
                    .expect("Error constructing payload header")
                    .as_mut(),
            );
        }
        if self
            .scope_flags
            .contains(IntegrityScopeFlags::INTEGRITY_SECURITY_HEADER)
        {
            if let Some(sh) = &self.security_header {
                optional_ippt_data.append(
                    self.construct_security_header(sh)
                        .expect("Error constructing security header")
                        .as_mut(),
                );
            } else {
                eprintln!("Security header flag set but no security header given!")
            }
        }

        self.security_target_contents = serde_cbor::to_vec(&payload_block.data()).unwrap();

        // create canonical form of other data
        if !matches!(payload_block.data(), CanonicalData::Data(_)) {
            let temp_bytes = serde_bytes::Bytes::new(self.security_target_contents.as_slice());
            self.security_target_contents = serde_cbor::to_vec(&temp_bytes).unwrap();
        }

        let mut ippt = Vec::<u8>::new();
        ippt.append(
            &mut serde_cbor::to_vec(&self.scope_flags)
                .expect("Error creating canonical form of scope flags"),
        );
        ippt.append(&mut optional_ippt_data);
        ippt.append(&mut self.security_target_contents);
        println!("ippt hex {:?}", hexify(&ippt));
        ippt
    }

    fn construct_payload_header(
        &self,
        payload_block: &CanonicalBlock,
    ) -> Result<ByteBuffer, serde_cbor::Error> {
        let mut header = Vec::<u8>::new();
        header.append(&mut serde_cbor::to_vec(&payload_block.block_type)?);
        header.append(&mut serde_cbor::to_vec(&payload_block.block_number)?);
        header.append(&mut serde_cbor::to_vec(&payload_block.block_control_flags)?);
        //header.append(&mut serde_cbor::to_vec(&payload_block.crc.to_code())?); //TODO: check if not needed?
        Ok(header)
    }

    fn construct_security_header(
        &self,
        security_block_parameter: &SecurityBlockHeader,
    ) -> Result<ByteBuffer, serde_cbor::Error> {
        let mut header = Vec::<u8>::new();
        header.append(&mut serde_cbor::to_vec(&security_block_parameter.0)?);
        header.append(&mut serde_cbor::to_vec(&security_block_parameter.1)?);
        header.append(&mut serde_cbor::to_vec(&security_block_parameter.2)?);
        Ok(header)
    }
}

impl Default for IntegrityProtectedPlaintext {
    fn default() -> Self {
        IntegrityProtectedPlaintext::new()
    }
}

impl Default for IpptBuilder {
    fn default() -> Self {
        IpptBuilder::new()
    }
}

// Integrity Scope Flags
// https://www.rfc-editor.org/rfc/rfc9173.html#name-integrity-scope-flags
pub type IntegrityScopeFlagsType = u16;

bitflags! {
    pub struct IntegrityScopeFlags: IntegrityScopeFlagsType {
        // Include primary block flag
        const INTEGRITY_PRIMARY_HEADER = 0x0001;
        // Include target header flag
        const INTEGRITY_PAYLOAD_HEADER = 0x0002;
        // Include security header flag
        const INTEGRITY_SECURITY_HEADER = 0x0004;
    }
}

pub trait ScopeValidation {
    fn flags(&self) -> IntegrityScopeFlags;
    fn contains(&self, flags: IntegrityScopeFlags) -> bool;
}
impl ScopeValidation for IntegrityScopeFlagsType {
    fn flags(&self) -> IntegrityScopeFlags {
        IntegrityScopeFlags::from_bits_truncate(*self)
    }
    fn contains(&self, flags: IntegrityScopeFlags) -> bool
    where
        Self: Sized,
    {
        self.flags().contains(flags)
    }
}

// Abstract Security Block
// https://www.rfc-editor.org/rfc/rfc9172.html#name-abstract-security-block

// Security Context Parameters
// https://www.rfc-editor.org/rfc/rfc9173.html#name-enumerations

// Create Builder?
#[derive(Debug, Clone, PartialEq)]
pub struct BibSecurityContextParameter {
    pub sha_variant: Option<(u8, ShaVariantType)>,
    pub wrapped_key: Option<(u8, Vec<u8>)>, // TODO: Wrapped Key //byte string
    pub integrity_scope_flags: Option<(u8, IntegrityScopeFlagsType)>,
}

impl BibSecurityContextParameter {
    pub fn new(
        sha_variant: Option<(u8, ShaVariantType)>,
        wrapped_key: Option<(u8, Vec<u8>)>,
        integrity_scope_flags: Option<(u8, IntegrityScopeFlagsType)>,
    ) -> Self {
        Self {
            sha_variant,
            wrapped_key,
            integrity_scope_flags,
        }
    }
}

impl Default for BibSecurityContextParameter {
    fn default() -> Self {
        BibSecurityContextParameter {
            sha_variant: Some((1, HMAC_SHA_384)),
            wrapped_key: None,
            integrity_scope_flags: Some((3, 0x0007)),
        }
    }
}

impl Serialize for BibSecurityContextParameter {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut num_elems = 0;
        if self.sha_variant.is_some() {
            num_elems += 1
        }
        if self.wrapped_key.is_some() {
            num_elems += 1
        }
        if self.integrity_scope_flags.is_some() {
            num_elems += 1
        }

        let mut seq = serializer.serialize_seq(Some(num_elems))?;

        if let Some(sv) = &self.sha_variant {
            seq.serialize_element(sv)?;
        }
        if let Some(wk) = &self.wrapped_key {
            seq.serialize_element(&(wk.0, serde_bytes::Bytes::new(&wk.1)))?;
        }
        if let Some(isf) = &self.integrity_scope_flags {
            seq.serialize_element(isf)?;
        }

        seq.end()
    }
}

impl<'de> Deserialize<'de> for BibSecurityContextParameter {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct BibSecurityContexParameterVisitor;

        impl<'de> Visitor<'de> for BibSecurityContexParameterVisitor {
            type Value = BibSecurityContextParameter;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a byte sequence")
            }

            fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
            where
                S: SeqAccess<'de>,
            {
                let sha_variant = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                // TODO: deal with wrapped key
                let wrapped_key = None;
                let integrity_scope_flags = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(2, &self))?;

                Ok(BibSecurityContextParameter {
                    sha_variant,
                    wrapped_key,
                    integrity_scope_flags,
                })
            }
        }

        deserializer.deserialize_seq(BibSecurityContexParameterVisitor)
    }
}

#[derive(Error, Debug)]
pub enum IntegrityBlockBuilderError {
    #[error("Security Tragets MUST have at least one enrty")]
    MissingSecurityTargets,
    #[error("Security Context Flag set but no context parameter given")]
    FlagSetButNoParameter,
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntegrityBlockBuilder {
    security_targets: Option<Vec<u64>>, // array of block numbers TODO:  MUST represent the block number of a block that exists in the bundle
    security_context_id: SecurityContextId,
    security_context_flags: SecurityContextFlag, // bit field
    security_source: EndpointID,
    security_context_parameters: Option<BibSecurityContextParameter>, // optional
    security_results: Vec<Vec<(u64, ByteBuffer)>>, // output of security operations
}

impl IntegrityBlockBuilder {
    pub fn new() -> IntegrityBlockBuilder {
        IntegrityBlockBuilder {
            security_targets: None,
            security_context_id: BIB_HMAC_SHA2_ID,
            security_context_flags: SEC_CONTEXT_ABSENT,
            security_source: EndpointID::none(),
            security_context_parameters: None,
            security_results: Vec::new(),
        }
    }

    pub fn security_targets(mut self, security_targets: Vec<u64>) -> Self {
        self.security_targets = Some(security_targets);
        self
    }
    /*
    pub fn security_context_id(mut self, security_context_id: SecurityContextId) -> Self {
        self.security_context_id = security_context_id;
        self
    }*/
    pub fn security_context_flags(mut self, security_context_flags: SecurityContextFlag) -> Self {
        self.security_context_flags = security_context_flags;
        self
    }
    pub fn security_source(mut self, security_source: EndpointID) -> Self {
        self.security_source = security_source;
        self
    }
    pub fn security_context_parameters(
        mut self,
        security_context_parameters: BibSecurityContextParameter,
    ) -> Self {
        self.security_context_parameters = Some(security_context_parameters);
        self
    }
    pub fn security_results(mut self, security_results: Vec<Vec<(u64, ByteBuffer)>>) -> Self {
        self.security_results = security_results;
        self
    }
    pub fn build(self) -> Result<IntegrityBlock, IntegrityBlockBuilderError> {
        if let Some(security_targets) = self.security_targets {
            if let Some(_security_context_parameters) = self.security_context_parameters.clone() {
                Ok(IntegrityBlock {
                    security_targets,
                    security_context_id: self.security_context_id,
                    security_context_flags: self.security_context_flags,
                    security_source: self.security_source,
                    security_context_parameters: self.security_context_parameters,
                    security_results: self.security_results,
                })
            } else {
                Err(IntegrityBlockBuilderError::FlagSetButNoParameter)
            }
        } else {
            Err(IntegrityBlockBuilderError::MissingSecurityTargets)
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntegrityBlock {
    pub security_targets: Vec<u64>, // array of block numbers
    pub security_context_id: SecurityContextId,
    pub security_context_flags: SecurityContextFlag, // bit field
    pub security_source: EndpointID,
    pub security_context_parameters: Option<BibSecurityContextParameter>,
    pub security_results: Vec<Vec<(u64, ByteBuffer)>>, // output of security operations
}

impl IntegrityBlock {
    pub fn new() -> IntegrityBlock {
        IntegrityBlock {
            security_targets: Vec::new(),
            security_context_id: BIB_HMAC_SHA2_ID,
            security_context_flags: SEC_CONTEXT_ABSENT,
            security_source: EndpointID::none(),
            security_context_parameters: None,
            security_results: Vec::new(),
        }
    }
    fn hmac_sha384_compute(&self, key_bytes: &[u8; 16], payload: &ByteBuffer) -> Vec<u8> {
        let mut mac = <Hmac<Sha384> as Mac>::new_from_slice(key_bytes)
            .expect("HMAC can take key of any size");
        mac.update(payload);
        mac.finalize().into_bytes().to_vec()

        // for testing only
        //
        // let result = mac.finalize();
        // let code_bytes = result.into_bytes();
        // println!("hmac: {:x}", code_bytes);
        // code_bytes.to_vec()
    }

    fn hmac_sha256_compute(&self, key_bytes: &[u8; 16], payload: &ByteBuffer) -> Vec<u8> {
        let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(key_bytes)
            .expect("HMAC can take key of any size");
        //let mut mac = Hmac::<Sha256>::new_from_slice(key_bytes).expect("HMAC can take key of any size");

        mac.update(payload);
        mac.finalize().into_bytes().to_vec()
    }

    fn hmac_sha512_compute(&self, key_bytes: &[u8; 16], payload: &ByteBuffer) -> Vec<u8> {
        let mut mac = <Hmac<Sha512> as Mac>::new_from_slice(key_bytes)
            .expect("HMAC can take key of any size");
        //let mut mac = Hmac::<Sha512>::new_from_slice(key_bytes).expect("HMAC can take key of any size");
        mac.update(payload);
        mac.finalize().into_bytes().to_vec()
    }

    pub fn compute_hmac(&mut self, key_bytes: [u8; 16], ippt_list: Vec<(u64, &ByteBuffer)>) {
        // match ippt_list values to security targets
        self.security_results = vec![];

        for ippt in ippt_list {
            if self.security_targets.contains(&ippt.0) {
                //let key_bytes = hex!("1a2b1a2b1a2b1a2b1a2b1a2b1a2b1a2b");
                let result_value = match self
                    .security_context_parameters
                    .as_ref()
                    .unwrap()
                    .sha_variant
                    .unwrap()
                    .1
                {
                    5 => self.hmac_sha256_compute(&key_bytes, ippt.1),
                    6 => self.hmac_sha384_compute(&key_bytes, ippt.1),
                    7 => self.hmac_sha512_compute(&key_bytes, ippt.1),
                    _ => panic!("Undefined Sha Variant."),
                };

                // Integrity Security Context BIB-HMAC-SHA2 has only one result field
                // that means for every target there will be only one vector entry
                // with the result id set to 1 and the result value being the
                // outcome of the security operation (-> the MAC)
                // result_id always 1 https://www.rfc-editor.org/rfc/rfc9173.html#name-results
                // +--------------------------+     +---------------------------+
                // |          Target 1        |     |         Target N          |
                // +----------+----+----------+     +---------------------------+
                // | Result 1 |    | Result M | ... | Result 1 |    |  Result K |
                // +----+-----+ .. +----+-----+     +---+------+ .. +----+------+
                // | Id |Value|    | Id |Value|     | Id |Value|    | Id | Value|
                // +----+-----+    +----+-----+     +----+-----+    +----+------+

                self.security_results.push(vec![(ippt.0, result_value)]);
            } else {
                eprint!("Security Target and Ippt mismatch. Make sure there is an ippt for each target.")
            }
        }
    }

    pub fn to_cbor(&self) -> ByteBuffer {
        let mut cbor_format = Vec::<u8>::new();

        cbor_format.append(&mut serde_cbor::to_vec(&self.security_targets).unwrap());
        cbor_format.append(&mut serde_cbor::to_vec(&self.security_context_id).unwrap());
        cbor_format.append(&mut serde_cbor::to_vec(&self.security_context_flags).unwrap());
        cbor_format.append(&mut serde_cbor::to_vec(&self.security_source).unwrap());
        cbor_format.append(&mut serde_cbor::to_vec(&self.security_context_parameters).unwrap());

        // iterate through each target. Create bytes for each signature and onstruct security results format again
        let mut res = Vec::new();
        for i in 0..self.security_targets.len() {
            let next_result = &self.security_results[i];
            let temp_mac = serde_bytes::Bytes::new(&next_result[0].1);
            res.push(vec![(&next_result[0].0, temp_mac)]);
        }
        cbor_format.append(&mut serde_cbor::to_vec(&res).unwrap());
        cbor_format
    }
}

impl Default for IntegrityBlock {
    fn default() -> Self {
        IntegrityBlock::new()
    }
}

impl Default for IntegrityBlockBuilder {
    fn default() -> Self {
        IntegrityBlockBuilder::new()
    }
}

/*
impl Serialize for IntegrityBlock {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let num_elems = 6;
        println!("in serialize");


        let mut seq = serializer.serialize_seq(Some(num_elems))?;
        seq.serialize_element(&self.security_targets)?;
        seq.serialize_element(&self.security_context_id)?;
        seq.serialize_element(&self.security_context_flags)?;
        seq.serialize_element(&self.security_source)?;
        seq.serialize_element(&self.security_context_parameters)?;
        //seq.serialize_element(&self.security_results)?;
        //let temp = &self.security_results;
        let temp_mac = &serde_bytes::Bytes::new(&self.security_results[0][0].1);

        let test = vec![vec![(1, temp_mac)]];
        seq.serialize_element(&test)?;

        seq.end()
    }
}*/

impl<'de> Deserialize<'de> for IntegrityBlock {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct IntegrityBlockVisitor;

        impl<'de> Visitor<'de> for IntegrityBlockVisitor {
            type Value = IntegrityBlock;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a byte sequence")
            }

            fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
            where
                S: SeqAccess<'de>,
            {
                let security_targets: Vec<u64> = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                let security_context_id = seq.next_element()?.unwrap();
                let security_context_flags = seq.next_element()?.unwrap();
                let security_source = seq.next_element()?.unwrap();

                let security_context_parameters = seq.next_element()?.unwrap();

                // TODO: deal with multiple targets
                let results: Vec<Vec<(u64, &[u8])>> = seq.next_element()?.unwrap();
                let mut security_results = Vec::new();

                (0..security_targets.len()).for_each(|i| {
                    let next_result = &results[i];
                    let temp_mac = next_result[0].1.to_vec();
                    security_results.push(vec![(next_result[0].0, temp_mac)]);
                });
                Ok(IntegrityBlock {
                    security_targets,
                    security_context_id,
                    security_context_flags,
                    security_source,
                    security_context_parameters,
                    security_results,
                })
            }
        }

        deserializer.deserialize_seq(IntegrityBlockVisitor)
    }
}

pub fn new_integrity_block(
    block_number: u64,
    bcf: BlockControlFlags,
    security_block: ByteBuffer,
) -> CanonicalBlock {
    CanonicalBlockBuilder::default()
        .block_type(INTEGRITY_BLOCK)
        .block_number(block_number)
        .block_control_flags(bcf.bits())
        .data(CanonicalData::Unknown(security_block)) // The deserializer doesn't know the integrity block type, with no changes made this also has to be encoded as Unknown
        .build()
        .unwrap()
}