Skip to content

Commit

Permalink
based on public PR
Browse files Browse the repository at this point in the history
PR: sfackler#2077
issue: sfackler#2059

It works!

fmt
  • Loading branch information
Firstyear authored and mvar-ms committed Apr 11, 2024
1 parent 83b3186 commit 39cbacf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions openssl-sys/build/run_bindgen.rs
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
Expand Up @@ -29,6 +29,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 @@ -66,6 +67,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
@@ -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
Expand Up @@ -184,6 +184,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
@@ -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)
}
}
}

0 comments on commit 39cbacf

Please sign in to comment.