-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkey_data.rs
More file actions
190 lines (175 loc) · 5.5 KB
/
key_data.rs
File metadata and controls
190 lines (175 loc) · 5.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Kadm5 [`KeyData`]
use std::{ffi::c_char, ptr::null_mut};
use kadmin_sys::*;
#[cfg(feature = "python")]
use pyo3::prelude::*;
use crate::{EncryptionType, SaltType, error::Result};
/// Kerberos data
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
pub struct Krb5Data {
/// Magic number
pub magic: krb5_magic,
/// Data
pub data: Vec<c_char>,
}
impl Krb5Data {
pub(crate) fn from_raw(data: &krb5_data) -> Self {
dbg!(data);
Self {
magic: data.magic,
data: unsafe { std::slice::from_raw_parts(data.data, data.length as usize) }.to_vec(),
}
}
}
/// Salt associated with a key
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
pub struct KeyDataSalt {
/// Salt type
pub r#type: SaltType,
/// Data
pub data: Krb5Data,
}
impl KeyDataSalt {
pub(crate) fn from_raw(keysalt: &krb5_keysalt) -> Result<Option<Self>> {
if keysalt.type_ == 0 {
return Ok(None);
}
Ok(Some(Self {
r#type: keysalt.type_.try_into()?,
data: Krb5Data::from_raw(&keysalt.data),
}))
}
}
/// Key block
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
pub struct KeyDataBlock {
/// Magic number
pub magic: krb5_magic,
/// Encryption type
pub enctype: EncryptionType,
/// Data
pub contents: Vec<krb5_octet>,
}
impl KeyDataBlock {
pub(crate) fn from_raw(block: &krb5_keyblock) -> Result<Self> {
Ok(Self {
magic: block.magic,
enctype: block.enctype.try_into()?,
contents: unsafe { std::slice::from_raw_parts(block.contents, block.length as usize) }
.to_vec(),
})
}
}
/// A single key data entry
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
pub struct KeyDataEntry {
/// Key version number
pub kvno: krb5_kvno,
/// Key
pub key: KeyDataBlock,
/// Salt
pub salt: Option<KeyDataSalt>,
}
/// Key data entries
#[derive(Clone, Debug)]
#[allow(clippy::exhaustive_structs)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
pub struct KeyData {
/// Key data entries
pub entries: Vec<KeyDataEntry>,
}
impl KeyData {
pub(crate) fn from_raw(n_key_data: krb5_int32, key_data: *mut kadm5_key_data) -> Result<Self> {
dbg!(n_key_data);
dbg!(key_data);
let raw_entries = unsafe { std::slice::from_raw_parts(key_data, n_key_data as usize) };
let mut entries = Vec::with_capacity(n_key_data as usize);
for raw_entry in raw_entries {
// We've checked above that the pointer is not null
entries.push(KeyDataEntry {
kvno: raw_entry.kvno,
key: KeyDataBlock::from_raw(&raw_entry.key)?,
salt: KeyDataSalt::from_raw(&raw_entry.salt)?,
});
}
Ok(Self { entries })
}
}
// impl TlData {
// /// Create a [`TlData`] from [`_krb5_tl_data`]
// pub(crate) fn from_raw(n_tl_data: krb5_int16, mut tl_data: *mut _krb5_tl_data) -> Self {
// let mut entries = Vec::with_capacity(n_tl_data as usize);
//
// while !tl_data.is_null() {
// // We've checked above that the pointer is not null
// let data_type = unsafe { (*tl_data).tl_data_type };
// let contents_length = unsafe { (*tl_data).tl_data_length };
// let contents = unsafe {
// std::slice::from_raw_parts((*tl_data).tl_data_contents, contents_length.into())
// }
// .to_vec();
// entries.push(TlDataEntry {
// data_type,
// contents,
// });
// tl_data = unsafe { (*tl_data).tl_data_next };
// }
//
// Self { entries }
// }
//
// /// Create a [`_krb5_tl_data`] from [`TlData`]
// ///
// /// Returns None if there are not TL-data.
// pub(crate) fn to_raw(&self) -> TlDataRaw {
// if self.entries.is_empty() {
// return TlDataRaw {
// raw: null_mut(),
// _raw_entries: vec![],
// _raw_contents: vec![],
// };
// }
//
// let mut raw_contents = Vec::new();
// let mut raw_entries: Vec<_> = self
// .entries
// .iter()
// .map(|entry| {
// let contents = entry.contents.clone();
// let data = _krb5_tl_data {
// tl_data_type: entry.data_type,
// tl_data_length: entry.contents.len() as krb5_ui_2,
// tl_data_contents: contents.as_ptr().cast_mut(),
// tl_data_next: null_mut(),
// };
// raw_contents.push(contents);
// data
// })
// .collect();
//
// for i in 1..raw_entries.len() {
// raw_entries[i - 1].tl_data_next = &mut raw_entries[i];
// }
//
// TlDataRaw {
// raw: raw_entries.as_mut_ptr(),
// _raw_entries: raw_entries,
// _raw_contents: raw_contents,
// }
// }
// }
//
// #[derive(Debug)]
// pub(crate) struct TlDataRaw {
// pub(crate) raw: *mut krb5_tl_data,
// pub(crate) _raw_entries: Vec<_krb5_tl_data>,
// pub(crate) _raw_contents: Vec<Vec<krb5_octet>>,
// }