RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

extract.c

Takes a PKCS #7 message and extracts any certificates or CRLs it contains, writing them out to binary files. The sample program, cert, can be used to examine the contents of those binaries.

/* $Id: extract.c,v 1.3 2004/03/02 05:18:41 gsingh Exp $ */
/* extract.c
** Copyright (c) 1999-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 takes a PKCS #7 message and extracts any certs or CRLs
** containted in it, writing them out to binary files.  The demo
** program, cert, can be used to examine the contents of those binaries.
**
** 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 "demoutil.h"

int main (int argc, char *argv[])
{
  int status = 0;
  
  CERTC_CTX ctx = NULL;
  ITEM msg = {NULL, 0}, data = {NULL, 0}, oid = {NULL, 0};
  LIST_OBJ certs = NULL, crls = NULL;
  LIST_OBJ verifiedSigners = NULL, untrustedSigners = NULL;
  unsigned int certCount = 0, crlCount = 0, i = 0;

  CERT_OBJ certObj = NULL;
  unsigned char *certBer = NULL;
  unsigned int certBerLen = 0;

  CRL_OBJ crlObj = NULL;
  unsigned char *crlBer = NULL;
  unsigned int crlBerLen = 0;

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

  status = RSA_SetOptions (&logParams, argc, argv);
  if (status != 0)
    goto CLEANUP;
  
  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

  RSA_PrintMessage ("Extract Certs and CRLs from a PKCS #7 message\n");
  RSA_PrintMessage ("=============================================\n");
  
  status = RSA_GetFileToAllocBuffer
           (&msg.data, &msg.len,
            "Enter name of file containing PKCS #7 binary");
  if (status != 0)
    goto CLEANUP;

  status = C_CreateListObject (&certs);
  if (status != 0)
    goto CLEANUP;

  status = C_CreateListObject (&crls);
  if (status != 0)
    goto CLEANUP;

  status = C_CreateListObject (&verifiedSigners);
  if (status != 0)
    goto CLEANUP;

  status = C_CreateListObject (&untrustedSigners);
  if (status != 0)
    goto CLEANUP;

  status = C_ReadSignedDataMsg (ctx, NULL, NULL, &msg, CMSF_NONE, &data, &oid,
                                certs, crls, verifiedSigners,
                                untrustedSigners);
  if (status != 0)
    goto CLEANUP;

  status = C_GetListObjectCount (certs, &certCount);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("The PKCS #7 message contains %u certificates.\n",
                    certCount);

  for (i = 0; i < certCount; i++) {
    status = C_GetListObjectEntry (certs, i, (POINTER *)&certObj);
    if (status != 0)
      goto CLEANUP;

    status = C_GetCertDER (certObj, &certBer, &certBerLen);
    if (status != 0)
      goto CLEANUP;

    status = RSA_WriteDataToFile
             (certBer, certBerLen, "Enter name of file to store cert binary");
    if (status != 0 && status != RSA_DEMO_E_CANCEL)
      goto CLEANUP;
  }

  status = C_GetListObjectCount (crls, &crlCount);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("The PKCS #7 message contains %u CRLs.\n", crlCount);

  for (i = 0; i < crlCount; i++) {
    status = C_GetListObjectEntry (crls, i, (POINTER *)&crlObj);
    if (status != 0)
      goto CLEANUP;

    status = C_GetCRLDER (crlObj, &crlBer, &crlBerLen);
    if (status != 0)
      goto CLEANUP;

    status = RSA_WriteDataToFile
             (crlBer, crlBerLen, "Enter name of file to store CRL binary");
    if (status != 0 && status != RSA_DEMO_E_CANCEL)
      goto CLEANUP;
  }
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("extract.c", status);
  else
    RSA_PrintMessage ("Success!\n");

  T_free (msg.data);
  T_free (data.data);
  C_DestroyListObject (&certs);
  C_DestroyListObject (&crls);
  C_DestroyListObject (&verifiedSigners);
  C_DestroyListObject (&untrustedSigners);
  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