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
use super::{CountedOctetString, KeyBlock};
use nom::multi::many_m_n;
use nom::number::complete::{be_i32, be_u16, be_u32, be_u8};
use nom::bytes::complete::take;
use nom::IResult;

/// Definition:
/// ```c
/// keytab_entry {
///     int32_t size;
///     uint16_t num_components;    /* sub 1 if version 0x501 */
///     counted_octet_string realm;
///     counted_octet_string components[num_components];
///     uint32_t name_type;   /* not present if version 0x501 */
///     uint32_t timestamp;
///     uint8_t vno8;
///     keyblock key;
///     uint32_t vno; /* only present if >= 4 bytes left in entry */
/// }
///```
///

#[derive(Clone, Debug, PartialEq)]
pub struct KeytabEntry {
    pub realm: CountedOctetString,
    pub components: Vec<CountedOctetString>,
    pub name_type: u32,
    pub timestamp: u32,
    pub vno8: u8,
    pub key: KeyBlock,
    pub vno: Option<u32>,
}

impl KeytabEntry {
    pub fn new(
        realm: CountedOctetString,
        components: Vec<CountedOctetString>,
        name_type: u32,
        timestamp: u32,
        vno8: u8,
        key: KeyBlock,
        vno: Option<u32>,
    ) -> Self {
        return Self {
            realm,
            components,
            name_type,
            timestamp,
            vno8,
            key,
            vno,
        };
    }

    /// Creates a new instance from the binary representation
    /// # Error
    /// Returns error when the binary has not the expected format.
    pub fn parse(raw: &[u8]) -> IResult<&[u8], Self> {
        let (raw, size) = be_i32(raw)?;

        // negative size means that the entry is deleted
        if size < 0 {
            let (raw, _) = take(-size as usize)(raw)?;
            return Ok((raw, Self::default()));
        }

        let (raw, raw_entry) = take (size as usize)(raw)?;

        let (raw_entry, num_components) = be_u16(raw_entry)?;
        let (raw_entry, realm) = CountedOctetString::parse(raw_entry)?;

        let (raw_entry, components) = many_m_n(
            num_components as usize,
            num_components as usize,
            CountedOctetString::parse,
        )(raw_entry)?;

        let (raw_entry, name_type) = be_u32(raw_entry)?;
        let (raw_entry, timestamp) = be_u32(raw_entry)?;
        let (raw_entry, vno8) = be_u8(raw_entry)?;
        let (raw_entry, key) = KeyBlock::parse(raw_entry)?;

        let vno;
        if raw_entry.len() > 0 {
            let (_, v) = be_u32(raw_entry)?;
            vno = Some(v);
        }else  {
            vno = None;
        }

        return Ok((
            raw,
            Self::new(realm, components, name_type, timestamp, vno8, key, vno),
        ));
    }

    /// Build the binary representation
    pub fn build(self) -> Vec<u8> {
        let mut bytes = Vec::new();

        let num_components = self.components.len() as u16;

        bytes.append(&mut num_components.to_be_bytes().to_vec());
        bytes.append(&mut self.realm.build());

        for component in self.components {
            bytes.append(&mut component.build());
        }

        bytes.append(&mut self.name_type.to_be_bytes().to_vec());
        bytes.append(&mut self.timestamp.to_be_bytes().to_vec());
        bytes.append(&mut self.vno8.to_be_bytes().to_vec());
        bytes.append(&mut self.key.build());

        if let Some(vno) = self.vno {
            bytes.append(&mut vno.to_be_bytes().to_vec());
        }

        let size = bytes.len() as i32;

        let mut size_bytes = size.to_be_bytes().to_vec();
        size_bytes.append(&mut bytes);

        return size_bytes;
    }
}

impl Default for KeytabEntry {

    fn default() -> Self {
        return Self::new(CountedOctetString::default(), Vec::new(), 0, 0, 0, KeyBlock::default(), Some(0));
    }

}

#[cfg(test)]
mod tests {
    use super::*;

