📄 prikeyinfo.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "prikeyder.h"
#include "derhelp.h"
#include "errorctx.h"
/* Set up the OpenSSL ASN.1 template.
*/
ASN1_SEQUENCE (Asn1PrivateKeyInfo) =
{
ASN1_SIMPLE (Asn1PrivateKeyInfo, version, ASN1_INTEGER),
ASN1_SIMPLE (Asn1PrivateKeyInfo, algId, Asn1AlgorithmId),
ASN1_SIMPLE (Asn1PrivateKeyInfo, encodedKey, ASN1_OCTET_STRING),
ASN1_OPT (Asn1PrivateKeyInfo, attributes, Asn1Encoded)
} ASN1_SEQUENCE_END (Asn1PrivateKeyInfo);
IMPLEMENT_ASN1_FUNCTIONS (Asn1PrivateKeyInfo)
int VoltEncodePrivateKeyInfoDer (
VoltLibCtx *libCtx,
unsigned char *oid,
unsigned int oidLen,
unsigned char *params,
unsigned int paramsLen,
unsigned char *encodedKey,
unsigned int encodedKeyLen,
unsigned char *keyDer,
unsigned int bufferSize,
unsigned int *keyDerLen
)
{
int status;
unsigned char *temp;
Asn1PrivateKeyInfo *priKeyInfo = (Asn1PrivateKeyInfo *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Create the ASN.1 "object".
*/
status = VT_ERROR_MEMORY;
VOLT_SET_FNCT_LINE (fnctLine)
priKeyInfo = Asn1PrivateKeyInfo_new ();
if (priKeyInfo == (Asn1PrivateKeyInfo *)0)
break;
/* Set the fields.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_INTEGER_set (priKeyInfo->version, 0) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1ObjectId_set (priKeyInfo->algId->oid, oid, oidLen) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1Encoded_setCreate (
&(priKeyInfo->algId->params), params, paramsLen) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_OCTET_STRING_set (
priKeyInfo->encodedKey, encodedKey, encodedKeyLen) != 1)
break;
/* How big does the buffer need to be?
*/
status = VT_ERROR_INVALID_INPUT;
VOLT_SET_FNCT_LINE (fnctLine)
*keyDerLen = i2d_Asn1PrivateKeyInfo (priKeyInfo, (unsigned char **)0);
if (*keyDerLen == 0)
break;
status = VT_ERROR_BUFFER_TOO_SMALL;
VOLT_SET_FNCT_LINE (fnctLine)
if (bufferSize < *keyDerLen)
break;
/* Now encode into the buffer.
*/
status = VT_ERROR_INVALID_INPUT;
temp = keyDer;
VOLT_SET_FNCT_LINE (fnctLine)
*keyDerLen = i2d_Asn1PrivateKeyInfo (priKeyInfo, &temp);
if (*keyDerLen == 0)
break;
status = 0;
} while (0);
if (priKeyInfo != (Asn1PrivateKeyInfo *)0)
Asn1PrivateKeyInfo_free (priKeyInfo);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
fnctLine, "VoltEncodePrivateKeyInfoDer", (char *)0)
return (status);
}
int VoltDecodePriKeyInfoCreate (
VoltLibCtx *libCtx,
unsigned char *encoding,
unsigned int maxEncodingLen,
Asn1PrivateKeyInfo **priKeyInfo
)
{
int status;
Asn1PrivateKeyInfo *newPriKeyInfo = (Asn1PrivateKeyInfo *)0;
unsigned char *temp;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Create the "object".
*/
status = VT_ERROR_MEMORY;
VOLT_SET_FNCT_LINE (fnctLine)
newPriKeyInfo = Asn1PrivateKeyInfo_new ();
if (newPriKeyInfo == (Asn1PrivateKeyInfo *)0)
break;
/* Decode.
*/
status = VT_ERROR_UNKNOWN_BER;
temp = encoding;
VOLT_SET_FNCT_LINE (fnctLine)
d2i_Asn1PrivateKeyInfo (&newPriKeyInfo, &temp, maxEncodingLen);
/* Did it work?
*/
if (newPriKeyInfo == (Asn1PrivateKeyInfo *)0)
break;
/* If successful, return the object.
*/
*priKeyInfo = newPriKeyInfo;
status = 0;
} while (0);
if (status == 0)
return (0);
/* If there was an error, destroy anything we created.
*/
if (newPriKeyInfo != (Asn1PrivateKeyInfo *)0)
Asn1PrivateKeyInfo_free (newPriKeyInfo);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
fnctLine, "VoltDecodePriKeyInfoCreate", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -