Skip to content

Commit ce22f28

Browse files
Store and update certificate URIs in the registration chain
1 parent 0b680c7 commit ce22f28

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use pallas::ledger::addresses::Address;
1313
/// See the [proposal] for more details.
1414
///
1515
/// [proposal]: https://github.com/cardano-foundation/CIPs/pull/888
16-
#[derive(Debug, Eq, PartialEq)]
16+
#[derive(Debug, Clone, Eq, PartialEq)]
1717
#[allow(clippy::module_name_repetitions)]
1818
pub struct Cip0134Uri {
1919
/// A URI string.

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ use x509_cert::der::oid::db::rfc5912::ID_CE_SUBJECT_ALT_NAME;
1414

1515
use crate::{
1616
cardano::cip509::{
17-
rbac::certs::{C509Cert, X509DerCert},
17+
rbac::{
18+
certs::{C509Cert, X509DerCert},
19+
Cip509RbacMetadata,
20+
},
1821
utils::Cip0134Uri,
1922
validation::URI,
2023
},
@@ -33,7 +36,7 @@ type UrisMap = HashMap<usize, Box<[Cip0134Uri]>>;
3336
pub struct Cip0134UriSet(Arc<Cip0134UriSetInner>);
3437

3538
/// Internal `Cip0134UriSet` data.
36-
#[derive(Debug, Eq, PartialEq)]
39+
#[derive(Debug, Clone, Eq, PartialEq)]
3740
struct Cip0134UriSetInner {
3841
/// URIs from x509 certificates.
3942
x_uris: UrisMap,
@@ -71,6 +74,74 @@ impl Cip0134UriSet {
7174
pub fn is_empty(&self) -> bool {
7275
self.x_uris().is_empty() && self.c_uris().is_empty()
7376
}
77+
78+
/// Return the updated URIs set.
79+
///
80+
/// The resulting set includes all the data from both the original and a new one. In
81+
/// the following example for brevity we only consider ony type of uris:
82+
/// ```text
83+
/// // Original data:
84+
/// 0: [uri_1]
85+
/// 1: [uri_2, uri_3]
86+
///
87+
/// // New data:
88+
/// 0: undefined
89+
/// 1: deleted
90+
/// 2: [uri_4]
91+
///
92+
/// // Resulting data:
93+
/// 0: [uri_1]
94+
/// 2: [uri_4]
95+
/// ```
96+
#[must_use]
97+
pub fn update(self, metadata: &Cip509RbacMetadata) -> Self {
98+
if self == metadata.certificate_uris {
99+
// Nothing to update.
100+
return self;
101+
}
102+
103+
let Cip0134UriSetInner {
104+
mut x_uris,
105+
mut c_uris,
106+
} = Arc::unwrap_or_clone(self.0);
107+
108+
for (index, cert) in metadata.x509_certs.iter().enumerate() {
109+
match cert {
110+
X509DerCert::Undefined => {
111+
// The certificate wasn't changed - there is nothing to do.
112+
},
113+
X509DerCert::Deleted => {
114+
x_uris.remove(&index);
115+
},
116+
X509DerCert::X509Cert(_) => {
117+
if let Some(uris) = metadata.certificate_uris.x_uris().get(&index) {
118+
x_uris.insert(index, uris.clone());
119+
}
120+
},
121+
}
122+
}
123+
124+
for (index, cert) in metadata.c509_certs.iter().enumerate() {
125+
match cert {
126+
C509Cert::Undefined => {
127+
// The certificate wasn't changed - there is nothing to do.
128+
},
129+
C509Cert::Deleted => {
130+
c_uris.remove(&index);
131+
},
132+
C509Cert::C509CertInMetadatumReference(_) => {
133+
debug!("Ignoring unsupported metadatum reference");
134+
},
135+
C509Cert::C509Certificate(_) => {
136+
if let Some(uris) = metadata.certificate_uris.c_uris().get(&index) {
137+
c_uris.insert(index, uris.clone());
138+
}
139+
},
140+
}
141+
}
142+
143+
Self(Arc::new(Cip0134UriSetInner { x_uris, c_uris }))
144+
}
74145
}
75146

76147
/// Iterates over X509 certificates and extracts CIP-0134 URIs.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
pub_key::SimplePublicKeyType,
3333
},
3434
types::cert_key_hash::CertKeyHash,
35+
utils::Cip0134UriSet,
3536
Cip509, Cip509Validation,
3637
},
3738
utils::general::decremented_index,
@@ -151,6 +152,8 @@ struct RegistrationChainInner {
151152
x509_certs: HashMap<usize, (PointTxIdx, X509Certificate)>,
152153
/// Map of index in array to point, transaction index, and c509 certificate.
153154
c509_certs: HashMap<usize, (PointTxIdx, C509)>,
155+
/// A set of URIs contained in both x509 and c509 certificates.
156+
certificate_uris: Cip0134UriSet,
154157
/// Map of index in array to point, transaction index, and public key.
155158
simple_keys: HashMap<usize, (PointTxIdx, VerifyingKey)>,
156159
/// List of point, transaction index, and certificate key hash.
@@ -202,6 +205,7 @@ impl RegistrationChainInner {
202205
let registration = cip509.metadata;
203206
let point_tx_idx = PointTxIdx::new(point, tx_idx);
204207

208+
let certificate_uris = registration.certificate_uris;
205209
let x509_cert_map = chain_root_x509_certs(registration.x509_certs, &point_tx_idx);
206210
let c509_cert_map = chain_root_c509_certs(registration.c509_certs, &point_tx_idx);
207211
let public_key_map = chain_root_public_keys(registration.pub_keys, &point_tx_idx);
@@ -221,6 +225,7 @@ impl RegistrationChainInner {
221225
current_tx_id_hash: txn.hash(),
222226
x509_certs: x509_cert_map,
223227
c509_certs: c509_cert_map,
228+
certificate_uris,
224229
simple_keys: public_key_map,
225230
revocations,
226231
role_data: role_data_map,
@@ -272,7 +277,7 @@ impl RegistrationChainInner {
272277

273278
let registration = cip509.metadata;
274279
let point_tx_idx = PointTxIdx::new(point, tx_idx);
275-
280+
new_inner.certificate_uris = new_inner.certificate_uris.update(&registration);
276281
update_x509_certs(&mut new_inner, registration.x509_certs, &point_tx_idx);
277282
update_c509_certs(&mut new_inner, registration.c509_certs, &point_tx_idx)?;
278283
update_public_keys(&mut new_inner, registration.pub_keys, &point_tx_idx);

0 commit comments

Comments
 (0)