is_bad.c
来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 499 行 · 第 1/2 页
C
499 行
/*-----------------------------------------------------------------------
* File: is_bad.c
*
Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
*-----------------------------------------------------------------------
*/
/*
* INTEL CONFIDENTIAL
* This file, software, or program is supplied under the terms of a
* license agreement or nondisclosure agreement with Intel Corporation
* and may not be copied or disclosed except in accordance with the
* terms of that agreement. This file, software, or program contains
* copyrighted material and/or trade secret information of Intel
* Corporation, and must be treated as such. Intel reserves all rights
* in this material, except as the license agreement or nondisclosure
* agreement specifically indicate.
*/
/*
* WARNING: EXPORT RESTRICTED.
* This software is subject to the U.S. Export Administration Regulations
* and other U.S. law, and may not be exported or re-exported to certain
* countries (currently Afghanistan (Taliban-controlled areas), Cuba, Iran,
* Iraq, Libya, North Korea, Serbia (except Kosovo), Sudan and Syria) or to
* persons or entities prohibited from receiving U.S. exports (including Denied
* Parties, Specially Designated Nationals, and entities on the Bureau of
* Export Administration Entity List or involved with missile technology or
* nuclear, chemical or biological weapons).
*/
/*
* This file verifies that the parse tree input to cl_IsBadCertificateParseTree
* corresponds to the parse tree expected for an X.509 certificate.
*/
#include "x_fndefs.h"
#define NUM_OPTIONAL_TBSCERT_FIELDS 4 /* A TbsCert has 4 optional fields, */
/* Version. issuerUID, subjectUID, extns*/
#define NUM_TBSCERT_VERSION_FIELDS 1 /* The Version sequence has one field, */
/* the version itself. */
#define NUM_TBSCERT_VALIDITY_FIELDS 2 /* The Validity sequence has two fields,*/
/* the startDate and endDate */
#define NUM_TBSCERT_SPKI_FIELDS 2 /* The SPKI sequence has two fields, */
/* the KPG AlgorithmId and the SPK. */
#define TBSCERT_VERSION_LENGTH 1 /* The TbsCert Version is of length = 1 */
/*-----------------------------------------------------------------------------
* Name: cl_IsBadDerNodeChild
*
* Description:
* For the input DerNodeChild, this function checks
* 1) whether its BER parsed item representation contains
* the input universal Tag and some content
* 2) whether the IsLeaf flag and Node pointer are consistent with the Tag
*
* Parameters:
* Child (input) : The DerNodeChild to be checked
* Tag (input) : The Tag value that should be in the DerNodeChild
*
* Return value:
* A TRUE/FALSE indicator of whether this is a valid DerNodeChild
*
* Error Codes:
* None
*---------------------------------------------------------------------------*/
CSSM_BOOL cl_IsBadDerNodeChild(struct der_node_child_struct * Child, uint8 Tag)
{
/* Child existence is verified by the calling function */
/* Check that the input DerNodeChild contains */
/* the input universal Tag and some Content */
if (BER_LengthOfTag(Child->X.Input.Tag) != BER_UNIVERSAL_TAG_LENGTH ||
*Child->X.Input.Tag != Tag ||
!Child->X.Input.ContentLength ||
!Child->X.Input.Content)
return CSSM_TRUE;
/* Check that the IsLeaf flag and Node pointer are consistent with the Tag */
if (Tag & BER_CONSTRUCTED) { /* If this is a constructed Tag,*/
if (Child->IsLeaf || !Child->X.Node) /* Verify that the Node exists */
return CSSM_TRUE;
} else { /* Otherwise, */
if (!Child->IsLeaf || Child->X.Node) /* Verify that IsLeaf == TRUE */
return CSSM_TRUE;
}
return CSSM_FALSE;
}
/*-----------------------------------------------------------------------------
* Name: cl_IsBadOidValueParseTree
*
* Description:
* This function checks whether this is a valid OID/value parse tree,
* such as an AlgorithmId parse tree or an Attribute type/value parse tree
*
* AlgorithmIdentifier ::= SEQUENCE {
* algorithm ALGORITHM.&id ({SupportedAlgorithms}),
* parameters ALGORITHM.&Type ({SupportedAlgorithms}{ @algorithm}) OPTIONAL }
*
* AttributeTypeAndValue ::= SEQUENCE
* type ATTRIBUTE.&id ({SupportedAttributes}),
* value ({ATTRIBUTE.&Type ({SupportedAttributes}{@type})}
*
* Parameters:
* ParentNode (input) : The top node in the parse tree to be checked
*
* Return value:
* A TRUE/FALSE indicator of whether this is a valid OID/value parse tree
*
* Error Codes:
* None
*---------------------------------------------------------------------------*/
CSSM_BOOL cl_IsBadOidValueParseTree(DER_NODE_PTR ParentNode)
{
/* ParentNode existence as a SEQUENCE is checked by the calling function */
/* Verify that it has either 1 child (the OID) or 2 children (OID+Value) */
if (ParentNode->Count < MIN_NUM_ALGID_FIELDS ||
ParentNode->Count > MAX_NUM_ALGID_FIELDS)
return CSSM_TRUE;
/* Verify that the first child is an OID */
return cl_IsBadDerNodeChild(
&ParentNode->Child[ALGID_ALGORITHM], BER_OBJECT_IDENTIFIER);
}
/*-----------------------------------------------------------------------------
* Name: cl_IsBadNameParseTree
*
* Description:
* This function checks whether this parse tree conforms
* to the Name structure defined by the X.509 specification.
*
* Name ::= SEQUENCE OF RelativeDistinguishedName
* RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue
* AttributeTypeAndValue ::= SEQUENCE
*
* Parameters:
* ParentNode (input) : The top node in the parse tree to be checked
*
* Return value:
* A TRUE/FALSE indicator of whether this is a valid Name parse tree
*
* Error Codes:
* None
*---------------------------------------------------------------------------*/
CSSM_BOOL cl_IsBadNameParseTree(DER_NODE_PTR ParentNode)
{
DER_NODE_PTR current_rdn;
sint32 i,j;
/* ParentNode existence as a SEQUENCE is checked by the calling function */
/* Verify that each RDN is a SET */
for (i=0; i < ParentNode->Count; i++)
{
if (cl_IsBadDerNodeChild(&ParentNode->Child[i], BER_CONSTRUCTED_SET))
return CSSM_TRUE;
/* Verify that each type/value pair is an OID/value SEQUENCE */
current_rdn = ParentNode->Child[i].X.Node;
for (j=0; j < current_rdn->Count; j++)
{
if (cl_IsBadDerNodeChild(¤t_rdn->Child[j],
BER_CONSTRUCTED_SEQUENCE))
return CSSM_TRUE;
if (cl_IsBadOidValueParseTree(current_rdn->Child[j].X.Node))
return CSSM_TRUE;
} /* Loop to next type/value pair */
} /* Loop to next RDN */
return CSSM_FALSE;
}
/*-----------------------------------------------------------------------------
* Name: cl_DetermineOptionalFieldExistence
*
* Description:
* In X.509 certificates, the version, issuerUID, subjectUID, and extensions
* fields do not necessarily appear in the DER-encoded representation.
*
* This function fills in an array that indicates which optional fields
* are present. A field that is not present will not have any values for
* the Tag, ContentLength, Content, and Node. Otherwise, it is assumed present.
*
* For required fields, this function assumes that they are present.
*
* The Tag, ContentLength, Content, Node, and subtree values will be checked
* for all present fields as part of cl_IsBadTbsCertParseTree.
*
* Parameters:
* ChildrenArray (input) : The TbsCert ChildArray
* Existence (output) : The array to be filled in with Boolean indicators
* of whether or not a particular child exists
*
* Return value:
* None
*
* Error Codes:
* None
*---------------------------------------------------------------------------*/
void cl_DetermineOptionalFieldExistence(
struct der_node_child_struct * ChildrenArray,
CSSM_BOOL *Existence)
{
uint32 i, opt_fields[] = {TBSCERT_VERSION,
TBSCERT_ISSUER_UID,
TBSCERT_SUBJECT_UID,
TBSCERT_EXTENSIONS};
/* Set all the fields to existence != FALSE */
cssm_memset(Existence, 0xFF, NUM_TBSCERT_FIELDS * sizeof(CSSM_BOOL));
/* Check for the absence of optional fields */
for (i=0; i < NUM_OPTIONAL_TBSCERT_FIELDS; i++)
{
if (!ChildrenArray[opt_fields[i]].X.Input.Tag &&
!ChildrenArray[opt_fields[i]].X.Input.ContentLength &&
!ChildrenArray[opt_fields[i]].X.Input.Content &&
!ChildrenArray[opt_fields[i]].X.Node)
Existence[opt_fields[i]] = CSSM_FALSE;
}
}
/*-----------------------------------------------------------------------------
* Name: cl_IsBadTbsCertParseTree
*
* Description:
* This function checks whether this parse tree conforms
* to the tbsCert structure defined by the X.509 specification.
*
* tbsCert ::= SEQUENCE {
* version [0] Version DEFAULT v1,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?