RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

exten.c

Displays Extensions object from a file. Generates certificate extensions object. Generates CRL extensions object. Generates CRL entries extensions object. Extracts extensions from attributes object.

/* $Id: exten.c,v 1.3 2004/03/02 05:18:40 gsingh Exp $ */
/* exten.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 file uses the procedures described in
** ../../utils/source/include/extnutil.h to parse and create extensions
** objects.
**
** 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"
#include "extnutil.h"
#include "attributil.h"

static int DisplayExtenObj (CERTC_CTX ctx);
static int GenerateExtenObj (CERTC_CTX ctx, unsigned int objectType);
static int GetExtenObjFromAttribObj (CERTC_CTX ctx);

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

  CERTC_CTX ctx = NULL;

  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 ("Extensions Object Demonstration\n");
  RSA_PrintMessage ("===============================\n");

  for (;;) {
    RSA_PrintMessage ("\nExtensions Object Operations\n");
    RSA_PrintMessage ("  A - Display extensions object from a file\n");
    RSA_PrintMessage ("  B - Generate certificate extensions object\n");
    RSA_PrintMessage ("  C - Generate CRL extensions object\n");
    RSA_PrintMessage ("  D - Generate CRL entries extensions object\n");
    RSA_PrintMessage ("  E - Extract extensions from attributes object\n");

    status = RSA_GetCommand (command, sizeof (command),
                             "Enter choice (blank to quit)");
    if (status != 0)
      goto CLEANUP;

    switch (command[0]) {
      case 'a':
      case 'A':
        status = DisplayExtenObj (ctx);
        break;
      case 'b':
      case 'B':
        status = GenerateExtenObj (ctx, CERT_EXTENSIONS_OBJ);
        break;
      case 'c':
      case 'C':
        status = GenerateExtenObj (ctx, CRL_EXTENSIONS_OBJ);
        break;
      case 'd':
      case 'D':
        status = GenerateExtenObj (ctx, CRL_ENTRY_EXTENSIONS_OBJ);
        break;
      case 'e':
      case 'E':
        status = GetExtenObjFromAttribObj (ctx);
        break;
      case '\0':
      case 'q':
      case 'Q':
        goto CLEANUP;
      default:
        RSA_PrintMessage ("Unrecognized Option: %c\n", command[0]);
        status = RSA_DEMO_E_INVALID_PARAMETER;
    }

    if (status != 0)
      RSA_PrintMessage ("Operation not completed.\n");
    else
      RSA_PrintMessage ("Operation successful!\n");
  }
    
CLEANUP:
  if (status != 0)
    RSA_PrintError ("exten.c", status);

  C_FinalizeCertC (&ctx);

  return status;
}  /* end main */

static int DisplayExtenObj (CERTC_CTX ctx)
{
  int status = 0;
  
  EXTENSIONS_OBJ extenObj = NULL;

  status = RSA_GetFileToExtensionsObject (ctx, &extenObj);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("\nExtensions Object Contents\n");
  status = RSA_PrintExtensionsObject (extenObj);
  if (status != 0)
    goto CLEANUP;

CLEANUP:
  if (status != 0)
    RSA_PrintError ("DisplayExtenObj", status);

  C_DestroyExtensionsObject (&extenObj);

  return status;
}  /* end DisplayExtenObj */

static int GenerateExtenObj (CERTC_CTX ctx, unsigned int objectType)
{
  int status = 0;

  EXTENSIONS_OBJ extenObj = (EXTENSIONS_OBJ)NULL_PTR;

  unsigned char *extenBer = NULL_PTR;
  unsigned int extenBerLen = 0;

  status = C_CreateExtensionsObject (&extenObj, objectType, ctx);
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetInputToExtensionsObject (extenObj, objectType);
  if (status != 0)
    goto CLEANUP;

  status = C_GetExtensionsObjectDER (extenObj, &extenBer, &extenBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_WriteDataToFile
           (extenBer, extenBerLen,
            "Enter file name to store extensions object binary");
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("GenerateExtenObj", status);

  C_DestroyExtensionsObject (&extenObj);
  
  return status;
}  /* end GenerateExtenObj */

static int GetExtenObjFromAttribObj (CERTC_CTX ctx)
{
  int status = 0;

  ATTRIBUTES_OBJ attribObj = NULL;
  EXTENSIONS_OBJ extenObj = NULL;

  unsigned int i = 0;
  
  unsigned int extenTypes[3] = {
    CERT_EXTENSIONS_OBJ, CRL_EXTENSIONS_OBJ, CRL_ENTRY_EXTENSIONS_OBJ
  };

  unsigned char *attribBer = NULL, *extenDer = NULL;
  unsigned int attribBerLen = 0, extenDerLen = 0;
  
  RSA_PrintMessage ("Enter name of file containing attributes object ");
  RSA_PrintMessage ("(blank to cancel):\n");
  status = RSA_GetFileToAllocBuffer (&attribBer, &attribBerLen, NULL);
  if (status != 0)
    goto CLEANUP;

  status = C_CreateAttributesObject (&attribObj);
  if (status != 0)
    goto CLEANUP;

  status = C_SetAttributesBER (attribObj, attribBer, attribBerLen);
  if (status != 0)
    goto CLEANUP;

  for (i = 0; i < sizeof (extenTypes)/sizeof (extenTypes[0]); i++) {
    status = C_CreateExtensionsObject (&extenObj, extenTypes[i], ctx);
    if (status != 0)
      goto CLEANUP;

    status = C_GetExtensionsInAttributesObj (extenObj, attribObj);
    if (status == 0)
      break;

    C_DestroyExtensionsObject (&extenObj);
  }
  if (status != 0)  /* if none of the extension types worked... */
    goto CLEANUP;

  status = C_GetExtensionsObjectDER (extenObj, &extenDer, &extenDerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_WriteDataToFile
           (extenDer, extenDerLen,
            "Enter file name to store extensions object binary");
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("GetExtenObjFromAttribObj", status);

  T_free (attribBer);
  C_DestroyAttributesObject (&attribObj);
  C_DestroyExtensionsObject (&extenObj);
  
  return status;
}  /* end GetExtenObjFromAttribObj */

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