从PkiAsn1Decode函数到ASN1Dec_SignedDataWithBlobs函数
从PkiAsn1Decode函数到ASN1Dec_SignedDataWithBlobs函数
第一部分:
//+-------------------------------------------------------------------------
// Update for decoding a signed message
//--------------------------------------------------------------------------
BOOL
WINAPI
ICM_UpdateDecodingSignedData(
IN OUT PCRYPT_MSG_INFO pcmi,
IN const BYTE *pbData,
IN DWORD cbData)
{
DWORD dwError = ERROR_SUCCESS;
BOOL fRet;
SignedDataWithBlobs *psdb = NULL;
PBYTE pb = NULL;
DWORD cb;
ASN1error_e Asn1Err;
ASN1decoding_t pDec = ICM_GetDecoder();
PBYTE pbDER = NULL;
DWORD cbDER;
ICM_HASH_INFO HashInfo; ZEROSTRUCT(HashInfo);
DWORD dwExceptionCode;
// Handle MappedFile Exceptions
__try {
if (PHASE_FIRST_FINAL == pcmi->dwPhase) {
if (0 != (Asn1Err = PkiAsn1Decode(
pDec,
(void **)&psdb,
SignedDataWithBlobs_PDU,
pbData,
cbData)))
第二部分:
//+-------------------------------------------------------------------------
// Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
// be freed by calling PkiAsn1FreeDecoded().
//--------------------------------------------------------------------------
ASN1error_e
WINAPI
PkiAsn1Decode(
IN ASN1decoding_t pDec,
OUT void **ppvAsn1Info,
IN ASN1uint32_t id,
IN const BYTE *pbEncoded,
IN DWORD cbEncoded
)
{
ASN1error_e Asn1Err;
*ppvAsn1Info = NULL;
Asn1Err = ASN1_Decode(
pDec,
ppvAsn1Info,
id,
ASN1DECODE_SETBUFFER,
(BYTE *) pbEncoded,
cbEncoded
);
if (ASN1_SUCCEEDED(Asn1Err))
Asn1Err = ASN1_SUCCESS;
else {
if (ASN1_ERR_BADARGS == Asn1Err)
Asn1Err = ASN1_ERR_EOD;
*ppvAsn1Info = NULL;
}
return Asn1Err;
}
第三部分:
/* decode a value */
ASN1error_e ASN1_Decode
(
ASN1decoding_t dec,
void **valref,
ASN1uint32_t id,
ASN1uint32_t flags,
ASN1octet_t *pbBuf,
ASN1uint32_t cbBufSize
)
{
else
if (ASN1_BER_RULE & dec->eRule)
{
ASN1BerDecFun_t pfnBER;
/* decode value */
if (NULL != (pfnBER = dec->module->BER.apfnDecoder[id]))
{
if ((*pfnBER)(dec, 0, *valref)) // lonchanc: tag is 0 to make it compiled
{
ASN1BERDecFlush(dec);
}
第四部分:
ASN1module_t ASN1_CreateModule
(
ASN1uint32_t version,
ASN1encodingrule_e eEncodingRule,
ASN1uint32_t dwFlags,
ASN1uint32_t cPDUs,
const ASN1GenericFun_t apfnEncoder[],
const ASN1GenericFun_t apfnDecoder[],
const ASN1FreeFun_t apfnFreeMemory[],
const ASN1uint32_t acbStructSize[],
ASN1magic_t nModuleName
)
{
ASN1module_t module = NULL;
/* compiler output and library version match together? */
if (
// version <= ASN1_THIS_VERSION &&
NULL != apfnEncoder &&
NULL != apfnDecoder &&
NULL != apfnFreeMemory &&
NULL != acbStructSize)
{
if (NULL != (module = (ASN1module_t)MemAlloc(sizeof(*module), nModuleName)))
{
module->nModuleName = nModuleName;
module->eRule = eEncodingRule;
module->dwFlags = dwFlags;
module->cPDUs = cPDUs;
module->apfnFreeMemory = apfnFreeMemory;
module->acbStructSize = acbStructSize;
if (ASN1_PER_RULE & eEncodingRule)
{
module->PER.apfnEncoder = (const ASN1PerEncFun_t *) apfnEncoder;
module->PER.apfnDecoder = (const ASN1PerDecFun_t *) apfnDecoder;
}
#ifdef ENABLE_BER
else
if (ASN1_BER_RULE & eEncodingRule)
{
module->BER.apfnEncoder = (const ASN1BerEncFun_t *) apfnEncoder;
module->BER.apfnDecoder = (const ASN1BerDecFun_t *) apfnDecoder;
}
#endif // ENABLE_BER
}
}
return module;
}
第五部分:
void ASN1CALL PKCS_Module_Startup(void)
{
PKCS_Module = ASN1_CreateModule(0x10000, ASN1_BER_RULE_DER, ASN1FLAGS_NOASSERT, 48, (const ASN1GenericFun_t *) encfntab, (const ASN1GenericFun_t *) decfntab, freefntab, sizetab, 0x73636b70);
}
第六部分:
};
typedef ASN1BerDecFun_t ASN1DecFun_t;
static const ASN1DecFun_t decfntab[48] = {
(ASN1DecFun_t) ASN1Dec_ObjectID, 0
(ASN1DecFun_t) ASN1Dec_ObjectIdentifierType,
(ASN1DecFun_t) ASN1Dec_OctetStringType,
(ASN1DecFun_t) ASN1Dec_IntegerType,
(ASN1DecFun_t) ASN1Dec_HugeIntegerType,
(ASN1DecFun_t) ASN1Dec_AlgorithmIdentifier,
(ASN1DecFun_t) ASN1Dec_AlgorithmIdentifierNC2,
(ASN1DecFun_t) ASN1Dec_AlgorithmIdentifiers,
(ASN1DecFun_t) ASN1Dec_AttributeSetValue,
(ASN1DecFun_t) ASN1Dec_AttributeSetValueNC,
(ASN1DecFun_t) ASN1Dec_SetOfAny, 10
(ASN1DecFun_t) ASN1Dec_AttributeNC2,
(ASN1DecFun_t) ASN1Dec_Attributes,
(ASN1DecFun_t) ASN1Dec_AttributesNC,
(ASN1DecFun_t) ASN1Dec_AttributesNC2,
(ASN1DecFun_t) ASN1Dec_CrlsNC,
(ASN1DecFun_t) ASN1Dec_CertificatesNC,
(ASN1DecFun_t) ASN1Dec_IssuerAndSerialNumber,
(ASN1DecFun_t) ASN1Dec_ContentInfo,
(ASN1DecFun_t) ASN1Dec_ContentInfoNC, 19
(ASN1DecFun_t) ASN1Dec_DigestAlgorithmIdentifiersNC, 20
(ASN1DecFun_t) ASN1Dec_SignerInfos,
(ASN1DecFun_t) ASN1Dec_DigestAlgorithmBlobs,
(ASN1DecFun_t) ASN1Dec_SignerInfosNC,
(ASN1DecFun_t) ASN1Dec_SignerInfoWithAABlobs,
(ASN1DecFun_t) ASN1Dec_SignerInfoWithAABlob,
(ASN1DecFun_t) ASN1Dec_SignerInfoWithAttrBlobs,
(ASN1DecFun_t) ASN1Dec_SignerInfoWithBlobs,
(ASN1DecFun_t) ASN1Dec_RecipientInfos,
(ASN1DecFun_t) ASN1Dec_EncryptedContentInfo,
(ASN1DecFun_t) ASN1Dec_RecipientInfo, 30
(ASN1DecFun_t) ASN1Dec_SignedAndEnvelopedData,
(ASN1DecFun_t) ASN1Dec_DigestedData,
(ASN1DecFun_t) ASN1Dec_EncryptedData,
(ASN1DecFun_t) ASN1Dec_CertIdentifier,
(ASN1DecFun_t) ASN1Dec_OriginatorInfo,
(ASN1DecFun_t) ASN1Dec_OriginatorInfoNC,
(ASN1DecFun_t) ASN1Dec_CmsRecipientInfos,
(ASN1DecFun_t) ASN1Dec_KeyTransRecipientInfo,
(ASN1DecFun_t) ASN1Dec_DigestInfo,
(ASN1DecFun_t) ASN1Dec_SignedData, 40
(ASN1DecFun_t) ASN1Dec_SignerInfo,
(ASN1DecFun_t) ASN1Dec_SignedDataWithBlobs, 42
(ASN1DecFun_t) ASN1Dec_EnvelopedData,
(ASN1DecFun_t) ASN1Dec_CmsEnvelopedData,
(ASN1DecFun_t) ASN1Dec_MailListRecipientInfo,
(ASN1DecFun_t) ASN1Dec_KeyAgreeRecipientInfo,
(ASN1DecFun_t) ASN1Dec_CmsRecipientInfo,
};
第七部分:
chenghao@chenghaodeiMac srv03rtm % grep "SignedDataWithBlobs_PDU" -nr ./ds/security/cryptoapi |grep -v "inary"
./ds/security/cryptoapi/pki/wincrmsg/wincrmsg.cpp:9789: SignedDataWithBlobs_PDU,
./ds/security/cryptoapi/pki/wincrmsg/wincrmsg.cpp:9850: PkiAsn1FreeInfo(pDec, SignedDataWithBlobs_PDU, psdb);
./ds/security/cryptoapi/pki/wincrmsg/wincrmsg.cpp:16372: SignedDataWithBlobs_PDU,
./ds/security/cryptoapi/pki/wincrmsg/pkcs.h:535:#define SignedDataWithBlobs_PDU 42
chenghao@chenghaodeiMac srv03rtm %
第八部分:
static int ASN1CALL ASN1Dec_SignedDataWithBlobs(ASN1decoding_t dec, ASN1uint32_t tag, SignedDataWithBlobs *val)
{
ASN1decoding_t dd;
ASN1octet_t *di;
ASN1uint32_t t;
if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
return 0;
ZeroMemory((val)->o, 1);
if (!ASN1BERDecS32Val(dd, 0x2, &(val)->version))
return 0;
if (!ASN1Dec_DigestAlgorithmIdentifiersNC(dd, 0, &(val)->digestAlgorithms))
return 0;
if (!ASN1Dec_ContentInfoNC(dd, 0, &(val)->contentInfo))
return 0;
ASN1BERDecPeekTag(dd, &t);
if (t == 0x80000000) {
(val)->o[0] |= 0x80;
if (!ASN1Dec_CertificatesNC(dd, 0x80000000, &(val)->certificates))
return 0;
}
ASN1BERDecPeekTag(dd, &t);
if (t == 0x80000001) {
(val)->o[0] |= 0x40;
if (!ASN1Dec_CrlsNC(dd, 0x80000001, &(val)->crls))
return 0;
}
if (!ASN1Dec_SignerInfosNC(dd, 0, &(val)->signerInfos))
return 0;
if (!ASN1BERDecEndOfContents(dec, dd, di))
return 0;
return 1;
}
第九部分:第二个例子A:
BOOL
WINAPI
#ifdef DEBUG_CRYPT_ASN1_MASTER
ICMTest_NewCryptMsgUpdate(
#else
CryptMsgUpdate(
#endif
IN HCRYPTMSG hCryptMsg,
IN const BYTE *pbData,
IN DWORD cbData,
IN BOOL fFinal)
{
if ((PHASE_FIRST_FINAL == pcmi->dwPhase) &&
(0 == pcmi->dwMsgType)) {
if (0 != (Asn1Err = PkiAsn1Decode(
pDec,
(void **)&pci,
ContentInfoNC_PDU,
pbData,
cbData)))
第十部分:第二个例子B:
static int ASN1CALL ASN1Dec_ContentInfoNC(ASN1decoding_t dec, ASN1uint32_t tag, ContentInfoNC *val)
{
ASN1decoding_t dd;
ASN1octet_t *di;
ASN1uint32_t t;
ASN1decoding_t dd0;
ASN1octet_t *di0;
if (!ASN1BERDecExplicitTag(dec, tag ? tag : 0x10, &dd, &di))
return 0;
ZeroMemory((val)->o, 1);
if (!ASN1BERDecObjectIdentifier2(dd, 0x6, &(val)->contentType))
return 0;
if (ASN1BERDecPeekTag(dd, &t)) {
if (t == 0x80000000) {
(val)->o[0] |= 0x80;
if (!ASN1BERDecExplicitTag(dd, 0x80000000, &dd0, &di0))
return 0;
if (!ASN1BERDecOpenType2(dd0, &(val)->content))
return 0;
if (!ASN1BERDecEndOfContents(dd, dd0, di0))
return 0;
}
}
if (!ASN1BERDecEndOfContents(dec, dd, di))
return 0;
return 1;
}