rvoip-rtp-core 0.2.3

RTP/RTCP protocol implementation for the rvoip stack
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! ZRTP packet implementation
//!
//! This module implements the ZRTP packet format as defined in RFC 6189.

use super::{ZrtpAuthTag, ZrtpCipher, ZrtpHash, ZrtpKeyAgreement, ZrtpSasType};
use crate::Error;

/// ZRTP message types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpMessageType {
    /// Hello message
    Hello,
    /// HelloACK message
    HelloAck,
    /// Commit message
    Commit,
    /// DH Part 1 message
    DHPart1,
    /// DH Part 2 message
    DHPart2,
    /// Confirm1 message
    Confirm1,
    /// Confirm2 message
    Confirm2,
    /// Conf2ACK message
    Conf2Ack,
    /// Error message
    Error,
    /// ErrorACK message
    ErrorAck,
    /// GoClear message
    GoClear,
    /// ClearACK message
    ClearAck,
    /// SASrelay message
    SasRelay,
    /// RelayACK message
    RelayAck,
    /// Ping message
    Ping,
    /// PingACK message
    PingAck,
}

impl ZrtpMessageType {
    /// Get the message type string (4 characters)
    pub fn to_str(&self) -> &'static str {
        match self {
            ZrtpMessageType::Hello => "Hello   ",
            ZrtpMessageType::HelloAck => "HelloACK",
            ZrtpMessageType::Commit => "Commit  ",
            ZrtpMessageType::DHPart1 => "DHPart1 ",
            ZrtpMessageType::DHPart2 => "DHPart2 ",
            ZrtpMessageType::Confirm1 => "Confirm1",
            ZrtpMessageType::Confirm2 => "Confirm2",
            ZrtpMessageType::Conf2Ack => "Conf2ACK",
            ZrtpMessageType::Error => "Error   ",
            ZrtpMessageType::ErrorAck => "ErrorACK",
            ZrtpMessageType::GoClear => "GoClear ",
            ZrtpMessageType::ClearAck => "ClearACK",
            ZrtpMessageType::SasRelay => "SASrelay",
            ZrtpMessageType::RelayAck => "RelayACK",
            ZrtpMessageType::Ping => "Ping    ",
            ZrtpMessageType::PingAck => "PingACK ",
        }
    }

    /// Parse a message type from a string
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "Hello   " => Some(ZrtpMessageType::Hello),
            "HelloACK" => Some(ZrtpMessageType::HelloAck),
            "Commit  " => Some(ZrtpMessageType::Commit),
            "DHPart1 " => Some(ZrtpMessageType::DHPart1),
            "DHPart2 " => Some(ZrtpMessageType::DHPart2),
            "Confirm1" => Some(ZrtpMessageType::Confirm1),
            "Confirm2" => Some(ZrtpMessageType::Confirm2),
            "Conf2ACK" => Some(ZrtpMessageType::Conf2Ack),
            "Error   " => Some(ZrtpMessageType::Error),
            "ErrorACK" => Some(ZrtpMessageType::ErrorAck),
            "GoClear " => Some(ZrtpMessageType::GoClear),
            "ClearACK" => Some(ZrtpMessageType::ClearAck),
            "SASrelay" => Some(ZrtpMessageType::SasRelay),
            "RelayACK" => Some(ZrtpMessageType::RelayAck),
            "Ping    " => Some(ZrtpMessageType::Ping),
            "PingACK " => Some(ZrtpMessageType::PingAck),
            _ => None,
        }
    }
}

/// ZRTP protocol version
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZrtpVersion {
    /// Version 1.2 (current version)
    V12,
}

impl ZrtpVersion {
    /// Get the version string
    pub fn to_str(&self) -> &'static str {
        match self {
            ZrtpVersion::V12 => "1.2",
        }
    }
}

