-
Notifications
You must be signed in to change notification settings - Fork 134
examples: add signing new cert using existing ca pem files #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iamjpotts
wants to merge
1
commit into
rustls:main
Choose a base branch
from
iamjpotts:jp/sign-server-with-intermediate-pems
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! Generate a new certificate, and sign it with an existing root or | ||
//! intermediate certificate. | ||
//! | ||
//! Requires four positional command line arguments: | ||
//! * File path to PEM containing signer's key pair | ||
//! * File path to PEM containing signer's certificate | ||
//! * File path for generated PEM containing output key pair | ||
//! * File path for generated PEM containing output certificate | ||
|
||
use std::error::Error; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use rcgen::{CertificateParams, DnType, ExtendedKeyUsagePurpose, Issuer, KeyPair, KeyUsagePurpose}; | ||
use time::{Duration, OffsetDateTime}; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let mut args = std::env::args().skip(1); | ||
|
||
let signer_keys_file = PathBuf::from( | ||
args.next() | ||
.ok_or("provide signer's pem keys file as 1st argument")?, | ||
); | ||
|
||
let signer_cert_file = PathBuf::from( | ||
args.next() | ||
.ok_or("provide signer's pem certificate file as 2nd argument")?, | ||
); | ||
|
||
let output_keys_file = | ||
PathBuf::from(args.next().ok_or("output pem keys file as 3rd argument")?); | ||
|
||
let output_cert_file = PathBuf::from(args.next().ok_or("output pem cert file as 4th fourth")?); | ||
|
||
// Read existing certificate authority | ||
let keys_pem = fs::read_to_string(&signer_keys_file)?; | ||
let cert_pem = fs::read_to_string(&signer_cert_file)?; | ||
|
||
let key_pair = KeyPair::from_pem(&keys_pem)?; | ||
let signer = Issuer::from_ca_cert_pem(&cert_pem, key_pair)?; | ||
|
||
// Create a new signed server certificate | ||
const DOMAIN: &str = "example.domain"; | ||
|
||
let sans = vec![DOMAIN.into()]; | ||
|
||
let mut params = CertificateParams::new(sans)?; | ||
|
||
params.distinguished_name.push(DnType::CommonName, DOMAIN); | ||
params.use_authority_key_identifier_extension = true; | ||
params.key_usages.push(KeyUsagePurpose::DigitalSignature); | ||
params | ||
.extended_key_usages | ||
.push(ExtendedKeyUsagePurpose::ServerAuth); | ||
|
||
const DAY: Duration = Duration::days(1); | ||
|
||
let yesterday = OffsetDateTime::now_utc() | ||
.checked_sub(DAY) | ||
.ok_or("invalid yesterday")?; | ||
|
||
let tomorrow = OffsetDateTime::now_utc() | ||
.checked_add(DAY) | ||
.ok_or("invalid tomorrow")?; | ||
|
||
params.not_before = yesterday; | ||
params.not_after = tomorrow; | ||
|
||
let output_keys = KeyPair::generate()?; | ||
let output_cert = params.signed_by(&output_keys, &signer)?; | ||
|
||
// Write new certificate | ||
fs::write(&output_keys_file, output_keys.serialize_pem())?; | ||
fs::write(&output_cert_file, output_cert.pem())?; | ||
|
||
println!("Wrote signed leaf certificate:"); | ||
println!(" keys: {}", output_keys_file.display()); | ||
println!(" cert: {}", output_cert_file.display()); | ||
println!(); | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.