(certs_path: &PathBuf, wss_host: &[String])
| 24 | } |
| 25 | |
| 26 | pub fn generate_certificates(certs_path: &PathBuf, wss_host: &[String]) -> Result<(), Error> { |
| 27 | /* Generate the CA certificate */ |
| 28 | let mut ca_params = CertificateParams::new(vec![ |
| 29 | "cln Root wss-proxy CA".to_string(), |
| 30 | "cln".to_string(), |
| 31 | "localhost".to_string(), |
| 32 | ])?; |
| 33 | ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained); |
| 34 | ca_params |
| 35 | .key_usages |
| 36 | .push(rcgen::KeyUsagePurpose::KeyCertSign); |
| 37 | ca_params.use_authority_key_identifier_extension = true; |
| 38 | let ca_key = KeyPair::generate()?; |
| 39 | let ca_cert = ca_params.self_signed(&ca_key)?; |
| 40 | let ca_issuer = Issuer::from_params(&ca_params, &ca_key); |
| 41 | |
| 42 | fs::create_dir_all(certs_path)?; |
| 43 | |
| 44 | fs::write(certs_path.join("ca.pem"), ca_cert.pem())?; |
| 45 | write_private_key( |
| 46 | certs_path.join("ca-key.pem"), |
| 47 | ca_key.serialize_pem().as_bytes(), |
| 48 | )?; |
| 49 | |
| 50 | /* Generate the server certificate signed by the CA */ |
| 51 | let mut server_params = CertificateParams::new(vec![ |
| 52 | format!("cln wss-proxy server"), |
| 53 | "cln".to_string(), |
| 54 | "localhost".to_string(), |
| 55 | ])?; |
| 56 | server_params.is_ca = rcgen::IsCa::NoCa; |
| 57 | server_params |
| 58 | .key_usages |
| 59 | .push(rcgen::KeyUsagePurpose::DigitalSignature); |
| 60 | server_params |
| 61 | .key_usages |
| 62 | .push(rcgen::KeyUsagePurpose::KeyEncipherment); |
| 63 | server_params |
| 64 | .key_usages |
| 65 | .push(rcgen::KeyUsagePurpose::KeyAgreement); |
| 66 | server_params.use_authority_key_identifier_extension = true; |
| 67 | server_params.distinguished_name = DistinguishedName::new(); |
| 68 | server_params |
| 69 | .distinguished_name |
| 70 | .push(rcgen::DnType::CommonName, "cln wss-proxy server"); |
| 71 | |
| 72 | /* It is convention to not include [] for ipv6 addresses in certificate SAN's */ |
| 73 | for host in wss_host.iter() { |
| 74 | let host_stripped = if host.starts_with('[') && host.ends_with(']') { |
| 75 | host[1..host.len() - 1].to_string() |
| 76 | } else { |
| 77 | host.to_owned() |
| 78 | }; |
| 79 | if let Ok(ip) = host_stripped.parse::<IpAddr>() { |
| 80 | server_params |
| 81 | .subject_alt_names |
| 82 | .push(rcgen::SanType::IpAddress(ip)); |
| 83 | } else if let Ok(dns) = Ia5String::try_from(host.to_owned()) { |
no test coverage detected