Skip to content

Commit

Permalink
Consistently spell KeyPair as two words
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jan 17, 2024
1 parent 127c711 commit 224d629
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
62 changes: 31 additions & 31 deletions rustls-cert-gen/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PemCertifiedKey {
#[derive(Default)]
pub struct CertificateBuilder {
params: CertificateParams,
alg: KeypairAlgorithm,
alg: KeyPairAlgorithm,
}

impl CertificateBuilder {
Expand All @@ -50,11 +50,11 @@ impl CertificateBuilder {
params.distinguished_name = DistinguishedName::new();
Self {
params,
alg: KeypairAlgorithm::EcdsaP256,
alg: KeyPairAlgorithm::EcdsaP256,
}
}
/// Set signature algorithm (instead of default).
pub fn signature_algorithm(mut self, alg: KeypairAlgorithm) -> anyhow::Result<Self> {
pub fn signature_algorithm(mut self, alg: KeyPairAlgorithm) -> anyhow::Result<Self> {
self.alg = alg;
Ok(self)
}
Expand All @@ -76,12 +76,12 @@ impl CertificateBuilder {
/// [CertificateParams] from which an [Ca] [Certificate] can be built
pub struct CaBuilder {
params: CertificateParams,
alg: KeypairAlgorithm,
alg: KeyPairAlgorithm,
}

impl CaBuilder {
/// Initialize `CaBuilder`
pub fn new(mut params: CertificateParams, alg: KeypairAlgorithm) -> Self {
pub fn new(mut params: CertificateParams, alg: KeyPairAlgorithm) -> Self {
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
params.key_usages.push(KeyUsagePurpose::DigitalSignature);
params.key_usages.push(KeyUsagePurpose::KeyCertSign);
Expand All @@ -106,7 +106,7 @@ impl CaBuilder {
}
/// build `Ca` Certificate.
pub fn build(self) -> Result<Ca, rcgen::Error> {
let key_pair = self.alg.to_keypair()?;
let key_pair = self.alg.to_key_pair()?;
let cert = Certificate::generate_self_signed(self.params, &key_pair)?;
Ok(Ca { cert, key_pair })
}
Expand Down Expand Up @@ -152,12 +152,12 @@ impl EndEntity {
/// [CertificateParams] from which an [EndEntity] [Certificate] can be built
pub struct EndEntityBuilder {
params: CertificateParams,
alg: KeypairAlgorithm,
alg: KeyPairAlgorithm,
}

impl EndEntityBuilder {
/// Initialize `EndEntityBuilder`
pub fn new(mut params: CertificateParams, alg: KeypairAlgorithm) -> Self {
pub fn new(mut params: CertificateParams, alg: KeyPairAlgorithm) -> Self {
params.is_ca = IsCa::NoCa;
params.use_authority_key_identifier_extension = true;
params.key_usages.push(KeyUsagePurpose::DigitalSignature);
Expand Down Expand Up @@ -196,36 +196,36 @@ impl EndEntityBuilder {
}
/// build `EndEntity` Certificate.
pub fn build(self, issuer: &Ca) -> Result<EndEntity, rcgen::Error> {
let key_pair = self.alg.to_keypair()?;
let key_pair = self.alg.to_key_pair()?;
let cert = Certificate::generate(self.params, &key_pair, &issuer.cert, &issuer.key_pair)?;
Ok(EndEntity { cert, key_pair })
}
}

/// Supported Keypair Algorithms
#[derive(Clone, Copy, Debug, Default, Bpaf, PartialEq)]
pub enum KeypairAlgorithm {
pub enum KeyPairAlgorithm {
Ed25519,
#[default]
EcdsaP256,
EcdsaP384,
}

impl fmt::Display for KeypairAlgorithm {
impl fmt::Display for KeyPairAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
KeypairAlgorithm::Ed25519 => write!(f, "ed25519"),
KeypairAlgorithm::EcdsaP256 => write!(f, "ecdsa-p256"),
KeypairAlgorithm::EcdsaP384 => write!(f, "ecdsa-p384"),
KeyPairAlgorithm::Ed25519 => write!(f, "ed25519"),
KeyPairAlgorithm::EcdsaP256 => write!(f, "ecdsa-p256"),
KeyPairAlgorithm::EcdsaP384 => write!(f, "ecdsa-p384"),
}
}
}

impl KeypairAlgorithm {
impl KeyPairAlgorithm {
/// Return an `rcgen::KeyPair` for the given varient
fn to_keypair(&self) -> Result<rcgen::KeyPair, rcgen::Error> {
fn to_key_pair(self) -> Result<rcgen::KeyPair, rcgen::Error> {
match self {
KeypairAlgorithm::Ed25519 => {
KeyPairAlgorithm::Ed25519 => {
use ring::signature::Ed25519KeyPair;

let rng = ring::rand::SystemRandom::new();
Expand All @@ -235,7 +235,7 @@ impl KeypairAlgorithm {

rcgen::KeyPair::from_der_and_sign_algo(pkcs8_bytes.as_ref(), alg)
},
KeypairAlgorithm::EcdsaP256 => {
KeyPairAlgorithm::EcdsaP256 => {
use ring::signature::EcdsaKeyPair;
use ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING;

Expand All @@ -246,7 +246,7 @@ impl KeypairAlgorithm {
.or(Err(rcgen::Error::RingUnspecified))?;
rcgen::KeyPair::from_der_and_sign_algo(pkcs8_bytes.as_ref(), alg)
},
KeypairAlgorithm::EcdsaP384 => {
KeyPairAlgorithm::EcdsaP384 => {
use ring::signature::EcdsaKeyPair;
use ring::signature::ECDSA_P384_SHA384_ASN1_SIGNING;

Expand Down Expand Up @@ -298,7 +298,7 @@ mod tests {
fn with_sig_algo_default() -> anyhow::Result<()> {
let end_entity = CertificateBuilder::new().end_entity();

assert_eq!(end_entity.alg, KeypairAlgorithm::EcdsaP256);
assert_eq!(end_entity.alg, KeyPairAlgorithm::EcdsaP256);
Ok(())
}
#[test]
Expand All @@ -324,7 +324,7 @@ mod tests {
fn serialize_end_entity_ecdsa_p384_sha384_sig() -> anyhow::Result<()> {
let ca = CertificateBuilder::new().certificate_authority().build()?;
let end_entity = CertificateBuilder::new()
.signature_algorithm(KeypairAlgorithm::EcdsaP384)?
.signature_algorithm(KeyPairAlgorithm::EcdsaP384)?
.end_entity()
.build(&ca)?
.serialize_pem();
Expand All @@ -343,7 +343,7 @@ mod tests {
fn serialize_end_entity_ed25519_sig() -> anyhow::Result<()> {
let ca = CertificateBuilder::new().certificate_authority().build()?;
let end_entity = CertificateBuilder::new()
.signature_algorithm(KeypairAlgorithm::Ed25519)?
.signature_algorithm(KeyPairAlgorithm::Ed25519)?
.end_entity()
.build(&ca)?
.serialize_pem();
Expand All @@ -365,7 +365,7 @@ mod tests {
#[test]
fn init_end_endity() {
let params = CertificateParams::default();
let cert = EndEntityBuilder::new(params, KeypairAlgorithm::default());
let cert = EndEntityBuilder::new(params, KeyPairAlgorithm::default());
assert_eq!(cert.params.is_ca, IsCa::NoCa)
}
#[test]
Expand All @@ -375,7 +375,7 @@ mod tests {
.build()
.unwrap();
let params = CertificateParams::default();
let mut cert = EndEntityBuilder::new(params, KeypairAlgorithm::default());
let mut cert = EndEntityBuilder::new(params, KeyPairAlgorithm::default());
assert_eq!(cert.params.is_ca, IsCa::NoCa);
assert_eq!(
cert.client_auth().params.extended_key_usages,
Expand All @@ -389,7 +389,7 @@ mod tests {
.build()
.unwrap();
let params = CertificateParams::default();
let mut cert = EndEntityBuilder::new(params, KeypairAlgorithm::default());
let mut cert = EndEntityBuilder::new(params, KeyPairAlgorithm::default());
assert_eq!(cert.params.is_ca, IsCa::NoCa);
assert_eq!(
cert.server_auth().params.extended_key_usages,
Expand All @@ -405,7 +405,7 @@ mod tests {
let name = "unexpected.oomyoo.xyz";
let names = vec![SanType::DnsName(name.into())];
let params = CertificateParams::default();
let cert = EndEntityBuilder::new(params, KeypairAlgorithm::default())
let cert = EndEntityBuilder::new(params, KeyPairAlgorithm::default())
.subject_alternative_names(names);
assert_eq!(
cert.params.subject_alt_names,
Expand All @@ -420,21 +420,21 @@ mod tests {
.unwrap();
let names = vec![];
let params = CertificateParams::default();
let cert = EndEntityBuilder::new(params, KeypairAlgorithm::default())
let cert = EndEntityBuilder::new(params, KeyPairAlgorithm::default())
.subject_alternative_names(names);
assert_eq!(cert.params.subject_alt_names, vec![]);
}

#[test]
fn keypair_algorithm_to_keypair() -> anyhow::Result<()> {
let keypair = KeypairAlgorithm::Ed25519.to_keypair()?;
fn key_pair_algorithm_to_keypair() -> anyhow::Result<()> {
let keypair = KeyPairAlgorithm::Ed25519.to_key_pair()?;
assert_eq!(format!("{:?}", keypair.algorithm()), "PKCS_ED25519");
let keypair = KeypairAlgorithm::EcdsaP256.to_keypair()?;
let keypair = KeyPairAlgorithm::EcdsaP256.to_key_pair()?;
assert_eq!(
format!("{:?}", keypair.algorithm()),
"PKCS_ECDSA_P256_SHA256"
);
let keypair = KeypairAlgorithm::EcdsaP384.to_keypair()?;
let keypair = KeyPairAlgorithm::EcdsaP384.to_key_pair()?;
assert_eq!(
format!("{:?}", keypair.algorithm()),
"PKCS_ECDSA_P384_SHA384"
Expand Down
8 changes: 4 additions & 4 deletions rustls-cert-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rcgen::SanType;
use std::{net::IpAddr, path::PathBuf};

mod cert;
use cert::{keypair_algorithm, CertificateBuilder, KeypairAlgorithm};
use cert::{key_pair_algorithm, CertificateBuilder, KeyPairAlgorithm};

fn main() -> anyhow::Result<()> {
let opts = options().run();
Expand Down Expand Up @@ -48,12 +48,12 @@ struct Options {
pub output: PathBuf,
/// Keypair algorithm
#[bpaf(
external(keypair_algorithm),
fallback(KeypairAlgorithm::EcdsaP256),
external(key_pair_algorithm),
fallback(KeyPairAlgorithm::EcdsaP256),
display_fallback,
group_help("Keypair Algorithm:")
)]
pub keypair_algorithm: KeypairAlgorithm,
pub keypair_algorithm: KeyPairAlgorithm,
/// Extended Key Usage Purpose: ClientAuth
#[bpaf(long)]
pub client_auth: bool,
Expand Down

0 comments on commit 224d629

Please sign in to comment.