@@ -116,13 +116,15 @@ fn load_android_root_certs(connector: &mut SslContextBuilder) -> Result<(), Erro
116
116
pub enum Error {
117
117
Normal ( ErrorStack ) ,
118
118
Ssl ( ssl:: Error , X509VerifyResult ) ,
119
+ EmptyChain ,
119
120
}
120
121
121
122
impl error:: Error for Error {
122
123
fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
123
124
match * self {
124
125
Error :: Normal ( ref e) => error:: Error :: source ( e) ,
125
126
Error :: Ssl ( ref e, _) => error:: Error :: source ( e) ,
127
+ Error :: EmptyChain => None ,
126
128
}
127
129
}
128
130
}
@@ -133,6 +135,10 @@ impl fmt::Display for Error {
133
135
Error :: Normal ( ref e) => fmt:: Display :: fmt ( e, fmt) ,
134
136
Error :: Ssl ( ref e, X509VerifyResult :: OK ) => fmt:: Display :: fmt ( e, fmt) ,
135
137
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
+ ) ,
136
142
}
137
143
}
138
144
}
@@ -164,14 +170,9 @@ impl Identity {
164
170
pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
165
171
let pkey = PKey :: private_key_from_pem ( key) ?;
166
172
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 ) ? ;
168
174
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 } )
175
176
}
176
177
}
177
178
0 commit comments