-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
361 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 @@ | ||
use openssl::{ | ||
error::ErrorStack as OpensslError, pkcs12::Pkcs12, pkey::PKey, stack::Stack, x509::X509, | ||
}; | ||
use snafu::{ResultExt, Snafu}; | ||
|
||
use crate::format::utils::split_pem_certificates; | ||
|
||
use super::{ | ||
well_known::{PemCertificate, Pkcs12Certificate, WellKnownSecretData}, | ||
SecretFormat, | ||
}; | ||
|
||
pub fn convert( | ||
from: WellKnownSecretData, | ||
to: SecretFormat, | ||
) -> Result<WellKnownSecretData, ConvertError> { | ||
match (from, to) { | ||
// Converting into the current format is always a no-op | ||
(from, to) if SecretFormat::from(&from) == to => Ok(from), | ||
|
||
(WellKnownSecretData::PemCertificate(pem), SecretFormat::Pkcs12Certificate) => Ok( | ||
WellKnownSecretData::Pkcs12Certificate(convert_pem_cert_to_pkcs12(pem)?), | ||
), | ||
|
||
(from, to) => NoValidConversionSnafu { from, to }.fail(), | ||
} | ||
} | ||
|
||
#[derive(Snafu, Debug)] | ||
pub enum ConvertError { | ||
#[snafu(display("no conversion defined from {from:?} to {to:?}"))] | ||
NoValidConversion { | ||
from: SecretFormat, | ||
to: SecretFormat, | ||
}, | ||
#[snafu( | ||
display("failed to convert from PEM certificate to PKCS#12"), | ||
context(false) | ||
)] | ||
PemCertToPkcs12 { source: PemCertToPkcs12Error }, | ||
} | ||
|
||
pub fn convert_pem_cert_to_pkcs12( | ||
pem: PemCertificate, | ||
) -> Result<Pkcs12Certificate, PemCertToPkcs12Error> { | ||
use pem_cert_to_pkcs12_error::*; | ||
let cert = X509::from_pem(&pem.certificate_pem).context(LoadCertSnafu)?; | ||
let key = PKey::private_key_from_pem(&pem.key_pem).context(LoadKeySnafu)?; | ||
|
||
let mut ca_stack = Stack::<X509>::new().context(LoadCaSnafu)?; | ||
for ca in split_pem_certificates(&pem.ca_pem) { | ||
X509::from_pem(ca) | ||
.and_then(|ca| ca_stack.push(ca)) | ||
.context(LoadCertSnafu)?; | ||
} | ||
|
||
let mut pkcs_builder = Pkcs12::builder(); | ||
|
||
Ok(Pkcs12Certificate { | ||
truststore: pkcs_builder | ||
.ca(ca_stack) | ||
.build2("") | ||
.and_then(|store| store.to_der()) | ||
.context(BuildTruststoreSnafu)?, | ||
keystore: pkcs_builder | ||
.cert(&cert) | ||
.pkey(&key) | ||
.build2("") | ||
.and_then(|store| store.to_der()) | ||
.context(BuildKeystoreSnafu)?, | ||
}) | ||
} | ||
|
||
#[derive(Snafu, Debug)] | ||
#[snafu(module)] | ||
pub enum PemCertToPkcs12Error { | ||
LoadCert { source: OpensslError }, | ||
LoadKey { source: OpensslError }, | ||
LoadCa { source: OpensslError }, | ||
BuildKeystore { source: OpensslError }, | ||
BuildTruststore { source: OpensslError }, | ||
} |
This file contains 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,33 @@ | ||
use std::collections::HashMap; | ||
|
||
pub use self::{ | ||
convert::ConvertError, | ||
well_known::{FromFilesError as ParseError, SecretFormat, WellKnownSecretData}, | ||
}; | ||
|
||
mod convert; | ||
mod utils; | ||
pub mod well_known; | ||
|
||
pub type SecretFiles = HashMap<String, Vec<u8>>; | ||
|
||
#[derive(Debug)] | ||
pub enum SecretData { | ||
WellKnown(well_known::WellKnownSecretData), | ||
Unknown(SecretFiles), | ||
} | ||
impl SecretData { | ||
pub fn into_files(self) -> SecretFiles { | ||
match self { | ||
SecretData::WellKnown(data) => data.into_files(), | ||
SecretData::Unknown(files) => files, | ||
} | ||
} | ||
|
||
pub fn parse(self) -> Result<WellKnownSecretData, ParseError> { | ||
match self { | ||
Self::WellKnown(x) => Ok(x), | ||
Self::Unknown(files) => WellKnownSecretData::from_files(files), | ||
} | ||
} | ||
} |
Oops, something went wrong.