1use serde_json::value::Value;
2use serde::{Deserialize, Serialize};
3use x509_parser::oid_registry::asn1_rs::oid;
4use x509_parser::prelude::*;
5
6use crate::enums::{decode_guid_le, parse_ntsecuritydescriptor};
7use crate::utils::date::string_to_epoch;
8use crate::objects::common::{
9 LdapObject,
10 AceTemplate,
11 SPNTarget,
12 Link,
13 Member
14};
15use crate::utils::crypto::calculate_sha1;
16
17use ldap3::SearchEntry;
18use log::{debug, error, trace};
19use std::collections::HashMap;
20use std::error::Error;
21
22#[derive(Debug, Clone, Deserialize, Serialize, Default)]
24pub struct RootCA {
25 #[serde(rename = "Properties")]
26 properties: RootCAProperties,
27 #[serde(rename = "DomainSID")]
28 domain_sid: String,
29 #[serde(rename = "Aces")]
30 aces: Vec<AceTemplate>,
31 #[serde(rename = "ObjectIdentifier")]
32 object_identifier: String,
33 #[serde(rename = "IsDeleted")]
34 is_deleted: bool,
35 #[serde(rename = "IsACLProtected")]
36 is_acl_protected: bool,
37 #[serde(rename = "ContainedBy")]
38 contained_by: Option<Member>,
39}
40
41impl RootCA {
42 pub fn new() -> Self {
44 Self { ..Default::default() }
45 }
46
47 pub fn parse(
49 &mut self,
50 result: SearchEntry,
51 domain: &String,
52 dn_sid: &mut HashMap<String, String>,
53 sid_type: &mut HashMap<String, String>,
54 domain_sid: &String
55 ) -> Result<(), Box<dyn Error>> {
56 let result_dn: String = result.dn.to_uppercase();
57 let result_attrs: HashMap<String, Vec<String>> = result.attrs;
58 let result_bin: HashMap<String, Vec<Vec<u8>>> = result.bin_attrs;
59
60 debug!("Parse RootCA: {}", result_dn);
62 for (key, value) in &result_attrs {
64 trace!(" {:?}:{:?}", key, value);
65 }
66 for (key, value) in &result_bin {
68 trace!(" {:?}:{:?}", key, value);
69 }
70
71 self.properties.domain = domain.to_uppercase();
73 self.properties.distinguishedname = result_dn;
74 self.properties.domainsid = domain_sid.to_string();
75 self.domain_sid = domain_sid.to_string();
76
77 for (key, value) in &result_attrs {
79 match key.as_str() {
80 "name" => {
81 let name = format!("{}@{}", &value[0], domain);
82 self.properties.name = name.to_uppercase();
83 }
84 "description" => {
85 self.properties.description = value.get(0).map(|s| s.clone());
86 }
87 "whenCreated" => {
88 let epoch = string_to_epoch(&value[0])?;
89 if epoch.is_positive() {
90 self.properties.whencreated = epoch;
91 }
92 }
93 "IsDeleted" => {
94 self.is_deleted = true.into();
95 }
96 _ => {}
97 }
98 }
99
100 for (key, value) in &result_bin {
102 match key.as_str() {
103 "objectGUID" => {
104 self.object_identifier = decode_guid_le(&value[0]).to_owned().into();
106 }
107 "nTSecurityDescriptor" => {
108 let entry_type = "RootCA".to_string();
110 let relations_ace = parse_ntsecuritydescriptor(
112 self,
113 &value[0],
114 entry_type,
115 &result_attrs,
116 &result_bin,
117 &domain,
118 );
119 self.aces = relations_ace;
120 }
121 "cACertificate" => {
122 let certsha1: String = calculate_sha1(&value[0]);
124 self.properties.certthumbprint = certsha1.to_string();
125 self.properties.certname = certsha1.to_string();
126 self.properties.certchain = vec![certsha1.to_string()];
127
128 let res = X509Certificate::from_der(&value[0]);
130 match res {
131 Ok((_rem, cert)) => {
132 for ext in cert.extensions() {
134 if &ext.oid == &oid!(2.5.29.19) {
136 if let ParsedExtension::BasicConstraints(basic_constraints) = &ext.parsed_extension() {
138 let _ca = &basic_constraints.ca;
139 let _path_len_constraint = &basic_constraints.path_len_constraint;
140 match _path_len_constraint {
143 Some(_path_len_constraint) => {
144 if _path_len_constraint > &0 {
145 self.properties.hasbasicconstraints = true;
146 self.properties.basicconstraintpathlength = _path_len_constraint.to_owned();
147
148 } else {
149 self.properties.hasbasicconstraints = false;
150 self.properties.basicconstraintpathlength = 0 as u32;
151 }
152 },
153 None => {
154 self.properties.hasbasicconstraints = false;
155 self.properties.basicconstraintpathlength = 0 as u32;
156 }
157 }
158 }
159 }
160 }
161 },
162 _ => error!("CA x509 certificate parsing failed: {:?}", res),
163 }
164 }
165 _ => {}
166 }
167 }
168
169 if self.object_identifier.to_string() != "SID" {
171 dn_sid.insert(
172 self.properties.distinguishedname.to_string(),
173 self.object_identifier.to_string()
174 );
175 sid_type.insert(
177 self.object_identifier.to_string(),
178 "RootCA".to_string()
179 );
180 }
181
182 Ok(())
185 }
186}
187
188impl LdapObject for RootCA {
189 fn to_json(&self) -> Value {
191 serde_json::to_value(&self).unwrap()
192 }
193
194 fn get_object_identifier(&self) -> &String {
196 &self.object_identifier
197 }
198 fn get_is_acl_protected(&self) -> &bool {
199 &self.is_acl_protected
200 }
201 fn get_aces(&self) -> &Vec<AceTemplate> {
202 &self.aces
203 }
204 fn get_spntargets(&self) -> &Vec<SPNTarget> {
205 panic!("Not used by current object.");
206 }
207 fn get_allowed_to_delegate(&self) -> &Vec<Member> {
208 panic!("Not used by current object.");
209 }
210 fn get_links(&self) -> &Vec<Link> {
211 panic!("Not used by current object.");
212 }
213 fn get_contained_by(&self) -> &Option<Member> {
214 &self.contained_by
215 }
216 fn get_child_objects(&self) -> &Vec<Member> {
217 panic!("Not used by current object.");
218 }
219 fn get_haslaps(&self) -> &bool {
220 &false
221 }
222
223 fn get_aces_mut(&mut self) -> &mut Vec<AceTemplate> {
225 &mut self.aces
226 }
227 fn get_spntargets_mut(&mut self) -> &mut Vec<SPNTarget> {
228 panic!("Not used by current object.");
229 }
230 fn get_allowed_to_delegate_mut(&mut self) -> &mut Vec<Member> {
231 panic!("Not used by current object.");
232 }
233
234 fn set_is_acl_protected(&mut self, is_acl_protected: bool) {
236 self.is_acl_protected = is_acl_protected;
237 self.properties.isaclprotected = is_acl_protected;
238 }
239 fn set_aces(&mut self, aces: Vec<AceTemplate>) {
240 self.aces = aces;
241 }
242 fn set_spntargets(&mut self, _spn_targets: Vec<SPNTarget>) {
243 }
245 fn set_allowed_to_delegate(&mut self, _allowed_to_delegate: Vec<Member>) {
246 }
248 fn set_links(&mut self, _links: Vec<Link>) {
249 }
251 fn set_contained_by(&mut self, contained_by: Option<Member>) {
252 self.contained_by = contained_by;
253 }
254 fn set_child_objects(&mut self, _child_objects: Vec<Member>) {
255 }
257}
258
259
260#[derive(Debug, Clone, Deserialize, Serialize)]
262pub struct RootCAProperties {
263 domain: String,
264 name: String,
265 distinguishedname: String,
266 domainsid: String,
267 isaclprotected: bool,
268 description: Option<String>,
269 whencreated: i64,
270 certthumbprint: String,
271 certname: String,
272 certchain: Vec<String>,
273 hasbasicconstraints: bool,
274 basicconstraintpathlength: u32,
275}
276
277impl Default for RootCAProperties {
278 fn default() -> RootCAProperties {
279 RootCAProperties {
280 domain: String::from(""),
281 name: String::from(""),
282 distinguishedname: String::from(""),
283 domainsid: String::from(""),
284 isaclprotected: false,
285 description: None,
286 whencreated: -1,
287 certthumbprint: String::from(""),
288 certname: String::from(""),
289 certchain: Vec::new(),
290 hasbasicconstraints: false,
291 basicconstraintpathlength: 0,
292 }
293 }
294}