Skip to content

Commit

Permalink
allow parsing several certificates from a single pem
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-Antoine Perennou <[email protected]>
  • Loading branch information
Keruspe committed Mar 28, 2022
1 parent 2496ac8 commit 73a09c9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/imp/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ impl Certificate {
Ok(Certificate(cert))
}

pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
let mut certs = X509::stack_from_pem(buf)?;
Ok(certs.drain(..).map(Certificate).collect())
}

pub fn to_der(&self) -> Result<Vec<u8>, Error> {
let der = self.0.to_der()?;
Ok(der)
Expand Down
16 changes: 16 additions & 0 deletions src/imp/security_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ impl Certificate {
panic!("Not implemented on iOS");
}

#[cfg(not(target_os = "ios"))]
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
let mut items = SecItems::default();
ImportOptions::new().items(&mut items).import(buf)?;
if items.identities.is_empty() && items.keys.is_empty() {
Ok(items.certificates.drain(..).map(Certificate).collect())
} else {
Err(Error(base::Error::from(errSecParam)))
}
}

#[cfg(target_os = "ios")]
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
panic!("Not implemented on iOS");
}

pub fn to_der(&self) -> Result<Vec<u8>, Error> {
Ok(self.0.to_der())
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ impl Certificate {
Ok(Certificate(cert))
}

/// Parses some PEM-formatted X509 certificates.
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>> {
let mut certs = imp::Certificate::stack_from_pem(buf)?;
Ok(certs.drain(..).map(Certificate).collect())
}

/// Returns the DER-encoded representation of this certificate.
pub fn to_der(&self) -> Result<Vec<u8>> {
let der = self.0.to_der()?;
Expand Down

0 comments on commit 73a09c9

Please sign in to comment.