@@ -117,6 +117,7 @@ pub enum Error {
117
117
Normal ( ErrorStack ) ,
118
118
Ssl ( ssl:: Error , X509VerifyResult ) ,
119
119
EmptyChain ,
120
+ NotPkcs8 ,
120
121
}
121
122
122
123
impl error:: Error for Error {
@@ -125,6 +126,7 @@ impl error::Error for Error {
125
126
Error :: Normal ( ref e) => error:: Error :: source ( e) ,
126
127
Error :: Ssl ( ref e, _) => error:: Error :: source ( e) ,
127
128
Error :: EmptyChain => None ,
129
+ Error :: NotPkcs8 => None ,
128
130
}
129
131
}
130
132
}
@@ -139,6 +141,7 @@ impl fmt::Display for Error {
139
141
fmt,
140
142
"at least one certificate must be provided to create an identity"
141
143
) ,
144
+ Error :: NotPkcs8 => write ! ( fmt, "expected PKCS#8 PEM" ) ,
142
145
}
143
146
}
144
147
}
@@ -171,6 +174,10 @@ impl Identity {
171
174
}
172
175
173
176
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
+
174
181
let pkey = PKey :: private_key_from_pem ( key) ?;
175
182
let mut cert_chain = X509 :: stack_from_pem ( buf) ?. into_iter ( ) ;
176
183
let cert = cert_chain. next ( ) . ok_or ( Error :: EmptyChain ) ?;
0 commit comments