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
use crate::{AlgorithmIdentifier, Attributes, Error, Result, Version};
use core::{convert::TryFrom, fmt};
use der::{Decodable, Encodable, Message};
#[cfg(feature = "alloc")]
use crate::PrivateKeyDocument;
#[cfg(feature = "encryption")]
use {
crate::EncryptedPrivateKeyDocument,
rand_core::{CryptoRng, RngCore},
};
#[cfg(feature = "pem")]
use {crate::pem, zeroize::Zeroizing};
#[derive(Clone)]
pub struct PrivateKeyInfo<'a> {
pub algorithm: AlgorithmIdentifier<'a>,
pub private_key: &'a [u8],
}
impl<'a> PrivateKeyInfo<'a> {
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
pub fn encrypt(
&self,
rng: impl CryptoRng + RngCore,
password: impl AsRef<[u8]>,
) -> Result<EncryptedPrivateKeyDocument> {
PrivateKeyDocument::from(self).encrypt(rng, password)
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_der(&self) -> PrivateKeyDocument {
self.into()
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem(&self) -> Zeroizing<alloc::string::String> {
Zeroizing::new(pem::encode(
self.to_der().as_ref(),
pem::PRIVATE_KEY_BOUNDARY,
))
}
}
impl<'a> TryFrom<&'a [u8]> for PrivateKeyInfo<'a> {
type Error = Error;
fn try_from(bytes: &'a [u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
}
impl<'a> TryFrom<der::Any<'a>> for PrivateKeyInfo<'a> {
type Error = der::Error;
fn try_from(any: der::Any<'a>) -> der::Result<PrivateKeyInfo<'a>> {
any.sequence(|decoder| {
if Version::V1 != Version::decode(decoder)? {
return Err(der::ErrorKind::Value {
tag: der::Tag::Integer,
}
.into());
}
let algorithm = decoder.decode()?;
let private_key = decoder.octet_string()?.into();
let _attributes: Option<Attributes<'_>> = decoder.optional()?;
Ok(Self {
algorithm,
private_key,
})
})
}
}
impl<'a> Message<'a> for PrivateKeyInfo<'a> {
fn fields<F, T>(&self, f: F) -> der::Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
{
f(&[
&u8::from(Version::V1),
&self.algorithm,
&der::OctetString::new(self.private_key)?,
])
}
}
impl<'a> fmt::Debug for PrivateKeyInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PrivateKeyInfo")
.field("algorithm", &self.algorithm)
.finish()
}
}