Skip to content

Commit 7e0fde4

Browse files
committed
allow parsing several certificates from a single pem
Signed-off-by: Marc-Antoine Perennou <[email protected]>
1 parent e092843 commit 7e0fde4

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
@@ -177,6 +177,11 @@ impl Certificate {
177177
Ok(Certificate(cert))
178178
}
179179

180+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
181+
let mut certs = X509::stack_from_pem(buf)?;
182+
Ok(certs.drain(..).map(Certificate).collect())
183+
}
184+
180185
pub fn to_der(&self) -> Result<Vec<u8>, Error> {
181186
let der = self.0.to_der()?;
182187
Ok(der)

src/imp/security_framework.rs

+16
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ impl Certificate {
167167
panic!("Not implemented on iOS");
168168
}
169169

170+
#[cfg(not(target_os = "ios"))]
171+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
172+
let mut items = SecItems::default();
173+
ImportOptions::new().items(&mut items).import(buf)?;
174+
if items.identities.is_empty() && items.keys.is_empty() {
175+
Ok(items.certificates.drain(..).map(Certificate).collect())
176+
} else {
177+
Err(Error(base::Error::from(errSecParam)))
178+
}
179+
}
180+
181+
#[cfg(target_os = "ios")]
182+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>, Error> {
183+
panic!("Not implemented on iOS");
184+
}
185+
170186
pub fn to_der(&self) -> Result<Vec<u8>, Error> {
171187
Ok(self.0.to_der())
172188
}

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ impl Certificate {
198198
Ok(Certificate(cert))
199199
}
200200

201+
/// Parses some PEM-formatted X509 certificates.
202+
pub fn stack_from_pem(buf: &[u8]) -> Result<Vec<Certificate>> {
203+
let mut certs = imp::Certificate::stack_from_pem(buf)?;
204+
Ok(certs.drain(..).map(Certificate).collect())
205+
}
206+
201207
/// Returns the DER-encoded representation of this certificate.
202208
pub fn to_der(&self) -> Result<Vec<u8>> {
203209
let der = self.0.to_der()?;

0 commit comments

Comments
 (0)