@@ -10,7 +10,7 @@ use self::openssl::ssl::{
10
10
self , MidHandshakeSslStream , SslAcceptor , SslConnector , SslContextBuilder , SslMethod ,
11
11
SslVerifyMode ,
12
12
} ;
13
- use self :: openssl:: x509:: { X509VerifyResult , X509 } ;
13
+ use self :: openssl:: x509:: { X509 , X509VerifyResult } ;
14
14
use std:: error;
15
15
use std:: fmt;
16
16
use std:: io;
@@ -155,7 +155,7 @@ impl From<ErrorStack> for Error {
155
155
pub struct Identity {
156
156
pkey : PKey < Private > ,
157
157
cert : X509 ,
158
- chain : Option < Vec < X509 > > ,
158
+ chain : Vec < X509 > ,
159
159
}
160
160
161
161
impl Identity {
@@ -165,19 +165,19 @@ impl Identity {
165
165
Ok ( Identity {
166
166
pkey : parsed. pkey ,
167
167
cert : parsed. cert ,
168
- chain : parsed. chain . map ( |stack| stack . into_iter ( ) . collect ( ) ) ,
168
+ chain : parsed. chain . into_iter ( ) . flat_map ( |x| x ) . collect ( ) ,
169
169
} )
170
170
}
171
171
172
172
pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
173
173
let pkey = PKey :: private_key_from_pem ( key) ?;
174
- let p_block = pem:: PemBlock :: new ( buf) ;
175
- let mut chain : Vec < X509 > = p_block . map ( |buf| X509 :: from_pem ( buf ) . unwrap ( ) ) . collect ( ) ;
176
- let cert = chain . pop ( ) ;
174
+ let mut cert_chain = pem:: PemBlock :: new ( buf) . map ( |buf| X509 :: from_pem ( buf ) . unwrap ( ) ) ;
175
+ let cert = cert_chain . next ( ) ;
176
+ let chain = cert_chain . collect ( ) ;
177
177
Ok ( Identity {
178
178
pkey,
179
179
cert : cert. expect ( "need identity cert" ) ,
180
- chain : Some ( chain) ,
180
+ chain : chain,
181
181
} )
182
182
}
183
183
}
@@ -277,10 +277,11 @@ impl TlsConnector {
277
277
if let Some ( ref identity) = builder. identity {
278
278
connector. set_certificate ( & identity. 0 . cert ) ?;
279
279
connector. set_private_key ( & identity. 0 . pkey ) ?;
280
- if let Some ( ref chain) = identity. 0 . chain {
281
- for cert in chain. iter ( ) . rev ( ) {
282
- connector. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
283
- }
280
+ for cert in identity. 0 . chain . iter ( ) {
281
+ // https://www.openssl.org/docs/manmaster/man3/SSL_CTX_add_extra_chain_cert.html
282
+ // specifies that "When sending a certificate chain, extra chain certificates are
283
+ // sent in order following the end entity certificate."
284
+ connector. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
284
285
}
285
286
}
286
287
supported_protocols ( builder. min_protocol , builder. max_protocol , & mut connector) ?;
@@ -328,10 +329,11 @@ impl TlsAcceptor {
328
329
let mut acceptor = SslAcceptor :: mozilla_intermediate ( SslMethod :: tls ( ) ) ?;
329
330
acceptor. set_private_key ( & builder. identity . 0 . pkey ) ?;
330
331
acceptor. set_certificate ( & builder. identity . 0 . cert ) ?;
331
- if let Some ( ref chain) = builder. identity . 0 . chain {
332
- for cert in chain. iter ( ) . rev ( ) {
333
- acceptor. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
334
- }
332
+ for cert in builder. identity . 0 . chain . iter ( ) {
333
+ // https://www.openssl.org/docs/manmaster/man3/SSL_CTX_add_extra_chain_cert.html
334
+ // specifies that "When sending a certificate chain, extra chain certificates are
335
+ // sent in order following the end entity certificate."
336
+ acceptor. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
335
337
}
336
338
supported_protocols ( builder. min_protocol , builder. max_protocol , & mut acceptor) ?;
337
339
0 commit comments