📄 cert_util.c
字号:
}
if (buflen <= 0)
return 0; /* no error -- no block */
/* see note in UnpkInPlaceVariableBlock */
if ( (*buf & 0xDF) != (tag & 0xDF) )
return 0;
if ( (*buf & 0x20) == 0x20)
constructed = 1;
bytesused = 1; /* eat the tag byte */
bytesused += PKIGetLength(buf+bytesused, &datasize);
if ((int)datasize == -1) { /* indef length */
datasize = 0;
indef = 1;
}
if (bytesused+datasize > buflen) {
PKIERR(PKIErrUnpackOverrun); /* note this error */
return 0;
}
if (indef == 1 && constructed != 1) {
PKIERR(PKIErrUnpackInvalidEncoding);
return 0;
}
if (bitstruct->val != NULL)
PKIFree(ctx->memMgr, bitstruct->val);
PKITRACE_PRINT_DATA(buf+bytesused,datasize);
if (constructed == 0) { /* primitive */
/* the first byte of the data is the number of unused bits */
bitstruct->nuub = *(buf+bytesused);
bytesused++;
/* the len is -1 since we just used one byte for the nuub value */
bitstruct->len = datasize-1;
bitstruct->val = (unsigned char *)PKIAlloc(ctx->memMgr, bitstruct->len);
if (bitstruct->val == NULL) {
PKIERR(PKIErrOutOfMemory);
return 0;
}
memcpy(bitstruct->val, buf+bytesused, bitstruct->len);
bytesused += bitstruct->len;
}
else if (indef == 1) { /* constructed, indefinite length */
bytesused += uipBitSegments(ctx, indef,
(unsigned char)(tag & 0xDF), bitstruct,
buf+bytesused,
buflen-bytesused, erret);
}
else { /* constructed, definite length */
bytesused += uipBitSegments(ctx, indef,
(unsigned char)(tag & 0xDF), bitstruct,
buf+bytesused,
datasize, erret);
}
return bytesused;
} /* PKIUnpkInPlaceBIT_STRING */
size_t PKIUnpackBIT_STRING(
PKICONTEXT *ctx,
PKIBIT_STRING **bitstruct,
const unsigned char *buf,
size_t buflen,
int *erret)
{
return(PKIUnpackBIT_STRINGInternal(ctx, bitstruct, buf, buflen,
PKIID_BIT_STRING, erret));
}
size_t PKIUnpackBIT_STRINGInternal(
PKICONTEXT *ctx,
PKIBIT_STRING **bitstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret) /* error return */
{
size_t bytesused;
PKIBIT_STRING *local = NULL;
if (erret == NULL)
return 0;
*erret = 0;
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (bitstruct == NULL) {
PKIERR(PKIErrUnpackNoStructure);
return 0;
}
*bitstruct = NULL;
if (buflen <= 0) return 0;
/* see note in UnpkInPlaceVariableBlock */
if ( (*buf & 0xDF) != (tag & 0xDF) ) return 0;
local = PKINewBIT_STRING(ctx);
bytesused = PKIUnpkInPlaceBIT_STRING(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreeBIT_STRING(ctx, local);
return 0;
}
*bitstruct = local;
return bytesused;
} /* PKIUnpackBIT_STRINGInternal */
/************************************************************************
* Routines for PKIGeneralizedTime
*************************************************************************/
size_t PKIPackGeneralizedTime(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIGeneralizedTime *timeblock,
int *erret)
{
return(PackVariableBlock(ctx, PKIID_GeneralizedTime, PKIFALSE, 0, buf, buflen,
timeblock, erret));
}
size_t PKIPackGeneralizedTimeInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIGeneralizedTime *timeblock,
unsigned char tag,
int *erret)
{
return(PackVariableBlock(ctx, tag, PKIFALSE, 0, buf, buflen, timeblock, erret));
} /* PKIPackGeneralizedTimeInternal */
size_t PKIUnpkInPlaceGeneralizedTime(
PKICONTEXT *ctx,
PKIGeneralizedTime *timestruct, /* output block */
const unsigned char *buf, /* loc of input pointer */
size_t buflen, /* max end of my region */
unsigned char tag,
int *erret) /* error return location */
{
return(UnpkInPlaceVariableBlock(ctx, tag, "GeneralizedTime", 0x18, timestruct,
buf, buflen, erret));
} /* PKIUnpkInPlaceGeneralizedTime */
size_t PKIUnpackGeneralizedTime(
PKICONTEXT *ctx,
PKIGeneralizedTime **timestruct,
const unsigned char *buf,
size_t buflen,
int *erret)
{
return(PKIUnpackGeneralizedTimeInternal(ctx, timestruct, buf, buflen,
PKIID_GeneralizedTime, erret));
}
size_t PKIUnpackGeneralizedTimeInternal(
PKICONTEXT *ctx,
PKIGeneralizedTime **timestruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret) /* error return */
{
size_t bytesused;
PKIGeneralizedTime *local = NULL;
if (erret == NULL) return 0;
*erret = 0;
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (timestruct == NULL) {
PKIERR(PKIErrUnpackNoStructure);
return 0;
}
*timestruct = NULL;
if (buflen <= 0) return 0;
/* see note in UnpkInPlaceVariableBlock */
if ( (*buf & 0xDF) != (tag & 0xDF) ) return 0;
local = PKINewGeneralizedTime(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlaceGeneralizedTime(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreeGeneralizedTime(ctx, local);
return 0;
}
*timestruct = local;
return bytesused;
} /* PKIUnpackGeneralizedTimeInternal */
/************************************************************************
* Routines for PKIIA5String
*************************************************************************/
size_t PKIPackIA5String(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIIA5String *strblock,
int *erret)
{
return(PackVariableBlock(ctx, PKIID_IA5String, PKIFALSE, 0, buf, buflen,
strblock, erret));
} /* PKIPackIA5String */
size_t PKIPackIA5StringInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIIA5String *strblock,
unsigned char tag,
int *erret)
{
return(PackVariableBlock(ctx, tag, PKIFALSE, 0, buf, buflen, strblock, erret));
} /* PKIPackIA5StringInternal */
size_t PKIUnpkInPlaceIA5String(
PKICONTEXT *ctx,
PKIIA5String *strblock, /* output block */
const unsigned char *buf, /* loc of input pointer */
size_t buflen, /* max end of my region */
unsigned char tag,
int *erret) /* error return location */
{
return (UnpkInPlaceVariableBlock(ctx, tag, "IA5String", 0x16, strblock, buf, buflen, erret));
} /* PKIUnpkInPlaceIA5String */
size_t PKIUnpackIA5String(
PKICONTEXT *ctx,
PKIIA5String **strblock,
const unsigned char *buf,
size_t buflen, /* my end pointer */
int *erret)
{
return(PKIUnpackIA5StringInternal(ctx, strblock, buf, buflen,
PKIID_IA5String, erret));
}
size_t PKIUnpackIA5StringInternal(
PKICONTEXT *ctx,
PKIIA5String **strblock,
const unsigned char *buf,
size_t buflen, /* my end pointer */
unsigned char tag,
int *erret) /* error return */
{
size_t bytesused;
PKIIA5String *local = NULL;
if (erret == NULL) return 0;
*erret = 0;
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (strblock == NULL) {
PKIERR(PKIErrUnpackNoStructure);
return 0;
}
*strblock = NULL;
if (buflen <= 0) return 0;
/* see note in UnpkInPlaceVariableBlock */
if ( (*buf & 0xDF) != (tag & 0xDF) ) return 0;
local = PKINewIA5String(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlaceIA5String(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreeIA5String(ctx, local);
return 0;
}
*strblock = local;
return bytesused;
} /* PKIUnpackIA5StringInternal */
/************************************************************************
* Routines for PKIINTEGER
*************************************************************************/
/************************************************************************
* normalize
*
* remove leading 0's from the integer byte string/
* This routine isn't the most efficient, but it should never have to
* move any bytes because all numbers should be normalized already.
*************************************************************************/
static void normalize( PKIINTEGER *b )
{
size_t i ;
unsigned char *x ;
if (b == NULL) return ;
if ((b->val) == NULL) {
b->len = 0 ; /* no bytes, no length */
return ;
}
x = b->val ; /* point to the array */
while ( (((x[0]^x[1])&0x80) == 0)
&& ((x[0] == 0)||(x[0] == 0xff))
&& ((b->len) > 1) ) { /* shorten this by 1 byte */
(b->len)-- ; /* note the shortening */
for (i=0; i<(b->len); i++) x[i] = x[i+1] ; /* do it */
} /* while */
return ;
} /* normalize */
size_t PKISizeofINTEGER(
PKICONTEXT *ctx,
PKIINTEGER *intblock,
int outerSizeFlag)
{
(void)ctx; /* for future use */
return PKISizeofINTEGERInternal(intblock, outerSizeFlag, PKIFALSE);
}
size_t PKISizeofINTEGERInternal(
PKIINTEGER *intblock,
int outerSizeFlag,
int expTaggedSize)
{
size_t length;
if (intblock == NULL)
return 0;
normalize(intblock);
length = intblock->len;
if (outerSizeFlag == PKITRUE)
length = length + 1 + PKILengthSize(length);
if (expTaggedSize == PKITRUE)
length = PKITagged(length, 0);
return length;
} /* PKISizeofINTEGERInternal */
size_t PKIPackINTEGER(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIINTEGER *intblock,
int *erret)
{
return(PKIPackINTEGERInternal(ctx, buf, buflen, intblock, PKIID_INTEGER, erret));
}
size_t PKIPackINTEGERInternal(
PKICONTEXT *ctx,
unsigned char *buf,
size_t buflen,
PKIINTEGER *intblock,
unsigned char tag,
int *erret)
{
if (intblock == NULL) return 0;
normalize(intblock);
return(PackVariableBlock(ctx, tag, PKIFALSE, 0, buf, buflen, intblock, erret));
}
size_t PKIUnpkInPlaceINTEGER(
PKICONTEXT *ctx,
PKIINTEGER *intstruct, /* output block */
const unsigned char *buf, /* loc of input pointer */
size_t buflen, /* max end of my region */
unsigned char tag,
int *erret) /* error return location */
{
return (UnpkInPlaceVariableBlock(ctx, tag, "INTEGER", 0x02, intstruct, buf, buflen, erret));
} /* PKIUnpkInPlaceINTEGER */
size_t PKIUnpackINTEGER(
PKICONTEXT *ctx,
PKIINTEGER **intstruct,
const unsigned char *buf,
size_t buflen,
int *erret)
{
return(PKIUnpackINTEGERInternal(ctx, intstruct, buf, buflen, PKIID_INTEGER, erret));
}
size_t PKIUnpackINTEGERInternal(
PKICONTEXT *ctx,
PKIINTEGER **intstruct,
const unsigned char *buf,
size_t buflen,
unsigned char tag,
int *erret) /* error return */
{
size_t bytesused;
PKIINTEGER *local = NULL;
if (erret == NULL) return 0;
*erret = 0;
if (ctx == NULL) {
PKIERR(PKIErrBadContext);
return 0;
}
if (intstruct == NULL) {
PKIERR(PKIErrUnpackNoStructure);
return 0;
}
*intstruct = NULL;
if (buflen <= 0) return 0;
if (*buf != tag) return 0;
local = PKINewINTEGER(ctx); /* carve a block for it */
bytesused = PKIUnpkInPlaceINTEGER(ctx, local, buf, buflen, tag, erret);
if (*erret != 0) {
if (local != NULL) PKIFreeINTEGER(ctx, local);
return 0;
}
*intstruct = local;
return bytesused;
} /* PKIUnpackINTEGERInternal */
/************************************************************************
* PKIGetIntVal
*
* Extract a long from the PKIINTEGER b.
*************************************************************************/
long PKIGetIntVal(
PKICONTEXT *ctx,
PKIINTEGER *b,
int *error)
{
long val = 0;
size_t i;
unsigned char *x;
(void)ctx; /* for future use */
if (error == NULL)
return 0;
*error = 0;
if ( b == NULL || b->len > 4 || b->val == NULL ) {
*error = -1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -