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

📄 pkcs7.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (bytesused > localsize || *erret != 0)
        break;

    /* field encryptedContent of EncryptedContentInfo */
    if (!indef && bytesused >= localsize) {
        PKITRACE_DECR_LEVEL;
        return bytesused;
    }
    if (indef && *(buf+bytesused) == 0x00 &&
                 *(buf+bytesused+1) == 0x00) {
        PKITRACE_DECR_LEVEL;
        bytesused += 2;
        return bytesused;
    }
    if (asnstruct->encryptedContent != NULL)
        PKIFreeEncryptedContent(ctx, asnstruct->encryptedContent);
    bytesused += PKIUnpackEncryptedContentInternal(ctx, &(asnstruct->encryptedContent),
                 buf+bytesused, localsize-bytesused,
                 0x80 | 0x00, erret);
    if (bytesused > localsize || *erret != 0)
        break;

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

    PKITRACE_DECR_LEVEL;
    if (bytesused > localsize && *erret == 0)
        PKIERR(PKIErrUnpackOverrun);
    if (!indef && bytesused < localsize && *erret == 0)
        PKIERR(PKIErrUnpackUnderrun);

    return bytesused;
} /* PKIUnpkInPlaceEncryptedContentInfo */

size_t PKIUnpackEncryptedContentInfoInternal(
    PKICONTEXT *ctx,
    PKIEncryptedContentInfo **asnstruct,
    unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PKIEncryptedContentInfo *local = NULL;

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

    if (ctx == NULL) {
        PKIERR(PKIErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) {
        PKIERR(PKIErrUnpackNoStructure);
        return 0;
    }
    *asnstruct = NULL;

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

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

    local = PKINewEncryptedContentInfo(ctx);	/* carve a block for it */
    bytesused = PKIUnpkInPlaceEncryptedContentInfo(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        PKIFreeEncryptedContentInfo(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* PKIUnpackEncryptedContentInfoInternal */


/******************************************************************
 * Routines for ExtendedCertificate
 ******************************************************************/

size_t PKISizeofExtendedCertificateInternal(PKIExtendedCertificate *asnstruct, int outerSizeFlag, int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    body_size =
            PKISizeofExtendedCertificateInfoInternal(&asnstruct->extendedCertificateInfo, PKITRUE, PKIFALSE)
          + PKISizeofSignatureAlgorithmIdentifierInternal(&asnstruct->signatureAlgorithm, PKITRUE, PKIFALSE)
          + PKISizeofSignatureInternal(&asnstruct->signature, PKITRUE, PKIFALSE) ;

    if (outerSizeFlag == PKITRUE)
        body_size = PKITagged(body_size, 1);

    if (expTaggedFlag == PKITRUE)
        body_size = PKITagged(body_size, 1); /* this is seq like */

    return body_size;

} /* PKISizeofExtendedCertificateInternal */

void PKIDropInPlaceExtendedCertificate(
    PKICONTEXT *ctx,
    PKIExtendedCertificate *f)
{
    if (ctx == NULL)
        return;

    if (f == NULL) return ;
    PKIDropInPlaceExtendedCertificateInfo(ctx, &(f->extendedCertificateInfo));
    PKIDropInPlaceSignatureAlgorithmIdentifier(ctx, &(f->signatureAlgorithm));
    PKIDropInPlaceSignature(ctx, &(f->signature));
} /* PKIDropInPlaceExtendedCertificate */

size_t PKIPackExtendedCertificateInternal(
    PKICONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PKIExtendedCertificate *asnstruct,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    size_t tagsize;
    size_t datasize;

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

    if (ctx == NULL) {
        PKIERR(PKIErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) return 0;

    /* lth of the block body */
    datasize = PKISizeofExtendedCertificate(ctx, asnstruct, PKIFALSE);
    tagsize = 1 + PKILengthSize(datasize);
    if (datasize+tagsize > buflen) {
        PKIERR(PKIErrPackBufferTooShort);
        return 0;
    }

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

  do {

    /* field extendedCertificateInfo of ExtendedCertificate */
    bytesused += PKIPackExtendedCertificateInfoInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->extendedCertificateInfo), PKIID_ExtendedCertificateInfo, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field signatureAlgorithm of ExtendedCertificate */
    bytesused += PKIPackSignatureAlgorithmIdentifierInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->signatureAlgorithm), PKIID_SignatureAlgorithmIdentifier, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field signature of ExtendedCertificate */
    bytesused += PKIPackSignatureInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->signature), PKIID_Signature, erret);
    if (bytesused > datasize || *erret != 0)
        break;

  } while(0);

    if (bytesused < datasize && *erret == 0)
        PKIERR(PKIErrPackUnderrun)
    else if (bytesused > datasize && *erret == 0)
        PKIERR(PKIErrPackOverrun)

    return bytesused;
} /* PKIPackExtendedCertificateInternal */

size_t PKIUnpkInPlaceExtendedCertificate(
    PKICONTEXT *ctx,
    PKIExtendedCertificate *asnstruct,
    unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused = 0;
    size_t datasize;
    size_t localsize;
    int indef = 0;

    PKITRACE_PRINT_FN((tag|0x20), 0x30, "SEQUENCE", "ExtendedCertificate" );

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

    if (ctx == NULL) {
        PKIERR(PKIErrBadContext);
        return 0;
    }

    if (asnstruct == NULL) {
        PKIERR(PKIErrUnpackNoStructure);
        return 0;
    }

    if (buf == NULL) {
        PKIERR(PKIErrUnpackNoBlockPtr);
        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) {
        PKIERR(PKIErrUnpackInvalidEncoding);
        return 0;
    }

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

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

    PKITRACE_INCR_LEVEL;
  do {

    /* field extendedCertificateInfo of ExtendedCertificate */
    bytesused += PKIUnpkInPlaceExtendedCertificateInfo(ctx, &(asnstruct->extendedCertificateInfo), buf+bytesused,
                        localsize-bytesused, PKIID_ExtendedCertificateInfo, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field signatureAlgorithm of ExtendedCertificate */
    bytesused += PKIUnpkInPlaceSignatureAlgorithmIdentifier(ctx, &(asnstruct->signatureAlgorithm), buf+bytesused,
                        localsize-bytesused, PKIID_SignatureAlgorithmIdentifier, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field signature of ExtendedCertificate */
    bytesused += PKIUnpkInPlaceSignature(ctx, &(asnstruct->signature), buf+bytesused,
                        localsize-bytesused, PKIID_Signature, erret);
    if (bytesused > localsize || *erret != 0)
        break;

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

    PKITRACE_DECR_LEVEL;
    if (bytesused > localsize && *erret == 0)
        PKIERR(PKIErrUnpackOverrun);
    if (!indef && bytesused < localsize && *erret == 0)
        PKIERR(PKIErrUnpackUnderrun);

    return bytesused;
} /* PKIUnpkInPlaceExtendedCertificate */

size_t PKIUnpackExtendedCertificateInternal(
    PKICONTEXT *ctx,
    PKIExtendedCertificate **asnstruct,
    unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PKIExtendedCertificate *local = NULL;

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

    if (ctx == NULL) {
        PKIERR(PKIErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) {
        PKIERR(PKIErrUnpackNoStructure);
        return 0;
    }
    *asnstruct = NULL;

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

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

    local = PKINewExtendedCertificate(ctx);	/* carve a block for it */
    bytesused = PKIUnpkInPlaceExtendedCertificate(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        PKIFreeExtendedCertificate(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* PKIUnpackExtendedCertificateInternal */


/******************************************************************
 * Routines for RecipientInfo
 ******************************************************************/

size_t PKISizeofRecipientInfoInternal(PKIRecipientInfo *asnstruct, int outerSizeFlag, int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    body_size =
            PKISizeofVersionInternal(&asnstruct->version, PKITRUE, PKIFALSE)
          + PKISizeofIssuerAndSerialNumberInternal(&asnstruct->issuerAndSerialNumber, PKITRUE, PKIFALSE)
          + PKISizeofKeyEncryptionAlgorithmIdentifierInternal(&asnstruct->keyEncryptionAlgorithm, PKITRUE, PKIFALSE)
          + PKISizeofEncryptedKeyInternal(&asnstruct->encryptedKey, PKITRUE, PKIFALSE) ;

    if (outerSizeFlag == PKITRUE)
        body_size = PKITagged(body_size, 1);

    if (expTaggedFlag == PKITRUE)
        body_size = PKITagged(body_size, 1); /* this is seq like */

    return body_size;

} /* PKISizeofRecipientInfoInternal */

void PKIDropInPlaceRecipientInfo(
    PKICONTEXT *ctx,
    PKIRecipientInfo *f)
{
    if (ctx == NULL)
        return;

    if (f == NULL) return ;
    PKIDropInPlaceVersion(ctx, &(f->version));
    PKIDropInPlaceIssuerAndSerialNumber(ctx, &(f->issuerAndSerialNumber));
    PKIDropInPlaceKeyEncryptionAlgorithmIdentifier(ctx, &(f->keyEncryptionAlgorithm));
    PKIDropInPlaceEncryptedKey(ctx, &(f->encryptedKey));
} /* PKIDropInPlaceRecipientInfo */

size_t PKIPackRecipientInfoInternal(
    PKICONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PKIRecipientInfo *asnstruct,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    size_t tagsize;
    size_t datasize;

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

    if (ctx == NULL) {
        PKIERR(PKIErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) return 0;

    /* lth of the block body */
    datasize = PKISizeofRecipientInfo(ctx, asnstruct, PKIFALSE);
    tagsize = 1 + PKILengthSize(datasize);
    if (datasize+tagsize > buflen) {
        PKIERR(PKIErrPackBufferTooShort);
        return 0;
    }

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

  do {

    /* field version of RecipientInfo */
    bytesused += PKIPackVersionInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->version), PKIID_Version, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field issuerAndSerialNumber of RecipientInfo */
    bytesused += PKIPackIssuerAndSerialNumberInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->issuerAndSerialNumber), PKIID_IssuerAndSerialNumber, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field keyEncryptionAlgorithm of RecipientInfo */
    bytesused += PKIPackKeyEncryptionAlgorithmIdentifierInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->keyEncryptionAlgorithm), PKIID_KeyEncryptionAlgorithmIdentifier, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field encryptedKey of RecipientInfo */
    bytesused += PKIPackEncryptedKeyInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->encryptedKey), PKIID_EncryptedKey, erret);
    if (bytesused > datasize || *erret != 0)
        break;

  } while(0);

    if (bytesused < datasize && *erret == 0)
        PKIERR(PKIErrPackUnderrun)
    else if (bytesused > datasize && *erret == 0)
        PKIERR(PKIErrPackOverrun)

    return bytesused;
} /* PKIPackRecipientInfoInternal */

size_t PKIUnpkInPlaceRecipientInfo(

⌨️ 快捷键说明

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