/// ZRTP packet structure
#[derive(Debug, Clone)]
pub struct ZrtpPacket {
    /// Message type
    message_type: ZrtpMessageType,
    /// Protocol version
    version: Option<ZrtpVersion>,
    /// Client identifier
    client_id: Option<String>,
    /// ZRTP identifier (ZID)
    zid: Option<[u8; 12]>,
    /// Supported ciphers
    ciphers: Vec<ZrtpCipher>,
    /// Supported hashes
    hashes: Vec<ZrtpHash>,
    /// Supported authentication tags
    auth_tags: Vec<ZrtpAuthTag>,
    /// Supported key agreement methods
    key_agreements: Vec<ZrtpKeyAgreement>,
    /// Supported SAS types
    sas_types: Vec<ZrtpSasType>,
    /// Selected cipher
    selected_cipher: Option<ZrtpCipher>,
    /// Selected hash
    selected_hash: Option<ZrtpHash>,
    /// Selected authentication tag
    selected_auth_tag: Option<ZrtpAuthTag>,
    /// Selected key agreement
    selected_key_agreement: Option<ZrtpKeyAgreement>,
    /// Selected SAS type
    selected_sas_type: Option<ZrtpSasType>,
    /// Public key
    public_key: Option<Vec<u8>>,
    /// Message authentication code
    mac: Option<Vec<u8>>,
    /// Raw packet data
    raw_data: Vec<u8>,
}

impl ZrtpPacket {
    /// Create a new ZRTP packet
    pub fn new(message_type: ZrtpMessageType) -> Self {
        Self {
            message_type,
            version: None,
            client_id: None,
            zid: None,
            ciphers: Vec::new(),
            hashes: Vec::new(),
            auth_tags: Vec::new(),
            key_agreements: Vec::new(),
            sas_types: Vec::new(),
            selected_cipher: None,
            selected_hash: None,
            selected_auth_tag: None,
            selected_key_agreement: None,
            selected_sas_type: None,
            public_key: None,
            mac: None,
            raw_data: Vec::new(),
        }
    }

    /// Get the message type
    pub fn message_type(&self) -> ZrtpMessageType {
        self.message_type
    }

    /// Set the protocol version
    pub fn set_version(&mut self, version: ZrtpVersion) {
        self.version = Some(version);
    }

    /// Set the client identifier
    pub fn set_client_id(&mut self, client_id: &str) {
        self.client_id = Some(client_id.to_string());
    }

    /// Set the ZID
    pub fn set_zid(&mut self, zid: &[u8; 12]) {
        self.zid = Some(*zid);
    }

    /// Get the ZID
    pub fn zid(&self) -> Option<[u8; 12]> {
        self.zid
    }

    /// Add a supported cipher
    pub fn add_cipher(&mut self, cipher: ZrtpCipher) {
        self.ciphers.push(cipher);
    }

    /// Get supported ciphers
    pub fn ciphers(&self) -> &[ZrtpCipher] {
        &self.ciphers
    }

    /// Add a supported hash
    pub fn add_hash(&mut self, hash: ZrtpHash) {
        self.hashes.push(hash);
    }

    /// Get supported hashes
    pub fn hashes(&self) -> &[ZrtpHash] {
        &self.hashes
    }

    /// Add a supported authentication tag
    pub fn add_auth_tag(&mut self, auth_tag: ZrtpAuthTag) {
        self.auth_tags.push(auth_tag);
    }

    /// Get supported authentication tags
    pub fn auth_tags(&self) -> &[ZrtpAuthTag] {
        &self.auth_tags
    }

    /// Add a supported key agreement method
    pub fn add_key_agreement(&mut self, key_agreement: ZrtpKeyAgreement) {
        self.key_agreements.push(key_agreement);
    }

    /// Get supported key agreement methods
    pub fn key_agreements(&self) -> &[ZrtpKeyAgreement] {
        &self.key_agreements
    }

    /// Add a supported SAS type
    pub fn add_sas_type(&mut self, sas_type: ZrtpSasType) {
        self.sas_types.push(sas_type);
    }

    /// Get supported SAS types
    pub fn sas_types(&self) -> &[ZrtpSasType] {
        &self.sas_types
    }

    /// Set the selected cipher
    pub fn set_cipher(&mut self, cipher: ZrtpCipher) {
        self.selected_cipher = Some(cipher);
    }

    /// Get the selected cipher
    pub fn cipher(&self) -> Option<ZrtpCipher> {
        self.selected_cipher
    }

    /// Set the selected hash
    pub fn set_hash(&mut self, hash: ZrtpHash) {
        self.selected_hash = Some(hash);
    }

    /// Get the selected hash
    pub fn hash(&self) -> Option<ZrtpHash> {
        self.selected_hash
    }

