📄 icutils.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "icutils.h"
#include "stringutil.h"
#include "errorctx.h"
int icPatimat (
char *pat,
char *str,
VoltLibCtx *libCtx
);
int icGetCNFromCertAlloc (
VoltLibCtx *libCtx,
Asn1X509Cert *cert,
char **commonName
)
{
int status;
unsigned int indexR, indexA, theTag, lengthLen, valueLen;
UInt32 lenLo, lenHi;
VoltX500NameObject *nameObj = (VoltX500NameObject *)0;
VoltX500RDN *currentRdn;
VoltX500Attribute *currentAttr;
unsigned char *buffer = (unsigned char *)0;
unsigned char cnOid[VoltCommonNameOidBytesLen + 2] =
{ 0x06, VoltCommonNameOidBytesLen, VoltCommonNameOidBytes };
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* We need the name decoded. In the Asn1X509Cert object, the name
* is an Asn1Encoded. To decode a name, use the X500Name object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltCreateX500NameObject (libCtx, &nameObj);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltDecodeX500Name (
nameObj, cert->innerCert->subject->base.data,
(unsigned int)(cert->innerCert->subject->base.length));
if (status != 0)
break;
/* Search for the commonName attribute.
*/
for (indexR = 0; indexR < nameObj->rdnsCount; ++indexR)
{
currentRdn = &(nameObj->rdns[indexR]);
for (indexA = 0; indexA < currentRdn->attrCount; ++indexA)
{
currentAttr = &(currentRdn->attributes[indexA]);
if (currentAttr->oid.len != VoltCommonNameOidBytesLen + 2)
continue;
if (Z2Memcmp (
currentAttr->oid.data, cnOid, VoltCommonNameOidBytesLen + 2) == 0)
break;
}
/* If we broke out early, we found the attribute, so break out of
* the RDN loop.
*/
if (indexA < currentRdn->attrCount)
break;
}
/* If we went through all RDN's and found no common name, error.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ENCODING;
if (indexR >= nameObj->rdnsCount)
break;
/* The value in the currentAttr is the value we're looking for.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltDecodeTagAndLen (
libCtx, currentAttr->value.data, currentAttr->value.len,
&theTag, &lengthLen, &lenLo, &lenHi, sizeof (unsigned int));
if (status != 0)
break;
valueLen = (unsigned int)lenLo;
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (valueLen + 1, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memcpy (
buffer, currentAttr->value.data + lengthLen + 1, valueLen);
buffer[valueLen] = 0;
*commonName = buffer;
status = 0;
} while (0);
VoltDestroyX500NameObject (&nameObj);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, errorType, fnctLine,
"icGetCNFromCertAlloc", (char *)0)
return (status);
}
int icBase64Encode (
unsigned char **out,
int *outLen,
unsigned char *in,
int inLen,
int oneLine,
VoltLibCtx *libCtx
)
{
int status;
unsigned int bufferSize;
VtAlgorithmObject b64 = (VtAlgorithmObject)0;
unsigned char *buffer = (unsigned char *)0;
VtBase64Info b64Info;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
b64Info.base64BlockSize = 64;
b64Info.newLineCharacter = VT_BASE64_NEW_LINE_CR_LF;
if (oneLine != 0)
b64Info.newLineCharacter = VT_BASE64_NO_NEW_LINE;
b64Info.errorCheck = VT_BASE64_NO_ERROR_CHECK;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreateAlgorithmObject (
(VtLibCtx)libCtx, VtAlgorithmImplBase64, (Pointer)&b64Info, &b64);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VtEncodeInit (b64);
if (status != 0)
break;
/* How big does the buffer need to be?
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtEncodeFinal (
b64, (VtRandomObject)0, in, inLen, (unsigned char *)0, 0, &bufferSize);
if (status == 0)
status = VT_ERROR_GENERAL;
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
/* Allocate the buffer to hold the result. Add one byte for a
* NULL-terminating character.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (bufferSize + 1, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtEncodeFinal (
b64, (VtRandomObject)0, in, inLen, buffer, bufferSize,
(unsigned int *)outLen);
if (status != 0)
break;
/* Place a NULL-terminating character, some callers might expect it.
*/
buffer[*outLen] = 0;
*out = buffer;
} while (0);
VtDestroyAlgorithmObject (&b64);
if (status == 0)
return (0);
/* If there was an error, make sure we free what we allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, errorType, fnctLine,
"icBase64Encode", (char *)0)
return (status);
}
int icBase64Decode (
unsigned char **out,
int *outLen,
unsigned char *in,
int inLen,
VoltLibCtx *libCtx
)
{
int status;
unsigned int bufferSize;
VtAlgorithmObject b64 = (VtAlgorithmObject)0;
unsigned char *buffer = (unsigned char *)0;
VtBase64Info b64Info;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
if (inLen < 0)
inLen = Z2Strlen (in);
do
{
b64Info.base64BlockSize = 64;
b64Info.newLineCharacter = VT_BASE64_NEW_LINE_CR_LF;
b64Info.errorCheck = VT_BASE64_NO_ERROR_CHECK;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreateAlgorithmObject (
(VtLibCtx)libCtx, VtAlgorithmImplBase64, (Pointer)&b64Info, &b64);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VtDecodeInit (b64);
if (status != 0)
break;
/* How big does the buffer need to be?
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtDecodeFinal (
b64, (VtRandomObject)0, in, inLen, (unsigned char *)0, 0, &bufferSize);
if (status == 0)
status = VT_ERROR_GENERAL;
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
/* Allocate the buffer to hold the result. Add one byte for a
* NULL-terminating character.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (bufferSize + 1, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtDecodeFinal (
b64, (VtRandomObject)0, in, inLen, buffer, bufferSize,
(unsigned int *)outLen);
if (status != 0)
break;
/* Place a NULL-terminating character, some callers might expect it.
*/
buffer[*outLen] = 0;
*out = buffer;
} while (0);
VtDestroyAlgorithmObject (&b64);
if (status == 0)
return (0);
/* If there was an error, make sure we free what we allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, errorType, fnctLine,
"icBase64Decode", (char *)0)
return (status);
}
int icStrrepl (
char **str,
char *find,
char *replace,
VoltLibCtx *libCtx
)
{
int status, totalLen, findLen, replaceLen, flag;
unsigned int preLen, postLen;
char *retVal, *newBuf;
char *currentLocation;
VOLT_DECLARE_FNCT_LINE (fnctLine)
retVal = *str;
totalLen = (int)Z2Strlen (retVal);
/* Get length of string to be replaced
*/
findLen = (int)Z2Strlen (find);
/* Get length of string to place
*/
replaceLen = (int)Z2Strlen (replace);
/* If they are equal lengths, flag is 0. If find is longer, flag is a
* positive number (it doesn't matter what). If replace is longer,
* flag is -1 (it must be -1, any other negative value will not work).
*/
flag = findLen - replaceLen;
if (flag < 0)
flag = -1;
/* Search through the string for the instrances of strings to be
* replaced
*/
currentLocation = retVal;
status = 0;
do
{
/* Find the next instance of the find.
*/
currentLocation = Z2Strstr (currentLocation, find);
if (currentLocation == (char *)0)
break;
switch (flag)
{
case 0:
/* If the replace string is the same length, Memcpy is all we
* need to do.
*/
Z2Memcpy (currentLocation, replace, replaceLen);
currentLocation += replaceLen;
break;
case -1:
/* The replacement string is longer, we need to reallocate.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
newBuf = (char *)Z3Malloc (totalLen + replaceLen - findLen + 1);
if (newBuf == (char *)0)
break;
/* Copy everything from before the find.
*/
preLen = (unsigned int)(currentLocation - retVal);
postLen = (unsigned int)totalLen - preLen - (unsigned int)findLen;
Z2Memcpy (newBuf, retVal, preLen);
/* Copy the replacement.
*/
Z2Memcpy (newBuf + preLen, replace, replaceLen);
/* Copy everything after the find.
*/
currentLocation = newBuf + preLen + replaceLen;
Z2Memcpy (currentLocation, retVal + preLen + findLen, postLen);
Z2Free (retVal);
retVal = newBuf;
totalLen += (replaceLen - findLen);
retVal[totalLen] = 0;
status = 0;
break;
default:
/* The find string is longer, we need to "compress".
*/
newBuf = retVal;
/* Copy everything from before the find.
*/
preLen = (unsigned int)(currentLocation - retVal);
postLen = (unsigned int)totalLen - preLen - (unsigned int)findLen;
Z2Memcpy (newBuf, retVal, preLen);
/* Copy the replacement.
*/
Z2Memcpy (newBuf + preLen, replace, replaceLen);
/* Copy everything after the find. This is a Memmove because
* the src and dest are part of the same buffer.
*/
currentLocation = newBuf + preLen + replaceLen;
Z2Memmove (currentLocation, retVal + preLen + findLen, postLen);
Z2Free (retVal);
retVal = newBuf;
totalLen -= (findLen - replaceLen);
retVal[totalLen] = 0;
break;
}
if (status != 0)
break;
} while (1);
*str = retVal;
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
"icStrrepl", (char *)0)
return (status);
}
int icSafeStrncpy (
char *dest,
char *src,
int len,
VoltLibCtx *libCtx
)
{
/* Copy len bytes.
*/
Z2Strncpy (dest, src, len);
/* If there's already a NULL-terminating character at the end of
* dest, we're done. How do we know there's a NULL-terminating
* character? If strlen(src) < len. If that's the case, strnlen
* copied strlen(src) bytes, then padded with 0's.
*/
if ((int)Z2Strlen (src) < len)
return (0);
/* If we reach this point in the code, strlen(src) is >= len, so
* strnlen copied len bytes but did not place a NULL-terminating
* character at the end. We need to add one. But we can't add one
* after the len bytes, because the dest buffer is only len bytes
* big. So replace the last byte with a 0.
*/
dest[len - 1] = 0;
return (0);
}
int icStrprependalloc (
char **str,
char *pre,
VoltLibCtx *libCtx
)
{
unsigned int currentLen, preLen;
char *newBuffer = (char *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
currentLen = 0;
if (*str != (char *)0)
currentLen = Z2Strlen (*str);
preLen = 0;
if (pre != (char *)0)
preLen = Z2Strlen (pre);
VOLT_SET_FNCT_LINE (fnctLine)
newBuffer = (char *)Z3Malloc (currentLen + preLen + 1);
if (newBuffer != (char*)0)
{
if (preLen != 0)
Z2Memcpy (newBuffer, pre, preLen);
if (currentLen != 0)
Z2Memcpy (newBuffer + preLen, *str, currentLen);
newBuffer[currentLen + preLen] = 0;
if (*str != (char *)0)
Z2Free (*str);
*str = newBuffer;
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -