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

Fix panics on failed initialization #165

Merged
merged 6 commits into from
Jan 10, 2024
Merged
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
19 changes: 18 additions & 1 deletion pkcs11/src/api/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,30 @@ pub extern "C" fn C_DecryptVerifyUpdate(
pulPartLen: cryptoki_sys::CK_ULONG_PTR,
) -> cryptoki_sys::CK_RV {
trace!("C_DecryptVerifyUpdate() called");

cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED
}

#[cfg(test)]
mod tests {

use super::*;
use crate::data::SESSION_MANAGER;
use crate::{backend::slot::init_for_tests, data::SESSION_MANAGER};

fn setup_session() -> cryptoki_sys::CK_SESSION_HANDLE {
SESSION_MANAGER.lock().unwrap().setup_dummy_session()
}

#[test]
fn test_decrypt_init_null_mech() {
init_for_tests();
let rv = C_DecryptInit(0, std::ptr::null_mut(), 0);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);
}

#[test]
fn test_decrypt_init_unknown_mech() {
init_for_tests();
let mut mech = cryptoki_sys::CK_MECHANISM {
mechanism: 15000, // doesn't exist
pParameter: std::ptr::null_mut(),
Expand All @@ -230,6 +233,7 @@ mod tests {

#[test]
fn test_decrypt_init_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let mut mech = cryptoki_sys::CK_MECHANISM {
Expand All @@ -244,6 +248,7 @@ mod tests {

#[test]
fn test_decrypt_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let rv = C_Decrypt(
Expand All @@ -258,6 +263,7 @@ mod tests {

#[test]
fn test_decrypt_null_data_len() {
init_for_tests();
let mut pEncryptedData = [0u8; 32];

let session_handle = setup_session();
Expand All @@ -274,6 +280,7 @@ mod tests {

#[test]
fn test_decrypt_null_encrypted_data() {
init_for_tests();
let mut pulDataLen = 0;

let session_handle = setup_session();
Expand All @@ -290,6 +297,7 @@ mod tests {

#[test]
fn test_decrypt_null_data() {
init_for_tests();
let mut pulDataLen = 0;

let session_handle = setup_session();
Expand All @@ -308,6 +316,7 @@ mod tests {

#[test]
fn test_decrypt_update_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let rv = C_DecryptUpdate(
Expand All @@ -322,6 +331,7 @@ mod tests {

#[test]
fn test_decrypt_update_null_encrypted_part() {
init_for_tests();
let session_handle = setup_session();

let mut pulPartLen = 0;
Expand All @@ -339,6 +349,7 @@ mod tests {

#[test]
fn test_decrypt_update_null_part_len() {
init_for_tests();
let session_handle = setup_session();

let mut pEncryptedPart = [0u8; 32];
Expand All @@ -356,6 +367,7 @@ mod tests {

#[test]
fn test_decrypt_update_operation_not_initialized() {
init_for_tests();
let session_handle = setup_session();

let mut pEncryptedPart = [0u8; 32];
Expand All @@ -374,6 +386,7 @@ mod tests {

#[test]
fn test_decrypt_final_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let mut pulLastPartLen = 0;
Expand All @@ -384,6 +397,7 @@ mod tests {

#[test]
fn test_decrypt_final_null_last_part_len() {
init_for_tests();
let session_handle = setup_session();

let mut lastPart = [0u8; 32];
Expand All @@ -394,6 +408,7 @@ mod tests {

#[test]
fn test_decrypt_final_operation_not_initialized() {
init_for_tests();
let session_handle = setup_session();

let mut lastPart = [0u8; 32];
Expand All @@ -405,6 +420,7 @@ mod tests {

// #[test]
// fn test_decrypt_final_null_last_part() {
// init_for_tests();
// let session_handle = setup_session();

// let mut pulLastPartLen = 0;
Expand All @@ -416,6 +432,7 @@ mod tests {
// unsupported function
#[test]
fn test_decrypt_verify_update() {
init_for_tests();
let rv = C_DecryptVerifyUpdate(
0,
std::ptr::null_mut(),
Expand Down
12 changes: 12 additions & 0 deletions pkcs11/src/api/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub extern "C" fn C_DigestKey(
hKey: cryptoki_sys::CK_OBJECT_HANDLE,
) -> cryptoki_sys::CK_RV {
trace!("C_DigestKey() called");

cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED
}

Expand All @@ -77,6 +78,7 @@ pub extern "C" fn C_DigestEncryptUpdate(
pulEncryptedPartLen: cryptoki_sys::CK_ULONG_PTR,
) -> cryptoki_sys::CK_RV {
trace!("C_DigestEncryptUpdate() called");

cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED
}

Expand All @@ -88,16 +90,20 @@ pub extern "C" fn C_DecryptDigestUpdate(
pulPartLen: cryptoki_sys::CK_ULONG_PTR,
) -> cryptoki_sys::CK_RV {
trace!("C_DecryptDigestUpdate() called ");

cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED
}

#[cfg(test)]
mod tests {
use cryptoki_sys::CK_ULONG;

use crate::backend::slot::init_for_tests;

use super::*;
#[test]
fn test_digest_init() {
init_for_tests();
let rv = C_DigestInit(0, std::ptr::null_mut());
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -113,6 +119,7 @@ mod tests {

#[test]
fn test_digest() {
init_for_tests();
let rv = C_Digest(
0,
std::ptr::null_mut(),
Expand All @@ -138,6 +145,7 @@ mod tests {

#[test]
fn test_digest_update() {
init_for_tests();
let rv = C_DigestUpdate(0, std::ptr::null_mut(), 0 as CK_ULONG);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -149,6 +157,7 @@ mod tests {

#[test]
fn test_digest_final() {
init_for_tests();
let rv = C_DigestFinal(0, std::ptr::null_mut(), std::ptr::null_mut());
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -161,12 +170,14 @@ mod tests {

#[test]
fn test_digest_key() {
init_for_tests();
let rv = C_DigestKey(0, 0);
assert_eq!(rv, cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED);
}

#[test]
fn test_digest_encrypt_update() {
init_for_tests();
let mut encrypted_part_len: CK_ULONG = 0;
let mut encrypted_part: Vec<u8> = Vec::new();
let mut part: Vec<u8> = Vec::new();
Expand All @@ -183,6 +194,7 @@ mod tests {

#[test]
fn test_decrypt_digest_update() {
init_for_tests();
let mut encrypted_part_len: CK_ULONG = 0;
let mut encrypted_part: Vec<u8> = Vec::new();
let mut part: Vec<u8> = Vec::new();
Expand Down
21 changes: 20 additions & 1 deletion pkcs11/src/api/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,20 @@ pub extern "C" fn C_EncryptFinal(

#[cfg(test)]
mod tests {
use crate::data::SESSION_MANAGER;
use crate::{backend::slot::init_for_tests, data::SESSION_MANAGER};

use super::*;

#[test]
fn test_encrypt_init_null_mechanism() {
init_for_tests();
let rv = C_EncryptInit(0, std::ptr::null_mut(), 0);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);
}

#[test]
fn test_encrypt_init_invalid_mechanism() {
init_for_tests();
let mut mechanism = cryptoki_sys::CK_MECHANISM {
mechanism: 15000,
pParameter: std::ptr::null_mut(),
Expand All @@ -265,6 +267,7 @@ mod tests {

#[test]
fn test_encrypt_init_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut mechanism = cryptoki_sys::CK_MECHANISM {
Expand All @@ -279,6 +282,7 @@ mod tests {

#[test]
fn test_encrypt_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut data: Vec<u8> = Vec::new();
Expand All @@ -297,6 +301,7 @@ mod tests {

#[test]
fn test_encrypt_update_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut data: Vec<u8> = Vec::new();
Expand All @@ -315,6 +320,7 @@ mod tests {

#[test]
fn test_encrypt_final_invalid_session() {
init_for_tests();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut encrypted_data: Vec<u8> = Vec::new();
Expand All @@ -326,6 +332,7 @@ mod tests {

#[test]
fn test_encrypt_null_data() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedDataLen: CK_ULONG = 0;
Expand All @@ -343,6 +350,7 @@ mod tests {

#[test]
fn test_encrypt_null_encrypted_data_len() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -360,6 +368,7 @@ mod tests {

#[test]
fn test_encrypt_null_encrypted_data() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -377,6 +386,7 @@ mod tests {

#[test]
fn test_encrypt_operation_not_initialized() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -395,6 +405,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_part() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPartLen: CK_ULONG = 0;
Expand All @@ -412,6 +423,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_encrypted_part_len() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -429,6 +441,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_encrypted_part() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -446,6 +459,7 @@ mod tests {

#[test]
fn test_encrypt_update_buffer_too_small() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = vec![0; 100];
Expand All @@ -464,6 +478,7 @@ mod tests {

#[test]
fn test_encrypt_update_operation_not_initialized() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = vec![0; 100];
Expand All @@ -482,6 +497,7 @@ mod tests {

#[test]
fn test_encrypt_final_null_encrypted_part() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPartLen: CK_ULONG = 0;
Expand All @@ -492,6 +508,7 @@ mod tests {

#[test]
fn test_encrypt_final_null_encrypted_part_len() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand All @@ -506,6 +523,7 @@ mod tests {

// #[test]
// fn test_encrypt_final_buffer_too_small() {
// init_for_tests();
// let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

// let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand All @@ -521,6 +539,7 @@ mod tests {

#[test]
fn test_encrypt_final_operation_not_initialized() {
init_for_tests();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand Down
Loading
Loading