pub const RPC_C_AUTHN_GSS_KERBEROS: u8 = 0x10;
pub const RPC_C_AUTHN_LEVEL_PKT_PRIVACY: u8 = 0x06;
pub const ICPR_SYNTAX_UUID: [u8; 16] = [
0x20, 0x60, 0xae, 0x91, 0x3c, 0x9e, 0xcf, 0x11, 0x8d, 0x7c, 0x00, 0xaa, 0x00, 0xc0, 0x91, 0xbe,
];
pub const NDR_TRANSFER_SYNTAX_UUID: [u8; 16] = [
0x04, 0x5d, 0x88, 0x8a, 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00, 0x2b, 0x10, 0x48, 0x60,
];
#[derive(Clone)]
pub struct KrbTicket {
pub ap_req: Vec<u8>,
pub session_key: [u8; 16],
}
impl KrbTicket {
pub fn new(ap_req: impl Into<Vec<u8>>, session_key: [u8; 16]) -> Self {
Self {
ap_req: ap_req.into(),
session_key,
}
}
}
impl std::fmt::Debug for KrbTicket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("KrbTicket")
.field("ap_req_len", &self.ap_req.len())
.field("session_key", &"[redacted; 16 bytes]")
.finish()
}
}
const RPC_VERS: u8 = 5;
const RPC_VERS_MINOR: u8 = 0;
const PTYPE_BIND: u8 = 11;
const PTYPE_AUTH3: u8 = 16;
const PFC_FIRST_FRAG: u8 = 0x01;
const PFC_LAST_FRAG: u8 = 0x02;
const DREP_LE: [u8; 4] = [0x10, 0x00, 0x00, 0x00];
fn header_auth(ptype: u8, frag_length: u16, auth_length: u16, call_id: u32) -> Vec<u8> {
let mut h = Vec::with_capacity(16);
h.push(RPC_VERS);
h.push(RPC_VERS_MINOR);
h.push(ptype);
h.push(PFC_FIRST_FRAG | PFC_LAST_FRAG);
h.extend_from_slice(&DREP_LE);
h.extend_from_slice(&frag_length.to_le_bytes());
h.extend_from_slice(&auth_length.to_le_bytes());
h.extend_from_slice(&call_id.to_le_bytes());
h
}
fn kerberos_sec_trailer(auth_pad_length: u8) -> [u8; 8] {
[
RPC_C_AUTHN_GSS_KERBEROS,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
auth_pad_length,
0,
0,
0,
0,
0,
]
}
fn bind_body(syntax_uuid: [u8; 16], ver_major: u16, ver_minor: u16) -> Vec<u8> {
let mut body = Vec::with_capacity(72);
body.extend_from_slice(&5840u16.to_le_bytes()); body.extend_from_slice(&5840u16.to_le_bytes()); body.extend_from_slice(&0u32.to_le_bytes()); body.push(1); body.push(0);
body.extend_from_slice(&0u16.to_le_bytes());
body.extend_from_slice(&0u16.to_le_bytes()); body.push(1); body.push(0);
body.extend_from_slice(&syntax_uuid);
body.extend_from_slice(&ver_major.to_le_bytes());
body.extend_from_slice(&ver_minor.to_le_bytes());
body.extend_from_slice(&NDR_TRANSFER_SYNTAX_UUID);
body.extend_from_slice(&2u16.to_le_bytes()); body.extend_from_slice(&0u16.to_le_bytes());
body
}
pub fn encode_kerberos_bind_pdu(
call_id: u32,
syntax_uuid: [u8; 16],
ver_major: u16,
ver_minor: u16,
ap_req: &[u8],
) -> Vec<u8> {
let body = bind_body(syntax_uuid, ver_major, ver_minor);
let frag_length = (16 + body.len() + 8 + ap_req.len()) as u16;
let mut pdu = header_auth(PTYPE_BIND, frag_length, ap_req.len() as u16, call_id);
pdu.extend_from_slice(&body);
pdu.extend_from_slice(&kerberos_sec_trailer(0));
pdu.extend_from_slice(ap_req);
pdu
}
pub fn encode_kerberos_auth3_pdu(call_id: u32, follow_up_token: &[u8]) -> Vec<u8> {
let frag_length = (16 + 4 + 8 + follow_up_token.len()) as u16;
let mut pdu = header_auth(
PTYPE_AUTH3,
frag_length,
follow_up_token.len() as u16,
call_id,
);
pdu.extend_from_slice(&[0, 0, 0, 0]);
pdu.extend_from_slice(&kerberos_sec_trailer(0));
pdu.extend_from_slice(follow_up_token);
pdu
}
#[cfg(test)]
mod tests {
use super::*;
fn fake_ap_req(len: usize) -> Vec<u8> {
let mut v = vec![
0x60, 0x82, 0x00, 0x00, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02,
0x02,
];
v.resize(len, 0xAB);
v
}
#[test]
fn bind_pdu_has_kerberos_auth_type_in_sec_trailer() {
let ap_req = fake_ap_req(120);
let pdu = encode_kerberos_bind_pdu(7, ICPR_SYNTAX_UUID, 0, 0, &ap_req);
assert_eq!(pdu[0], 5, "rpc_vers");
assert_eq!(pdu[2], PTYPE_BIND, "ptype must be BIND");
assert_eq!(pdu[3], PFC_FIRST_FRAG | PFC_LAST_FRAG, "single-frag flags");
let frag = u16::from_le_bytes([pdu[8], pdu[9]]) as usize;
assert_eq!(frag, pdu.len(), "frag_length matches PDU length");
let auth_length = u16::from_le_bytes([pdu[10], pdu[11]]) as usize;
assert_eq!(auth_length, ap_req.len(), "auth_length must equal |AP-REQ|");
assert_eq!(u32::from_le_bytes(pdu[12..16].try_into().unwrap()), 7);
let st = pdu.len() - ap_req.len() - 8;
assert_eq!(
pdu[st], RPC_C_AUTHN_GSS_KERBEROS,
"auth_type in sec_trailer must be 0x10 (GSS_KERBEROS), got 0x{:02x}",
pdu[st]
);
assert_eq!(
pdu[st + 1],
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
"auth_level must be 0x06 (PKT_PRIVACY)"
);
assert_eq!(pdu[st + 2], 0, "auth_pad_length");
assert_eq!(&pdu[st + 4..st + 8], &[0, 0, 0, 0], "auth_context_id = 0");
assert_eq!(&pdu[pdu.len() - ap_req.len()..], &ap_req[..]);
}
#[test]
fn bind_pdu_offers_icpr_and_ndr_syntaxes() {
let ap_req = fake_ap_req(64);
let pdu = encode_kerberos_bind_pdu(1, ICPR_SYNTAX_UUID, 0, 0, &ap_req);
let abs_off = 16 + 16 ;
assert_eq!(&pdu[abs_off..abs_off + 16], &ICPR_SYNTAX_UUID);
let ndr_off = abs_off + 16 + 4;
assert_eq!(&pdu[ndr_off..ndr_off + 16], &NDR_TRANSFER_SYNTAX_UUID);
assert_eq!(
u16::from_le_bytes([pdu[ndr_off + 16], pdu[ndr_off + 17]]),
2
);
}
#[test]
fn auth3_pdu_carries_empty_or_followup_token() {
let empty = encode_kerberos_auth3_pdu(9, &[]);
assert_eq!(empty[2], PTYPE_AUTH3);
assert_eq!(u16::from_le_bytes([empty[10], empty[11]]), 0);
assert_eq!(
u16::from_le_bytes([empty[8], empty[9]]) as usize,
empty.len()
);
let st = empty.len() - 8;
assert_eq!(empty[st], RPC_C_AUTHN_GSS_KERBEROS);
assert_eq!(empty[st + 1], RPC_C_AUTHN_LEVEL_PKT_PRIVACY);
let tok = [0xAAu8; 32];
let with_tok = encode_kerberos_auth3_pdu(9, &tok);
assert_eq!(
u16::from_le_bytes([with_tok[10], with_tok[11]]) as usize,
tok.len()
);
assert_eq!(&with_tok[with_tok.len() - tok.len()..], &tok);
}
#[test]
fn ticket_debug_redacts_secrets() {
let ticket = KrbTicket::new(vec![0xDE; 42], [0xFF; 16]);
let dbg = format!("{ticket:?}");
assert!(dbg.contains("ap_req_len: 42"));
assert!(dbg.contains("redacted"));
assert!(!dbg.contains("255, 255, 255"));
}
#[test]
fn icpr_uuid_encoding_matches_canonical_form() {
let expected: [u8; 16] = [
0x20, 0x60, 0xae, 0x91, 0x3c, 0x9e, 0xcf, 0x11, 0x8d, 0x7c, 0x00, 0xaa, 0x00, 0xc0, 0x91, 0xbe, ];
assert_eq!(ICPR_SYNTAX_UUID, expected);
}
}