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

📄 pgpx509cert.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        PGPASN_ERR(kPGPASNError_ErrPackBufferTooShort);
        return 0;
    }

    /* this is a SEQUENCE */
    bytesused = PGPASN_PutTag(buf, (unsigned char)(tag|0x20), datasize);
    if (bytesused != tagsize) {
        PGPASN_ERR(kPGPASNError_ErrPackOverrun);
        return bytesused;
    }
    datasize += tagsize;

  do {

    /* field cA of BasicConstraints */
    if (asnstruct->cA != NULL) { /* optional */
        bytesused += pgpasn_PackBOOLEANInternal(ctx, buf+bytesused, buflen-bytesused,
                          asnstruct->cA, PGPASN_ID_BOOLEAN, erret );
        if (bytesused > datasize || *erret != 0)
            break;
    }

    /* field pathLenConstraint of BasicConstraints */
    if (asnstruct->pathLenConstraint != NULL) { /* optional */
        bytesused += pgpasn_PackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
                          asnstruct->pathLenConstraint, PGPASN_ID_INTEGER, erret );
        if (bytesused > datasize || *erret != 0)
            break;
    }

  } while(0);

    if (bytesused < datasize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrPackUnderrun)
    else if (bytesused > datasize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrPackOverrun)

    return bytesused;
} /* pgpasn_PackBasicConstraintsInternal */

