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

📄 pgpx509cert.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 5 页
字号:

    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_PackDirectoryStringInternal */

size_t pgpasn_UnpkInPlaceDirectoryString(
     PGPASN_CONTEXT *ctx,
     PGPASN_DirectoryString *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) {

    /* teletexString */
    case PGPASN_ID_T61String:
    case 0x20|PGPASN_ID_T61String:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewT61String(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceT61String(ctx, (PGPASN_T61String *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_T61String, erret));
        /*NOTREACHED*/
        break;

    /* printableString */
    case PGPASN_ID_PrintableString:
    case 0x20|PGPASN_ID_PrintableString:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewPrintableString(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlacePrintableString(ctx, (PGPASN_PrintableString *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_PrintableString, erret));
        /*NOTREACHED*/
        break;

    /* universalString */
    case PGPASN_ID_UniversalString:
    case 0x20|PGPASN_ID_UniversalString:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewUniversalString(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceUniversalString(ctx, (PGPASN_UniversalString *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_UniversalString, erret));
        /*NOTREACHED*/
        break;

    /* utf8String */
    case PGPASN_ID_UTF8String:
    case 0x20|PGPASN_ID_UTF8String:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewUTF8String(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceUTF8String(ctx, (PGPASN_UTF8String *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_UTF8String, erret));
        /*NOTREACHED*/
        break;

    /* bmpString */
    case PGPASN_ID_BMPString:
    case 0x20|PGPASN_ID_BMPString:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewBMPString(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceBMPString(ctx, (PGPASN_BMPString *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_BMPString, erret));
        /*NOTREACHED*/
        break;

    /* ia5String */
    case PGPASN_ID_IA5String:
    case 0x20|PGPASN_ID_IA5String:
        asnstruct->CHOICE_field_type = *buf;
        asnstruct->data = (void *)pgpasn_NewIA5String(ctx);
        if (asnstruct->data == NULL) {
            PGPASN_ERR(kPGPASNError_ErrOutOfMemory);
            return 0;
        }
        return (pgpasn_UnpkInPlaceIA5String(ctx, (PGPASN_IA5String *)(asnstruct->data),
                    buf, buflen,
                    PGPASN_ID_IA5String, erret));
        /*NOTREACHED*/
        break;

    default:
        PGPASN_ERR(kPGPASNError_ErrChoiceBadType);
        return 0;

    } /* switch */

} /* pgpasn_UnpkInPlaceDirectoryString */

size_t pgpasn_UnpackDirectoryStringInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_DirectoryString **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PGPASN_DirectoryString *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_NewDirectoryString(ctx) ; /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceDirectoryString(ctx, local, buf, buflen, tag, erret);
    if (*erret == kPGPASNError_ErrChoiceBadType) {
        *erret = 0;
        if (local != NULL) pgpasn_FreeDirectoryString(ctx, local);
        return 0;
    }
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeDirectoryString(ctx, local);
        return 0;
    }

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


/******************************************************************
 * Routines for DssParms
 ******************************************************************/

size_t pgpasn_SizeofDssParmsInternal(
    PGPASN_DssParms *asnstruct,
    int outerSizeFlag,
    int expTaggedFlag)
{
    size_t body_size = 0;

    if (asnstruct == NULL)
        return 0;

    body_size =
            pgpasn_SizeofINTEGERInternal(&asnstruct->p, PGPASN_TRUE, PGPASN_FALSE)
          + pgpasn_SizeofINTEGERInternal(&asnstruct->q, PGPASN_TRUE, PGPASN_FALSE)
          + pgpasn_SizeofINTEGERInternal(&asnstruct->g, PGPASN_TRUE, PGPASN_FALSE) ;

    if (outerSizeFlag == PGPASN_TRUE)
        body_size = PGPASN_Tagged(body_size, 1);

    if (expTaggedFlag == PGPASN_TRUE)
        body_size = PGPASN_Tagged(body_size, 1); /* this is seq like */

    return body_size;

} /* pgpasn_SizeofDssParmsInternal */

void pgpasn_DropInPlaceDssParms(
    PGPASN_CONTEXT *ctx,
    PGPASN_DssParms *f)
{
    if (ctx == NULL)
        return;

    if (f == NULL) return ;
    pgpasn_DropInPlaceINTEGER(ctx, &(f->p));
    pgpasn_DropInPlaceINTEGER(ctx, &(f->q));
    pgpasn_DropInPlaceINTEGER(ctx, &(f->g));
} /* pgpasn_DropInPlaceDssParms */

size_t pgpasn_PackDssParmsInternal(
    PGPASN_CONTEXT *ctx,
    unsigned char *buf,
    size_t buflen,
    PGPASN_DssParms *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) {
        PGPASN_ERR(kPGPASNError_ErrBadContext);
        return 0;
    }
    if (asnstruct == NULL) return 0;

    /* lth of the block body */
    datasize = pgpasn_SizeofDssParms(ctx, asnstruct, PGPASN_FALSE);
    tagsize = 1 + PGPASN_LengthSize(datasize);
    if (datasize+tagsize > buflen) {
        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 p of DssParms */
    bytesused += pgpasn_PackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->p), PGPASN_ID_INTEGER, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field q of DssParms */
    bytesused += pgpasn_PackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->q), PGPASN_ID_INTEGER, erret);
    if (bytesused > datasize || *erret != 0)
        break;

    /* field g of DssParms */
    bytesused += pgpasn_PackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
                       &(asnstruct->g), 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_PackDssParmsInternal */

size_t pgpasn_UnpkInPlaceDssParms(
    PGPASN_CONTEXT *ctx,
    PGPASN_DssParms *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", "DssParms" );

    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 p of DssParms */
    bytesused += pgpasn_UnpkInPlaceINTEGER(ctx, &(asnstruct->p), buf+bytesused,
                        localsize-bytesused, PGPASN_ID_INTEGER, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field q of DssParms */
    bytesused += pgpasn_UnpkInPlaceINTEGER(ctx, &(asnstruct->q), buf+bytesused,
                        localsize-bytesused, PGPASN_ID_INTEGER, erret);
    if (bytesused > localsize || *erret != 0)
        break;

    /* field g of DssParms */
    bytesused += pgpasn_UnpkInPlaceINTEGER(ctx, &(asnstruct->g), 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_UnpkInPlaceDssParms */

size_t pgpasn_UnpackDssParmsInternal(
    PGPASN_CONTEXT *ctx,
    PGPASN_DssParms **asnstruct,
    const unsigned char *buf,
    size_t buflen,
    unsigned char tag,
    int *erret)
{
    size_t bytesused;
    PGPASN_DssParms *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_NewDssParms(ctx); /* carve a block for it */
    bytesused = pgpasn_UnpkInPlaceDssParms(ctx, local, buf, buflen, tag, erret);
    if (*erret != 0) {
        if (local != NULL) pgpasn_FreeDssParms(ctx, local);
        return 0;
    }
    *asnstruct = local;
    return bytesused;
} /* pgpasn_UnpackDssParmsInternal */


/**************************************************************

⌨️ 快捷键说明

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