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

📄 cert.c

📁 PGP—Pretty Good Privacy
💻 C
📖 第 1 页 / 共 5 页
字号:
    if ((int)datasize == -1) {
        localsize = buflen;
        indef = 1;
    }
    else {
        localsize = bytesused + datasize;
        if (localsize > buflen) {
            PKIERR(PKIErrUnpackOverrun);
            return 0;
        }
    }

    PKITRACE_INCR_LEVEL;
  do {

    /* field type of AttributeTypeAndValue */
    bytesused += PKIUnpkInPlaceAttributeType(ctx, &(asnstruct->type), buf+bytesused,
                        localsize-bytesused, PKIID_AttributeType, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field value of AttributeTypeAndValue */
    bytesused += PKIUnpkInPlaceAttributeValue(ctx, &(asnstruct->value), buf+bytesused,
                        localsize-bytesused, PKIID_AttributeValue, 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;
} /* PKIUnpkInPlaceAttributeTypeAndValue */

size_t PKIUnpackAttributeTypeAndValueInternal(
    PKICONTEXT *ctx,
    PKIAttributeTypeAndValue **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PKIAttributeTypeAndValue *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 = PKINewAttributeTypeAndValue(ctx); /* carve a block for it */
    bytesused = PKIUnpkInPlaceAttributeTypeAndValue(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) PKIFreeAttributeTypeAndValue(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* PKIUnpackAttributeTypeAndValueInternal */


/******************************************************************
 * Routines for BasicConstraints
 ******************************************************************/

size_t PKISizeofBasicConstraintsInternal(
    PKIBasicConstraints *asnstruct,
    int outerSizeFlag,
    int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    body_size =
            PKISizeofBOOLEANInternal(asnstruct->cA, PKITRUE, PKIFALSE)
          + PKISizeofINTEGERInternal(asnstruct->pathLenConstraint, 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;

} /* PKISizeofBasicConstraintsInternal */

void PKIDropInPlaceBasicConstraints(
    PKICONTEXT *ctx,
    PKIBasicConstraints *f)
{
    if (ctx == NULL)
        return;

    if (f == NULL) return ;
    PKIFreeBOOLEAN(ctx, f->cA);
    f->cA = NULL;
    PKIFreeINTEGER(ctx, f->pathLenConstraint);
    f->pathLenConstraint = NULL;
} /* PKIDropInPlaceBasicConstraints */

size_t PKIPackBasicConstraintsInternal(
    PKICONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PKIBasicConstraints *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 = PKISizeofBasicConstraints(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 cA of BasicConstraints */
    if (asnstruct->cA != NULL) { /* optional */
        bytesused += PKIPackBOOLEANInternal(ctx, buf+bytesused, buflen-bytesused,
                          asnstruct->cA, PKIID_BOOLEAN, erret );
        if (bytesused > datasize || *erret != 0)
            break;
    }

    /* field pathLenConstraint of BasicConstraints */
    if (asnstruct->pathLenConstraint != NULL) { /* optional */
        bytesused += PKIPackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
                          asnstruct->pathLenConstraint, PKIID_INTEGER, 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;
} /* PKIPackBasicConstraintsInternal */

size_t PKIUnpkInPlaceBasicConstraints(
    PKICONTEXT *ctx,
    PKIBasicConstraints *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;

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

    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 cA of BasicConstraints */
    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->cA != NULL)
        PKIFreeBOOLEAN(ctx, asnstruct->cA);
    bytesused += PKIUnpackBOOLEANInternal(ctx, &(asnstruct->cA),
                    buf+bytesused, localsize-bytesused, PKIID_BOOLEAN, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field pathLenConstraint of BasicConstraints */
    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->pathLenConstraint != NULL)
        PKIFreeINTEGER(ctx, asnstruct->pathLenConstraint);
    bytesused += PKIUnpackINTEGERInternal(ctx, &(asnstruct->pathLenConstraint),
                    buf+bytesused, localsize-bytesused, PKIID_INTEGER, 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;
} /* PKIUnpkInPlaceBasicConstraints */

size_t PKIUnpackBasicConstraintsInternal(
    PKICONTEXT *ctx,
    PKIBasicConstraints **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PKIBasicConstraints *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 = PKINewBasicConstraints(ctx); /* carve a block for it */
    bytesused = PKIUnpkInPlaceBasicConstraints(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) PKIFreeBasicConstraints(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* PKIUnpackBasicConstraintsInternal */


/******************************************************************
 * Routines for DirectoryString
 ******************************************************************/

size_t PKISizeofDirectoryStringInternal(
    PKIDirectoryString *asnstruct,
    int outerSizeFlag,
    int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    switch (asnstruct->CHOICE_field_type) {
      case PKIID_T61String:
      case 0x20|PKIID_T61String:
        body_size = PKISizeofT61StringInternal((PKIT61String *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      case PKIID_PrintableString:
      case 0x20|PKIID_PrintableString:
        body_size = PKISizeofPrintableStringInternal((PKIPrintableString *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      case PKIID_UniversalString:
      case 0x20|PKIID_UniversalString:
        body_size = PKISizeofUniversalStringInternal((PKIUniversalString *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      case PKIID_UTF8String:
      case 0x20|PKIID_UTF8String:
        body_size = PKISizeofUTF8StringInternal((PKIUTF8String *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      case PKIID_BMPString:
      case 0x20|PKIID_BMPString:
        body_size = PKISizeofBMPStringInternal((PKIBMPString *)(asnstruct->data), outerSizeFlag, expTaggedFlag);
        break;

      default:
        break;

    } /* switch */

    return (body_size);
} /* PKISizeofDirectoryStringInternal */

void PKIDropInPlaceDirectoryString(
    PKICONTEXT *ctx,
    PKIDirectoryString *f)
{
    if (ctx == NULL) return;
    if (f == NULL) return;

    switch(f->CHOICE_field_type) {

    case PKIID_T61String:
    case 0x20|PKIID_T61String:
        PKIFreeT61String(ctx, (PKIT61String *)( f->data ));
        break;
    case PKIID_PrintableString:
    case 0x20|PKIID_PrintableString:
        PKIFreePrintableString(ctx, (PKIPrintableString *)( f->data ));
        break;
    case PKIID_UniversalString:
    case 0x20|PKIID_UniversalString:
        PKIFreeUniversalString(ctx, (PKIUniversalString *)( f->data ));
        break;
    case PKIID_UTF8String:
    case 0x20|PKIID_UTF8String:
        PKIFreeUTF8String(ctx, (PKIUTF8String *)( f->data ));
        break;
    case PKIID_BMPString:
    case 0x20|PKIID_BMPString:
        PKIFreeBMPString(ctx, (PKIBMPString *)( f->data ));
        break;
    default:
        break;
    } /* switch */

} /* PKIDropInPlaceDirectoryString */

size_t PKIPackDirectoryStringInternal(
    PKICONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PKIDirectoryString *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) {
        PKIERR(PKIErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) return 0;

    /* lth of the block body */
    datasize = PKISizeofDirectoryString(ctx, asnstruct, PKITRUE);
    if (datasize > buflen) {
        PKIERR(PKIErrPackBufferTooShort);
        return 0;
    }

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

    case PKIID_T61String:
        bytesused += PKIPackT61StringInternal(ctx, buf+bytesused, buflen-bytesused,
                     (PKIT61String *)(asnstruct->data),
                     PKIID_T61String,
                     erret);
        break;

    case PKIID_PrintableString:

⌨️ 快捷键说明

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