use crate::gkdi::{GroupKeyEnvelope, RootKeyId};
use dcerpc::ndr::{NdrDecoder, NdrEncoder};
use dcerpc::transport::RpcTcp;
use dcerpc::{epm, Result as RpcResult, RpcError, Syntax};
pub fn isdkey_syntax() -> Syntax {
Syntax::new("b9785960-524f-11df-8b6d-83dcded72085", 1, 0)
}
pub const OPNUM_GET_KEY: u16 = 0;
pub fn encode_get_key(
target_sd: &[u8],
root_key_id: Option<RootKeyId>,
l0: i32,
l1: i32,
l2: i32,
) -> Vec<u8> {
let mut e = NdrEncoder::new();
e.u32(target_sd.len() as u32);
e.u32(target_sd.len() as u32); e.bytes(target_sd);
match root_key_id {
Some(id) => {
e.referent();
e.uuid(&id.0);
}
None => e.null_ptr(),
}
e.u32(l0 as u32);
e.u32(l1 as u32);
e.u32(l2 as u32);
e.into_bytes()
}
pub fn decode_get_key(stub: &[u8]) -> RpcResult<(Vec<u8>, u32)> {
let mut d = NdrDecoder::new(stub);
let cb_out = d.u32()? as usize;
let inner_ref = d.u32()?; let mut buf = Vec::new();
if inner_ref != 0 && cb_out > 0 {
let max = d.u32()? as usize;
buf = d.read_bytes(max)?.to_vec();
}
let _ = cb_out; let hresult = d.u32().unwrap_or(0);
Ok((buf, hresult))
}
#[allow(clippy::too_many_arguments)] pub async fn get_key(
host: &str,
domain: &str,
user: &str,
password: &str,
target_sd: &[u8],
root_key_id: Option<RootKeyId>,
l0: i32,
l1: i32,
l2: i32,
) -> RpcResult<GroupKeyEnvelope> {
let port = epm::resolve_port(host, isdkey_syntax()).await?;
let mut rpc = RpcTcp::connect(&format!("{host}:{port}")).await?;
rpc.bind_sealed(isdkey_syntax(), domain, user, password, "ADHAMMER")
.await?;
let stub = encode_get_key(target_sd, root_key_id, l0, l1, l2);
let resp = rpc.call_sealed(OPNUM_GET_KEY, &stub).await?;
let (bytes, hresult) = decode_get_key(&resp)?;
if hresult != 0 {
return Err(RpcError::Protocol(format!(
"MS-GKDI GetKey failed: HRESULT 0x{hresult:08x}"
)));
}
GroupKeyEnvelope::parse(&bytes)
.map_err(|e| RpcError::Protocol(format!("parse GroupKeyEnvelope: {e}")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_key_request_shape() {
let stub = encode_get_key(&[], None, 361, 7, 19);
assert_eq!(stub.len(), 4 + 4 + 4 + 4 * 3);
assert_eq!(&stub[0..4], &0u32.to_le_bytes()); assert_eq!(&stub[4..8], &0u32.to_le_bytes()); assert_eq!(&stub[8..12], &0u32.to_le_bytes()); assert_eq!(u32::from_le_bytes(stub[12..16].try_into().unwrap()), 361);
assert_eq!(u32::from_le_bytes(stub[16..20].try_into().unwrap()), 7);
assert_eq!(u32::from_le_bytes(stub[20..24].try_into().unwrap()), 19);
}
#[test]
fn get_key_request_with_root_key_and_sd() {
let sd = [0xAAu8; 8];
let root = RootKeyId([
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10,
]);
let stub = encode_get_key(&sd, Some(root), 1, 2, 3);
assert_eq!(u32::from_le_bytes(stub[0..4].try_into().unwrap()), 8);
assert_eq!(u32::from_le_bytes(stub[4..8].try_into().unwrap()), 8);
assert_eq!(&stub[8..16], &sd);
let referent = u32::from_le_bytes(stub[16..20].try_into().unwrap());
assert_ne!(referent, 0);
assert_eq!(&stub[20..36], &root.0); assert_eq!(u32::from_le_bytes(stub[36..40].try_into().unwrap()), 1);
assert_eq!(u32::from_le_bytes(stub[40..44].try_into().unwrap()), 2);
assert_eq!(u32::from_le_bytes(stub[44..48].try_into().unwrap()), 3);
}
#[test]
fn get_key_reply_null_pointer_yields_empty_bytes() {
let mut r = Vec::new();
r.extend_from_slice(&0u32.to_le_bytes());
r.extend_from_slice(&0u32.to_le_bytes());
r.extend_from_slice(&0x80070005u32.to_le_bytes());
let (bytes, hres) = decode_get_key(&r).unwrap();
assert!(bytes.is_empty());
assert_eq!(hres, 0x80070005);
}
#[test]
fn get_key_reply_carries_envelope_bytes() {
let payload = [0xBBu8; 5];
let mut r = Vec::new();
r.extend_from_slice(&(payload.len() as u32).to_le_bytes()); r.extend_from_slice(&0x2_0000u32.to_le_bytes()); r.extend_from_slice(&(payload.len() as u32).to_le_bytes()); r.extend_from_slice(&payload);
while r.len() % 4 != 0 {
r.push(0);
}
r.extend_from_slice(&0u32.to_le_bytes()); let (bytes, hres) = decode_get_key(&r).unwrap();
assert_eq!(bytes, payload);
assert_eq!(hres, 0);
}
#[test]
fn truncated_reply_is_error_not_panic() {
for cut in 0..24 {
let _ = decode_get_key(&vec![0u8; cut]);
}
}
}