stun-agent 0.1.2

Rust Sans I/O framwework to implement STUN agents
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
use crate::integrity::{IntegrityError, TransportIntegrity};
use crate::message::StunAttributes;
use crate::{Integrity, ProtectedAttributeIterator};
use log::{debug, info};
use stun_rs::attributes::stun::{MessageIntegrity, MessageIntegritySha256, UserName};
use stun_rs::{HMACKey, MessageClass, StunMessage, TransactionId};

#[derive(Debug)]
pub struct ShortTermCredentialClient {
    user_name: UserName,
    key: HMACKey,
    integrity: Option<Integrity>,
    validator: TransportIntegrity,
}

impl ShortTermCredentialClient {
    pub fn new(
        user_name: UserName,
        key: HMACKey,
        integrity: Option<Integrity>,
        is_reliable: bool,
    ) -> ShortTermCredentialClient {
        Self {
            user_name,
            key,
            integrity,
            validator: TransportIntegrity::new(is_reliable),
        }
    }

    /// 9.1.4 Receiving a Response
    fn process_message(
        &mut self,
        raw_buffer: &[u8],
        msg: &StunMessage,
    ) -> Result<(), IntegrityError> {
        let mut integrity = None;
        let mut integrity_sha256 = None;

        for attr in msg.attributes().protected_iter() {
            if attr.is_message_integrity() {
                integrity = Some(attr);
            }
            if attr.is_message_integrity_sha256() {
                integrity_sha256 = Some(attr);
            }
            if msg.class() != MessageClass::Indication
                && integrity.is_some()
                && integrity_sha256.is_some()
            {
                // Only one integrity attribute must be set in a response
                info!(
                    "[{:?}], both integrity attributes are set",
                    msg.transaction_id()
                );
                return Err(IntegrityError::Discarded);
            }
        }

        match &self.integrity {
            Some(val) => {
                let integrity = match val {
                    Integrity::MessageIntegrity => integrity,
                    Integrity::MessageIntegritySha256 => integrity_sha256,
                };

                self.validator
                    .compute_message_integrity(&self.key, integrity, raw_buffer, msg)
            }
            None => {
                let integrity = integrity.or(integrity_sha256);
                self.validator
                    .compute_message_integrity(&self.key, integrity, raw_buffer, msg)?;
                if msg.class() != MessageClass::Indication {
                    // We only update the integrity mechanism if the message is either a
                    // success response or an error response
                    if let Some(attr) = integrity {
                        if attr.is_message_integrity() {
                            self.integrity = Some(Integrity::MessageIntegrity);
                        } else {
                            self.integrity = Some(Integrity::MessageIntegritySha256);
                        }
                    }
                }
                Ok(())
            }
        }
    }

    pub fn recv_message(
        &mut self,
        raw_buffer: &[u8],
        msg: &StunMessage,
    ) -> Result<(), IntegrityError> {
        if msg.class() == MessageClass::Request {
            debug!(
                "[{:?}], received a request message, discarded",
                msg.transaction_id()
            );
            Err(IntegrityError::Discarded)
        } else {
            self.process_message(raw_buffer, msg)
        }
    }

    // 9.1.2.  Forming a Request or Indication
    // For a request or indication message, the agent MUST include the
    // USERNAME, MESSAGE-INTEGRITY-SHA256, and MESSAGE-INTEGRITY attributes
    // in the message unless the agent knows from an external mechanism
    // which message integrity algorithm is supported by both agents.  In
    // this case, either MESSAGE-INTEGRITY or MESSAGE-INTEGRITY-SHA256 MUST
    // be included in addition to USERNAME.  The HMAC for the MESSAGE-
    // INTEGRITY attribute is computed as described in Section 14.5, and the
    // HMAC for the MESSAGE-INTEGRITY-SHA256 attributes is computed as
    // described in Section 14.6.  Note that the password is never included
    // in the request or indication.
    fn prepare_request_or_indication(&self, attributes: &mut StunAttributes) {
        remove_auth_and_integrity_attrs(attributes);
        // Add username attribute
        attributes.add(self.user_name.clone());

        if let Some(integrity) = &self.integrity {
            // Add integrity attribute
            match integrity {
                Integrity::MessageIntegrity => {
                    attributes.add(MessageIntegrity::new(self.key.clone()))
                }
                Integrity::MessageIntegritySha256 => {
                    attributes.add(MessageIntegritySha256::new(self.key.clone()))
                }
            }
        } else {
            // Add MESSAGE-INTEGRITY and MESSAGE-INTEGRITY-SHA256 attributes
            attributes.add(MessageIntegrity::new(self.key.clone()));
            attributes.add(MessageIntegritySha256::new(self.key.clone()));
        }
    }

    pub fn add_attributes(&self, attributes: &mut StunAttributes) {
        self.prepare_request_or_indication(attributes);
    }

    pub fn signal_protection_violated_on_timeout(
        &mut self,
        transaction_id: &TransactionId,
    ) -> bool {
        self.validator
            .signal_protection_violated_on_timeout(transaction_id)
    }
}

