000022162 - How to convert UTCTime string to UNIT4 in RSA BSAFE Cert-C

Document created by RSA Customer Support Employee on Jun 16, 2016Last modified by RSA Customer Support Employee on Apr 21, 2017
Version 2Show Document
  • View in full screen mode

Article Content

Article Number000022162
Applies ToRSA BSAFE Cert-C
IssueHow to convert UTCTime string to UNIT4 in RSA BSAFE Cert-C
Resolution

To convert a UTCTime string (in the form of [local time +- difference from GMT], e.g. "491231235900+0000") to UNIT4, use the following code:

include <string.h>
#define OK 0;
#define ERROR -1;
typedef unsigned char UCHAR;

int Uto4(UINT4 *time, char *str)
{
  CERTC_CTX   certc_ctx = NULL;
  int         tag, sts;
  unsigned int tagClass;
  ITEM  derStr = {NULL, 0};
  unsigned char *outputDER;  
  unsigned int outputLen;


  if (C_InitializeCertC (NULL, NULL, 0, &certc_ctx) != 0) {
    return ERROR;
  }

  /********************************/
  /* String -> Octet String       */
  /********************************/
  if (C_DEREncodeString(
              certc_ctx,
              VT_OCTET_STRING,
              VTC_UNIVERSAL,
              (UCHAR *)str,
              strlen(str),
              &derStr.data,
              &derStr.len) != 0) {
    C_FinalizeCertC(&certc_ctx);
    return ERROR;
  }

  /********************************/
  /* UTCTime -> UINT4             */
  /********************************/
  if ((sts = C_BERDecodeUTCTime(
               certc_ctx,
               derStr.data,
               derStr.len,
               &tag,
               &tagClass,
               time)) != 0) {
    /* Release           */
    free(derStr.data);
    C_FinalizeCertC (&certc_ctx);
    return ERROR;
  }

  // CHECK
  /********************************/
  /* UINT4 -> UTCTime             */
  /********************************/
  if ((sts = C_DEREncodeUTCTime(
           certc_ctx,
           tag,
           tagClass,
           *time,
           &outputDER,                  
           &outputLen)) != 0) {
    /* Release           */
    C_FinalizeCertC (&certc_ctx);
    return ERROR;
  }
  RSA_PrintBuf("Encoded time", outputDER, outputLen);

  /* Release           */
  memset(derStr.data, 0, derStr.len);
  free(derStr.data);
  C_FinalizeCertC (&certc_ctx);

  return OK;
}

static int RSA_UTCLocalTimeToUint4 ()
{
  int status = 0;
  UINT4 time, time2;
  // char *str = "491231235900+0000";
  // char *str = "291231235900+0000";
  // char *str = "291231235900-0900";
  char *str = "291231235900+1300";

  do {
    status = Uto4(&time, str);
    if (status != 0)
      break;
    printf("Uint4 time is 0x%02x\n", time);

    RSA_PrintUint4Time ("Printable Uint4 time is", time);

    status = RSA_GetInputToUint4Time(&time2, "Enter time");

  } while (0);

  return (status);
}

Legacy Article IDa27114

Attachments

    Outcomes