OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
DER_Decode.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005-2010 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * DER_Decode.h - DER decoding routines
26 */
27
28#ifndef _DER_DECODE_H_
29#define _DER_DECODE_H_
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#include "libDER.h"
36
37/*
38 * Decoding one item consists of extracting its tag, a pointer
39 * to the actual content, and the length of the content. Those
40 * three are represented by a DERDecodedInfo.
41 */
46
47/*
48 * Basic decoding primitive. Only works with:
49 *
50 * -- definite length encoding
51 * -- one-byte tags
52 * -- max content length fits in a DERSize
53 *
54 * No malloc or copy of the contents is performed; the returned
55 * content->content.data is a pointer into the incoming der data.
56 */
58 const DERItem *der, /* data to decode */
59 DERDecodedInfo *decoded); /* RETURNED */
60
61/*
62 * Given a BIT_STRING, in the form of its raw content bytes,
63 * obtain the number of unused bits and the raw bit string bytes.
64 */
66 const DERItem *contents,
67 DERItem *bitStringBytes, /* RETURNED */
68 DERByte *numUnusedBits); /* RETURNED */
69
70/*
71 * Given a BOOLEAN, in the form of its raw content bytes,
72 * obtain its value.
73 */
75 const DERItem *contents,
76 bool defaultValue,
77 bool *value); /* RETURNED */
78
80 const DERItem *contents,
81 uint32_t *value); /* RETURNED */
82
83/*
84 * Sequence/set decode support.
85 */
86
87/* state representing a sequence or set being decoded */
88typedef struct {
92
93/*
94 * To decode a set or sequence, call DERDecodeSeqInit or
95 * DERDecodeSeqContentInit once, then call DERDecodeSeqNext to
96 * get each enclosed item.
97 *
98 * DERDecodeSeqNext returns DR_EndOfSequence when no more
99 * items are available.
100 */
101
102/*
103 * Use this to parse the top level sequence's tag and content length.
104 */
106 const DERItem *der, /* data to decode */
107 DERTag *tag, /* RETURNED tag of sequence/set. This will be
108 * either ASN1_CONSTR_SEQUENCE or
109 * ASN1_CONSTR_SET. */
110 DERSequence *derSeq); /* RETURNED, to use in DERDecodeSeqNext */
111
112/*
113 * Use this to start in on decoding a sequence's content, when
114 * the top-level tag and content have already been decoded.
115 */
117 const DERItem *content,
118 DERSequence *derSeq); /* RETURNED, to use in DERDecodeSeqNext */
119
120/* obtain the next decoded item in a sequence or set */
122 DERSequence *derSeq,
123 DERDecodedInfo *decoded); /* RETURNED */
124
125/*
126 * High level sequence decode.
127 */
128
129/*
130 * Per-item decode options.
131 */
132
133/* Explicit default, no options */
134#define DER_DEC_NO_OPTS 0x0000
135
136/* This item optional, can be skipped during decode */
137#define DER_DEC_OPTIONAL 0x0001
138
139/* Skip the tag check; accept anything. */
140#define DER_DEC_ASN_ANY 0x0002
141
142/* Skip item, no write to DERDecodedInfo (but tag check still performed) */
143#define DER_DEC_SKIP 0x0004
144
145/* Save full DER encoding in DERDecodedInfo, including tag and length. Normally
146 * only the content is saved. */
147#define DER_DEC_SAVE_DER 0x0008
148
149/*
150 * High level sequence parse, starting with top-level tag and content.
151 * Top level tag must be ASN1_CONSTR_SEQUENCE - if it's not, and that's
152 * OK, use DERParseSequenceContent().
153 *
154 * These never return DR_EndOfSequence - if an *unexpected* end of sequence
155 * occurs, return DR_IncompleteSeq.
156 *
157 * Results of the decoding of one item are placed in a DERItem whose address
158 * is the dest arg plus the offset value in the associated DERItemSpec.
159 *
160 * Items which are optional (DER_DEC_OPTIONAL) and which are not found,
161 * leave their associated DERDecodedInfos unmodified.
162 *
163 * Processing of a sequence ends on detection of any error or after the
164 * last DERItemSpec is processed.
165 *
166 * The sizeToZero argument, if nonzero, indicates the number of bytes
167 * starting at dest to zero before processing the sequence. This is
168 * generally desirable, particularly if there are any DER_DEC_OPTIONAL
169 * items in the sequence; skipped optional items are detected by the
170 * caller via a NULL DERDecodedInfo.content.data; if this hasn't been
171 * explicitly zeroed (generally, by passing a nonzero value of sizeToZero),
172 * skipped items can't be detected.
173 */
175 const DERItem *der,
176 DERShort numItems, /* size of itemSpecs[] */
177 const DERItemSpec *itemSpecs,
178 void *dest, /* DERDecodedInfo(s) here RETURNED */
179 DERSize sizeToZero); /* optional */
180
181/* high level sequence parse, starting with sequence's content */
183 const DERItem *content,
184 DERShort numItems, /* size of itemSpecs[] */
185 const DERItemSpec *itemSpecs,
186 void *dest, /* DERDecodedInfo(s) here RETURNED */
187 DERSize sizeToZero); /* optional */
188
189#ifdef __cplusplus
190}
191#endif
192
193#endif /* _DER_DECODE_H_ */
194
UINT8 value
DERReturn DERParseSequenceContent(const DERItem *content, DERShort numItems, const DERItemSpec *itemSpecs, void *dest, DERSize sizeToZero)
Definition DER_Decode.c:303
DERReturn DERDecodeSeqInit(const DERItem *der, DERTag *tag, DERSequence *derSeq)
Definition DER_Decode.c:211
DERReturn DERDecodeSeqNext(DERSequence *derSeq, DERDecodedInfo *decoded)
Definition DER_Decode.c:251
DERReturn DERDecodeSeqContentInit(const DERItem *content, DERSequence *derSeq)
Definition DER_Decode.c:241
DERReturn DERDecodeItem(const DERItem *der, DERDecodedInfo *decoded)
Definition DER_Decode.c:63
DERReturn DERParseBitString(const DERItem *contents, DERItem *bitStringBytes, DERByte *numUnusedBits)
Definition DER_Decode.c:150
DERReturn DERParseSequence(const DERItem *der, DERShort numItems, const DERItemSpec *itemSpecs, void *dest, DERSize sizeToZero)
Definition DER_Decode.c:281
DERReturn DERParseBoolean(const DERItem *contents, bool defaultValue, bool *value)
Definition DER_Decode.c:172
DERReturn DERParseInteger(const DERItem *contents, uint32_t *value)
Definition DER_Decode.c:188
DERReturn
Definition libDER.h:20
size_t DERSize
uint16_t DERShort
uint64_t DERTag
uint8_t DERByte
UINT32 uint32_t
DERItem content
Definition DER_Decode.h:44
DERByte * end
Definition DER_Decode.h:90
DERByte * nextItem
Definition DER_Decode.h:89