From 5479e522a5fc375a107d88452d95a6035152975e Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Wed, 24 Jun 2015 10:43:19 -0400 Subject: First batch of tweaks after testing with hsmbully. --- pkcs11.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 19 deletions(-) (limited to 'pkcs11.c') diff --git a/pkcs11.c b/pkcs11.c index cb80888..f99face 100644 --- a/pkcs11.c +++ b/pkcs11.c @@ -107,6 +107,14 @@ #define DEBUG_SQL 1 #endif +#ifndef DEBUG_HAL +#define DEBUG_HAL 0 +#endif + +#ifndef DEBUG_PKCS11 +#define DEBUG_PKCS11 1 +#endif + /* * Default filename for SQL database lives. Can be overriden at * runtime by setting PKCS11_DATABASE environment variable. @@ -124,12 +132,27 @@ #define USE_POSIX 1 #endif +/* + * Whether to use POSIX threads. + */ + +#ifndef USE_PTHREADS +#define USE_PTHREADS USE_POSIX +#endif + +#if USE_PTHREADS && !USE_POSIX +#error Can not use POSIX threads without using POSIX +#endif + #if USE_POSIX #include -#include #include #endif +#if USE_PTHREADS +#include +#endif + /* @@ -298,10 +321,6 @@ static pid_t initialized_pid; * Error checking for libhal calls. */ -#ifndef DEBUG_HAL -#define DEBUG_HAL 0 -#endif - #if DEBUG_HAL static int _hal_check(const hal_error_t err, const char * const expr, const const * const file, const unsigned line) @@ -373,7 +392,7 @@ static int _hal_check(const hal_error_t err, const char * const expr, const cons * Mutex implementation using POSIX mutexes. */ -#if USE_POSIX +#if USE_PTHREADS static CK_RV posix_mutex_create(CK_VOID_PTR_PTR ppMutex) { @@ -484,7 +503,7 @@ static CK_RV posix_mutex_unlock(CK_VOID_PTR pMutex) return rv; } -#endif /* USE_POSIX */ +#endif /* USE_PTHREADS */ @@ -1018,7 +1037,10 @@ static CK_RV p11_object_check_rights(const p11_session_t *session, const CK_OBJECT_HANDLE object_handle, const p11_object_access_t rights) { - static const char session_handle_query[] = + static const char object_exists_query[] = + " SELECT count(*) FROM object WHERE object_handle = ?1"; + + static const char session_object_query[] = " SELECT session_handle FROM session NATURAL JOIN object WHERE object_handle = ?1"; CK_BBOOL object_is_private; @@ -1040,7 +1062,7 @@ static CK_RV p11_object_check_rights(const p11_session_t *session, } /* - * Private objects don't for sessions in the wrong state. + * Private objects don't exist for sessions in the wrong state. */ switch (session->state) { @@ -1051,12 +1073,23 @@ static CK_RV p11_object_check_rights(const p11_session_t *session, lose(CKR_OBJECT_HANDLE_INVALID); } + /* + * Does the object even exist? + */ + + if (!sql_check_ok(sql_prepare(&q, object_exists_query)) || + !sql_check_ok(sqlite3_bind_int64(q, 1, object_handle)) || + !sql_check_row(sqlite3_step(q)) || + !sqlite3_column_int(q, 0)) + lose(CKR_OBJECT_HANDLE_INVALID); + /* * Session objects are only visible to the session which created them. */ if (!is_token_handle(object_handle) && - (!sql_check_ok(sql_prepare(&q, session_handle_query)) || + (!sql_check_ok(sql_finalize_and_clear(&q)) || + !sql_check_ok(sql_prepare(&q, session_object_query)) || !sql_check_ok(sqlite3_bind_int64(q, 1, object_handle)) || !sql_check_row(sqlite3_step(q)) || sqlite3_column_int64(q, 0) != session->handle)) @@ -1632,6 +1665,11 @@ static CK_RV p11_check_keypair_attributes_check_template_1(const CK_ATTRIBUTE_TY rv = CKR_OK; fail: +#if DEBUG_PKCS11 + if (rv != CKR_OK) + fprintf(stderr, "p11_check_keypair_attributes_check_template_1() rejected attribute 0x%08lx\n", + (unsigned long) type); +#endif return rv; } @@ -2067,7 +2105,7 @@ CK_RV C_Initialize(CK_VOID_PTR pInitArgs) } else if ((a->flags & CKF_OS_LOCKING_OK) != 0) { -#if USE_POSIX +#if USE_PTHREADS mutex_cb_create = posix_mutex_create; mutex_cb_destroy = posix_mutex_destroy; mutex_cb_lock = posix_mutex_lock; @@ -2599,9 +2637,8 @@ CK_RV C_DestroyObject(CK_SESSION_HANDLE hSession, !sql_check_ok(sql_finalize_and_clear(&q)))) lose(CKR_FUNCTION_FAILED); - if ( - !sql_check_ok(sql_prepare(&q, delete_object)) || - !sql_check_ok(sqlite3_bind_int64(q, 1, hObject)) || + if (!sql_check_ok(sql_prepare(&q, delete_object)) || + !sql_check_ok(sqlite3_bind_int64(q, 1, hObject)) || !sql_check_done(sqlite3_step(q))) lose(CKR_FUNCTION_FAILED); @@ -3255,6 +3292,59 @@ CK_RV C_GenerateRandom(CK_SESSION_HANDLE hSession, +/* + * hsmbully wants additional methods, no real surprise. + */ + +/* + * Supply information about a particular mechanism. We may want a + * more generic structure for this, for the moment, just answer the + * questions hsmbully is asking. + * + * Not really sure whether I should be setting CKF_HW here or not, RSA + * is a mix of hardware and software at the moment, but I'm also a + * little unclear on what "the device" means in this context, so let's + * just say that if it's implemented by libhal or the Verilog hiding + * behind libhal, it's implemented in hardware. + */ + +CK_RV C_GetMechanismInfo(CK_SLOT_ID slotID, + CK_MECHANISM_TYPE type, + CK_MECHANISM_INFO_PTR pInfo) +{ + /* + * No locking here, no obvious need for it. + */ + + if (pInfo == NULL) + return CKR_ARGUMENTS_BAD; + + if (slotID != P11_ONE_AND_ONLY_SLOT) + return CKR_SLOT_ID_INVALID; + + switch (type) { + + case CKM_RSA_PKCS_KEY_PAIR_GEN: + pInfo->ulMinKeySize = 1024; + pInfo->ulMaxKeySize = 8192; + pInfo->flags = CKF_HW | CKF_GENERATE_KEY_PAIR; + break; + + case CKM_RSA_PKCS: + pInfo->ulMinKeySize = 1024; + pInfo->ulMaxKeySize = 8192; + pInfo->flags = CKF_HW | CKF_SIGN; + break; + + default: + return CKR_MECHANISM_INVALID; + } + + return CKR_OK; +} + + + /* * Stubs for unsupported functions below here. Per the PKCS #11 * specification, it's OK to skip implementing almost any function in @@ -3287,11 +3377,6 @@ CK_RV C_GetMechanismList(CK_SLOT_ID slotID, CK_ULONG_PTR pulCount) { return CKR_FUNCTION_NOT_SUPPORTED; } -CK_RV C_GetMechanismInfo(CK_SLOT_ID slotID, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR pInfo) -{ return CKR_FUNCTION_NOT_SUPPORTED; } - CK_RV C_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, -- cgit v1.2.3