security/nss/tests/pkcs11/netscape/trivial/trivial.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * This is a very trivial program I wrote for testing out a
michael@0 7 * couple data-only Cryptoki modules for NSS. It's not a "real"
michael@0 8 * test program that prints out nice "PASS" or "FAIL" messages;
michael@0 9 * it just makes calls and dumps data.
michael@0 10 */
michael@0 11
michael@0 12 #include "config.h"
michael@0 13
michael@0 14 #ifdef HAVE_NSPR_H
michael@0 15 #include "nspr.h"
michael@0 16 #else
michael@0 17 #error "NSPR is required."
michael@0 18 #endif
michael@0 19
michael@0 20 #ifdef WITH_NSS
michael@0 21 #define FGMR 1
michael@0 22 #include "ck.h"
michael@0 23 #else
michael@0 24 #include "pkcs11t.h"
michael@0 25 #include "pkcs11.h"
michael@0 26 #endif
michael@0 27
michael@0 28 /* The RSA versions are sloppier with namespaces */
michael@0 29 #ifndef CK_TRUE
michael@0 30 #define CK_TRUE TRUE
michael@0 31 #endif
michael@0 32
michael@0 33 #ifndef CK_FALSE
michael@0 34 #define CK_FALSE FALSE
michael@0 35 #endif
michael@0 36
michael@0 37 int
michael@0 38 rmain
michael@0 39 (
michael@0 40 int argc,
michael@0 41 char *argv[]
michael@0 42 );
michael@0 43
michael@0 44 int
michael@0 45 main
michael@0 46 (
michael@0 47 int argc,
michael@0 48 char *argv[]
michael@0 49 )
michael@0 50 {
michael@0 51 int rv = 0;
michael@0 52
michael@0 53 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 14);
michael@0 54 rv = rmain(argc, argv);
michael@0 55 PR_Cleanup();
michael@0 56
michael@0 57 return rv;
michael@0 58 }
michael@0 59
michael@0 60 static CK_ATTRIBUTE_TYPE all_known_attribute_types[] = {
michael@0 61 CKA_CLASS,
michael@0 62 CKA_TOKEN,
michael@0 63 CKA_PRIVATE,
michael@0 64 CKA_LABEL,
michael@0 65 CKA_APPLICATION,
michael@0 66 CKA_VALUE,
michael@0 67 CKA_CERTIFICATE_TYPE,
michael@0 68 CKA_ISSUER,
michael@0 69 CKA_SERIAL_NUMBER,
michael@0 70 CKA_KEY_TYPE,
michael@0 71 CKA_SUBJECT,
michael@0 72 CKA_ID,
michael@0 73 CKA_SENSITIVE,
michael@0 74 CKA_ENCRYPT,
michael@0 75 CKA_DECRYPT,
michael@0 76 CKA_WRAP,
michael@0 77 CKA_UNWRAP,
michael@0 78 CKA_SIGN,
michael@0 79 CKA_SIGN_RECOVER,
michael@0 80 CKA_VERIFY,
michael@0 81 CKA_VERIFY_RECOVER,
michael@0 82 CKA_DERIVE,
michael@0 83 CKA_START_DATE,
michael@0 84 CKA_END_DATE,
michael@0 85 CKA_MODULUS,
michael@0 86 CKA_MODULUS_BITS,
michael@0 87 CKA_PUBLIC_EXPONENT,
michael@0 88 CKA_PRIVATE_EXPONENT,
michael@0 89 CKA_PRIME_1,
michael@0 90 CKA_PRIME_2,
michael@0 91 CKA_EXPONENT_1,
michael@0 92 CKA_EXPONENT_2,
michael@0 93 CKA_COEFFICIENT,
michael@0 94 CKA_PRIME,
michael@0 95 CKA_SUBPRIME,
michael@0 96 CKA_BASE,
michael@0 97 CKA_VALUE_BITS,
michael@0 98 CKA_VALUE_LEN,
michael@0 99 CKA_EXTRACTABLE,
michael@0 100 CKA_LOCAL,
michael@0 101 CKA_NEVER_EXTRACTABLE,
michael@0 102 CKA_ALWAYS_SENSITIVE,
michael@0 103 CKA_MODIFIABLE,
michael@0 104 #ifdef CKA_NETSCAPE
michael@0 105 CKA_NETSCAPE_URL,
michael@0 106 CKA_NETSCAPE_EMAIL,
michael@0 107 CKA_NETSCAPE_SMIME_INFO,
michael@0 108 CKA_NETSCAPE_SMIME_TIMESTAMP,
michael@0 109 CKA_NETSCAPE_PKCS8_SALT,
michael@0 110 CKA_NETSCAPE_PASSWORD_CHECK,
michael@0 111 CKA_NETSCAPE_EXPIRES,
michael@0 112 #endif /* CKA_NETSCAPE */
michael@0 113 #ifdef CKA_TRUST
michael@0 114 CKA_TRUST_DIGITAL_SIGNATURE,
michael@0 115 CKA_TRUST_NON_REPUDIATION,
michael@0 116 CKA_TRUST_KEY_ENCIPHERMENT,
michael@0 117 CKA_TRUST_DATA_ENCIPHERMENT,
michael@0 118 CKA_TRUST_KEY_AGREEMENT,
michael@0 119 CKA_TRUST_KEY_CERT_SIGN,
michael@0 120 CKA_TRUST_CRL_SIGN,
michael@0 121 CKA_TRUST_SERVER_AUTH,
michael@0 122 CKA_TRUST_CLIENT_AUTH,
michael@0 123 CKA_TRUST_CODE_SIGNING,
michael@0 124 CKA_TRUST_EMAIL_PROTECTION,
michael@0 125 CKA_TRUST_IPSEC_END_SYSTEM,
michael@0 126 CKA_TRUST_IPSEC_TUNNEL,
michael@0 127 CKA_TRUST_IPSEC_USER,
michael@0 128 CKA_TRUST_TIME_STAMPING,
michael@0 129 #endif /* CKA_TRUST */
michael@0 130 };
michael@0 131
michael@0 132 static number_of_all_known_attribute_types =
michael@0 133 (sizeof(all_known_attribute_types)/sizeof(all_known_attribute_types[0]));
michael@0 134
michael@0 135 int
michael@0 136 usage
michael@0 137 (
michael@0 138 char *argv0
michael@0 139 )
michael@0 140 {
michael@0 141 PR_fprintf(PR_STDERR, "Usage: %s [-i {string|--}] <library>.so\n", argv0);
michael@0 142 return 1;
michael@0 143 }
michael@0 144
michael@0 145 int
michael@0 146 rmain
michael@0 147 (
michael@0 148 int argc,
michael@0 149 char *argv[]
michael@0 150 )
michael@0 151 {
michael@0 152 char *argv0 = argv[0];
michael@0 153 PRLibrary *lib;
michael@0 154 CK_C_GetFunctionList gfl;
michael@0 155 CK_FUNCTION_LIST_PTR epv = (CK_FUNCTION_LIST_PTR)NULL;
michael@0 156 CK_RV ck_rv;
michael@0 157 CK_INFO info;
michael@0 158 CK_ULONG nSlots;
michael@0 159 CK_SLOT_ID *pSlots;
michael@0 160 CK_ULONG i;
michael@0 161 CK_C_INITIALIZE_ARGS ia, *iap;
michael@0 162
michael@0 163 (void)memset(&ia, 0, sizeof(CK_C_INITIALIZE_ARGS));
michael@0 164 iap = (CK_C_INITIALIZE_ARGS *)NULL;
michael@0 165 while( argv++, --argc ) {
michael@0 166 if( '-' == argv[0][0] ) {
michael@0 167 switch( argv[0][1] ) {
michael@0 168 case 'i':
michael@0 169 iap = &ia;
michael@0 170 if( ((char *)NULL != argv[1]) && ('-' != argv[1][0]) ) {
michael@0 171 #ifdef WITH_NSS
michael@0 172 ia.pConfig = argv[1];
michael@0 173 ia.ulConfigLen = strlen(argv[1]);
michael@0 174 argv++, --argc;
michael@0 175 #else
michael@0 176 return usage(argv0);
michael@0 177 #endif /* WITH_NSS */
michael@0 178 }
michael@0 179 break;
michael@0 180 case '-':
michael@0 181 argv++, --argc;
michael@0 182 goto endargs;
michael@0 183 default:
michael@0 184 return usage(argv0);
michael@0 185 }
michael@0 186 } else {
michael@0 187 break;
michael@0 188 }
michael@0 189 }
michael@0 190 endargs:;
michael@0 191
michael@0 192 if( 1 != argc ) {
michael@0 193 return usage(argv0);
michael@0 194 }
michael@0 195
michael@0 196 lib = PR_LoadLibrary(argv[0]);
michael@0 197 if( (PRLibrary *)NULL == lib ) {
michael@0 198 PR_fprintf(PR_STDERR, "Can't load %s: %ld, %ld\n", argv[1], PR_GetError(), PR_GetOSError());
michael@0 199 return 1;
michael@0 200 }
michael@0 201
michael@0 202 gfl = (CK_C_GetFunctionList)PR_FindSymbol(lib, "C_GetFunctionList");
michael@0 203 if( (CK_C_GetFunctionList)NULL == gfl ) {
michael@0 204 PR_fprintf(PR_STDERR, "Can't find C_GetFunctionList in %s: %ld, %ld\n", argv[1],
michael@0 205 PR_GetError(), PR_GetOSError());
michael@0 206 return 1;
michael@0 207 }
michael@0 208
michael@0 209 ck_rv = (*gfl)(&epv);
michael@0 210 if( CKR_OK != ck_rv ) {
michael@0 211 PR_fprintf(PR_STDERR, "CK_GetFunctionList returned 0x%08x\n", ck_rv);
michael@0 212 return 1;
michael@0 213 }
michael@0 214
michael@0 215 PR_fprintf(PR_STDOUT, "Module %s loaded, epv = 0x%08x.\n\n", argv[1], (CK_ULONG)epv);
michael@0 216
michael@0 217 /* C_Initialize */
michael@0 218 ck_rv = epv->C_Initialize(iap);
michael@0 219 if( CKR_OK != ck_rv ) {
michael@0 220 PR_fprintf(PR_STDERR, "C_Initialize returned 0x%08x\n", ck_rv);
michael@0 221 return 1;
michael@0 222 }
michael@0 223
michael@0 224 /* C_GetInfo */
michael@0 225 (void)memset(&info, 0, sizeof(CK_INFO));
michael@0 226 ck_rv = epv->C_GetInfo(&info);
michael@0 227 if( CKR_OK != ck_rv ) {
michael@0 228 PR_fprintf(PR_STDERR, "C_GetInfo returned 0x%08x\n", ck_rv);
michael@0 229 return 1;
michael@0 230 }
michael@0 231
michael@0 232 PR_fprintf(PR_STDOUT, "Module Info:\n");
michael@0 233 PR_fprintf(PR_STDOUT, " cryptokiVersion = %lu.%02lu\n",
michael@0 234 (PRUint32)info.cryptokiVersion.major, (PRUint32)info.cryptokiVersion.minor);
michael@0 235 PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", info.manufacturerID);
michael@0 236 PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", info.flags);
michael@0 237 PR_fprintf(PR_STDOUT, " libraryDescription = \"%.32s\"\n", info.libraryDescription);
michael@0 238 PR_fprintf(PR_STDOUT, " libraryVersion = %lu.%02lu\n",
michael@0 239 (PRUint32)info.libraryVersion.major, (PRUint32)info.libraryVersion.minor);
michael@0 240 PR_fprintf(PR_STDOUT, "\n");
michael@0 241
michael@0 242 /* C_GetSlotList */
michael@0 243 nSlots = 0;
michael@0 244 ck_rv = epv->C_GetSlotList(CK_FALSE, (CK_SLOT_ID_PTR)CK_NULL_PTR, &nSlots);
michael@0 245 switch( ck_rv ) {
michael@0 246 case CKR_BUFFER_TOO_SMALL:
michael@0 247 case CKR_OK:
michael@0 248 break;
michael@0 249 default:
michael@0 250 PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, NULL, ) returned 0x%08x\n", ck_rv);
michael@0 251 return 1;
michael@0 252 }
michael@0 253
michael@0 254 PR_fprintf(PR_STDOUT, "There are %lu slots.\n", nSlots);
michael@0 255
michael@0 256 pSlots = (CK_SLOT_ID_PTR)PR_Calloc(nSlots, sizeof(CK_SLOT_ID));
michael@0 257 if( (CK_SLOT_ID_PTR)NULL == pSlots ) {
michael@0 258 PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nSlots * sizeof(CK_SLOT_ID));
michael@0 259 return 1;
michael@0 260 }
michael@0 261
michael@0 262 ck_rv = epv->C_GetSlotList(CK_FALSE, pSlots, &nSlots);
michael@0 263 if( CKR_OK != ck_rv ) {
michael@0 264 PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, , ) returned 0x%08x\n", ck_rv);
michael@0 265 return 1;
michael@0 266 }
michael@0 267
michael@0 268 for( i = 0; i < nSlots; i++ ) {
michael@0 269 PR_fprintf(PR_STDOUT, " [%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);
michael@0 270 }
michael@0 271
michael@0 272 PR_fprintf(PR_STDOUT, "\n");
michael@0 273
michael@0 274 /* C_GetSlotInfo */
michael@0 275 for( i = 0; i < nSlots; i++ ) {
michael@0 276 CK_SLOT_INFO sinfo;
michael@0 277
michael@0 278 PR_fprintf(PR_STDOUT, "[%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);
michael@0 279
michael@0 280 (void)memset(&sinfo, 0, sizeof(CK_SLOT_INFO));
michael@0 281 ck_rv = epv->C_GetSlotInfo(pSlots[i], &sinfo);
michael@0 282 if( CKR_OK != ck_rv ) {
michael@0 283 PR_fprintf(PR_STDERR, "C_GetSlotInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 284 return 1;
michael@0 285 }
michael@0 286
michael@0 287 PR_fprintf(PR_STDOUT, " Slot Info:\n");
michael@0 288 PR_fprintf(PR_STDOUT, " slotDescription = \"%.64s\"\n", sinfo.slotDescription);
michael@0 289 PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", sinfo.manufacturerID);
michael@0 290 PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", sinfo.flags);
michael@0 291 PR_fprintf(PR_STDOUT, " -> TOKEN PRESENT = %s\n",
michael@0 292 sinfo.flags & CKF_TOKEN_PRESENT ? "TRUE" : "FALSE");
michael@0 293 PR_fprintf(PR_STDOUT, " -> REMOVABLE DEVICE = %s\n",
michael@0 294 sinfo.flags & CKF_REMOVABLE_DEVICE ? "TRUE" : "FALSE");
michael@0 295 PR_fprintf(PR_STDOUT, " -> HW SLOT = %s\n",
michael@0 296 sinfo.flags & CKF_HW_SLOT ? "TRUE" : "FALSE");
michael@0 297 PR_fprintf(PR_STDOUT, " hardwareVersion = %lu.%02lu\n",
michael@0 298 (PRUint32)sinfo.hardwareVersion.major, (PRUint32)sinfo.hardwareVersion.minor);
michael@0 299 PR_fprintf(PR_STDOUT, " firmwareVersion = %lu.%02lu\n",
michael@0 300 (PRUint32)sinfo.firmwareVersion.major, (PRUint32)sinfo.firmwareVersion.minor);
michael@0 301
michael@0 302 if( sinfo.flags & CKF_TOKEN_PRESENT ) {
michael@0 303 CK_TOKEN_INFO tinfo;
michael@0 304 CK_MECHANISM_TYPE *pMechanismList;
michael@0 305 CK_ULONG nMechanisms = 0;
michael@0 306 CK_ULONG j;
michael@0 307
michael@0 308 (void)memset(&tinfo, 0, sizeof(CK_TOKEN_INFO));
michael@0 309 ck_rv = epv->C_GetTokenInfo(pSlots[i], &tinfo);
michael@0 310 if( CKR_OK != ck_rv ) {
michael@0 311 PR_fprintf(PR_STDERR, "C_GetTokenInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 312 return 1;
michael@0 313 }
michael@0 314
michael@0 315 PR_fprintf(PR_STDOUT, " Token Info:\n");
michael@0 316 PR_fprintf(PR_STDOUT, " label = \"%.32s\"\n", tinfo.label);
michael@0 317 PR_fprintf(PR_STDOUT, " manufacturerID = \"%.32s\"\n", tinfo.manufacturerID);
michael@0 318 PR_fprintf(PR_STDOUT, " model = \"%.16s\"\n", tinfo.model);
michael@0 319 PR_fprintf(PR_STDOUT, " serialNumber = \"%.16s\"\n", tinfo.serialNumber);
michael@0 320 PR_fprintf(PR_STDOUT, " flags = 0x%08lx\n", tinfo.flags);
michael@0 321 PR_fprintf(PR_STDOUT, " -> RNG = %s\n",
michael@0 322 tinfo.flags & CKF_RNG ? "TRUE" : "FALSE");
michael@0 323 PR_fprintf(PR_STDOUT, " -> WRITE PROTECTED = %s\n",
michael@0 324 tinfo.flags & CKF_WRITE_PROTECTED ? "TRUE" : "FALSE");
michael@0 325 PR_fprintf(PR_STDOUT, " -> LOGIN REQUIRED = %s\n",
michael@0 326 tinfo.flags & CKF_LOGIN_REQUIRED ? "TRUE" : "FALSE");
michael@0 327 PR_fprintf(PR_STDOUT, " -> USER PIN INITIALIZED = %s\n",
michael@0 328 tinfo.flags & CKF_USER_PIN_INITIALIZED ? "TRUE" : "FALSE");
michael@0 329 PR_fprintf(PR_STDOUT, " -> RESTORE KEY NOT NEEDED = %s\n",
michael@0 330 tinfo.flags & CKF_RESTORE_KEY_NOT_NEEDED ? "TRUE" : "FALSE");
michael@0 331 PR_fprintf(PR_STDOUT, " -> CLOCK ON TOKEN = %s\n",
michael@0 332 tinfo.flags & CKF_CLOCK_ON_TOKEN ? "TRUE" : "FALSE");
michael@0 333 #ifdef CKF_SUPPORTS_PARALLEL
michael@0 334 PR_fprintf(PR_STDOUT, " -> SUPPORTS PARALLEL = %s\n",
michael@0 335 tinfo.flags & CKF_SUPPORTS_PARALLEL ? "TRUE" : "FALSE");
michael@0 336 #endif /* CKF_SUPPORTS_PARALLEL */
michael@0 337 PR_fprintf(PR_STDOUT, " -> PROTECTED AUTHENTICATION PATH = %s\n",
michael@0 338 tinfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH ? "TRUE" : "FALSE");
michael@0 339 PR_fprintf(PR_STDOUT, " -> DUAL_CRYPTO_OPERATIONS = %s\n",
michael@0 340 tinfo.flags & CKF_DUAL_CRYPTO_OPERATIONS ? "TRUE" : "FALSE");
michael@0 341 PR_fprintf(PR_STDOUT, " ulMaxSessionCount = %lu\n", tinfo.ulMaxSessionCount);
michael@0 342 PR_fprintf(PR_STDOUT, " ulSessionCount = %lu\n", tinfo.ulSessionCount);
michael@0 343 PR_fprintf(PR_STDOUT, " ulMaxRwSessionCount = %lu\n", tinfo.ulMaxRwSessionCount);
michael@0 344 PR_fprintf(PR_STDOUT, " ulRwSessionCount = %lu\n", tinfo.ulRwSessionCount);
michael@0 345 PR_fprintf(PR_STDOUT, " ulMaxPinLen = %lu\n", tinfo.ulMaxPinLen);
michael@0 346 PR_fprintf(PR_STDOUT, " ulMinPinLen = %lu\n", tinfo.ulMinPinLen);
michael@0 347 PR_fprintf(PR_STDOUT, " ulTotalPublicMemory = %lu\n", tinfo.ulTotalPublicMemory);
michael@0 348 PR_fprintf(PR_STDOUT, " ulFreePublicMemory = %lu\n", tinfo.ulFreePublicMemory);
michael@0 349 PR_fprintf(PR_STDOUT, " ulTotalPrivateMemory = %lu\n", tinfo.ulTotalPrivateMemory);
michael@0 350 PR_fprintf(PR_STDOUT, " ulFreePrivateMemory = %lu\n", tinfo.ulFreePrivateMemory);
michael@0 351 PR_fprintf(PR_STDOUT, " hardwareVersion = %lu.%02lu\n",
michael@0 352 (PRUint32)tinfo.hardwareVersion.major, (PRUint32)tinfo.hardwareVersion.minor);
michael@0 353 PR_fprintf(PR_STDOUT, " firmwareVersion = %lu.%02lu\n",
michael@0 354 (PRUint32)tinfo.firmwareVersion.major, (PRUint32)tinfo.firmwareVersion.minor);
michael@0 355 PR_fprintf(PR_STDOUT, " utcTime = \"%.16s\"\n", tinfo.utcTime);
michael@0 356
michael@0 357
michael@0 358 ck_rv = epv->C_GetMechanismList(pSlots[i], (CK_MECHANISM_TYPE_PTR)CK_NULL_PTR, &nMechanisms);
michael@0 359 switch( ck_rv ) {
michael@0 360 case CKR_BUFFER_TOO_SMALL:
michael@0 361 case CKR_OK:
michael@0 362 break;
michael@0 363 default:
michael@0 364 PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, NULL, ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 365 return 1;
michael@0 366 }
michael@0 367
michael@0 368 PR_fprintf(PR_STDOUT, " %lu mechanisms:\n", nMechanisms);
michael@0 369
michael@0 370 pMechanismList = (CK_MECHANISM_TYPE_PTR)PR_Calloc(nMechanisms, sizeof(CK_MECHANISM_TYPE));
michael@0 371 if( (CK_MECHANISM_TYPE_PTR)NULL == pMechanismList ) {
michael@0 372 PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n",
michael@0 373 nMechanisms * sizeof(CK_MECHANISM_TYPE));
michael@0 374 return 1;
michael@0 375 }
michael@0 376
michael@0 377 ck_rv = epv->C_GetMechanismList(pSlots[i], pMechanismList, &nMechanisms);
michael@0 378 if( CKR_OK != ck_rv ) {
michael@0 379 PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 380 return 1;
michael@0 381 }
michael@0 382
michael@0 383 for( j = 0; j < nMechanisms; j++ ) {
michael@0 384 PR_fprintf(PR_STDOUT, " {%lu}: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]);
michael@0 385 }
michael@0 386
michael@0 387 PR_fprintf(PR_STDOUT, "\n");
michael@0 388
michael@0 389 for( j = 0; j < nMechanisms; j++ ) {
michael@0 390 CK_MECHANISM_INFO minfo;
michael@0 391
michael@0 392 (void)memset(&minfo, 0, sizeof(CK_MECHANISM_INFO));
michael@0 393 ck_rv = epv->C_GetMechanismInfo(pSlots[i], pMechanismList[j], &minfo);
michael@0 394 if( CKR_OK != ck_rv ) {
michael@0 395 PR_fprintf(PR_STDERR, "C_GetMechanismInfo(%lu, %lu, ) returned 0x%08x\n", pSlots[i],
michael@0 396 pMechanismList[j]);
michael@0 397 return 1;
michael@0 398 }
michael@0 399
michael@0 400 PR_fprintf(PR_STDOUT, " [%lu]: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]);
michael@0 401 PR_fprintf(PR_STDOUT, " ulMinKeySize = %lu\n", minfo.ulMinKeySize);
michael@0 402 PR_fprintf(PR_STDOUT, " ulMaxKeySize = %lu\n", minfo.ulMaxKeySize);
michael@0 403 PR_fprintf(PR_STDOUT, " flags = 0x%08x\n", minfo.flags);
michael@0 404 PR_fprintf(PR_STDOUT, " -> HW = %s\n", minfo.flags & CKF_HW ? "TRUE" : "FALSE");
michael@0 405 PR_fprintf(PR_STDOUT, " -> ENCRYPT = %s\n", minfo.flags & CKF_ENCRYPT ? "TRUE" : "FALSE");
michael@0 406 PR_fprintf(PR_STDOUT, " -> DECRYPT = %s\n", minfo.flags & CKF_DECRYPT ? "TRUE" : "FALSE");
michael@0 407 PR_fprintf(PR_STDOUT, " -> DIGEST = %s\n", minfo.flags & CKF_DIGEST ? "TRUE" : "FALSE");
michael@0 408 PR_fprintf(PR_STDOUT, " -> SIGN = %s\n", minfo.flags & CKF_SIGN ? "TRUE" : "FALSE");
michael@0 409 PR_fprintf(PR_STDOUT, " -> SIGN_RECOVER = %s\n", minfo.flags & CKF_SIGN_RECOVER ? "TRUE" : "FALSE");
michael@0 410 PR_fprintf(PR_STDOUT, " -> VERIFY = %s\n", minfo.flags & CKF_VERIFY ? "TRUE" : "FALSE");
michael@0 411 PR_fprintf(PR_STDOUT, " -> VERIFY_RECOVER = %s\n", minfo.flags & CKF_VERIFY_RECOVER ? "TRUE" : "FALSE");
michael@0 412 PR_fprintf(PR_STDOUT, " -> GENERATE = %s\n", minfo.flags & CKF_GENERATE ? "TRUE" : "FALSE");
michael@0 413 PR_fprintf(PR_STDOUT, " -> GENERATE_KEY_PAIR = %s\n", minfo.flags & CKF_GENERATE_KEY_PAIR ? "TRUE" : "FALSE");
michael@0 414 PR_fprintf(PR_STDOUT, " -> WRAP = %s\n", minfo.flags & CKF_WRAP ? "TRUE" : "FALSE");
michael@0 415 PR_fprintf(PR_STDOUT, " -> UNWRAP = %s\n", minfo.flags & CKF_UNWRAP ? "TRUE" : "FALSE");
michael@0 416 PR_fprintf(PR_STDOUT, " -> DERIVE = %s\n", minfo.flags & CKF_DERIVE ? "TRUE" : "FALSE");
michael@0 417 PR_fprintf(PR_STDOUT, " -> EXTENSION = %s\n", minfo.flags & CKF_EXTENSION ? "TRUE" : "FALSE");
michael@0 418
michael@0 419 PR_fprintf(PR_STDOUT, "\n");
michael@0 420 }
michael@0 421
michael@0 422 if( tinfo.flags & CKF_LOGIN_REQUIRED ) {
michael@0 423 PR_fprintf(PR_STDERR, "*** LOGIN REQUIRED but not yet implemented ***\n");
michael@0 424 /* all the stuff about logging in as SO and setting the user pin if needed, etc. */
michael@0 425 return 2;
michael@0 426 }
michael@0 427
michael@0 428 /* session to find objects */
michael@0 429 {
michael@0 430 CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
michael@0 431 CK_SESSION_INFO sinfo;
michael@0 432 CK_ATTRIBUTE_PTR pTemplate;
michael@0 433 CK_ULONG tnObjects = 0;
michael@0 434
michael@0 435 ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
michael@0 436 if( CKR_OK != ck_rv ) {
michael@0 437 PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 438 return 1;
michael@0 439 }
michael@0 440
michael@0 441 PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h);
michael@0 442
michael@0 443 (void)memset(&sinfo, 0, sizeof(CK_SESSION_INFO));
michael@0 444 ck_rv = epv->C_GetSessionInfo(h, &sinfo);
michael@0 445 if( CKR_OK != ck_rv ) {
michael@0 446 PR_fprintf(PR_STDOUT, "C_GetSessionInfo(%lu, ) returned 0x%08x\n", h, ck_rv);
michael@0 447 return 1;
michael@0 448 }
michael@0 449
michael@0 450 PR_fprintf(PR_STDOUT, " SESSION INFO:\n");
michael@0 451 PR_fprintf(PR_STDOUT, " slotID = %lu\n", sinfo.slotID);
michael@0 452 PR_fprintf(PR_STDOUT, " state = %lu\n", sinfo.state);
michael@0 453 PR_fprintf(PR_STDOUT, " flags = 0x%08x\n", sinfo.flags);
michael@0 454 #ifdef CKF_EXCLUSIVE_SESSION
michael@0 455 PR_fprintf(PR_STDOUT, " -> EXCLUSIVE SESSION = %s\n", sinfo.flags & CKF_EXCLUSIVE_SESSION ? "TRUE" : "FALSE");
michael@0 456 #endif /* CKF_EXCLUSIVE_SESSION */
michael@0 457 PR_fprintf(PR_STDOUT, " -> RW SESSION = %s\n", sinfo.flags & CKF_RW_SESSION ? "TRUE" : "FALSE");
michael@0 458 PR_fprintf(PR_STDOUT, " -> SERIAL SESSION = %s\n", sinfo.flags & CKF_SERIAL_SESSION ? "TRUE" : "FALSE");
michael@0 459 #ifdef CKF_INSERTION_CALLBACK
michael@0 460 PR_fprintf(PR_STDOUT, " -> INSERTION CALLBACK = %s\n", sinfo.flags & CKF_INSERTION_CALLBACK ? "TRUE" : "FALSE");
michael@0 461 #endif /* CKF_INSERTION_CALLBACK */
michael@0 462 PR_fprintf(PR_STDOUT, " ulDeviceError = %lu\n", sinfo.ulDeviceError);
michael@0 463 PR_fprintf(PR_STDOUT, "\n");
michael@0 464
michael@0 465 ck_rv = epv->C_FindObjectsInit(h, (CK_ATTRIBUTE_PTR)CK_NULL_PTR, 0);
michael@0 466 if( CKR_OK != ck_rv ) {
michael@0 467 PR_fprintf(PR_STDOUT, "C_FindObjectsInit(%lu, NULL_PTR, 0) returned 0x%08x\n", h, ck_rv);
michael@0 468 return 1;
michael@0 469 }
michael@0 470
michael@0 471 pTemplate = (CK_ATTRIBUTE_PTR)PR_Calloc(number_of_all_known_attribute_types, sizeof(CK_ATTRIBUTE));
michael@0 472 if( (CK_ATTRIBUTE_PTR)NULL == pTemplate ) {
michael@0 473 PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n",
michael@0 474 number_of_all_known_attribute_types * sizeof(CK_ATTRIBUTE));
michael@0 475 return 1;
michael@0 476 }
michael@0 477
michael@0 478 PR_fprintf(PR_STDOUT, " All objects:\n");
michael@0 479
michael@0 480 while(1) {
michael@0 481 CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0;
michael@0 482 CK_ULONG nObjects = 0;
michael@0 483 CK_ULONG k;
michael@0 484 CK_ULONG nAttributes = 0;
michael@0 485 CK_ATTRIBUTE_PTR pT2;
michael@0 486 CK_ULONG l;
michael@0 487
michael@0 488 ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects);
michael@0 489 if( CKR_OK != ck_rv ) {
michael@0 490 PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv);
michael@0 491 return 1;
michael@0 492 }
michael@0 493
michael@0 494 if( 0 == nObjects ) {
michael@0 495 PR_fprintf(PR_STDOUT, "\n");
michael@0 496 break;
michael@0 497 }
michael@0 498
michael@0 499 tnObjects++;
michael@0 500
michael@0 501 PR_fprintf(PR_STDOUT, " OBJECT HANDLE %lu:\n", o);
michael@0 502
michael@0 503 for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
michael@0 504 pTemplate[k].type = all_known_attribute_types[k];
michael@0 505 pTemplate[k].pValue = (CK_VOID_PTR)CK_NULL_PTR;
michael@0 506 pTemplate[k].ulValueLen = 0;
michael@0 507 }
michael@0 508
michael@0 509 ck_rv = epv->C_GetAttributeValue(h, o, pTemplate, number_of_all_known_attribute_types);
michael@0 510 switch( ck_rv ) {
michael@0 511 case CKR_OK:
michael@0 512 case CKR_ATTRIBUTE_SENSITIVE:
michael@0 513 case CKR_ATTRIBUTE_TYPE_INVALID:
michael@0 514 case CKR_BUFFER_TOO_SMALL:
michael@0 515 break;
michael@0 516 default:
michael@0 517 PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {all attribute types}, %lu) returned 0x%08x\n",
michael@0 518 h, o, number_of_all_known_attribute_types, ck_rv);
michael@0 519 return 1;
michael@0 520 }
michael@0 521
michael@0 522 for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
michael@0 523 if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
michael@0 524 nAttributes++;
michael@0 525 }
michael@0 526 }
michael@0 527
michael@0 528 if( 1 ) {
michael@0 529 PR_fprintf(PR_STDOUT, " %lu attributes:\n", nAttributes);
michael@0 530 for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
michael@0 531 if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
michael@0 532 PR_fprintf(PR_STDOUT, " 0x%08x (len = %lu)\n", pTemplate[k].type,
michael@0 533 pTemplate[k].ulValueLen);
michael@0 534 }
michael@0 535 }
michael@0 536 PR_fprintf(PR_STDOUT, "\n");
michael@0 537 }
michael@0 538
michael@0 539 pT2 = (CK_ATTRIBUTE_PTR)PR_Calloc(nAttributes, sizeof(CK_ATTRIBUTE));
michael@0 540 if( (CK_ATTRIBUTE_PTR)NULL == pT2 ) {
michael@0 541 PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n",
michael@0 542 nAttributes * sizeof(CK_ATTRIBUTE));
michael@0 543 return 1;
michael@0 544 }
michael@0 545
michael@0 546 for( l = 0, k = 0; k < number_of_all_known_attribute_types; k++ ) {
michael@0 547 if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
michael@0 548 pT2[l].type = pTemplate[k].type;
michael@0 549 pT2[l].ulValueLen = pTemplate[k].ulValueLen;
michael@0 550 pT2[l].pValue = (CK_VOID_PTR)PR_Malloc(pT2[l].ulValueLen);
michael@0 551 if( (CK_VOID_PTR)NULL == pT2[l].pValue ) {
michael@0 552 PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", pT2[l].ulValueLen);
michael@0 553 return 1;
michael@0 554 }
michael@0 555 l++;
michael@0 556 }
michael@0 557 }
michael@0 558
michael@0 559 PR_ASSERT( l == nAttributes );
michael@0 560
michael@0 561 ck_rv = epv->C_GetAttributeValue(h, o, pT2, nAttributes);
michael@0 562 switch( ck_rv ) {
michael@0 563 case CKR_OK:
michael@0 564 case CKR_ATTRIBUTE_SENSITIVE:
michael@0 565 case CKR_ATTRIBUTE_TYPE_INVALID:
michael@0 566 case CKR_BUFFER_TOO_SMALL:
michael@0 567 break;
michael@0 568 default:
michael@0 569 PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {existent attribute types}, %lu) returned 0x%08x\n",
michael@0 570 h, o, nAttributes, ck_rv);
michael@0 571 return 1;
michael@0 572 }
michael@0 573
michael@0 574 for( l = 0; l < nAttributes; l++ ) {
michael@0 575 PR_fprintf(PR_STDOUT, " type = 0x%08x, len = %ld", pT2[l].type, (CK_LONG)pT2[l].ulValueLen);
michael@0 576 if( -1 == (CK_LONG)pT2[l].ulValueLen ) {
michael@0 577 ;
michael@0 578 } else {
michael@0 579 CK_ULONG m;
michael@0 580
michael@0 581 if( pT2[l].ulValueLen <= 8 ) {
michael@0 582 PR_fprintf(PR_STDOUT, ", value = ");
michael@0 583 } else {
michael@0 584 PR_fprintf(PR_STDOUT, ", value = \n ");
michael@0 585 }
michael@0 586
michael@0 587 for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) {
michael@0 588 PR_fprintf(PR_STDOUT, "%02x", (CK_ULONG)(0xff & ((CK_CHAR_PTR)pT2[l].pValue)[m]));
michael@0 589 }
michael@0 590
michael@0 591 PR_fprintf(PR_STDOUT, " ");
michael@0 592
michael@0 593 for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) {
michael@0 594 CK_CHAR c = ((CK_CHAR_PTR)pT2[l].pValue)[m];
michael@0 595 if( (c < 0x20) || (c >= 0x7f) ) {
michael@0 596 c = '.';
michael@0 597 }
michael@0 598 PR_fprintf(PR_STDOUT, "%c", c);
michael@0 599 }
michael@0 600 }
michael@0 601
michael@0 602 PR_fprintf(PR_STDOUT, "\n");
michael@0 603 }
michael@0 604
michael@0 605 PR_fprintf(PR_STDOUT, "\n");
michael@0 606
michael@0 607 for( l = 0; l < nAttributes; l++ ) {
michael@0 608 PR_Free(pT2[l].pValue);
michael@0 609 }
michael@0 610 PR_Free(pT2);
michael@0 611 } /* while(1) */
michael@0 612
michael@0 613 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 614 if( CKR_OK != ck_rv ) {
michael@0 615 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 616 return 1;
michael@0 617 }
michael@0 618
michael@0 619 PR_fprintf(PR_STDOUT, " (%lu objects total)\n", tnObjects);
michael@0 620
michael@0 621 ck_rv = epv->C_CloseSession(h);
michael@0 622 if( CKR_OK != ck_rv ) {
michael@0 623 PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 624 return 1;
michael@0 625 }
michael@0 626 } /* session to find objects */
michael@0 627
michael@0 628 /* session to create, find, and delete a couple session objects */
michael@0 629 {
michael@0 630 CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
michael@0 631 CK_ATTRIBUTE one[7], two[7], three[7], delta[1], mask[1];
michael@0 632 CK_OBJECT_CLASS cko_data = CKO_DATA;
michael@0 633 CK_BBOOL false = CK_FALSE, true = CK_TRUE;
michael@0 634 char *key = "TEST PROGRAM";
michael@0 635 CK_ULONG key_len = strlen(key);
michael@0 636 CK_OBJECT_HANDLE hOneIn = (CK_OBJECT_HANDLE)0, hTwoIn = (CK_OBJECT_HANDLE)0,
michael@0 637 hThreeIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0;
michael@0 638 CK_OBJECT_HANDLE found[10];
michael@0 639 CK_ULONG nFound;
michael@0 640
michael@0 641 ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
michael@0 642 if( CKR_OK != ck_rv ) {
michael@0 643 PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 644 return 1;
michael@0 645 }
michael@0 646
michael@0 647 PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h);
michael@0 648
michael@0 649 one[0].type = CKA_CLASS;
michael@0 650 one[0].pValue = &cko_data;
michael@0 651 one[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 652 one[1].type = CKA_TOKEN;
michael@0 653 one[1].pValue = &false;
michael@0 654 one[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 655 one[2].type = CKA_PRIVATE;
michael@0 656 one[2].pValue = &false;
michael@0 657 one[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 658 one[3].type = CKA_MODIFIABLE;
michael@0 659 one[3].pValue = &true;
michael@0 660 one[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 661 one[4].type = CKA_LABEL;
michael@0 662 one[4].pValue = "Test data object one";
michael@0 663 one[4].ulValueLen = strlen(one[4].pValue);
michael@0 664 one[5].type = CKA_APPLICATION;
michael@0 665 one[5].pValue = key;
michael@0 666 one[5].ulValueLen = key_len;
michael@0 667 one[6].type = CKA_VALUE;
michael@0 668 one[6].pValue = "Object one";
michael@0 669 one[6].ulValueLen = strlen(one[6].pValue);
michael@0 670
michael@0 671 two[0].type = CKA_CLASS;
michael@0 672 two[0].pValue = &cko_data;
michael@0 673 two[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 674 two[1].type = CKA_TOKEN;
michael@0 675 two[1].pValue = &false;
michael@0 676 two[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 677 two[2].type = CKA_PRIVATE;
michael@0 678 two[2].pValue = &false;
michael@0 679 two[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 680 two[3].type = CKA_MODIFIABLE;
michael@0 681 two[3].pValue = &true;
michael@0 682 two[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 683 two[4].type = CKA_LABEL;
michael@0 684 two[4].pValue = "Test data object two";
michael@0 685 two[4].ulValueLen = strlen(two[4].pValue);
michael@0 686 two[5].type = CKA_APPLICATION;
michael@0 687 two[5].pValue = key;
michael@0 688 two[5].ulValueLen = key_len;
michael@0 689 two[6].type = CKA_VALUE;
michael@0 690 two[6].pValue = "Object two";
michael@0 691 two[6].ulValueLen = strlen(two[6].pValue);
michael@0 692
michael@0 693 three[0].type = CKA_CLASS;
michael@0 694 three[0].pValue = &cko_data;
michael@0 695 three[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 696 three[1].type = CKA_TOKEN;
michael@0 697 three[1].pValue = &false;
michael@0 698 three[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 699 three[2].type = CKA_PRIVATE;
michael@0 700 three[2].pValue = &false;
michael@0 701 three[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 702 three[3].type = CKA_MODIFIABLE;
michael@0 703 three[3].pValue = &true;
michael@0 704 three[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 705 three[4].type = CKA_LABEL;
michael@0 706 three[4].pValue = "Test data object three";
michael@0 707 three[4].ulValueLen = strlen(three[4].pValue);
michael@0 708 three[5].type = CKA_APPLICATION;
michael@0 709 three[5].pValue = key;
michael@0 710 three[5].ulValueLen = key_len;
michael@0 711 three[6].type = CKA_VALUE;
michael@0 712 three[6].pValue = "Object three";
michael@0 713 three[6].ulValueLen = strlen(three[6].pValue);
michael@0 714
michael@0 715 ck_rv = epv->C_CreateObject(h, one, 7, &hOneIn);
michael@0 716 if( CKR_OK != ck_rv ) {
michael@0 717 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, one, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 718 return 1;
michael@0 719 }
michael@0 720
michael@0 721 PR_fprintf(PR_STDOUT, " Created object one: handle = %lu\n", hOneIn);
michael@0 722
michael@0 723 ck_rv = epv->C_CreateObject(h, two, 7, &hTwoIn);
michael@0 724 if( CKR_OK != ck_rv ) {
michael@0 725 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, two, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 726 return 1;
michael@0 727 }
michael@0 728
michael@0 729 PR_fprintf(PR_STDOUT, " Created object two: handle = %lu\n", hTwoIn);
michael@0 730
michael@0 731 ck_rv = epv->C_CreateObject(h, three, 7, &hThreeIn);
michael@0 732 if( CKR_OK != ck_rv ) {
michael@0 733 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, three, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 734 return 1;
michael@0 735 }
michael@0 736
michael@0 737 PR_fprintf(PR_STDOUT, " Created object three: handle = %lu\n", hThreeIn);
michael@0 738
michael@0 739 delta[0].type = CKA_VALUE;
michael@0 740 delta[0].pValue = "Copied object";
michael@0 741 delta[0].ulValueLen = strlen(delta[0].pValue);
michael@0 742
michael@0 743 ck_rv = epv->C_CopyObject(h, hThreeIn, delta, 1, &hDeltaIn);
michael@0 744 if( CKR_OK != ck_rv ) {
michael@0 745 PR_fprintf(PR_STDERR, "C_CopyObject(%lu, %lu, delta, 1, ) returned 0x%08x\n",
michael@0 746 h, hThreeIn, ck_rv);
michael@0 747 return 1;
michael@0 748 }
michael@0 749
michael@0 750 PR_fprintf(PR_STDOUT, " Copied object three: new handle = %lu\n", hDeltaIn);
michael@0 751
michael@0 752 mask[0].type = CKA_APPLICATION;
michael@0 753 mask[0].pValue = key;
michael@0 754 mask[0].ulValueLen = key_len;
michael@0 755
michael@0 756 ck_rv = epv->C_FindObjectsInit(h, mask, 1);
michael@0 757 if( CKR_OK != ck_rv ) {
michael@0 758 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n",
michael@0 759 h, ck_rv);
michael@0 760 return 1;
michael@0 761 }
michael@0 762
michael@0 763 (void)memset(&found, 0, sizeof(found));
michael@0 764 nFound = 0;
michael@0 765 ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
michael@0 766 if( CKR_OK != ck_rv ) {
michael@0 767 PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n",
michael@0 768 h, ck_rv);
michael@0 769 return 1;
michael@0 770 }
michael@0 771
michael@0 772 if( 4 != nFound ) {
michael@0 773 PR_fprintf(PR_STDERR, "Found %lu objects, not 4.\n", nFound);
michael@0 774 return 1;
michael@0 775 }
michael@0 776
michael@0 777 PR_fprintf(PR_STDOUT, " Found 4 objects: %lu, %lu, %lu, %lu\n",
michael@0 778 found[0], found[1], found[2], found[3]);
michael@0 779
michael@0 780 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 781 if( CKR_OK != ck_rv ) {
michael@0 782 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 783 return 1;
michael@0 784 }
michael@0 785
michael@0 786 ck_rv = epv->C_DestroyObject(h, hThreeIn);
michael@0 787 if( CKR_OK != ck_rv ) {
michael@0 788 PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, hThreeIn, ck_rv);
michael@0 789 return 1;
michael@0 790 }
michael@0 791
michael@0 792 PR_fprintf(PR_STDOUT, " Destroyed object three (handle = %lu)\n", hThreeIn);
michael@0 793
michael@0 794 delta[0].type = CKA_APPLICATION;
michael@0 795 delta[0].pValue = "Changed application";
michael@0 796 delta[0].ulValueLen = strlen(delta[0].pValue);
michael@0 797
michael@0 798 ck_rv = epv->C_SetAttributeValue(h, hTwoIn, delta, 1);
michael@0 799 if( CKR_OK != ck_rv ) {
michael@0 800 PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n",
michael@0 801 h, hTwoIn, ck_rv);
michael@0 802 return 1;
michael@0 803 }
michael@0 804
michael@0 805 PR_fprintf(PR_STDOUT, " Changed object two (handle = %lu).\n", hTwoIn);
michael@0 806
michael@0 807 /* Can another session find these session objects? */
michael@0 808 {
michael@0 809 CK_SESSION_HANDLE h2 = (CK_SESSION_HANDLE)0;
michael@0 810
michael@0 811 ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h2);
michael@0 812 if( CKR_OK != ck_rv ) {
michael@0 813 PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 814 return 1;
michael@0 815 }
michael@0 816
michael@0 817 PR_fprintf(PR_STDOUT, " Opened a second session: handle = 0x%08x\n", h2);
michael@0 818
michael@0 819 /* mask is still the same */
michael@0 820
michael@0 821 ck_rv = epv->C_FindObjectsInit(h2, mask, 1);
michael@0 822 if( CKR_OK != ck_rv ) {
michael@0 823 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n",
michael@0 824 h2, ck_rv);
michael@0 825 return 1;
michael@0 826 }
michael@0 827
michael@0 828 (void)memset(&found, 0, sizeof(found));
michael@0 829 nFound = 0;
michael@0 830 ck_rv = epv->C_FindObjects(h2, found, 10, &nFound);
michael@0 831 if( CKR_OK != ck_rv ) {
michael@0 832 PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n",
michael@0 833 h2, ck_rv);
michael@0 834 return 1;
michael@0 835 }
michael@0 836
michael@0 837 if( 2 != nFound ) {
michael@0 838 PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
michael@0 839 return 1;
michael@0 840 }
michael@0 841
michael@0 842 PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n",
michael@0 843 found[0], found[1]);
michael@0 844
michael@0 845 ck_rv = epv->C_FindObjectsFinal(h2);
michael@0 846 if( CKR_OK != ck_rv ) {
michael@0 847 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h2, ck_rv);
michael@0 848 return 1;
michael@0 849 }
michael@0 850
michael@0 851 /* Leave the session hanging open, we'll CloseAllSessions later */
michael@0 852 } /* Can another session find these session objects? */
michael@0 853
michael@0 854 ck_rv = epv->C_CloseAllSessions(pSlots[i]);
michael@0 855 if( CKR_OK != ck_rv ) {
michael@0 856 PR_fprintf(PR_STDERR, "C_CloseAllSessions(%lu) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 857 return 1;
michael@0 858 }
michael@0 859 } /* session to create, find, and delete a couple session objects */
michael@0 860
michael@0 861 /* Might be interesting to do a find here to verify that all session objects are gone. */
michael@0 862
michael@0 863 if( tinfo.flags & CKF_WRITE_PROTECTED ) {
michael@0 864 PR_fprintf(PR_STDOUT, "Token is write protected, skipping token-object tests.\n");
michael@0 865 } else {
michael@0 866 CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
michael@0 867 CK_ATTRIBUTE tobj[7], tsobj[7], stobj[7], delta[1], mask[2];
michael@0 868 CK_OBJECT_CLASS cko_data = CKO_DATA;
michael@0 869 CK_BBOOL false = CK_FALSE, true = CK_TRUE;
michael@0 870 char *key = "TEST PROGRAM";
michael@0 871 CK_ULONG key_len = strlen(key);
michael@0 872 CK_OBJECT_HANDLE hTIn = (CK_OBJECT_HANDLE)0, hTSIn = (CK_OBJECT_HANDLE)0,
michael@0 873 hSTIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0;
michael@0 874 CK_OBJECT_HANDLE found[10];
michael@0 875 CK_ULONG nFound;
michael@0 876
michael@0 877 ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
michael@0 878 if( CKR_OK != ck_rv ) {
michael@0 879 PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 880 return 1;
michael@0 881 }
michael@0 882
michael@0 883 PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h);
michael@0 884
michael@0 885 tobj[0].type = CKA_CLASS;
michael@0 886 tobj[0].pValue = &cko_data;
michael@0 887 tobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 888 tobj[1].type = CKA_TOKEN;
michael@0 889 tobj[1].pValue = &true;
michael@0 890 tobj[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 891 tobj[2].type = CKA_PRIVATE;
michael@0 892 tobj[2].pValue = &false;
michael@0 893 tobj[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 894 tobj[3].type = CKA_MODIFIABLE;
michael@0 895 tobj[3].pValue = &true;
michael@0 896 tobj[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 897 tobj[4].type = CKA_LABEL;
michael@0 898 tobj[4].pValue = "Test data object token";
michael@0 899 tobj[4].ulValueLen = strlen(tobj[4].pValue);
michael@0 900 tobj[5].type = CKA_APPLICATION;
michael@0 901 tobj[5].pValue = key;
michael@0 902 tobj[5].ulValueLen = key_len;
michael@0 903 tobj[6].type = CKA_VALUE;
michael@0 904 tobj[6].pValue = "Object token";
michael@0 905 tobj[6].ulValueLen = strlen(tobj[6].pValue);
michael@0 906
michael@0 907 tsobj[0].type = CKA_CLASS;
michael@0 908 tsobj[0].pValue = &cko_data;
michael@0 909 tsobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 910 tsobj[1].type = CKA_TOKEN;
michael@0 911 tsobj[1].pValue = &true;
michael@0 912 tsobj[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 913 tsobj[2].type = CKA_PRIVATE;
michael@0 914 tsobj[2].pValue = &false;
michael@0 915 tsobj[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 916 tsobj[3].type = CKA_MODIFIABLE;
michael@0 917 tsobj[3].pValue = &true;
michael@0 918 tsobj[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 919 tsobj[4].type = CKA_LABEL;
michael@0 920 tsobj[4].pValue = "Test data object token->session";
michael@0 921 tsobj[4].ulValueLen = strlen(tsobj[4].pValue);
michael@0 922 tsobj[5].type = CKA_APPLICATION;
michael@0 923 tsobj[5].pValue = key;
michael@0 924 tsobj[5].ulValueLen = key_len;
michael@0 925 tsobj[6].type = CKA_VALUE;
michael@0 926 tsobj[6].pValue = "Object token->session";
michael@0 927 tsobj[6].ulValueLen = strlen(tsobj[6].pValue);
michael@0 928
michael@0 929 stobj[0].type = CKA_CLASS;
michael@0 930 stobj[0].pValue = &cko_data;
michael@0 931 stobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 932 stobj[1].type = CKA_TOKEN;
michael@0 933 stobj[1].pValue = &false;
michael@0 934 stobj[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 935 stobj[2].type = CKA_PRIVATE;
michael@0 936 stobj[2].pValue = &false;
michael@0 937 stobj[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 938 stobj[3].type = CKA_MODIFIABLE;
michael@0 939 stobj[3].pValue = &true;
michael@0 940 stobj[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 941 stobj[4].type = CKA_LABEL;
michael@0 942 stobj[4].pValue = "Test data object session->token";
michael@0 943 stobj[4].ulValueLen = strlen(stobj[4].pValue);
michael@0 944 stobj[5].type = CKA_APPLICATION;
michael@0 945 stobj[5].pValue = key;
michael@0 946 stobj[5].ulValueLen = key_len;
michael@0 947 stobj[6].type = CKA_VALUE;
michael@0 948 stobj[6].pValue = "Object session->token";
michael@0 949 stobj[6].ulValueLen = strlen(stobj[6].pValue);
michael@0 950
michael@0 951 ck_rv = epv->C_CreateObject(h, tobj, 7, &hTIn);
michael@0 952 if( CKR_OK != ck_rv ) {
michael@0 953 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 954 return 1;
michael@0 955 }
michael@0 956
michael@0 957 PR_fprintf(PR_STDOUT, " Created object token: handle = %lu\n", hTIn);
michael@0 958
michael@0 959 ck_rv = epv->C_CreateObject(h, tsobj, 7, &hTSIn);
michael@0 960 if( CKR_OK != ck_rv ) {
michael@0 961 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 962 return 1;
michael@0 963 }
michael@0 964
michael@0 965 PR_fprintf(PR_STDOUT, " Created object token->session: handle = %lu\n", hTSIn);
michael@0 966 ck_rv = epv->C_CreateObject(h, stobj, 7, &hSTIn);
michael@0 967 if( CKR_OK != ck_rv ) {
michael@0 968 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 969 return 1;
michael@0 970 }
michael@0 971
michael@0 972 PR_fprintf(PR_STDOUT, " Created object session->token: handle = %lu\n", hSTIn);
michael@0 973
michael@0 974 /* I've created two token objects and one session object; find the two */
michael@0 975
michael@0 976 mask[0].type = CKA_APPLICATION;
michael@0 977 mask[0].pValue = key;
michael@0 978 mask[0].ulValueLen = key_len;
michael@0 979 mask[1].type = CKA_TOKEN;
michael@0 980 mask[1].pValue = &true;
michael@0 981 mask[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 982
michael@0 983 ck_rv = epv->C_FindObjectsInit(h, mask, 2);
michael@0 984 if( CKR_OK != ck_rv ) {
michael@0 985 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n",
michael@0 986 h, ck_rv);
michael@0 987 return 1;
michael@0 988 }
michael@0 989
michael@0 990 (void)memset(&found, 0, sizeof(found));
michael@0 991 nFound = 0;
michael@0 992 ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
michael@0 993 if( CKR_OK != ck_rv ) {
michael@0 994 PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n",
michael@0 995 h, ck_rv);
michael@0 996 return 1;
michael@0 997 }
michael@0 998
michael@0 999 if( 2 != nFound ) {
michael@0 1000 PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
michael@0 1001 return 1;
michael@0 1002 }
michael@0 1003
michael@0 1004 PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n",
michael@0 1005 found[0], found[1]);
michael@0 1006
michael@0 1007 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 1008 if( CKR_OK != ck_rv ) {
michael@0 1009 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 1010 return 1;
michael@0 1011 }
michael@0 1012
michael@0 1013 /* Convert a token to session object */
michael@0 1014
michael@0 1015 delta[0].type = CKA_TOKEN;
michael@0 1016 delta[0].pValue = &false;
michael@0 1017 delta[0].ulValueLen = sizeof(CK_BBOOL);
michael@0 1018
michael@0 1019 ck_rv = epv->C_SetAttributeValue(h, hTSIn, delta, 1);
michael@0 1020 if( CKR_OK != ck_rv ) {
michael@0 1021 PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n",
michael@0 1022 h, hTSIn, ck_rv);
michael@0 1023 return 1;
michael@0 1024 }
michael@0 1025
michael@0 1026 PR_fprintf(PR_STDOUT, " Changed object from token to session (handle = %lu).\n", hTSIn);
michael@0 1027
michael@0 1028 /* Now find again; there should be one */
michael@0 1029
michael@0 1030 mask[0].type = CKA_APPLICATION;
michael@0 1031 mask[0].pValue = key;
michael@0 1032 mask[0].ulValueLen = key_len;
michael@0 1033 mask[1].type = CKA_TOKEN;
michael@0 1034 mask[1].pValue = &true;
michael@0 1035 mask[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 1036
michael@0 1037 ck_rv = epv->C_FindObjectsInit(h, mask, 2);
michael@0 1038 if( CKR_OK != ck_rv ) {
michael@0 1039 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n",
michael@0 1040 h, ck_rv);
michael@0 1041 return 1;
michael@0 1042 }
michael@0 1043
michael@0 1044 (void)memset(&found, 0, sizeof(found));
michael@0 1045 nFound = 0;
michael@0 1046 ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
michael@0 1047 if( CKR_OK != ck_rv ) {
michael@0 1048 PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n",
michael@0 1049 h, ck_rv);
michael@0 1050 return 1;
michael@0 1051 }
michael@0 1052
michael@0 1053 if( 1 != nFound ) {
michael@0 1054 PR_fprintf(PR_STDERR, "Found %lu objects, not 1.\n", nFound);
michael@0 1055 return 1;
michael@0 1056 }
michael@0 1057
michael@0 1058 PR_fprintf(PR_STDOUT, " Found 1 objects: %lu\n",
michael@0 1059 found[0]);
michael@0 1060
michael@0 1061 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 1062 if( CKR_OK != ck_rv ) {
michael@0 1063 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 1064 return 1;
michael@0 1065 }
michael@0 1066
michael@0 1067 /* Convert a session to a token object */
michael@0 1068
michael@0 1069 delta[0].type = CKA_TOKEN;
michael@0 1070 delta[0].pValue = &true;
michael@0 1071 delta[0].ulValueLen = sizeof(CK_BBOOL);
michael@0 1072
michael@0 1073 ck_rv = epv->C_SetAttributeValue(h, hSTIn, delta, 1);
michael@0 1074 if( CKR_OK != ck_rv ) {
michael@0 1075 PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n",
michael@0 1076 h, hSTIn, ck_rv);
michael@0 1077 return 1;
michael@0 1078 }
michael@0 1079
michael@0 1080 PR_fprintf(PR_STDOUT, " Changed object from session to token (handle = %lu).\n", hSTIn);
michael@0 1081
michael@0 1082 /* Now find again; there should be two again */
michael@0 1083
michael@0 1084 mask[0].type = CKA_APPLICATION;
michael@0 1085 mask[0].pValue = key;
michael@0 1086 mask[0].ulValueLen = key_len;
michael@0 1087 mask[1].type = CKA_TOKEN;
michael@0 1088 mask[1].pValue = &true;
michael@0 1089 mask[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 1090
michael@0 1091 ck_rv = epv->C_FindObjectsInit(h, mask, 2);
michael@0 1092 if( CKR_OK != ck_rv ) {
michael@0 1093 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n",
michael@0 1094 h, ck_rv);
michael@0 1095 return 1;
michael@0 1096 }
michael@0 1097
michael@0 1098 (void)memset(&found, 0, sizeof(found));
michael@0 1099 nFound = 0;
michael@0 1100 ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
michael@0 1101 if( CKR_OK != ck_rv ) {
michael@0 1102 PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n",
michael@0 1103 h, ck_rv);
michael@0 1104 return 1;
michael@0 1105 }
michael@0 1106
michael@0 1107 if( 2 != nFound ) {
michael@0 1108 PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
michael@0 1109 return 1;
michael@0 1110 }
michael@0 1111
michael@0 1112 PR_fprintf(PR_STDOUT, " Found 2 objects: %lu, %lu\n",
michael@0 1113 found[0], found[1]);
michael@0 1114
michael@0 1115 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 1116 if( CKR_OK != ck_rv ) {
michael@0 1117 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 1118 return 1;
michael@0 1119 }
michael@0 1120
michael@0 1121 /* Delete the two (found) token objects to clean up */
michael@0 1122
michael@0 1123 ck_rv = epv->C_DestroyObject(h, found[0]);
michael@0 1124 if( CKR_OK != ck_rv ) {
michael@0 1125 PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[0], ck_rv);
michael@0 1126 return 1;
michael@0 1127 }
michael@0 1128
michael@0 1129 PR_fprintf(PR_STDOUT, " Destroyed token object (handle = %lu)\n", found[0]);
michael@0 1130
michael@0 1131 ck_rv = epv->C_DestroyObject(h, found[1]);
michael@0 1132 if( CKR_OK != ck_rv ) {
michael@0 1133 PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[1], ck_rv);
michael@0 1134 return 1;
michael@0 1135 }
michael@0 1136
michael@0 1137 PR_fprintf(PR_STDOUT, " Destroyed token object (handle = %lu)\n", found[1]);
michael@0 1138
michael@0 1139 /* Close the session and all objects should be gone */
michael@0 1140
michael@0 1141 ck_rv = epv->C_CloseSession(h);
michael@0 1142 if( CKR_OK != ck_rv ) {
michael@0 1143 PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 1144 return 1;
michael@0 1145 }
michael@0 1146 } /* if( tinfo.flags & CKF_WRITE_PROTECTED ) */
michael@0 1147
michael@0 1148 if( tinfo.flags & CKF_WRITE_PROTECTED ) {
michael@0 1149 PR_fprintf(PR_STDOUT, "Token is write protected, skipping leaving a record.\n");
michael@0 1150 } else {
michael@0 1151 CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
michael@0 1152 CK_ATTRIBUTE record[7], mask[2];
michael@0 1153 CK_OBJECT_CLASS cko_data = CKO_DATA;
michael@0 1154 CK_BBOOL false = CK_FALSE, true = CK_TRUE;
michael@0 1155 char *key = "TEST RECORD";
michael@0 1156 CK_ULONG key_len = strlen(key);
michael@0 1157 CK_OBJECT_HANDLE hin = (CK_OBJECT_HANDLE)0;
michael@0 1158 char timebuffer[256];
michael@0 1159
michael@0 1160 ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
michael@0 1161 if( CKR_OK != ck_rv ) {
michael@0 1162 PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
michael@0 1163 return 1;
michael@0 1164 }
michael@0 1165
michael@0 1166 PR_fprintf(PR_STDOUT, " Opened a session: handle = 0x%08x\n", h);
michael@0 1167
michael@0 1168 /* I can't believe how hard NSPR makes this operation */
michael@0 1169 {
michael@0 1170 time_t now = 0;
michael@0 1171 struct tm *tm;
michael@0 1172 time(&now);
michael@0 1173 tm = localtime(&now);
michael@0 1174 strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %T %Z", tm);
michael@0 1175 }
michael@0 1176
michael@0 1177 record[0].type = CKA_CLASS;
michael@0 1178 record[0].pValue = &cko_data;
michael@0 1179 record[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 1180 record[1].type = CKA_TOKEN;
michael@0 1181 record[1].pValue = &true;
michael@0 1182 record[1].ulValueLen = sizeof(CK_BBOOL);
michael@0 1183 record[2].type = CKA_PRIVATE;
michael@0 1184 record[2].pValue = &false;
michael@0 1185 record[2].ulValueLen = sizeof(CK_BBOOL);
michael@0 1186 record[3].type = CKA_MODIFIABLE;
michael@0 1187 record[3].pValue = &true;
michael@0 1188 record[3].ulValueLen = sizeof(CK_BBOOL);
michael@0 1189 record[4].type = CKA_LABEL;
michael@0 1190 record[4].pValue = "Test record";
michael@0 1191 record[4].ulValueLen = strlen(record[4].pValue);
michael@0 1192 record[5].type = CKA_APPLICATION;
michael@0 1193 record[5].pValue = key;
michael@0 1194 record[5].ulValueLen = key_len;
michael@0 1195 record[6].type = CKA_VALUE;
michael@0 1196 record[6].pValue = timebuffer;
michael@0 1197 record[6].ulValueLen = strlen(timebuffer)+1;
michael@0 1198
michael@0 1199 PR_fprintf(PR_STDOUT, " Timestamping with \"%s\"\n", timebuffer);
michael@0 1200
michael@0 1201 ck_rv = epv->C_CreateObject(h, record, 7, &hin);
michael@0 1202 if( CKR_OK != ck_rv ) {
michael@0 1203 PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
michael@0 1204 return 1;
michael@0 1205 }
michael@0 1206
michael@0 1207 PR_fprintf(PR_STDOUT, " Created record object: handle = %lu\n", hin);
michael@0 1208
michael@0 1209 PR_fprintf(PR_STDOUT, " == All test timestamps ==\n");
michael@0 1210
michael@0 1211 mask[0].type = CKA_CLASS;
michael@0 1212 mask[0].pValue = &cko_data;
michael@0 1213 mask[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
michael@0 1214 mask[1].type = CKA_APPLICATION;
michael@0 1215 mask[1].pValue = key;
michael@0 1216 mask[1].ulValueLen = key_len;
michael@0 1217
michael@0 1218 ck_rv = epv->C_FindObjectsInit(h, mask, 2);
michael@0 1219 if( CKR_OK != ck_rv ) {
michael@0 1220 PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n",
michael@0 1221 h, ck_rv);
michael@0 1222 return 1;
michael@0 1223 }
michael@0 1224
michael@0 1225 while( 1 ) {
michael@0 1226 CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0;
michael@0 1227 CK_ULONG nObjects = 0;
michael@0 1228 CK_ATTRIBUTE value[1];
michael@0 1229 char buffer[1024];
michael@0 1230
michael@0 1231 ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects);
michael@0 1232 if( CKR_OK != ck_rv ) {
michael@0 1233 PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv);
michael@0 1234 return 1;
michael@0 1235 }
michael@0 1236
michael@0 1237 if( 0 == nObjects ) {
michael@0 1238 PR_fprintf(PR_STDOUT, "\n");
michael@0 1239 break;
michael@0 1240 }
michael@0 1241
michael@0 1242 value[0].type = CKA_VALUE;
michael@0 1243 value[0].pValue = buffer;
michael@0 1244 value[0].ulValueLen = sizeof(buffer);
michael@0 1245
michael@0 1246 ck_rv = epv->C_GetAttributeValue(h, o, value, 1);
michael@0 1247 switch( ck_rv ) {
michael@0 1248 case CKR_OK:
michael@0 1249 PR_fprintf(PR_STDOUT, " %s\n", value[0].pValue);
michael@0 1250 break;
michael@0 1251 case CKR_ATTRIBUTE_SENSITIVE:
michael@0 1252 PR_fprintf(PR_STDOUT, " [Sensitive???]\n");
michael@0 1253 break;
michael@0 1254 case CKR_ATTRIBUTE_TYPE_INVALID:
michael@0 1255 PR_fprintf(PR_STDOUT, " [Invalid attribute???]\n");
michael@0 1256 break;
michael@0 1257 case CKR_BUFFER_TOO_SMALL:
michael@0 1258 PR_fprintf(PR_STDOUT, " (result > 1k (%lu))\n", value[0].ulValueLen);
michael@0 1259 break;
michael@0 1260 default:
michael@0 1261 PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, CKA_VALUE, 1) returned 0x%08x\n",
michael@0 1262 h, o);
michael@0 1263 return 1;
michael@0 1264 }
michael@0 1265 } /* while */
michael@0 1266
michael@0 1267 ck_rv = epv->C_FindObjectsFinal(h);
michael@0 1268 if( CKR_OK != ck_rv ) {
michael@0 1269 PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
michael@0 1270 return 1;
michael@0 1271 }
michael@0 1272 } /* "leaving a record" else clause */
michael@0 1273
michael@0 1274 }
michael@0 1275
michael@0 1276 PR_fprintf(PR_STDOUT, "\n");
michael@0 1277 }
michael@0 1278
michael@0 1279 return 0;
michael@0 1280 }

mercurial