Skip to content
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ All notable changes to this project should be documented in this file.
- [Handle imports where only the Seed is
provided](https://github.com/latchset/kryoptic/pull/330)

* Openssl context now loads the default openssl confgiuration file by default
- [Load default openssl config when
possible](https://github.com/latchset/kryoptic/pull/333)

# [1.2.0]
## 2025-06-09

Expand Down
1 change: 1 addition & 0 deletions ossl/ossl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "openssl/obj_mac.h"
#include "openssl/kdf.h"
#include "openssl/err.h"
#include "openssl/provider.h"

#ifdef _KRYOPTIC_FIPS_
#include "crypto/evp.h"
Expand Down
35 changes: 32 additions & 3 deletions ossl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,29 @@ pub(crate) use trace_ossl;
/// A structure representing the main crypto library context
pub struct OsslContext {
context: *mut OSSL_LIB_CTX,
providers: Vec<*mut OSSL_PROVIDER>,
}

static LEGACY_PROVIDER_NAME: &CStr = c"legacy";

impl OsslContext {
pub fn new_lib_ctx() -> OsslContext {
OsslContext {
context: unsafe { OSSL_LIB_CTX_new() },
providers: Vec::new(),
}
}

#[allow(dead_code)]
pub(crate) fn from_ctx(ctx: *mut OSSL_LIB_CTX) -> OsslContext {
OsslContext { context: ctx }
OsslContext {
context: ctx,
providers: Vec::new(),
}
}

pub fn load_configuration_file(
&self,
&mut self,
fname: Option<&Path>,
) -> Result<(), Error> {
let filename: *const c_char = match fname {
Expand All @@ -239,10 +246,29 @@ impl OsslContext {
}
}

pub fn load_default_configuration(&self) -> Result<(), Error> {
pub fn load_default_configuration(&mut self) -> Result<(), Error> {
self.load_configuration_file(None)
}

pub fn load_legacy_provider(&mut self) -> Result<(), Error> {
if unsafe {
OSSL_PROVIDER_available(self.ptr(), LEGACY_PROVIDER_NAME.as_ptr())
} == 1
{
return Ok(());
}

let provider = unsafe {
OSSL_PROVIDER_load(self.ptr(), LEGACY_PROVIDER_NAME.as_ptr())
};
if provider.is_null() {
Err(Error::new(ErrorKind::OsslError))
} else {
self.providers.push(provider);
Ok(())
}
}

pub fn ptr(&self) -> *mut OSSL_LIB_CTX {
self.context
}
Expand All @@ -251,6 +277,9 @@ impl OsslContext {
impl Drop for OsslContext {
fn drop(&mut self) {
unsafe {
while let Some(provider) = self.providers.pop() {
OSSL_PROVIDER_unload(provider);
}
OSSL_LIB_CTX_free(self.context);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/ossl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
/// The static instance of the library context lazily created on first use
#[cfg(not(feature = "fips"))]
static OSSL_CONTEXT: ::std::sync::LazyLock<::ossl::OsslContext> =
::std::sync::LazyLock::new(|| ::ossl::OsslContext::new_lib_ctx());
::std::sync::LazyLock::new(|| {
let mut ctx = ::ossl::OsslContext::new_lib_ctx();
/* Failing to load the default configuration file is not
* considered fatal as there are cases when the config
* file is not available in the location built into the
* openssl code. For example in CI static build the
* canonical location is set to /usr/local/ssl/openssl.cnf
* but there is no file at that path in the containers */
let _ = ctx.load_default_configuration();
ctx
});

pub mod aes;
pub mod common;
Expand Down
Loading