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