Debian Codegraph @

src/response/rpki.rs

raw
1 //! RDAP RPKI Extension (draft-ietf-regext-rdap-rpki)
2 //!
3 //! This module implements the RDAP extension for accessing RPKI registration data
4 //! including Route Origin Authorization (ROA), Autonomous System Provider Authorization (ASPA),
5 //! and X.509 Resource Certificate objects.
6 //!
7 //! See [draft-ietf-regext-rdap-rpki](https://www.ietf.org/archive/id/draft-ietf-regext-rdap-rpki-04.txt)
8
9 use std::collections::HashSet;
10
11 use crate::prelude::ContentExtensions;
12
13 use {
14 crate::prelude::{Common, Extension, ObjectCommon},
15 serde::{Deserialize, Serialize},
16 };
17
18 use super::{
19 CommonFields, Entity, Event, ExtensionId, GetSelfLink, Notice, Numberish, ObjectCommonFields,
20 Remark, SelfLink, ToChild, ToResponse, to_opt_vec, types::Link,
21 };
22
23 // Common data members for all RPKI objects (Section 3)
24
25 /// Represents a digest of an RPKI object.
26 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
27 pub struct Rpk1Digest {
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub digest: Option<String>,
30
31 #[serde(rename = "digestAlgorithm")]
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub digest_algorithm: Option<String>,
34 }
35
36 #[buildstructor::buildstructor]
37 impl Rpk1Digest {
38 #[builder(visibility = "pub")]
39 fn new(digest: String, digest_algorithm: String) -> Self {
40 Self {
41 digest: Some(digest),
42 digest_algorithm: Some(digest_algorithm),
43 }
44 }
45 }
46
47 /// ROA IP address block within a ROA.
48 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
49 pub struct Rpk1RoaIp {
50 #[serde(skip_serializing_if = "Option::is_none")]
51 pub ip: Option<String>,
52
53 #[serde(rename = "maxLength")]
54 #[serde(skip_serializing_if = "Option::is_none")]
55 pub max_length: Option<Numberish<u8>>,
56 }
57
58 #[buildstructor::buildstructor]
59 impl Rpk1RoaIp {
60 #[builder(visibility = "pub")]
61 fn new(ip: String, max_length: u8) -> Self {
62 Self {
63 ip: Some(ip),
64 max_length: Some(Numberish::<u8>::from(max_length)),
65 }
66 }
67 }
68
69 /// Subject public key information for X.509 certificates.
70 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
71 pub struct Rpk1SubjectPublicKeyInfo {
72 #[serde(rename = "publicKeyAlgorithm")]
73 #[serde(skip_serializing_if = "Option::is_none")]
74 pub public_key_algorithm: Option<String>,
75
76 #[serde(rename = "publicKey")]
77 #[serde(skip_serializing_if = "Option::is_none")]
78 pub public_key: Option<String>,
79 }
80
81 #[buildstructor::buildstructor]
82 impl Rpk1SubjectPublicKeyInfo {
83 #[builder(visibility = "pub")]
84 fn new(public_key_algorithm: String, public_key: String) -> Self {
85 Self {
86 public_key_algorithm: Some(public_key_algorithm),
87 public_key: Some(public_key),
88 }
89 }
90 }
91
92 /// RPKI type indicating the relationship between repository and CA.
93 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, strum::AsRefStr, strum::Display)]
94 #[serde(rename_all = "snake_case")]
95 pub enum Rpk1RpkiType {
96 Hosted,
97 Delegated,
98 Hybrid,
99 }
100
101 // ROA Object Class (Section 4.1)
102
103 /// Route Origin Authorization object.
104 ///
105 /// See [draft-ietf-regext-rdap-rpki Section 4.1](https://www.ietf.org/archive/id/draft-ietf-regext-rdap-rpki-04.html#section-4.1)
106 ///
107 /// ```rust
108 /// use icann_rdap_common::prelude::*;
109 ///
110 /// let roa = Rpk1Roa::response_obj()
111 /// .handle("ROA-1")
112 /// .roa_ip(Rpk1RoaIp::builder().ip("2001:db8::/48".to_string()).max_length(64).build())
113 /// .origin_autnum(65536)
114 /// .build();
115 /// ```
116 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
117 pub struct Rpk1Roa {
118 #[serde(flatten)]
119 pub common: Common,
120
121 #[serde(flatten)]
122 pub object_common: ObjectCommon,
123
124 #[serde(skip_serializing_if = "Option::is_none")]
125 pub name: Option<String>,
126
127 #[serde(skip_serializing_if = "Option::is_none")]
128 pub digests: Option<Vec<Rpk1Digest>>,
129
130 #[serde(rename = "notValidBefore")]
131 #[serde(skip_serializing_if = "Option::is_none")]
132 pub not_valid_before: Option<String>,
133
134 #[serde(rename = "notValidAfter")]
135 #[serde(skip_serializing_if = "Option::is_none")]
136 pub not_valid_after: Option<String>,
137
138 #[serde(rename = "publicationUri")]
139 #[serde(skip_serializing_if = "Option::is_none")]
140 pub publication_uri: Option<String>,
141
142 #[serde(rename = "notificationUri")]
143 #[serde(skip_serializing_if = "Option::is_none")]
144 pub notification_uri: Option<String>,
145
146 #[serde(rename = "rpkiType")]
147 #[serde(skip_serializing_if = "Option::is_none")]
148 pub rpki_type: Option<Rpk1RpkiType>,
149
150 #[serde(rename = "roaIps")]
151 #[serde(skip_serializing_if = "Option::is_none")]
152 pub roa_ips: Option<Vec<Rpk1RoaIp>>,
153
154 #[serde(rename = "originAutnum")]
155 #[serde(skip_serializing_if = "Option::is_none")]
156 pub origin_autnum: Option<Numberish<u32>>,
157 }
158
159 #[buildstructor::buildstructor]
160 impl Rpk1Roa {
161 /// Builds a basic ROA object for embedding.
162 #[builder(visibility = "pub")]
163 #[allow(clippy::too_many_arguments)]
164 fn new(
165 handle: Option<String>,
166 name: Option<String>,
167 digests: Vec<Rpk1Digest>,
168 not_valid_before: Option<String>,
169 not_valid_after: Option<String>,
170 publication_uri: Option<String>,
171 notification_uri: Option<String>,
172 entities: Vec<Entity>,
173 rpki_type: Option<Rpk1RpkiType>,
174 roa_ips: Vec<Rpk1RoaIp>,
175 origin_autnum: Option<u32>,
176 remarks: Vec<Remark>,
177 links: Vec<Link>,
178 events: Vec<Event>,
179 statuses: Vec<String>,
180 redacted: Option<Vec<crate::response::redacted::Redacted>>,
181 ) -> Self {
182 Self {
183 common: Common::builder().build(),
184 object_common: ObjectCommon::entity()
185 .and_handle(handle)
186 .and_remarks(to_opt_vec(remarks.clone()))
187 .and_links(to_opt_vec(links.clone()))
188 .and_events(to_opt_vec(events.clone()))
189 .status(statuses.clone())
190 .and_entities(to_opt_vec(entities.clone()))
191 .and_redacted(redacted.clone())
192 .build(),
193 name: name.clone(),
194 digests: if !digests.is_empty() {
195 Some(digests)
196 } else {
197 None
198 },
199 not_valid_before,
200 not_valid_after,
201 publication_uri,
202 notification_uri,
203 rpki_type,
204 roa_ips: if !roa_ips.is_empty() {
205 Some(roa_ips)
206 } else {
207 None
208 },
209 origin_autnum: origin_autnum.map(Numberish::from),
210 }
211 }
212
213 /// Builds a ROA object for a response.
214 #[builder(entry = "response_obj", visibility = "pub")]
215 #[allow(clippy::too_many_arguments)]
216 fn new_response_obj(
217 handle: Option<String>,
218 name: Option<String>,
219 digests: Vec<Rpk1Digest>,
220 not_valid_before: Option<String>,
221 not_valid_after: Option<String>,
222 publication_uri: Option<String>,
223 notification_uri: Option<String>,
224 entities: Vec<Entity>,
225 rpki_type: Option<Rpk1RpkiType>,
226 roa_ips: Vec<Rpk1RoaIp>,
227 origin_autnum: Option<u32>,
228 remarks: Vec<Remark>,
229 links: Vec<Link>,
230 events: Vec<Event>,
231 statuses: Vec<String>,
232 notices: Vec<Notice>,
233 extensions: Vec<Extension>,
234 redacted: Option<Vec<crate::response::redacted::Redacted>>,
235 ) -> Self {
236 let mut rpki_exts = vec![ExtensionId::Rpki1.to_extension()];
237 rpki_exts.extend(extensions);
238 let common = Common::level0()
239 .extensions(rpki_exts)
240 .and_notices(to_opt_vec(notices))
241 .build();
242 let mut roa = Rpk1Roa::builder()
243 .and_handle(handle)
244 .and_name(name)
245 .digests(digests)
246 .and_not_valid_before(not_valid_before)
247 .and_not_valid_after(not_valid_after)
248 .and_publication_uri(publication_uri)
249 .and_notification_uri(notification_uri)
250 .entities(entities)
251 .and_rpki_type(rpki_type)
252 .roa_ips(roa_ips)
253 .and_origin_autnum(origin_autnum)
254 .remarks(remarks)
255 .links(links)
256 .events(events)
257 .statuses(statuses)
258 .and_redacted(redacted)
259 .build();
260 roa.common = common;
261 roa
262 }
263
264 #[builder(entry = "illegal", visibility = "pub(crate)")]
265 #[allow(dead_code)]
266 fn new_illegal(
267 handle: Option<String>,
268 name: Option<String>,
269 digests: Option<Vec<Rpk1Digest>>,
270 not_valid_before: Option<String>,
271 not_valid_after: Option<String>,
272 publication_uri: Option<String>,
273 notification_uri: Option<String>,
274 _entities: Option<Vec<Entity>>,
275 rpki_type: Option<Rpk1RpkiType>,
276 roa_ips: Option<Vec<Rpk1RoaIp>>,
277 origin_autnum: Option<u32>,
278 ) -> Self {
279 Self {
280 common: Common::level0()
281 .extension(ExtensionId::Rpki1.to_extension())
282 .build(),
283 object_common: ObjectCommon {
284 object_class_name: "rpki1_roa".to_string(),
285 handle: handle.map(|s| s.into()),
286 remarks: None,
287 links: None,
288 events: None,
289 status: None,
290 port_43: None,
291 entities: _entities,
292 redacted: None,
293 },
294 name,
295 digests,
296 not_valid_before,
297 not_valid_after,
298 publication_uri,
299 notification_uri,
300 rpki_type,
301 roa_ips,
302 origin_autnum: origin_autnum.map(Numberish::from),
303 }
304 }
305
306 pub fn roa_ips(&self) -> &[Rpk1RoaIp] {
307 self.roa_ips.as_deref().unwrap_or_default()
308 }
309
310 pub fn origin_autnum(&self) -> Option<u32> {
311 self.origin_autnum.as_ref().and_then(|n| n.as_u32())
312 }
313 }
314
315 impl ToResponse for Rpk1Roa {
316 fn to_response(self) -> super::RdapResponse {
317 super::RdapResponse::Rpki1Roa(Box::new(self))
318 }
319 }
320
321 impl GetSelfLink for Rpk1Roa {
322 fn self_link(&self) -> Option<&Link> {
323 self.object_common.self_link()
324 }
325 }
326
327 impl SelfLink for Rpk1Roa {
328 fn with_self_link(mut self, link: Link) -> Self {
329 self.object_common = self.object_common.with_self_link(link);
330 self
331 }
332 }
333
334 impl ToChild for Rpk1Roa {
335 fn to_child(mut self) -> Self {
336 self.common = Common {
337 rdap_conformance: None,
338 notices: None,
339 };
340 self
341 }
342 }
343
344 impl CommonFields for Rpk1Roa {
345 fn common(&self) -> &Common {
346 &self.common
347 }
348 }
349
350 impl ObjectCommonFields for Rpk1Roa {
351 fn object_common(&self) -> &ObjectCommon {
352 &self.object_common
353 }
354 }
355
356 impl ContentExtensions for Rpk1Roa {
357 fn content_extensions(&self) -> HashSet<ExtensionId> {
358 let mut exts = HashSet::new();
359 exts.insert(ExtensionId::Rpki1);
360 exts.extend(self.common().content_extensions());
361 exts.extend(self.object_common().content_extensions());
362 exts
363 }
364 }
365
366 // ASPA Object Class (Section 5.1)
367
368 /// Autonomous System Provider Authorization object.
369 ///
370 /// See [draft-ietf-regext-rdap-rpki Section 5.1](https://www.ietf.org/archive/id/draft-ietf-regext-rdap-rpki-04.html#section-5.1)
371 ///
372 /// ```rust
373 /// use icann_rdap_common::prelude::*;
374 ///
375 /// let aspa = Rpk1Aspa::response_obj()
376 /// .handle("ASPA-1")
377 /// .customer_autnum(65536)
378 /// .provider_autnum(65542)
379 /// .build();
380 /// ```
381 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
382 pub struct Rpk1Aspa {
383 #[serde(flatten)]
384 pub common: Common,
385
386 #[serde(flatten)]
387 pub object_common: ObjectCommon,
388
389 #[serde(skip_serializing_if = "Option::is_none")]
390 pub name: Option<String>,
391
392 #[serde(skip_serializing_if = "Option::is_none")]
393 pub digests: Option<Vec<Rpk1Digest>>,
394
395 #[serde(rename = "notValidBefore")]
396 #[serde(skip_serializing_if = "Option::is_none")]
397 pub not_valid_before: Option<String>,
398
399 #[serde(rename = "notValidAfter")]
400 #[serde(skip_serializing_if = "Option::is_none")]
401 pub not_valid_after: Option<String>,
402
403 #[serde(rename = "publicationUri")]
404 #[serde(skip_serializing_if = "Option::is_none")]
405 pub publication_uri: Option<String>,
406
407 #[serde(rename = "notificationUri")]
408 #[serde(skip_serializing_if = "Option::is_none")]
409 pub notification_uri: Option<String>,
410
411 #[serde(rename = "rpkiType")]
412 #[serde(skip_serializing_if = "Option::is_none")]
413 pub rpki_type: Option<Rpk1RpkiType>,
414
415 #[serde(rename = "customerAutnum")]
416 #[serde(skip_serializing_if = "Option::is_none")]
417 pub customer_autnum: Option<Numberish<u32>>,
418
419 #[serde(rename = "providerAutnums")]
420 #[serde(skip_serializing_if = "Option::is_none")]
421 pub provider_autnums: Option<Vec<Numberish<u32>>>,
422 }
423
424 #[buildstructor::buildstructor]
425 impl Rpk1Aspa {
426 /// Builds a basic ASPA object for embedding.
427 #[builder(visibility = "pub")]
428 #[allow(clippy::too_many_arguments)]
429 fn new(
430 handle: Option<String>,
431 name: Option<String>,
432 digests: Vec<Rpk1Digest>,
433 not_valid_before: Option<String>,
434 not_valid_after: Option<String>,
435 publication_uri: Option<String>,
436 notification_uri: Option<String>,
437 entities: Vec<Entity>,
438 rpki_type: Option<Rpk1RpkiType>,
439 customer_autnum: Option<u32>,
440 provider_autnums: Vec<u32>,
441 remarks: Vec<Remark>,
442 links: Vec<Link>,
443 events: Vec<Event>,
444 statuses: Vec<String>,
445 redacted: Option<Vec<crate::response::redacted::Redacted>>,
446 ) -> Self {
447 Self {
448 common: Common::builder().build(),
449 object_common: ObjectCommon::entity()
450 .and_handle(handle)
451 .and_remarks(to_opt_vec(remarks.clone()))
452 .and_links(to_opt_vec(links.clone()))
453 .and_events(to_opt_vec(events.clone()))
454 .status(statuses.clone())
455 .and_entities(to_opt_vec(entities.clone()))
456 .and_redacted(redacted.clone())
457 .build(),
458 name: name.clone(),
459 digests: if !digests.is_empty() {
460 Some(digests)
461 } else {
462 None
463 },
464 not_valid_before,
465 not_valid_after,
466 publication_uri,
467 notification_uri,
468 rpki_type,
469 customer_autnum: customer_autnum.map(Numberish::from),
470 provider_autnums: if !provider_autnums.is_empty() {
471 Some(provider_autnums.into_iter().map(Numberish::from).collect())
472 } else {
473 None
474 },
475 }
476 }
477
478 /// Builds an ASPA object for a response.
479 #[builder(entry = "response_obj", visibility = "pub")]
480 #[allow(clippy::too_many_arguments)]
481 fn new_response_obj(
482 handle: Option<String>,
483 name: Option<String>,
484 digests: Vec<Rpk1Digest>,
485 not_valid_before: Option<String>,
486 not_valid_after: Option<String>,
487 publication_uri: Option<String>,
488 notification_uri: Option<String>,
489 entities: Vec<Entity>,
490 rpki_type: Option<Rpk1RpkiType>,
491 customer_autnum: Option<u32>,
492 provider_autnums: Vec<u32>,
493 remarks: Vec<Remark>,
494 links: Vec<Link>,
495 events: Vec<Event>,
496 statuses: Vec<String>,
497 notices: Vec<Notice>,
498 extensions: Vec<Extension>,
499 redacted: Option<Vec<crate::response::redacted::Redacted>>,
500 ) -> Self {
501 let mut rpki_exts = vec![ExtensionId::Rpki1.to_extension()];
502 rpki_exts.extend(extensions);
503 let common = Common::level0()
504 .extensions(rpki_exts)
505 .and_notices(to_opt_vec(notices))
506 .build();
507 let mut aspa = Rpk1Aspa::builder()
508 .and_handle(handle)
509 .and_name(name)
510 .digests(digests)
511 .and_not_valid_before(not_valid_before)
512 .and_not_valid_after(not_valid_after)
513 .and_publication_uri(publication_uri)
514 .and_notification_uri(notification_uri)
515 .entities(entities)
516 .and_rpki_type(rpki_type)
517 .and_customer_autnum(customer_autnum)
518 .provider_autnums(provider_autnums)
519 .remarks(remarks)
520 .links(links)
521 .events(events)
522 .statuses(statuses)
523 .and_redacted(redacted)
524 .build();
525 aspa.common = common;
526 aspa
527 }
528
529 #[builder(entry = "illegal", visibility = "pub(crate)")]
530 #[allow(dead_code)]
531 fn new_illegal(
532 handle: Option<String>,
533 name: Option<String>,
534 digests: Option<Vec<Rpk1Digest>>,
535 not_valid_before: Option<String>,
536 not_valid_after: Option<String>,
537 publication_uri: Option<String>,
538 notification_uri: Option<String>,
539 _entities: Option<Vec<Entity>>,
540 rpki_type: Option<Rpk1RpkiType>,
541 customer_autnum: Option<u32>,
542 provider_autnums: Option<Vec<u32>>,
543 ) -> Self {
544 Self {
545 common: Common::level0()
546 .extension(ExtensionId::Rpki1.to_extension())
547 .build(),
548 object_common: ObjectCommon {
549 object_class_name: "rpki1_aspa".to_string(),
550 handle: handle.map(|s| s.into()),
551 remarks: None,
552 links: None,
553 events: None,
554 status: None,
555 port_43: None,
556 entities: _entities,
557 redacted: None,
558 },
559 name,
560 digests,
561 not_valid_before,
562 not_valid_after,
563 publication_uri,
564 notification_uri,
565 rpki_type,
566 customer_autnum: customer_autnum.map(Numberish::from),
567 provider_autnums: provider_autnums
568 .map(|a| a.into_iter().map(Numberish::from).collect()),
569 }
570 }
571
572 pub fn customer_autnum(&self) -> Option<u32> {
573 self.customer_autnum.as_ref().and_then(|n| n.as_u32())
574 }
575
576 pub fn provider_autnums(&self) -> Vec<u32> {
577 self.provider_autnums
578 .as_ref()
579 .map(|a| a.iter().filter_map(|n| n.as_u32()).collect())
580 .unwrap_or_default()
581 }
582 }
583
584 impl ToResponse for Rpk1Aspa {
585 fn to_response(self) -> super::RdapResponse {
586 super::RdapResponse::Rpki1Aspa(Box::new(self))
587 }
588 }
589
590 impl GetSelfLink for Rpk1Aspa {
591 fn self_link(&self) -> Option<&Link> {
592 self.object_common.self_link()
593 }
594 }
595
596 impl SelfLink for Rpk1Aspa {
597 fn with_self_link(mut self, link: Link) -> Self {
598 self.object_common = self.object_common.with_self_link(link);
599 self
600 }
601 }
602
603 impl ToChild for Rpk1Aspa {
604 fn to_child(mut self) -> Self {
605 self.common = Common {
606 rdap_conformance: None,
607 notices: None,
608 };
609 self
610 }
611 }
612
613 impl CommonFields for Rpk1Aspa {
614 fn common(&self) -> &Common {
615 &self.common
616 }
617 }
618
619 impl ObjectCommonFields for Rpk1Aspa {
620 fn object_common(&self) -> &ObjectCommon {
621 &self.object_common
622 }
623 }
624
625 impl ContentExtensions for Rpk1Aspa {
626 fn content_extensions(&self) -> HashSet<ExtensionId> {
627 let mut exts = HashSet::new();
628 exts.insert(ExtensionId::Rpki1);
629 exts.extend(self.common().content_extensions());
630 exts.extend(self.object_common().content_extensions());
631 exts
632 }
633 }
634
635 // X.509 Resource Certificate Object Class (Section 6.1)
636
637 /// X.509 Resource Certificate object.
638 ///
639 /// See [draft-ietf-regext-rdap-rpki Section 6.1](https://www.ietf.org/archive/id/draft-ietf-regext-rdap-rpki-04.html#section-6.1)
640 ///
641 /// ```rust
642 /// use icann_rdap_common::prelude::*;
643 ///
644 /// let cert = Rpk1X509ResourceCert::response_obj()
645 /// .handle("CERT-1")
646 /// .serial_number("1234")
647 /// .issuer("CN=RIR-CA")
648 /// .subject("CN=ISP-CA")
649 /// .build();
650 /// ```
651 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
652 pub struct Rpk1X509ResourceCert {
653 #[serde(flatten)]
654 pub common: Common,
655
656 #[serde(flatten)]
657 pub object_common: ObjectCommon,
658
659 #[serde(skip_serializing_if = "Option::is_none")]
660 pub digests: Option<Vec<Rpk1Digest>>,
661
662 #[serde(skip_serializing_if = "Option::is_none")]
663 #[serde(rename = "serialNumber")]
664 pub serial_number: Option<String>,
665
666 #[serde(skip_serializing_if = "Option::is_none")]
667 pub issuer: Option<String>,
668
669 #[serde(rename = "signatureAlgorithm")]
670 #[serde(skip_serializing_if = "Option::is_none")]
671 pub signature_algorithm: Option<String>,
672
673 #[serde(skip_serializing_if = "Option::is_none")]
674 pub subject: Option<String>,
675
676 #[serde(rename = "subjectPublicKeyInfo")]
677 #[serde(skip_serializing_if = "Option::is_none")]
678 pub subject_public_key_info: Option<Rpk1SubjectPublicKeyInfo>,
679
680 #[serde(rename = "subjectKeyIdentifier")]
681 #[serde(skip_serializing_if = "Option::is_none")]
682 pub subject_key_identifier: Option<String>,
683
684 #[serde(rename = "notValidBefore")]
685 #[serde(skip_serializing_if = "Option::is_none")]
686 pub not_valid_before: Option<String>,
687
688 #[serde(rename = "notValidAfter")]
689 #[serde(skip_serializing_if = "Option::is_none")]
690 pub not_valid_after: Option<String>,
691
692 #[serde(rename = "publicationUri")]
693 #[serde(skip_serializing_if = "Option::is_none")]
694 pub publication_uri: Option<String>,
695
696 #[serde(rename = "notificationUri")]
697 #[serde(skip_serializing_if = "Option::is_none")]
698 pub notification_uri: Option<String>,
699
700 #[serde(rename = "rpkiType")]
701 #[serde(skip_serializing_if = "Option::is_none")]
702 pub rpki_type: Option<Rpk1RpkiType>,
703
704 #[serde(skip_serializing_if = "Option::is_none")]
705 pub ips: Option<Vec<String>>,
706
707 #[serde(skip_serializing_if = "Option::is_none")]
708 pub autnums: Option<Vec<Numberish<u32>>>,
709 }
710
711 #[buildstructor::buildstructor]
712 impl Rpk1X509ResourceCert {
713 /// Builds a basic X.509 Resource Certificate object for embedding.
714 #[builder(visibility = "pub")]
715 #[allow(clippy::too_many_arguments)]
716 fn new(
717 handle: Option<String>,
718 digests: Vec<Rpk1Digest>,
719 serial_number: Option<String>,
720 issuer: Option<String>,
721 signature_algorithm: Option<String>,
722 subject: Option<String>,
723 subject_public_key_info: Option<Rpk1SubjectPublicKeyInfo>,
724 subject_key_identifier: Option<String>,
725 ips: Vec<String>,
726 autnums: Vec<u32>,
727 not_valid_before: Option<String>,
728 not_valid_after: Option<String>,
729 publication_uri: Option<String>,
730 notification_uri: Option<String>,
731 entities: Vec<Entity>,
732 rpki_type: Option<Rpk1RpkiType>,
733 remarks: Vec<Remark>,
734 links: Vec<Link>,
735 events: Vec<Event>,
736 statuses: Vec<String>,
737 redacted: Option<Vec<crate::response::redacted::Redacted>>,
738 ) -> Self {
739 Self {
740 common: Common::builder().build(),
741 object_common: ObjectCommon::entity()
742 .and_handle(handle)
743 .and_remarks(to_opt_vec(remarks.clone()))
744 .and_links(to_opt_vec(links.clone()))
745 .and_events(to_opt_vec(events.clone()))
746 .status(statuses.clone())
747 .and_entities(to_opt_vec(entities.clone()))
748 .and_redacted(redacted.clone())
749 .build(),
750 digests: if !digests.is_empty() {
751 Some(digests)
752 } else {
753 None
754 },
755 serial_number,
756 issuer,
757 signature_algorithm,
758 subject,
759 subject_public_key_info,
760 subject_key_identifier,
761 not_valid_before,
762 not_valid_after,
763 publication_uri,
764 notification_uri,
765 rpki_type,
766 ips: if !ips.is_empty() { Some(ips) } else { None },
767 autnums: if !autnums.is_empty() {
768 Some(autnums.into_iter().map(Numberish::from).collect())
769 } else {
770 None
771 },
772 }
773 }
774
775 /// Builds an X.509 Resource Certificate object for a response.
776 #[builder(entry = "response_obj", visibility = "pub")]
777 #[allow(clippy::too_many_arguments)]
778 fn new_response_obj(
779 handle: Option<String>,
780 digests: Vec<Rpk1Digest>,
781 serial_number: Option<String>,
782 issuer: Option<String>,
783 signature_algorithm: Option<String>,
784 subject: Option<String>,
785 subject_public_key_info: Option<Rpk1SubjectPublicKeyInfo>,
786 subject_key_identifier: Option<String>,
787 ips: Vec<String>,
788 autnums: Vec<u32>,
789 not_valid_before: Option<String>,
790 not_valid_after: Option<String>,
791 publication_uri: Option<String>,
792 notification_uri: Option<String>,
793 entities: Vec<Entity>,
794 rpki_type: Option<Rpk1RpkiType>,
795 remarks: Vec<Remark>,
796 links: Vec<Link>,
797 events: Vec<Event>,
798 statuses: Vec<String>,
799 notices: Vec<Notice>,
800 extensions: Vec<Extension>,
801 redacted: Option<Vec<crate::response::redacted::Redacted>>,
802 ) -> Self {
803 let mut rpki_exts = vec![ExtensionId::Rpki1.to_extension()];
804 rpki_exts.extend(extensions);
805 let common = Common::level0()
806 .extensions(rpki_exts)
807 .and_notices(to_opt_vec(notices))
808 .build();
809 let mut cert = Rpk1X509ResourceCert::builder()
810 .and_handle(handle)
811 .digests(digests)
812 .and_serial_number(serial_number)
813 .and_issuer(issuer)
814 .and_signature_algorithm(signature_algorithm)
815 .and_subject(subject)
816 .and_subject_public_key_info(subject_public_key_info)
817 .and_subject_key_identifier(subject_key_identifier)
818 .ips(ips)
819 .autnums(autnums)
820 .and_not_valid_before(not_valid_before)
821 .and_not_valid_after(not_valid_after)
822 .and_publication_uri(publication_uri)
823 .and_notification_uri(notification_uri)
824 .entities(entities)
825 .and_rpki_type(rpki_type)
826 .remarks(remarks)
827 .links(links)
828 .events(events)
829 .statuses(statuses)
830 .and_redacted(redacted)
831 .build();
832 cert.common = common;
833 cert
834 }
835
836 #[builder(entry = "illegal", visibility = "pub(crate)")]
837 #[allow(dead_code)]
838 fn new_illegal(
839 handle: Option<String>,
840 digests: Option<Vec<Rpk1Digest>>,
841 serial_number: Option<String>,
842 issuer: Option<String>,
843 signature_algorithm: Option<String>,
844 subject: Option<String>,
845 subject_public_key_info: Option<Rpk1SubjectPublicKeyInfo>,
846 subject_key_identifier: Option<String>,
847 ips: Option<Vec<String>>,
848 autnums: Option<Vec<u32>>,
849 not_valid_before: Option<String>,
850 not_valid_after: Option<String>,
851 publication_uri: Option<String>,
852 notification_uri: Option<String>,
853 _entities: Option<Vec<Entity>>,
854 rpki_type: Option<Rpk1RpkiType>,
855 ) -> Self {
856 Self {
857 common: Common::level0()
858 .extension(ExtensionId::Rpki1.to_extension())
859 .build(),
860 object_common: ObjectCommon {
861 object_class_name: "rpki1_x509ResourceCert".to_string(),
862 handle: handle.map(|s| s.into()),
863 remarks: None,
864 links: None,
865 events: None,
866 status: None,
867 port_43: None,
868 entities: _entities,
869 redacted: None,
870 },
871 digests,
872 serial_number,
873 issuer,
874 signature_algorithm,
875 subject,
876 subject_public_key_info,
877 subject_key_identifier,
878 not_valid_before,
879 not_valid_after,
880 publication_uri,
881 notification_uri,
882 rpki_type,
883 ips,
884 autnums: autnums.map(|a| a.into_iter().map(Numberish::from).collect()),
885 }
886 }
887
888 pub fn subject_public_key_info(&self) -> Option<&Rpk1SubjectPublicKeyInfo> {
889 self.subject_public_key_info.as_ref()
890 }
891
892 pub fn subject_key_identifier(&self) -> Option<&str> {
893 self.subject_key_identifier.as_deref()
894 }
895
896 pub fn ips(&self) -> &[String] {
897 self.ips.as_deref().unwrap_or_default()
898 }
899
900 pub fn autnums(&self) -> Vec<u32> {
901 self.autnums
902 .as_ref()
903 .map(|a| a.iter().filter_map(|n| n.as_u32()).collect())
904 .unwrap_or_default()
905 }
906 }
907
908 impl ToResponse for Rpk1X509ResourceCert {
909 fn to_response(self) -> super::RdapResponse {
910 super::RdapResponse::Rpki1X509ResourceCert(Box::new(self))
911 }
912 }
913
914 impl GetSelfLink for Rpk1X509ResourceCert {
915 fn self_link(&self) -> Option<&Link> {
916 self.object_common.self_link()
917 }
918 }
919
920 impl SelfLink for Rpk1X509ResourceCert {
921 fn with_self_link(mut self, link: Link) -> Self {
922 self.object_common = self.object_common.with_self_link(link);
923 self
924 }
925 }
926
927 impl ToChild for Rpk1X509ResourceCert {
928 fn to_child(mut self) -> Self {
929 self.common = Common {
930 rdap_conformance: None,
931 notices: None,
932 };
933 self
934 }
935 }
936
937 impl CommonFields for Rpk1X509ResourceCert {
938 fn common(&self) -> &Common {
939 &self.common
940 }
941 }
942
943 impl ObjectCommonFields for Rpk1X509ResourceCert {
944 fn object_common(&self) -> &ObjectCommon {
945 &self.object_common
946 }
947 }
948
949 impl ContentExtensions for Rpk1X509ResourceCert {
950 fn content_extensions(&self) -> HashSet<ExtensionId> {
951 let mut exts = HashSet::new();
952 exts.insert(ExtensionId::Rpki1);
953 exts.extend(self.common().content_extensions());
954 exts.extend(self.object_common().content_extensions());
955 exts
956 }
957 }
958
959 // Search Results (Sections 4.3.1, 5.3.1, 6.3.1)
960
961 /// ROA search results.
962 #[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Eq)]
963 pub struct Rpk1RoaSearchResults {
964 #[serde(flatten)]
965 pub common: Common,
966
967 #[serde(rename = "rpki1_roaSearchResults")]
968 pub results: Vec<Rpk1Roa>,
969 }
970
971 #[buildstructor::buildstructor]
972 impl Rpk1RoaSearchResults {
973 #[builder(entry = "response_obj", visibility = "pub")]
974 fn new_response_obj(results: Vec<Rpk1Roa>, extensions: Vec<Extension>) -> Self {
975 Self {
976 common: Common::level0().extensions(extensions).build(),
977 results,
978 }
979 }
980
981 pub fn results(&self) -> &[Rpk1Roa] {
982 &self.results
983 }
984 }
985
986 impl CommonFields for Rpk1RoaSearchResults {
987 fn common(&self) -> &Common {
988 &self.common
989 }
990 }
991
992 impl ToResponse for Rpk1RoaSearchResults {
993 fn to_response(self) -> super::RdapResponse {
994 super::RdapResponse::Rpki1RoaSearchResults(Box::new(self))
995 }
996 }
997
998 impl ContentExtensions for Rpk1RoaSearchResults {
999 fn content_extensions(&self) -> HashSet<ExtensionId> {
1000 let mut exts = HashSet::new();
1001 exts.insert(ExtensionId::Rpki1);
1002 self.results()
1003 .iter()
1004 .for_each(|r| exts.extend(r.content_extensions()));
1005 exts.extend(self.common().content_extensions());
1006 exts
1007 }
1008 }
1009
1010 /// ASPA search results.
1011 #[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Eq)]
1012 pub struct Rpk1AspaSearchResults {
1013 #[serde(flatten)]
1014 pub common: Common,
1015
1016 #[serde(rename = "rpki1_aspaSearchResults")]
1017 pub results: Vec<Rpk1Aspa>,
1018 }
1019
1020 #[buildstructor::buildstructor]
1021 impl Rpk1AspaSearchResults {
1022 #[builder(entry = "response_obj", visibility = "pub")]
1023 fn new_response_obj(results: Vec<Rpk1Aspa>, extensions: Vec<Extension>) -> Self {
1024 Self {
1025 common: Common::level0().extensions(extensions).build(),
1026 results,
1027 }
1028 }
1029
1030 pub fn results(&self) -> &[Rpk1Aspa] {
1031 &self.results
1032 }
1033 }
1034
1035 impl CommonFields for Rpk1AspaSearchResults {
1036 fn common(&self) -> &Common {
1037 &self.common
1038 }
1039 }
1040
1041 impl ToResponse for Rpk1AspaSearchResults {
1042 fn to_response(self) -> super::RdapResponse {
1043 super::RdapResponse::Rpki1AspaSearchResults(Box::new(self))
1044 }
1045 }
1046
1047 impl ContentExtensions for Rpk1AspaSearchResults {
1048 fn content_extensions(&self) -> HashSet<ExtensionId> {
1049 let mut exts = HashSet::new();
1050 exts.insert(ExtensionId::Rpki1);
1051 self.results()
1052 .iter()
1053 .for_each(|r| exts.extend(r.content_extensions()));
1054 exts.extend(self.common().content_extensions());
1055 exts
1056 }
1057 }
1058
1059 /// X.509 Resource Certificate search results.
1060 #[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Eq)]
1061 pub struct Rpk1X509ResourceCertSearchResults {
1062 #[serde(flatten)]
1063 pub common: Common,
1064
1065 #[serde(rename = "rpki1_x509ResourceCertSearchResults")]
1066 pub results: Vec<Rpk1X509ResourceCert>,
1067 }
1068
1069 #[buildstructor::buildstructor]
1070 impl Rpk1X509ResourceCertSearchResults {
1071 #[builder(entry = "response_obj", visibility = "pub")]
1072 fn new_response_obj(results: Vec<Rpk1X509ResourceCert>, extensions: Vec<Extension>) -> Self {
1073 Self {
1074 common: Common::level0().extensions(extensions).build(),
1075 results,
1076 }
1077 }
1078
1079 pub fn results(&self) -> &[Rpk1X509ResourceCert] {
1080 &self.results
1081 }
1082 }
1083
1084 impl CommonFields for Rpk1X509ResourceCertSearchResults {
1085 fn common(&self) -> &Common {
1086 &self.common
1087 }
1088 }
1089
1090 impl ToResponse for Rpk1X509ResourceCertSearchResults {
1091 fn to_response(self) -> super::RdapResponse {
1092 super::RdapResponse::Rpki1X509ResourceCertSearchResults(Box::new(self))
1093 }
1094 }
1095
1096 impl ContentExtensions for Rpk1X509ResourceCertSearchResults {
1097 fn content_extensions(&self) -> HashSet<ExtensionId> {
1098 let mut exts = HashSet::new();
1099 exts.insert(ExtensionId::Rpki1);
1100 self.results()
1101 .iter()
1102 .for_each(|r| exts.extend(r.content_extensions()));
1103 exts.extend(self.common().content_extensions());
1104 exts
1105 }
1106 }
1107
1108 #[cfg(test)]
1109 mod tests {
1110 use super::*;
1111 use crate::response::RdapResponse;
1112
1113 #[test]
1114 fn test_roa_serialize() {
1115 let roa = Rpk1Roa::illegal()
1116 .handle("ROA-1")
1117 .roa_ips(vec![Rpk1RoaIp::new("2001:db8::/48".to_string(), 64)])
1118 .origin_autnum(65536)
1119 .build();
1120 let json = serde_json::to_string(&roa).unwrap();
1121 assert!(json.contains("rpki1_roa"));
1122 assert!(json.contains("roaIps"));
1123 assert!(json.contains("originAutnum"));
1124 }
1125
1126 #[test]
1127 fn test_aspa_serialize() {
1128 let aspa = Rpk1Aspa::illegal()
1129 .handle("ASPA-1")
1130 .customer_autnum(65536)
1131 .provider_autnums(vec![65542])
1132 .build();
1133 let json = serde_json::to_string(&aspa).unwrap();
1134 assert!(json.contains("rpki1_aspa"));
1135 assert!(json.contains("customerAutnum"));
1136 assert!(json.contains("providerAutnums"));
1137 }
1138
1139 #[test]
1140 fn test_x509_cert_serialize() {
1141 let cert = Rpk1X509ResourceCert::illegal()
1142 .handle("CERT-1")
1143 .serial_number("1234")
1144 .issuer("CN=RIR-CA")
1145 .subject("CN=ISP-CA")
1146 .ips(vec!["192.0.2.0/24".to_string()])
1147 .autnums(vec![65536])
1148 .build();
1149 let json = serde_json::to_string(&cert).unwrap();
1150 assert!(json.contains("rpki1_x509ResourceCert"));
1151 assert!(json.contains("serialNumber"));
1152 assert!(json.contains("issuer"));
1153 assert!(json.contains("subject"));
1154 }
1155
1156 #[test]
1157 fn test_roa_to_response() {
1158 let roa = Rpk1Roa::illegal().handle("ROA-1").build();
1159 let response = roa.to_response();
1160 assert!(matches!(response, RdapResponse::Rpki1Roa(_)));
1161 }
1162
1163 #[test]
1164 fn test_aspa_to_response() {
1165 let aspa = Rpk1Aspa::illegal().handle("ASPA-1").build();
1166 let response = aspa.to_response();
1167 assert!(matches!(response, RdapResponse::Rpki1Aspa(_)));
1168 }
1169
1170 #[test]
1171 fn test_x509_to_response() {
1172 let cert = Rpk1X509ResourceCert::illegal().handle("CERT-1").build();
1173 let response = cert.to_response();
1174 assert!(matches!(response, RdapResponse::Rpki1X509ResourceCert(_)));
1175 }
1176
1177 #[test]
1178 fn test_roa_content_extensions() {
1179 let roa = Rpk1Roa::illegal().handle("ROA-1").build();
1180 let exts = roa.content_extensions();
1181 assert!(exts.contains(&ExtensionId::Rpki1));
1182 }
1183
1184 #[test]
1185 fn test_aspa_content_extensions() {
1186 let aspa = Rpk1Aspa::illegal().handle("ASPA-1").build();
1187 let exts = aspa.content_extensions();
1188 assert!(exts.contains(&ExtensionId::Rpki1));
1189 }
1190
1191 #[test]
1192 fn test_x509_content_extensions() {
1193 let cert = Rpk1X509ResourceCert::illegal().handle("CERT-1").build();
1194 let exts = cert.content_extensions();
1195 assert!(exts.contains(&ExtensionId::Rpki1));
1196 }
1197
1198 #[test]
1199 fn test_rpki_type_serialize() {
1200 let json = serde_json::to_string(&Rpk1RpkiType::Hosted).unwrap();
1201 assert_eq!(json, "\"hosted\"");
1202 let json = serde_json::to_string(&Rpk1RpkiType::Delegated).unwrap();
1203 assert_eq!(json, "\"delegated\"");
1204 let json = serde_json::to_string(&Rpk1RpkiType::Hybrid).unwrap();
1205 assert_eq!(json, "\"hybrid\"");
1206 }
1207
1208 #[test]
1209 fn test_rpki_type_deserialize() {
1210 let hosted: Rpk1RpkiType = serde_json::from_str("\"hosted\"").unwrap();
1211 assert_eq!(hosted, Rpk1RpkiType::Hosted);
1212 let delegated: Rpk1RpkiType = serde_json::from_str("\"delegated\"").unwrap();
1213 assert_eq!(delegated, Rpk1RpkiType::Delegated);
1214 let hybrid: Rpk1RpkiType = serde_json::from_str("\"hybrid\"").unwrap();
1215 assert_eq!(hybrid, Rpk1RpkiType::Hybrid);
1216 }
1217
1218 #[test]
1219 fn test_digest_serialize() {
1220 let digest = Rpk1Digest::new(
1221 "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069".to_string(),
1222 "SHA-256".to_string(),
1223 );
1224 let json = serde_json::to_string(&digest).unwrap();
1225 assert!(json.contains("digest"));
1226 assert!(json.contains("digestAlgorithm"));
1227 assert!(json.contains("SHA-256"));
1228 }
1229
1230 #[test]
1231 fn test_roa_ip_serialize() {
1232 let roa_ip = Rpk1RoaIp::new("2001:db8::/48".to_string(), 64);
1233 let json = serde_json::to_string(&roa_ip).unwrap();
1234 assert!(json.contains("ip"));
1235 assert!(json.contains("maxLength"));
1236 assert!(json.contains("64"));
1237 }
1238
1239 #[test]
1240 fn test_subject_public_key_info_serialize() {
1241 let spki = Rpk1SubjectPublicKeyInfo::new("id-ecPublicKey".to_string(), "04...".to_string());
1242 let json = serde_json::to_string(&spki).unwrap();
1243 assert!(json.contains("publicKeyAlgorithm"));
1244 assert!(json.contains("publicKey"));
1245 }
1246 }