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

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * This is a very trivial program I wrote for testing out a 
Packit 40b132
 * couple data-only Cryptoki modules for NSS.  It's not a "real"
Packit 40b132
 * test program that prints out nice "PASS" or "FAIL" messages;
Packit 40b132
 * it just makes calls and dumps data.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "config.h"
Packit 40b132
Packit 40b132
#ifdef HAVE_NSPR_H
Packit 40b132
#include "nspr.h"
Packit 40b132
#else
Packit 40b132
#error "NSPR is required."
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#ifdef WITH_NSS
Packit 40b132
#define FGMR 1
Packit 40b132
#include "ck.h"
Packit 40b132
#else
Packit 40b132
#include "pkcs11t.h"
Packit 40b132
#include "pkcs11.h"
Packit 40b132
#endif
Packit 40b132
Packit 40b132
/* The RSA versions are sloppier with namespaces */
Packit 40b132
#ifndef CK_TRUE
Packit 40b132
#define CK_TRUE TRUE
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#ifndef CK_FALSE
Packit 40b132
#define CK_FALSE FALSE
Packit 40b132
#endif
Packit 40b132
Packit 40b132
int
Packit 40b132
rmain
Packit 40b132
(
Packit 40b132
  int argc,
Packit 40b132
  char *argv[]
Packit 40b132
);
Packit 40b132
Packit 40b132
int
Packit 40b132
main
Packit 40b132
(
Packit 40b132
  int argc,
Packit 40b132
  char *argv[]
Packit 40b132
)
Packit 40b132
{
Packit 40b132
  int rv = 0;
Packit 40b132
Packit 40b132
  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 14);
Packit 40b132
  rv = rmain(argc, argv);
Packit 40b132
  PR_Cleanup();
Packit 40b132
Packit 40b132
  return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
static CK_ATTRIBUTE_TYPE all_known_attribute_types[] = {
Packit 40b132
  CKA_CLASS,
Packit 40b132
  CKA_TOKEN,
Packit 40b132
  CKA_PRIVATE,
Packit 40b132
  CKA_LABEL,
Packit 40b132
  CKA_APPLICATION,
Packit 40b132
  CKA_VALUE,
Packit 40b132
  CKA_CERTIFICATE_TYPE,
Packit 40b132
  CKA_ISSUER,
Packit 40b132
  CKA_SERIAL_NUMBER,
Packit 40b132
  CKA_KEY_TYPE,
Packit 40b132
  CKA_SUBJECT,
Packit 40b132
  CKA_ID,
Packit 40b132
  CKA_SENSITIVE,
Packit 40b132
  CKA_ENCRYPT,
Packit 40b132
  CKA_DECRYPT,
Packit 40b132
  CKA_WRAP,
Packit 40b132
  CKA_UNWRAP,
Packit 40b132
  CKA_SIGN,
Packit 40b132
  CKA_SIGN_RECOVER,
Packit 40b132
  CKA_VERIFY,
Packit 40b132
  CKA_VERIFY_RECOVER,
Packit 40b132
  CKA_DERIVE,
Packit 40b132
  CKA_START_DATE,
Packit 40b132
  CKA_END_DATE,
Packit 40b132
  CKA_MODULUS,
Packit 40b132
  CKA_MODULUS_BITS,
Packit 40b132
  CKA_PUBLIC_EXPONENT,
Packit 40b132
  CKA_PRIVATE_EXPONENT,
Packit 40b132
  CKA_PRIME_1,
Packit 40b132
  CKA_PRIME_2,
Packit 40b132
  CKA_EXPONENT_1,
Packit 40b132
  CKA_EXPONENT_2,
Packit 40b132
  CKA_COEFFICIENT,
Packit 40b132
  CKA_PRIME,
Packit 40b132
  CKA_SUBPRIME,
Packit 40b132
  CKA_BASE,
Packit 40b132
  CKA_VALUE_BITS,
Packit 40b132
  CKA_VALUE_LEN,
Packit 40b132
  CKA_EXTRACTABLE,
Packit 40b132
  CKA_LOCAL,
Packit 40b132
  CKA_NEVER_EXTRACTABLE,
Packit 40b132
  CKA_ALWAYS_SENSITIVE,
Packit 40b132
  CKA_MODIFIABLE,
Packit 40b132
#ifdef CKA_NETSCAPE
Packit 40b132
  CKA_NETSCAPE_URL,
Packit 40b132
  CKA_NETSCAPE_EMAIL,
Packit 40b132
  CKA_NETSCAPE_SMIME_INFO,
Packit 40b132
  CKA_NETSCAPE_SMIME_TIMESTAMP,
Packit 40b132
  CKA_NETSCAPE_PKCS8_SALT,
Packit 40b132
  CKA_NETSCAPE_PASSWORD_CHECK,
Packit 40b132
  CKA_NETSCAPE_EXPIRES,
Packit 40b132
#endif /* CKA_NETSCAPE */
Packit 40b132
#ifdef CKA_TRUST
Packit 40b132
  CKA_TRUST_DIGITAL_SIGNATURE,
Packit 40b132
  CKA_TRUST_NON_REPUDIATION,
Packit 40b132
  CKA_TRUST_KEY_ENCIPHERMENT,
Packit 40b132
  CKA_TRUST_DATA_ENCIPHERMENT,
Packit 40b132
  CKA_TRUST_KEY_AGREEMENT,
Packit 40b132
  CKA_TRUST_KEY_CERT_SIGN,
Packit 40b132
  CKA_TRUST_CRL_SIGN,
Packit 40b132
  CKA_TRUST_SERVER_AUTH,
Packit 40b132
  CKA_TRUST_CLIENT_AUTH,
Packit 40b132
  CKA_TRUST_CODE_SIGNING,
Packit 40b132
  CKA_TRUST_EMAIL_PROTECTION,
Packit 40b132
  CKA_TRUST_IPSEC_END_SYSTEM,
Packit 40b132
  CKA_TRUST_IPSEC_TUNNEL,
Packit 40b132
  CKA_TRUST_IPSEC_USER,
Packit 40b132
  CKA_TRUST_TIME_STAMPING,
Packit 40b132
#endif /* CKA_TRUST */
Packit 40b132
};
Packit 40b132
Packit 40b132
static number_of_all_known_attribute_types = 
Packit 40b132
  (sizeof(all_known_attribute_types)/sizeof(all_known_attribute_types[0]));
