ber_der.h

来自「Next BIOS Source code : Extensible Firmw」· C头文件 代码 · 共 403 行 · 第 1/2 页

H
403
字号
 */
sint32 
BER_DecodeBitString(
	const uint8 *Octets,                  /* pointer to input BER_BIT_STRING octets */
          uint8 *Bytes,                   /* pointer to output buffer */
          uint32 SizeofBytes);            /* size of output buffer in bytes */
/*
 * Function to DER-encode a bit string. The function result is the number of *BYTES* used
 * to DER-encode the bit string. If the output buffer is too small, -1 is returned and it
 * is not changed. Any odd bytes must be in the most significant bits of the trailing byte.
 */
sint32  
DER_EncodeBitString(
	const uint8 *Bytes,                   /* pointer to input bit string */
          uint32 NumberOfBits,            /* length of bit string in *BITS* */
          uint8 *Octets,                  /* pointer to output buffer */
          uint32 SizeofOctets);           /* length of output buffer in BYTES */
/* decode an object identifier into an array of unsigned long integers */
sint32 
BER_OidDecode(                    /* return number of integers found */
    const uint8 *Octets,          /* input BER OBJECT IDENTIFIER */
          uint32 *Numbers,        /* array of unsigned long for output */
          sint32 Dimension);      /* dimension of numbers array */

/* encode an array of unsigned long to a DER object identifier octets */
sint32 
DER_EncodeOid(                    /* return number of bytes used to encode it */
    const uint32 *Numbers,        /* the array of integers */
          sint32 Dimension,       /* number of integers in OID */
          uint8 *Octets);         /* output buffer */

/* expand a single BER item */
const uint8 *
BER_ExpandItem(                   /* return updated octets if successful, 0 if not */
    const uint8 *Octets,          /* input BER to parse */
          uint32 MaxLength,       /* maximum length of BER to parse (may not all be in memory) */
          BER_PARSED_ITEM_PTR Output);        /* output is single structure */

/*
**  find a BER attribute value (still BER), given an OID and a BER-encoded attribute list
**  returns NIL if not found. Assumes SEQ(OID, value) structure somewhere in subtree.
*/
const uint8 *
BER_FindAttribute(          /* find BER attribute value for given OID in attributes */
    BER_HANDLE AppHandle,
    BER_MEMORY_FUNCS_PTR MemoryFunctions,
    const uint8 *Oid,             /* desired BER OID */
    const uint8 *Attributes);     /* BER with SEQ(OID, value) pairs somewhere */
/* return the cardinality of a series of BER items */
sint32
BER_CountItems(
	const uint8 *Octets, 
	      uint32 Length);
/*
**  In order to prevent multiple copying of data, and a general memory allocation free-for-all,
**  lets construct a parse tree, and then traverse it to get the size of the output. A second
**  traversal can then construct the output DER string. So we need a tree node structure...
*/
typedef struct der_complex_struct DER_COMPLEX_TYPE;
typedef DER_COMPLEX_TYPE *DER_COMPLEX_TYPE_PTR;

typedef struct der_explicit_choices {
    sint32 NumberOfChoices;         /* number of tag values */
    sint32 ParentField;             /* tag field (same field means implicit tag) */
    BER_PARSED_ITEM_PTR *Oids;      /* vector of object ID tag values (if implicit--just use tag) */
    DER_COMPLEX_TYPE_PTR *Subtypes; /* corresponding subtypes */
} DER_EXPLICIT_CHOICES, *DER_EXPLICIT_CHOICES_PTR;

typedef struct der_complex_struct {
    const uint8 *TypeTag;                 /* tag for this complex type */
          sint32 NumberOfDefaults;        /* number of fields (SEQUENCE) or types (SET) */
          DER_EXPLICIT_CHOICES_PTR *Choices;  /* if explicitly tagged, pointer to choices for each field*/
    const uint8 **ContextTags;            /* context tags for this type if implicit, else null. */
    const uint8 **OriginalTags;           /* real tags for each field */
          BER_PARSED_ITEM_PTR Defaults;   /* defaults for this type, else null */
          DER_COMPLEX_TYPE_PTR *Subtypes; /* complex subtypes, if any (may be null) */
          char *Name;                     /* name for debugging purposes */
} DER_COMPLEX_STRUCT, *DER_COMPLEX_STRUCT_PTR;

typedef struct der_node_struct DER_NODE;
typedef DER_NODE *DER_NODE_PTR;

typedef struct der_node_child_struct {
    uint8 IsLeaf;
    struct {
        BER_PARSED_ITEM Input;
        DER_NODE_PTR Node;
    } X;
} DER_NODE_CHILD, *DER_NODE_CHILD_PTR;

