RSA BSAFE Cert-C

Certificate Components for C

Crypto-C 6.2.1 Developer's Guide
Search

p10util.c

Prints information contained in a PKCS #10 certificate request object in a readable manner. Gathers user input to place into a PKCS #10 certificate request.

/* $Id: p10util.c,v 1.3 2004/03/02 05:18:38 gsingh Exp $ */
/* p10util.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 contains routines that are used to print the information
** contained in a PKCS #10 request object in a readable manner, as well as
** routines used to gather user input to place into a cert request object.
*/

#include "p10util.h"
#include "keyutil.h"

int RSA_PrintPkcs10Object (PKCS10_OBJ pkcs10Obj)
{
  int status = 0;
  
  PKCS10_FIELDS pkcs10Fields;

  status = C_GetPKCS10Fields (pkcs10Obj, &pkcs10Fields);
  if (status != 0)
    goto CLEANUP;

  switch (pkcs10Fields.version) {
    case PKCS10_VERSION_1:
      RSA_PrintMessage ("PKCS #10 Version 1\n");
      break;
    default:
      RSA_PrintMessage ("Unknown version: %u\n", pkcs10Fields.version);
  }

  RSA_PrintNameObject ("Subject Name", pkcs10Fields.subjectName);
  RSA_PrintBuf ("Public Key BER", pkcs10Fields.publicKey.data,
                pkcs10Fields.publicKey.len);
  RSA_PrintAttributesObject (NULL, pkcs10Fields.attribute);

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

  return status;
}  /* end RSA_PrintPkcs10Object */

int RSA_GetInputToPkcs10Object (PKCS10_OBJ pkcs10Obj, CERTC_CTX ctx)
{
  int status = 0;
  
  PKCS10_FIELDS pkcs10Fields;
  B_KEY_OBJ publicKey = NULL;
  B_KEY_OBJ privateKey = NULL;

  pkcs10Fields.subjectName = NULL;
  pkcs10Fields.attribute = NULL;
  pkcs10Fields.publicKey.data = NULL;

  pkcs10Fields.version = PKCS10_VERSION_1;

  status = C_CreateNameObject (&pkcs10Fields.subjectName);
  if (status != 0)
    goto CLEANUP;
  
  status = RSA_GetNameObject (pkcs10Fields.subjectName, "subject");
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetFileToAllocBuffer
           (&pkcs10Fields.publicKey.data, &pkcs10Fields.publicKey.len,
            "Enter name of file containing public key BER (blank to create)");
  if (status == RSA_DEMO_E_CANCEL) {
    status = RSA_GenerateKeypair (ctx, &publicKey, &privateKey);
    if (status != 0)
      goto CLEANUP;

    status = RSA_GetKeyBer (RSA_DEMO_PUBLIC_KEY, publicKey,
                            &pkcs10Fields.publicKey);
  }
  if (status != 0)
    goto CLEANUP;

  status = C_CreateAttributesObject (&pkcs10Fields.attribute);
  if (status != 0)
    goto CLEANUP;

  status = RSA_GetAttributesObject (pkcs10Fields.attribute);
  if (status != 0)
    goto CLEANUP;

  pkcs10Fields.reserved = NULL_PTR;

  status = C_SetPKCS10Fields (pkcs10Obj, &pkcs10Fields);
  if (status != 0)
    goto CLEANUP;

  if (privateKey == NULL) {
    status = RSA_GetKeyObjFromFile (RSA_DEMO_PRIVATE_KEY, &privateKey);
    if (status != 0)
      goto CLEANUP;
  }

  status = RSA_SignPkcs10Obj (pkcs10Obj, privateKey);
  
CLEANUP:
  if (status != 0)
    RSA_PrintError ("RSA_GetInputToPkcs10Object", status);

  C_DestroyNameObject (&pkcs10Fields.subjectName);
  B_DestroyKeyObject (&publicKey);
  B_DestroyKeyObject (&privateKey);
  C_DestroyAttributesObject (&pkcs10Fields.attribute);
  T_free (pkcs10Fields.publicKey.data);
  
  return status;
}  /* end RSA_GetInputToPkcs10Object */

int RSA_SignPkcs10Obj (PKCS10_OBJ pkcs10Obj, B_KEY_OBJ privateKey)
{
  int status = 0, sa = 0;
  ALGORITHM_IDENTIFIER algId;

  status = RSA_ChooseSignatureAlgorithmPrompt (&algId);
  if (status != 0)
    goto CLEANUP;

  sa = algId.algorithmId;
  
  status = C_SignPKCS10 (pkcs10Obj, privateKey, sa);

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

  return status;
}  /* end RSA_SignPkcs10Obj */

int RSA_GetKeyFromPkcs10 (PKCS10_OBJ pkcs10Obj, B_KEY_OBJ *publicKey)
{
  int status = 0;
  
  PKCS10_FIELDS pkcs10Fields;

  *publicKey = NULL;

  status = C_GetPKCS10Fields (pkcs10Obj, &pkcs10Fields);
  if (status != 0)
    goto CLEANUP;

  status = B_CreateKeyObject (publicKey);
  if (status != 0)
    goto CLEANUP;

  status = RSA_SetKeyBer (RSA_DEMO_PUBLIC_KEY, *publicKey,
                          pkcs10Fields.publicKey);

CLEANUP:
  if (status != 0) {
    B_DestroyKeyObject (publicKey);
    RSA_PrintError ("RSA_GetKeyFromPkcs10", status);
  }

  return status;
}  /* end RSA_GetKeyFromPkcs10 */

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