|
@@ -20,6 +20,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
20
|
20
|
|
21
|
21
|
#include <stdint.h>
|
22
|
22
|
#include <stddef.h>
|
|
23
|
+#include <string.h>
|
23
|
24
|
#include <errno.h>
|
24
|
25
|
#include <ipxe/asn1.h>
|
25
|
26
|
|
|
@@ -43,11 +44,23 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
43
|
44
|
#define EINFO_EINVAL_ASN1_LEN \
|
44
|
45
|
__einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )
|
45
|
46
|
|
|
47
|
+/**
|
|
48
|
+ * Invalidate ASN.1 object cursor
|
|
49
|
+ *
|
|
50
|
+ * @v cursor ASN.1 object cursor
|
|
51
|
+ */
|
|
52
|
+void asn1_invalidate_cursor ( struct asn1_cursor *cursor ) {
|
|
53
|
+ static uint8_t asn1_invalid_object[] = { ASN1_END, 0 };
|
|
54
|
+
|
|
55
|
+ cursor->data = asn1_invalid_object;
|
|
56
|
+ cursor->len = 0;
|
|
57
|
+}
|
|
58
|
+
|
46
|
59
|
/**
|
47
|
60
|
* Start parsing ASN.1 object
|
48
|
61
|
*
|
49
|
62
|
* @v cursor ASN.1 object cursor
|
50
|
|
- * @v type Expected type
|
|
63
|
+ * @v type Expected type, or ASN1_ANY
|
51
|
64
|
* @ret len Length of object body, or negative error
|
52
|
65
|
*
|
53
|
66
|
* The object cursor will be updated to point to the start of the
|
|
@@ -67,7 +80,7 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
|
67
|
80
|
}
|
68
|
81
|
|
69
|
82
|
/* Check the tag byte */
|
70
|
|
- if ( *( ( uint8_t * ) cursor->data ) != type ) {
|
|
83
|
+ if ( ( type != ASN1_ANY ) && ( type != asn1_type ( cursor ) ) ) {
|
71
|
84
|
DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
|
72
|
85
|
cursor, type, *( ( uint8_t * ) cursor->data ) );
|
73
|
86
|
return -ENXIO;
|
|
@@ -110,7 +123,7 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
|
110
|
123
|
* Enter ASN.1 object
|
111
|
124
|
*
|
112
|
125
|
* @v cursor ASN.1 object cursor
|
113
|
|
- * @v type Expected type
|
|
126
|
+ * @v type Expected type, or ASN1_ANY
|
114
|
127
|
* @ret rc Return status code
|
115
|
128
|
*
|
116
|
129
|
* The object cursor will be updated to point to the body of the
|
|
@@ -137,7 +150,7 @@ int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
|
137
|
150
|
* Skip ASN.1 object if present
|
138
|
151
|
*
|
139
|
152
|
* @v cursor ASN.1 object cursor
|
140
|
|
- * @v type Expected type
|
|
153
|
+ * @v type Expected type, or ASN1_ANY
|
141
|
154
|
* @ret rc Return status code
|
142
|
155
|
*
|
143
|
156
|
* The object cursor will be updated to point to the next ASN.1
|
|
@@ -168,7 +181,7 @@ int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) {
|
168
|
181
|
* Skip ASN.1 object
|
169
|
182
|
*
|
170
|
183
|
* @v cursor ASN.1 object cursor
|
171
|
|
- * @v type Expected type
|
|
184
|
+ * @v type Expected type, or ASN1_ANY
|
172
|
185
|
* @ret rc Return status code
|
173
|
186
|
*
|
174
|
187
|
* The object cursor will be updated to point to the next ASN.1
|
|
@@ -185,3 +198,42 @@ int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {
|
185
|
198
|
|
186
|
199
|
return 0;
|
187
|
200
|
}
|
|
201
|
+
|
|
202
|
+/**
|
|
203
|
+ * Enter ASN.1 object of any type
|
|
204
|
+ *
|
|
205
|
+ * @v cursor ASN.1 object cursor
|
|
206
|
+ * @ret rc Return status code
|
|
207
|
+ */
|
|
208
|
+int asn1_enter_any ( struct asn1_cursor *cursor ) {
|
|
209
|
+ return asn1_enter ( cursor, ASN1_ANY );
|
|
210
|
+}
|
|
211
|
+
|
|
212
|
+/**
|
|
213
|
+ * Skip ASN.1 object of any type
|
|
214
|
+ *
|
|
215
|
+ * @v cursor ASN.1 object cursor
|
|
216
|
+ * @ret rc Return status code
|
|
217
|
+ */
|
|
218
|
+int asn1_skip_any ( struct asn1_cursor *cursor ) {
|
|
219
|
+ return asn1_skip ( cursor, ASN1_ANY );
|
|
220
|
+}
|
|
221
|
+
|
|
222
|
+/**
|
|
223
|
+ * Compare two ASN.1 objects
|
|
224
|
+ *
|
|
225
|
+ * @v cursor1 ASN.1 object cursor
|
|
226
|
+ * @v cursor2 ASN.1 object cursor
|
|
227
|
+ * @ret difference Difference as returned by memcmp()
|
|
228
|
+ *
|
|
229
|
+ * Note that invalid and empty cursors will compare as equal with each
|
|
230
|
+ * other.
|
|
231
|
+ */
|
|
232
|
+int asn1_compare ( const struct asn1_cursor *cursor1,
|
|
233
|
+ const struct asn1_cursor *cursor2 ) {
|
|
234
|
+ int difference;
|
|
235
|
+
|
|
236
|
+ difference = ( cursor2->len - cursor1->len );
|
|
237
|
+ return ( difference ? difference :
|
|
238
|
+ memcmp ( cursor1->data, cursor2->data, cursor1->len ) );
|
|
239
|
+}
|