-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FFI call to fetch mTLS identities and to provision PIV slot (#56)
* Add FFI call to fetch mTLS identities * Add PIV util call * Fix comment
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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,50 @@ | ||
use crate::config::UpdatableConfiguration; | ||
use std::{ffi::{c_char, CStr, CString}, ptr::null}; | ||
|
||
#[no_mangle] | ||
/// Read the mTLS identities of the primary server (the first one) given a config path | ||
pub unsafe extern "C" fn ffi_get_identities_of_primary_server(config_path: *const c_char) -> *const c_char { | ||
let cf = CStr::from_ptr(config_path); | ||
let config_path = match cf.to_str() { | ||
Err(_) => return null(), | ||
Ok(s) => s, | ||
}; | ||
|
||
let updatable_configuration = match UpdatableConfiguration::new(config_path) { | ||
|
||
Ok(c) => c, | ||
Err(e) => { | ||
error!("Configuration was invalid: {e}"); | ||
return null(); | ||
} | ||
}; | ||
|
||
let server = match updatable_configuration.get_configuration().servers.first() { | ||
Some(s) => &s.mtls_cert, | ||
None => return null(), | ||
}; | ||
|
||
let cert = match x509_parser::pem::parse_x509_pem(server.as_bytes()) { | ||
Err(e) => { | ||
error!("Unable to parse mTLS cert PEM: {e}"); | ||
return null(); | ||
}, | ||
Ok((_, s)) => s, | ||
}; | ||
|
||
let subject = match cert.parse_x509() { | ||
Err(e) => { | ||
error!("Unable to parse mTLS cert: {e}"); | ||
return null(); | ||
}, | ||
Ok(c) => c.tbs_certificate.subject().to_string(), | ||
}; | ||
|
||
match CString::new(subject) { | ||
Err(e) => { | ||
error!("Unable to marshall subject to CString: {e}"); | ||
return null(); | ||
}, | ||
Ok(s) => s.into_raw(), | ||
} | ||
} |