Packit 40b132
Packit 40b132
int
Packit 40b132
usage
Packit 40b132
(
Packit 40b132
  char *argv0
Packit 40b132
)
Packit 40b132
{
Packit 40b132
  PR_fprintf(PR_STDERR, "Usage: %s [-i {string|--}] <library>.so\n", argv0);
Packit 40b132
  return 1;
Packit 40b132
}
Packit 40b132
Packit 40b132
int
Packit 40b132
rmain
Packit 40b132
(
Packit 40b132
  int argc,
Packit 40b132
  char *argv[]
Packit 40b132
)
Packit 40b132
{
Packit 40b132
  char *argv0 = argv[0];
Packit 40b132
  PRLibrary *lib;
Packit 40b132
  CK_C_GetFunctionList gfl;
Packit 40b132
  CK_FUNCTION_LIST_PTR epv = (CK_FUNCTION_LIST_PTR)NULL;
Packit 40b132
  CK_RV ck_rv;
Packit 40b132
  CK_INFO info;
Packit 40b132
  CK_ULONG nSlots;
Packit 40b132
  CK_SLOT_ID *pSlots;
Packit 40b132
  CK_ULONG i;
Packit 40b132
  CK_C_INITIALIZE_ARGS ia, *iap;
Packit 40b132
Packit 40b132
  (void)memset(&ia, 0, sizeof(CK_C_INITIALIZE_ARGS));
Packit 40b132
  iap = (CK_C_INITIALIZE_ARGS *)NULL;
Packit 40b132
  while( argv++, --argc ) {
Packit 40b132
    if( '-' == argv[0][0] ) {
Packit 40b132
      switch( argv[0][1] ) {
Packit 40b132
      case 'i':
Packit 40b132
        iap = &ia;
Packit 40b132
        if( ((char *)NULL != argv[1]) && ('-' != argv[1][0]) ) {
Packit 40b132
#ifdef WITH_NSS
Packit 40b132
          ia.pConfig = argv[1];
Packit 40b132
          ia.ulConfigLen = strlen(argv[1]);
Packit 40b132
          argv++, --argc;
Packit 40b132
#else
Packit 40b132
          return usage(argv0);
Packit 40b132
#endif /* WITH_NSS */
Packit 40b132
        }
Packit 40b132
        break;
Packit 40b132
      case '-':
Packit 40b132
        argv++, --argc;
Packit 40b132
        goto endargs;
Packit 40b132
      default:
Packit 40b132
        return usage(argv0);
Packit 40b132
      }
Packit 40b132
    } else {
Packit 40b132
      break;
Packit 40b132
    }
Packit 40b132
  }
Packit 40b132
 endargs:;
Packit 40b132
Packit 40b132
  if( 1 != argc ) {
Packit 40b132
    return usage(argv0);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  lib = PR_LoadLibrary(argv[0]);
Packit 40b132
  if( (PRLibrary *)NULL == lib ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "Can't load %s: %ld, %ld\n", argv[1], PR_GetError(), PR_GetOSError());
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  gfl = (CK_C_GetFunctionList)PR_FindSymbol(lib, "C_GetFunctionList");
Packit 40b132
  if( (CK_C_GetFunctionList)NULL == gfl ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "Can't find C_GetFunctionList in %s: %ld, %ld\n", argv[1], 
Packit 40b132
               PR_GetError(), PR_GetOSError());
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  ck_rv = (*gfl)(&epv;;
Packit 40b132
  if( CKR_OK != ck_rv ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "CK_GetFunctionList returned 0x%08x\n", ck_rv);
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  PR_fprintf(PR_STDOUT, "Module %s loaded, epv = 0x%08x.\n\n", argv[1], (CK_ULONG)epv);
Packit 40b132
Packit 40b132
  /* C_Initialize */
Packit 40b132
  ck_rv = epv->C_Initialize(iap);
Packit 40b132
  if( CKR_OK != ck_rv ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "C_Initialize returned 0x%08x\n", ck_rv);
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  /* C_GetInfo */
Packit 40b132
  (void)memset(&info, 0, sizeof(CK_INFO));
Packit 40b132
  ck_rv = epv->C_GetInfo(&info;;
Packit 40b132
  if( CKR_OK != ck_rv ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "C_GetInfo returned 0x%08x\n", ck_rv);
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  PR_fprintf(PR_STDOUT, "Module Info:\n");
Packit 40b132
  PR_fprintf(PR_STDOUT, "    cryptokiVersion = %lu.%02lu\n", 
Packit 40b132
             (PRUint32)info.cryptokiVersion.major, (PRUint32)info.cryptokiVersion.minor);
Packit 40b132
  PR_fprintf(PR_STDOUT, "    manufacturerID = \"%.32s\"\n", info.manufacturerID);
Packit 40b132
  PR_fprintf(PR_STDOUT, "    flags = 0x%08lx\n", info.flags);
Packit 40b132
  PR_fprintf(PR_STDOUT, "    libraryDescription = \"%.32s\"\n", info.libraryDescription);
Packit 40b132
  PR_fprintf(PR_STDOUT, "    libraryVersion = %lu.%02lu\n", 
Packit 40b132
             (PRUint32)info.libraryVersion.major, (PRUint32)info.libraryVersion.minor);
Packit 40b132
  PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
Packit 40b132
  /* C_GetSlotList */
Packit 40b132
  nSlots = 0;
Packit 40b132
  ck_rv = epv->C_GetSlotList(CK_FALSE, (CK_SLOT_ID_PTR)CK_NULL_PTR, &nSlots);
Packit 40b132
  switch( ck_rv ) {
Packit 40b132
  case CKR_BUFFER_TOO_SMALL:
Packit 40b132
  case CKR_OK:
Packit 40b132
    break;
Packit 40b132
  default:
Packit 40b132
    PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, NULL, ) returned 0x%08x\n", ck_rv);
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  PR_fprintf(PR_STDOUT, "There are %lu slots.\n", nSlots);
Packit 40b132
Packit 40b132
  pSlots = (CK_SLOT_ID_PTR)PR_Calloc(nSlots, sizeof(CK_SLOT_ID));
Packit 40b132
  if( (CK_SLOT_ID_PTR)NULL == pSlots ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nSlots * sizeof(CK_SLOT_ID));
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  ck_rv = epv->C_GetSlotList(CK_FALSE, pSlots, &nSlots);
Packit 40b132
  if( CKR_OK != ck_rv ) {
Packit 40b132
    PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, , ) returned 0x%08x\n", ck_rv);
Packit 40b132
    return 1;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  for( i = 0; i < nSlots; i++ ) {
Packit 40b132
    PR_fprintf(PR_STDOUT, "    [%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
Packit 40b132
  /* C_GetSlotInfo */
Packit 40b132
  for( i = 0; i < nSlots; i++ ) {
Packit 40b132
    CK_SLOT_INFO sinfo;
Packit 40b132
Packit 40b132
    PR_fprintf(PR_STDOUT, "[%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);
Packit 40b132
Packit 40b132
    (void)memset(&sinfo, 0, sizeof(CK_SLOT_INFO));
Packit 40b132
    ck_rv = epv->C_GetSlotInfo(pSlots[i], &sinfo);
Packit 40b132
    if( CKR_OK != ck_rv ) {
Packit 40b132
      PR_fprintf(PR_STDERR, "C_GetSlotInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
      return 1;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PR_fprintf(PR_STDOUT, "    Slot Info:\n");
Packit 40b132
    PR_fprintf(PR_STDOUT, "        slotDescription = \"%.64s\"\n", sinfo.slotDescription);
Packit 40b132
    PR_fprintf(PR_STDOUT, "        manufacturerID = \"%.32s\"\n", sinfo.manufacturerID);
Packit 40b132
    PR_fprintf(PR_STDOUT, "        flags = 0x%08lx\n", sinfo.flags);
Packit 40b132
    PR_fprintf(PR_STDOUT, "            -> TOKEN PRESENT = %s\n", 
Packit 40b132
               sinfo.flags & CKF_TOKEN_PRESENT ? "TRUE" : "FALSE");
Packit 40b132
    PR_fprintf(PR_STDOUT, "            -> REMOVABLE DEVICE = %s\n",
Packit 40b132
               sinfo.flags & CKF_REMOVABLE_DEVICE ? "TRUE" : "FALSE");
Packit 40b132
    PR_fprintf(PR_STDOUT, "            -> HW SLOT = %s\n", 
Packit 40b132
               sinfo.flags & CKF_HW_SLOT ? "TRUE" : "FALSE");
Packit 40b132
    PR_fprintf(PR_STDOUT, "        hardwareVersion = %lu.%02lu\n", 
Packit 40b132
               (PRUint32)sinfo.hardwareVersion.major, (PRUint32)sinfo.hardwareVersion.minor);
Packit 40b132
    PR_fprintf(PR_STDOUT, "        firmwareVersion = %lu.%02lu\n",
Packit 40b132
               (PRUint32)sinfo.firmwareVersion.major, (PRUint32)sinfo.firmwareVersion.minor);
Packit 40b132
Packit 40b132
    if( sinfo.flags & CKF_TOKEN_PRESENT ) {
Packit 40b132
      CK_TOKEN_INFO tinfo;
Packit 40b132
      CK_MECHANISM_TYPE *pMechanismList;
Packit 40b132
      CK_ULONG nMechanisms = 0;
Packit 40b132
      CK_ULONG j;
Packit 40b132
Packit 40b132
      (void)memset(&tinfo, 0, sizeof(CK_TOKEN_INFO));
Packit 40b132
      ck_rv = epv->C_GetTokenInfo(pSlots[i], &tinfo);
Packit 40b132
      if( CKR_OK != ck_rv ) {
Packit 40b132
        PR_fprintf(PR_STDERR, "C_GetTokenInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
        return 1;
Packit 40b132
      }
Packit 40b132
Packit 40b132
      PR_fprintf(PR_STDOUT, "    Token Info:\n");
Packit 40b132
      PR_fprintf(PR_STDOUT, "        label = \"%.32s\"\n", tinfo.label);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        manufacturerID = \"%.32s\"\n", tinfo.manufacturerID);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        model = \"%.16s\"\n", tinfo.model);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        serialNumber = \"%.16s\"\n", tinfo.serialNumber);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        flags = 0x%08lx\n", tinfo.flags);
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> RNG = %s\n",
Packit 40b132
                 tinfo.flags & CKF_RNG ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> WRITE PROTECTED = %s\n",
Packit 40b132
                 tinfo.flags & CKF_WRITE_PROTECTED ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> LOGIN REQUIRED = %s\n",
Packit 40b132
                 tinfo.flags & CKF_LOGIN_REQUIRED ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> USER PIN INITIALIZED = %s\n",
Packit 40b132
                 tinfo.flags & CKF_USER_PIN_INITIALIZED ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> RESTORE KEY NOT NEEDED = %s\n",
Packit 40b132
                 tinfo.flags & CKF_RESTORE_KEY_NOT_NEEDED ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> CLOCK ON TOKEN = %s\n",
Packit 40b132
                 tinfo.flags & CKF_CLOCK_ON_TOKEN ? "TRUE" : "FALSE");
Packit 40b132
#ifdef CKF_SUPPORTS_PARALLEL
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> SUPPORTS PARALLEL = %s\n",
Packit 40b132
                 tinfo.flags & CKF_SUPPORTS_PARALLEL ? "TRUE" : "FALSE");
Packit 40b132
#endif /* CKF_SUPPORTS_PARALLEL */
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> PROTECTED AUTHENTICATION PATH = %s\n",
Packit 40b132
                 tinfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "            -> DUAL_CRYPTO_OPERATIONS = %s\n", 
Packit 40b132
                 tinfo.flags & CKF_DUAL_CRYPTO_OPERATIONS ? "TRUE" : "FALSE");
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulMaxSessionCount = %lu\n", tinfo.ulMaxSessionCount);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulSessionCount = %lu\n", tinfo.ulSessionCount);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulMaxRwSessionCount = %lu\n", tinfo.ulMaxRwSessionCount);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulRwSessionCount = %lu\n", tinfo.ulRwSessionCount);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulMaxPinLen = %lu\n", tinfo.ulMaxPinLen);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulMinPinLen = %lu\n", tinfo.ulMinPinLen);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulTotalPublicMemory = %lu\n", tinfo.ulTotalPublicMemory);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulFreePublicMemory = %lu\n", tinfo.ulFreePublicMemory);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulTotalPrivateMemory = %lu\n", tinfo.ulTotalPrivateMemory);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        ulFreePrivateMemory = %lu\n", tinfo.ulFreePrivateMemory);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        hardwareVersion = %lu.%02lu\n", 
Packit 40b132
                 (PRUint32)tinfo.hardwareVersion.major, (PRUint32)tinfo.hardwareVersion.minor);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        firmwareVersion = %lu.%02lu\n",