// Remove authentication or message integrity attributes
fn remove_auth_and_integrity_attrs(attributes: &mut StunAttributes) {
    attributes.remove::<UserName>();
    attributes.remove::<MessageIntegrity>();
    attributes.remove::<MessageIntegritySha256>();
}

#[cfg(test)]
mod short_term_cred_mech_tests {
    use super::*;
    use stun_rs::attributes::stun::Software;
    use stun_rs::methods::BINDING;
    use stun_rs::StunAttribute;
    use stun_rs::{MessageDecoderBuilder, MessageEncoderBuilder, StunError, StunMessageBuilder};

    const USERNAME: &str = "test-username";
    const PASSWORD: &str = "test-password";
    const SOFTWARE: &str = "STUN test client";

    fn init_logging() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    fn create_stun_message(buffer: &mut [u8], class: MessageClass, integrity: Integrity) -> usize {
        let software = Software::new(SOFTWARE).expect("Could not create Software attribute");
        let user_name = UserName::try_from(USERNAME).expect("Can not create USERNAME attribute");
        let key = HMACKey::new_short_term(PASSWORD).expect("Could not create HMACKey");
        let integrity: StunAttribute = match integrity {
            Integrity::MessageIntegrity => MessageIntegrity::new(key).into(),
            Integrity::MessageIntegritySha256 => MessageIntegritySha256::new(key).into(),
        };

        let enc_msg = StunMessageBuilder::new(BINDING, class)
            .with_attribute(software)
            .with_attribute(user_name)
            .with_attribute(integrity)
            .build();

        let encoder = MessageEncoderBuilder::default().build();
        encoder
            .encode(buffer, &enc_msg)
            .expect("Failed to encode message")
    }

    fn check_attributes(integrity: Option<Integrity>, attributes: &[StunAttribute]) {
        let mut iter = attributes.iter();

        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        match integrity {
            Some(Integrity::MessageIntegrity) => {
                let attr = iter.next().expect("Expected attribute MessageIntegrity");
                assert!(attr.is_message_integrity());
                assert!(iter.next().is_none());
            }
            Some(Integrity::MessageIntegritySha256) => {
                let attr = iter
                    .next()
                    .expect("Expected attribute MessageIntegritySha256");
                assert!(attr.is_message_integrity_sha256());
                assert!(iter.next().is_none());
            }
            None => {
                let attr = iter.next().expect("Expected attribute MessageIntegrity");
                assert!(attr.is_message_integrity());

                let attr = iter
                    .next()
                    .expect("Expected attribute MessageIntegritySha256");
                assert!(attr.is_message_integrity_sha256());
                assert!(iter.next().is_none());
            }
        }
    }

    fn new_short_term_auth_client(
        integrity: Option<Integrity>,
        is_reliable: bool,
    ) -> Result<ShortTermCredentialClient, StunError> {
        Ok(ShortTermCredentialClient::new(
            UserName::new(USERNAME)?,
            HMACKey::new_short_term(PASSWORD)?,
            integrity,
            is_reliable,
        ))
    }

    #[test]
    fn test_send_request() {
        init_logging();

        let integrity = None;
        let client = new_short_term_auth_client(integrity, false)
            .expect("Failed to create ShortTermAuthClient");
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(integrity, &attrs);

        let integrity = Some(Integrity::MessageIntegrity);
        let client = new_short_term_auth_client(integrity, false)
            .expect("Failed to create ShortTermAuthClient");
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(integrity, &attrs);

        let integrity = Some(Integrity::MessageIntegritySha256);
        let client = new_short_term_auth_client(integrity, false)
            .expect("Failed to create ShortTermAuthClient");
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(integrity, &attrs);
    }

    #[test]
    fn test_recv_response_message_integrity() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::SuccessResponse,
            Integrity::MessageIntegrity,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        client
            .recv_message(&buffer[..size], &msg)
            .expect("Failed to process response");

