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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{Error, Result, RsaPublicKey, Version};
use core::{convert::TryFrom, fmt};
use der::{
asn1::{Any, UIntBytes},
Decodable, Encodable, Message,
};
#[cfg(feature = "alloc")]
use crate::RsaPrivateKeyDocument;
#[cfg(feature = "pem")]
use {
crate::{pem, LineEnding},
alloc::string::String,
zeroize::Zeroizing,
};
#[cfg(feature = "pem")]
pub(crate) const PEM_TYPE_LABEL: &str = "RSA PRIVATE KEY";
#[derive(Clone)]
pub struct RsaPrivateKey<'a> {
pub version: Version,
pub modulus: UIntBytes<'a>,
pub public_exponent: UIntBytes<'a>,
pub private_exponent: UIntBytes<'a>,
pub prime1: UIntBytes<'a>,
pub prime2: UIntBytes<'a>,
pub exponent1: UIntBytes<'a>,
pub exponent2: UIntBytes<'a>,
pub coefficient: UIntBytes<'a>,
}
impl<'a> RsaPrivateKey<'a> {
pub fn public_key(&self) -> RsaPublicKey<'a> {
RsaPublicKey {
modulus: self.modulus,
public_exponent: self.public_exponent,
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_der(&self) -> RsaPrivateKeyDocument {
self.into()
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem(&self) -> Result<Zeroizing<String>> {
self.to_pem_with_le(LineEnding::default())
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem_with_le(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
let pem_doc = pem::encode_string(PEM_TYPE_LABEL, line_ending, self.to_der().as_ref())?;
Ok(Zeroizing::new(pem_doc))
}
}
impl<'a> From<RsaPrivateKey<'a>> for RsaPublicKey<'a> {
fn from(private_key: RsaPrivateKey<'a>) -> RsaPublicKey<'a> {
private_key.public_key()
}
}
impl<'a> From<&RsaPrivateKey<'a>> for RsaPublicKey<'a> {
fn from(private_key: &RsaPrivateKey<'a>) -> RsaPublicKey<'a> {
private_key.public_key()
}
}
impl<'a> TryFrom<&'a [u8]> for RsaPrivateKey<'a> {
type Error = Error;
fn try_from(bytes: &'a [u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
}
impl<'a> TryFrom<Any<'a>> for RsaPrivateKey<'a> {
type Error = der::Error;
fn try_from(any: Any<'a>) -> der::Result<RsaPrivateKey<'a>> {
any.sequence(|decoder| {
Ok(Self {
version: decoder.decode()?,
modulus: decoder.decode()?,
public_exponent: decoder.decode()?,
private_exponent: decoder.decode()?,
prime1: decoder.decode()?,
prime2: decoder.decode()?,
exponent1: decoder.decode()?,
exponent2: decoder.decode()?,
coefficient: decoder.decode()?,
})
})
}
}
impl<'a> Message<'a> for RsaPrivateKey<'a> {
fn fields<F, T>(&self, f: F) -> der::Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
{
f(&[
&self.version,
&self.modulus,
&self.public_exponent,
&self.private_exponent,
&self.prime1,
&self.prime2,
&self.exponent1,
&self.exponent2,
&self.coefficient,
])
}
}
impl<'a> fmt::Debug for RsaPrivateKey<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RsaPrivateKey")
.field("version", &self.version)
.field("modulus", &self.modulus)
.field("public_exponent", &self.public_exponent)
.field("private_exponent", &"...")
.field("prime1", &"...")
.field("prime2", &"...")
.field("exponent1", &"...")
.field("exponent2", &"...")
.field("coefficient", &"...")
.finish()
}
}