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
use std::num::NonZeroU32;

use crate::connection::{CompletionCode, IpmiCommand, Message, NetFn, ParseResponseError};

use super::{AuthError, AuthType};

#[derive(Debug, Clone, Copy)]
pub struct SessionChallenge {
    pub temporary_session_id: NonZeroU32,
    pub challenge_string: [u8; 16],
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetSessionChallenge {
    auth_type: AuthType,
    username: [u8; 16],
}

impl GetSessionChallenge {
    pub fn new(auth_type: AuthType, username: Option<&str>) -> Option<Self> {
        let bytes = username.map(|u| u.as_bytes()).unwrap_or(&[]);
        if bytes.len() > 16 {
            return None;
        }

        let mut username = [0u8; 16];
        bytes
            .iter()
            .enumerate()
            .for_each(|(idx, b)| username[idx] = *b);

        Some(Self {
            auth_type,
            username,
        })
    }

    pub fn auth_type(&self) -> AuthType {
        self.auth_type
    }

    pub fn username(&self) -> &str {
        let end = self.username.iter().take_while(|v| **v != 0).count();
        unsafe { core::str::from_utf8_unchecked(&self.username[..end]) }
    }
}

impl Into<Message> for GetSessionChallenge {
    fn into(self) -> Message {
        let mut data = vec![0u8; 17];

        data[0] = self.auth_type.into();
        data[1..].copy_from_slice(&self.username);

        Message::new_request(NetFn::App, 0x39, data)
    }
}

impl IpmiCommand for GetSessionChallenge {
    type Output = SessionChallenge;

    type Error = AuthError;

    fn parse_response(
        completion_code: CompletionCode,
        data: &[u8],
    ) -> Result<Self::Output, ParseResponseError<Self::Error>> {
        Self::check_cc_success(completion_code)?;

        if data.len() != 20 {
            return Err(ParseResponseError::NotEnoughData);
        }

        let temporary_session_id =
            NonZeroU32::try_from(u32::from_le_bytes(data[0..4].try_into().unwrap()))
                .map_err(|_| AuthError::InvalidZeroSession)?;

        let challenge_string = data[4..20].try_into().unwrap();

        Ok(SessionChallenge {
            temporary_session_id,
            challenge_string,
        })
    }
}