        // This message should not be timedout out as it was successfully authenticated
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));

        // Processing the message must make the client pick the MessageIntegrity mechanism
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(Some(Integrity::MessageIntegrity), &attrs);

        // Change the integrity attribute to MessageIntegritySha256 in the next
        // response must make the client drop the message
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::SuccessResponse,
            Integrity::MessageIntegritySha256,
        );
        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");
        assert_eq!(
            Err(IntegrityError::Discarded),
            client.recv_message(&buffer[..size], &msg)
        );

        // A timeout on this not authenticated message must fire a ProtectionViolated error
        assert!(client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_response_message_integrity_sha256() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::SuccessResponse,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        client
            .recv_message(&buffer[..size], &msg)
            .expect("Failed to process response");

        // This message should not be timeout out as it was successfully authenticated
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));

        // Processing the message must make the client pick the MessageIntegritySha256 mechanism
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(Some(Integrity::MessageIntegritySha256), &attrs);

        // Change the integrity attribute to MessageIntegrity in the next
        // response must make the client drop the message
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::SuccessResponse,
            Integrity::MessageIntegrity,
        );
        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");
        assert_eq!(
            Err(IntegrityError::Discarded),
            client.recv_message(&buffer[..size], &msg)
        );

        // A timout on this not authenticated message must fire a ProtectionViolated error
        assert!(client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_response_message_integrity_both() {
        init_logging();

        let software =
            Software::new("STUN test client").expect("Could not create Software attribute");
        let user_name = UserName::try_from(USERNAME).expect("Can not create USERNAME attribute");
        let key = HMACKey::new_short_term(PASSWORD).expect("Could not create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::SuccessResponse)
            .with_attribute(software)
            .with_attribute(user_name)
            .with_attribute(integrity)
            .with_attribute(integrity_sha256)
            .build();

        let encoder = MessageEncoderBuilder::default().build();
        let mut buffer: [u8; 150] = [0x00; 150];
        let size = encoder
            .encode(&mut buffer, &msg)
            .expect("Failed to encode message");

        // Responses can not have integrity and integrity_sha256 attributes at the same time
        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        assert_eq!(
            Err(IntegrityError::Discarded),
            client.recv_message(&buffer[..size], &msg)
        );

        // Protection violated error should not be signaled if both integrity attributes were present
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_response_message_integrity_miss_both() {
        init_logging();

        let software =
            Software::new("STUN test client").expect("Could not create Software attribute");
        let user_name = UserName::try_from(USERNAME).expect("Can not create USERNAME attribute");

        let msg = StunMessageBuilder::new(BINDING, MessageClass::SuccessResponse)
            .with_attribute(software)
            .with_attribute(user_name)
            .build();

        let encoder = MessageEncoderBuilder::default().build();
        let mut buffer: [u8; 150] = [0x00; 150];
        let size = encoder
            .encode(&mut buffer, &msg)
            .expect("Failed to encode message");

        // Responses have neither integrity nor integrity_sha256 attributes
        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        assert_eq!(
            Err(IntegrityError::Discarded),
            client.recv_message(&buffer[..size], &msg)
        );

        // A timout on this not authenticated message must fire a ProtectionViolated error
        assert!(client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_response_on_reliable() {
        init_logging();

        let software =
            Software::new("STUN test client").expect("Could not create Software attribute");
        let user_name = UserName::try_from(USERNAME).expect("Can not create USERNAME attribute");

        let msg = StunMessageBuilder::new(BINDING, MessageClass::SuccessResponse)
            .with_attribute(software)
            .with_attribute(user_name)
            .build();

        let encoder = MessageEncoderBuilder::default().build();
        let mut buffer: [u8; 150] = [0x00; 150];
        let size = encoder
            .encode(&mut buffer, &msg)
            .expect("Failed to encode message");

        // Responses have neither integrity nor integrity_sha256 attributes
        let mut client =
            new_short_term_auth_client(None, true).expect("Failed to create ShortTermAuthClient");
        assert_eq!(
            Err(IntegrityError::ProtectionViolated),
            client.recv_message(&buffer[..size], &msg)
        );

        // A timout on a reliable channel must not fire a ProtectionViolated error because it was already signaled
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_request_on_unreliable_fail() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::Indication,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        client
            .recv_message(&buffer[..size], &msg)
            .expect("Failed to process response");

        // No protection violated error should be signaled for indications
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_indication_on_unreliable_valid() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::Indication,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        client
            .recv_message(&buffer[..size], &msg)
            .expect("Failed to process response");

        // No protection violated error should be signaled for indications
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_indication_on_unreliable_invalid() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::Indication,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        // Change the first bit (0x21) of the magic cookie to make the integrity check fail
        buffer[4] = 0xFF;
        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        let error = client
            .recv_message(&buffer[..size], &msg)
            .expect_err("Failed to process response");
        assert_eq!(IntegrityError::Discarded, error);

        // No protection violated error should be signaled for indications
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_recv_request_on_unreliable_discarded() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::Request,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        let error = client
            .recv_message(&buffer[..size], &msg)
            .expect_err("Failed to process response");
        assert_eq!(IntegrityError::Discarded, error);

        // No protection violated error should be signaled for indications
        assert!(!client.signal_protection_violated_on_timeout(msg.transaction_id()));
    }

    #[test]
    fn test_no_pick_integrity_on_indication() {
        init_logging();

        let mut buffer: [u8; 150] = [0x00; 150];
        let _ = create_stun_message(
            &mut buffer,
            MessageClass::Indication,
            Integrity::MessageIntegritySha256,
        );

        let decoder = MessageDecoderBuilder::default().build();
        let (msg, size) = decoder.decode(&buffer).expect("Failed to decode message");

        let mut client =
            new_short_term_auth_client(None, false).expect("Failed to create ShortTermAuthClient");
        client
            .recv_message(&buffer[..size], &msg)
            .expect("Failed to process response");

        // Request must set both integrity attributes until the server picks one
        let mut attributes = StunAttributes::default();
        client.add_attributes(&mut attributes);
        let attrs: Vec<StunAttribute> = attributes.into();
        check_attributes(None, &attrs);
    }
}