📄 subjpubkey.c
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "pubkeyder.h"
#include "derhelp.h"
#include "errorctx.h"
/* Set up the OpenSSL ASN.1 template.
*/
ASN1_SEQUENCE (Asn1SubjectPublicKey) =
{
ASN1_SIMPLE (Asn1SubjectPublicKey, algId, Asn1AlgorithmId),
ASN1_SIMPLE (Asn1SubjectPublicKey, encodedKey, ASN1_BIT_STRING)
} ASN1_SEQUENCE_END (Asn1SubjectPublicKey);
IMPLEMENT_ASN1_FUNCTIONS (Asn1SubjectPublicKey)
int VoltEncodeSubjectPublicKeyDer (
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;
Asn1SubjectPublicKey *subjPubKey = (Asn1SubjectPublicKey *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Create the ASN.1 "object".
*/
status = VT_ERROR_MEMORY;
VOLT_SET_FNCT_LINE (fnctLine)
subjPubKey = Asn1SubjectPublicKey_new ();
if (subjPubKey == (Asn1SubjectPublicKey *)0)
break;
/* Set the fields.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1ObjectId_set (subjPubKey->algId->oid, oid, oidLen) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (Asn1Encoded_setCreate (
&(subjPubKey->algId->params), params, paramsLen) != 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (ASN1_STRING_set (
subjPubKey->encodedKey, encodedKey, encodedKeyLen) != 1)
break;
/* The standards specify that the unused bits will be 0, regardless
* of the values of the trailing bits (this is common in unnamed
* bit strings). However, how does OpenSSL's ASN.1 engine override
* the default behavior of counting the trailing 0 bits? Because
* there is almost no documentation to accompany OpenSSL (who could
* possibly believe that no documentation is remotely acceptable in
* software!?) and the source code is practically uncommented and
* written to be the hands-down winner of the Obfuscated C Code
* Competition, there appears to be no "official" way to solve this
* problem.
* The solution appears to be to set the flags field in the
* ASN1_BIT_STRING struct to ASN1_STRING_FLAG_BITS_LEFT.
* So far, we've tested and it works.
*/
subjPubKey->encodedKey->flags = ASN1_STRING_FLAG_BITS_LEFT;
/* How big does the buffer need to be?
*/
status = VT_ERROR_INVALID_INPUT;
VOLT_SET_FNCT_LINE (fnctLine)
*keyDerLen = i2d_Asn1SubjectPublicKey (subjPubKey, (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_Asn1SubjectPublicKey (subjPubKey, &temp);
if (*keyDerLen == 0)
break;
status = 0;
} while (0);
if (subjPubKey != (Asn1SubjectPublicKey *)0)
Asn1SubjectPublicKey_free (subjPubKey);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
fnctLine, "VoltEncodeSubjectPublicKeyDer", (char *)0)
return (status);
}
int VoltDecodeSubjPubKeyCreate (
VoltLibCtx *libCtx,
unsigned char *encoding,
unsigned int maxEncodingLen,
Asn1SubjectPublicKey **subjPubKey
)
{
int status;
Asn1SubjectPublicKey *newSubjPubKey = (Asn1SubjectPublicKey *)0;
unsigned char *temp;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Create the "object".
*/
status = VT_ERROR_MEMORY;
VOLT_SET_FNCT_LINE (fnctLine)
newSubjPubKey = Asn1SubjectPublicKey_new ();
if (newSubjPubKey == (Asn1SubjectPublicKey *)0)
break;
/* Decode.
*/
status = VT_ERROR_UNKNOWN_BER;
temp = encoding;
VOLT_SET_FNCT_LINE (fnctLine)
d2i_Asn1SubjectPublicKey (&newSubjPubKey, &temp, maxEncodingLen);
/* Did it work?
*/
if (newSubjPubKey == (Asn1SubjectPublicKey *)0)
break;
/* If successful, return the object.
*/
*subjPubKey = newSubjPubKey;
status = 0;
} while (0);
if (status == 0)
return (0);
/* If there was an error, destroy anything we created.
*/
if (newSubjPubKey != (Asn1SubjectPublicKey *)0)
Asn1SubjectPublicKey_free (newSubjPubKey);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY,
fnctLine, "VoltDecodeSubjPubKeyCreate", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -