Skip to content

Commit 73a09c9

Browse files
committed
allow parsing several certificates from a single pem
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
1 parent 2496ac8 commit 73a09c9

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/imp/openssl.rs

+5
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ impl Certificate {
200200
Ok(Certificate(cert))
201201
}
202202

203+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
204+
let mut certs = X509::stack_from_pem(buf)?;
205+
Ok(certs.drain(..).map(Certificate).collect())
206+
}
207+
203208
pub fn to_der(&self) -> Result<Vec<u8>, Error> {
204209
let der = self.0.to_der()?;
205210
Ok(der)

src/imp/security_framework.rs

+16
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ impl Certificate {
219219
panic!("Not implemented on iOS");
220220
}
221221

222+
#[cfg(not(target_os = "ios"))]
223+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
224+
let mut items = SecItems::default();
225+
ImportOptions::new().items(&mut items).import(buf)?;
226+
if items.identities.is_empty() && items.keys.is_empty() {
227+
Ok(items.certificates.drain(..).map(Certificate).collect())
228+
} else {
229+
Err(Error(base::Error::from(errSecParam)))
230+
}
231+
}
232+
233+
#[cfg(target_os = "ios")]
234+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
235+
panic!("Not implemented on iOS");
236+
}
237+
222238
pub fn to_der(&self) -> Result<Vec<u8>, Error> {
223239
Ok(self.0.to_der())
224240
}

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ impl Certificate {
209209
Ok(Certificate(cert))
210210
}
211211

212+
/// Parses some PEM-formatted X509 certificates.
213+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>> {
214+
let mut certs = imp::Certificate::stack_from_pem(buf)?;
215+
Ok(certs.drain(..).map(Certificate).collect())
216+
}
217+
212218
/// Returns the DER-encoded representation of this certificate.
213219
pub fn to_der(&self) -> Result<Vec<u8>> {
214220
let der = self.0.to_der()?;

0 commit comments

Comments
 (0)