📄 pkcs12.c
字号:
body_size = PKITagged(body_size, 1); /* this is seq like */
return body_size;
} /* PKISizeofPFXInternal */
void PKIDropInPlacePFX(
PKICONTEXT *ctx,
PKIPFX *f)
{
if (ctx == NULL)
return;
if (f == NULL) return ;
PKIDropInPlaceVersion(ctx, &(f->version));
PKIDropInPlaceContentInfo(ctx, &(f->authSafes));
PKIFreeMacData(ctx, f->macData);
f->macData = NULL;
} /* PKIDropInPlacePFX */
size_t PKIPackPFXInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIPFX *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 = PKISizeofPFX(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 PFX */
bytesused += PKIPackVersionInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->version), PKIID_Version, erret);
if (bytesused > datasize || *erret != 0)
break;
/* field authSafes of PFX */
bytesused += PKIPackContentInfoInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->authSafes), PKIID_ContentInfo, erret);
if (bytesused > datasize || *erret != 0)
break;
/* field macData of PFX */
if (asnstruct->macData != NULL) { /* optional */
bytesused += PKIPackMacDataInternal(ctx, buf+bytesused, buflen-bytesused,
asnstruct->macData, PKIID_MacData, 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;
} /* PKIPackPFXInternal */
size_t PKIUnpkInPlacePFX(
PKICONTEXT *ctx,
PKIPFX *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", "PFX" );
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 version of PFX */
bytesused += PKIUnpkInPlaceVersion(ctx, &(asnstruct->version), buf+bytesused,
localsize-bytesused, PKIID_Version, erret);
if (bytesused > localsize || *erret != 0)
break;
/* field authSafes of PFX */
bytesused += PKIUnpkInPlaceContentInfo(ctx, &(asnstruct->authSafes), buf+bytesused,
localsize-bytesused, PKIID_ContentInfo, erret);
if (bytesused > localsize || *erret != 0)
break;
/* field macData of PFX */
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->macData != NULL)
PKIFreeMacData(ctx, asnstruct->macData);
bytesused += PKIUnpackMacDataInternal(ctx, &(asnstruct->macData),
buf+bytesused, localsize-bytesused, PKIID_MacData, 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;
} /* PKIUnpkInPlacePFX */
size_t PKIUnpackPFXInternal(
PKICONTEXT *ctx,
PKIPFX **asnstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused;
PKIPFX *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 = PKINewPFX(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlacePFX(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreePFX(ctx, local);
return 0;
}
*asnstruct = local;
return bytesused;
} /* PKIUnpackPFXInternal */
/******************************************************************
* Routines for SafeContents
******************************************************************/
size_t PKISizeofSafeContentsInternal(
PKISafeContents *asnstruct,
int outerSizeFlag,
int expTaggedFlag)
{
long i, lth;
size_t body_size = 0;
if (asnstruct == NULL)
return 0;
lth = asnstruct->n ;
if (lth > PKIMAX_SafeContents)
lth = PKIMAX_SafeContents; /* clamp it */
for (i=0; i<lth; i++)
body_size += PKISizeofSafeBagInternal((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;
} /* PKISizeofSafeContentsInternal */
void PKIDropInPlaceSafeContents(
PKICONTEXT *ctx,
PKISafeContents *f)
{
long i, lth;
if (ctx == NULL) return;
if (f == NULL) return;
lth = f->n ;
if (lth > PKIMAX_SafeContents)
lth = PKIMAX_SafeContents ; /* clamp it */
for (i=0;i<lth;i++) {
PKIFreeSafeBag(ctx, (f->elt)[i]);
(f->elt)[i] = NULL;
}
} /* PKIDropInPlaceSafeContents */
size_t PKIPackSafeContentsInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKISafeContents *asnstruct,
unsigned char tag,
int *erret )
{
size_t bytesused;
size_t tagsize;
size_t datasize;
long numElem;
int i;
if (erret == NULL) return 0; /* can't report errors */
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (asnstruct == NULL) return 0;
/* make sure there aren't too many elements */
numElem = asnstruct->n;
if (numElem > PKIMAX_SafeContents) {
PKIERR(PKIErrPackSEQOFArrayTooLong);
return 0;
}
/* lth of the block body */
datasize = PKISizeofSafeContents(ctx, asnstruct, PKIFALSE);
tagsize = 1 + PKILengthSize(datasize);
if (datasize+tagsize > buflen) {
PKIERR(PKIErrPackBufferTooShort);
return 0;
}
/* this is a SEQ_OF */
bytesused = PKIPutTag(buf, (unsigned char)(tag|0x20), datasize);
if (bytesused != tagsize) {
PKIERR(PKIErrPackOverrun);
return bytesused;
}
datasize += tagsize;
for (i=0; i<numElem; i++) {
bytesused += PKIPackSafeBagInternal(ctx, buf+bytesused, buflen-bytesused,
(asnstruct->elt)[i],
PKIID_SafeBag, erret);
if (bytesused > datasize || *erret != 0)
break;
}
if (bytesused < datasize && *erret == 0)
PKIERR(PKIErrPackUnderrun)
else if (bytesused > datasize && *erret == 0)
PKIERR(PKIErrPackOverrun);
return bytesused;
} /* PKIPackSafeContentsInternal */
size_t PKIUnpkInPlaceSafeContents(
PKICONTEXT *ctx,
PKISafeContents *asnstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused = 0;
size_t datasize;
size_t localsize;
int i ;
int indef = 0;
PKITRACE_PRINT_FN((tag|0x20), 0x30, "SEQUENCE OF", "SafeContents");
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;
}
bytesused ++; /* consume the tag byte */
PKITRACE_INCR_LEVEL;
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_SafeContents) && (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] = PKINewSafeBag(ctx);
bytesused += PKIUnpkInPlaceSafeBag(ctx, asnstruct->elt[i], buf+bytesused,
localsize-bytesused, PKIID_SafeBag, 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;
} /* PKIUnpkInPlaceSafeContents */
size_t PKIUnpackSafeContentsInternal(
PKICONTEXT *ctx,
PKISafeContents **asnstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused;
PKISafeContents *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 ta
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -