Debian Codegraph @

third_party/rust/mls-rs-identity-x509/src/lib.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5 #![cfg_attr(not(feature = "std"), no_std)]
6 extern crate alloc;
7
8 mod error;
9 mod identity_extractor;
10 mod provider;
11 mod traits;
12 mod util;
13
14 use alloc::vec::Vec;
15 use core::fmt::{self, Debug};
16
17 pub use error::*;
18 pub use identity_extractor::*;
19 pub use provider::*;
20 pub use traits::*;
21
22 pub use mls_rs_core::identity::{CertificateChain, DerCertificate};
23
24 #[cfg(all(test, target_arch = "wasm32"))]
25 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
26
27 #[derive(Clone, PartialEq, Eq)]
28 /// X.509 certificate request in DER format.
29 pub struct DerCertificateRequest(Vec<u8>);
30
31 impl Debug for DerCertificateRequest {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 mls_rs_core::debug::pretty_bytes(&self.0)
34 .named("DerCertificateRequest")
35 .fmt(f)
36 }
37 }
38
39 impl DerCertificateRequest {
40 /// Create a DER certificate request from raw bytes.
41 pub fn new(data: Vec<u8>) -> DerCertificateRequest {
42 DerCertificateRequest(data)
43 }
44
45 /// Convert this certificate request into raw bytes.
46 pub fn into_vec(self) -> Vec<u8> {
47 self.0
48 }
49 }
50
51 impl From<Vec<u8>> for DerCertificateRequest {
52 fn from(data: Vec<u8>) -> Self {
53 DerCertificateRequest(data)
54 }
55 }
56
57 impl AsRef<[u8]> for DerCertificateRequest {
58 fn as_ref(&self) -> &[u8] {
59 &self.0
60 }
61 }
62
63 #[cfg(all(test, feature = "std"))]
64 pub(crate) mod test_utils {
65
66 use alloc::vec;
67 use mls_rs_core::{crypto::SignaturePublicKey, error::IntoAnyError, identity::SigningIdentity};
68 use rand::{thread_rng, Rng};
69
70 use crate::{CertificateChain, DerCertificate};
71
72 #[derive(Debug, thiserror::Error)]
73 #[error("test error")]
74 pub struct TestError;
75
76 impl IntoAnyError for TestError {
77 fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> {
78 Ok(self.into())
79 }
80 }
81
82 pub fn test_certificate_chain() -> CertificateChain {
83 (0..3)
84 .map(|_| {
85 let mut data = [0u8; 32];
86 thread_rng().fill(&mut data);
87 DerCertificate::from(data.to_vec())
88 })
89 .collect::<CertificateChain>()
90 }
91
92 pub fn test_signing_identity() -> SigningIdentity {
93 let chain = test_certificate_chain();
94 test_signing_identity_with_chain(chain)
95 }
96
97 pub fn test_signing_identity_with_chain(chain: CertificateChain) -> SigningIdentity {
98 SigningIdentity {
99 signature_key: SignaturePublicKey::from(vec![0u8; 128]),
100 credential: chain.into_credential(),
101 }
102 }
103 }