Skip to content

Commit

Permalink
SSL certificate generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Oct 27, 2023
1 parent 4ccfdba commit 6357294
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 8 deletions.
399 changes: 391 additions & 8 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"chia-protocol/fuzz",
"chia_py_streamable_macro",
"chia_streamable_macro",
"chia-ssl",
"chia-tools",
"chia-traits",
"chia-wallet",
Expand Down
18 changes: 18 additions & 0 deletions chia-ssl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "chia-ssl"
version = "0.2.12"
edition = "2021"
license = "Apache-2.0"
description = "Chia SSL X.509 certificate generator"
authors = ["Brandon Haggstrom <[email protected]>"]
homepage = "https://github.com/Chia-Network/chia_rs/chia-ssl/"
repository = "https://github.com/Chia-Network/chia_rs/chia-ssl/"

[dependencies]
lazy_static = "1.4.0"
rand = "0.8.5"
rcgen = { version = "0.11.1", features = ["pem", "x509-parser"] }
rsa = "0.9.2"
rustls = "0.21.2"
thiserror = "1.0.50"
time = "0.3.22"
19 changes: 19 additions & 0 deletions chia-ssl/chia_ca.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDKTCCAhGgAwIBAgIUXIpxI5MoZQ65/vhc7DK/d5ymoMUwDQYJKoZIhvcNAQEL
BQAwRDENMAsGA1UECgwEQ2hpYTEQMA4GA1UEAwwHQ2hpYSBDQTEhMB8GA1UECwwY
T3JnYW5pYyBGYXJtaW5nIERpdmlzaW9uMB4XDTIxMDEyMzA4NTEwNloXDTMxMDEy
MTA4NTEwNlowRDENMAsGA1UECgwEQ2hpYTEQMA4GA1UEAwwHQ2hpYSBDQTEhMB8G
A1UECwwYT3JnYW5pYyBGYXJtaW5nIERpdmlzaW9uMIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAzz/L219Zjb5CIKnUkpd2julGC+j3E97KUiuOalCH9wdq
gpJi9nBqLccwPCSFXFew6CNBIBM+CW2jT3UVwgzjdXJ7pgtu8gWj0NQ6NqSLiXV2
WbpZovfrVh3x7Z4bjPgI3ouWjyehUfmK1GPIld4BfUSQtPlUJ53+XT32GRizUy+b
0CcJ84jp1XvyZAMajYnclFRNNJSw9WXtTlMUu+Z1M4K7c4ZPwEqgEnCgRc0TCaXj
180vo7mCHJQoDiNSCRATwfH+kWxOOK/nePkq2t4mPSFaX8xAS4yILISIOWYn7sNg
dy9D6gGNFo2SZ0FR3x9hjUjYEV3cPqg3BmNE3DDynQIDAQABoxMwETAPBgNVHRMB
Af8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAEugnFQjzHhS0eeCqUwOHmP3ww
/rXPkKF+bJ6uiQgXZl+B5W3m3zaKimJeyatmuN+5ST1gUET+boMhbA/7grXAsRsk
SFTHG0T9CWfPiuimVmGCzoxLGpWDMJcHZncpQZ72dcy3h7mjWS+U59uyRVHeiprE
hvSyoNSYmfvh7vplRKS1wYeA119LL5fRXvOQNW6pSsts17auu38HWQGagSIAd1UP
5zEvDS1HgvaU1E09hlHzlpdSdNkAx7si0DMzxKHUg9oXeRZedt6kcfyEmryd52Mj
1r1R9mf4iMIUv1zc2sHVc1omxnCw9+7U4GMWLtL5OgyJyfNyoxk3tC+D3KNU
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions chia-ssl/chia_ca.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDPP8vbX1mNvkIg
qdSSl3aO6UYL6PcT3spSK45qUIf3B2qCkmL2cGotxzA8JIVcV7DoI0EgEz4JbaNP
dRXCDON1cnumC27yBaPQ1Do2pIuJdXZZulmi9+tWHfHtnhuM+Ajei5aPJ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DRK4jJ76UGFKI2jo1m46vueYVJGkhn2jHsbeQKBgDim
/Ew20Col6QSmfhwKFpvjYsYtfaeEWns8zFxupCozz+PS+Dc2KGzqKwJ3pJgqOy29
l9fybtXf+uq5DFan2hF1C80lclUaiNoMGqol1TtXr6rPNIi59AumNXY/7tuvu2vE
bCsPH/L28ARPKdKEuYT4Umu/ol6aze7fHLymwrCbAoGAbm4vMLIy8wHYrAj5/een
YthUYbQ+XWOrrI1dJoGiwCIXTL4iBSO1ZjkR6M7yg9Zxv0BuqA+2tZqImLXsqIq/
g7Jtrn7LfogS3KOiVa3QAHoXegMrRf4S3DyP/GW8E27MMvqBEkdCDX53h07wvPng
0NRspbyMaX43/tp8ZWkaZf0=
-----END PRIVATE KEY-----
16 changes: 16 additions & 0 deletions chia-ssl/src/ca.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use lazy_static::lazy_static;
use rcgen::{Certificate, CertificateParams, KeyPair};

