@@ -116,13 +116,15 @@ fn load_android_root_certs(connector: &mut SslContextBuilder) -> Result<(), Erro
116116pub enum Error {
117117 Normal ( ErrorStack ) ,
118118 Ssl ( ssl:: Error , X509VerifyResult ) ,
119+ EmptyChain ,
119120}
120121
121122impl error:: Error for Error {
122123 fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
123124 match * self {
124125 Error :: Normal ( ref e) => error:: Error :: source ( e) ,
125126 Error :: Ssl ( ref e, _) => error:: Error :: source ( e) ,
127+ Error :: EmptyChain => None ,
126128 }
127129 }
128130}
@@ -133,6 +135,10 @@ impl fmt::Display for Error {
133135 Error :: Normal ( ref e) => fmt:: Display :: fmt ( e, fmt) ,
134136 Error :: Ssl ( ref e, X509VerifyResult :: OK ) => fmt:: Display :: fmt ( e, fmt) ,
135137 Error :: Ssl ( ref e, v) => write ! ( fmt, "{} ({})" , e, v) ,
138+ Error :: EmptyChain => write ! (
139+ fmt,
140+ "at least one certificate must be provided to create an identity"
141+ ) ,
136142 }
137143 }
138144}
@@ -164,14 +170,9 @@ impl Identity {
164170 pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
165171 let pkey = PKey :: private_key_from_pem ( key) ?;
166172 let mut cert_chain = X509 :: stack_from_pem ( buf) ?. into_iter ( ) ;
167- let cert = cert_chain. next ( ) ;
173+ let cert = cert_chain. next ( ) . ok_or ( Error :: EmptyChain ) ? ;
168174 let chain = cert_chain. collect ( ) ;
169- Ok ( Identity {
170- pkey,
171- // an identity must have at least one certificate, the leaf cert
172- cert : cert. expect ( "at least one certificate must be provided to create an identity" ) ,
173- chain,
174- } )
175+ Ok ( Identity { pkey, cert, chain } )
175176 }
176177}
177178
0 commit comments