struct der_node_struct {
    const uint8 *Tag;
    const uint8 *OriginalTag;
    sint32 Count;
    uint32 Length;                  /* filled in by sizeofDERTree */
    DER_COMPLEX_TYPE *Type;
    DER_NODE_CHILD_PTR Child;
};

/* allocate DER_node with  nChildren children */
DER_NODE_PTR 
DER_AllocateNode(BER_HANDLE AppHandle,
                 BER_MEMORY_FUNCS_PTR MemoryFunctions, 
                 sint32 NumberOfChildren);

/* Copy parse tree nodes to create another duplicate tree */
DER_NODE_PTR
DER_DuplicateParseTree(
		BER_HANDLE AppHandle, 
		BER_MEMORY_FUNCS_PTR MemoryFunctions, 
		const DER_NODE *Tree);

/* recursively free DER nodes   */
void 
DER_RecycleTree(BER_HANDLE AppHandle,
                BER_MEMORY_FUNCS_PTR MemoryFunctions,
                DER_NODE_PTR Node);

/* expand one level in parsing a BER ASN.1 sequence */
sint32
BER_ExpandSequence(                           /* return count of items sucessfully parsed */
    const uint8 *Octets,                      /* input BER to parse */
          uint32 InputLength,                 /* length of BER to parse (may not all be in memory) */
          sint32 Count,                       /* input number of components */
    const DER_EXPLICIT_CHOICES_PTR *Choices,  /* explicit tag choices for each field, if any */
    const uint8 **ContextTags,                /* implicit context-specific tags for each component */
    const uint8 **OriginalTags,               /* original tags for each component */
    const BER_PARSED_ITEM *Defaults,          /* default values */
          BER_PARSED_ITEM *Output);           /* output: array of parsed items */

/* expand a BER set one level */
sint32 
BER_ExpandSet(                                /* return count of items in set sucessfully parsed */
    const uint8 *Octets,                      /* input BER to parse */
          uint32 InputLength,                 /* length of BER to parse (may not all be in memory) */
          sint32 MaxCount,                    /* maximum number of items we can accept (card of output) */
          sint32 NumberOfTypes,               /* number of tags */
    const BER_PARSED_ITEM *Defaults,          /* default values */
          BER_PARSED_ITEM_PTR Output);        /* output: array of parsed items */

/*
**  parse complex BER object
**  if the length of the root object is not the length of the octets, we have an error.
*/
DER_NODE_PTR 
BER_ParseComplexObject(                      /* return parse tree */
    BER_HANDLE AppHandle,
    BER_MEMORY_FUNCS_PTR MemoryFunctions,
    DER_COMPLEX_TYPE *Type,          /* type of object */
    BER_PARSED_ITEM *Item);          /* BER object broken into tag, length, content */

/* 
**  Gather up parts of values and concatentate into a larger BER object 
*/
uint32 
DER_ConstructItem(
    const uint8 *BerTag,                      /* top-level tag to output */
          sint32 Count,                       /* number of elements */
          BER_PARSED_ITEM *Inputs,    /* array of parsed items */
          uint8 *Octets);                     /* target memory for the output */
/* 
 * Generate a DER-encoded, flat octet representation of a given parse tree.
 * The input parse tree is *not* modified (a temporary copy is modified).
 * A buffer containing the DER is returned if successful, NULL if unsuccessful.
 * The user is responsible for deallocating the buffer using the memory free function.
 */
uint8 *
DER_Generate(
	BER_HANDLE AppHandle,
    BER_MEMORY_FUNCS_PTR MemoryFunctions,
	const DER_NODE *InputTree);
/*
**  add up sizes of parse tree, so we can reserve a sufficiently large output buffer
**  Eliminate instances of default values for sequence and set nodes (yes, a side effect). 
*/
uint32 
DER_SizeofTree(DER_NODE_PTR Node);

/* 
**  copy tree to contiguous octet buffer--we assume output is sufficiently large
**  Be sure to sort the set tags when traversing the tree.
*/
uint32 
DER_CopyTree(
    DER_NODE_PTR Node,  /* tree to linearize */
    uint8 *Octets);     /* output buffer */

/*
**  return true iff two DER nodes are equivalent
*/
sint32 
DER_EqualNodes(DER_NODE_PTR A, DER_NODE_PTR B);

/*
**  print out DER tree
*/
void 
DER_PrintTree(DER_NODE_PTR Node, sint32 Level);

#endif /* BER_DER_H */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?