Packit 40b132
                 (PRUint32)tinfo.firmwareVersion.major, (PRUint32)tinfo.firmwareVersion.minor);
Packit 40b132
      PR_fprintf(PR_STDOUT, "        utcTime = \"%.16s\"\n", tinfo.utcTime);
Packit 40b132
Packit 40b132
Packit 40b132
      ck_rv = epv->C_GetMechanismList(pSlots[i], (CK_MECHANISM_TYPE_PTR)CK_NULL_PTR, &nMechanisms);
Packit 40b132
      switch( ck_rv ) {
Packit 40b132
      case CKR_BUFFER_TOO_SMALL:
Packit 40b132
      case CKR_OK:
Packit 40b132
        break;
Packit 40b132
      default:
Packit 40b132
        PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, NULL, ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
        return 1;
Packit 40b132
      }
Packit 40b132
Packit 40b132
      PR_fprintf(PR_STDOUT, "    %lu mechanisms:\n", nMechanisms);
Packit 40b132
Packit 40b132
      pMechanismList = (CK_MECHANISM_TYPE_PTR)PR_Calloc(nMechanisms, sizeof(CK_MECHANISM_TYPE));
Packit 40b132
      if( (CK_MECHANISM_TYPE_PTR)NULL == pMechanismList ) {
Packit 40b132
        PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", 
Packit 40b132
                   nMechanisms * sizeof(CK_MECHANISM_TYPE));
Packit 40b132
        return 1;
Packit 40b132
      }
Packit 40b132
Packit 40b132
      ck_rv = epv->C_GetMechanismList(pSlots[i], pMechanismList, &nMechanisms);
Packit 40b132
      if( CKR_OK != ck_rv ) {
Packit 40b132
        PR_fprintf(PR_STDERR, "C_GetMechanismList(%lu, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
        return 1;
Packit 40b132
      }
Packit 40b132
Packit 40b132
      for( j = 0; j < nMechanisms; j++ ) {
Packit 40b132
        PR_fprintf(PR_STDOUT, "        {%lu}: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]);
Packit 40b132
      }
Packit 40b132
Packit 40b132
      PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
Packit 40b132
      for( j = 0; j < nMechanisms; j++ ) {
Packit 40b132
        CK_MECHANISM_INFO minfo;
Packit 40b132
Packit 40b132
        (void)memset(&minfo, 0, sizeof(CK_MECHANISM_INFO));
Packit 40b132
        ck_rv = epv->C_GetMechanismInfo(pSlots[i], pMechanismList[j], &minfo);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_GetMechanismInfo(%lu, %lu, ) returned 0x%08x\n", pSlots[i], 
Packit 40b132
                     pMechanismList[j]);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    [%lu]: CK_MECHANISM_TYPE = %lu\n", (j+1), pMechanismList[j]);
Packit 40b132
        PR_fprintf(PR_STDOUT, "    ulMinKeySize = %lu\n", minfo.ulMinKeySize);
Packit 40b132
        PR_fprintf(PR_STDOUT, "    ulMaxKeySize = %lu\n", minfo.ulMaxKeySize);
Packit 40b132
        PR_fprintf(PR_STDOUT, "    flags = 0x%08x\n", minfo.flags);
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> HW = %s\n", minfo.flags & CKF_HW ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> ENCRYPT = %s\n", minfo.flags & CKF_ENCRYPT ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> DECRYPT = %s\n", minfo.flags & CKF_DECRYPT ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> DIGEST = %s\n", minfo.flags & CKF_DIGEST ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> SIGN = %s\n", minfo.flags & CKF_SIGN ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> SIGN_RECOVER = %s\n", minfo.flags & CKF_SIGN_RECOVER ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> VERIFY = %s\n", minfo.flags & CKF_VERIFY ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> VERIFY_RECOVER = %s\n", minfo.flags & CKF_VERIFY_RECOVER ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> GENERATE = %s\n", minfo.flags & CKF_GENERATE ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> GENERATE_KEY_PAIR = %s\n", minfo.flags & CKF_GENERATE_KEY_PAIR ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> WRAP = %s\n", minfo.flags & CKF_WRAP ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> UNWRAP = %s\n", minfo.flags & CKF_UNWRAP ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> DERIVE = %s\n", minfo.flags & CKF_DERIVE ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        -> EXTENSION = %s\n", minfo.flags & CKF_EXTENSION ? "TRUE" : "FALSE");
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
      }
Packit 40b132
Packit 40b132
      if( tinfo.flags & CKF_LOGIN_REQUIRED ) {
Packit 40b132
        PR_fprintf(PR_STDERR, "*** LOGIN REQUIRED but not yet implemented ***\n");
Packit 40b132
        /* all the stuff about logging in as SO and setting the user pin if needed, etc. */
Packit 40b132
        return 2;
Packit 40b132
      }
Packit 40b132
Packit 40b132
      /* session to find objects */
Packit 40b132
      {
Packit 40b132
        CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
Packit 40b132
        CK_SESSION_INFO sinfo;
Packit 40b132
        CK_ATTRIBUTE_PTR pTemplate;
Packit 40b132
        CK_ULONG tnObjects = 0;
Packit 40b132
Packit 40b132
        ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Opened a session: handle = 0x%08x\n", h);
Packit 40b132
Packit 40b132
        (void)memset(&sinfo, 0, sizeof(CK_SESSION_INFO));
Packit 40b132
        ck_rv = epv->C_GetSessionInfo(h, &sinfo);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDOUT, "C_GetSessionInfo(%lu, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    SESSION INFO:\n");
Packit 40b132
        PR_fprintf(PR_STDOUT, "        slotID = %lu\n", sinfo.slotID);
Packit 40b132
        PR_fprintf(PR_STDOUT, "        state = %lu\n", sinfo.state);
Packit 40b132
        PR_fprintf(PR_STDOUT, "        flags = 0x%08x\n", sinfo.flags);
Packit 40b132
#ifdef CKF_EXCLUSIVE_SESSION
Packit 40b132
        PR_fprintf(PR_STDOUT, "            -> EXCLUSIVE SESSION = %s\n", sinfo.flags & CKF_EXCLUSIVE_SESSION ? "TRUE" : "FALSE");
Packit 40b132
#endif /* CKF_EXCLUSIVE_SESSION */
Packit 40b132
        PR_fprintf(PR_STDOUT, "            -> RW SESSION = %s\n", sinfo.flags & CKF_RW_SESSION ? "TRUE" : "FALSE");
Packit 40b132
        PR_fprintf(PR_STDOUT, "            -> SERIAL SESSION = %s\n", sinfo.flags & CKF_SERIAL_SESSION ? "TRUE" : "FALSE");
Packit 40b132
#ifdef CKF_INSERTION_CALLBACK
Packit 40b132
        PR_fprintf(PR_STDOUT, "            -> INSERTION CALLBACK = %s\n", sinfo.flags & CKF_INSERTION_CALLBACK ? "TRUE" : "FALSE");
Packit 40b132
#endif /* CKF_INSERTION_CALLBACK */
Packit 40b132
        PR_fprintf(PR_STDOUT, "        ulDeviceError = %lu\n", sinfo.ulDeviceError);
Packit 40b132
        PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, (CK_ATTRIBUTE_PTR)CK_NULL_PTR, 0);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDOUT, "C_FindObjectsInit(%lu, NULL_PTR, 0) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        pTemplate = (CK_ATTRIBUTE_PTR)PR_Calloc(number_of_all_known_attribute_types, sizeof(CK_ATTRIBUTE));
Packit 40b132
        if( (CK_ATTRIBUTE_PTR)NULL == pTemplate ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", 
Packit 40b132
                     number_of_all_known_attribute_types * sizeof(CK_ATTRIBUTE));
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    All objects:\n");
Packit 40b132
Packit 40b132
        while(1) {
Packit 40b132
          CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0;
Packit 40b132
          CK_ULONG nObjects = 0;
Packit 40b132
          CK_ULONG k;
Packit 40b132
          CK_ULONG nAttributes = 0;
Packit 40b132
          CK_ATTRIBUTE_PTR pT2;
Packit 40b132
          CK_ULONG l;
Packit 40b132
Packit 40b132
          ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects);
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          if( 0 == nObjects ) {
Packit 40b132
            PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
            break;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          tnObjects++;
Packit 40b132
Packit 40b132
          PR_fprintf(PR_STDOUT, "        OBJECT HANDLE %lu:\n", o);
Packit 40b132
Packit 40b132
          for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
Packit 40b132
            pTemplate[k].type = all_known_attribute_types[k];
Packit 40b132
            pTemplate[k].pValue = (CK_VOID_PTR)CK_NULL_PTR;
Packit 40b132
            pTemplate[k].ulValueLen = 0;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          ck_rv = epv->C_GetAttributeValue(h, o, pTemplate, number_of_all_known_attribute_types);
Packit 40b132
          switch( ck_rv ) {
Packit 40b132
          case CKR_OK:
Packit 40b132
          case CKR_ATTRIBUTE_SENSITIVE:
Packit 40b132
          case CKR_ATTRIBUTE_TYPE_INVALID:
Packit 40b132
          case CKR_BUFFER_TOO_SMALL:
Packit 40b132
            break;
Packit 40b132
          default:
Packit 40b132
            PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {all attribute types}, %lu) returned 0x%08x\n",
Packit 40b132
                       h, o, number_of_all_known_attribute_types, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
Packit 40b132
            if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
Packit 40b132
              nAttributes++;
Packit 40b132
            }
