feat: v0.2.0-rc, add generate_self_signed_ca.rs

This commit is contained in:
2024-03-30 17:28:22 +08:00
parent be9cd0b78e
commit 538187bdcf
2 changed files with 28 additions and 2 deletions

View File

@@ -23,8 +23,16 @@
}
```
Generate self signed certificate:
```shell
$ cargo r --example generate_self_signed_ca
```
Important
* intermediate certificate only tested ECDSA(P384) with SHA384
* Intermediate certificate tested:
* ECDSA(P384) with SHA384
* P256 with SHA256
* P384 with SHA256 is NOT supported
* P256 with SHA256 should be supported, but not tested

View File

@@ -0,0 +1,18 @@
use rcgen::{BasicConstraints, Certificate, CertificateParams, DistinguishedName, DnType, IsCa, KeyPair, PKCS_ECDSA_P256_SHA256};
fn main() {
let key_pair = KeyPair::generate(&PKCS_ECDSA_P256_SHA256).expect("Generate key pair failed");
let key_pem = key_pair.serialize_pem();
let mut certificate_params = CertificateParams::default();
certificate_params.key_pair = Some(key_pair);
certificate_params.is_ca = IsCa::Ca(BasicConstraints::Constrained(0));
let mut distinguished_name = DistinguishedName::new();
distinguished_name.push(DnType::CommonName, "Proxy Inspector Test CA");
certificate_params.distinguished_name = distinguished_name;
let certificate = Certificate::from_params(certificate_params)
.unwrap_or_else(|e| panic!("Generate cert failed: {}", e));
let certificate_pem = certificate.serialize_pem_with_signer(&certificate).expect("Sign cert failed");
println!("CERTIFICATE:\n{}", certificate_pem);
println!("KEY:\n{}", key_pem);
}