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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial