|
1 | 1 | use anyhow::{anyhow, Context, Result}; |
2 | 2 | use chrono::{DateTime, Utc}; |
3 | 3 | #[cfg(feature = "tls")] |
4 | | -use rustls_pki_types::{CertificateDer, PrivateKeyDer}; |
| 4 | +use rustls_pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer}; |
5 | 5 | use std::{ |
6 | 6 | borrow::Cow, |
7 | 7 | path::Path, |
@@ -62,42 +62,40 @@ pub fn glob(pattern: &str, target: &str) -> bool { |
62 | 62 |
|
63 | 63 | // Load public certificate from file. |
64 | 64 | #[cfg(feature = "tls")] |
65 | | -pub fn load_certs<T: AsRef<Path>>(filename: T) -> Result<Vec<CertificateDer<'static>>> { |
66 | | - // Open certificate file. |
67 | | - let cert_file = std::fs::File::open(filename.as_ref()) |
68 | | - .with_context(|| format!("Failed to access `{}`", filename.as_ref().display()))?; |
69 | | - let mut reader = std::io::BufReader::new(cert_file); |
70 | | - |
71 | | - // Load and return certificate. |
| 65 | +pub fn load_certs<T: AsRef<Path>>(file_name: T) -> Result<Vec<CertificateDer<'static>>> { |
72 | 66 | let mut certs = vec![]; |
73 | | - for cert in rustls_pemfile::certs(&mut reader) { |
74 | | - let cert = cert.with_context(|| "Failed to load certificate")?; |
| 67 | + for cert in CertificateDer::pem_file_iter(file_name.as_ref()).with_context(|| { |
| 68 | + format!( |
| 69 | + "Failed to load cert file at `{}`", |
| 70 | + file_name.as_ref().display() |
| 71 | + ) |
| 72 | + })? { |
| 73 | + let cert = cert.with_context(|| { |
| 74 | + format!( |
| 75 | + "Invalid certificate data in file `{}`", |
| 76 | + file_name.as_ref().display() |
| 77 | + ) |
| 78 | + })?; |
75 | 79 | certs.push(cert) |
76 | 80 | } |
77 | 81 | if certs.is_empty() { |
78 | | - anyhow::bail!("No supported certificate in file"); |
| 82 | + anyhow::bail!( |
| 83 | + "No supported certificate in file `{}`", |
| 84 | + file_name.as_ref().display() |
| 85 | + ); |
79 | 86 | } |
80 | 87 | Ok(certs) |
81 | 88 | } |
82 | 89 |
|
83 | 90 | // Load private key from file. |
84 | 91 | #[cfg(feature = "tls")] |
85 | | -pub fn load_private_key<T: AsRef<Path>>(filename: T) -> Result<PrivateKeyDer<'static>> { |
86 | | - let key_file = std::fs::File::open(filename.as_ref()) |
87 | | - .with_context(|| format!("Failed to access `{}`", filename.as_ref().display()))?; |
88 | | - let mut reader = std::io::BufReader::new(key_file); |
89 | | - |
90 | | - // Load and return a single private key. |
91 | | - for key in rustls_pemfile::read_all(&mut reader) { |
92 | | - let key = key.with_context(|| "There was a problem with reading private key")?; |
93 | | - match key { |
94 | | - rustls_pemfile::Item::Pkcs1Key(key) => return Ok(PrivateKeyDer::Pkcs1(key)), |
95 | | - rustls_pemfile::Item::Pkcs8Key(key) => return Ok(PrivateKeyDer::Pkcs8(key)), |
96 | | - rustls_pemfile::Item::Sec1Key(key) => return Ok(PrivateKeyDer::Sec1(key)), |
97 | | - _ => {} |
98 | | - } |
99 | | - } |
100 | | - anyhow::bail!("No supported private key in file"); |
| 92 | +pub fn load_private_key<T: AsRef<Path>>(file_name: T) -> Result<PrivateKeyDer<'static>> { |
| 93 | + PrivateKeyDer::from_pem_file(file_name.as_ref()).with_context(|| { |
| 94 | + format!( |
| 95 | + "Failed to load key file at `{}`", |
| 96 | + file_name.as_ref().display() |
| 97 | + ) |
| 98 | + }) |
101 | 99 | } |
102 | 100 |
|
103 | 101 | pub fn parse_range(range: &str, size: u64) -> Option<Vec<(u64, u64)>> { |
|
0 commit comments