📄 rsakeytype.c
字号:
status, 0, obj, status, 0, 0,
(char *)0, "BuildRsaPubKeyInfo", fnctLine, (char *)0)
return (status);
}
static int BuildRsaPriKeyInfo (
VoltKeyObject *obj,
VoltRsaPrivateKey *priKey,
unsigned char *buffer,
unsigned int *bufferSize
)
{
int status;
unsigned int totalSize, spaceAvailable;
unsigned int sign, dataLen;
VoltMpIntCtx *mpCtx = obj->mpCtx;
VtRSAPriKeyInfo *keyInfo = (VtRSAPriKeyInfo *)buffer;
unsigned char *currentBuf;
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* spaceAvailable is how much space is currently available.
*/
spaceAvailable = 0;
totalSize = sizeof (VtRSAPriKeyInfo);
if (*bufferSize > sizeof (VtRSAPriKeyInfo))
spaceAvailable = *bufferSize - sizeof (VtRSAPriKeyInfo);
do
{
currentBuf = (unsigned char *)0;
if (spaceAvailable != 0)
currentBuf = (buffer + sizeof (VtRSAPriKeyInfo));
/* Try to get the modulus.
*/
if (priKey->modulus != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->modulus, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->modulus.data = currentBuf;
keyInfo->modulus.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
/* Try to get the public exponent.
*/
if (priKey->pubExpo != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->pubExpo, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->pubExpo.data = currentBuf;
keyInfo->pubExpo.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
/* Try to get the private exponent.
*/
if (priKey->priExpo != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->priExpo, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->priExpo.data = currentBuf;
keyInfo->priExpo.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
/* Try to get the primes.
*/
if (priKey->prime1 != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->prime1, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->prime1.data = currentBuf;
keyInfo->prime1.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
if (priKey->prime2 != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->prime2, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->prime2.data = currentBuf;
keyInfo->prime2.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
/* Try to get the exponents.
*/
if (priKey->expo1 != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->expo1, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->exponent1.data = currentBuf;
keyInfo->exponent1.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
if (priKey->expo2 != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->expo2, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->exponent2.data = currentBuf;
keyInfo->exponent2.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
/* Try to get the coefficient.
*/
if (priKey->coeff != (VoltMpInt *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = mpCtx->MpIntToOctetString (
priKey->coeff, &sign, currentBuf, spaceAvailable, &dataLen);
if (status == 0)
{
keyInfo->coefficient.data = currentBuf;
keyInfo->coefficient.len = dataLen;
currentBuf += dataLen;
spaceAvailable -= dataLen;
}
else
{
/* If not 0, then status should be BufferTooSmall.
*/
if (status != VT_ERROR_BUFFER_TOO_SMALL)
break;
spaceAvailable = 0;
}
totalSize += dataLen;
}
*bufferSize = totalSize;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, obj, status, 0, 0,
(char *)0, "BuildRsaPriKeyInfo", fnctLine, (char *)0)
return (status);
}
int VoltCloneRsaPubKey (
Pointer sourceObject,
Pointer *destObject
)
{
int status;
unsigned int usageFlag;
VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
VtKeyObject newKey = (VtKeyObject)0;
VtRSAPubKeyInfo *getInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* We know the source is an object, is it a key object?
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if (VOLT_OBJECT_TYPE_NOT_EQUAL (sourceObject, VOLT_OBJECT_TYPE_KEY))
break;
/* Is the object an RSA public key.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_RSA)
break;
if ((obj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0)
break;
usageFlag = VT_RSA_KEY_USAGE_SIGN_VERIFY;
if ((obj->keyType & VOLT_KEY_TYPE_ENCRYPT) != 0)
usageFlag = VT_RSA_KEY_USAGE_ENCRYPT_DECRYPT;
/* Get the RSA key data out of the source object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltKeyGetRsaPublic (
(VtKeyObject)obj, usageFlag, (Pointer *)&getInfo);
if (status != 0)
break;
/* Create and set a key object with this data.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreateKeyObject (
obj->voltObject.libraryCtx, VtKeyImplMpCtx, (Pointer)(obj->mpCtx),
&newKey);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (usageFlag == VT_RSA_KEY_USAGE_SIGN_VERIFY)
{
status = VtSetKeyParam (
newKey, VtKeyParamRSAPublicVerify, (Pointer)getInfo);
}
else
{
status = VtSetKeyParam (
newKey, VtKeyParamRSAPublicEncrypt, (Pointer)getInfo);
}
if (status != 0)
break;
/* If all that worked, we have our clone.
*/
*destObject = (Pointer)newKey;
} while (0);
/* If successful, return 0.
*/
if (status == 0)
return (0);
/* If there was an error, destroy anything we created.
*/
VtDestroyKeyObject (&newKey);
VOLT_LOG_ERROR_INFO (
0, obj, status, 0, errorType,
(char *)0, "VoltCloneDsaPubKey", fnctLine, (char *)0)
return (status);
}
int VoltCloneRsaPriKey (
Pointer sourceObject,
Pointer *destObject
)
{
int status;
unsigned int usageFlag;
VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
VtKeyObject newKey = (VtKeyObject)0;
VtRSAPriKeyInfo *getInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* We know the source is an object, is it a key object?
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if (VOLT_OBJECT_TYPE_NOT_EQUAL (sourceObject, VOLT_OBJECT_TYPE_KEY))
break;
/* Is the object an RSA private key.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_RSA)
break;
if ((obj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
break;
usageFlag = VT_RSA_KEY_USAGE_SIGN_VERIFY;
if ((obj->keyType & VOLT_KEY_TYPE_ENCRYPT) != 0)
usageFlag = VT_RSA_KEY_USAGE_ENCRYPT_DECRYPT;
/* Get the RSA key data out of the source object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltKeyGetRsaPrivate (
(VtKeyObject)obj, usageFlag, (Pointer *)&getInfo);
if (status != 0)
break;
/* Create and set a key object with this data.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreateKeyObject (
obj->voltObject.libraryCtx, VtKeyImplMpCtx, (Pointer)(obj->mpCtx),
&newKey);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (usageFlag == VT_RSA_KEY_USAGE_SIGN_VERIFY)
{
status = VtSetKeyParam (
newKey, VtKeyParamRSAPrivateSign, (Pointer)getInfo);
}
else
{
status = VtSetKeyParam (
newKey, VtKeyParamRSAPrivateDecrypt, (Pointer)getInfo);
}
if (status != 0)
break;
/* If all that worked, we have our clone.
*/
*destObject = (Pointer)newKey;
} while (0);
/* If successful, return 0.
*/
if (status == 0)
return (0);
/* If there was an error, destroy anything we created.
*/
VtDestroyKeyObject (&newKey);
VOLT_LOG_ERROR_INFO (
0, obj, status, 0, errorType,
(char *)0, "VoltCloneDsaPriKey", fnctLine, (char *)0)
return (status);
}
void RSAKeyDataDestroy (
Pointer obj,
Pointer ctx
)
{
VoltLibCtx *libCtx;
VoltMpIntCtx *mpCtx;
VoltRsaPublicKey *pubKey;
VoltRsaPrivateKey *priKey;
/* Is there anything to destroy?
*/
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
/* This destructor is used by keys and algorithm objects. Therefore,
* the only thing we know about the obj passed in is that it is a
* VoltObject.
*/
libCtx = (VoltLibCtx *)(((VoltObject *)obj)->libraryCtx);
pubKey = (VoltRsaPublicKey *)ctx;
priKey = (VoltRsaPrivateKey *)ctx;
if (pubKey->modulus != (VoltMpInt *)0)
mpCtx = pubKey->modulus->mpCtx;
/* If this is a private key, there are more private values to destroy.
*/
if (priKey->type == VOLT_KEY_TYPE_PRIVATE)
{
if (priKey->priExpo != (VoltMpInt *)0)
mpCtx = priKey->priExpo->mpCtx;
else if (priKey->prime1 != (VoltMpInt *)0)
mpCtx = priKey->prime1->mpCtx;
if (mpCtx != (VoltMpIntCtx *)0)
{
if (priKey->priExpo != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->priExpo));
if (priKey->prime1 != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->prime1));
if (priKey->prime2 != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->prime2));
if (priKey->expo1 != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->expo1));
if (priKey->expo2 != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->expo2));
if (priKey->coeff != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(priKey->coeff));
}
}
else if (pubKey->type == VOLT_KEY_TYPE_PUBLIC)
{
if (pubKey->modulus != (VoltMpInt *)0)
mpCtx = pubKey->modulus->mpCtx;
}
else
{
/* It's not private, if it's not public, it's not something this
* destructor can destroy.
*/
return;
}
if (mpCtx != (VoltMpIntCtx *)0)
{
if (pubKey->modulus != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(pubKey->modulus));
if (pubKey->pubExpo != (VoltMpInt *)0)
mpCtx->DestroyMpInt (&(pubKey->pubExpo));
}
if (pubKey->keyItems != (VtRSAPubKeyInfo *)0)
Z2Free (pubKey->keyItems);
/* Now free up the memory of the key data struct.
*/
Z2Free (ctx);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -