Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ lazy_static = {version = "^1.4"}

[build-dependencies]
pkg-config = {version = "^0.3"}
bindgen = {version = "^0.65"}
bindgen = {version = "^0.70"}

[features]
MD5 = []
AesGcm = []
xmlSecOpenSSLAppKeyLoad = []
xmlSecOpenSSLAppKeyLoadEx = []
24 changes: 18 additions & 6 deletions src/crypto/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ pub enum XmlSecSignatureMethod
Aes128Cbc,
Aes192Cbc,
Aes256Cbc,
// Aes128Gcm,
// Aes192Gcm,
// Aes256Gcm,
#[cfg(feature = "AesGcm")]
Aes128Gcm,
#[cfg(feature = "AesGcm")]
Aes192Gcm,
#[cfg(feature = "AesGcm")]
Aes256Gcm,
KWAes128,
KWAes192,
KWAes256,
Expand All @@ -26,15 +29,18 @@ pub enum XmlSecSignatureMethod
EcdsaSha256,
EcdsaSha384,
EcdsaSha512,
#[cfg(feature = "MD5")]
HmacMd5,
HmacRipemd160,
HmacSha1,
HmacSha224,
HmacSha256,
HmacSha384,
HmacSha512,
#[cfg(feature = "MD5")]
Md5,
Ripemd160,
#[cfg(feature = "MD5")]
RsaMd5,
RsaRipemd160,
RsaSha1,
Expand Down Expand Up @@ -62,9 +68,12 @@ impl XmlSecSignatureMethod
Self::Aes128Cbc => unsafe { bindings::xmlSecOpenSSLTransformAes128CbcGetKlass() },
Self::Aes192Cbc => unsafe { bindings::xmlSecOpenSSLTransformAes192CbcGetKlass() },
Self::Aes256Cbc => unsafe { bindings::xmlSecOpenSSLTransformAes256CbcGetKlass() },
// Self::Aes128Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes128GcmGetKlass() },
// Self::Aes192Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes192GcmGetKlass() },
// Self::Aes256Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes256GcmGetKlass() },
#[cfg(feature = "AesGcm")]
Self::Aes128Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes128GcmGetKlass() },
#[cfg(feature = "AesGcm")]
Self::Aes192Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes192GcmGetKlass() },
#[cfg(feature = "AesGcm")]
Self::Aes256Gcm => unsafe { bindings::xmlSecOpenSSLTransformAes256GcmGetKlass() },
Self::KWAes128 => unsafe { bindings::xmlSecOpenSSLTransformKWAes128GetKlass() },
Self::KWAes192 => unsafe { bindings::xmlSecOpenSSLTransformKWAes192GetKlass() },
Self::KWAes256 => unsafe { bindings::xmlSecOpenSSLTransformKWAes256GetKlass() },
Expand All @@ -77,15 +86,18 @@ impl XmlSecSignatureMethod
Self::EcdsaSha256 => unsafe { bindings::xmlSecOpenSSLTransformEcdsaSha256GetKlass() },
Self::EcdsaSha384 => unsafe { bindings::xmlSecOpenSSLTransformEcdsaSha384GetKlass() },
Self::EcdsaSha512 => unsafe { bindings::xmlSecOpenSSLTransformEcdsaSha512GetKlass() },
#[cfg(feature = "MD5")]
Self::HmacMd5 => unsafe { bindings::xmlSecOpenSSLTransformHmacMd5GetKlass() },
Self::HmacRipemd160 => unsafe { bindings::xmlSecOpenSSLTransformHmacRipemd160GetKlass() },
Self::HmacSha1 => unsafe { bindings::xmlSecOpenSSLTransformHmacSha1GetKlass() },
Self::HmacSha224 => unsafe { bindings::xmlSecOpenSSLTransformHmacSha224GetKlass() },
Self::HmacSha256 => unsafe { bindings::xmlSecOpenSSLTransformHmacSha256GetKlass() },
Self::HmacSha384 => unsafe { bindings::xmlSecOpenSSLTransformHmacSha384GetKlass() },
Self::HmacSha512 => unsafe { bindings::xmlSecOpenSSLTransformHmacSha512GetKlass() },
#[cfg(feature = "MD5")]
Self::Md5 => unsafe { bindings::xmlSecOpenSSLTransformMd5GetKlass() },
Self::Ripemd160 => unsafe { bindings::xmlSecOpenSSLTransformRipemd160GetKlass() },
#[cfg(feature = "MD5")]
Self::RsaMd5 => unsafe { bindings::xmlSecOpenSSLTransformRsaMd5GetKlass() },
Self::RsaRipemd160 => unsafe { bindings::xmlSecOpenSSLTransformRsaRipemd160GetKlass() },
Self::RsaSha1 => unsafe { bindings::xmlSecOpenSSLTransformRsaSha1GetKlass() },
Expand Down
35 changes: 33 additions & 2 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl XmlSecKey
{
/// Load key from file by specifying path, its format in the file, and optionally the password required to
/// decrypt/unlock.
#[cfg(feature = "xmlSecOpenSSLAppKeyLoad")]
pub fn from_file(path: &str, format: XmlSecKeyFormat, password: Option<&str>) -> XmlSecResult<Self>
{
// TODO deprecate internals for Rust read-from-file and then loading with `from_memory`
Expand Down Expand Up @@ -73,6 +74,36 @@ impl XmlSecKey
Ok(Self(key))
}

#[cfg(feature = "xmlSecOpenSSLAppKeyLoadEx")]
pub fn from_file(path: &str, format: XmlSecKeyDataType, password: Option<&str>) -> XmlSecResult<Self>
{
// TODO deprecate internals for Rust read-from-file and then loading with `from_memory`

crate::xmlsec::guarantee_xmlsec_init();

// TODO proper sanitization/error handling of input
let cpath = CString::new(path).unwrap();
let cpasswd = password.map(|p| CString::new(p).unwrap());

let cpasswd_ptr = cpasswd.map(|cstr| cstr.as_ptr())
.unwrap_or(null());

// Load key from file
let key = unsafe { bindings::xmlSecOpenSSLAppKeyLoadEx(
cpath.as_ptr(),
format as u32,
cpasswd_ptr,
null_mut(),
null_mut()
) };

if key.is_null() {
return Err(XmlSecError::KeyLoadError);
}

Ok(Self(key))
}

/// Load key from buffer in memory, specifying format and optionally the password required to decrypt/unlock.
pub fn from_memory(buffer: &[u8], format: XmlSecKeyFormat, password: Option<&str>) -> XmlSecResult<Self>
{
Expand All @@ -87,7 +118,7 @@ impl XmlSecKey
// Load key from buffer
let key = unsafe { bindings::xmlSecOpenSSLAppKeyLoadMemory(
buffer.as_ptr(),
buffer.len() as u32,
(buffer.len() as u32).try_into().unwrap(),
format as u32,
cpasswd_ptr,
null_mut(),
Expand Down Expand Up @@ -122,7 +153,7 @@ impl XmlSecKey
bindings::xmlSecOpenSSLAppKeyCertLoadMemory(
self.0,
buff.as_ptr(),
buff.len() as u32,
(buff.len() as u32).try_into().unwrap(),
format as u32
)
};
Expand Down