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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::collections::HashMap;
use lber::structure::{StructureTag, PL};
use lber::structures::{ASNTag, Boolean, OctetString, Sequence, Tag};
use lber::universal::Types;
use lazy_static::lazy_static;
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub enum ControlType {
PagedResults,
PostReadResp,
PreReadResp,
SyncDone,
SyncState,
ManageDsaIt,
MatchedValues,
}
mod assertion;
pub use self::assertion::Assertion;
mod content_sync;
pub use self::content_sync::parse_syncinfo;
pub use self::content_sync::{EntryState, RefreshMode, SyncDone, SyncInfo, SyncRequest, SyncState};
mod paged_results;
pub use self::paged_results::PagedResults;
mod proxy_auth;
pub use self::proxy_auth::ProxyAuth;
mod read_entry;
pub use self::read_entry::{PostRead, PostReadResp, PreRead, PreReadResp, ReadEntryResp};
mod relax_rules;
pub use self::relax_rules::RelaxRules;
mod manage_dsa_it;
pub use self::manage_dsa_it::ManageDsaIt;
mod matched_values;
pub use self::matched_values::MatchedValues;
#[rustfmt::skip]
lazy_static! {
static ref CONTROLS: HashMap<&'static str, ControlType> = {
let mut map = HashMap::new();
map.insert(self::paged_results::PAGED_RESULTS_OID, ControlType::PagedResults);
map.insert(self::read_entry::POST_READ_OID, ControlType::PostReadResp);
map.insert(self::read_entry::PRE_READ_OID, ControlType::PreReadResp);
map.insert(self::content_sync::SYNC_DONE_OID, ControlType::SyncDone);
map.insert(self::content_sync::SYNC_STATE_OID, ControlType::SyncState);
map.insert(self::manage_dsa_it::MANAGE_DSA_IT_OID, ControlType::ManageDsaIt);
map.insert(self::matched_values::MATCHED_VALUES_OID, ControlType::MatchedValues);
map
};
}
pub trait IntoRawControlVec {
fn into(self) -> Vec<RawControl>;
}
impl IntoRawControlVec for Vec<RawControl> {
fn into(self) -> Vec<RawControl> {
self
}
}
impl<R> IntoRawControlVec for R
where
RawControl: From<R>,
{
fn into(self) -> Vec<RawControl> {
vec![std::convert::Into::into(self)]
}
}
pub trait MakeCritical {
fn critical(self) -> CriticalControl<Self>
where
Self: Sized,
{
CriticalControl { control: self }
}
}
pub struct CriticalControl<T> {
control: T,
}
impl<T> From<CriticalControl<T>> for RawControl
where
T: Into<RawControl>,
{
fn from(cc: CriticalControl<T>) -> RawControl {
let mut rc = cc.control.into();
rc.crit = true;
rc
}
}
pub trait ControlParser {
fn parse(val: &[u8]) -> Self;
}
#[derive(Clone, Debug)]
pub struct Control(pub Option<ControlType>, pub RawControl);
#[derive(Clone, Debug)]
pub struct RawControl {
pub ctype: String,
pub crit: bool,
pub val: Option<Vec<u8>>,
}
impl RawControl {
pub fn parse<T: ControlParser>(&self) -> T {
T::parse(self.val.as_ref().expect("value"))
}
}
pub fn build_tag(rc: RawControl) -> StructureTag {
let mut seq = vec![Tag::OctetString(OctetString {
inner: Vec::from(rc.ctype.as_bytes()),
..Default::default()
})];
if rc.crit {
seq.push(Tag::Boolean(Boolean {
inner: true,
..Default::default()
}));
}
if let Some(val) = rc.val {
seq.push(Tag::OctetString(OctetString {
inner: val,
..Default::default()
}));
}
Tag::Sequence(Sequence {
inner: seq,
..Default::default()
})
.into_structure()
}
pub fn parse_controls(t: StructureTag) -> Vec<Control> {
let tags = t.expect_constructed().expect("result sequence").into_iter();
let mut ctrls = Vec::new();
for ctrl in tags {
let mut components = ctrl.expect_constructed().expect("components").into_iter();
let ctype = String::from_utf8(
components
.next()
.expect("element")
.expect_primitive()
.expect("octet string"),
)
.expect("control type");
let next = components.next();
let (crit, maybe_val) = match next {
None => (false, None),
Some(c) => match c {
StructureTag {
id, ref payload, ..
} if id == Types::Boolean as u64 => match *payload {
PL::P(ref v) => (v[0] != 0, components.next()),
PL::C(_) => panic!("decoding error"),
},
StructureTag { id, .. } if id == Types::OctetString as u64 => {
(false, Some(c.clone()))
}
_ => panic!("decoding error"),
},
};
let val = match maybe_val {
None => None,
Some(v) => Some(v.expect_primitive().expect("octet string")),
};
let known_type = match CONTROLS.get(&*ctype) {
Some(val) => Some(*val),
None => None,
};
ctrls.push(Control(known_type, RawControl { ctype, crit, val }));
}
ctrls
}