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

Commit 0ca0c3f

Browse files
committed
feat: validate cert before returning, fmt changes
chore: nightly cargo fmt for neater formatting
1 parent 639ffca commit 0ca0c3f

File tree

17 files changed

+58
-82
lines changed

17 files changed

+58
-82
lines changed

src/api/cacheable_cert.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
use bigdecimal::num_bigint::BigUint;
65
use der::{Any, DecodePem};
7-
use log::{debug, trace};
6+
use log::debug;
87
use spki::AlgorithmIdentifier;
98

109
use crate::Constrained;
1110
use crate::certs::Target;
1211
use crate::certs::idcert::IdCert;
1312
use crate::key::PublicKey;
1413
use crate::signature::Signature;
15-
use crate::types::der::asn1::Uint;
1614

1715
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1816
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

src/api/core/federated_identity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod registration_required {
2727
.send()
2828
.await;
2929
let pem = HttpClient::handle_response::<String>(request_response).await?;
30-
log::debug!("Received IdCert: \n{}", pem);
30+
log::debug!("Received IdCert: \n{pem}");
3131
let id_cert = IdCert::<S, T::PublicKey>::from_pem_unchecked(&pem)?;
3232
match id_cert.full_verify_home_server(
3333
std::time::SystemTime::now()
@@ -184,7 +184,7 @@ mod registration_not_required {
184184
request = request.body(json!({ "timestamp": time }).to_string());
185185
}
186186
let response = request.send().await;
187-
trace!("Got response: {:?}", response);
187+
trace!("Got response: {response:?}");
188188
let id_cert = HttpClient::handle_response::<CacheableIdCert>(response).await?;
189189
Ok(id_cert)
190190
}

src/api/core/migration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ mod registration_not_required {
204204
Ok(text) => from_str::<Vec<KeyTrialResponse>>(&text)
205205
.map_err(RequestError::DeserializationError),
206206
Err(e) => Err(RequestError::Custom {
207-
reason: format!("Could not get the full response text: {}", e),
207+
reason: format!("Could not get the full response text: {e}"),
208208
}),
209209
}
210210
}