size_t pgpasn_UnpkInPlaceBasicConstraints(
    PGPASN_CONTEXT *ctx,
    PGPASN_BasicConstraints *asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused = 0;
    size_t datasize;
    size_t localsize;
    int indef = 0;

    PGPASN_TRACE_PRINT_FN((tag|0x20), 0x30, "SEQUENCE", "BasicConstraints" );

    if (erret == NULL) return 0; /* can't report errors */
    *erret = 0;

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }

    if (asnstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }

    if (buf == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoBlockPtr);
        return 0;
    }

    if (buflen <= 0) return 0; /* no error -- no block */

    if ( (*buf & 0xDF) != (tag & 0xDF) )
        return 0; /* no error code, just no block */
    if ( (*buf & 0x20) != 0x20) {
        PGPASN_ERR(kPGPASNError_ErrUnpackInvalidEncoding);
        return 0;
    }

    /* accept the tag byte */
    bytesused++;

    /* get the block length */
    bytesused += PGPASN_GetLength(buf+bytesused, &datasize);
    if ((int)datasize == -1) {
        localsize = buflen;
        indef = 1;
    }
    else {
        localsize = bytesused + datasize;
        if (localsize > buflen) {
            PGPASN_ERR(kPGPASNError_ErrUnpackOverrun);
            return 0;
        }
    }

    PGPASN_TRACE_INCR_LEVEL;
  do {

    /* field cA of BasicConstraints */
    if (!indef && bytesused >= localsize) {
        PGPASN_TRACE_DECR_LEVEL;
        return bytesused;
    }
    if (indef && *(buf+bytesused) == 0x00 &&
                 *(buf+bytesused+1) == 0x00) {
        PGPASN_TRACE_DECR_LEVEL;
        bytesused += 2;
        return bytesused;
    }
    if (asnstruct->cA != NULL)
        pgpasn_FreeBOOLEAN(ctx, asnstruct->cA);
    bytesused += pgpasn_UnpackBOOLEANInternal(ctx, &(asnstruct->cA),
                    buf+bytesused, localsize-bytesused, PGPASN_ID_BOOLEAN, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field pathLenConstraint of BasicConstraints */
    if (!indef && bytesused >= localsize) {
        PGPASN_TRACE_DECR_LEVEL;
        return bytesused;
    }
    if (indef && *(buf+bytesused) == 0x00 &&
                 *(buf+bytesused+1) == 0x00) {
        PGPASN_TRACE_DECR_LEVEL;
        bytesused += 2;
        return bytesused;
    }
    if (asnstruct->pathLenConstraint != NULL)
        pgpasn_FreeINTEGER(ctx, asnstruct->pathLenConstraint);
    bytesused += pgpasn_UnpackINTEGERInternal(ctx, &(asnstruct->pathLenConstraint),
                    buf+bytesused, localsize-bytesused, PGPASN_ID_INTEGER, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    if (indef) {
        if ( *(buf+bytesused) != 0x00 &&
             *(buf+bytesused+1) != 0x00 ) {
            PGPASN_ERR(kPGPASNError_ErrUnpackInvalidEncoding);
            break;
        }
        bytesused += 2;
    }
  } while(0);

    PGPASN_TRACE_DECR_LEVEL;
    if (bytesused > localsize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrUnpackOverrun);
    if (!indef && bytesused < localsize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrUnpackUnderrun);

    return bytesused;
} /* pgpasn_UnpkInPlaceBasicConstraints */

size_t pgpasn_UnpackBasicConstraintsInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_BasicConstraints **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PGPASN_BasicConstraints *local = NULL;

    if (erret == NULL) return 0;
    *erret = 0;

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    *asnstruct = NULL;

    if (buflen <= 0) return 0; /* no bytes left */

    if ( (*buf & 0xDF) != (tag & 0xDF) ) 
        return 0; /* not correct tag */

    local = pgpasn_NewBasicConstraints(ctx); /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceBasicConstraints(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeBasicConstraints(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* pgpasn_UnpackBasicConstraintsInternal */


/******************************************************************
 * Routines for CertificateValidityDate
 ******************************************************************/

size_t pgpasn_SizeofCertificateValidityDateInternal(
    PGPASN_CertificateValidityDate *asnstruct,
    int outerSizeFlag,
    int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    switch (asnstruct->CHOICE_field_type) {
      case PGPASN_ID_UTCTime:
      case 0x20|PGPASN_ID_UTCTime:
        body_size = pgpasn_SizeofUTCTimeInternal((PGPASN_UTCTime *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      case PGPASN_ID_GeneralizedTime:
      case 0x20|PGPASN_ID_GeneralizedTime:
        body_size = pgpasn_SizeofGeneralizedTimeInternal((PGPASN_GeneralizedTime *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      default:
        break;

    } /* switch */

    return (body_size);
} /* pgpasn_SizeofCertificateValidityDateInternal */

void pgpasn_DropInPlaceCertificateValidityDate(
    PGPASN_CONTEXT *ctx,
    PGPASN_CertificateValidityDate *f)
{
    if (ctx == NULL) return;
    if (f == NULL) return;

    switch(f->CHOICE_field_type) {

    case PGPASN_ID_UTCTime:
    case 0x20|PGPASN_ID_UTCTime:
        pgpasn_FreeUTCTime(ctx, (PGPASN_UTCTime *)( f->data ));
        break;
    case PGPASN_ID_GeneralizedTime:
    case 0x20|PGPASN_ID_GeneralizedTime:
        pgpasn_FreeGeneralizedTime(ctx, (PGPASN_GeneralizedTime *)( f->data ));
        break;
    default:
        break;
    } /* switch */

} /* pgpasn_DropInPlaceCertificateValidityDate */

size_t pgpasn_PackCertificateValidityDateInternal(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_CertificateValidityDate *asnstruct,
    unsigned char tag,
    int *erret)
{
    size_t bytesused = 0;
    size_t datasize;

    (void)tag; /* unused */

    if (erret == NULL) return 0; /* can't report errors */

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) return 0;

    /* lth of the block body */
    datasize = pgpasn_SizeofCertificateValidityDate(ctx, asnstruct, PGPASN_TRUE);
    if (datasize > buflen) {
        PGPASN_ERR(kPGPASNError_ErrPackBufferTooShort);
        return 0;
    }

    switch ( (asnstruct->CHOICE_field_type & 0xDF) ) {

    case PGPASN_ID_UTCTime:
        bytesused += pgpasn_PackUTCTimeInternal(ctx, buf+bytesused, buflen-bytesused,
                     (PGPASN_UTCTime *)(asnstruct->data),
                     PGPASN_ID_UTCTime,
                     erret);
        break;

    case PGPASN_ID_GeneralizedTime:
        bytesused += pgpasn_PackGeneralizedTimeInternal(ctx, buf+bytesused, buflen-bytesused,
                     (PGPASN_GeneralizedTime *)(asnstruct->data),
                     PGPASN_ID_GeneralizedTime,
                     erret);
        break;

    default:
        PGPASN_ERR( kPGPASNError_ErrChoiceBadType );
        break;
    } /* switch */

    if (bytesused < datasize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrPackUnderrun)
    else if (bytesused > datasize && *erret == 0)
        PGPASN_ERR(kPGPASNError_ErrPackOverrun)

  return bytesused;
} /* pgpasn_PackCertificateValidityDateInternal */

size_t pgpasn_UnpkInPlaceCertificateValidityDate(
     PGPASN_CONTEXT *ctx,
     PGPASN_CertificateValidityDate *asnstruct,/* output block */
     const unsigned char *buf,
     size_t buflen,
     unsigned char tag,
     int *erret)
{
    (void)tag; /* unused */


    if (erret == NULL) return 0; /* can't report errors */

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }

    if (buf == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoBlockPtr);
        return 0;
    }

    if (buflen <= 0) return 0; /* no error -- no block */

    switch (*buf) {

    /* utcTime */
    case PGPASN_ID_UTCTime:
    case 0x20|PGPASN_ID_UTCTime:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewUTCTime(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceUTCTime(ctx, (PGPASN_UTCTime *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_UTCTime, erret));
        /*NOTREACHED*/
        break;

    /* generalTime */
    case PGPASN_ID_GeneralizedTime:
    case 0x20|PGPASN_ID_GeneralizedTime:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewGeneralizedTime(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceGeneralizedTime(ctx, (PGPASN_GeneralizedTime *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_GeneralizedTime, erret));
        /*NOTREACHED*/
        break;

    default:
        PGPASN_ERR(kPGPASNError_ErrChoiceBadType);
        return 0;

    } /* switch */

} /* pgpasn_UnpkInPlaceCertificateValidityDate */

size_t pgpasn_UnpackCertificateValidityDateInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_CertificateValidityDate **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PGPASN_CertificateValidityDate *local = NULL ;

    if (erret == NULL) return 0;
    *erret = 0;

    if (ctx == NULL) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) {
        PGPASN_ERR(kPGPASNError_ErrUnpackNoStructure);
        return 0;
    }
    *asnstruct = NULL;

    if (buflen <= 0) return 0; /* no bytes left */

    local = pgpasn_NewCertificateValidityDate(ctx) ; /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceCertificateValidityDate(ctx, local, buf, buflen, tag, erret);
    if (*erret == kPGPASNError_ErrChoiceBadType) {
        *erret = 0;
        if (local != NULL) pgpasn_FreeCertificateValidityDate(ctx, local);
        return 0;
    }
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeCertificateValidityDate(ctx, local);
        return 0;
    }

    *asnstruct = local;
    return bytesused;
} /* pgpasn_UnpackCertificateValidityDateInternal */


/******************************************************************
 * Routines for ContentInfo
 ******************************************************************/

size_t pgpasn_SizeofContentInfoInternal(
    PGPASN_ContentInfo *asnstruct,
    int outerSizeFlag,
    int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

⌨️ 快捷键说明

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