use std::time::Duration;
use picky_asn1::date::GeneralizedTime;
use picky_asn1::wrapper::{
Asn1SequenceOf, ExplicitContextTag0, ExplicitContextTag1, ExplicitContextTag2, ExplicitContextTag3,
ExplicitContextTag4, ExplicitContextTag5, ExplicitContextTag6, ExplicitContextTag7, ExplicitContextTag9,
ExplicitContextTag10, ExplicitContextTag11, IntegerAsn1, OctetStringAsn1, Optional,
};
use picky_asn1_der::application_tag::ApplicationTag;
use picky_krb::constants::key_usages::TICKET_REP;
use picky_krb::crypto::CipherSuite;
use picky_krb::data_types::{
EncTicketPart, EncTicketPartInner, EncryptedData, EncryptionKey, HostAddresses, KerberosFlags, KerberosTime,
LastReq, LastReqInner, PrincipalName, Realm, Ticket, TicketInner, TransitedEncoding,
};
use picky_krb::messages::EncKdcRepPart;
use time::OffsetDateTime;
use crate::KERBEROS_VERSION;
use crate::error::KdcError;
pub(super) struct MakeTicketParams<'ticket_enc_key> {
pub realm: Realm,
pub session_key: Vec<u8>,
pub ticket_encryption_key: &'ticket_enc_key [u8],
pub kdc_options: KerberosFlags,
pub sname: PrincipalName,
pub cname: PrincipalName,
pub etype: CipherSuite,
pub auth_time: OffsetDateTime,
pub end_time: OffsetDateTime,
}
pub(super) fn make_ticket(params: MakeTicketParams<'_>) -> Result<Ticket, KdcError> {
let MakeTicketParams {
realm,
session_key,
ticket_encryption_key,
kdc_options,
sname,
cname,
etype,
auth_time,
end_time,
} = params;
let ticket_enc_part = EncTicketPart::from(EncTicketPartInner {
flags: ExplicitContextTag0::from(kdc_options),
key: ExplicitContextTag1::from(EncryptionKey {
key_type: ExplicitContextTag0::from(IntegerAsn1::from(vec![u8::from(etype)])),
key_value: ExplicitContextTag1::from(OctetStringAsn1::from(session_key)),
}),
crealm: ExplicitContextTag2::from(realm.clone()),
cname: ExplicitContextTag3::from(cname),
transited: ExplicitContextTag4::from(TransitedEncoding {
tr_type: ExplicitContextTag0::from(IntegerAsn1::from(vec![0])),
contents: ExplicitContextTag1::from(OctetStringAsn1::from(vec![1])),
}),
auth_time: ExplicitContextTag5::from(KerberosTime::from(GeneralizedTime::from(auth_time))),
starttime: Optional::from(None),
endtime: ExplicitContextTag7::from(KerberosTime::from(GeneralizedTime::from(end_time))),
renew_till: Optional::from(None),
caddr: Optional::from(None),
authorization_data: Optional::from(None),
});
let ticket_enc_data = CipherSuite::Aes256CtsHmacSha196.cipher().encrypt(
ticket_encryption_key,
TICKET_REP,
&picky_asn1_der::to_vec(&ticket_enc_part)?,
)?;
Ok(Ticket::from(TicketInner {
tkt_vno: ExplicitContextTag0::from(IntegerAsn1::from(vec![KERBEROS_VERSION])),
realm: ExplicitContextTag1::from(realm),
sname: ExplicitContextTag2::from(sname),
enc_part: ExplicitContextTag3::from(EncryptedData {
etype: ExplicitContextTag0::from(IntegerAsn1::from(vec![u8::from(CipherSuite::Aes256CtsHmacSha196)])),
kvno: Optional::from(None),
cipher: ExplicitContextTag2::from(OctetStringAsn1::from(ticket_enc_data)),
}),
}))
}
pub(super) struct RepEncPartParams {
pub etype: CipherSuite,
pub session_key: Vec<u8>,
pub nonce: Vec<u8>,
pub kdc_options: KerberosFlags,
pub auth_time: OffsetDateTime,
pub end_time: OffsetDateTime,
pub realm: Realm,
pub sname: PrincipalName,
pub addresses: Option<HostAddresses>,
}
pub(super) fn make_rep_enc_part<const TAG: u8>(
params: RepEncPartParams,
encryption_key: &[u8],
key_usage: i32,
) -> Result<Vec<u8>, KdcError> {
let RepEncPartParams {
etype,
session_key,
nonce,
kdc_options,
auth_time,
end_time,
realm,
sname,
addresses,
} = params;
let enc_part = ApplicationTag::<_, TAG>::from(EncKdcRepPart {
key: ExplicitContextTag0::from(EncryptionKey {
key_type: ExplicitContextTag0::from(IntegerAsn1::from(vec![u8::from(etype.clone())])),
key_value: ExplicitContextTag1::from(OctetStringAsn1::from(session_key)),
}),
last_req: ExplicitContextTag1::from(LastReq::from(vec![LastReqInner {
lr_type: ExplicitContextTag0::from(IntegerAsn1::from(vec![0])),
lr_value: ExplicitContextTag1::from(KerberosTime::from(GeneralizedTime::from(
auth_time - Duration::from_secs(60),
))),
}])),
nonce: ExplicitContextTag2::from(IntegerAsn1::from(nonce)),
key_expiration: Optional::from(None),
flags: ExplicitContextTag4::from(kdc_options),
auth_time: ExplicitContextTag5::from(KerberosTime::from(GeneralizedTime::from(auth_time))),
start_time: Optional::from(Some(ExplicitContextTag6::from(KerberosTime::from(
GeneralizedTime::from(auth_time),
)))),
end_time: ExplicitContextTag7::from(KerberosTime::from(GeneralizedTime::from(end_time))),
renew_till: Optional::from(None),
srealm: ExplicitContextTag9::from(realm),
sname: ExplicitContextTag10::from(sname),
caddr: Optional::from(addresses.map(|addresses| ExplicitContextTag11::from(Asn1SequenceOf::from(addresses.0)))),
encrypted_pa_data: Optional::from(None),
});
let enc_data = etype
.cipher()
.encrypt(encryption_key, key_usage, &picky_asn1_der::to_vec(&enc_part)?)?;
Ok(enc_data)
}