trivial.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,314 行 · 第 1/4 页

C
1,314
字号
/*  * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is a trivial PKCS#11 test program. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#ifdef DEBUGstatic const char CVS_ID[] = "@(#) $RCSfile: trivial.c,v $ $Revision: 1.2 $ $Date: 2000/07/20 21:48:26 $ $Name: NSS_3_1_1_RTM $";#endif /* DEBUG *//* * This is a very trivial program I wrote for testing out a  * couple data-only Cryptoki modules for NSS.  It's not a "real" * test program that prints out nice "PASS" or "FAIL" messages; * it just makes calls and dumps data. */#include "config.h"#ifdef HAVE_NSPR_H#include "nspr.h"#else#error "NSPR is required."#endif#ifdef WITH_NSS#define FGMR 1#include "ck.h"#else#include "pkcs11t.h"#include "pkcs11.h"#endif/* The RSA versions are sloppier with namespaces */#ifndef CK_TRUE#define CK_TRUE TRUE#endif#ifndef CK_FALSE#define CK_FALSE FALSE#endifintrmain(  int argc,  char *argv[]);intmain(  int argc,  char *argv[]){  int rv = 0;  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 14);  rv = rmain(argc, argv);  PR_Cleanup();  return rv;}static CK_ATTRIBUTE_TYPE all_known_attribute_types[] = {  CKA_CLASS,  CKA_TOKEN,  CKA_PRIVATE,  CKA_LABEL,  CKA_APPLICATION,  CKA_VALUE,  CKA_CERTIFICATE_TYPE,  CKA_ISSUER,  CKA_SERIAL_NUMBER,  CKA_KEY_TYPE,  CKA_SUBJECT,  CKA_ID,  CKA_SENSITIVE,  CKA_ENCRYPT,  CKA_DECRYPT,  CKA_WRAP,  CKA_UNWRAP,  CKA_SIGN,  CKA_SIGN_RECOVER,  CKA_VERIFY,  CKA_VERIFY_RECOVER,  CKA_DERIVE,  CKA_START_DATE,  CKA_END_DATE,  CKA_MODULUS,  CKA_MODULUS_BITS,  CKA_PUBLIC_EXPONENT,  CKA_PRIVATE_EXPONENT,  CKA_PRIME_1,  CKA_PRIME_2,  CKA_EXPONENT_1,  CKA_EXPONENT_2,  CKA_COEFFICIENT,  CKA_PRIME,  CKA_SUBPRIME,  CKA_BASE,  CKA_VALUE_BITS,  CKA_VALUE_LEN,  CKA_EXTRACTABLE,  CKA_LOCAL,  CKA_NEVER_EXTRACTABLE,  CKA_ALWAYS_SENSITIVE,  CKA_MODIFIABLE,#ifdef CKA_NETSCAPE  CKA_NETSCAPE_URL,  CKA_NETSCAPE_EMAIL,  CKA_NETSCAPE_SMIME_INFO,  CKA_NETSCAPE_SMIME_TIMESTAMP,  CKA_NETSCAPE_PKCS8_SALT,  CKA_NETSCAPE_PASSWORD_CHECK,  CKA_NETSCAPE_EXPIRES,#endif /* CKA_NETSCAPE */#ifdef CKA_TRUST  CKA_TRUST_DIGITAL_SIGNATURE,  CKA_TRUST_NON_REPUDIATION,  CKA_TRUST_KEY_ENCIPHERMENT,  CKA_TRUST_DATA_ENCIPHERMENT,  CKA_TRUST_KEY_AGREEMENT,  CKA_TRUST_KEY_CERT_SIGN,  CKA_TRUST_CRL_SIGN,  CKA_TRUST_SERVER_AUTH,  CKA_TRUST_CLIENT_AUTH,  CKA_TRUST_CODE_SIGNING,  CKA_TRUST_EMAIL_PROTECTION,  CKA_TRUST_IPSEC_END_SYSTEM,  CKA_TRUST_IPSEC_TUNNEL,  CKA_TRUST_IPSEC_USER,  CKA_TRUST_TIME_STAMPING,#endif /* CKA_TRUST */};static number_of_all_known_attribute_types =   (sizeof(all_known_attribute_types)/sizeof(all_known_attribute_types[0]));intusage(  char *argv0){  PR_fprintf(PR_STDERR, "Usage: %s [-i {string|--}] <library>.so\n", argv0);  return 1;}intrmain(  int argc,  char *argv[]){  char *argv0 = argv[0];  PRLibrary *lib;  CK_C_GetFunctionList gfl;  CK_FUNCTION_LIST_PTR epv = (CK_FUNCTION_LIST_PTR)NULL;  CK_RV ck_rv;  CK_INFO info;  CK_ULONG nSlots;  CK_SLOT_ID *pSlots;  CK_ULONG i;  CK_C_INITIALIZE_ARGS ia, *iap;  (void)memset(&ia, 0, sizeof(CK_C_INITIALIZE_ARGS));  iap = (CK_C_INITIALIZE_ARGS *)NULL;  while( argv++, --argc ) {    if( '-' == argv[0][0] ) {      switch( argv[0][1] ) {      case 'i':        iap = &ia;        if( ((char *)NULL != argv[1]) && ('-' != argv[1][0]) ) {#ifdef WITH_NSS          ia.pConfig = argv[1];          ia.ulConfigLen = strlen(argv[1]);          argv++, --argc;#else          return usage(argv0);#endif /* WITH_NSS */        }        break;      case '-':        argv++, --argc;        goto endargs;      default:        return usage(argv0);      }    } else {      break;    }  } endargs:;  if( 1 != argc ) {    return usage(argv0);  }  lib = PR_LoadLibrary(argv[0]);  if( (PRLibrary *)NULL == lib ) {    PR_fprintf(PR_STDERR, "Can't load %s: %ld, %ld\n", argv[1], PR_GetError(), PR_GetOSError());    return 1;  }  gfl = (CK_C_GetFunctionList)PR_FindSymbol(lib, "C_GetFunctionList");  if( (CK_C_GetFunctionList)NULL == gfl ) {    PR_fprintf(PR_STDERR, "Can't find C_GetFunctionList in %s: %ld, %ld\n", argv[1],                PR_GetError(), PR_GetOSError());    return 1;  }  ck_rv = (*gfl)(&epv);  if( CKR_OK != ck_rv ) {    PR_fprintf(PR_STDERR, "CK_GetFunctionList returned 0x%08x\n", ck_rv);    return 1;  }  PR_fprintf(PR_STDOUT, "Module %s loaded, epv = 0x%08x.\n\n", argv[1], (CK_ULONG)epv);  /* C_Initialize */  ck_rv = epv->C_Initialize(iap);  if( CKR_OK != ck_rv ) {    PR_fprintf(PR_STDERR, "C_Initialize returned 0x%08x\n", ck_rv);    return 1;  }  /* C_GetInfo */  (void)memset(&info, 0, sizeof(CK_INFO));  ck_rv = epv->C_GetInfo(&info);  if( CKR_OK != ck_rv ) {    PR_fprintf(PR_STDERR, "C_GetInfo returned 0x%08x\n", ck_rv);    return 1;  }  PR_fprintf(PR_STDOUT, "Module Info:\n");  PR_fprintf(PR_STDOUT, "    cryptokiVersion = %lu.%02lu\n",              (PRUint32)info.cryptokiVersion.major, (PRUint32)info.cryptokiVersion.minor);  PR_fprintf(PR_STDOUT, "    manufacturerID = \"%.32s\"\n", info.manufacturerID);  PR_fprintf(PR_STDOUT, "    flags = 0x%08lx\n", info.flags);  PR_fprintf(PR_STDOUT, "    libraryDescription = \"%.32s\"\n", info.libraryDescription);  PR_fprintf(PR_STDOUT, "    libraryVersion = %lu.%02lu\n",              (PRUint32)info.libraryVersion.major, (PRUint32)info.libraryVersion.minor);  PR_fprintf(PR_STDOUT, "\n");  /* C_GetSlotList */  nSlots = 0;  ck_rv = epv->C_GetSlotList(CK_FALSE, (CK_SLOT_ID_PTR)CK_NULL_PTR, &nSlots);  switch( ck_rv ) {  case CKR_BUFFER_TOO_SMALL:  case CKR_OK:    break;  default:    PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, NULL, ) returned 0x%08x\n", ck_rv);    return 1;  }  PR_fprintf(PR_STDOUT, "There are %lu slots.\n", nSlots);  pSlots = (CK_SLOT_ID_PTR)PR_Calloc(nSlots, sizeof(CK_SLOT_ID));  if( (CK_SLOT_ID_PTR)NULL == pSlots ) {    PR_fprintf(PR_STDERR, "[memory allocation of %lu bytes failed]\n", nSlots * sizeof(CK_SLOT_ID));    return 1;  }  ck_rv = epv->C_GetSlotList(CK_FALSE, pSlots, &nSlots);  if( CKR_OK != ck_rv ) {    PR_fprintf(PR_STDERR, "C_GetSlotList(FALSE, , ) returned 0x%08x\n", ck_rv);    return 1;  }  for( i = 0; i < nSlots; i++ ) {    PR_fprintf(PR_STDOUT, "    [%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);  }  PR_fprintf(PR_STDOUT, "\n");  /* C_GetSlotInfo */  for( i = 0; i < nSlots; i++ ) {    CK_SLOT_INFO sinfo;    PR_fprintf(PR_STDOUT, "[%lu]: CK_SLOT_ID = %lu\n", (i+1), pSlots[i]);    (void)memset(&sinfo, 0, sizeof(CK_SLOT_INFO));    ck_rv = epv->C_GetSlotInfo(pSlots[i], &sinfo);    if( CKR_OK != ck_rv ) {      PR_fprintf(PR_STDERR, "C_GetSlotInfo(%lu, ) returned 0x%08x\n", pSlots[i], ck_rv);      return 1;    }    PR_fprintf(PR_STDOUT, "    Slot Info:\n");    PR_fprintf(PR_STDOUT, "        slotDescription = \"%.64s\"\n", sinfo.slotDescription);    PR_fprintf(PR_STDOUT, "        manufacturerID = \"%.32s\"\n", sinfo.manufacturerID);    PR_fprintf(PR_STDOUT, "        flags = 0x%08lx\n", sinfo.flags);    PR_fprintf(PR_STDOUT, "            -> TOKEN PRESENT = %s\n",                sinfo.flags & CKF_TOKEN_PRESENT ? "TRUE" : "FALSE");    PR_fprintf(PR_STDOUT, "            -> REMOVABLE DEVICE = %s\n",               sinfo.flags & CKF_REMOVABLE_DEVICE ? "TRUE" : "FALSE");    PR_fprintf(PR_STDOUT, "            -> HW SLOT = %s\n",                sinfo.flags & CKF_HW_SLOT ? "TRUE" : "FALSE");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?