@@ -5,19 +5,19 @@ use self::openssl::error::ErrorStack;
5
5
use self :: openssl:: hash:: MessageDigest ;
6
6
use self :: openssl:: nid:: Nid ;
7
7
use self :: openssl:: pkcs12:: Pkcs12 ;
8
- use self :: openssl:: pkey:: PKey ;
8
+ use self :: openssl:: pkey:: { PKey , Private } ;
9
9
use self :: openssl:: ssl:: {
10
10
self , MidHandshakeSslStream , SslAcceptor , SslConnector , SslContextBuilder , SslMethod ,
11
11
SslVerifyMode ,
12
12
} ;
13
- use self :: openssl:: x509:: { X509 , X509VerifyResult } ;
13
+ use self :: openssl:: x509:: { X509VerifyResult , X509 } ;
14
14
use std:: error;
15
15
use std:: fmt;
16
16
use std:: io;
17
17
use std:: sync:: { Once , ONCE_INIT } ;
18
+ use pem;
18
19
19
20
use { Protocol , TlsAcceptorBuilder , TlsConnectorBuilder } ;
20
- use self :: openssl:: pkey:: Private ;
21
21
22
22
#[ cfg( have_min_max_version) ]
23
23
fn supported_protocols (
@@ -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 : Vec < X509 > ,
158
+ chain : Option < Vec < X509 > > ,
159
159
}
160
160
161
161
impl Identity {
@@ -165,7 +165,19 @@ impl Identity {
165
165
Ok ( Identity {
166
166
pkey : parsed. pkey ,
167
167
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) ,
169
181
} )
170
182
}
171
183
}
@@ -265,8 +277,10 @@ impl TlsConnector {
265
277
if let Some ( ref identity) = builder. identity {
266
278
connector. set_certificate ( & identity. 0 . cert ) ?;
267
279
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
+ }
270
284
}
271
285
}
272
286
supported_protocols ( builder. min_protocol , builder. max_protocol , & mut connector) ?;
@@ -314,8 +328,10 @@ impl TlsAcceptor {
314
328
let mut acceptor = SslAcceptor :: mozilla_intermediate ( SslMethod :: tls ( ) ) ?;
315
329
acceptor. set_private_key ( & builder. identity . 0 . pkey ) ?;
316
330
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
+ }
319
335
}
320
336
supported_protocols ( builder. min_protocol , builder. max_protocol , & mut acceptor) ?;
321
337
0 commit comments