📄 pkcs7.c
字号:
/*******************************************************************
*
* This file was generated by TIS/ASN1COMP Ver. 4.0, an ASN.1 compiler.
* TIS/ASN1COMP is Copyright (c) 1998, TIS Labs at Network Associates, Inc.
*
* This file was AUTOMATICALLY GENERATED on Wed Nov 18 16:32:31 1998
*
******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pkcs7_asn.h"
/*******************************************************************
*
* Code for internal routines
*
* DropInPlaceXXX: drops the contents of a block, in place
*
* _PackXXX: packs up a structure into an ASN.1 block
*
* UnpkInPlaceXXX: unpacks an ASN.1 block into a structure in place
*
* _unPackXXX: carves a structure block and unpacks into it
*
******************************************************************/
/******************************************************************
* Routines for CertificateRevocationLists
******************************************************************/
size_t PKISizeofCertificateRevocationListsInternal(
PKICertificateRevocationLists *asnstruct,
int outerSizeFlag,
int expTaggedFlag)
{
size_t body_size = 0;
long i, lth;
if (asnstruct == NULL)
return 0;
lth = asnstruct->n;
if (lth > PKIMAX_CertificateRevocationLists)
lth = PKIMAX_CertificateRevocationLists; /* clamp it */
for (i=0;i<lth;i++)
body_size += PKISizeofCertificateListInternal((asnstruct->elt)[i], 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;
} /* PKISizeofCertificateRevocationListsInternal */
void PKIDropInPlaceCertificateRevocationLists(
PKICONTEXT *ctx,
PKICertificateRevocationLists *f)
{
long i, lth;
if (ctx == NULL) return;
if (f == NULL) return;
lth = f->n;
if (lth > PKIMAX_CertificateRevocationLists)
lth = PKIMAX_CertificateRevocationLists; /* clamp it */
for (i=0;i<lth;i++) {
PKIFreeCertificateList(ctx, (f->elt)[i] );
(f->elt)[i] = NULL;
}
} /* PKIDropInPlaceCertificateRevocationLists */
size_t PKIPackCertificateRevocationListsInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKICertificateRevocationLists *asnstruct,
unsigned char tag,
int *erret)
{
size_t bytesused;
size_t tagsize;
size_t datasize;
long numElem;
int i, j;
size_t length;
size_t max = 0;
PKIVariableBlock temp[PKIMAX_CertificateRevocationLists];
if (erret == NULL) return 0; /* can't report errors */
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (asnstruct == NULL) return 0; /* nothing to pack */
/* make sure there aren't too many elements */
numElem = asnstruct->n;
if (numElem > PKIMAX_CertificateRevocationLists) {
PKIERR(PKIErrPackSETOFArrayTooLong);
return 0;
}
datasize = PKISizeofCertificateRevocationLists(ctx, asnstruct, PKIFALSE);
tagsize = 1 + PKILengthSize(datasize);
if (datasize+tagsize > buflen) {
PKIERR(PKIErrPackBufferTooShort);
return 0;
}
/* this is a SET_OF */
bytesused = PKIPutTag(buf, (unsigned char)(tag|0x20), datasize);
if (bytesused != tagsize) {
PKIERR(PKIErrPackOverrun);
return bytesused;
}
datasize += tagsize;
if (numElem == 1) {
bytesused += PKIPackCertificateListInternal(ctx, buf+bytesused, buflen-bytesused,
(asnstruct->elt)[0], PKIID_CertificateList, erret);
}
else {
/* calculate lengths and max */
for (i=0; i<numElem; i++) {
length = PKISizeofCertificateList(ctx, (asnstruct->elt)[i], PKITRUE);
if (length > max)
max = length;
temp[i].len = length;
}
/* temporarily allocate and pack */
*erret = 0;
for (i=0; i<numElem; i++) {
temp[i].val = (unsigned char *)PKIAlloc(ctx->memMgr, max);
memset(temp[i].val, 0, max);
(void) PKIPackCertificateListInternal(ctx, temp[i].val,
max, (asnstruct->elt)[i],
PKIID_CertificateList, erret );
if (*erret != 0) {
for (j=0; j<i; j++)
PKIFree(ctx->memMgr, temp[j].val);
return 0;
}
}
/* sort and look for duplicates */
qsort(temp, numElem, sizeof(PKIVariableBlock), PKICompareElems);
for (i=0; i<(numElem-1); i++)
if (memcmp(temp[i].val, temp[i+1].val, max) == 0) {
PKIERR(PKIErrPackSETOFUnsortable);
for (j=0; j<i; j++)
PKIFree(ctx->memMgr, temp[j].val);
return 0;
}
/* pack for real and deallocate temp */
for (i=0; i<numElem; i++) {
(void)memcpy(buf+bytesused, temp[i].val, temp[i].len);
bytesused += temp[i].len;
if (bytesused > datasize)
break;
PKIFree(ctx->memMgr, temp[i].val);
}
}
if (bytesused < datasize && *erret == 0)
PKIERR(PKIErrPackUnderrun)
else if (bytesused > datasize && *erret == 0)
PKIERR(PKIErrPackOverrun)
return bytesused;
} /* PKIPackCertificateRevocationListsInternal */
size_t PKIUnpkInPlaceCertificateRevocationLists(
PKICONTEXT *ctx,
PKICertificateRevocationLists *asnstruct,
unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret )
{
size_t bytesused;
size_t datasize;
size_t localsize;
long i;
int indef = 0;
PKITRACE_PRINT_FN((tag|0x20), 0x31, "SET OF", "CertificateRevocationLists");
if (erret == NULL) return 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; /* out of bytes, no action */
if ( (*buf & 0xDF) != (tag & 0xDF) )
return 0; /* not my kind of block */
if ( (*buf & 0x20) != 0x20) {
PKIERR(PKIErrUnpackInvalidEncoding);
return 0;
}
PKITRACE_INCR_LEVEL;
bytesused = 1; /* consume the tag byte */
bytesused += PKIGetLength(buf+bytesused, &datasize);
if ((int)datasize == -1) {
localsize = buflen;
indef = 1;
}
else {
localsize = bytesused + datasize;
if (localsize > buflen) {
PKIERR(PKIErrUnpackOverrun);
asnstruct->n = -1 ; /* note where (-1 treated as 0) */
PKITRACE_DECR_LEVEL;
return 0;
}
}
for (i=0; (i < PKIMAX_CertificateRevocationLists) && (bytesused < localsize); i++) {
/* if this is indef length and we have EOC, done */
if (indef && *(buf+bytesused) == 0x00 &&
*(buf+bytesused+1) == 0x00 ) {
break;
}
if (asnstruct->elt[i] == NULL)
asnstruct->elt[i] = PKINewCertificateList(ctx);
bytesused += PKIUnpkInPlaceCertificateList(ctx, asnstruct->elt[i],
buf+bytesused, localsize-bytesused,
PKIID_CertificateList, erret);
if (*erret != 0 || asnstruct->elt[i] == NULL)
break;
asnstruct->n = i+1 ; /* note the new element */
} /* for */
if (indef) {
if ( *(buf+bytesused) != 0x00 &&
*(buf+bytesused+1) != 0x00 ) {
PKIERR(PKIErrUnpackInvalidEncoding);
}
else
bytesused += 2;
}
PKITRACE_DECR_LEVEL;
if (bytesused > localsize && *erret == 0)
PKIERR(PKIErrUnpackOverrun);
if (!indef && bytesused < localsize && *erret == 0)
PKIERR(PKIErrUnpackUnderrun);
return bytesused;
} /* PKIUnpkInPlaceCertificateRevocationLists */
size_t PKIUnpackCertificateRevocationListsInternal(
PKICONTEXT *ctx,
PKICertificateRevocationLists **asnstruct,
unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused;
PKICertificateRevocationLists *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 = PKINewCertificateRevocationLists(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlaceCertificateRevocationLists(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
PKIFreeCertificateRevocationLists(ctx, local);
return 0;
}
*asnstruct = local;
return bytesused;
} /* PKIUnpackCertificateRevocationListsInternal */
/******************************************************************
* Routines for ContentInfo
******************************************************************/
size_t PKISizeofContentInfoInternal(PKIContentInfo *asnstruct, int outerSizeFlag, int expTaggedFlag)
{
size_t body_size = 0;
if (asnstruct == NULL)
return 0;
body_size =
PKISizeofContentTypeInternal(&asnstruct->contentType, PKITRUE, PKIFALSE)
+ PKISizeofANYInternal(asnstruct->content, PKITRUE, PKITRUE) ;
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;
} /* PKISizeofContentInfoInternal */
void PKIDropInPlaceContentInfo(
PKICONTEXT *ctx,
PKIContentInfo *f)
{
if (ctx == NULL)
return;
if (f == NULL) return ;
PKIDropInPlaceContentType(ctx, &(f->contentType));
PKIFreeANY(ctx, f->content);
f->content = NULL;
} /* PKIDropInPlaceContentInfo */
size_t PKIPackContentInfoInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIContentInfo *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 = PKISizeofContentInfo(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 contentType of ContentInfo */
bytesused += PKIPackContentTypeInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->contentType), PKIID_ContentType, erret);
if (bytesused > datasize || *erret != 0)
break;
/* field content of ContentInfo */
if (asnstruct->content != NULL) { /* optional */
bytesused += PKIPutTag(buf+bytesused, 0xa0 | 0x00, PKISizeofANY(ctx, asnstruct->content, PKITRUE));
bytesused += PKIPackANYInternal(ctx, buf+bytesused, buflen-bytesused,
asnstruct->content, PKIID_ANY, 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;
} /* PKIPackContentInfoInternal */
size_t PKIUnpkInPlaceContentInfo(
PKICONTEXT *ctx,
PKIContentInfo *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", "ContentInfo" );
if (erret == NULL) return 0; /* can't report errors */
*erret = 0;
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -