Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions src/imp/schannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ impl Certificate {
}
}

pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
pem::PemBlock::new(buf)
.iter()
.map(|pem| CertContext::from_pem(pem).map(Certificate))
.collect()
}

pub fn to_der(&self) -> Result<Vec<u8>, Error> {
Ok(self.0.to_der().to_vec())
}
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