RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

p11dblist.c

Sets up a PKCS #11 device database provider and lists the certificates and keys on the device.

/* $Id: p11dblist.c,v 1.3 2004/03/02 05:18:39 gsingh Exp $ */
/* p11dblist.c
** Copyright (c) 2000-2002, RSA Security Inc.
**
** This file is used to demonstrate how to interface to an RSA Security
** licensed development product.  You have a royalty-free right to use,
** modify, reproduce and distribute this demonstration file (including
** any modified version), provided that you agree that RSA Security has
** no warranty, implied or otherwise, or liability for this demonstration
** file or any modified version.
**
** This program sets up a PKCS #11 database provider and lists the certs
** and keys on the device.
**
** When compiling, define the macro RSA_REQUIRE_FILE_LOG (-D compile
** option, or equivelent) to force the program to return an error code
** if file logging cannot be initialized.  For example, if the file
** containing the log message format strings cannot be located (certc.msg
** or equivalent).
*/

#include "certc.h"
#include "filelog.h"
#include "pkcs11db.h"
#include "rsacsp.h"
#include "demoutil.h"
#include "certutil.h"
#include "dbutil.h"
#include "p11util.h"

int main (int argc, char *argv[])
{
  int status = 0;

  CERTC_CTX ctx = NULL;

  B_PKCS11_SESSION p11SessionInfo;
  PKCS11_INIT_PARAMS p11InitParams;
  PKCS11_CRYPTO_PARAMS p11CryptoParams;

  SERVICE_HANDLER p11DbServiceHandler = {
    SPT_DATABASE, "Sample PKCS #11 Database", S_InitializePKCS11DB
  };

  SERVICE_HANDLER p11CryptoServiceHandler = {
    SPT_CRYPTO, "Crypto Provider with PKCS #11", S_InitializeDefaultCSP2
  };

  char *libraryName = NULL;
  ITEM tokenLabel = {NULL, 0}, passphrase = {NULL, 0};

  FILE_LOG_PARAMS logParams = {NULL, NULL};
  SERVICE_HANDLER logHandler = {
    SPT_LOG, "Default File Log", S_InitializeFileLog
  };

  /* Initialize variables for graceful error-handling */
  T_memset ((POINTER)&p11SessionInfo, 0, sizeof (p11SessionInfo));
  T_memset ((POINTER)&p11InitParams, 0, sizeof (p11InitParams));
  T_memset ((POINTER)&p11CryptoParams, 0, sizeof (p11CryptoParams));

  status = RSA_SetOptions (&logParams, argc, argv);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("PKCS #11 Cert and Private Key Enumeration\n");
  RSA_PrintMessage ("=========================================\n");

  status = C_InitializeCertC (NULL, NULL, 0, &ctx);
  if (status != 0)
    goto CLEANUP;

  /* Attempt to initialize file logging, but unless RSA_REQUIRE_FILE_LOG is
   * defined, treat it as a non-fatal condition.
   */
  status = C_RegisterService (ctx, &logHandler, (POINTER)&logParams,
                              SERVICE_ORDER_FIRST);
#ifdef RSA_REQUIRE_FILE_LOG
  if (status != 0)
    goto CLEANUP;
#endif

  /* This function is described in samples/common/include/p11util.h */
  status = RSA_Pkcs11InfoPrompt (&libraryName, &tokenLabel, &passphrase);
  if (status != 0)
    goto CLEANUP;

  p11SessionInfo.libraryName = libraryName;
  p11SessionInfo.tokenLabel.data = tokenLabel.data;
  p11SessionInfo.tokenLabel.len = tokenLabel.len;
  p11SessionInfo.passPhrase.data = passphrase.data;
  p11SessionInfo.passPhrase.len = passphrase.len;

  p11InitParams.pPKCS11Info = &p11SessionInfo;

  status = C_RegisterService (ctx, &p11DbServiceHandler,
                              (POINTER)&p11InitParams, SERVICE_ORDER_LAST);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("Registration of PKCS #11 DB provider successful.\n");

  /* We need the PKCS #11 Crypto provider since we're doing key generation.
     Currently, only one connection per chooser (crypto service provider
     instance) is supported */
  p11CryptoParams.pSessionInfo = &p11SessionInfo;
  p11CryptoParams.sessionCount = 1;

  status = C_RegisterService (ctx, &p11CryptoServiceHandler,
                              (POINTER)&p11CryptoParams, SERVICE_ORDER_LAST);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage
    ("Registration of Crypto Provider with PKCS #11 successful.\n");

  RSA_PrintMessage ("\nDumping database contents...\n");
  status = RSA_DumpDatabaseContents (ctx, p11DbServiceHandler.name);

CLEANUP:
  if (status != 0)
    RSA_PrintError ("p11dblist.c", status);
  else
    RSA_PrintMessage ("Success!\n");

  T_memset (passphrase.data, 0, passphrase.len);

  T_free ((POINTER)libraryName);
  T_free (tokenLabel.data);
  T_free (passphrase.data);

  C_FinalizeCertC (&ctx);

  return status;
}  /* end main */




Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 067-001001-2720-001-000 - 2.7.2