    /// Set the selected authentication tag
    pub fn set_auth_tag(&mut self, auth_tag: ZrtpAuthTag) {
        self.selected_auth_tag = Some(auth_tag);
    }

    /// Get the selected authentication tag
    pub fn auth_tag(&self) -> Option<ZrtpAuthTag> {
        self.selected_auth_tag
    }

    /// Set the selected key agreement
    pub fn set_key_agreement(&mut self, key_agreement: ZrtpKeyAgreement) {
        self.selected_key_agreement = Some(key_agreement);
    }

    /// Get the selected key agreement
    pub fn key_agreement(&self) -> Option<ZrtpKeyAgreement> {
        self.selected_key_agreement
    }

    /// Set the selected SAS type
    pub fn set_sas_type(&mut self, sas_type: ZrtpSasType) {
        self.selected_sas_type = Some(sas_type);
    }

    /// Get the selected SAS type
    pub fn sas_type(&self) -> Option<ZrtpSasType> {
        self.selected_sas_type
    }

    /// Set the public key
    pub fn set_public_key(&mut self, public_key: &[u8]) {
        self.public_key = Some(public_key.to_vec());
    }

    /// Get the public key
    pub fn public_key(&self) -> Option<Vec<u8>> {
        self.public_key.clone()
    }

    /// Set the MAC
    pub fn set_mac(&mut self, mac: &[u8]) {
        self.mac = Some(mac.to_vec());
    }

    /// Get the MAC
    pub fn mac(&self) -> Option<Vec<u8>> {
        self.mac.clone()
    }

