Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20231030 2059 pkcs11 #2077

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions openssl-sys/build/run_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const INCLUDES: &str = "
#include <openssl/sha.h>
#include <openssl/ssl.h>
#include <openssl/stack.h>
#include <openssl/store.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/x509v3.h>
Expand Down
2 changes: 2 additions & 0 deletions openssl-sys/src/handwritten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use self::sha::*;
pub use self::srtp::*;
pub use self::ssl::*;
pub use self::stack::*;
pub use self::store::*;
pub use self::tls1::*;
pub use self::types::*;
pub use self::x509::*;
Expand Down Expand Up @@ -64,6 +65,7 @@ mod sha;
mod srtp;
mod ssl;
mod stack;
mod store;
mod tls1;
mod types;
mod x509;
Expand Down
35 changes: 35 additions & 0 deletions openssl-sys/src/handwritten/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::super::*;
use libc::*;

pub enum OSSL_STORE_CTX {}

pub enum OSSL_STORE_INFO {}

pub const OSSL_STORE_INFO_PUBKEY: c_int = 3;
pub const OSSL_STORE_INFO_PKEY: c_int = 4;
pub const OSSL_STORE_INFO_CERT: c_int = 5;
pub const OSSL_STORE_INFO_CRL: c_int = 6;

extern "C" {
pub fn OSSL_STORE_open(
uri: *const c_char,
// const UI_METHOD *ui_method,
ui_method: *const c_void,
ui_data: *const c_void,
// OSSL_STORE_post_process_info_fn post_process,
post_process: *const c_void,
post_process_data: *const c_void,
) -> *mut OSSL_STORE_CTX;

pub fn OSSL_STORE_load(ctx: *mut OSSL_STORE_CTX) -> *mut OSSL_STORE_INFO;

pub fn OSSL_STORE_INFO_get_type(store_info: *mut OSSL_STORE_INFO) -> c_int;

pub fn OSSL_STORE_INFO_get1_PUBKEY(store_info: *mut OSSL_STORE_INFO) -> *mut EVP_PKEY;

pub fn OSSL_STORE_INFO_get1_PKEY(store_info: *mut OSSL_STORE_INFO) -> *mut EVP_PKEY;

pub fn OSSL_STORE_INFO_free(store_info: *mut OSSL_STORE_INFO);

pub fn OSSL_STORE_close(ctx: *mut OSSL_STORE_CTX) -> c_int;
}
1 change: 1 addition & 0 deletions openssl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub mod sign;
pub mod srtp;
pub mod ssl;
pub mod stack;
pub mod store;
pub mod string;
pub mod symm;
pub mod version;
Expand Down
51 changes: 51 additions & 0 deletions openssl/src/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use foreign_types::ForeignType;

use std::ffi::CString;
use std::ptr;

use crate::cvt_p;
use crate::error::ErrorStack;
use crate::pkey::{PKey, Private};

pub struct Store(*mut ffi::OSSL_STORE_CTX);

impl Drop for Store {
fn drop(&mut self) {
unsafe {
ffi::OSSL_STORE_close(self.0);
}
}
}

impl Store {
pub fn private_key_from_uri(uri: &str) -> Result<Option<PKey<Private>>, ErrorStack> {
let uri = CString::new(uri).unwrap();
unsafe {
let store = cvt_p(ffi::OSSL_STORE_open(
uri.as_ptr(),
ptr::null(),
ptr::null(),
ptr::null(),
ptr::null(),
))
.map(|p| Store(p))?;

let mut store_info = cvt_p(ffi::OSSL_STORE_load(store.0))?;

while store_info != ptr::null_mut() {
let type_ = ffi::OSSL_STORE_INFO_get_type(store_info);
if type_ == ffi::OSSL_STORE_INFO_PKEY {
let pkey_ptr = cvt_p(ffi::OSSL_STORE_INFO_get1_PKEY(store_info))?;
return Ok(Some(PKey::from_ptr(pkey_ptr)));
}

ffi::OSSL_STORE_INFO_free(store_info);

store_info = cvt_p(ffi::OSSL_STORE_load(store.0))?;
}

// error?
Ok(None)
}
}
}