rustls_pemfile/
pemfile.rs1use alloc::format;
2use alloc::string::String;
3use alloc::vec::Vec;
4#[cfg(feature = "std")]
5use core::iter;
6#[cfg(feature = "std")]
7use std::io::{self, ErrorKind};
8
9use pki_types::{
10 pem, CertificateDer, CertificateRevocationListDer, CertificateSigningRequestDer,
11 PrivatePkcs1KeyDer, PrivatePkcs8KeyDer, PrivateSec1KeyDer, SubjectPublicKeyInfoDer,
12};
13
14#[non_exhaustive]
16#[derive(Debug, PartialEq)]
17pub enum Item {
18 X509Certificate(CertificateDer<'static>),
22
23 SubjectPublicKeyInfo(SubjectPublicKeyInfoDer<'static>),
27
28 Pkcs1Key(PrivatePkcs1KeyDer<'static>),
32
33 Pkcs8Key(PrivatePkcs8KeyDer<'static>),
37
38 Sec1Key(PrivateSec1KeyDer<'static>),
42
43 Crl(CertificateRevocationListDer<'static>),
47
48 Csr(CertificateSigningRequestDer<'static>),
52}
53
54impl Item {
55 #[cfg(feature = "std")]
56 fn from_buf(rd: &mut dyn io::BufRead) -> Result<Option<Self>, pem::Error> {
57 loop {
58 match pem::from_buf(rd)? {
59 Some((kind, data)) => match Self::from_kind(kind, data) {
60 Some(item) => return Ok(Some(item)),
61 None => continue,
62 },
63
64 None => return Ok(None),
65 }
66 }
67 }
68
69 fn from_slice(pem: &[u8]) -> Result<Option<(Self, &[u8])>, pem::Error> {
70 let mut iter = <(pem::SectionKind, Vec<u8>) as pem::PemObject>::pem_slice_iter(pem);
71
72 for found in iter.by_ref() {
73 match found {
74 Ok((kind, data)) => match Self::from_kind(kind, data) {
75 Some(item) => return Ok(Some((item, iter.remainder()))),
76 None => continue,
77 },
78 Err(err) => return Err(err.into()),
79 }
80 }
81
82 Ok(None)
83 }
84
85 fn from_kind(kind: pem::SectionKind, data: Vec<u8>) -> Option<Self> {
86 use pem::SectionKind::*;
87 match kind {
88 Certificate => Some(Self::X509Certificate(data.into())),
89 PublicKey => Some(Self::SubjectPublicKeyInfo(data.into())),
90 RsaPrivateKey => Some(Self::Pkcs1Key(data.into())),
91 PrivateKey => Some(Self::Pkcs8Key(data.into())),
92 EcPrivateKey => Some(Self::Sec1Key(data.into())),
93 Crl => Some(Self::Crl(data.into())),
94 Csr => Some(Self::Csr(data.into())),
95 _ => None,
96 }
97 }
98}
99
100#[derive(Debug, PartialEq)]
105pub enum Error {
106 MissingSectionEnd {
108 end_marker: Vec<u8>,
110 },
111
112 IllegalSectionStart {
114 line: Vec<u8>,
116 },
117
118 Base64Decode(String),
120}
121
122#[cfg(feature = "std")]
123impl From<Error> for io::Error {
124 fn from(error: Error) -> Self {
125 match error {
126 Error::MissingSectionEnd { end_marker } => io::Error::new(
127 ErrorKind::InvalidData,
128 format!(
129 "section end {:?} missing",
130 String::from_utf8_lossy(&end_marker)
131 ),
132 ),
133
134 Error::IllegalSectionStart { line } => io::Error::new(
135 ErrorKind::InvalidData,
136 format!(
137 "illegal section start: {:?}",
138 String::from_utf8_lossy(&line)
139 ),
140 ),
141
142 Error::Base64Decode(err) => io::Error::new(ErrorKind::InvalidData, err),
143 }
144 }
145}
146
147impl From<pem::Error> for Error {
148 fn from(error: pem::Error) -> Self {
149 match error {
150 pem::Error::MissingSectionEnd { end_marker } => Error::MissingSectionEnd { end_marker },
151 pem::Error::IllegalSectionStart { line } => Error::IllegalSectionStart { line },
152 pem::Error::Base64Decode(str) => Error::Base64Decode(str),
153
154 other => Error::Base64Decode(format!("{other:?}")),
157 }
158 }
159}
160
161pub fn read_one_from_slice(input: &[u8]) -> Result<Option<(Item, &[u8])>, Error> {
168 Item::from_slice(input).map_err(Into::into)
169}
170
171#[cfg(feature = "std")]
180pub fn read_one(rd: &mut dyn io::BufRead) -> Result<Option<Item>, io::Error> {
181 Item::from_buf(rd).map_err(|err| match err {
182 pem::Error::Io(io) => io,
183 other => Error::from(other).into(),
184 })
185}
186
187#[cfg(feature = "std")]
189pub fn read_all(rd: &mut dyn io::BufRead) -> impl Iterator<Item = Result<Item, io::Error>> + '_ {
190 iter::from_fn(move || read_one(rd).transpose())
191}