@@ -117,6 +117,7 @@ pub enum Error {
117117 Normal ( ErrorStack ) ,
118118 Ssl ( ssl:: Error , X509VerifyResult ) ,
119119 EmptyChain ,
120+ NotPkcs8 ,
120121}
121122
122123impl error:: Error for Error {
@@ -125,6 +126,7 @@ impl error::Error for Error {
125126 Error :: Normal ( ref e) => error:: Error :: source ( e) ,
126127 Error :: Ssl ( ref e, _) => error:: Error :: source ( e) ,
127128 Error :: EmptyChain => None ,
129+ Error :: NotPkcs8 => None ,
128130 }
129131 }
130132}
@@ -139,6 +141,7 @@ impl fmt::Display for Error {
139141 fmt,
140142 "at least one certificate must be provided to create an identity"
141143 ) ,
144+ Error :: NotPkcs8 => write ! ( fmt, "expected PKCS#8 PEM" ) ,
142145 }
143146 }
144147}
@@ -171,6 +174,10 @@ impl Identity {
171174 }
172175
173176 pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
177+ if !key. starts_with ( b"-----BEGIN PRIVATE KEY-----" ) {
178+ return Err ( Error :: NotPkcs8 ) ;
179+ }
180+
174181 let pkey = PKey :: private_key_from_pem ( key) ?;
175182 let mut cert_chain = X509 :: stack_from_pem ( buf) ?. into_iter ( ) ;
176183 let cert = cert_chain. next ( ) . ok_or ( Error :: EmptyChain ) ?;
0 commit comments