Skip to content

Commit 332311b

Browse files
committed
Resolve an injection vulnerability in EKU creation
1 parent 482575b commit 332311b

File tree

3 files changed

+35
-70
lines changed

3 files changed

+35
-70
lines changed

openssl/src/asn1.rs

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::bio::MemBio;
3939
use crate::bn::{BigNum, BigNumRef};
4040
use crate::error::ErrorStack;
4141
use crate::nid::Nid;
42+
use crate::stack::Stackable;
4243
use crate::string::OpensslString;
4344
use crate::{cvt, cvt_p};
4445
use openssl_macros::corresponds;
@@ -592,6 +593,10 @@ foreign_type_and_impl_send_sync! {
592593
pub struct Asn1ObjectRef;
593594
}
594595

596+
impl Stackable for Asn1Object {
597+
type StackType = ffi::stack_st_ASN1_OBJECT;
598+
}
599+
595600
impl Asn1Object {
596601
/// Constructs an ASN.1 Object Identifier from a string representation of the OID.
597602
#[corresponds(OBJ_txt2obj)]

openssl/src/x509/extension.rs

+22-70
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
//! ```
1919
use std::fmt::Write;
2020

21+
use crate::asn1::Asn1Object;
2122
use crate::error::ErrorStack;
2223
use crate::nid::Nid;
23-
use crate::x509::{Asn1Object, GeneralName, Stack, X509Extension, X509v3Context};
24+
use crate::x509::{GeneralName, Stack, X509Extension, X509v3Context};
2425
use foreign_types::ForeignType;
2526

2627
/// An extension which indicates whether a certificate is a CA certificate.
@@ -223,18 +224,7 @@ impl KeyUsage {
223224
/// for which the certificate public key can be used for.
224225
pub struct ExtendedKeyUsage {
225226
critical: bool,
226-
server_auth: bool,
227-
client_auth: bool,
228-
code_signing: bool,
229-
email_protection: bool,
230-
time_stamping: bool,
231-
ms_code_ind: bool,
232-
ms_code_com: bool,
233-
ms_ctl_sign: bool,
234-
ms_sgc: bool,
235-
ms_efs: bool,
236-
ns_sgc: bool,
237-
other: Vec<String>,
227+
items: Vec<String>,
238228
}
239229

240230
impl Default for ExtendedKeyUsage {
@@ -248,18 +238,7 @@ impl ExtendedKeyUsage {
248238
pub fn new() -> ExtendedKeyUsage {
249239
ExtendedKeyUsage {
250240
critical: false,
251-
server_auth: false,
252-
client_auth: false,
253-
code_signing: false,
254-
email_protection: false,
255-
time_stamping: false,
256-
ms_code_ind: false,
257-
ms_code_com: false,
258-
ms_ctl_sign: false,
259-
ms_sgc: false,
260-
ms_efs: false,
261-
ns_sgc: false,
262-
other: vec![],
241+
items: vec![],
263242
}
264243
}
265244

@@ -271,101 +250,74 @@ impl ExtendedKeyUsage {
271250

272251
/// Sets the `serverAuth` flag to `true`.
273252
pub fn server_auth(&mut self) -> &mut ExtendedKeyUsage {
274-
self.server_auth = true;
275-
self
253+
self.other("serverAuth")
276254
}
277255

278256
/// Sets the `clientAuth` flag to `true`.
279257
pub fn client_auth(&mut self) -> &mut ExtendedKeyUsage {
280-
self.client_auth = true;
281-
self
258+
self.other("clientAuth")
282259
}
283260

284261
/// Sets the `codeSigning` flag to `true`.
285262
pub fn code_signing(&mut self) -> &mut ExtendedKeyUsage {
286-
self.code_signing = true;
287-
self
263+
self.other("codeSigning")
288264
}
289265

290266
/// Sets the `emailProtection` flag to `true`.
291267
pub fn email_protection(&mut self) -> &mut ExtendedKeyUsage {
292-
self.email_protection = true;
293-
self
268+
self.other("emailProtection")
294269
}
295270

296271
/// Sets the `timeStamping` flag to `true`.
297272
pub fn time_stamping(&mut self) -> &mut ExtendedKeyUsage {
298-
self.time_stamping = true;
299-
self
273+
self.other("timeStamping")
300274
}
301275

302276
/// Sets the `msCodeInd` flag to `true`.
303277
pub fn ms_code_ind(&mut self) -> &mut ExtendedKeyUsage {
304-
self.ms_code_ind = true;
305-
self
278+
self.other("msCodeInd")
306279
}
307280

308281
/// Sets the `msCodeCom` flag to `true`.
309282
pub fn ms_code_com(&mut self) -> &mut ExtendedKeyUsage {
310-
self.ms_code_com = true;
311-
self
283+
self.other("msCodeCom")
312284
}
313285

314286
/// Sets the `msCTLSign` flag to `true`.
315287
pub fn ms_ctl_sign(&mut self) -> &mut ExtendedKeyUsage {
316-
self.ms_ctl_sign = true;
317-
self
288+
self.other("msCTLSign")
318289
}
319290

320291
/// Sets the `msSGC` flag to `true`.
321292
pub fn ms_sgc(&mut self) -> &mut ExtendedKeyUsage {
322-
self.ms_sgc = true;
323-
self
293+
self.other("msSGC")
324294
}
325295

326296
/// Sets the `msEFS` flag to `true`.
327297
pub fn ms_efs(&mut self) -> &mut ExtendedKeyUsage {
328-
self.ms_efs = true;
329-
self
298+
self.other("msEFS")
330299
}
331300

332301
/// Sets the `nsSGC` flag to `true`.
333302
pub fn ns_sgc(&mut self) -> &mut ExtendedKeyUsage {
334-
self.ns_sgc = true;
335-
self
303+
self.other("nsSGC")
336304
}
337305

338306
/// Sets a flag not already defined.
339307
pub fn other(&mut self, other: &str) -> &mut ExtendedKeyUsage {
340-
self.other.push(other.to_owned());
308+
self.items.push(other.to_string());
341309
self
342310
}
343311

344312
/// Return the `ExtendedKeyUsage` extension as an `X509Extension`.
345313
pub fn build(&self) -> Result<X509Extension, ErrorStack> {
346-
let mut value = String::new();
347-
let mut first = true;
348-
append(&mut value, &mut first, self.critical, "critical");
349-
append(&mut value, &mut first, self.server_auth, "serverAuth");
350-
append(&mut value, &mut first, self.client_auth, "clientAuth");
351-
append(&mut value, &mut first, self.code_signing, "codeSigning");
352-
append(
353-
&mut value,
354-
&mut first,
355-
self.email_protection,
356-
"emailProtection",
357-
);
358-
append(&mut value, &mut first, self.time_stamping, "timeStamping");
359-
append(&mut value, &mut first, self.ms_code_ind, "msCodeInd");
360-
append(&mut value, &mut first, self.ms_code_com, "msCodeCom");
361-
append(&mut value, &mut first, self.ms_ctl_sign, "msCTLSign");
362-
append(&mut value, &mut first, self.ms_sgc, "msSGC");
363-
append(&mut value, &mut first, self.ms_efs, "msEFS");
364-
append(&mut value, &mut first, self.ns_sgc, "nsSGC");
365-
for other in &self.other {
366-
append(&mut value, &mut first, true, other);
314+
let mut stack = Stack::new()?;
315+
for item in &self.items {
316+
stack.push(Asn1Object::from_str(item)?)?;
317+
}
318+
unsafe {
319+
X509Extension::new_internal(Nid::EXT_KEY_USAGE, self.critical, stack.as_ptr().cast())
367320
}
368-
X509Extension::new_nid(None, None, Nid::EXT_KEY_USAGE, &value)
369321
}
370322
}
371323

openssl/src/x509/tests.rs

+8
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ fn x509_extension_to_der() {
325325
}
326326
}
327327

328+
#[test]
329+
fn eku_invalid_other() {
330+
assert!(ExtendedKeyUsage::new()
331+
.other("1.1.1.1.1,2.2.2.2.2")
332+
.build()
333+
.is_err());
334+
}
335+
328336
#[test]
329337
fn x509_req_builder() {
330338
let pkey = pkey();

0 commit comments

Comments
 (0)