src/certs/capabilities/basic_constraints.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl TryFrom<Extension> for BasicConstraints {
187187
/// these resulting [BasicConstraints].
188188
fn try_from(value: Extension) -> Result<Self, Self::Error> {
189189
trace!("Converting Extension to BasicConstraints");
190-
trace!("Extension: {:#?}", value);
190+
trace!("Extension: {value:#?}");
191191
#[allow(unreachable_patterns)]
192192
if value.critical && !matches!(value.extn_id.to_string().as_str(), OID_BASIC_CONSTRAINTS) {
193193
// Error if we encounter a "critical" X.509 extension which we do not know of
@@ -242,8 +242,7 @@ impl TryFrom<Extension> for BasicConstraints {
242242
}
243243
if bool_encounters > 1 || int_encounters > 1 || null_encounters > 1 {
244244
warn!(
245-
"Encountered too many values in BasicConstraints. BasicConstraints are likely malformed. BasicConstraints: {:#?}",
246-
value
245+
"Encountered too many values in BasicConstraints. BasicConstraints are likely malformed. BasicConstraints: {value:#?}"
247246
);
248247
return Err(CertificateConversionError::InvalidInput(
249248
InvalidInput::Length {

src/certs/capabilities/key_usage.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl KeyUsages {
9090
/// ```
9191
pub fn from_bitstring(bitstring: BitString) -> Result<Self, CertificateConversionError> {
9292
let mut byte_array = bitstring.raw_bytes().to_vec();
93-
log::trace!("[from_bitstring] BitString raw bytes: {:?}", byte_array);
93+
log::trace!("[from_bitstring] BitString raw bytes: {byte_array:?}");
9494
let mut key_usages = Vec::new();
9595
if byte_array == [0] || byte_array.is_empty() {
9696
// TODO: PLEASE write a test for this. Is an empty byte array valid? Is a byte array with a single 0 valid, and does it mean that no KeyUsage is set? -bitfl0wer
@@ -135,7 +135,7 @@ impl KeyUsages {
135135
"Could not properly convert this BitString to KeyUsages. The BitString contains a value not representable by KeyUsages".to_string(),
136136
)));
137137
}
138-
log::debug!("[from_bitstring] Converted KeyUsages: {:?}", key_usages);
138+
log::debug!("[from_bitstring] Converted KeyUsages: {key_usages:?}");
139139
Ok(KeyUsages { key_usages })
140140
}
141141

@@ -189,8 +189,8 @@ impl KeyUsages {
189189
// bits.
190190
unused_bits = 7;
191191
}
192-
log::debug!("[to_bitstring] Unused bits: {}", unused_bits);
193-
log::debug!("[to_bitstring] Encoded values: {:?}", encoded_numbers_vec);
192+
log::debug!("[to_bitstring] Unused bits: {unused_bits}");
193+
log::debug!("[to_bitstring] Encoded values: {encoded_numbers_vec:?}");
194194
BitString::new(unused_bits, encoded_numbers_vec)
195195
.expect("Error when converting KeyUsages to BitString. Please report this error to https://github.com/polyphony-chat/polyproto")
196196
}
@@ -225,7 +225,7 @@ impl TryFrom<Attribute> for KeyUsages {
225225
}
226226
};
227227
let inner_value = value.values.get(0).expect("Illegal state. Please report this error to https://github.com/polyphony-chat/polyproto");
228-
log::debug!("Inner value: {:?}", inner_value);
228+
log::debug!("Inner value: {inner_value:?}");
229229
KeyUsages::from_bitstring(BitString::from_der(&inner_value.to_der()?)?)
230230
}
231231
}

src/certs/idcert.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
use der::asn1::Uint;
65
use der::pem::LineEnding;
76
use der::{Decode, DecodePem, Encode, EncodePem};
87
use x509_cert::Certificate;
@@ -132,7 +131,7 @@ impl<S: Signature, P: PublicKey<S>> IdCert<S, P> {
132131
log::trace!("[IdCert::from_actor_csr()] creating actor certificate");
133132
let signature_algorithm = signing_key.algorithm_identifier();
134133
log::trace!("[IdCert::from_actor_csr()] creating IdCertTbs");
135-
log::trace!("[IdCert::from_actor_csr()] Issuer: {}", issuer);
134+
log::trace!("[IdCert::from_actor_csr()] Issuer: {issuer}");
136135
log::trace!(
137136
"[IdCert::from_actor_csr()] Subject: {}",
138137
id_csr.inner_csr.subject
@@ -293,8 +292,7 @@ impl<S: Signature, P: PublicKey<S>> IdCert<S, P> {
293292
Ok(der) => der,
294293
Err(_) => {
295294
log::warn!(
296-
"[IdCert::full_verify_actor(&self)] {}",
297-
ERR_CERTIFICATE_TO_DER_ERROR
295+
"[IdCert::full_verify_actor(&self)] {ERR_CERTIFICATE_TO_DER_ERROR}"
298296
);
299297
return Err(InvalidCert::InvalidProperties(ConstraintError::Malformed(
300298
Some(ERR_CERTIFICATE_TO_DER_ERROR.to_string()),
@@ -327,8 +325,7 @@ impl<S: Signature, P: PublicKey<S>> IdCert<S, P> {
327325
Ok(data) => data,
328326
Err(_) => {
329327
log::warn!(
330-
"[IdCert::full_verify_home_server(&self)] {}",
331-
ERR_CERTIFICATE_TO_DER_ERROR
328+
"[IdCert::full_verify_home_server(&self)] {ERR_CERTIFICATE_TO_DER_ERROR}"
332329
);
333330
return Err(InvalidCert::InvalidProperties(ConstraintError::Malformed(
334331
Some(ERR_CERTIFICATE_TO_DER_ERROR.to_string()),

src/certs/idcerttbs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn rdns_to_url(rdn_sequence: &RdnSequence) -> Result<url::Url, url::ParseError>
223223
url_str += ".";
224224
}
225225
let _ = url_str.pop();
226-
trace!(r#"Trying to parse string "{}" as url::Url..."#, url_str);
226+
trace!(r#"Trying to parse string "{url_str}" as url::Url..."#);
227227
Url::parse(url_str.trim())
228228
}
229229

@@ -256,7 +256,7 @@ impl<P: Profile, S: Signature, Q: PublicKey<S>> TryFrom<TbsCertificateInner<P>>
256256
value.serial_number.as_bytes(),
257257
)?);
258258

259-
Ok(Self {
259+
let id_cert_tbs = Self {
260260
serial_number,
261261
signature_algorithm: value.signature,
262262
issuer: value.issuer,
@@ -265,7 +265,10 @@ impl<P: Profile, S: Signature, Q: PublicKey<S>> TryFrom<TbsCertificateInner<P>>
265265
subject_public_key: subject_public_key_info,
266266
capabilities,
267267
s: std::marker::PhantomData,
268-
})
268+
};
269+
id_cert_tbs.validate(None)?;
270+
271+
Ok(id_cert_tbs)
269272
}
270273
}
271274

@@ -282,8 +285,7 @@ impl<P: Profile, S: Signature, Q: PublicKey<S>> TryFrom<IdCertTbs<S, Q>>
282285
Err(e) => {
283286
return Err(CertificateConversionError::InvalidInput(
284287
crate::errors::base::InvalidInput::Malformed(format!(
285-
"Could not convert serial number: {}",
286-
e
288+
"Could not convert serial number: {e}"
287289
)),
288290
));
289291
}

src/certs/idcsr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<S: Signature, P: PublicKey<S>> IdCsr<S, P> {
8484
signature_algorithm,
8585
signature,
8686
};
87-
log::trace!("[IdCsr::new()] Validating self with Target: {:?}", target);
87+
log::trace!("[IdCsr::new()] Validating self with Target: {target:?}");
8888
id_csr.validate(target)?;
8989
Ok(id_csr)
9090
}

src/certs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl SessionId {
6666

6767
/// Converts this [SessionId] into a [Name] for use in a certificate.
6868
pub fn to_rdn_sequence(&self) -> Name {
69-
RdnSequence::from_str(&format!("uniqueIdentifier={}", self)).unwrap()
69+
RdnSequence::from_str(&format!("uniqueIdentifier={self}")).unwrap()
7070
}
7171

7272
/// Returns the inner [Ia5String] of this [SessionId] as an owned value.

src/constraints/capabilities.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ impl Constrained for Capabilities {
6565
}
6666
if !key_cert_sign {
6767
return Err(ConstraintError::Malformed(Some(format!(
68-
"{} Missing capability \"KeyCertSign\"",
69-
ERR_MSG_HOME_SERVER_MISSING_CA_ATTR
68+
"{ERR_MSG_HOME_SERVER_MISSING_CA_ATTR} Missing capability \"KeyCertSign\""
7069
))));
7170
}
7271
}

0 commit comments

Comments
 (0)