    /// Convert cipher to 4-character code
    fn cipher_to_str(cipher: ZrtpCipher) -> &'static str {
        match cipher {
            ZrtpCipher::Aes1 => "AES1",
            ZrtpCipher::Aes3 => "AES3",
            ZrtpCipher::TwoF => "TwoF",
        }
    }

    /// Convert hash to 4-character code
    fn hash_to_str(hash: ZrtpHash) -> &'static str {
        match hash {
            ZrtpHash::S256 => "S256",
            ZrtpHash::S384 => "S384",
        }
    }

    /// Convert authentication tag to 4-character code
    fn auth_tag_to_str(auth_tag: ZrtpAuthTag) -> &'static str {
        match auth_tag {
            ZrtpAuthTag::HS32 => "HS32",
            ZrtpAuthTag::HS80 => "HS80",
        }
    }

    /// Convert key agreement to 4-character code
    fn key_agreement_to_str(key_agreement: ZrtpKeyAgreement) -> &'static str {
        match key_agreement {
            ZrtpKeyAgreement::DH3k => "DH3k",
            ZrtpKeyAgreement::DH4k => "DH4k",
            ZrtpKeyAgreement::EC25 => "EC25",
            ZrtpKeyAgreement::EC38 => "EC38",
        }
    }

    /// Convert SAS type to 4-character code
    fn sas_type_to_str(sas_type: ZrtpSasType) -> &'static str {
        match sas_type {
            ZrtpSasType::B32 => "B32 ",
            ZrtpSasType::B32E => "B32E",
        }
    }

    /// Parse cipher from 4-character code
    fn str_to_cipher(s: &str) -> Option<ZrtpCipher> {
        match s {
            "AES1" => Some(ZrtpCipher::Aes1),
            "AES3" => Some(ZrtpCipher::Aes3),
            "TwoF" => Some(ZrtpCipher::TwoF),
            _ => None,
        }
    }

    /// Parse hash from 4-character code
    fn str_to_hash(s: &str) -> Option<ZrtpHash> {
        match s {
            "S256" => Some(ZrtpHash::S256),
            "S384" => Some(ZrtpHash::S384),
            _ => None,
        }
    }

    /// Parse authentication tag from 4-character code
    fn str_to_auth_tag(s: &str) -> Option<ZrtpAuthTag> {
        match s {
            "HS32" => Some(ZrtpAuthTag::HS32),
            "HS80" => Some(ZrtpAuthTag::HS80),
            _ => None,
        }
    }

    /// Parse key agreement from 4-character code
    fn str_to_key_agreement(s: &str) -> Option<ZrtpKeyAgreement> {
        match s {
            "DH3k" => Some(ZrtpKeyAgreement::DH3k),
            "DH4k" => Some(ZrtpKeyAgreement::DH4k),
            "EC25" => Some(ZrtpKeyAgreement::EC25),
            "EC38" => Some(ZrtpKeyAgreement::EC38),
            _ => None,
        }
    }

    /// Parse SAS type from 4-character code
    fn str_to_sas_type(s: &str) -> Option<ZrtpSasType> {
        match s {
            "B32 " => Some(ZrtpSasType::B32),
            "B32E" => Some(ZrtpSasType::B32E),
            _ => None,
        }
    }

    /// Serialize the packet to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        // For the sake of this implementation, we'll create a simplified ZRTP packet format
        // In a real implementation, we'd follow the exact format in RFC 6189
        let mut data = Vec::new();

        // ZRTP magic cookie
        data.extend_from_slice(b"ZRTP");

        // Message type
        data.extend_from_slice(self.message_type.to_str().as_bytes());

        // For Hello message, include supported algorithms
        if self.message_type == ZrtpMessageType::Hello {
            // Version
            if let Some(version) = &self.version {
                data.extend_from_slice(version.to_str().as_bytes());
            } else {
                data.extend_from_slice(b"1.2");
            }

            // Client ID
            if let Some(client_id) = &self.client_id {
                // Fixed-length client ID (16 bytes)
                let mut client_id_bytes = [0u8; 16];
                for (i, b) in client_id.bytes().enumerate().take(16) {
                    client_id_bytes[i] = b;
                }
                data.extend_from_slice(&client_id_bytes);
            } else {
                data.extend_from_slice(&[0u8; 16]);
            }

            // ZID
            if let Some(zid) = &self.zid {
                data.extend_from_slice(zid);
            } else {
                data.extend_from_slice(&[0u8; 12]);
            }

            // Cipher count
            data.push(self.ciphers.len() as u8);

            // Ciphers
            for cipher in &self.ciphers {
                data.extend_from_slice(Self::cipher_to_str(*cipher).as_bytes());
            }

            // Hash count
            data.push(self.hashes.len() as u8);

            // Hashes
            for hash in &self.hashes {
                data.extend_from_slice(Self::hash_to_str(*hash).as_bytes());
            }

            // Auth tag count
            data.push(self.auth_tags.len() as u8);

            // Auth tags
            for auth_tag in &self.auth_tags {
                data.extend_from_slice(Self::auth_tag_to_str(*auth_tag).as_bytes());
            }

            // Key agreement count
            data.push(self.key_agreements.len() as u8);

            // Key agreements
            for key_agreement in &self.key_agreements {
                data.extend_from_slice(Self::key_agreement_to_str(*key_agreement).as_bytes());
            }

            // SAS type count
            data.push(self.sas_types.len() as u8);

            // SAS types
            for sas_type in &self.sas_types {
                data.extend_from_slice(Self::sas_type_to_str(*sas_type).as_bytes());
            }
        }

        // For Commit message, include selected algorithms
        if self.message_type == ZrtpMessageType::Commit {
            // ZID
            if let Some(zid) = &self.zid {
                data.extend_from_slice(zid);
            } else {
                data.extend_from_slice(&[0u8; 12]);
            }

            // Selected cipher
            if let Some(cipher) = &self.selected_cipher {
                data.extend_from_slice(Self::cipher_to_str(*cipher).as_bytes());
            } else {
                data.extend_from_slice(b"AES1");
            }

            // Selected hash
            if let Some(hash) = &self.selected_hash {
                data.extend_from_slice(Self::hash_to_str(*hash).as_bytes());
            } else {
                data.extend_from_slice(b"S256");
            }

            // Selected auth tag
            if let Some(auth_tag) = &self.selected_auth_tag {
                data.extend_from_slice(Self::auth_tag_to_str(*auth_tag).as_bytes());
            } else {
                data.extend_from_slice(b"HS80");
            }

            // Selected key agreement
            if let Some(key_agreement) = &self.selected_key_agreement {
                data.extend_from_slice(Self::key_agreement_to_str(*key_agreement).as_bytes());
            } else {
                data.extend_from_slice(b"EC25");
            }

            // Selected SAS type
            if let Some(sas_type) = &self.selected_sas_type {
                data.extend_from_slice(Self::sas_type_to_str(*sas_type).as_bytes());
            } else {
                data.extend_from_slice(b"B32 ");
            }
        }

        // For DH Part 1/2 messages, include public key
        if self.message_type == ZrtpMessageType::DHPart1
            || self.message_type == ZrtpMessageType::DHPart2
        {
            if let Some(public_key) = &self.public_key {
                // Public key length
                let key_len = public_key.len() as u16;
                data.extend_from_slice(&key_len.to_be_bytes());

                // Public key
                data.extend_from_slice(public_key);
            }
        }

        // For Confirm messages, include MAC
        if self.message_type == ZrtpMessageType::Confirm1
            || self.message_type == ZrtpMessageType::Confirm2
        {
            // ZID
            if let Some(zid) = &self.zid {
                data.extend_from_slice(zid);
            } else {
                data.extend_from_slice(&[0u8; 12]);
            }

            if let Some(mac) = &self.mac {
                // MAC length
                let mac_len = mac.len() as u16;
                data.extend_from_slice(&mac_len.to_be_bytes());

                // MAC
                data.extend_from_slice(mac);
            }
        }

        data
    }

    /// Parse a ZRTP packet from bytes
    pub fn parse(data: &[u8]) -> Result<Self, Error> {
        if data.len() < 12 {
            return Err(Error::ParseError("ZRTP packet too short".into()));
        }

        // Check ZRTP magic cookie
        if &data[0..4] != b"ZRTP" {
            return Err(Error::ParseError("Invalid ZRTP magic cookie".into()));
        }

        // Parse message type
        let msg_type_str = std::str::from_utf8(&data[4..12])
            .map_err(|_| Error::ParseError("Invalid UTF-8 in message type".into()))?;

        let message_type = ZrtpMessageType::from_str(msg_type_str)
            .ok_or_else(|| Error::ParseError(format!("Unknown message type: {}", msg_type_str)))?;

        let mut packet = ZrtpPacket::new(message_type);
        packet.raw_data = data.to_vec();

        // Parse Hello message
        if message_type == ZrtpMessageType::Hello {
            if data.len() < 28 {
                return Err(Error::ParseError("Hello message too short".into()));
            }

            // Version
            let version_str = std::str::from_utf8(&data[12..15])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in version".into()))?;

            if version_str == "1.2" {
                packet.version = Some(ZrtpVersion::V12);
            }

            // Client ID
            let client_id = std::str::from_utf8(&data[15..31])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in client ID".into()))?
                .trim_end_matches('\0')
                .to_string();

            packet.client_id = Some(client_id);

            // ZID
            let mut zid = [0u8; 12];
            zid.copy_from_slice(&data[31..43]);
            packet.zid = Some(zid);

            // Parse supported algorithms
            let mut pos = 43;

            if pos < data.len() {
                // Cipher count
                let cipher_count = data[pos] as usize;
                pos += 1;

                // Ciphers
                for _ in 0..cipher_count {
                    if pos + 4 > data.len() {
                        break;
                    }

                    let cipher_str = std::str::from_utf8(&data[pos..pos + 4])
                        .map_err(|_| Error::ParseError("Invalid UTF-8 in cipher".into()))?;

                    if let Some(cipher) = Self::str_to_cipher(cipher_str) {
                        packet.ciphers.push(cipher);
                    }

                    pos += 4;
                }
            }

            if pos < data.len() {
                // Hash count
                let hash_count = data[pos] as usize;
                pos += 1;

                // Hashes
                for _ in 0..hash_count {
                    if pos + 4 > data.len() {
                        break;
                    }

                    let hash_str = std::str::from_utf8(&data[pos..pos + 4])
                        .map_err(|_| Error::ParseError("Invalid UTF-8 in hash".into()))?;

                    if let Some(hash) = Self::str_to_hash(hash_str) {
                        packet.hashes.push(hash);
                    }

                    pos += 4;
                }
            }

            if pos < data.len() {
                // Auth tag count
                let auth_tag_count = data[pos] as usize;
                pos += 1;

                // Auth tags
                for _ in 0..auth_tag_count {
                    if pos + 4 > data.len() {
                        break;
                    }

                    let auth_tag_str = std::str::from_utf8(&data[pos..pos + 4])
                        .map_err(|_| Error::ParseError("Invalid UTF-8 in auth tag".into()))?;

                    if let Some(auth_tag) = Self::str_to_auth_tag(auth_tag_str) {
                        packet.auth_tags.push(auth_tag);
                    }

                    pos += 4;
                }
            }

            if pos < data.len() {
                // Key agreement count
                let key_agreement_count = data[pos] as usize;
                pos += 1;

                // Key agreements
                for _ in 0..key_agreement_count {
                    if pos + 4 > data.len() {
                        break;
                    }

                    let key_agreement_str = std::str::from_utf8(&data[pos..pos + 4])
                        .map_err(|_| Error::ParseError("Invalid UTF-8 in key agreement".into()))?;

                    if let Some(key_agreement) = Self::str_to_key_agreement(key_agreement_str) {
                        packet.key_agreements.push(key_agreement);
                    }

                    pos += 4;
                }
            }

            if pos < data.len() {
                // SAS type count
                let sas_type_count = data[pos] as usize;
                pos += 1;

                // SAS types
                for _ in 0..sas_type_count {
                    if pos + 4 > data.len() {
                        break;
                    }

                    let sas_type_str = std::str::from_utf8(&data[pos..pos + 4])
                        .map_err(|_| Error::ParseError("Invalid UTF-8 in SAS type".into()))?;

                    if let Some(sas_type) = Self::str_to_sas_type(sas_type_str) {
                        packet.sas_types.push(sas_type);
                    }

                    pos += 4;
                }
            }
        }

        // Parse Commit message
        if message_type == ZrtpMessageType::Commit {
            if data.len() < 32 {
                return Err(Error::ParseError("Commit message too short".into()));
            }

            // ZID
            let mut zid = [0u8; 12];
            zid.copy_from_slice(&data[12..24]);
            packet.zid = Some(zid);

            // Selected cipher
            let cipher_str = std::str::from_utf8(&data[24..28])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in cipher".into()))?;

            packet.selected_cipher = Self::str_to_cipher(cipher_str);

            // Selected hash
            let hash_str = std::str::from_utf8(&data[28..32])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in hash".into()))?;

            packet.selected_hash = Self::str_to_hash(hash_str);

            // Selected auth tag
            let auth_tag_str = std::str::from_utf8(&data[32..36])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in auth tag".into()))?;

            packet.selected_auth_tag = Self::str_to_auth_tag(auth_tag_str);

            // Selected key agreement
            let key_agreement_str = std::str::from_utf8(&data[36..40])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in key agreement".into()))?;

            packet.selected_key_agreement = Self::str_to_key_agreement(key_agreement_str);

            // Selected SAS type
            let sas_type_str = std::str::from_utf8(&data[40..44])
                .map_err(|_| Error::ParseError("Invalid UTF-8 in SAS type".into()))?;

            packet.selected_sas_type = Self::str_to_sas_type(sas_type_str);
        }

        // Parse DH Part 1/2 messages
        if message_type == ZrtpMessageType::DHPart1 || message_type == ZrtpMessageType::DHPart2 {
            if data.len() < 14 {
                return Err(Error::ParseError("DH Part message too short".into()));
            }

            // Public key length
            let key_len = u16::from_be_bytes([data[12], data[13]]) as usize;

            if data.len() < 14 + key_len {
                return Err(Error::ParseError("Public key data incomplete".into()));
            }

            // Public key
            packet.public_key = Some(data[14..14 + key_len].to_vec());
        }

        // Parse Confirm messages
        if message_type == ZrtpMessageType::Confirm1 || message_type == ZrtpMessageType::Confirm2 {
            if data.len() < 26 {
                return Err(Error::ParseError("Confirm message too short".into()));
            }

            // ZID
            let mut zid = [0u8; 12];
            zid.copy_from_slice(&data[12..24]);
            packet.zid = Some(zid);

            // MAC length
            let mac_len = u16::from_be_bytes([data[24], data[25]]) as usize;

            if data.len() < 26 + mac_len {
                return Err(Error::ParseError("MAC data incomplete".into()));
            }

            // MAC
            packet.mac = Some(data[26..26 + mac_len].to_vec());
        }

        Ok(packet)
    }
}