📄 pkcs12.c
字号:
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 = PKISizeofSecretBag(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 secretType of SecretBag */
bytesused += PKIPackOBJECT_IDInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->secretType), PKIID_OBJECT_ID, erret);
if (bytesused > datasize || *erret != 0)
break;
/* field secret of SecretBag */
bytesused += PKIPutTag(buf+bytesused, 0xa0 | 0x00, PKISizeofANY(ctx, &(asnstruct->secret ), PKITRUE));
bytesused += PKIPackANYInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->secret), 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;
} /* PKIPackSecretBagInternal */
size_t PKIUnpkInPlaceSecretBag(
PKICONTEXT *ctx,
PKISecretBag *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", "SecretBag" );
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 secretType of SecretBag */
bytesused += PKIUnpkInPlaceOBJECT_ID(ctx, &(asnstruct->secretType), buf+bytesused,
localsize-bytesused, PKIID_OBJECT_ID, erret);
if (bytesused > localsize || *erret != 0)
break;
/* field secret of SecretBag */
{ /* local declaration block*/
size_t taggeddatasize;
size_t taggedlocalsize;
size_t used;
used = PKITakeTag(buf+bytesused, 0xa0 | 0x00,
&taggeddatasize);
bytesused += used;
if ((int)taggeddatasize == -1 && used != 0) {
PKITRACE_PRINT_TAG(0xa0|0x00, 0x00);
PKITRACE_INCR_LEVEL;
bytesused += PKIUnpkInPlaceANY(ctx, &(asnstruct->secret), buf+bytesused,
localsize-bytesused, PKIID_ANY, erret);
PKITRACE_DECR_LEVEL;
if ( *(buf+bytesused) != 0x00 &&
*(buf+bytesused+1) != 0x00 ) {
PKIERR(PKIErrUnpackInvalidEncoding);
break;
}
bytesused += 2;
}
else if (taggeddatasize > 0 && used != 0) {
taggedlocalsize = bytesused + taggeddatasize;
PKITRACE_PRINT_TAG(0xa0|0x00, 0x00);
PKITRACE_INCR_LEVEL;
bytesused += PKIUnpkInPlaceANY(ctx, &(asnstruct->secret), buf+bytesused,
localsize-bytesused, PKIID_ANY, erret);
PKITRACE_DECR_LEVEL;
if (bytesused != taggedlocalsize) {
PKIERR(PKIErrUnpackTaggedLth);
break;
}
}
} /* for the local declaration block */
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;
} /* PKIUnpkInPlaceSecretBag */
size_t PKIUnpackSecretBagInternal(
PKICONTEXT *ctx,
PKISecretBag **asnstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused;
PKISecretBag *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 = PKINewSecretBag(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlaceSecretBag(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreeSecretBag(ctx, local);
return 0;
}
*asnstruct = local;
return bytesused;
} /* PKIUnpackSecretBagInternal */
/******************************************************************
* Routines for pkcs_12PbeParams
******************************************************************/
size_t PKISizeofpkcs_12PbeParamsInternal(
PKIpkcs_12PbeParams *asnstruct,
int outerSizeFlag,
int expTaggedFlag)
{
size_t body_size = 0;
if (asnstruct == NULL)
return 0;
body_size =
PKISizeofOCTET_STRINGInternal(&asnstruct->salt, PKITRUE, PKIFALSE)
+ PKISizeofINTEGERInternal(&asnstruct->iterationCount, 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;
} /* PKISizeofpkcs_12PbeParamsInternal */
void PKIDropInPlacepkcs_12PbeParams(
PKICONTEXT *ctx,
PKIpkcs_12PbeParams *f)
{
if (ctx == NULL)
return;
if (f == NULL) return ;
PKIDropInPlaceOCTET_STRING(ctx, &(f->salt));
PKIDropInPlaceINTEGER(ctx, &(f->iterationCount));
} /* PKIDropInPlacepkcs_12PbeParams */
size_t PKIPackpkcs_12PbeParamsInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIpkcs_12PbeParams *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 = PKISizeofpkcs_12PbeParams(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 salt of pkcs_12PbeParams */
bytesused += PKIPackOCTET_STRINGInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->salt), PKIID_OCTET_STRING, erret);
if (bytesused > datasize || *erret != 0)
break;
/* field iterationCount of pkcs_12PbeParams */
bytesused += PKIPackINTEGERInternal(ctx, buf+bytesused, buflen-bytesused,
&(asnstruct->iterationCount), 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;
} /* PKIPackpkcs_12PbeParamsInternal */
size_t PKIUnpkInPlacepkcs_12PbeParams(
PKICONTEXT *ctx,
PKIpkcs_12PbeParams *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", "pkcs_12PbeParams" );
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 salt of pkcs_12PbeParams */
bytesused += PKIUnpkInPlaceOCTET_STRING(ctx, &(asnstruct->salt), buf+bytesused,
localsize-bytesused, PKIID_OCTET_STRING, erret);
if (bytesused > localsize || *erret != 0)
break;
/* field iterationCount of pkcs_12PbeParams */
bytesused += PKIUnpkInPlaceINTEGER(ctx, &(asnstruct->iterationCount), 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;
} /* PKIUnpkInPlacepkcs_12PbeParams */
size_t PKIUnpackpkcs_12PbeParamsInternal(
PKICONTEXT *ctx,
PKIpkcs_12PbeParams **asnstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret)
{
size_t bytesused;
PKIpkcs_12PbeParams *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 = PKINewpkcs_12PbeParams(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlacepkcs_12PbeParams(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreepkcs_12PbeParams(ctx, local);
return 0;
}
*asnstruct = local;
return bytesused;
} /* PKIUnpackpkcs_12PbeParamsInternal */
/******************************************************************
* Routines for PFX
******************************************************************/
size_t PKISizeofPFXInternal(
PKIPFX *asnstruct,
int outerSizeFlag,
int expTaggedFlag)
{
size_t body_size = 0;
if (asnstruct == NULL)
return 0;
body_size =
PKISizeofVersionInternal(&asnstruct->version, PKITRUE, PKIFALSE)
+ PKISizeofContentInfoInternal(&asnstruct->authSafes, PKITRUE, PKIFALSE)
+ PKISizeofMacDataInternal(asnstruct->macData, PKITRUE, PKIFALSE) ;
if (outerSizeFlag == PKITRUE)
body_size = PKITagged(body_size, 1);
if (expTaggedFlag == PKITRUE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -