kerberos_ccache/
ccache.rs

1use super::credential::Credential;
2use super::header::Header;
3use super::principal::Principal;
4use crate::mappers::{ccache_to_krb_cred, krb_cred_to_ccache};
5use crate::{ConvertError, ConvertResult};
6use kerberos_asn1::KrbCred;
7use nom::number::complete::be_u16;
8use nom::{many0, named, tag, IResult};
9use std::convert::{TryFrom, TryInto};
10
11named!(parse_version, tag!(&[0x05, 0x04]));
12named!(parse_credentials<&[u8], Vec<Credential>>, many0!(Credential::parse));
13
14/// To store an array of credentials.
15/// # Definition
16/// ```c
17/// ccache {
18///     uint16_t file_format_version; /* 0x0504 */
19///     uint16_t headerlen; /* only if version is 0x0504 */
20///     header headers[]; /* only if version is 0x0504 */
21///     principal primary_principal;
22///     credential credentials[*];
23/// };
24///
25#[derive(Debug, PartialEq, Clone)]
26pub struct CCache {
27    pub file_format_version: u16,
28    pub header: Header,
29    pub primary_principal: Principal,
30    pub credentials: Vec<Credential>,
31}
32
33impl CCache {
34    const VERSION_5: u16 = 0x0504;
35
36    pub fn new(
37        header: Header,
38        primary_principal: Principal,
39        credentials: Vec<Credential>,
40    ) -> Self {
41        return Self {
42            file_format_version: Self::VERSION_5,
43            header,
44            primary_principal,
45            credentials,
46        };
47    }
48
49    /// Build the binary representation
50    pub fn build(self) -> Vec<u8> {
51        let mut bytes = self.file_format_version.to_be_bytes().to_vec();
52
53        let mut header_bytes = self.header.build();
54        let header_len = header_bytes.len() as u16;
55
56        bytes.append(&mut header_len.to_be_bytes().to_vec());
57        bytes.append(&mut header_bytes);
58        bytes.append(&mut self.primary_principal.build());
59
60        for credential in self.credentials.into_iter() {
61            bytes.append(&mut credential.build());
62        }
63
64        return bytes;
65    }
66
67    /// Creates a new instance from the binary representation
68    /// # Error
69    /// Returns error when the binary has not the expected format.
70    pub fn parse(raw: &[u8]) -> IResult<&[u8], Self> {
71        let (raw, _file_format_version) = parse_version(raw)?;
72        let (raw, _headerlen) = be_u16(raw)?;
73        let (raw, header) = Header::parse(raw)?;
74        let (raw, primary_principal) = Principal::parse(raw)?;
75        let (raw, credentials) = parse_credentials(raw)?;
76
77        let ccache = Self::new(header, primary_principal, credentials);
78        return Ok((raw, ccache));
79    }
80}
81
82impl TryFrom<KrbCred> for CCache {
83    type Error = ConvertError;
84
85    fn try_from(krb_cred: KrbCred) -> ConvertResult<Self> {
86        return krb_cred_to_ccache(krb_cred);
87    }
88}
89
90impl TryInto<KrbCred> for CCache {
91    type Error = ConvertError;
92
93    fn try_into(self) -> ConvertResult<KrbCred> {
94        return ccache_to_krb_cred(self);
95    }
96}
97
98#[cfg(test)]
99mod test {
100    use super::super::*;
101    use super::*;
102    use chrono::prelude::*;
103    use kerberos_constants::etypes::*;
104    use kerberos_constants::principal_names::*;
105    use kerberos_constants::ticket_flags;
106
107    static RAW_CCACHE: &'static [u8] = &[
108        0x05, 0x04, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff,
109        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
110        0x00, 0x00, 0x00, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e,
111        0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0x00, 0x00, 0x00, 0x06, 0x6d, 0x69,
112        0x63, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
113        0x00, 0x00, 0x00, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e,
114        0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0x00, 0x00, 0x00, 0x06, 0x6d, 0x69,
115        0x63, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
116        0x00, 0x00, 0x00, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e,
117        0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0x00, 0x00, 0x00, 0x06, 0x6b, 0x72,
118        0x62, 0x74, 0x67, 0x74, 0x00, 0x00, 0x00, 0x0e, 0x4b, 0x49, 0x4e, 0x47,
119        0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0x00, 0x12,
120        0x00, 0x00, 0x00, 0x20, 0x01, 0x27, 0x59, 0x90, 0x9b, 0x2a, 0xbf, 0x45,
121        0xbc, 0x36, 0x95, 0x7c, 0x32, 0xc9, 0x16, 0xe6, 0xde, 0xbe, 0x82, 0xfd,
122        0x9d, 0x64, 0xcf, 0x28, 0x1b, 0x23, 0xea, 0x73, 0xfc, 0x91, 0xd4, 0xc2,
123        0x5d, 0x22, 0x00, 0x65, 0x5d, 0x22, 0x00, 0x65, 0x5d, 0x22, 0x8d, 0x05,
124        0x5d, 0x23, 0x51, 0xe2, 0x00, 0x50, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
125        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x17, 0x61, 0x82, 0x04,
126        0x13, 0x30, 0x82, 0x04, 0x0f, 0xa0, 0x03, 0x02, 0x01, 0x05, 0xa1, 0x10,
127        0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48, 0x45,
128        0x41, 0x52, 0x54, 0x53, 0xa2, 0x23, 0x30, 0x21, 0xa0, 0x03, 0x02, 0x01,
129        0x01, 0xa1, 0x1a, 0x30, 0x18, 0x1b, 0x06, 0x6b, 0x72, 0x62, 0x74, 0x67,
130        0x74, 0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d, 0x2e, 0x48,
131        0x45, 0x41, 0x52, 0x54, 0x53, 0xa3, 0x82, 0x03, 0xcf, 0x30, 0x82, 0x03,
132        0xcb, 0xa0, 0x03, 0x02, 0x01, 0x12, 0xa1, 0x03, 0x02, 0x01, 0x02, 0xa2,
133        0x82, 0x03, 0xbd, 0x04, 0x82, 0x03, 0xb9, 0x0a, 0x33, 0x84, 0xdb, 0x8b,
134        0x31, 0x15, 0x29, 0x20, 0x37, 0xe5, 0xe7, 0xb0, 0x50, 0x1f, 0xbe, 0x91,
135        0x11, 0xff, 0x69, 0x28, 0x6f, 0xc4, 0x9e, 0x79, 0xf8, 0x79, 0x88, 0xb1,
136        0x7b, 0xcd, 0xc5, 0xe1, 0x87, 0x91, 0xf8, 0x44, 0x95, 0x2f, 0x35, 0x0e,
137        0xe9, 0xd5, 0x83, 0x74, 0x18, 0x2b, 0x84, 0xe0, 0xa8, 0xd1, 0x21, 0xcf,
138        0xe0, 0x40, 0x42, 0x63, 0xb7, 0x42, 0x60, 0xe8, 0x3d, 0x56, 0x2a, 0x08,
139        0x96, 0x39, 0x8e, 0x1f, 0xfa, 0xb2, 0x11, 0x35, 0x09, 0x61, 0xba, 0xdd,
140        0x8e, 0xfc, 0x71, 0xf9, 0x0f, 0x39, 0x46, 0x43, 0x50, 0xd9, 0x32, 0x24,
141        0xcc, 0x64, 0xa1, 0x46, 0xe0, 0x1b, 0x86, 0x0f, 0xac, 0x86, 0x6e, 0x64,
142        0xcc, 0x01, 0x1f, 0xcd, 0x50, 0xb3, 0xa0, 0x43, 0x93, 0x83, 0x9c, 0x6e,
143        0x73, 0xec, 0xee, 0x7f, 0x8b, 0x52, 0xb3, 0xa3, 0x0b, 0xcd, 0xbf, 0xd2,
144        0x51, 0xdf, 0x05, 0x4c, 0x6b, 0x77, 0x99, 0x35, 0x54, 0x83, 0x9f, 0x29,
145        0xef, 0x69, 0x7e, 0x31, 0xbd, 0x1a, 0x38, 0x2d, 0x63, 0xb0, 0x00, 0xb2,
146        0x42, 0x9c, 0x3e, 0xe9, 0x82, 0x29, 0xde, 0xfd, 0xb7, 0x9d, 0x4c, 0x89,
147        0x28, 0xdf, 0xac, 0xab, 0x36, 0x85, 0xda, 0xd0, 0x03, 0x05, 0xa4, 0x12,
148        0x24, 0x3a, 0x18, 0xf7, 0xdb, 0xbe, 0x2a, 0xd8, 0x8d, 0xaa, 0x2c, 0x76,
149        0xe7, 0x21, 0xe7, 0x6c, 0xde, 0x02, 0x17, 0xc6, 0x4c, 0xfe, 0x49, 0x0e,
150        0xb2, 0x4a, 0x65, 0xd5, 0x44, 0xee, 0x3f, 0xef, 0x1a, 0x43, 0x54, 0xfa,
151        0xd2, 0x9e, 0xd9, 0xf4, 0xbf, 0x40, 0x93, 0x3b, 0x1e, 0x92, 0x9f, 0x1e,
152        0xcf, 0x9a, 0xbe, 0xdc, 0xfc, 0xd4, 0xd0, 0xcc, 0x29, 0xe5, 0x51, 0xf8,
153        0x94, 0xde, 0xe8, 0xa6, 0x2e, 0x20, 0x60, 0xed, 0xdd, 0x51, 0x07, 0xd1,
154        0xbe, 0x5f, 0x65, 0x45, 0x7e, 0x96, 0x47, 0xa3, 0x29, 0x67, 0x22, 0x66,
155        0x35, 0x61, 0xa7, 0x39, 0x18, 0x19, 0x35, 0x9f, 0xe4, 0x74, 0x50, 0xce,
156        0x2b, 0x41, 0x58, 0xe7, 0x8a, 0x19, 0xbd, 0x2f, 0x4d, 0x76, 0x37, 0xed,
157        0xa4, 0x93, 0x00, 0xd7, 0x0b, 0x2b, 0xea, 0x45, 0x46, 0x8b, 0xc0, 0xaa,
158        0x7d, 0xb5, 0xd9, 0x61, 0x73, 0x3b, 0x6a, 0xc5, 0x5a, 0x93, 0xef, 0xee,
159        0xb9, 0xe4, 0x10, 0x37, 0xbd, 0xf0, 0x96, 0x70, 0x98, 0x85, 0xb8, 0x99,
160        0x8c, 0x53, 0x94, 0x38, 0x66, 0x18, 0x34, 0x1b, 0x3b, 0x08, 0x0e, 0xd0,
161        0x4b, 0x9c, 0x03, 0x54, 0xe5, 0x6b, 0x7e, 0x66, 0xd6, 0x74, 0x2b, 0xca,
162        0x9a, 0xaa, 0x3a, 0xc1, 0x3e, 0xc9, 0xf5, 0x75, 0x1c, 0xff, 0xc2, 0xdf,
163        0x1e, 0xd7, 0x0b, 0xca, 0x55, 0x4c, 0x50, 0x2b, 0x80, 0x89, 0xde, 0x2c,
164        0x51, 0x8d, 0x4c, 0x3f, 0x8b, 0x16, 0x22, 0xec, 0x3e, 0x04, 0x05, 0x58,
165        0xae, 0x09, 0xe6, 0x80, 0x02, 0x21, 0xac, 0xee, 0x86, 0x1f, 0xbb, 0xb0,
166        0x91, 0x69, 0xb9, 0x15, 0xdf, 0xb9, 0x86, 0xe5, 0xcf, 0xc1, 0x0e, 0xb7,
167        0x92, 0xb8, 0xe4, 0x55, 0x4d, 0x00, 0x90, 0xb6, 0xb0, 0x67, 0x28, 0x1d,
168        0xcd, 0x4b, 0x57, 0x98, 0x86, 0x7f, 0xee, 0x60, 0x17, 0x00, 0x0b, 0x1a,
169        0xf7, 0x02, 0xac, 0x37, 0xd5, 0x9b, 0xfc, 0xfa, 0xa2, 0x1f, 0xaa, 0x9a,
170        0x88, 0xa2, 0xbc, 0x10, 0xbd, 0xb8, 0x4f, 0xb8, 0xa6, 0x5b, 0xab, 0x0b,
171        0x25, 0x57, 0xad, 0xe4, 0x91, 0xbb, 0x05, 0x39, 0x68, 0xfe, 0x91, 0x6a,
172        0xdf, 0xb0, 0x05, 0x68, 0x7e, 0x76, 0xc2, 0x04, 0x72, 0x71, 0x36, 0x56,
173        0x87, 0x1f, 0x88, 0x7a, 0x1f, 0xc5, 0x22, 0x5a, 0x1a, 0x3d, 0x0b, 0x7b,
174        0x21, 0x32, 0xf3, 0x4f, 0xc4, 0x5a, 0x09, 0xa1, 0x9e, 0x9c, 0x16, 0xa7,
175        0x5d, 0xc0, 0xc8, 0x94, 0x47, 0x28, 0xad, 0x94, 0xac, 0x79, 0x16, 0xeb,
176        0x54, 0x71, 0x4c, 0x98, 0x92, 0x68, 0x8a, 0x3f, 0xd8, 0xb5, 0xc9, 0x70,
177        0x20, 0x4a, 0x4e, 0xb2, 0x57, 0x7c, 0x95, 0x99, 0xed, 0xb7, 0x29, 0x3a,
178        0x39, 0x81, 0x84, 0xe6, 0x33, 0x90, 0x8c, 0xfd, 0x0c, 0xbc, 0x4f, 0x9d,
179        0x6a, 0xd0, 0xde, 0x05, 0x60, 0x3d, 0x9f, 0x1a, 0xa7, 0x71, 0xa3, 0x2f,
180        0x83, 0x77, 0x4e, 0xb0, 0x11, 0x80, 0x05, 0xb4, 0xd0, 0x00, 0x82, 0x73,
181        0x64, 0x68, 0xae, 0xbc, 0x20, 0x1a, 0x2a, 0x8a, 0x18, 0x48, 0x43, 0xbf,
182        0x32, 0x64, 0x6b, 0xad, 0x66, 0x78, 0x8e, 0x24, 0xb1, 0x5f, 0xf7, 0xd7,
183        0x2d, 0x9a, 0x86, 0x9e, 0x25, 0x12, 0x8d, 0x90, 0x00, 0x32, 0x46, 0x70,
184        0x34, 0xcb, 0x9d, 0xa9, 0x3c, 0x84, 0x4c, 0x60, 0xc4, 0x4c, 0x39, 0xf0,
185        0x4c, 0xfb, 0x8e, 0x91, 0xb7, 0x3b, 0xbf, 0xb0, 0xe9, 0xf4, 0xa3, 0x9b,
186        0x7b, 0x57, 0xcf, 0xd9, 0xfb, 0x46, 0x81, 0xa9, 0xd5, 0x8f, 0x54, 0xc2,
187        0x2b, 0xc2, 0x35, 0x4d, 0xb0, 0xc3, 0x84, 0xd4, 0x97, 0x07, 0x6e, 0x3c,
188        0xa5, 0xe6, 0x40, 0xac, 0xad, 0x2b, 0xf9, 0xff, 0x62, 0x68, 0xea, 0x69,
189        0x41, 0x31, 0xe6, 0x31, 0x69, 0x40, 0x69, 0x1f, 0x3a, 0x03, 0x9b, 0x15,
190        0xd3, 0x19, 0x16, 0x7d, 0x87, 0xa0, 0xbb, 0xb2, 0xaf, 0x5c, 0x91, 0xd4,
191        0x41, 0x7c, 0x85, 0xcd, 0xbe, 0x1a, 0x99, 0xab, 0xf7, 0x9c, 0xeb, 0x1a,
192        0x8f, 0x0b, 0x97, 0xf1, 0xda, 0xc0, 0xe5, 0x18, 0xb1, 0xbe, 0x08, 0x20,
193        0x7d, 0x27, 0x75, 0x0c, 0xc9, 0x15, 0xee, 0x07, 0x18, 0x4b, 0x17, 0x6c,
194        0x90, 0xb9, 0x26, 0x83, 0xd0, 0x93, 0x0d, 0x5d, 0x6c, 0x7a, 0xa9, 0x32,
195        0xa5, 0x49, 0xd8, 0x32, 0xc8, 0xc0, 0x3f, 0x8a, 0x43, 0x6d, 0xb4, 0xe4,
196        0xe6, 0xe7, 0x60, 0x18, 0x40, 0xc7, 0x48, 0x69, 0xfb, 0x37, 0xfc, 0x77,
197        0x84, 0x6a, 0x8a, 0xb6, 0x2d, 0xf4, 0xce, 0x62, 0xda, 0x14, 0xe2, 0x60,
198        0xd0, 0x1b, 0xfd, 0xfa, 0x74, 0xde, 0xf9, 0xe8, 0xdc, 0x55, 0xcd, 0x31,
199        0x87, 0xd6, 0xa0, 0xf7, 0x96, 0xc8, 0x65, 0x31, 0xf9, 0x0a, 0x86, 0x73,
200        0x7f, 0x8e, 0xa7, 0xf7, 0xa1, 0x77, 0x54, 0x91, 0x9a, 0xd1, 0x05, 0x7c,
201        0xc2, 0xd7, 0xdb, 0x41, 0x63, 0x5c, 0x9b, 0xc9, 0x21, 0x5e, 0x8f, 0x53,
202        0xcf, 0xfd, 0xba, 0x9c, 0x0b, 0xde, 0xe4, 0xea, 0x3e, 0x42, 0x51, 0xc6,
203        0x56, 0x13, 0xe2, 0x5b, 0x3e, 0xee, 0x8b, 0x21, 0xe2, 0x77, 0xd4, 0x81,
204        0x42, 0x8a, 0xa6, 0xc3, 0x2e, 0xa5, 0xe8, 0x05, 0xf4, 0x17, 0xd3, 0x2c,
205        0x34, 0x89, 0x42, 0x0a, 0xcb, 0x0b, 0xd7, 0xbf, 0x4e, 0x35, 0x3b, 0x28,
206        0x38, 0x16, 0xc9, 0x43, 0xae, 0x3e, 0xd7, 0xb1, 0x25, 0x61, 0x42, 0xe7,
207        0xbb, 0x5f, 0xf0, 0x2d, 0xc7, 0x20, 0x0f, 0xdf, 0xe6, 0x3c, 0x3d, 0x46,
208        0x0a, 0xae, 0xee, 0xa3, 0xc6, 0x59, 0x04, 0x25, 0xd2, 0x3d, 0x3c, 0xce,
209        0xe6, 0x05, 0xc3, 0xab, 0xbc, 0xb5, 0xaf, 0x75, 0x96, 0xdf, 0xb6, 0x13,
210        0x7a, 0x0a, 0xfb, 0x6e, 0xb2, 0x80, 0x16, 0xc5, 0xd4, 0x75, 0x81, 0x1d,
211        0x1e, 0x26, 0xf5, 0x1f, 0x14, 0x75, 0x4a, 0xde, 0x3d, 0x65, 0x6e, 0xb7,
212        0x13, 0x3c, 0x8d, 0xbe, 0x40, 0xbe, 0xa0, 0x15, 0xd8, 0x36, 0xd8, 0x88,
213        0x00, 0x00, 0x00, 0x00,
214    ];
215    static RAW_TICKET: &'static [u8] = &[
216        0x61, 0x82, 0x04, 0x13, 0x30, 0x82, 0x04, 0x0f, 0xa0, 0x03, 0x02, 0x01,
217        0x05, 0xa1, 0x10, 0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f, 0x4d,
218        0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0xa2, 0x23, 0x30, 0x21, 0xa0,
219        0x03, 0x02, 0x01, 0x01, 0xa1, 0x1a, 0x30, 0x18, 0x1b, 0x06, 0x6b, 0x72,
220        0x62, 0x74, 0x67, 0x74, 0x1b, 0x0e, 0x4b, 0x49, 0x4e, 0x47, 0x44, 0x4f,
221        0x4d, 0x2e, 0x48, 0x45, 0x41, 0x52, 0x54, 0x53, 0xa3, 0x82, 0x03, 0xcf,
222        0x30, 0x82, 0x03, 0xcb, 0xa0, 0x03, 0x02, 0x01, 0x12, 0xa1, 0x03, 0x02,
223        0x01, 0x02, 0xa2, 0x82, 0x03, 0xbd, 0x04, 0x82, 0x03, 0xb9, 0x0a, 0x33,
224        0x84, 0xdb, 0x8b, 0x31, 0x15, 0x29, 0x20, 0x37, 0xe5, 0xe7, 0xb0, 0x50,
225        0x1f, 0xbe, 0x91, 0x11, 0xff, 0x69, 0x28, 0x6f, 0xc4, 0x9e, 0x79, 0xf8,
226        0x79, 0x88, 0xb1, 0x7b, 0xcd, 0xc5, 0xe1, 0x87, 0x91, 0xf8, 0x44, 0x95,
227        0x2f, 0x35, 0x0e, 0xe9, 0xd5, 0x83, 0x74, 0x18, 0x2b, 0x84, 0xe0, 0xa8,
228        0xd1, 0x21, 0xcf, 0xe0, 0x40, 0x42, 0x63, 0xb7, 0x42, 0x60, 0xe8, 0x3d,
229        0x56, 0x2a, 0x08, 0x96, 0x39, 0x8e, 0x1f, 0xfa, 0xb2, 0x11, 0x35, 0x09,
230        0x61, 0xba, 0xdd, 0x8e, 0xfc, 0x71, 0xf9, 0x0f, 0x39, 0x46, 0x43, 0x50,
231        0xd9, 0x32, 0x24, 0xcc, 0x64, 0xa1, 0x46, 0xe0, 0x1b, 0x86, 0x0f, 0xac,
232        0x86, 0x6e, 0x64, 0xcc, 0x01, 0x1f, 0xcd, 0x50, 0xb3, 0xa0, 0x43, 0x93,
233        0x83, 0x9c, 0x6e, 0x73, 0xec, 0xee, 0x7f, 0x8b, 0x52, 0xb3, 0xa3, 0x0b,
234        0xcd, 0xbf, 0xd2, 0x51, 0xdf, 0x05, 0x4c, 0x6b, 0x77, 0x99, 0x35, 0x54,
235        0x83, 0x9f, 0x29, 0xef, 0x69, 0x7e, 0x31, 0xbd, 0x1a, 0x38, 0x2d, 0x63,
236        0xb0, 0x00, 0xb2, 0x42, 0x9c, 0x3e, 0xe9, 0x82, 0x29, 0xde, 0xfd, 0xb7,
237        0x9d, 0x4c, 0x89, 0x28, 0xdf, 0xac, 0xab, 0x36, 0x85, 0xda, 0xd0, 0x03,
238        0x05, 0xa4, 0x12, 0x24, 0x3a, 0x18, 0xf7, 0xdb, 0xbe, 0x2a, 0xd8, 0x8d,
239        0xaa, 0x2c, 0x76, 0xe7, 0x21, 0xe7, 0x6c, 0xde, 0x02, 0x17, 0xc6, 0x4c,
240        0xfe, 0x49, 0x0e, 0xb2, 0x4a, 0x65, 0xd5, 0x44, 0xee, 0x3f, 0xef, 0x1a,
241        0x43, 0x54, 0xfa, 0xd2, 0x9e, 0xd9, 0xf4, 0xbf, 0x40, 0x93, 0x3b, 0x1e,
242        0x92, 0x9f, 0x1e, 0xcf, 0x9a, 0xbe, 0xdc, 0xfc, 0xd4, 0xd0, 0xcc, 0x29,
243        0xe5, 0x51, 0xf8, 0x94, 0xde, 0xe8, 0xa6, 0x2e, 0x20, 0x60, 0xed, 0xdd,
244        0x51, 0x07, 0xd1, 0xbe, 0x5f, 0x65, 0x45, 0x7e, 0x96, 0x47, 0xa3, 0x29,
245        0x67, 0x22, 0x66, 0x35, 0x61, 0xa7, 0x39, 0x18, 0x19, 0x35, 0x9f, 0xe4,
246        0x74, 0x50, 0xce, 0x2b, 0x41, 0x58, 0xe7, 0x8a, 0x19, 0xbd, 0x2f, 0x4d,
247        0x76, 0x37, 0xed, 0xa4, 0x93, 0x00, 0xd7, 0x0b, 0x2b, 0xea, 0x45, 0x46,
248        0x8b, 0xc0, 0xaa, 0x7d, 0xb5, 0xd9, 0x61, 0x73, 0x3b, 0x6a, 0xc5, 0x5a,
249        0x93, 0xef, 0xee, 0xb9, 0xe4, 0x10, 0x37, 0xbd, 0xf0, 0x96, 0x70, 0x98,
250        0x85, 0xb8, 0x99, 0x8c, 0x53, 0x94, 0x38, 0x66, 0x18, 0x34, 0x1b, 0x3b,
251        0x08, 0x0e, 0xd0, 0x4b, 0x9c, 0x03, 0x54, 0xe5, 0x6b, 0x7e, 0x66, 0xd6,
252        0x74, 0x2b, 0xca, 0x9a, 0xaa, 0x3a, 0xc1, 0x3e, 0xc9, 0xf5, 0x75, 0x1c,
253        0xff, 0xc2, 0xdf, 0x1e, 0xd7, 0x0b, 0xca, 0x55, 0x4c, 0x50, 0x2b, 0x80,
254        0x89, 0xde, 0x2c, 0x51, 0x8d, 0x4c, 0x3f, 0x8b, 0x16, 0x22, 0xec, 0x3e,
255        0x04, 0x05, 0x58, 0xae, 0x09, 0xe6, 0x80, 0x02, 0x21, 0xac, 0xee, 0x86,
256        0x1f, 0xbb, 0xb0, 0x91, 0x69, 0xb9, 0x15, 0xdf, 0xb9, 0x86, 0xe5, 0xcf,
257        0xc1, 0x0e, 0xb7, 0x92, 0xb8, 0xe4, 0x55, 0x4d, 0x00, 0x90, 0xb6, 0xb0,
258        0x67, 0x28, 0x1d, 0xcd, 0x4b, 0x57, 0x98, 0x86, 0x7f, 0xee, 0x60, 0x17,
259        0x00, 0x0b, 0x1a, 0xf7, 0x02, 0xac, 0x37, 0xd5, 0x9b, 0xfc, 0xfa, 0xa2,
260        0x1f, 0xaa, 0x9a, 0x88, 0xa2, 0xbc, 0x10, 0xbd, 0xb8, 0x4f, 0xb8, 0xa6,
261        0x5b, 0xab, 0x0b, 0x25, 0x57, 0xad, 0xe4, 0x91, 0xbb, 0x05, 0x39, 0x68,
262        0xfe, 0x91, 0x6a, 0xdf, 0xb0, 0x05, 0x68, 0x7e, 0x76, 0xc2, 0x04, 0x72,
263        0x71, 0x36, 0x56, 0x87, 0x1f, 0x88, 0x7a, 0x1f, 0xc5, 0x22, 0x5a, 0x1a,
264        0x3d, 0x0b, 0x7b, 0x21, 0x32, 0xf3, 0x4f, 0xc4, 0x5a, 0x09, 0xa1, 0x9e,
265        0x9c, 0x16, 0xa7, 0x5d, 0xc0, 0xc8, 0x94, 0x47, 0x28, 0xad, 0x94, 0xac,
266        0x79, 0x16, 0xeb, 0x54, 0x71, 0x4c, 0x98, 0x92, 0x68, 0x8a, 0x3f, 0xd8,
267        0xb5, 0xc9, 0x70, 0x20, 0x4a, 0x4e, 0xb2, 0x57, 0x7c, 0x95, 0x99, 0xed,
268        0xb7, 0x29, 0x3a, 0x39, 0x81, 0x84, 0xe6, 0x33, 0x90, 0x8c, 0xfd, 0x0c,
269        0xbc, 0x4f, 0x9d, 0x6a, 0xd0, 0xde, 0x05, 0x60, 0x3d, 0x9f, 0x1a, 0xa7,
270        0x71, 0xa3, 0x2f, 0x83, 0x77, 0x4e, 0xb0, 0x11, 0x80, 0x05, 0xb4, 0xd0,
271        0x00, 0x82, 0x73, 0x64, 0x68, 0xae, 0xbc, 0x20, 0x1a, 0x2a, 0x8a, 0x18,
272        0x48, 0x43, 0xbf, 0x32, 0x64, 0x6b, 0xad, 0x66, 0x78, 0x8e, 0x24, 0xb1,
273        0x5f, 0xf7, 0xd7, 0x2d, 0x9a, 0x86, 0x9e, 0x25, 0x12, 0x8d, 0x90, 0x00,
274        0x32, 0x46, 0x70, 0x34, 0xcb, 0x9d, 0xa9, 0x3c, 0x84, 0x4c, 0x60, 0xc4,
275        0x4c, 0x39, 0xf0, 0x4c, 0xfb, 0x8e, 0x91, 0xb7, 0x3b, 0xbf, 0xb0, 0xe9,
276        0xf4, 0xa3, 0x9b, 0x7b, 0x57, 0xcf, 0xd9, 0xfb, 0x46, 0x81, 0xa9, 0xd5,
277        0x8f, 0x54, 0xc2, 0x2b, 0xc2, 0x35, 0x4d, 0xb0, 0xc3, 0x84, 0xd4, 0x97,
278        0x07, 0x6e, 0x3c, 0xa5, 0xe6, 0x40, 0xac, 0xad, 0x2b, 0xf9, 0xff, 0x62,
279        0x68, 0xea, 0x69, 0x41, 0x31, 0xe6, 0x31, 0x69, 0x40, 0x69, 0x1f, 0x3a,
280        0x03, 0x9b, 0x15, 0xd3, 0x19, 0x16, 0x7d, 0x87, 0xa0, 0xbb, 0xb2, 0xaf,
281        0x5c, 0x91, 0xd4, 0x41, 0x7c, 0x85, 0xcd, 0xbe, 0x1a, 0x99, 0xab, 0xf7,
282        0x9c, 0xeb, 0x1a, 0x8f, 0x0b, 0x97, 0xf1, 0xda, 0xc0, 0xe5, 0x18, 0xb1,
283        0xbe, 0x08, 0x20, 0x7d, 0x27, 0x75, 0x0c, 0xc9, 0x15, 0xee, 0x07, 0x18,
284        0x4b, 0x17, 0x6c, 0x90, 0xb9, 0x26, 0x83, 0xd0, 0x93, 0x0d, 0x5d, 0x6c,
285        0x7a, 0xa9, 0x32, 0xa5, 0x49, 0xd8, 0x32, 0xc8, 0xc0, 0x3f, 0x8a, 0x43,
286        0x6d, 0xb4, 0xe4, 0xe6, 0xe7, 0x60, 0x18, 0x40, 0xc7, 0x48, 0x69, 0xfb,
287        0x37, 0xfc, 0x77, 0x84, 0x6a, 0x8a, 0xb6, 0x2d, 0xf4, 0xce, 0x62, 0xda,
288        0x14, 0xe2, 0x60, 0xd0, 0x1b, 0xfd, 0xfa, 0x74, 0xde, 0xf9, 0xe8, 0xdc,
289        0x55, 0xcd, 0x31, 0x87, 0xd6, 0xa0, 0xf7, 0x96, 0xc8, 0x65, 0x31, 0xf9,
290        0x0a, 0x86, 0x73, 0x7f, 0x8e, 0xa7, 0xf7, 0xa1, 0x77, 0x54, 0x91, 0x9a,
291        0xd1, 0x05, 0x7c, 0xc2, 0xd7, 0xdb, 0x41, 0x63, 0x5c, 0x9b, 0xc9, 0x21,
292        0x5e, 0x8f, 0x53, 0xcf, 0xfd, 0xba, 0x9c, 0x0b, 0xde, 0xe4, 0xea, 0x3e,
293        0x42, 0x51, 0xc6, 0x56, 0x13, 0xe2, 0x5b, 0x3e, 0xee, 0x8b, 0x21, 0xe2,
294        0x77, 0xd4, 0x81, 0x42, 0x8a, 0xa6, 0xc3, 0x2e, 0xa5, 0xe8, 0x05, 0xf4,
295        0x17, 0xd3, 0x2c, 0x34, 0x89, 0x42, 0x0a, 0xcb, 0x0b, 0xd7, 0xbf, 0x4e,
296        0x35, 0x3b, 0x28, 0x38, 0x16, 0xc9, 0x43, 0xae, 0x3e, 0xd7, 0xb1, 0x25,
297        0x61, 0x42, 0xe7, 0xbb, 0x5f, 0xf0, 0x2d, 0xc7, 0x20, 0x0f, 0xdf, 0xe6,
298        0x3c, 0x3d, 0x46, 0x0a, 0xae, 0xee, 0xa3, 0xc6, 0x59, 0x04, 0x25, 0xd2,
299        0x3d, 0x3c, 0xce, 0xe6, 0x05, 0xc3, 0xab, 0xbc, 0xb5, 0xaf, 0x75, 0x96,
300        0xdf, 0xb6, 0x13, 0x7a, 0x0a, 0xfb, 0x6e, 0xb2, 0x80, 0x16, 0xc5, 0xd4,
301        0x75, 0x81, 0x1d, 0x1e, 0x26, 0xf5, 0x1f, 0x14, 0x75, 0x4a, 0xde, 0x3d,
302        0x65, 0x6e, 0xb7, 0x13, 0x3c, 0x8d, 0xbe, 0x40, 0xbe, 0xa0, 0x15, 0xd8,
303        0x36, 0xd8, 0x88,
304    ];
305
306    #[test]
307    fn build_ccache() {
308        let ticket = CountedOctetString::new(RAW_TICKET.to_vec());
309
310        let realm_string =
311            CountedOctetString::new("KINGDOM.HEARTS".as_bytes().to_vec());
312
313        let client_principal = Principal::new(
314            NT_PRINCIPAL as u32,
315            realm_string.clone(),
316            vec![CountedOctetString::new("mickey".as_bytes().to_vec())],
317        );
318        let server_principal = Principal::new(
319            NT_PRINCIPAL as u32,
320            realm_string.clone(),
321            vec![
322                CountedOctetString::new("krbtgt".as_bytes().to_vec()),
323                realm_string.clone(),
324            ],
325        );
326
327        let key = KeyBlock::new(
328            AES256_CTS_HMAC_SHA1_96 as u16,
329            vec![
330                0x01, 0x27, 0x59, 0x90, 0x9b, 0x2a, 0xbf, 0x45, 0xbc, 0x36,
331                0x95, 0x7c, 0x32, 0xc9, 0x16, 0xe6, 0xde, 0xbe, 0x82, 0xfd,
332                0x9d, 0x64, 0xcf, 0x28, 0x1b, 0x23, 0xea, 0x73, 0xfc, 0x91,
333                0xd4, 0xc2,
334            ],
335        );
336
337        let is_skey = 0;
338
339        let tktflags = ticket_flags::FORWARDABLE
340            | ticket_flags::PROXIABLE
341            | ticket_flags::RENEWABLE
342            | ticket_flags::INITIAL
343            | ticket_flags::PRE_AUTHENT;
344
345        let time = Times::new(
346            Utc.with_ymd_and_hms(2019, 7, 7, 14, 23, 33).unwrap().timestamp() as u32,
347            Utc.with_ymd_and_hms(2019, 7, 7, 14, 23, 33).unwrap().timestamp() as u32,
348            Utc.with_ymd_and_hms(2019, 7, 8, 0, 23, 33).unwrap().timestamp() as u32,
349            Utc.with_ymd_and_hms(2019, 7, 8, 14, 23, 30).unwrap().timestamp() as u32,
350        );
351
352        let credential = Credential::new(
353            client_principal.clone(),
354            server_principal,
355            key,
356            time,
357            is_skey,
358            tktflags,
359            ticket,
360        );
361
362        let header = Header::default();
363
364        let ccache = CCache::new(header, client_principal, vec![credential]);
365
366        assert_eq!(RAW_CCACHE.to_vec(), ccache.build());
367    }
368
369    #[test]
370    fn test_parse_ccache() {
371        let ticket = CountedOctetString::new(RAW_TICKET.to_vec());
372
373        let realm_string =
374            CountedOctetString::new("KINGDOM.HEARTS".as_bytes().to_vec());
375
376        let client_principal = Principal::new(
377            NT_PRINCIPAL as u32,
378            realm_string.clone(),
379            vec![CountedOctetString::new("mickey".as_bytes().to_vec())],
380        );
381        let server_principal = Principal::new(
382            NT_PRINCIPAL as u32,
383            realm_string.clone(),
384            vec![
385                CountedOctetString::new("krbtgt".as_bytes().to_vec()),
386                realm_string.clone(),
387            ],
388        );
389
390        let key = KeyBlock::new(
391            AES256_CTS_HMAC_SHA1_96 as u16,
392            vec![
393                0x01, 0x27, 0x59, 0x90, 0x9b, 0x2a, 0xbf, 0x45, 0xbc, 0x36,
394                0x95, 0x7c, 0x32, 0xc9, 0x16, 0xe6, 0xde, 0xbe, 0x82, 0xfd,
395                0x9d, 0x64, 0xcf, 0x28, 0x1b, 0x23, 0xea, 0x73, 0xfc, 0x91,
396                0xd4, 0xc2,
397            ],
398        );
399
400        let is_skey = 0;
401
402        let tktflags = ticket_flags::FORWARDABLE
403            | ticket_flags::PROXIABLE
404            | ticket_flags::RENEWABLE
405            | ticket_flags::INITIAL
406            | ticket_flags::PRE_AUTHENT;
407
408        let time = Times::new(
409            Utc.with_ymd_and_hms(2019, 7, 7, 14, 23, 33).unwrap().timestamp() as u32,
410            Utc.with_ymd_and_hms(2019, 7, 7, 14, 23, 33).unwrap().timestamp() as u32,
411            Utc.with_ymd_and_hms(2019, 7, 8, 0, 23, 33).unwrap().timestamp() as u32,
412            Utc.with_ymd_and_hms(2019, 7, 8, 14, 23, 30).unwrap().timestamp() as u32,
413        );
414
415        let credential = Credential::new(
416            client_principal.clone(),
417            server_principal,
418            key,
419            time,
420            is_skey,
421            tktflags,
422            ticket,
423        );
424
425        let header = Header::new(
426            Header::DELTA_TIME,
427            DeltaTime::new(u32::max_value(), 0).build(),
428        );
429
430        let ccache = CCache::new(header, client_principal, vec![credential]);
431
432        assert_eq!(ccache, CCache::parse(RAW_CCACHE).unwrap().1);
433    }
434
435    #[test]
436    #[should_panic(expected = "input: [0], code: Tag")]
437    fn test_parse_ccache_error() {
438        CCache::parse(&[0]).unwrap();
439    }
440}