Skip to content

Commit beb8ebc

Browse files
committed
cleaned up unwraps and expects
1 parent ab437ba commit beb8ebc

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/imp/openssl.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,17 @@ impl Identity {
171171

172172
pub fn from_pkcs8(buf: &[u8], key: &[u8]) -> Result<Identity, Error> {
173173
let pkey = PKey::private_key_from_pem(key)?;
174-
let mut cert_chain = pem::PemBlock::new(buf).map(|buf| X509::from_pem(buf).unwrap());
174+
let mut cert_chain = vec!();
175+
for buf in pem::PemBlock::new(buf) {
176+
cert_chain.push(X509::from_pem(buf)?);
177+
}
178+
let mut cert_chain = cert_chain.into_iter();
175179
let cert = cert_chain.next();
176180
let chain = cert_chain.collect();
177181
Ok(Identity {
178182
pkey,
179-
cert: cert.expect("need identity cert"),
183+
// an identity must have at least one certificate, the leaf cert
184+
cert: cert.expect("at least one certificate must be provided to create an identity"),
180185
chain: chain,
181186
})
182187
}

src/imp/schannel.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,27 @@ impl Identity {
101101
pub fn from_pkcs8(pem: &[u8], key: &[u8]) -> Result<Identity, Error> {
102102
let mut store = Memory::new()?.into_store();
103103
let mut cert_iter = crate::pem::PemBlock::new(pem).into_iter();
104-
let leaf = cert_iter.next().unwrap();
105-
let cert = CertContext::from_pem(std::str::from_utf8(leaf).unwrap()).unwrap();
104+
let leaf = cert_iter.next().expect("at least one certificate must be provided to create an identity");
105+
let cert = CertContext::from_pem(std::str::from_utf8(leaf).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "leaf cert contains invalid utf8"))?)?;
106106

107107
let mut options = AcquireOptions::new();
108108
options.container("schannel");
109109
let type_ = ProviderType::rsa_full();
110110

111111
let mut container = match options.acquire(type_) {
112112
Ok(container) => container,
113-
Err(_) => options.new_keyset(true).acquire(type_).unwrap(),
113+
Err(_) => options.new_keyset(true).acquire(type_)?,
114114
};
115-
let key = crate::pem::pem_to_der(key, Some(crate::pem::PEM_PRIVATE_KEY)).unwrap();
115+
let key = crate::pem::pem_to_der(key, Some(crate::pem::PEM_PRIVATE_KEY)).expect("invalid PKCS8 key provided");
116116
container.import()
117-
.import_pkcs8(&key)
118-
.unwrap();
117+
.import_pkcs8(&key)?;
119118

120119
cert.set_key_prov_info()
121120
.container("schannel")
122121
.type_(type_)
123122
.keep_open(true)
124123
.key_spec(KeySpec::key_exchange())
125-
.set()
126-
.unwrap();
124+
.set()?;
127125
let mut context = store.add_cert(&cert, CertAdd::Always)?;
128126

129127
for int_cert in cert_iter {

src/imp/security_framework.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ impl Identity {
9191
let dir = TempDir::new().unwrap();
9292
let keychain = keychain::CreateOptions::new()
9393
.password("password")
94-
.create(dir.path().join("identity.keychain"))
95-
.unwrap();
94+
.create(dir.path().join("identity.keychain"))?;
9695

9796
let mut items = SecItems::default();
9897

0 commit comments

Comments
 (0)