| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-module.c: A test PKCS#11 module implementation |
| 3 |
|
| 4 |
Copyright (C) 2009 Stefan Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "mock-module.h" |
| 26 |
|
| 27 |
#include "egg/egg-secure-memory.h" |
| 28 |
|
| 29 |
/* Include all the module entry points */ |
| 30 |
#include "gkm/gkm-module-ep.h" |
| 31 |
GKM_DEFINE_MODULE (test_module, GKM_TYPE_MODULE); |
| 32 |
|
| 33 |
#include "gkm/gkm-certificate.h" |
| 34 |
|
| 35 |
EGG_SECURE_DEFINE_GLIB_GLOBALS (); |
| 36 |
|
| 37 |
GkmModule* |
| 38 |
mock_module_initialize_and_enter (void) |
| 39 |
{ |
| 40 |
CK_RV rv; |
| 41 |
|
| 42 |
gkm_crypto_initialize (); |
| 43 |
rv = test_module_function_list->C_Initialize (NULL); |
| 44 |
g_return_val_if_fail (rv == CKR_OK, NULL); |
| 45 |
|
| 46 |
g_return_val_if_fail (pkcs11_module, NULL); |
| 47 |
|
| 48 |
mock_module_enter (); |
| 49 |
return pkcs11_module; |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
mock_module_leave_and_finalize (void) |
| 54 |
{ |
| 55 |
CK_RV rv; |
| 56 |
|
| 57 |
mock_module_leave (); |
| 58 |
rv = test_module_function_list->C_Finalize (NULL); |
| 59 |
g_return_if_fail (rv == CKR_OK); |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
mock_module_leave (void) |
| 64 |
{ |
| 65 |
g_mutex_unlock (&pkcs11_module_mutex); |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
mock_module_enter (void) |
| 70 |
{ |
| 71 |
g_mutex_lock (&pkcs11_module_mutex); |
| 72 |
} |
| 73 |
|
| 74 |
GkmSession* |
| 75 |
mock_module_open_session (gboolean writable) |
| 76 |
{ |
| 77 |
CK_ULONG flags = CKF_SERIAL_SESSION; |
| 78 |
CK_SESSION_HANDLE handle; |
| 79 |
GkmSession *session; |
| 80 |
CK_RV rv; |
| 81 |
|
| 82 |
if (writable) |
| 83 |
flags |= CKF_RW_SESSION; |
| 84 |
|
| 85 |
rv = gkm_module_C_OpenSession (pkcs11_module, 1, flags, NULL, NULL, &handle); |
| 86 |
g_assert (rv == CKR_OK); |
| 87 |
|
| 88 |
session = gkm_module_lookup_session (pkcs11_module, handle); |
| 89 |
g_assert (session); |
| 90 |
|
| 91 |
return session; |
| 92 |
} |
| 93 |
|
| 94 |
GkmObject* |
| 95 |
mock_module_object_new (GkmSession *session) |
| 96 |
{ |
| 97 |
CK_BBOOL token = CK_FALSE; |
| 98 |
CK_OBJECT_CLASS klass = CKO_CERTIFICATE; |
| 99 |
CK_CERTIFICATE_TYPE type = CKC_X_509; |
| 100 |
GkmObject *object; |
| 101 |
|
| 102 |
gsize n_data; |
| 103 |
gchar *data; |
| 104 |
|
| 105 |
CK_ATTRIBUTE attrs[] = { |
| 106 |
{ CKA_VALUE, NULL, 0 }, |
| 107 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 108 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 109 |
{ CKA_CERTIFICATE_TYPE, &type, sizeof (type) }, |
| 110 |
}; |
| 111 |
|
| 112 |
if (!g_file_get_contents (SRCDIR "/pkcs11/gkm/fixtures/test-certificate-1.der", &data, &n_data, NULL)) |
| 113 |
g_assert_not_reached (); |
| 114 |
|
| 115 |
attrs[0].pValue = data; |
| 116 |
attrs[0].ulValueLen = n_data; |
| 117 |
|
| 118 |
object = gkm_session_create_object_for_factory (session, GKM_FACTORY_CERTIFICATE, NULL, |
| 119 |
attrs, G_N_ELEMENTS (attrs)); |
| 120 |
if (object) /* Owned by storage */ |
| 121 |
g_object_unref (object); |
| 122 |
|
| 123 |
g_free (data); |
| 124 |
return object; |
| 125 |
} |