RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

name.c

Uses functions in nameutil for parsing and generating a Name object. Can be used to create Name objects that are incorporated into a certificate request or a certificate.

/* $Id: name.c,v 1.4 2004/03/02 05:18:41 gsingh Exp $ */
/* name.c
** Copyright (c) 1999-2003, 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
** samples/common/include/nameutil.h to parse and create name 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 "nameutil.h"

static int DisplayNameObj (void);
static int GenerateNameObj (void);

/*  Set reverse flag to 1 to use C_GetNameStringReverse()  */
static int DisplayRfc2253Name (int reverseFlag);

static int AddEntriesToNameObj (void);

static int GenerateNameFromString (void);

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

  FILE_LOG_PARAMS logParams = {(char *)0, (char *)0};
  SERVICE_HANDLER logHandler = {
    SPT_LOG, "Default File Log", S_InitializeFileLog
  };

  status = RSA_SetOptions (&logParams, argc, argv);
  if (status != 0)
    goto CLEANUP;
  
  status = C_InitializeCertC ((SERVICE_HANDLER *)0, (unsigned char **)0, 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 ("Name Object Demonstration\n");
  RSA_PrintMessage ("=========================\n");

  for (;;) {
    RSA_PrintMessage ("\nName Object Operations\n");
    RSA_PrintMessage ("  A - Display name object from a file\n");
    RSA_PrintMessage ("  B - Generate name object\n");
    RSA_PrintMessage ("  C - Display name in RFC 2253 format\n");
    RSA_PrintMessage ("  D - Display name in reverse RFC 2253 order\n");
    RSA_PrintMessage ("  E - Add attributes to existing name object\n");
    RSA_PrintMessage ("  F - Generate name object from RFC 2253 string\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 = DisplayNameObj ();
        break;
      case 'b':
      case 'B':
        status = GenerateNameObj ();
        break;
      case 'c':
      case 'C':
        status = DisplayRfc2253Name (0);
        break;
      case 'd':
      case 'D':
        status = DisplayRfc2253Name (1);
        break;
      case 'e':
      case 'E':
        status = AddEntriesToNameObj ();
        break;
      case 'f':
      case 'F':
        status = GenerateNameFromString ();
        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 ("name.c", status);

  C_FinalizeCertC (&ctx);

  return status;
}  /* end main */

static int DisplayNameObj ()
{
  int status = 0;
  
  unsigned char *nameBer = NULL_PTR;
  unsigned int nameBerLen = 0;
  
  NAME_OBJ nameObj = NULL_PTR;

  status = RSA_GetFileToAllocBuffer (&nameBer, &nameBerLen,
                                     "Enter name of name object binary");
  if (status != 0)
    goto CLEANUP;

  status = C_CreateNameObject (&nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_SetNameBER (nameObj, nameBer, nameBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_PrintNameObject ("\nName Object Contents", nameObj);
  if (status != 0)
    goto CLEANUP;

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

  C_DestroyNameObject (&nameObj);
  T_free (nameBer);

  return status;
}  /* end DisplayNameObj */

static int GenerateNameObj ()
{
  int status = 0;

  NAME_OBJ nameObj = NULL_PTR;

  unsigned char *nameBer = NULL_PTR;
  unsigned int nameBerLen = 0;

  status = C_CreateNameObject (&nameObj);
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetInputToNameObject (nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_GetNameDER (nameObj, &nameBer, &nameBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_WriteDataToFile
           (nameBer, nameBerLen,
            "Enter file name to store name object binary");
  if (status != 0)
    goto CLEANUP;
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("GenerateNameObj", status);

  C_DestroyNameObject (&nameObj);
  
  return status;
}  /* end GenerateNameObj */

static int DisplayRfc2253Name (int reverseFlag)
{
  int status = 0;
  char *nameString = (char *)0;
  unsigned char *nameBer = (unsigned char *)0;
  unsigned int nameBerLen = 0;
  
  NAME_OBJ nameObj = NULL_PTR;

  status = RSA_GetFileToAllocBuffer (&nameBer, &nameBerLen,
                                     "Enter name of name object binary");
  if (status != 0)
    goto CLEANUP;

  status = C_CreateNameObject (&nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_SetNameBER (nameObj, nameBer, nameBerLen);
  if (status != 0)
    goto CLEANUP;

  if (reverseFlag == 0)
    status = C_GetNameString (nameObj, &nameString);
  else
    status = C_GetNameStringReverse (nameObj, &nameString);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("RFC 2253 Representation of name object contents\n");
  RSA_PrintMessage ("  %s\n", nameString);

CLEANUP:
  if (status != 0)
    RSA_PrintError ("DisplayRfc2253Name", status);
  
  C_DestroyNameObject (&nameObj);
  T_free (nameBer);

  return status;
}  /* end DisplayRfc2253Name */

static int AddEntriesToNameObj ()
{
  int status = 0;
  
  unsigned char *nameBer = (unsigned char *)0, *newNameBer = (unsigned char *)0;
  unsigned int nameBerLen = 0, newNameBerLen = 0;
  
  NAME_OBJ nameObj = NULL_PTR;

  status = RSA_GetFileToAllocBuffer (&nameBer, &nameBerLen,
                                     "Enter name of name object binary");
  if (status != 0)
    goto CLEANUP;

  status = C_CreateNameObject (&nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_SetNameBER (nameObj, nameBer, nameBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_PrintNameObject ("\nExisting Name Object Contents", nameObj);
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetInputToNameObject (nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_GetNameDER (nameObj, &newNameBer, &newNameBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_WriteDataToFile
           (newNameBer, newNameBerLen,
            "Enter file name to store new name object binary");
  if (status != 0)
    goto CLEANUP;
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("AddEntriesToNameObj", status);

  C_DestroyNameObject (&nameObj);
  T_free (nameBer);

  return status;
}  /* end AddEntriesToNameObj */

static int GenerateNameFromString ()
{
  int status;
  char userInput[RSA_DEMO_MAX_LINE_LEN];

  NAME_OBJ nameObj = (NAME_OBJ)NULL_PTR;
  unsigned char *nameBer;
  unsigned int nameBerLen;

  status = C_CreateNameObject (&nameObj);
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetCommand (userInput, sizeof (userInput),
                           "Enter string to convert to name object");
  if (status != 0)
    goto CLEANUP;

  status = C_SetNameString (nameObj, userInput);
  if (status != 0)
    goto CLEANUP;

  status = RSA_PrintNameObject ("Generated Name Object", nameObj);
  if (status != 0)
    goto CLEANUP;

  status = C_GetNameDER (nameObj, &nameBer, &nameBerLen);
  if (status != 0)
    goto CLEANUP;

  status = RSA_WriteDataToFile
           (nameBer, nameBerLen,
            "Enter file name to store new name object binary");
  if (status != 0)
    goto CLEANUP;

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

  return status;
}  /* end GenerateNameFromString */

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