11extern crate openssl;
22extern crate openssl_probe;
3+ extern crate pem_rfc7468;
34
45use self :: openssl:: error:: ErrorStack ;
56use self :: openssl:: hash:: MessageDigest ;
@@ -117,6 +118,8 @@ pub enum Error {
117118 Normal ( ErrorStack ) ,
118119 Ssl ( ssl:: Error , X509VerifyResult ) ,
119120 EmptyChain ,
121+ DecodePemLabel ( pem_rfc7468:: Error ) ,
122+ NotPkcs8 ,
120123}
121124
122125impl error:: Error for Error {
@@ -125,6 +128,8 @@ impl error::Error for Error {
125128 Error :: Normal ( ref e) => error:: Error :: source ( e) ,
126129 Error :: Ssl ( ref e, _) => error:: Error :: source ( e) ,
127130 Error :: EmptyChain => None ,
131+ Error :: DecodePemLabel ( ref e) => error:: Error :: source ( e) ,
132+ Error :: NotPkcs8 => None ,
128133 }
129134 }
130135}
@@ -139,6 +144,8 @@ impl fmt::Display for Error {
139144 fmt,
140145 "at least one certificate must be provided to create an identity"
141146 ) ,
147+ Error :: DecodePemLabel ( ref e) => fmt:: Display :: fmt ( e, fmt) ,
148+ Error :: NotPkcs8 => write ! ( fmt, "expected PKCS#8 PEM" ) ,
142149 }
143150 }
144151}
@@ -171,6 +178,11 @@ impl Identity {
171178 }
172179
173180 pub fn from_pkcs8 ( buf : & [ u8 ] , key : & [ u8 ] ) -> Result < Identity , Error > {
181+ let label = pem_rfc7468:: decode_label ( key) . map_err ( Error :: DecodePemLabel ) ?;
182+ if label != "PRIVATE KEY" {
183+ return Err ( Error :: NotPkcs8 ) ;
184+ }
185+
174186 let pkey = PKey :: private_key_from_pem ( key) ?;
175187 let mut cert_chain = X509 :: stack_from_pem ( buf) ?. into_iter ( ) ;
176188 let cert = cert_chain. next ( ) . ok_or ( Error :: EmptyChain ) ?;
0 commit comments