mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-02 22:41:29 +03:00
gen or load certificate
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
use std::io::{self, Read};
|
||||
use std::path::PathBuf;
|
||||
use std::io::{self, BufWriter, Read, Write};
|
||||
use std::path::Path;
|
||||
use std::{fs::File, io::BufReader};
|
||||
|
||||
use rcgen::KeyPair;
|
||||
use rustls::pki_types::CertificateDer;
|
||||
use sha2::{Digest, Sha256};
|
||||
use thiserror::Error;
|
||||
use webrtc_dtls::crypto::{Certificate, CryptoPrivateKey};
|
||||
use webrtc_dtls::crypto::Certificate;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
@@ -27,6 +25,10 @@ pub enum Error {
|
||||
Dtls(#[from] webrtc_dtls::Error),
|
||||
#[error("{0}")]
|
||||
Other(String),
|
||||
#[error("a file containing the public key is missing")]
|
||||
PkMissing,
|
||||
#[error("a file containing the private key is missing")]
|
||||
SkMissing,
|
||||
}
|
||||
|
||||
pub fn generate_fingerprint(cert: &[u8]) -> String {
|
||||
@@ -41,42 +43,34 @@ pub fn generate_fingerprint(cert: &[u8]) -> String {
|
||||
fingerprint
|
||||
}
|
||||
|
||||
/// load_key_and_certificate reads certificates or key from file
|
||||
pub fn load_key_and_certificate(
|
||||
key_path: PathBuf,
|
||||
certificate_path: PathBuf,
|
||||
) -> Result<Certificate, Error> {
|
||||
let private_key = load_key(key_path)?;
|
||||
|
||||
let certificate = load_certificate(certificate_path)?;
|
||||
|
||||
Ok(Certificate {
|
||||
certificate,
|
||||
private_key,
|
||||
})
|
||||
pub fn certificate_fingerprint(cert: &Certificate) -> String {
|
||||
let certificate = cert.certificate.get(0).expect("certificate missing");
|
||||
generate_fingerprint(certificate)
|
||||
}
|
||||
|
||||
/// load_key Load/read key from file
|
||||
pub fn load_key(path: PathBuf) -> Result<CryptoPrivateKey, Error> {
|
||||
let f = File::open(path)?;
|
||||
let mut reader = BufReader::new(f);
|
||||
let mut buf = vec![];
|
||||
reader.read_to_end(&mut buf)?;
|
||||
|
||||
let s = String::from_utf8(buf).expect("utf8 of file");
|
||||
|
||||
let key_pair = KeyPair::from_pem(s.as_str()).expect("key pair in file");
|
||||
|
||||
Ok(CryptoPrivateKey::from_key_pair(&key_pair).expect("crypto key pair"))
|
||||
}
|
||||
|
||||
/// load_certificate Load/read certificate(s) from file
|
||||
pub fn load_certificate(path: PathBuf) -> Result<Vec<CertificateDer<'static>>, Error> {
|
||||
/// load certificate from file
|
||||
pub fn load_certificate(path: &Path) -> Result<Certificate, Error> {
|
||||
let f = File::open(path)?;
|
||||
|
||||
let mut reader = BufReader::new(f);
|
||||
match rustls_pemfile::certs(&mut reader).collect::<Result<Vec<_>, _>>() {
|
||||
Ok(certs) => Ok(certs.into_iter().map(CertificateDer::from).collect()),
|
||||
Err(_) => Err(Error::ErrNoCertificateFound),
|
||||
let mut pem = String::new();
|
||||
reader.read_to_string(&mut pem)?;
|
||||
Ok(Certificate::from_pem(pem.as_str())?)
|
||||
}
|
||||
|
||||
pub(crate) fn load_or_generate_key_and_cert(path: &Path) -> Result<Certificate, Error> {
|
||||
if path.exists() && path.is_file() {
|
||||
return Ok(load_certificate(path)?);
|
||||
} else {
|
||||
return Ok(generate_key_and_cert(path)?);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn generate_key_and_cert(path: &Path) -> Result<Certificate, Error> {
|
||||
let cert = Certificate::generate_self_signed(["ignored".to_owned()])?;
|
||||
let serialized = cert.serialize_pem();
|
||||
let f = File::create(path)?;
|
||||
let mut writer = BufWriter::new(f);
|
||||
writer.write(serialized.as_bytes())?;
|
||||
Ok(cert)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user