RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

cftest.c

Change the information contained in a CERT_OBJ. The resulting certificate will be invalid. Can be used as a template to "tamper" with certificates to generate invalid certificates for testing purposes.

/* $Id: cftest.c,v 1.3 2004/03/02 05:18:35 gsingh Exp $ */
/* cftest.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 is just a simple testcase demonstrating how to change the
** information contained in a CERT_OBJ.  Note that the resulting
** certificate will not be valid, since we do not create a new
** signature!  This can be used as a template to "tamper" with certs
** to generate certificates which should be caught as invalid for
** testing purposes.
**
** 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 "certutil.h"

int main (int argc, char *argv[])
{
  int status = 0;
  CERTC_CTX ctx = NULL;
  ITEM certBer = {NULL, 0};
  CERT_FIELDS certFields;
  CERT_OBJ certObj = 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 ("Cert Fields Manipulation Test\n");
  RSA_PrintMessage ("=============================\n");

  status = RSA_GetFileToAllocBuffer (&certBer.data, &certBer.len,
                                     "Enter name of cert binary");
  if (status != 0)
    goto CLEANUP;

  status = C_CreateCertObject (&certObj, ctx);
  if (status != 0)
    goto CLEANUP;

  status = C_SetCertBER (certObj, certBer.data, certBer.len);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("***Original Cert\n");
  status = RSA_PrintCertObject (certObj);
  if (status != 0)
    goto CLEANUP;

  status = C_GetCertFields (certObj, &certFields);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("Adding to Issuer name...\n");
  status = RSA_GetInputToNameObject (certFields.issuerName);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("Setting Validity End to current time...\n");
  T_time (&certFields.validity.end);

  RSA_PrintMessage ("Changing Subject name...\n");
  C_ResetNameObject (certFields.subjectName);

  /* RSA_GetInputToNameObject is described in
     samples/common/include/certutil.h */
  status = RSA_GetInputToNameObject (certFields.subjectName);
  if (status != 0)
    goto CLEANUP;

  status = C_SetCertFields (certObj, &certFields);
  if (status != 0)
    goto CLEANUP;

  RSA_PrintMessage ("***Cert after C_SetCertFields\n");

  /* RSA_PrintCertObject is described in samples/common/include/certutil.h */
  status = RSA_PrintCertObject (certObj);
  if (status != 0)
    goto CLEANUP;

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

  T_free (certBer.data);
  C_DestroyCertObject (&certObj);
  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