Generating a CSR in Rust
Before calling IssueCertificate, the caller must provide a CSR.
Your provisioning service might generate the key + CSR on behalf of the device:
use rcgen::{Certificate, CertificateParams, DistinguishedName, DnType};
pub fn generate_csr(
common_name: &str,
dns_sans: &[&str],
) -> anyhow::Result<(String, rcgen::KeyPair)> {
let mut params = CertificateParams::new(dns_sans.to_vec())?;
params.distinguished_name = DistinguishedName::new();
params.distinguished_name.push(DnType::CommonName, common_name);
let key_pair = rcgen::KeyPair::generate()?;
let cert = params.serialize_request(&key_pair)?;
Ok((cert.pem(), key_pair))
}
Security note: if the provisioning service generates the private key,
it must securely transmit it to the device. The device should ideally
generate its own key and send only the CSR (which is the standard pattern).