Coverage Report

Created: 2026-02-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ipsec-parser-0.7.0/src/esp.rs
Line
Count
Source
1
use crate::ikev2::IkeV2Header;
2
use crate::ikev2_parser::parse_ikev2_header;
3
use nom::bytes::streaming::take;
4
use nom::combinator::rest;
5
use nom::number::streaming::be_u32;
6
use nom::IResult;
7
8
/// Encapsulating Security Payload Packet Format
9
///
10
/// Defined in [RFC2406](https://tools.ietf.org/html/rfc2406) section 2
11
#[derive(Debug)]
12
pub struct ESPHeader<'a> {
13
    pub spi_index: &'a [u8],
14
    pub seq: u32,
15
    pub data: &'a [u8],
16
}
17
18
/// UDP-encapsulated Packet Formats
19
///
20
/// Defined in [RFC3948](https://tools.ietf.org/html/rfc3948) section 2
21
#[derive(Debug)]
22
pub enum ESPData<'a> {
23
    ESP(ESPHeader<'a>),
24
    IKE(IkeV2Header),
25
}
26
27
/// Parse an encapsulated ESP packet
28
///
29
/// The type of encapsulated data depends on the first field (`spi_index`): 0 is a forbidden SPI
30
/// index, and indicates that the header is an IKE header.
31
/// Any other value indicates an ESP header.
32
///
33
/// *Note: input is entirely consumed*
34
0
pub fn parse_esp_encapsulated(i: &[u8]) -> IResult<&[u8], ESPData> {
35
0
    if be_u32(i)?.1 == 0 {
36
0
        parse_ikev2_header(i).map(|x| (x.0, ESPData::IKE(x.1)))
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated::{closure#0}
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated::{closure#0}
37
    } else {
38
0
        parse_esp_header(i).map(|x| (x.0, ESPData::ESP(x.1)))
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated::{closure#1}
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated::{closure#1}
39
    }
40
0
}
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated
Unexecuted instantiation: ipsec_parser::esp::parse_esp_encapsulated
41
42
/// Parse an ESP packet
43
///
44
/// The ESP header contains:
45
///
46
/// - the SPI index
47
/// - the sequence number
48
/// - the payload data (which can be encrypted)
49
///
50
/// *Note: input is entirely consumed*
51
0
pub fn parse_esp_header(i: &[u8]) -> IResult<&[u8], ESPHeader> {
52
0
    let (i, spi_index) = take(4usize)(i)?;
53
0
    let (i, seq) = be_u32(i)?;
54
0
    let (i, data) = rest(i)?;
55
0
    let hdr = ESPHeader {
56
0
        spi_index,
57
0
        seq,
58
0
        data,
59
0
    };
60
0
    Ok((i, hdr))
61
0
}
Unexecuted instantiation: ipsec_parser::esp::parse_esp_header
Unexecuted instantiation: ipsec_parser::esp::parse_esp_header