Resolve an injection vulnerability in EKU creation
This commit is contained in:
parent
1eea7c5271
commit
0f28001027
|
|
@ -38,6 +38,7 @@ use crate::bio::MemBio;
|
||||||
use crate::bn::{BigNum, BigNumRef};
|
use crate::bn::{BigNum, BigNumRef};
|
||||||
use crate::error::ErrorStack;
|
use crate::error::ErrorStack;
|
||||||
use crate::nid::Nid;
|
use crate::nid::Nid;
|
||||||
|
use crate::stack::Stackable;
|
||||||
use crate::string::OpensslString;
|
use crate::string::OpensslString;
|
||||||
use crate::{cvt, cvt_p};
|
use crate::{cvt, cvt_p};
|
||||||
|
|
||||||
|
|
@ -555,6 +556,10 @@ foreign_type_and_impl_send_sync! {
|
||||||
pub struct Asn1Object;
|
pub struct Asn1Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Stackable for Asn1Object {
|
||||||
|
type StackType = ffi::stack_st_ASN1_OBJECT;
|
||||||
|
}
|
||||||
|
|
||||||
impl Asn1Object {
|
impl Asn1Object {
|
||||||
/// Constructs an ASN.1 Object Identifier from a string representation of
|
/// Constructs an ASN.1 Object Identifier from a string representation of
|
||||||
/// the OID.
|
/// the OID.
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,10 @@
|
||||||
//! ```
|
//! ```
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use crate::asn1::Asn1Object;
|
||||||
use crate::error::ErrorStack;
|
use crate::error::ErrorStack;
|
||||||
use crate::nid::Nid;
|
use crate::nid::Nid;
|
||||||
use crate::x509::{Asn1Object, GeneralName, Stack, X509Extension, X509v3Context};
|
use crate::x509::{GeneralName, Stack, X509Extension, X509v3Context};
|
||||||
use foreign_types::ForeignType;
|
use foreign_types::ForeignType;
|
||||||
|
|
||||||
/// An extension which indicates whether a certificate is a CA certificate.
|
/// An extension which indicates whether a certificate is a CA certificate.
|
||||||
|
|
@ -222,18 +223,7 @@ impl KeyUsage {
|
||||||
/// for which the certificate public key can be used for.
|
/// for which the certificate public key can be used for.
|
||||||
pub struct ExtendedKeyUsage {
|
pub struct ExtendedKeyUsage {
|
||||||
critical: bool,
|
critical: bool,
|
||||||
server_auth: bool,
|
items: Vec<String>,
|
||||||
client_auth: bool,
|
|
||||||
code_signing: bool,
|
|
||||||
email_protection: bool,
|
|
||||||
time_stamping: bool,
|
|
||||||
ms_code_ind: bool,
|
|
||||||
ms_code_com: bool,
|
|
||||||
ms_ctl_sign: bool,
|
|
||||||
ms_sgc: bool,
|
|
||||||
ms_efs: bool,
|
|
||||||
ns_sgc: bool,
|
|
||||||
other: Vec<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ExtendedKeyUsage {
|
impl Default for ExtendedKeyUsage {
|
||||||
|
|
@ -247,18 +237,7 @@ impl ExtendedKeyUsage {
|
||||||
pub fn new() -> ExtendedKeyUsage {
|
pub fn new() -> ExtendedKeyUsage {
|
||||||
ExtendedKeyUsage {
|
ExtendedKeyUsage {
|
||||||
critical: false,
|
critical: false,
|
||||||
server_auth: false,
|
items: vec![],
|
||||||
client_auth: false,
|
|
||||||
code_signing: false,
|
|
||||||
email_protection: false,
|
|
||||||
time_stamping: false,
|
|
||||||
ms_code_ind: false,
|
|
||||||
ms_code_com: false,
|
|
||||||
ms_ctl_sign: false,
|
|
||||||
ms_sgc: false,
|
|
||||||
ms_efs: false,
|
|
||||||
ns_sgc: false,
|
|
||||||
other: vec![],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -270,95 +249,69 @@ impl ExtendedKeyUsage {
|
||||||
|
|
||||||
/// Sets the `serverAuth` flag to `true`.
|
/// Sets the `serverAuth` flag to `true`.
|
||||||
pub fn server_auth(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn server_auth(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.server_auth = true;
|
self.other("serverAuth")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `clientAuth` flag to `true`.
|
/// Sets the `clientAuth` flag to `true`.
|
||||||
pub fn client_auth(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn client_auth(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.client_auth = true;
|
self.other("clientAuth")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `codeSigning` flag to `true`.
|
/// Sets the `codeSigning` flag to `true`.
|
||||||
pub fn code_signing(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn code_signing(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.code_signing = true;
|
self.other("codeSigning")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `timeStamping` flag to `true`.
|
/// Sets the `timeStamping` flag to `true`.
|
||||||
pub fn time_stamping(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn time_stamping(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.time_stamping = true;
|
self.other("timeStamping")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `msCodeInd` flag to `true`.
|
/// Sets the `msCodeInd` flag to `true`.
|
||||||
pub fn ms_code_ind(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ms_code_ind(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ms_code_ind = true;
|
self.other("msCodeInd")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `msCodeCom` flag to `true`.
|
/// Sets the `msCodeCom` flag to `true`.
|
||||||
pub fn ms_code_com(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ms_code_com(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ms_code_com = true;
|
self.other("msCodeCom")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `msCTLSign` flag to `true`.
|
/// Sets the `msCTLSign` flag to `true`.
|
||||||
pub fn ms_ctl_sign(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ms_ctl_sign(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ms_ctl_sign = true;
|
self.other("msCTLSign")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `msSGC` flag to `true`.
|
/// Sets the `msSGC` flag to `true`.
|
||||||
pub fn ms_sgc(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ms_sgc(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ms_sgc = true;
|
self.other("msSGC")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `msEFS` flag to `true`.
|
/// Sets the `msEFS` flag to `true`.
|
||||||
pub fn ms_efs(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ms_efs(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ms_efs = true;
|
self.other("msEFS")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the `nsSGC` flag to `true`.
|
/// Sets the `nsSGC` flag to `true`.
|
||||||
pub fn ns_sgc(&mut self) -> &mut ExtendedKeyUsage {
|
pub fn ns_sgc(&mut self) -> &mut ExtendedKeyUsage {
|
||||||
self.ns_sgc = true;
|
self.other("nsSGC")
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a flag not already defined.
|
/// Sets a flag not already defined.
|
||||||
pub fn other(&mut self, other: &str) -> &mut ExtendedKeyUsage {
|
pub fn other(&mut self, other: &str) -> &mut ExtendedKeyUsage {
|
||||||
self.other.push(other.to_owned());
|
self.items.push(other.to_string());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the `ExtendedKeyUsage` extension as an `X509Extension`.
|
/// Return the `ExtendedKeyUsage` extension as an `X509Extension`.
|
||||||
pub fn build(&self) -> Result<X509Extension, ErrorStack> {
|
pub fn build(&self) -> Result<X509Extension, ErrorStack> {
|
||||||
let mut value = String::new();
|
let mut stack = Stack::new()?;
|
||||||
let mut first = true;
|
for item in &self.items {
|
||||||
append(&mut value, &mut first, self.critical, "critical");
|
stack.push(Asn1Object::from_str(item)?)?;
|
||||||
append(&mut value, &mut first, self.server_auth, "serverAuth");
|
}
|
||||||
append(&mut value, &mut first, self.client_auth, "clientAuth");
|
unsafe {
|
||||||
append(&mut value, &mut first, self.code_signing, "codeSigning");
|
X509Extension::new_internal(Nid::EXT_KEY_USAGE, self.critical, stack.as_ptr().cast())
|
||||||
append(
|
|
||||||
&mut value,
|
|
||||||
&mut first,
|
|
||||||
self.email_protection,
|
|
||||||
"emailProtection",
|
|
||||||
);
|
|
||||||
append(&mut value, &mut first, self.time_stamping, "timeStamping");
|
|
||||||
append(&mut value, &mut first, self.ms_code_ind, "msCodeInd");
|
|
||||||
append(&mut value, &mut first, self.ms_code_com, "msCodeCom");
|
|
||||||
append(&mut value, &mut first, self.ms_ctl_sign, "msCTLSign");
|
|
||||||
append(&mut value, &mut first, self.ms_sgc, "msSGC");
|
|
||||||
append(&mut value, &mut first, self.ms_efs, "msEFS");
|
|
||||||
append(&mut value, &mut first, self.ns_sgc, "nsSGC");
|
|
||||||
for other in &self.other {
|
|
||||||
append(&mut value, &mut first, true, other);
|
|
||||||
}
|
}
|
||||||
X509Extension::new_nid(None, None, Nid::EXT_KEY_USAGE, &value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -288,6 +288,14 @@ fn x509_extension_to_der() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn eku_invalid_other() {
|
||||||
|
assert!(ExtendedKeyUsage::new()
|
||||||
|
.other("1.1.1.1.1,2.2.2.2.2")
|
||||||
|
.build()
|
||||||
|
.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn x509_req_builder() {
|
fn x509_req_builder() {
|
||||||
let pkey = pkey();
|
let pkey = pkey();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue