@@ -5,19 +5,19 @@ use self::openssl::error::ErrorStack;
55use self :: openssl:: hash:: MessageDigest ;
66use self :: openssl:: nid:: Nid ;
77use self :: openssl:: pkcs12:: Pkcs12 ;
8- use self :: openssl:: pkey:: PKey ;
8+ use self :: openssl:: pkey:: { PKey , Private } ;
99use self :: openssl:: ssl:: {
1010 self , MidHandshakeSslStream , SslAcceptor , SslConnector , SslContextBuilder , SslMethod ,
1111 SslVerifyMode ,
1212} ;
13- use self :: openssl:: x509:: { X509 , X509VerifyResult } ;
13+ use self :: openssl:: x509:: { X509VerifyResult , X509 } ;
1414use std:: error;
1515use std:: fmt;
1616use std:: io;
1717use std:: sync:: { Once , ONCE_INIT } ;
18+ use pem;
1819
1920use { Protocol , TlsAcceptorBuilder , TlsConnectorBuilder } ;
20- use self :: openssl:: pkey:: Private ;
2121
2222#[ cfg( have_min_max_version) ]
2323fn supported_protocols (
@@ -155,7 +155,7 @@ impl From<ErrorStack> for Error {
155155pub struct Identity {
156156 pkey : PKey < Private > ,
157157 cert : X509 ,
158- chain : Vec < X509 > ,
158+ chain : Option < Vec < X509 > > ,
159159}
160160
161161impl Identity {
@@ -165,7 +165,19 @@ impl Identity {
165165 Ok ( Identity {
166166 pkey : parsed. pkey ,
167167 cert : parsed. cert ,
168- chain : parsed. chain . into_iter ( ) . flat_map ( |x| x) . collect ( ) ,
168+ chain : parsed. chain . map ( |stack| stack. into_iter ( ) . collect ( ) ) ,
169+ } )
170+ }
171+
172+ pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
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 ( ) ;
177+ Ok ( Identity {
178+ pkey,
179+ cert : cert. expect ( "need identity cert" ) ,
180+ chain : Some ( chain) ,
169181 } )
170182 }
171183}
@@ -265,8 +277,10 @@ impl TlsConnector {
265277 if let Some ( ref identity) = builder. identity {
266278 connector. set_certificate ( & identity. 0 . cert ) ?;
267279 connector. set_private_key ( & identity. 0 . pkey ) ?;
268- for cert in identity. 0 . chain . iter ( ) . rev ( ) {
269- connector. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
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+ }
270284 }
271285 }
272286 supported_protocols ( builder. min_protocol , builder. max_protocol , & mut connector) ?;
@@ -314,8 +328,10 @@ impl TlsAcceptor {
314328 let mut acceptor = SslAcceptor :: mozilla_intermediate ( SslMethod :: tls ( ) ) ?;
315329 acceptor. set_private_key ( & builder. identity . 0 . pkey ) ?;
316330 acceptor. set_certificate ( & builder. identity . 0 . cert ) ?;
317- for cert in builder. identity . 0 . chain . iter ( ) . rev ( ) {
318- acceptor. add_extra_chain_cert ( cert. to_owned ( ) ) ?;
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+ }
319335 }
320336 supported_protocols ( builder. min_protocol , builder. max_protocol , & mut acceptor) ?;
321337
0 commit comments