1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Certificate module
//!
//! This module provides functionality for handling client certificates and keys.
use std::{fmt::Debug, future::Future};
#[derive(Debug, Clone)]
/// Supported encodings for client certificates.
pub enum ContentEncoding {
/// PEM encoding.
PEM,
/// DER encoding.
DER,
}
/// Extension trait for Identity to provide native certificate loading methods.
pub trait IdentityNativeExt {
/// Load a DER encoded PKCS#12 archive from a slice of bytes
///
/// # Arguments
///
/// * `bundle` - The DER encoded PKCS#12 archive.
/// * `password` - The password for the PKCS#12 archive.
///
/// # Returns
///
/// * `Identity` - The new Identity instance.
///
fn from_pkcs12(bundle: &[u8], password: Option<String>) -> Self;
/// Load a DER encoded PKCS#12 archive from a file
///
/// # Arguments
///
/// * `file` - The path to the DER encoded PKCS#12 archive.
/// * `password` - The password for the PKCS#12 archive.
///
/// # Returns
///
/// * `Identity` - The new Identity instance.
///
fn from_pkcs12_file(
file: &str,
password: Option<String>,
) -> impl Future<Output = std::io::Result<Self>>
where
Self: Sized;
}
/// Extension trait for Identity to provide certificate loading methods.
pub trait IdentityExt {
/// Load DER encoded certificate and key from a slice of bytes
///
/// # Arguments
///
/// * `cert` - The DER encoded certificate.
/// * `key` - The DER encoded PKCS8 private key.
/// * `encoding` - The encoding of the certificate and key.
///
/// # Returns
///
/// * `Identity` - The new Identity instance.
fn from_pkcs8(cert: &[u8], key: &[u8], encoding: ContentEncoding) -> Self;
/// Load DER encoded certificate and key from files
///
/// # Arguments
///
/// * `cert` - The path to the DER encoded certificate.
/// * `key` - The path to the DER encoded PKCS8 private key.
/// * `encoding` - The encoding of the certificate and key.
///
/// # Returns
///
/// * `Identity` - The new Identity instance.
///
fn from_pkcs8_file(
cert: &str,
key: &str,
encoding: ContentEncoding,
) -> impl Future<Output = std::io::Result<Self>>
where
Self: Sized;
}
/// Identity
pub trait Identity {
/// Get the certificate
///
/// # Returns
///
/// * `&Vec<u8>` - The certificate
fn cert(&self) -> &Vec<u8>;
/// Get the private key
///
/// # Returns
///
/// * `&Option<Vec<u8>>` - The private key
fn key(&self) -> &Option<Vec<u8>>;
/// Get the encoding
///
/// # Returns
///
/// * `&Option<ContentEncoding>` - The encoding
fn encoding(&self) -> &Option<ContentEncoding>;
}
/// Certificate
pub trait CertificateExt {
/// Create certificate from slice of DER encoded bytes.
///
/// # Arguments
///
/// * `data` - The client certificate data.
///
/// # Returns
///
/// * `Certificate` - The new Certificate instance.
///
fn from_slice(data: &[u8], encoding: ContentEncoding) -> Self;
/// Create certificate from file of DER encoded file.
///
/// # Arguments
///
/// * `file` - The client certificate file path.
///
/// # Returns
///
/// * `Result<Certificate, std::io::Error>` - The new Certificate instance.
///
fn from_file(
file: &str,
encoding: ContentEncoding,
) -> impl Future<Output = std::io::Result<Self>>
where
Self: Sized;
}
/// Certificate
pub trait Certificate {
/// Allow get the client certificate path.
///
/// # Returns
///
/// * `&str` - The client certificate path.
///
fn as_bytes(&self) -> &Vec<u8>;
}