Skip to content

Commit

Permalink
Merge pull request #86 from Telecominfraproject/dev-tls-decode-base64
Browse files Browse the repository at this point in the history
Try do decode base64 TLS certs/keys
  • Loading branch information
Cahb authored Oct 2, 2024
2 parents 67cddad + ff1902f commit 3068894
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/cgw_tls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::cgw_app_args::CGWWSSArgs;
use crate::cgw_errors::{collect_results, Error, Result};

use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use eui48::MacAddress;
use rustls_pki_types::{CertificateDer, PrivateKeyDer};
use std::fs;
use std::io::BufRead;
use std::io::{BufRead, Read};
use std::path::Path;
use std::{fs::File, io::BufReader, str::FromStr, sync::Arc};
use tokio::net::TcpStream;
Expand All @@ -20,34 +22,60 @@ use x509_parser::parse_x509_certificate;
const CGW_TLS_CERTIFICATES_PATH: &str = "/etc/cgw/certs";
const CGW_TLS_NB_INFRA_CERTS_PATH: &str = "/etc/cgw/nb_infra/certs";

pub async fn cgw_tls_read_certs(cert_file: &str) -> Result<Vec<CertificateDer<'static>>> {
let file = match File::open(cert_file) {
async fn cgw_tls_read_file(file_path: &str) -> Result<Vec<u8>> {
let mut file = match File::open(file_path) {
Ok(f) => f,
Err(e) => {
return Err(Error::Tls(format!(
"Failed to open TLS certificate file: {}. Error: {}",
cert_file, e
"Failed to open file: {}. Error: {}",
file_path, e
)));
}
};

let mut reader = BufReader::new(file);

collect_results(rustls_pemfile::certs(&mut reader))
}

pub async fn cgw_tls_read_private_key(private_key_file: &str) -> Result<PrivateKeyDer<'static>> {
let file = match File::open(private_key_file) {
Ok(f) => f,
let metadata = match fs::metadata(file_path) {
Ok(meta) => meta,
Err(e) => {
return Err(Error::Tls(format!(
"Failed to open TLS private key file: {}. Error: {}",
private_key_file, e
"Failed to read {} metadata. Error: {}",
file_path, e
)));
}
};

let mut reader = BufReader::new(file);
let mut buffer = vec![0; metadata.len() as usize];
if let Err(e) = file.read_exact(&mut buffer) {
return Err(Error::Tls(format!(
"Failed to read {} file. Error: {}",
file_path, e
)));
}

let decoded_buffer = {
if let Ok(d) = BASE64_STANDARD.decode(buffer.clone()) {
info!(
"Cert file {} is base64 encoded, trying to use decoded.",
file_path
);
d
} else {
buffer
}
};

Ok(decoded_buffer)
}

pub async fn cgw_tls_read_certs(cert_file: &str) -> Result<Vec<CertificateDer<'static>>> {
let buffer = cgw_tls_read_file(cert_file).await?;
let mut reader = BufReader::new(buffer.as_slice());

collect_results(rustls_pemfile::certs(&mut reader))
}

pub async fn cgw_tls_read_private_key(private_key_file: &str) -> Result<PrivateKeyDer<'static>> {
let buffer = cgw_tls_read_file(private_key_file).await?;
let mut reader = BufReader::new(buffer.as_slice());

match rustls_pemfile::private_key(&mut reader) {
Ok(ret_pk) => match ret_pk {
Expand Down

0 comments on commit 3068894

Please sign in to comment.