Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Commit 5e1d89c

Browse files
committed
feat: add tryfrom name for homeserverdn
+ convenience functions, make fields pub
1 parent 94f18da commit 5e1d89c

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

src/types/pdn.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,29 @@ pub enum PolyprotoDistinguishedName {
2525
HomeServerDn(HomeServerDN),
2626
}
2727

28+
impl From<ActorDN> for PolyprotoDistinguishedName {
29+
fn from(value: ActorDN) -> Self {
30+
Self::ActorDn(value)
31+
}
32+
}
33+
34+
impl From<HomeServerDN> for PolyprotoDistinguishedName {
35+
fn from(value: HomeServerDN) -> Self {
36+
Self::HomeServerDn(value)
37+
}
38+
}
39+
2840
#[derive(Debug, Clone, PartialEq, Eq)]
2941
/// A [PolyprotoDistinguishedName] with all necessary fields for an actor certificate.
3042
///
3143
/// This struct is a higher-level abstraction of X.509 [distinguished names](https://ldap.com/ldap-dns-and-rdns/),
3244
/// providing easier access to inner values compared to using [x509_cert::name::Name] in a raw manner.
3345
pub struct ActorDN {
34-
federation_id: FederationId,
35-
local_name: LocalName,
36-
domain_name: DomainName,
37-
session_id: SessionId,
38-
additional_fields: RelativeDistinguishedName,
46+
pub federation_id: FederationId,
47+
pub local_name: LocalName,
48+
pub domain_name: DomainName,
49+
pub session_id: SessionId,
50+
pub additional_fields: RelativeDistinguishedName,
3951
}
4052

4153
impl Hash for ActorDN {
@@ -56,16 +68,17 @@ impl Hash for ActorDN {
5668
/// This struct is a higher-level abstraction of X.509 [distinguished names](https://ldap.com/ldap-dns-and-rdns/),
5769
/// providing easier access to inner values compared to using [x509_cert::name::Name] in a raw manner.
5870
pub struct HomeServerDN {
59-
domain_name: DomainName,
60-
additional_fields: Vec<RelativeDistinguishedName>,
71+
pub domain_name: DomainName,
72+
pub additional_fields: RelativeDistinguishedName,
6173
}
6274

6375
impl Hash for HomeServerDN {
6476
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
6577
self.domain_name.hash(state);
66-
self.additional_fields
67-
.iter()
68-
.for_each(|additional_field| additional_field.to_string().hash(state));
78+
self.additional_fields.0.iter().for_each(|item| {
79+
item.oid.hash(state);
80+
item.value.value().hash(state);
81+
});
6982
}
7083
}
7184

@@ -135,6 +148,33 @@ impl TryFrom<Name> for ActorDN {
135148
}
136149
}
137150

151+
impl TryFrom<Name> for HomeServerDN {
152+
type Error = crate::errors::InvalidInput;
153+
154+
fn try_from(x509_distinguished_name: Name) -> Result<Self, Self::Error> {
155+
x509_distinguished_name
156+
.validate(Some(crate::certs::Target::Actor))
157+
.map_err(|e| crate::errors::InvalidInput::Malformed(e.to_string()))?;
158+
let mut maybe_domain_names: Vec<AttributeTypeAndValue> = Vec::new();
159+
let mut maybe_additional_fields: Vec<AttributeTypeAndValue> = Vec::new();
160+
for relative_distinguished_name in x509_distinguished_name.0.into_iter() {
161+
for attribute_value_and_item in relative_distinguished_name.0.iter() {
162+
match attribute_value_and_item.oid {
163+
OID_RDN_DOMAIN_COMPONENT => {
164+
maybe_domain_names.push(attribute_value_and_item.clone())
165+
}
166+
_other => maybe_additional_fields.push(attribute_value_and_item.clone()),
167+
}
168+
}
169+
}
170+
let domain_name = DomainName::try_from(maybe_domain_names.as_slice())?;
171+
Ok(HomeServerDN {
172+
domain_name,
173+
additional_fields: RelativeDistinguishedName::try_from(maybe_additional_fields).map_err(|e| crate::errors::InvalidInput::Malformed(format!("Could not parse ActorDN additional_fields: Name attribute contained additional information which was not a valid RelativeDistinguishedName: {e}")))?,
174+
})
175+
}
176+
}
177+
138178
/// Helper function. Takes an exclusive reference `Option<AttributeTypeAndValue>`, inspects if it
139179
/// holds a value, and
140180
///

0 commit comments

Comments
 (0)