Packit 40b132
          }
Packit 40b132
Packit 40b132
          if( 1 ) {
Packit 40b132
            PR_fprintf(PR_STDOUT, "            %lu attributes:\n", nAttributes);
Packit 40b132
            for( k = 0; k < number_of_all_known_attribute_types; k++ ) {
Packit 40b132
              if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
Packit 40b132
                PR_fprintf(PR_STDOUT, "                0x%08x (len = %lu)\n", pTemplate[k].type, 
Packit 40b132
                           pTemplate[k].ulValueLen);
Packit 40b132
              }
Packit 40b132
            }
Packit 40b132
            PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
          }
Packit 40b132
Packit 40b132
          pT2 = (CK_ATTRIBUTE_PTR)PR_Calloc(nAttributes, sizeof(CK_ATTRIBUTE));
Packit 40b132
          if( (CK_ATTRIBUTE_PTR)NULL == pT2 ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", 
Packit 40b132
                       nAttributes * sizeof(CK_ATTRIBUTE));
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          for( l = 0, k = 0; k < number_of_all_known_attribute_types; k++ ) {
Packit 40b132
            if( -1 != (CK_LONG)pTemplate[k].ulValueLen ) {
Packit 40b132
              pT2[l].type = pTemplate[k].type;
Packit 40b132
              pT2[l].ulValueLen = pTemplate[k].ulValueLen;
Packit 40b132
              pT2[l].pValue = (CK_VOID_PTR)PR_Malloc(pT2[l].ulValueLen);
Packit 40b132
              if( (CK_VOID_PTR)NULL == pT2[l].pValue ) {
Packit 40b132
                PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", pT2[l].ulValueLen);
Packit 40b132
                return 1;
Packit 40b132
              }
Packit 40b132
              l++;
Packit 40b132
            }
Packit 40b132
          }
Packit 40b132
Packit 40b132
          PR_ASSERT( l == nAttributes );
Packit 40b132
Packit 40b132
          ck_rv = epv->C_GetAttributeValue(h, o, pT2, nAttributes);
Packit 40b132
          switch( ck_rv ) {
Packit 40b132
          case CKR_OK:
Packit 40b132
          case CKR_ATTRIBUTE_SENSITIVE:
Packit 40b132
          case CKR_ATTRIBUTE_TYPE_INVALID:
Packit 40b132
          case CKR_BUFFER_TOO_SMALL:
Packit 40b132
            break;
Packit 40b132
          default:
Packit 40b132
            PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, {existent attribute types}, %lu) returned 0x%08x\n",
Packit 40b132
                       h, o, nAttributes, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          for( l = 0; l < nAttributes; l++ ) {
Packit 40b132
            PR_fprintf(PR_STDOUT, "            type = 0x%08x, len = %ld", pT2[l].type, (CK_LONG)pT2[l].ulValueLen);
Packit 40b132
            if( -1 == (CK_LONG)pT2[l].ulValueLen ) {
Packit 40b132
              ;
Packit 40b132
            } else {
Packit 40b132
              CK_ULONG m;
Packit 40b132
Packit 40b132
              if( pT2[l].ulValueLen <= 8 ) {
Packit 40b132
                PR_fprintf(PR_STDOUT, ", value = ");
Packit 40b132
              } else {
Packit 40b132
                PR_fprintf(PR_STDOUT, ", value = \n                ");
Packit 40b132
              }
Packit 40b132
Packit 40b132
              for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) {
Packit 40b132
                PR_fprintf(PR_STDOUT, "%02x", (CK_ULONG)(0xff & ((CK_CHAR_PTR)pT2[l].pValue)[m]));
Packit 40b132
              }
Packit 40b132
Packit 40b132
              PR_fprintf(PR_STDOUT, " ");
Packit 40b132
Packit 40b132
              for( m = 0; (m < pT2[l].ulValueLen) && (m < 20); m++ ) {
Packit 40b132
                CK_CHAR c = ((CK_CHAR_PTR)pT2[l].pValue)[m];
Packit 40b132
                if( (c < 0x20) || (c >= 0x7f) ) {
Packit 40b132
                  c = '.';
Packit 40b132
                }
Packit 40b132
                PR_fprintf(PR_STDOUT, "%c", c);
Packit 40b132
              }
Packit 40b132
            }
Packit 40b132
Packit 40b132
            PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
          }
Packit 40b132
          
Packit 40b132
          PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
Packit 40b132
          for( l = 0; l < nAttributes; l++ ) {
Packit 40b132
            PR_Free(pT2[l].pValue);
Packit 40b132
          }
Packit 40b132
          PR_Free(pT2);
Packit 40b132
        } /* while(1) */
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    (%lu objects total)\n", tnObjects);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CloseSession(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
      } /* session to find objects */
Packit 40b132
Packit 40b132
      /* session to create, find, and delete a couple session objects */
