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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

New safe and idiomatic interface #43

Closed
wants to merge 3 commits into from
Closed
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
37 changes: 2 additions & 35 deletions Cargo.toml
Expand Up @@ -10,38 +10,5 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[package]
name = "pkcs11"
version = "0.6.0"
authors = ["Marcus Heese <marcus.heese@gmail.com>"]
edition = '2018'
description = "Rust PKCS#11 Library"
#documentation = "https://github.com/mheese/rust-pkcs11"
homepage = "https://github.com/mheese/rust-pkcs11"
repository = "https://github.com/mheese/rust-pkcs11"
readme = "README.md"
keywords = ["pkcs11", "cryptoki"]
categories = ["external-ffi-bindings", "cryptography", "hardware-support"]
license = "Apache-2.0"
license-file = "LICENSE"
exclude = [
"pkcs11-docs/**",
]

[badges]
maintenance = { status = "actively-developed" }
codecov = { repository = "mheese/rust-pkcs11", branch = "master", service = "github" }
is-it-maintained-issue-resolution = { repository = "mheese/rust-pkcs11" }
is-it-maintained-open-issues = { repository = "mheese/rust-pkcs11" }

[dependencies]
libloading = "0.6.1"
num-bigint = "0.2.6"
#libc = "0.2.33"

[dev-dependencies]
num-traits = "0.2.11"
hex = "0.4.2"
serial_test = "0.4.0"
serial_test_derive = "0.4.0"
[workspace]
members = ["pkcs11", "pkcs11-sys"]
28 changes: 28 additions & 0 deletions pkcs11-sys/Cargo.toml
@@ -0,0 +1,28 @@
[package]
name = "pkcs11-sys"
version = "0.6.0"
authors = ["Marcus Heese <marcus.heese@gmail.com>"]
edition = "2018"
homepage = "https://github.com/mheese/rust-pkcs11"
repository = "https://github.com/mheese/rust-pkcs11"
readme = "README.md"
keywords = ["pkcs11", "cryptoki"]
categories = ["external-ffi-bindings", "cryptography", "hardware-support"]
license-file = "LICENSE"
links = "pkcs11"
build="build.rs"

include = [
"README.md",
"Cargo.toml",
"build.rs",
]

[build-dependencies]
bindgen = { version = "0.56.0", optional = true }

[dependencies]
libloading = "0.6.3"

[features]
generate-bindings = ["bindgen"]
45 changes: 45 additions & 0 deletions pkcs11-sys/build.rs
@@ -0,0 +1,45 @@
fn main() {
#[cfg(feature = "generate-bindings")]
{
generate_bindings();
}

#[cfg(not(feature = "generate-bindings"))]
{
let supported_platforms = vec![String::from("x86_64-unknown-linux-gnu")];
let target = std::env::var("TARGET").unwrap();

// check if target is in the list of supported ones or panic with nice message
if !supported_platforms.contains(&target) {
panic!(format!("Compilation target ({}) is not part of the supported targets ({:?}). Please compile with the \"generate-bindings\" feature or add support for your platform :)", target, supported_platforms));
}
}
}

// Only on a specific feature
#[cfg(feature = "generate-bindings")]
fn generate_bindings() {
let bindings = bindgen::Builder::default()
.header("pkcs11.h")
.dynamic_library_name("Pkcs11")
// The PKCS11 library works in a slightly different way to most shared libraries. We have
// to call `C_GetFunctionList`, which returns a list of pointers to the _actual_ library
// functions. This is the only function we need to create a binding for.
.whitelist_function("C_GetFunctionList")
// This is needed because no types will be generated if `whitelist_function` is used.
// Unsure if this is a bug.
.whitelist_type("*")
// Derive the `Debug` trait for the generated structs where possible.
.derive_debug(true)
// Derive the `Default` trait for the generated structs where possible.
.derive_default(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/pkcs11_bindings.rs file.
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("pkcs11_bindings.rs"))
.expect("Couldn't write bindings!");
}