| 1 |
/* |
| 2 |
* COPYRIGHT (c) International Business Machines Corp. 2005-2017 |
| 3 |
* |
| 4 |
* This program is provided under the terms of the Common Public License, |
| 5 |
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this |
| 6 |
* software constitutes recipient's acceptance of CPL-1.0 terms which can be |
| 7 |
* found in the file LICENSE file or at |
| 8 |
* https://opensource.org/licenses/cpl1.0.php |
| 9 |
*/ |
| 10 |
|
| 11 |
// File: tok_obj.c |
| 12 |
// |
| 13 |
// Test driver for testing the proper storage of token objects |
| 14 |
// |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <memory.h> |
| 20 |
#include <dlfcn.h> |
| 21 |
|
| 22 |
#include "pkcs11types.h" |
| 23 |
|
| 24 |
#include "regress.h" |
| 25 |
#include "common.c" |
| 26 |
|
| 27 |
int do_GetInfo(void); |
| 28 |
|
| 29 |
CK_RV C_GetFunctionList(CK_FUNCTION_LIST **); |
| 30 |
CK_BBOOL do_GetFunctionList(void); |
| 31 |
|
| 32 |
int do_inittoken(CK_FUNCTION_LIST * funcs, CK_BYTE * sopass) |
| 33 |
{ |
| 34 |
CK_BYTE label[32]; |
| 35 |
int len; |
| 36 |
CK_RV rc; |
| 37 |
|
| 38 |
memcpy(label, "L13 ", 32); |
| 39 |
|
| 40 |
for (len = 0; len < 31; len++) { |
| 41 |
if (label[len] == '\0') { |
| 42 |
label[len] = ' '; |
| 43 |
break; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
rc = funcs->C_InitToken(SLOT_ID, NULL, strlen((char *) sopass), label); |
| 48 |
if (rc != CKR_ARGUMENTS_BAD) { |
| 49 |
show_error(" C_InitToken Fail #1", rc); |
| 50 |
goto done; |
| 51 |
} |
| 52 |
|
| 53 |
rc = funcs->C_InitToken(SLOT_ID, sopass, strlen((char *) sopass), NULL); |
| 54 |
if (rc != CKR_ARGUMENTS_BAD) { |
| 55 |
show_error(" C_InitToken Fail #2", rc); |
| 56 |
goto done; |
| 57 |
} |
| 58 |
|
| 59 |
rc = funcs->C_InitToken(SLOT_ID, sopass, strlen((char *) sopass), label); |
| 60 |
if (rc != CKR_OK) { |
| 61 |
show_error(" C_InitToken #1", rc); |
| 62 |
goto done; |
| 63 |
} |
| 64 |
|
| 65 |
done: |
| 66 |
return rc; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
int main(int argc, char **argv) |
| 71 |
{ |
| 72 |
CK_BYTE *pass = NULL; |
| 73 |
int rc; |
| 74 |
int i; |
| 75 |
|
| 76 |
SLOT_ID = 0; |
| 77 |
|
| 78 |
for (i = 1; i < argc; i++) { |
| 79 |
if (strcmp(argv[i], "-slot") == 0) { |
| 80 |
if (i + 1 >= argc) { |
| 81 |
printf("Slot number missing\n"); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
SLOT_ID = atoi(argv[i + 1]); |
| 85 |
i++; |
| 86 |
} else if (strcmp(argv[i], "-pass") == 0) { |
| 87 |
if (i + 1 >= argc) { |
| 88 |
printf("PIN argument missing\n"); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
pass = (CK_BYTE_PTR) strdup(argv[i + 1]); |
| 92 |
i++; |
| 93 |
} else { |
| 94 |
printf("usage: %s [-slot <num>] [-h] -pass pass\n\n", argv[0]); |
| 95 |
printf("By default, Slot 0 is used\n\n"); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (!pass) { |
| 101 |
printf("usage: %s [-slot <num>] [-h] [-pass pass]\n\n", argv[0]); |
| 102 |
printf("By default, Slot 0 is used\n\n"); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
printf("Using slot #%lu...\n\n", SLOT_ID); |
| 107 |
|
| 108 |
rc = do_GetFunctionList(); |
| 109 |
if ((rc != TRUE) || (funcs == NULL)) { |
| 110 |
printf("do_GetFunctionList failed.\n"); |
| 111 |
return rc; |
| 112 |
} |
| 113 |
|
| 114 |
funcs->C_Initialize(NULL); |
| 115 |
|
| 116 |
rc = do_inittoken(funcs, pass); |
| 117 |
|
| 118 |
free(pass); |
| 119 |
|
| 120 |
funcs->C_Finalize(NULL); |
| 121 |
|
| 122 |
return rc; |
| 123 |
} |