Packit 40b132
      {
Packit 40b132
        CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
Packit 40b132
        CK_ATTRIBUTE one[7], two[7], three[7], delta[1], mask[1];
Packit 40b132
        CK_OBJECT_CLASS cko_data = CKO_DATA;
Packit 40b132
        CK_BBOOL false = CK_FALSE, true = CK_TRUE;
Packit 40b132
        char *key = "TEST PROGRAM";
Packit 40b132
        CK_ULONG key_len = strlen(key);
Packit 40b132
        CK_OBJECT_HANDLE hOneIn = (CK_OBJECT_HANDLE)0, hTwoIn = (CK_OBJECT_HANDLE)0, 
Packit 40b132
          hThreeIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0;
Packit 40b132
        CK_OBJECT_HANDLE found[10];
Packit 40b132
        CK_ULONG nFound;
Packit 40b132
Packit 40b132
        ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Opened a session: handle = 0x%08x\n", h);
Packit 40b132
Packit 40b132
        one[0].type = CKA_CLASS;
Packit 40b132
        one[0].pValue = &cko_data;
Packit 40b132
        one[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        one[1].type = CKA_TOKEN;
Packit 40b132
        one[1].pValue = &fals;;
Packit 40b132
        one[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        one[2].type = CKA_PRIVATE;
Packit 40b132
        one[2].pValue = &fals;;
Packit 40b132
        one[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        one[3].type = CKA_MODIFIABLE;
Packit 40b132
        one[3].pValue = &tru;;
Packit 40b132
        one[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        one[4].type = CKA_LABEL;
Packit 40b132
        one[4].pValue = "Test data object one";
Packit 40b132
        one[4].ulValueLen = strlen(one[4].pValue);
Packit 40b132
        one[5].type = CKA_APPLICATION;
Packit 40b132
        one[5].pValue = key;
Packit 40b132
        one[5].ulValueLen = key_len;
Packit 40b132
        one[6].type = CKA_VALUE;
Packit 40b132
        one[6].pValue = "Object one";
Packit 40b132
        one[6].ulValueLen = strlen(one[6].pValue);
Packit 40b132
Packit 40b132
        two[0].type = CKA_CLASS;
Packit 40b132
        two[0].pValue = &cko_data;
Packit 40b132
        two[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        two[1].type = CKA_TOKEN;
Packit 40b132
        two[1].pValue = &fals;;
Packit 40b132
        two[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        two[2].type = CKA_PRIVATE;
Packit 40b132
        two[2].pValue = &fals;;
Packit 40b132
        two[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        two[3].type = CKA_MODIFIABLE;
Packit 40b132
        two[3].pValue = &tru;;
Packit 40b132
        two[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        two[4].type = CKA_LABEL;
Packit 40b132
        two[4].pValue = "Test data object two";
Packit 40b132
        two[4].ulValueLen = strlen(two[4].pValue);
Packit 40b132
        two[5].type = CKA_APPLICATION;
Packit 40b132
        two[5].pValue = key;
Packit 40b132
        two[5].ulValueLen = key_len;
Packit 40b132
        two[6].type = CKA_VALUE;
Packit 40b132
        two[6].pValue = "Object two";
Packit 40b132
        two[6].ulValueLen = strlen(two[6].pValue);
Packit 40b132
Packit 40b132
        three[0].type = CKA_CLASS;
Packit 40b132
        three[0].pValue = &cko_data;
Packit 40b132
        three[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        three[1].type = CKA_TOKEN;
Packit 40b132
        three[1].pValue = &fals;;
Packit 40b132
        three[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        three[2].type = CKA_PRIVATE;
Packit 40b132
        three[2].pValue = &fals;;
Packit 40b132
        three[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        three[3].type = CKA_MODIFIABLE;
Packit 40b132
        three[3].pValue = &tru;;
Packit 40b132
        three[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        three[4].type = CKA_LABEL;
Packit 40b132
        three[4].pValue = "Test data object three";
Packit 40b132
        three[4].ulValueLen = strlen(three[4].pValue);
Packit 40b132
        three[5].type = CKA_APPLICATION;
Packit 40b132
        three[5].pValue = key;
Packit 40b132
        three[5].ulValueLen = key_len;
Packit 40b132
        three[6].type = CKA_VALUE;
Packit 40b132
        three[6].pValue = "Object three";
Packit 40b132
        three[6].ulValueLen = strlen(three[6].pValue);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, one, 7, &hOneIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, one, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object one: handle = %lu\n", hOneIn);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, two, 7, &hTwoIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, two, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object two: handle = %lu\n", hTwoIn);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, three, 7, &hThreeIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, three, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object three: handle = %lu\n", hThreeIn);
Packit 40b132
Packit 40b132
        delta[0].type = CKA_VALUE;
Packit 40b132
        delta[0].pValue = "Copied object";
Packit 40b132
        delta[0].ulValueLen = strlen(delta[0].pValue);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CopyObject(h, hThreeIn, delta, 1, &hDeltaIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CopyObject(%lu, %lu, delta, 1, ) returned 0x%08x\n", 
Packit 40b132
                     h, hThreeIn, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Copied object three: new handle = %lu\n", hDeltaIn);
Packit 40b132
Packit 40b132
        mask[0].type = CKA_APPLICATION;
Packit 40b132
        mask[0].pValue = key;
Packit 40b132
        mask[0].ulValueLen = key_len;
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, mask, 1);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        (void)memset(&found, 0, sizeof(found));
Packit 40b132
        nFound = 0;
Packit 40b132
        ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        if( 4 != nFound ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "Found %lu objects, not 4.\n", nFound);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Found 4 objects: %lu, %lu, %lu, %lu\n", 
Packit 40b132
                   found[0], found[1], found[2], found[3]);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        ck_rv = epv->C_DestroyObject(h, hThreeIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, hThreeIn, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Destroyed object three (handle = %lu)\n", hThreeIn);
Packit 40b132
Packit 40b132
        delta[0].type = CKA_APPLICATION;
Packit 40b132
        delta[0].pValue = "Changed application";
Packit 40b132
        delta[0].ulValueLen = strlen(delta[0].pValue);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_SetAttributeValue(h, hTwoIn, delta, 1);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", 
Packit 40b132
                     h, hTwoIn, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Changed object two (handle = %lu).\n", hTwoIn);
Packit 40b132
Packit 40b132
        /* Can another session find these session objects? */
Packit 40b132
        {
Packit 40b132
          CK_SESSION_HANDLE h2 = (CK_SESSION_HANDLE)0;
Packit 40b132
Packit 40b132
          ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h2;;
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          PR_fprintf(PR_STDOUT, "    Opened a second session: handle = 0x%08x\n", h2);
Packit 40b132
Packit 40b132
          /* mask is still the same */
Packit 40b132
Packit 40b132
          ck_rv = epv->C_FindObjectsInit(h2, mask, 1);
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", 
Packit 40b132
                       h2, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          (void)memset(&found, 0, sizeof(found));
Packit 40b132
          nFound = 0;
Packit 40b132
          ck_rv = epv->C_FindObjects(h2, found, 10, &nFound);
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", 
Packit 40b132
                       h2, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          if( 2 != nFound ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          PR_fprintf(PR_STDOUT, "    Found 2 objects: %lu, %lu\n", 
Packit 40b132
                     found[0], found[1]);
Packit 40b132
Packit 40b132
          ck_rv = epv->C_FindObjectsFinal(h2);
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h2, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          /* Leave the session hanging open, we'll CloseAllSessions later */
Packit 40b132
        } /* Can another session find these session objects? */
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CloseAllSessions(pSlots[i]);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CloseAllSessions(%lu) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
      } /* session to create, find, and delete a couple session objects */
Packit 40b132
Packit 40b132
      /* Might be interesting to do a find here to verify that all session objects are gone. */
Packit 40b132
Packit 40b132
      if( tinfo.flags & CKF_WRITE_PROTECTED ) {
Packit 40b132
        PR_fprintf(PR_STDOUT, "Token is write protected, skipping token-object tests.\n");
Packit 40b132
      } else {
Packit 40b132
        CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
Packit 40b132
        CK_ATTRIBUTE tobj[7], tsobj[7], stobj[7], delta[1], mask[2];
Packit 40b132
        CK_OBJECT_CLASS cko_data = CKO_DATA;
Packit 40b132
        CK_BBOOL false = CK_FALSE, true = CK_TRUE;
Packit 40b132
        char *key = "TEST PROGRAM";
Packit 40b132
        CK_ULONG key_len = strlen(key);
Packit 40b132
        CK_OBJECT_HANDLE hTIn = (CK_OBJECT_HANDLE)0, hTSIn = (CK_OBJECT_HANDLE)0, 
Packit 40b132
          hSTIn = (CK_OBJECT_HANDLE)0, hDeltaIn = (CK_OBJECT_HANDLE)0;
Packit 40b132
        CK_OBJECT_HANDLE found[10];
Packit 40b132
        CK_ULONG nFound;
Packit 40b132
Packit 40b132
        ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Opened a session: handle = 0x%08x\n", h);
Packit 40b132
Packit 40b132
        tobj[0].type = CKA_CLASS;
Packit 40b132
        tobj[0].pValue = &cko_data;
Packit 40b132
        tobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        tobj[1].type = CKA_TOKEN;
Packit 40b132
        tobj[1].pValue = &tru;;
Packit 40b132
        tobj[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tobj[2].type = CKA_PRIVATE;
Packit 40b132
        tobj[2].pValue = &fals;;
Packit 40b132
        tobj[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tobj[3].type = CKA_MODIFIABLE;
Packit 40b132
        tobj[3].pValue = &tru;;
Packit 40b132
        tobj[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tobj[4].type = CKA_LABEL;
Packit 40b132
        tobj[4].pValue = "Test data object token";
Packit 40b132
        tobj[4].ulValueLen = strlen(tobj[4].pValue);
Packit 40b132
        tobj[5].type = CKA_APPLICATION;
Packit 40b132
        tobj[5].pValue = key;
Packit 40b132
        tobj[5].ulValueLen = key_len;
Packit 40b132
        tobj[6].type = CKA_VALUE;
Packit 40b132
        tobj[6].pValue = "Object token";
Packit 40b132
        tobj[6].ulValueLen = strlen(tobj[6].pValue);
Packit 40b132
Packit 40b132
        tsobj[0].type = CKA_CLASS;
Packit 40b132
        tsobj[0].pValue = &cko_data;
Packit 40b132
        tsobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        tsobj[1].type = CKA_TOKEN;
Packit 40b132
        tsobj[1].pValue = &tru;;
Packit 40b132
        tsobj[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tsobj[2].type = CKA_PRIVATE;
Packit 40b132
        tsobj[2].pValue = &fals;;
Packit 40b132
        tsobj[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tsobj[3].type = CKA_MODIFIABLE;
Packit 40b132
        tsobj[3].pValue = &tru;;
Packit 40b132
        tsobj[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        tsobj[4].type = CKA_LABEL;
Packit 40b132
        tsobj[4].pValue = "Test data object token->session";
Packit 40b132
        tsobj[4].ulValueLen = strlen(tsobj[4].pValue);
Packit 40b132
        tsobj[5].type = CKA_APPLICATION;
Packit 40b132
        tsobj[5].pValue = key;
Packit 40b132
        tsobj[5].ulValueLen = key_len;
Packit 40b132
        tsobj[6].type = CKA_VALUE;
Packit 40b132
        tsobj[6].pValue = "Object token->session";
Packit 40b132
        tsobj[6].ulValueLen = strlen(tsobj[6].pValue);
Packit 40b132
Packit 40b132
        stobj[0].type = CKA_CLASS;
Packit 40b132
        stobj[0].pValue = &cko_data;
Packit 40b132
        stobj[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        stobj[1].type = CKA_TOKEN;
Packit 40b132
        stobj[1].pValue = &fals;;
Packit 40b132
        stobj[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        stobj[2].type = CKA_PRIVATE;
Packit 40b132
        stobj[2].pValue = &fals;;
Packit 40b132
        stobj[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        stobj[3].type = CKA_MODIFIABLE;
Packit 40b132
        stobj[3].pValue = &tru;;
Packit 40b132
        stobj[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        stobj[4].type = CKA_LABEL;
Packit 40b132
        stobj[4].pValue = "Test data object session->token";
Packit 40b132
        stobj[4].ulValueLen = strlen(stobj[4].pValue);
Packit 40b132
        stobj[5].type = CKA_APPLICATION;
Packit 40b132
        stobj[5].pValue = key;
Packit 40b132
        stobj[5].ulValueLen = key_len;
Packit 40b132
        stobj[6].type = CKA_VALUE;
Packit 40b132
        stobj[6].pValue = "Object session->token";
Packit 40b132
        stobj[6].ulValueLen = strlen(stobj[6].pValue);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, tobj, 7, &hTIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object token: handle = %lu\n", hTIn);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, tsobj, 7, &hTSIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object token->session: handle = %lu\n", hTSIn);
Packit 40b132
        ck_rv = epv->C_CreateObject(h, stobj, 7, &hSTIn);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created object session->token: handle = %lu\n", hSTIn);
Packit 40b132
Packit 40b132
        /* I've created two token objects and one session object; find the two */
Packit 40b132
Packit 40b132
        mask[0].type = CKA_APPLICATION;
Packit 40b132
        mask[0].pValue = key;
Packit 40b132
        mask[0].ulValueLen = key_len;
Packit 40b132
        mask[1].type = CKA_TOKEN;
Packit 40b132
        mask[1].pValue = &tru;;
Packit 40b132
        mask[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, mask, 2);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        (void)memset(&found, 0, sizeof(found));
Packit 40b132
        nFound = 0;
Packit 40b132
        ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        if( 2 != nFound ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Found 2 objects: %lu, %lu\n", 
Packit 40b132
                   found[0], found[1]);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        /* Convert a token to session object */
Packit 40b132
Packit 40b132
        delta[0].type = CKA_TOKEN;
Packit 40b132
        delta[0].pValue = &fals;;
Packit 40b132
        delta[0].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_SetAttributeValue(h, hTSIn, delta, 1);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", 
Packit 40b132
                     h, hTSIn, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Changed object from token to session (handle = %lu).\n", hTSIn);
Packit 40b132
Packit 40b132
        /* Now find again; there should be one */
Packit 40b132
Packit 40b132
        mask[0].type = CKA_APPLICATION;
Packit 40b132
        mask[0].pValue = key;
Packit 40b132
        mask[0].ulValueLen = key_len;
Packit 40b132
        mask[1].type = CKA_TOKEN;
Packit 40b132
        mask[1].pValue = &tru;;
Packit 40b132
        mask[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, mask, 2);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        (void)memset(&found, 0, sizeof(found));
Packit 40b132
        nFound = 0;
Packit 40b132
        ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        if( 1 != nFound ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "Found %lu objects, not 1.\n", nFound);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Found 1 objects: %lu\n", 
Packit 40b132
                   found[0]);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        /* Convert a session to a token object */
Packit 40b132
Packit 40b132
        delta[0].type = CKA_TOKEN;
Packit 40b132
        delta[0].pValue = &tru;;
Packit 40b132
        delta[0].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_SetAttributeValue(h, hSTIn, delta, 1);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_SetAttributeValue(%lu, %lu, delta, 1) returned 0x%08x\n", 
Packit 40b132
                     h, hSTIn, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Changed object from session to token (handle = %lu).\n", hSTIn);
Packit 40b132
Packit 40b132
        /* Now find again; there should be two again */
Packit 40b132
Packit 40b132
        mask[0].type = CKA_APPLICATION;
Packit 40b132
        mask[0].pValue = key;
Packit 40b132
        mask[0].ulValueLen = key_len;
Packit 40b132
        mask[1].type = CKA_TOKEN;
Packit 40b132
        mask[1].pValue = &tru;;
Packit 40b132
        mask[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, mask, 2);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 2) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        (void)memset(&found, 0, sizeof(found));
Packit 40b132
        nFound = 0;
Packit 40b132
        ck_rv = epv->C_FindObjects(h, found, 10, &nFound);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjects(%lu,, 10, ) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        if( 2 != nFound ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "Found %lu objects, not 2.\n", nFound);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Found 2 objects: %lu, %lu\n", 
Packit 40b132
                   found[0], found[1]);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        /* Delete the two (found) token objects to clean up */
Packit 40b132
Packit 40b132
        ck_rv = epv->C_DestroyObject(h, found[0]);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[0], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Destroyed token object (handle = %lu)\n", found[0]);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_DestroyObject(h, found[1]);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_DestroyObject(%lu, %lu) returned 0x%08x\n", h, found[1], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Destroyed token object (handle = %lu)\n", found[1]);
Packit 40b132
        
Packit 40b132
        /* Close the session and all objects should be gone */
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CloseSession(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CloseSession(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
      } /* if( tinfo.flags & CKF_WRITE_PROTECTED ) */
Packit 40b132
Packit 40b132
      if( tinfo.flags & CKF_WRITE_PROTECTED ) {
Packit 40b132
        PR_fprintf(PR_STDOUT, "Token is write protected, skipping leaving a record.\n");
Packit 40b132
      } else {
Packit 40b132
        CK_SESSION_HANDLE h = (CK_SESSION_HANDLE)0;
Packit 40b132
        CK_ATTRIBUTE record[7], mask[2];
Packit 40b132
        CK_OBJECT_CLASS cko_data = CKO_DATA;
Packit 40b132
        CK_BBOOL false = CK_FALSE, true = CK_TRUE;
Packit 40b132
        char *key = "TEST RECORD";
Packit 40b132
        CK_ULONG key_len = strlen(key);
Packit 40b132
        CK_OBJECT_HANDLE hin = (CK_OBJECT_HANDLE)0;
Packit 40b132
        char timebuffer[256];
Packit 40b132
Packit 40b132
        ck_rv = epv->C_OpenSession(pSlots[i], CKF_SERIAL_SESSION, (CK_VOID_PTR)CK_NULL_PTR, (CK_NOTIFY)CK_NULL_PTR, &h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_OpenSession(%lu, CKF_SERIAL_SESSION, , ) returned 0x%08x\n", pSlots[i], ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Opened a session: handle = 0x%08x\n", h);
Packit 40b132
Packit 40b132
        /* I can't believe how hard NSPR makes this operation */
Packit 40b132
        {
Packit 40b132
          time_t now = 0;
Packit 40b132
          struct tm *tm;
Packit 40b132
          time(&now;;
Packit 40b132
          tm = localtime(&now;;
Packit 40b132
          strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %T %Z", tm);
Packit 40b132
        }
Packit 40b132
Packit 40b132
        record[0].type = CKA_CLASS;
Packit 40b132
        record[0].pValue = &cko_data;
Packit 40b132
        record[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        record[1].type = CKA_TOKEN;
Packit 40b132
        record[1].pValue = &tru;;
Packit 40b132
        record[1].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        record[2].type = CKA_PRIVATE;
Packit 40b132
        record[2].pValue = &fals;;
Packit 40b132
        record[2].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        record[3].type = CKA_MODIFIABLE;
Packit 40b132
        record[3].pValue = &tru;;
Packit 40b132
        record[3].ulValueLen = sizeof(CK_BBOOL);
Packit 40b132
        record[4].type = CKA_LABEL;
Packit 40b132
        record[4].pValue = "Test record";
Packit 40b132
        record[4].ulValueLen = strlen(record[4].pValue);
Packit 40b132
        record[5].type = CKA_APPLICATION;
Packit 40b132
        record[5].pValue = key;
Packit 40b132
        record[5].ulValueLen = key_len;
Packit 40b132
        record[6].type = CKA_VALUE;
Packit 40b132
        record[6].pValue = timebuffer;
Packit 40b132
        record[6].ulValueLen = strlen(timebuffer)+1;
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Timestamping with \"%s\"\n", timebuffer);
Packit 40b132
Packit 40b132
        ck_rv = epv->C_CreateObject(h, record, 7, &hin);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_CreateObject(%lu, tobj, 7, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PR_fprintf(PR_STDOUT, "    Created record object: handle = %lu\n", hin);
Packit 40b132
        
Packit 40b132
        PR_fprintf(PR_STDOUT, "   == All test timestamps ==\n");
Packit 40b132
Packit 40b132
        mask[0].type = CKA_CLASS;
Packit 40b132
        mask[0].pValue = &cko_data;
Packit 40b132
        mask[0].ulValueLen = sizeof(CK_OBJECT_CLASS);
Packit 40b132
        mask[1].type = CKA_APPLICATION;
Packit 40b132
        mask[1].pValue = key;
Packit 40b132
        mask[1].ulValueLen = key_len;
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsInit(h, mask, 2);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsInit(%lu, mask, 1) returned 0x%08x\n", 
Packit 40b132
                     h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
Packit 40b132
        while( 1 ) {
Packit 40b132
          CK_OBJECT_HANDLE o = (CK_OBJECT_HANDLE)0;
Packit 40b132
          CK_ULONG nObjects = 0;
Packit 40b132
          CK_ATTRIBUTE value[1];
Packit 40b132
          char buffer[1024];
Packit 40b132
Packit 40b132
          ck_rv = epv->C_FindObjects(h, &o, 1, &nObjects);
Packit 40b132
          if( CKR_OK != ck_rv ) {
Packit 40b132
            PR_fprintf(PR_STDERR, "C_FindObjects(%lu, , 1, ) returned 0x%08x\n", h, ck_rv);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          if( 0 == nObjects ) {
Packit 40b132
            PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
            break;
Packit 40b132
          }
Packit 40b132
Packit 40b132
          value[0].type = CKA_VALUE;
Packit 40b132
          value[0].pValue = buffer;
Packit 40b132
          value[0].ulValueLen = sizeof(buffer);
Packit 40b132
Packit 40b132
          ck_rv = epv->C_GetAttributeValue(h, o, value, 1);
Packit 40b132
          switch( ck_rv ) {
Packit 40b132
          case CKR_OK:
Packit 40b132
            PR_fprintf(PR_STDOUT, "    %s\n", value[0].pValue);
Packit 40b132
            break;
Packit 40b132
          case CKR_ATTRIBUTE_SENSITIVE:
Packit 40b132
            PR_fprintf(PR_STDOUT, "    [Sensitive???]\n");
Packit 40b132
            break;
Packit 40b132
          case CKR_ATTRIBUTE_TYPE_INVALID:
Packit 40b132
            PR_fprintf(PR_STDOUT, "    [Invalid attribute???]\n");
Packit 40b132
            break;
Packit 40b132
          case CKR_BUFFER_TOO_SMALL:
Packit 40b132
            PR_fprintf(PR_STDOUT, "    (result > 1k (%lu))\n", value[0].ulValueLen);
Packit 40b132
            break;
Packit 40b132
          default:
Packit 40b132
            PR_fprintf(PR_STDERR, "C_GetAtributeValue(%lu, %lu, CKA_VALUE, 1) returned 0x%08x\n",
Packit 40b132
                       h, o);
Packit 40b132
            return 1;
Packit 40b132
          }
Packit 40b132
        } /* while */
Packit 40b132
Packit 40b132
        ck_rv = epv->C_FindObjectsFinal(h);
Packit 40b132
        if( CKR_OK != ck_rv ) {
Packit 40b132
          PR_fprintf(PR_STDERR, "C_FindObjectsFinal(%lu) returned 0x%08x\n", h, ck_rv);
Packit 40b132
          return 1;
Packit 40b132
        }
Packit 40b132
      } /* "leaving a record" else clause */
Packit 40b132
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PR_fprintf(PR_STDOUT, "\n");
Packit 40b132
  }
Packit 40b132
Packit 40b132
  return 0;
Packit 40b132
}