1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* COPYRIGHT (c) International Business Machines Corp. 2005-2017
*
* This program is provided under the terms of the Common Public License,
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
* software constitutes recipient's acceptance of CPL-1.0 terms which can be
* found in the file LICENSE file or at
* https://opensource.org/licenses/cpl1.0.php
*/
/* File: tok_obj.c
*
* Test driver for testing the proper storage of token objects
*/
#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <dlfcn.h>
#include "pkcs11types.h"
#include "regress.h"
#include "defs.h"
int do_GetInfo(void);
void init_coprocessor(void);
CK_RV _C_GetFunctionList(CK_FUNCTION_LIST **);
CK_RV open_session_and_login(void)
{
CK_FLAGS flags;
CK_SESSION_HANDLE h_session;
CK_RV rc;
CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
CK_ULONG user_pin_len;
if (get_user_pin(user_pin))
return CKR_FUNCTION_FAILED;
user_pin_len = (CK_ULONG) strlen((char *) user_pin);
// create a USER R/W session
flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
rc = funcs->C_OpenSession(SLOT_ID, flags, NULL, NULL, &h_session);
rc = funcs->C_Login(h_session, CKU_USER, user_pin, user_pin_len);
return rc;
}
// do_create_token_object()
int do_create_token_object(void)
{
CK_FLAGS flags;
CK_SESSION_HANDLE h_session;
CK_RV rc;
CK_BYTE true = TRUE;
CK_BYTE false = FALSE;
CK_OBJECT_HANDLE h_cert1;
CK_OBJECT_CLASS cert1_class = CKO_CERTIFICATE;
CK_CERTIFICATE_TYPE cert1_type = CKC_X_509;
CK_BYTE cert1_subject[] = "Certificate subject #1";
CK_BYTE cert1_value[] =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
CK_ATTRIBUTE cert1_attribs[] = {
{CKA_CLASS, &cert1_class, sizeof(cert1_class)},
{CKA_TOKEN, &true, sizeof(true)},
{CKA_CERTIFICATE_TYPE, &cert1_type, sizeof(cert1_type)},
{CKA_SUBJECT, &cert1_subject, sizeof(cert1_subject)},
{CKA_VALUE, &cert1_value, sizeof(cert1_value)},
{CKA_PRIVATE, &true, sizeof(false)}
};
// create a USER R/W session
flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
rc = funcs->C_OpenSession(SLOT_ID, flags, NULL, NULL, &h_session);
if (rc != CKR_OK) {
show_error(" C_OpenSession #1", rc);
rc = FALSE;
goto done;
}
// create the token objects
rc = funcs->C_CreateObject(h_session, cert1_attribs, 6, &h_cert1);
if (rc != CKR_OK) {
show_error(" C_CreateObject #1", rc);
rc = FALSE;
goto done;
}
rc = TRUE;
done:
funcs->C_CloseSession(h_session);
return rc;
}
#define NUMOBJS 10
void *thread_func(void *thid)
{
int i = 0;
CK_RV rv;
UNUSED(thid);
do {
rv = do_create_token_object();
if (rv != 1)
return NULL;
} while (++i < NUMOBJS);
return thid;
}
#define THREADCNT 5
int main(int argc, char **argv)
{
int i, rc;
pthread_t id[100];
int thid[100];
SLOT_ID = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-slot") == 0) {
if (i + 1 >= argc) {
printf("Slot number missing\n");
return -1;
}
SLOT_ID = atoi(argv[i + 1]);
i++;
}
if (strcmp(argv[i], "-h") == 0) {
printf("usage: %s [-slot <num>] [-h]\n\n", argv[0]);
printf("By default, Slot #1 is used\n\n");
return 0;
}
}
printf("Using slot #%lu...\n\n", SLOT_ID);
rc = do_GetFunctionList();
if (!rc)
return -1;
funcs->C_Initialize(NULL);
if ((rc = open_session_and_login()) != CKR_OK)
return rc;
for (i = 0; i < THREADCNT; i++) {
thid[i] = i;
printf("Creating thread %d \n", thid[i]);
pthread_create(&id[i], NULL, (void *(*)(void *)) thread_func,
(void *) &(thid[i]));
}
for (i = 0; i < THREADCNT; i++) {
printf("Joining thread %ld\n", id[i]);
pthread_join(id[i], NULL);
}
rc = funcs->C_Finalize(NULL);
if (rc != CKR_OK)
return -1;
return 0;
}
|