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
149
150
151
use crate::{Error, PrivateKeyInfo, Result, SubjectPublicKeyInfo};
use alloc::{borrow::ToOwned, vec::Vec};
use core::{convert::TryFrom, fmt};
use zeroize::Zeroizing;
#[cfg(feature = "pem")]
use crate::pem;
#[cfg(feature = "pem")]
use core::str::FromStr;
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct PrivateKeyDocument(Zeroizing<Vec<u8>>);
impl PrivateKeyDocument {
pub fn from_der(bytes: &[u8]) -> Result<Self> {
PrivateKeyInfo::from_der(bytes)?;
Ok(Self(Zeroizing::new(bytes.to_owned())))
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn from_pem(s: &str) -> Result<Self> {
let der_bytes = pem::parse(s, pem::BEGIN_PRIVATE_KEY, pem::END_PRIVATE_KEY)?;
Self::from_der(&*der_bytes)
}
pub fn private_key_info(&self) -> PrivateKeyInfo<'_> {
PrivateKeyInfo::from_der(self.0.as_ref()).expect("constructor failed to validate document")
}
}
impl AsRef<[u8]> for PrivateKeyDocument {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl TryFrom<&[u8]> for PrivateKeyDocument {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self> {
Self::from_der(bytes)
}
}
impl fmt::Debug for PrivateKeyDocument {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple("PrivateKeyDocument")
.field(&self.private_key_info())
.finish()
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl FromStr for PrivateKeyDocument {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Self::from_pem(s)
}
}
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct PublicKeyDocument(Vec<u8>);
impl PublicKeyDocument {
pub fn from_der(bytes: &[u8]) -> Result<Self> {
SubjectPublicKeyInfo::from_der(bytes)?;
Ok(Self(bytes.to_owned()))
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn from_pem(s: &str) -> Result<Self> {
let der_bytes = pem::parse(s, pem::BEGIN_PUBLIC_KEY, pem::END_PUBLIC_KEY)?;
Self::from_der(&*der_bytes)
}
pub fn spki(&self) -> SubjectPublicKeyInfo<'_> {
SubjectPublicKeyInfo::from_der(self.0.as_ref())
.expect("constructor failed to validate document")
}
}
impl AsRef<[u8]> for PublicKeyDocument {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl TryFrom<&[u8]> for PublicKeyDocument {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self> {
Self::from_der(bytes)
}
}
impl fmt::Debug for PublicKeyDocument {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple("PublicKeyDocument")
.field(&self.spki())
.finish()
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl FromStr for PublicKeyDocument {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Self::from_pem(s)
}
}