use crate::{
attr::{Attribute, AttributeValue, Attributes},
ext::Extension,
name::Name,
};
use alloc::vec::Vec;
use const_oid::db::rfc5912::ID_EXTENSION_REQ;
use const_oid::{AssociatedOid, ObjectIdentifier};
use der::asn1::BitString;
use der::{
asn1::{Any, SetOfVec},
Decode, Enumerated, Sequence,
};
use spki::{AlgorithmIdentifierOwned, SubjectPublicKeyInfoOwned};
#[cfg(feature = "pem")]
use der::pem::PemLabel;
#[derive(Clone, Debug, Copy, PartialEq, Eq, Enumerated, Default)]
#[asn1(type = "INTEGER")]
#[repr(u8)]
pub enum Version {
#[default]
V1 = 0,
}
#[derive(Clone, Debug, PartialEq, Eq, Sequence)]
pub struct CertReqInfo {
pub version: Version,
pub subject: Name,
pub public_key: SubjectPublicKeyInfoOwned,
#[asn1(context_specific = "0", tag_mode = "IMPLICIT")]
pub attributes: Attributes,
}
#[derive(Clone, Debug, PartialEq, Eq, Sequence)]
pub struct CertReq {
pub info: CertReqInfo,
pub algorithm: AlgorithmIdentifierOwned,
pub signature: BitString,
}
#[cfg(feature = "pem")]
impl PemLabel for CertReq {
const PEM_LABEL: &'static str = "CERTIFICATE REQUEST";
}
impl<'a> TryFrom<&'a [u8]> for CertReq {
type Error = der::Error;
fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> {
Self::from_der(bytes)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct ExtensionReq(pub Vec<Extension>);
impl AssociatedOid for ExtensionReq {
const OID: ObjectIdentifier = ID_EXTENSION_REQ;
}
impl_newtype!(ExtensionReq, Vec<Extension>);
impl TryFrom<ExtensionReq> for Attribute {
type Error = der::Error;
fn try_from(extension_req: ExtensionReq) -> der::Result<Attribute> {
let mut values: SetOfVec<AttributeValue> = Default::default();
values.insert(Any::encode_from(&extension_req.0)?)?;
Ok(Attribute {
oid: ExtensionReq::OID,
values,
})
}
}
pub mod attributes {
use alloc::vec;
use const_oid::AssociatedOid;
use der::{
asn1::{Any, ObjectIdentifier, SetOfVec},
EncodeValue, Length, Result, Tag, Tagged, Writer,
};
use crate::{attr::Attribute, ext::pkix::name::DirectoryString};
pub trait AsAttribute: AssociatedOid + Tagged + EncodeValue + Sized {
fn to_attribute(&self) -> Result<Attribute> {
let inner: Any = der::asn1::Any::encode_from(self)?;
let values = SetOfVec::try_from(vec![inner])?;
Ok(Attribute {
oid: Self::OID,
values,
})
}
}
pub struct ChallengePassword(pub DirectoryString);
impl AsAttribute for ChallengePassword {}
impl AssociatedOid for ChallengePassword {
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.9.7");
}
impl Tagged for ChallengePassword {
fn tag(&self) -> Tag {
self.0.tag()
}
}
impl EncodeValue for ChallengePassword {
fn value_len(&self) -> Result<Length> {
self.0.value_len()
}
fn encode_value(&self, encoder: &mut impl Writer) -> Result<()> {
self.0.encode_value(encoder)
}
}
}