    static RAW_ENTRY: &'static [u8] = &[
        0x00, 0x00, 0x00, 0x35, 0x00, 0x01, 0x00, 0x0a, 0x64, 0x6f, 0x6d, 0x61,
        0x69, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x04, 0x75, 0x73, 0x65, 0x72,
        0x00, 0x00, 0x00, 0x01, 0x60, 0x04, 0x57, 0x1f, 0x01, 0x00, 0x17, 0x00,
        0x10, 0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06, 0xbd,
        0xd8, 0x30, 0xb7, 0x58, 0x6c, 0x00, 0x00, 0x00, 0x01,
    ];

    static RAW_ENTRY_NO_VNO: &'static [u8] = &[
        0x00, 0x00, 0x00, 0x31, 0x00, 0x01, 0x00, 0x0a, 0x64, 0x6f, 0x6d, 0x61,
        0x69, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x04, 0x75, 0x73, 0x65, 0x72,
        0x00, 0x00, 0x00, 0x01, 0x60, 0x04, 0x57, 0x1f, 0x01, 0x00, 0x17, 0x00,
        0x10, 0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06, 0xbd,
        0xd8, 0x30, 0xb7, 0x58, 0x6c,
    ];

    #[test]
    fn test_parse_keytab_entry() {
        let entry = KeytabEntry {
            realm: CountedOctetString::new("domain.com".as_bytes().to_vec()),
            components: vec![CountedOctetString::new(
                "user".as_bytes().to_vec(),
            )],
            name_type: 1,
            timestamp: 0x6004571f,
            vno8: 1,
            key: KeyBlock::new(
                0x0017,
                vec![
                    0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06,
                    0xbd, 0xd8, 0x30, 0xb7, 0x58, 0x6c,
                ],
            ),
            vno: Some(0x00000001),
        };

        assert_eq!(entry, KeytabEntry::parse(RAW_ENTRY).unwrap().1);
    }

    #[test]
    fn test_parse_keytab_entry_no_vno() {
        let entry = KeytabEntry {
            realm: CountedOctetString::new("domain.com".as_bytes().to_vec()),
            components: vec![CountedOctetString::new(
                "user".as_bytes().to_vec(),
            )],
            name_type: 1,
            timestamp: 0x6004571f,
            vno8: 1,
            key: KeyBlock::new(
                0x0017,
                vec![
                    0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06,
                    0xbd, 0xd8, 0x30, 0xb7, 0x58, 0x6c,
                ],
            ),
            vno: None,
        };

        assert_eq!(entry, KeytabEntry::parse(RAW_ENTRY_NO_VNO).unwrap().1);
    }

    #[test]
    fn test_build_keytab_entry() {
        let entry = KeytabEntry {
            realm: CountedOctetString::new("domain.com".as_bytes().to_vec()),
            components: vec![CountedOctetString::new(
                "user".as_bytes().to_vec(),
            )],
            name_type: 1,
            timestamp: 0x6004571f,
            vno8: 1,
            key: KeyBlock::new(
                0x0017,
                vec![
                    0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06,
                    0xbd, 0xd8, 0x30, 0xb7, 0x58, 0x6c,
                ],
            ),
            vno: Some(0x00000001),
        };

        assert_eq!(RAW_ENTRY.to_vec(), entry.build());
    }

    #[test]
    fn test_build_keytab_entry_no_vno() {
        let entry = KeytabEntry {
            realm: CountedOctetString::new("domain.com".as_bytes().to_vec()),
            components: vec![CountedOctetString::new(
                "user".as_bytes().to_vec(),
            )],
            name_type: 1,
            timestamp: 0x6004571f,
            vno8: 1,
            key: KeyBlock::new(
                0x0017,
                vec![
                    0x88, 0x46, 0xf7, 0xea, 0xee, 0x8f, 0xb1, 0x17, 0xad, 0x06,
                    0xbd, 0xd8, 0x30, 0xb7, 0x58, 0x6c,
                ],
            ),
            vno: None,
        };

        assert_eq!(RAW_ENTRY_NO_VNO.to_vec(), entry.build());
    }
}