pub const CHIA_CA_KEY: &str = include_str!("../chia_ca.key");
pub const CHIA_CA_CRT: &str = include_str!("../chia_ca.crt");

lazy_static! {
pub static ref CHIA_CA: Certificate = load_ca_cert();
}

fn load_ca_cert() -> Certificate {
let key_pair = KeyPair::from_pem(CHIA_CA_KEY).expect("could not load CA keypair");
let params = CertificateParams::from_ca_cert_pem(CHIA_CA_CRT, key_pair)
.expect("could not create CA params");
Certificate::from_params(params).expect("could not create certificate")
}
19 changes: 19 additions & 0 deletions chia-ssl/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rcgen::RcgenError;
use time::error::ComponentRange;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum Error {
#[error("{0}")]
KeyGen(#[from] rsa::Error),

#[error("{0}")]
Pkcs8(#[from] rsa::pkcs8::Error),

#[error("{0}")]
CertGen(#[from] RcgenError),

#[error("{0}")]
DateRange(#[from] ComponentRange),
}
52 changes: 52 additions & 0 deletions chia-ssl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use rcgen::{Certificate, CertificateParams, DistinguishedName, DnType, KeyPair, SanType};
use rsa::{
pkcs8::{EncodePrivateKey, LineEnding},
RsaPrivateKey,
};
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time};

mod ca;
mod error;

pub use ca::*;
pub use error::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ChiaCertificate {
pub cert_pem: String,
pub key_pem: String,
}

impl ChiaCertificate {
pub fn generate() -> Result<ChiaCertificate> {
let mut rng = rand::thread_rng();

let key = RsaPrivateKey::new(&mut rng, 2048)?;
let key_pem = key.to_pkcs8_pem(LineEnding::default())?.to_string();

let mut params = CertificateParams::default();

params.alg = &rcgen::PKCS_RSA_SHA256;
params.key_pair = Some(KeyPair::from_pem(&key_pem)?);

let mut subject = DistinguishedName::new();
subject.push(DnType::CommonName, "Chia");
subject.push(DnType::OrganizationName, "Chia");
subject.push(DnType::OrganizationalUnitName, "Organic Farming Division");
params.distinguished_name = subject;

params.subject_alt_names = vec![SanType::DnsName("chia.net".to_string())];

params.not_before = OffsetDateTime::now_utc() - Duration::DAY;
params.not_after = PrimitiveDateTime::new(
Date::from_calendar_date(2100, Month::August, 2)?,
Time::MIDNIGHT,
)
.assume_utc();

let cert = Certificate::from_params(params)?;
let cert_pem = cert.serialize_pem_with_signer(&CHIA_CA)?;

Ok(ChiaCertificate { cert_pem, key_pem })
}
}

0 comments on commit 6357294

Please sign in to comment.