⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 si_crpto.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:

  return TRUE;
}

static INT16
wap_encode_signed_content (wap_cvt_t *obj, SignedContent *p)
{
  if (!wap_cvt_uint8 (obj, &p->version) ||
      !wap_encode_signature (obj, &(p->signature)) ||
      !wap_encode_signer_infos (obj, p->signerInfo) ||
      !wap_encode_content_info (obj, &(p->contentInfo)) ||
      !wap_encode_authenticated_attribute (obj,
                                           &(p->authenticatedAttribute))) {
    return FALSE;
  }

  return TRUE;
}


/************************************************************
 * The public routines of this module:
 *   ST_createDigestInfo
 *   ST_encodeSignedContent
 *   ST_newSignedContent
 *   ST_freeSignedContent
 *
 ************************************************************/

#ifdef SIGN_TEXT_FIXED

#define HASHBUF_UMT_LENGTH          95
#define UMT_LENGTH                  13
#define UMT_START_POS               45

static BYTE hashbuf_umt[HASHBUF_UMT_LENGTH] = {
  0x31, 0x5d,
  0x30, 0x18,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03,
  0x31, 0x0b,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
  0x30, 0x1c,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05,
  0x31, 0x0f,
  0x17, 0x0d, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0x30, 0x23,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04,
  0x31, 0x16,
  0x04, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};


#define HASHBUF_NONCE_LENGTH        91
#define NONCE_LENGTH                 8
#define NONCE_START_POS             46

static BYTE hashbuf_nonce[HASHBUF_NONCE_LENGTH] = {
  0x31, 0x59,
  0x30, 0x18,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03,
  0x31, 0x0b,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
  0x30, 0x18,
  0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x19, 0x03,
  0x31, 0x0a,
  0x04, 0x08, 0, 0, 0, 0, 0, 0, 0, 0,
  0x30, 0x23,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04,
  0x31, 0x16,
  0x04, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

#else
#define HASHBUF_UMT_LENGTH          89
#define UMT_LENGTH                  13
#define UMT_START_POS               41

static BYTE hashbuf_umt[HASHBUF_UMT_LENGTH] = {
  0x31, 0x57,
  0x30, 0x16,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
  0x30, 0x1a,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05,
  0x17, 0x0d, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0x30, 0x21,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04,
  0x04, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};


#define HASHBUF_NONCE_LENGTH        85
#define NONCE_LENGTH                 8
#define NONCE_START_POS             42

static BYTE hashbuf_nonce[HASHBUF_NONCE_LENGTH] = {
  0x31, 0x57,
  0x30, 0x16,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
  0x30, 0x16,
  0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x19, 0x03,
  0x04, 0x08, 0, 0, 0, 0, 0, 0, 0, 0,
  0x30, 0x21,
  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04,
  0x04, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

#endif

const static BYTE digestinfo_prefix[15] = {
  0x30, 0x21,
  0x30, 0x09,
  0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  0x05, 0x00,
  0x04, 0x14
};

/*
 * ST_createDigestInfo
 *
 * Prepare the value to be encrypted given a buffer of data.
 * The hash is formatted in a DER-encoded structure compatible
 * with PKCS#7. See WMLScript Crypto Library Specification, section 6.2.
 *
 * Parameters:
 *   buf        the data to be signed, encoded as UTF-8
 *   buflen     length of data
 *   digestinfo where the result is placed (usually 35 bytes)
 *   diglen     on input, the length of the digestinfo buffer; on output,
 *              the number of bytes placed in digestinfo
 *   options    as received from the signText function
 *   sc         a pointer to a structure of type SignedContent;
 *              this routine will store values in this structure,
 *              to be passed to the routine ST_packResult later.
 *
 * NOTE: caller should NOT deallocate the buffer "buf" explicitly.
 * It should be released by a call to ST_freeSignedContent after
 * ST_encodeSignedContent has used it.
 *
 * Return value: 0 if OK, -1 if an error occurred.
 */
INT16
ST_createDigestInfo (BYTE *buf, UINT16 buflen,
                     BYTE *digestinfo, UINT16 *diglen,
                     UINT8 options,
                     SignedContent *sc)
{
  UINT16 len;
  BYTE   *hashbuf;

  if ((buf == NULL) || (digestinfo == NULL) ||
      (*diglen < 35) || (sc == NULL)) {
    return -1;
  }

  if (cfg_wae_ua_current_time_is_gmt) {
    len = HASHBUF_UMT_LENGTH;
    hashbuf = hashbuf_umt;
    /* Write UTC time into hashbuf[41..53] */
    Time2String (CLNTa_currentTime (), (CHAR *)(hashbuf_umt + UMT_START_POS));
    hashbuf_umt[UMT_START_POS + UMT_LENGTH - 1] = 'Z';
    sc->authenticatedAttribute.attributeType = ATTRIBUTETYPE_GMT_UTC_TIME;
    B_COPYSTRINGN (sc->authenticatedAttribute._u.gmtUTCTime,
                   hashbuf_umt + UMT_START_POS, UMT_LENGTH - 1);
  }
  else {
    len = HASHBUF_NONCE_LENGTH;
    hashbuf = hashbuf_nonce;
    /* Write random number into hashbuf[42..49] */
    CRYPTa_generateRandom (hashbuf_nonce + NONCE_START_POS, NONCE_LENGTH);
    sc->authenticatedAttribute.attributeType = ATTRIBUTETYPE_SIGNER_NONCE;
    B_COPYSTRINGN (sc->authenticatedAttribute._u.signerNonce,
                   hashbuf_nonce + NONCE_START_POS, NONCE_LENGTH);
  }

  /* Store content */
  sc->contentInfo.contentType = CONTENTTYPE_TEXT;
  sc->contentInfo.contentEncoding = IANA_CHARSET_UTF8;
  if (options & 0x01) {
    sc->contentInfo.contentPresent = 1;
  }
  else {
    sc->contentInfo.contentPresent = 0;
  }    
  sc->contentInfo.contentLen = buflen;
  sc->contentInfo.content = buf;

  /* Compute SHA-1 hash of given data *
   * Write computed hash into hashbuf */
  *diglen = 20;
  if (CRYPTa_hash (HASH_SHA, buf, buflen, hashbuf + len - 20) != CRV_OK) {
    *diglen = 0;
    sc->contentInfo.content = NULL;
    return -1;
  }

  /* Calculate SHA-1 hash of the hashbuf */
  B_COPYSTRINGN (digestinfo, digestinfo_prefix, 15);
  *diglen = 20;
  if (CRYPTa_hash (HASH_SHA, hashbuf, len, digestinfo + 15) != CRV_OK) {
    *diglen = 15;
    sc->contentInfo.content = NULL;
    return -1;
  }
  
  *diglen = 35;
  return 0;
}


/*
 * ST_encodeSignedContent
 *
 * Given the contents of SignedContent,
 * computed the base-64-encoding of that structure.
 *
 * Parameters:
 *   sc        the SignedContent structure
 *   result    a pointer to a newly allocated string will be placed here,
 *             holding the result: a null-terminated byte string
 *   reslen    the length of the result string
 *
 * NOTE: it is the caller's responsibility to deallocate the result string.
 *
 * Return value: 0 if OK, -1 if an error occurred.
 */
INT16
ST_encodeSignedContent (SignedContent *sc,
                        BYTE **result, UINT16 *reslen)
{
  wap_cvt_t cvt_obj;
  BYTE     *buf;
  UINT16    buflen;

  wap_cvt_init (&cvt_obj, WAP_CVT_ENCODE_SIZE, NULL, 0);
  if (!wap_encode_signed_content (&cvt_obj, sc))
    return -1;
  buflen = (UINT16)cvt_obj.pos;
  buf = OSConnectorAlloc (buflen);
  if (buf == NULL) {
    return -1;
  }

  wap_cvt_init (&cvt_obj, WAP_CVT_ENCODE, buf, buflen);
  if (!wap_encode_signed_content (&cvt_obj, sc)) {
    OSConnectorFree (buf);
    return -1;
  }

  if (Base64Encoding (buf, buflen, result, reslen) < 0) {
    OSConnectorFree (buf);
    return -1;
  }

  OSConnectorFree (buf);

  return 0;
}

/*
 * ST_newSignedContent
 *
 * Allocate and initialize a new SignedContent structure.
 * Returns NULL in case of error.
 */
SignedContent *
ST_newSignedContent (void)
{
  SignedContent *sc = NEWARRAY (SignedContent, 1);

  if (sc == NULL) {
    return NULL;
  }

  sc->version = 1;

  sc->signature.algorithm = SIGALG_NULL;
  sc->signature.siglen = 0;
  sc->signature.sig = NULL;

  sc->signerInfo[0].signerInfoType = SIGNERINFO_IMPLICIT;
  sc->signerInfo[0].infoLen = 0;
  sc->signerInfo[0].info = NULL;
  sc->signerInfo[1].signerInfoType = SIGNERINFO_IMPLICIT;
  sc->signerInfo[1].infoLen = 0;
  sc->signerInfo[1].info = NULL;

  sc->contentInfo.contentType = 0;
  sc->contentInfo.contentEncoding = 0;
  sc->contentInfo.contentPresent = 0;
  sc->contentInfo.contentLen = 0;
  sc->contentInfo.content = NULL;

  sc->authenticatedAttribute.attributeType = 0;

  return sc;
}


/*
 * ST_freeSignedContent
 *
 * Deallocate all relevant fields in the SignedContent structure,
 * as well as the SignedContent structure itself.
 */
void
ST_freeSignedContent (SignedContent *sc)
{
  DEALLOC (&(sc->signature.sig));

  if (sc->signerInfo[0].signerInfoType != SIGNERINFO_IMPLICIT) {
    DEALLOC (&(sc->signerInfo[0].info));
  }
  if (sc->signerInfo[1].signerInfoType != SIGNERINFO_IMPLICIT) {
    DEALLOC (&(sc->signerInfo[1].info));
  }

  DEALLOC (&(sc->contentInfo.content));

  DEALLOC (&sc);
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -