Skip to content

Commit 0b680c7

Browse files
Store x509 certificates (Cip509RbacMetadata::x509_certs) in the decoded format (#128)
1 parent 6ad8cdc commit 0b680c7

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

rust/rbac-registration/src/cardano/cip509/rbac/certs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum X509DerCert {
1818
/// Deleted indicates the key is deleted.
1919
Deleted,
2020
/// X.509 certificate.
21-
X509Cert(Vec<u8>),
21+
X509Cert(Box<Certificate>),
2222
}
2323

2424
impl Decode<'_, ()> for X509DerCert {
@@ -34,9 +34,10 @@ impl Decode<'_, ()> for X509DerCert {
3434
minicbor::data::Type::Undefined => Ok(Self::Undefined),
3535
minicbor::data::Type::Bytes => {
3636
let data = decode_bytes(d, "X509DerCert")?;
37-
Certificate::from_der(&data)
38-
.map_err(|_| decode::Error::message("Invalid x509 certificate"))?;
39-
Ok(Self::X509Cert(data.clone()))
37+
let certificate = Certificate::from_der(&data).map_err(|e| {
38+
decode::Error::message(format!("Invalid x509 certificate: {e:?}"))
39+
})?;
40+
Ok(Self::X509Cert(Box::new(certificate)))
4041
},
4142
_ => Err(decode::Error::message("Invalid datatype for X509DerCert")),
4243
}

rust/rbac-registration/src/cardano/cip509/utils/cip134/uri_set.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use c509_certificate::{
1010
};
1111
use der_parser::der::parse_der_sequence;
1212
use tracing::debug;
13-
use x509_cert::der::{oid::db::rfc5912::ID_CE_SUBJECT_ALT_NAME, Decode};
13+
use x509_cert::der::oid::db::rfc5912::ID_CE_SUBJECT_ALT_NAME;
1414

1515
use crate::{
1616
cardano::cip509::{
@@ -81,8 +81,6 @@ fn extract_x509_uris(certificates: &[X509DerCert]) -> Result<UrisMap> {
8181
let X509DerCert::X509Cert(cert) = cert else {
8282
continue;
8383
};
84-
let cert = x509_cert::Certificate::from_der(cert)
85-
.with_context(|| "Failed to decode X509 certificate from DER")?;
8684
// Find the "subject alternative name" extension.
8785
let Some(extension) = cert
8886
.tbs_certificate

rust/rbac-registration/src/registration/cardano/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use point_tx_idx::PointTxIdx;
2222
use role_data::RoleData;
2323
use tracing::error;
2424
use uuid::Uuid;
25+
use x509_cert::certificate::Certificate as X509Certificate;
2526

2627
use crate::{
2728
cardano::cip509::{
@@ -102,7 +103,7 @@ impl RegistrationChain {
102103

103104
/// Get the map of index in array to point, transaction index, and x509 certificate.
104105
#[must_use]
105-
pub fn x509_certs(&self) -> &HashMap<usize, (PointTxIdx, Vec<u8>)> {
106+
pub fn x509_certs(&self) -> &HashMap<usize, (PointTxIdx, X509Certificate)> {
106107
&self.inner.x509_certs
107108
}
108109

@@ -147,7 +148,7 @@ struct RegistrationChainInner {
147148

148149
// RBAC
149150
/// Map of index in array to point, transaction index, and x509 certificate.
150-
x509_certs: HashMap<usize, (PointTxIdx, Vec<u8>)>,
151+
x509_certs: HashMap<usize, (PointTxIdx, X509Certificate)>,
151152
/// Map of index in array to point, transaction index, and c509 certificate.
152153
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
153154
/// Map of index in array to point, transaction index, and public key.
@@ -304,15 +305,18 @@ fn is_valid_cip509(validation_data: &Cip509Validation) -> bool {
304305
/// Process x509 certificate for chain root.
305306
fn chain_root_x509_certs(
306307
x509_certs: Vec<X509DerCert>, point_tx_idx: &PointTxIdx,
307-
) -> HashMap<usize, (PointTxIdx, Vec<u8>)> {
308-
let mut map = HashMap::new();
309-
for (idx, cert) in x509_certs.into_iter().enumerate() {
310-
// Chain root, expect only the certificate not undefined or delete
311-
if let X509DerCert::X509Cert(cert) = cert {
312-
map.insert(idx, (point_tx_idx.clone(), cert));
313-
}
314-
}
315-
map
308+
) -> HashMap<usize, (PointTxIdx, X509Certificate)> {
309+
x509_certs
310+
.into_iter()
311+
.enumerate()
312+
.filter_map(|(index, cert)| {
313+
if let X509DerCert::X509Cert(cert) = cert {
314+
Some((index, (point_tx_idx.clone(), *cert)))
315+
} else {
316+
None
317+
}
318+
})
319+
.collect()
316320
}
317321

318322
/// Update x509 certificates in the registration chain.
@@ -331,7 +335,7 @@ fn update_x509_certs(
331335
X509DerCert::X509Cert(cert) => {
332336
new_inner
333337
.x509_certs
334-
.insert(idx, (point_tx_idx.clone(), cert));
338+
.insert(idx, (point_tx_idx.clone(), *cert));
335339
},
336340
}
337341
}

0 commit comments

Comments
 (0)