/src/boringssl/include/openssl/asn1.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef OPENSSL_HEADER_ASN1_H |
16 | | #define OPENSSL_HEADER_ASN1_H |
17 | | |
18 | | #include <openssl/base.h> // IWYU pragma: export |
19 | | |
20 | | #include <time.h> |
21 | | |
22 | | #include <openssl/bio.h> |
23 | | #include <openssl/bn.h> |
24 | | #include <openssl/stack.h> |
25 | | |
26 | | #if defined(__cplusplus) |
27 | | extern "C" { |
28 | | #endif |
29 | | |
30 | | |
31 | | // Legacy ASN.1 library. |
32 | | // |
33 | | // This header is part of OpenSSL's ASN.1 implementation. It is retained for |
34 | | // compatibility but should not be used by new code. The functions are difficult |
35 | | // to use correctly, and have buggy or non-standard behaviors. They are thus |
36 | | // particularly prone to behavior changes and API removals, as BoringSSL |
37 | | // iterates on these issues. |
38 | | // |
39 | | // Use the new |CBS| and |CBB| library in <openssl/bytestring.h> instead. |
40 | | |
41 | | |
42 | | // Tag constants. |
43 | | // |
44 | | // These constants are used in various APIs to specify ASN.1 types and tag |
45 | | // components. See the specific API's documentation for details on which values |
46 | | // are used and how. |
47 | | |
48 | | // The following constants are tag classes. |
49 | 2.66M | #define V_ASN1_UNIVERSAL 0x00 |
50 | | #define V_ASN1_APPLICATION 0x40 |
51 | | #define V_ASN1_CONTEXT_SPECIFIC 0x80 |
52 | 331k | #define V_ASN1_PRIVATE 0xc0 |
53 | | |
54 | | // V_ASN1_CONSTRUCTED indicates an element is constructed, rather than |
55 | | // primitive. |
56 | 603k | #define V_ASN1_CONSTRUCTED 0x20 |
57 | | |
58 | | // V_ASN1_PRIMITIVE_TAG is the highest tag number which can be encoded in a |
59 | | // single byte. Note this is unrelated to whether an element is constructed or |
60 | | // primitive. |
61 | | // |
62 | | // TODO(davidben): Make this private. |
63 | 331k | #define V_ASN1_PRIMITIVE_TAG 0x1f |
64 | | |
65 | | // V_ASN1_MAX_UNIVERSAL is the highest supported universal tag number. It is |
66 | | // necessary to avoid ambiguity with |V_ASN1_NEG| and |MBSTRING_FLAG|. |
67 | | // |
68 | | // TODO(davidben): Make this private. |
69 | 513k | #define V_ASN1_MAX_UNIVERSAL 0xff |
70 | | |
71 | | // V_ASN1_UNDEF is used in some APIs to indicate an ASN.1 element is omitted. |
72 | 0 | #define V_ASN1_UNDEF (-1) |
73 | | |
74 | | // V_ASN1_OTHER is used in |ASN1_TYPE| to indicate a non-universal ASN.1 type. |
75 | 2.56M | #define V_ASN1_OTHER (-3) |
76 | | |
77 | | // V_ASN1_ANY is used by the ASN.1 templates to indicate an ANY type. |
78 | 2.22M | #define V_ASN1_ANY (-4) |
79 | | |
80 | | // V_ASN1_ANY_AS_STRING is used by the ASN.1 templates to indicate an ANY type |
81 | | // represented with |ASN1_STRING| instead of |ASN1_TYPE|. |
82 | 1.67M | #define V_ASN1_ANY_AS_STRING (-5) |
83 | | |
84 | | // The following constants are tag numbers for universal types. |
85 | | #define V_ASN1_EOC 0 |
86 | 1.67M | #define V_ASN1_BOOLEAN 1 |
87 | 413k | #define V_ASN1_INTEGER 2 |
88 | 182k | #define V_ASN1_BIT_STRING 3 |
89 | 326k | #define V_ASN1_OCTET_STRING 4 |
90 | 221k | #define V_ASN1_NULL 5 |
91 | 1.06M | #define V_ASN1_OBJECT 6 |
92 | | #define V_ASN1_OBJECT_DESCRIPTOR 7 |
93 | | #define V_ASN1_EXTERNAL 8 |
94 | | #define V_ASN1_REAL 9 |
95 | 451k | #define V_ASN1_ENUMERATED 10 |
96 | 692k | #define V_ASN1_UTF8STRING 12 |
97 | 2.97M | #define V_ASN1_SEQUENCE 16 |
98 | 2.62M | #define V_ASN1_SET 17 |
99 | 325k | #define V_ASN1_NUMERICSTRING 18 |
100 | 361k | #define V_ASN1_PRINTABLESTRING 19 |
101 | 342k | #define V_ASN1_T61STRING 20 |
102 | | #define V_ASN1_TELETEXSTRING 20 |
103 | 328k | #define V_ASN1_VIDEOTEXSTRING 21 |
104 | 397k | #define V_ASN1_IA5STRING 22 |
105 | 567k | #define V_ASN1_UTCTIME 23 |
106 | 554k | #define V_ASN1_GENERALIZEDTIME 24 |
107 | 418k | #define V_ASN1_GRAPHICSTRING 25 |
108 | 16.0k | #define V_ASN1_ISO64STRING 26 |
109 | 421k | #define V_ASN1_VISIBLESTRING 26 |
110 | 442k | #define V_ASN1_GENERALSTRING 27 |
111 | 549k | #define V_ASN1_UNIVERSALSTRING 28 |
112 | 553k | #define V_ASN1_BMPSTRING 30 |
113 | | |
114 | | // The following constants are used for |ASN1_STRING| values that represent |
115 | | // negative INTEGER and ENUMERATED values. See |ASN1_STRING| for more details. |
116 | 585k | #define V_ASN1_NEG 0x100 |
117 | 237k | #define V_ASN1_NEG_INTEGER (V_ASN1_INTEGER | V_ASN1_NEG) |
118 | 222k | #define V_ASN1_NEG_ENUMERATED (V_ASN1_ENUMERATED | V_ASN1_NEG) |
119 | | |
120 | | // The following constants are bitmask representations of ASN.1 types. |
121 | 561 | #define B_ASN1_NUMERICSTRING 0x0001 |
122 | 286k | #define B_ASN1_PRINTABLESTRING 0x0002 |
123 | 275k | #define B_ASN1_T61STRING 0x0004 |
124 | | #define B_ASN1_TELETEXSTRING 0x0004 |
125 | 121 | #define B_ASN1_VIDEOTEXSTRING 0x0008 |
126 | 275k | #define B_ASN1_IA5STRING 0x0010 |
127 | 67 | #define B_ASN1_GRAPHICSTRING 0x0020 |
128 | 396 | #define B_ASN1_ISO64STRING 0x0040 |
129 | 39.2k | #define B_ASN1_VISIBLESTRING 0x0040 |
130 | 467 | #define B_ASN1_GENERALSTRING 0x0080 |
131 | 59.9k | #define B_ASN1_UNIVERSALSTRING 0x0100 |
132 | 162 | #define B_ASN1_OCTET_STRING 0x0200 |
133 | 1.81k | #define B_ASN1_BIT_STRING 0x0400 |
134 | 276k | #define B_ASN1_BMPSTRING 0x0800 |
135 | 81.0k | #define B_ASN1_UTF8STRING 0x2000 |
136 | 7.50k | #define B_ASN1_UTCTIME 0x4000 |
137 | 1 | #define B_ASN1_GENERALIZEDTIME 0x8000 |
138 | 1.44k | #define B_ASN1_SEQUENCE 0x10000 |
139 | | |
140 | | // ASN1_tag2bit converts |tag| from the tag number of a universal type to a |
141 | | // corresponding |B_ASN1_*| constant, or zero if |tag| has no bitmask. |
142 | | OPENSSL_EXPORT unsigned long ASN1_tag2bit(int tag); |
143 | | |
144 | | // ASN1_tag2str returns a string representation of |tag|, interpret as a tag |
145 | | // number for a universal type, or |V_ASN1_NEG_*|. |
146 | | OPENSSL_EXPORT const char *ASN1_tag2str(int tag); |
147 | | |
148 | | |
149 | | // API conventions. |
150 | | // |
151 | | // The following sample functions document the calling conventions used by |
152 | | // legacy ASN.1 APIs. |
153 | | |
154 | | #if 0 // Sample functions |
155 | | |
156 | | // d2i_SAMPLE parses a structure from up to |len| bytes at |*inp|. On success, |
157 | | // it advances |*inp| by the number of bytes read and returns a newly-allocated |
158 | | // |SAMPLE| object containing the parsed structure. If |out| is non-NULL, it |
159 | | // additionally frees the previous value at |*out| and updates |*out| to the |
160 | | // result. If parsing or allocating the result fails, it returns NULL. |
161 | | // |
162 | | // This function does not reject trailing data in the input. This allows the |
163 | | // caller to parse a sequence of concatenated structures. Callers parsing only |
164 | | // one structure should check for trailing data by comparing the updated |*inp| |
165 | | // with the end of the input. |
166 | | // |
167 | | // Note: If |out| and |*out| are both non-NULL, the object at |*out| is not |
168 | | // updated in-place. Instead, it is freed, and the pointer is updated to the |
169 | | // new object. This differs from OpenSSL. Callers are recommended to set |out| |
170 | | // to NULL and instead use the return value. |
171 | | SAMPLE *d2i_SAMPLE(SAMPLE **out, const uint8_t **inp, long len); |
172 | | |
173 | | // i2d_SAMPLE marshals |in|. On error, it returns a negative value. On success, |
174 | | // it returns the length of the result and outputs it via |outp| as follows: |
175 | | // |
176 | | // If |outp| is NULL, the function writes nothing. This mode can be used to size |
177 | | // buffers. |
178 | | // |
179 | | // If |outp| is non-NULL but |*outp| is NULL, the function sets |*outp| to a |
180 | | // newly-allocated buffer containing the result. The caller is responsible for |
181 | | // releasing |*outp| with |OPENSSL_free|. This mode is recommended for most |
182 | | // callers. |
183 | | // |
184 | | // If |outp| and |*outp| are non-NULL, the function writes the result to |
185 | | // |*outp|, which must have enough space available, and advances |*outp| just |
186 | | // past the output. |
187 | | // |
188 | | // WARNING: In the third mode, the function does not internally check output |
189 | | // bounds. Failing to correctly size the buffer will result in a potentially |
190 | | // exploitable memory error. |
191 | | int i2d_SAMPLE(const SAMPLE *in, uint8_t **outp); |
192 | | |
193 | | #endif // Sample functions |
194 | | |
195 | | // The following typedefs are sometimes used for pointers to functions like |
196 | | // |d2i_SAMPLE| and |i2d_SAMPLE|. Note, however, that these act on |void*|. |
197 | | // Calling a function with a different pointer type is undefined in C, so this |
198 | | // is only valid with a wrapper. |
199 | | typedef void *d2i_of_void(void **, const unsigned char **, long); |
200 | | typedef int i2d_of_void(const void *, unsigned char **); |
201 | | |
202 | | |
203 | | // ASN.1 types. |
204 | | // |
205 | | // An |ASN1_ITEM| represents an ASN.1 type and allows working with ASN.1 types |
206 | | // generically. |
207 | | // |
208 | | // |ASN1_ITEM|s use a different namespace from C types and are accessed via |
209 | | // |ASN1_ITEM_*| macros. So, for example, |ASN1_OCTET_STRING| is both a C type |
210 | | // and the name of an |ASN1_ITEM|, referenced as |
211 | | // |ASN1_ITEM_rptr(ASN1_OCTET_STRING)|. |
212 | | // |
213 | | // Each |ASN1_ITEM| has a corresponding C type, typically with the same name, |
214 | | // which represents values in the ASN.1 type. This type is either a pointer type |
215 | | // or |ASN1_BOOLEAN|. When it is a pointer, NULL pointers represent omitted |
216 | | // values. For example, an OCTET STRING value is declared with the C type |
217 | | // |ASN1_OCTET_STRING*| and uses the |ASN1_ITEM| named |ASN1_OCTET_STRING|. An |
218 | | // OPTIONAL OCTET STRING uses the same C type and represents an omitted value |
219 | | // with a NULL pointer. |ASN1_BOOLEAN| is described in a later section. |
220 | | |
221 | | // DECLARE_ASN1_ITEM declares an |ASN1_ITEM| with name |name|. The |ASN1_ITEM| |
222 | | // may be referenced with |ASN1_ITEM_rptr|. Uses of this macro should document |
223 | | // the corresponding ASN.1 and C types. |
224 | | #define DECLARE_ASN1_ITEM(name) extern OPENSSL_EXPORT const ASN1_ITEM name##_it; |
225 | | |
226 | | // ASN1_ITEM_rptr returns the |const ASN1_ITEM *| named |name|. |
227 | 295k | #define ASN1_ITEM_rptr(name) (&(name##_it)) |
228 | | |
229 | | // ASN1_ITEM_EXP is an abstraction for referencing an |ASN1_ITEM| in a |
230 | | // constant-initialized structure, such as a method table. It exists because, on |
231 | | // some OpenSSL platforms, |ASN1_ITEM| references are indirected through |
232 | | // functions. Structures reference the |ASN1_ITEM| by declaring a field like |
233 | | // |ASN1_ITEM_EXP *item| and initializing it with |ASN1_ITEM_ref|. |
234 | | typedef const ASN1_ITEM ASN1_ITEM_EXP; |
235 | | |
236 | | // ASN1_ITEM_ref returns an |ASN1_ITEM_EXP*| for the |ASN1_ITEM| named |name|. |
237 | | #define ASN1_ITEM_ref(name) (&(name##_it)) |
238 | | |
239 | | // ASN1_ITEM_ptr converts |iptr|, which must be an |ASN1_ITEM_EXP*| to a |
240 | | // |const ASN1_ITEM*|. |
241 | 3.31M | #define ASN1_ITEM_ptr(iptr) (iptr) |
242 | | |
243 | | // ASN1_VALUE_st (aka |ASN1_VALUE|) is an opaque type used as a placeholder for |
244 | | // the C type corresponding to an |ASN1_ITEM|. |
245 | | typedef struct ASN1_VALUE_st ASN1_VALUE; |
246 | | |
247 | | // ASN1_item_new allocates a new value of the C type corresponding to |it|, or |
248 | | // NULL on error. On success, the caller must release the value with |
249 | | // |ASN1_item_free|, or the corresponding C type's free function, when done. The |
250 | | // new value will initialize fields of the value to some default state, such as |
251 | | // an empty string. Note, however, that this default state sometimes omits |
252 | | // required values, such as with CHOICE types. |
253 | | // |
254 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
255 | | // |ASN1_BOOLEAN|. |
256 | | // |
257 | | // WARNING: Casting the result of this function to the wrong type is a |
258 | | // potentially exploitable memory error. Callers must ensure the value is used |
259 | | // consistently with |it|. Prefer using type-specific functions such as |
260 | | // |ASN1_OCTET_STRING_new|. |
261 | | OPENSSL_EXPORT ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it); |
262 | | |
263 | | // ASN1_item_free releases memory associated with |val|, which must be an object |
264 | | // of the C type corresponding to |it|. |
265 | | // |
266 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
267 | | // |ASN1_BOOLEAN|. |
268 | | // |
269 | | // WARNING: Passing a pointer of the wrong type into this function is a |
270 | | // potentially exploitable memory error. Callers must ensure |val| is consistent |
271 | | // with |it|. Prefer using type-specific functions such as |
272 | | // |ASN1_OCTET_STRING_free|. |
273 | | OPENSSL_EXPORT void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it); |
274 | | |
275 | | // ASN1_item_d2i parses the ASN.1 type |it| from up to |len| bytes at |*inp|. |
276 | | // It behaves like |d2i_SAMPLE|, except that |out| and the return value are cast |
277 | | // to |ASN1_VALUE| pointers. |
278 | | // |
279 | | // TODO(https://crbug.com/boringssl/444): C strict aliasing forbids type-punning |
280 | | // |T*| and |ASN1_VALUE*| the way this function signature does. When that bug is |
281 | | // resolved, we will need to pick which type |*out| is (probably |T*|). Do not |
282 | | // use a non-NULL |out| to avoid ending up on the wrong side of this question. |
283 | | // |
284 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
285 | | // |ASN1_BOOLEAN|. |
286 | | // |
287 | | // WARNING: Casting the result of this function to the wrong type, or passing a |
288 | | // pointer of the wrong type into this function, are potentially exploitable |
289 | | // memory errors. Callers must ensure |out| is consistent with |it|. Prefer |
290 | | // using type-specific functions such as |d2i_ASN1_OCTET_STRING|. |
291 | | OPENSSL_EXPORT ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **out, |
292 | | const unsigned char **inp, long len, |
293 | | const ASN1_ITEM *it); |
294 | | |
295 | | // ASN1_item_i2d marshals |val| as the ASN.1 type associated with |it|, as |
296 | | // described in |i2d_SAMPLE|. |
297 | | // |
298 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
299 | | // |ASN1_BOOLEAN|. |
300 | | // |
301 | | // WARNING: Passing a pointer of the wrong type into this function is a |
302 | | // potentially exploitable memory error. Callers must ensure |val| is consistent |
303 | | // with |it|. Prefer using type-specific functions such as |
304 | | // |i2d_ASN1_OCTET_STRING|. |
305 | | OPENSSL_EXPORT int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **outp, |
306 | | const ASN1_ITEM *it); |
307 | | |
308 | | // ASN1_item_dup returns a newly-allocated copy of |x|, or NULL on error. |x| |
309 | | // must be an object of |it|'s C type. |
310 | | // |
311 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
312 | | // |ASN1_BOOLEAN|. |
313 | | // |
314 | | // WARNING: Casting the result of this function to the wrong type, or passing a |
315 | | // pointer of the wrong type into this function, are potentially exploitable |
316 | | // memory errors. Prefer using type-specific functions such as |
317 | | // |ASN1_STRING_dup|. |
318 | | OPENSSL_EXPORT void *ASN1_item_dup(const ASN1_ITEM *it, void *x); |
319 | | |
320 | | // The following functions behave like |ASN1_item_d2i| but read from |in| |
321 | | // instead. |out| is the same parameter as in |ASN1_item_d2i|, but written with |
322 | | // |void*| instead. The return values similarly match. |
323 | | // |
324 | | // These functions may not be used with |ASN1_ITEM|s whose C type is |
325 | | // |ASN1_BOOLEAN|. |
326 | | // |
327 | | // WARNING: These functions do not bound how much data is read from |in|. |
328 | | // Parsing an untrusted input could consume unbounded memory. |
329 | | OPENSSL_EXPORT void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *out); |
330 | | OPENSSL_EXPORT void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *out); |
331 | | |
332 | | // The following functions behave like |ASN1_item_i2d| but write to |out| |
333 | | // instead. |in| is the same parameter as in |ASN1_item_i2d|, but written with |
334 | | // |void*| instead. |
335 | | // |
336 | | // These functions may not be used with |ASN1_ITEM|s whose C type is |
337 | | // |ASN1_BOOLEAN|. |
338 | | OPENSSL_EXPORT int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *in); |
339 | | OPENSSL_EXPORT int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *in); |
340 | | |
341 | | // ASN1_item_unpack parses |oct|'s contents as |it|'s ASN.1 type. It returns a |
342 | | // newly-allocated instance of |it|'s C type on success, or NULL on error. |
343 | | // |
344 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
345 | | // |ASN1_BOOLEAN|. |
346 | | // |
347 | | // WARNING: Casting the result of this function to the wrong type is a |
348 | | // potentially exploitable memory error. Callers must ensure the value is used |
349 | | // consistently with |it|. |
350 | | OPENSSL_EXPORT void *ASN1_item_unpack(const ASN1_STRING *oct, |
351 | | const ASN1_ITEM *it); |
352 | | |
353 | | // ASN1_item_pack marshals |obj| as |it|'s ASN.1 type. If |out| is NULL, it |
354 | | // returns a newly-allocated |ASN1_STRING| with the result, or NULL on error. |
355 | | // If |out| is non-NULL, but |*out| is NULL, it does the same but additionally |
356 | | // sets |*out| to the result. If both |out| and |*out| are non-NULL, it writes |
357 | | // the result to |*out| and returns |*out| on success or NULL on error. |
358 | | // |
359 | | // This function may not be used with |ASN1_ITEM|s whose C type is |
360 | | // |ASN1_BOOLEAN|. |
361 | | // |
362 | | // WARNING: Passing a pointer of the wrong type into this function is a |
363 | | // potentially exploitable memory error. Callers must ensure |val| is consistent |
364 | | // with |it|. |
365 | | OPENSSL_EXPORT ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, |
366 | | ASN1_STRING **out); |
367 | | |
368 | | |
369 | | // Booleans. |
370 | | // |
371 | | // This library represents ASN.1 BOOLEAN values with |ASN1_BOOLEAN|, which is an |
372 | | // integer type. FALSE is zero, TRUE is 0xff, and an omitted OPTIONAL BOOLEAN is |
373 | | // -1. |
374 | | |
375 | | // ASN1_BOOLEAN_FALSE is FALSE as an |ASN1_BOOLEAN|. |
376 | 382 | #define ASN1_BOOLEAN_FALSE 0 |
377 | | |
378 | | // ASN1_BOOLEAN_TRUE is TRUE as an |ASN1_BOOLEAN|. Some code incorrectly uses |
379 | | // 1, so prefer |b != ASN1_BOOLEAN_FALSE| over |b == ASN1_BOOLEAN_TRUE|. |
380 | 233 | #define ASN1_BOOLEAN_TRUE 0xff |
381 | | |
382 | | // ASN1_BOOLEAN_NONE, in contexts where the |ASN1_BOOLEAN| represents an |
383 | | // OPTIONAL BOOLEAN, is an omitted value. Using this value in other contexts is |
384 | | // undefined and may be misinterpreted as TRUE. |
385 | 170k | #define ASN1_BOOLEAN_NONE (-1) |
386 | | |
387 | | // d2i_ASN1_BOOLEAN parses a DER-encoded ASN.1 BOOLEAN from up to |len| bytes at |
388 | | // |*inp|. On success, it advances |*inp| by the number of bytes read and |
389 | | // returns the result. If |out| is non-NULL, it additionally writes the result |
390 | | // to |*out|. On error, it returns |ASN1_BOOLEAN_NONE|. |
391 | | // |
392 | | // This function does not reject trailing data in the input. This allows the |
393 | | // caller to parse a sequence of concatenated structures. Callers parsing only |
394 | | // one structure should check for trailing data by comparing the updated |*inp| |
395 | | // with the end of the input. |
396 | | // |
397 | | // WARNING: This function's is slightly different from other |d2i_*| functions |
398 | | // because |ASN1_BOOLEAN| is not a pointer type. |
399 | | OPENSSL_EXPORT ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, |
400 | | const unsigned char **inp, |
401 | | long len); |
402 | | |
403 | | // i2d_ASN1_BOOLEAN marshals |a| as a DER-encoded ASN.1 BOOLEAN, as described in |
404 | | // |i2d_SAMPLE|. |
405 | | OPENSSL_EXPORT int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp); |
406 | | |
407 | | // The following |ASN1_ITEM|s have ASN.1 type BOOLEAN and C type |ASN1_BOOLEAN|. |
408 | | // |ASN1_TBOOLEAN| and |ASN1_FBOOLEAN| must be marked OPTIONAL. When omitted, |
409 | | // they are parsed as TRUE and FALSE, respectively, rather than |
410 | | // |ASN1_BOOLEAN_NONE|. |
411 | | DECLARE_ASN1_ITEM(ASN1_BOOLEAN) |
412 | | DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) |
413 | | DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) |
414 | | |
415 | | |
416 | | // Strings. |
417 | | // |
418 | | // ASN.1 contains a myriad of string types, as well as types that contain data |
419 | | // that may be encoded into a string. This library uses a single type, |
420 | | // |ASN1_STRING|, to represent most values. |
421 | | |
422 | | // An asn1_string_st (aka |ASN1_STRING|) represents a value of a string-like |
423 | | // ASN.1 type. It contains a |type| field, and a byte string |data| field with a |
424 | | // type-specific representation. This type-specific representation does not |
425 | | // always correspond to the DER encoding of the type. |
426 | | // |
427 | | // If |type| is one of |V_ASN1_OCTET_STRING|, |V_ASN1_UTF8STRING|, |
428 | | // |V_ASN1_NUMERICSTRING|, |V_ASN1_PRINTABLESTRING|, |V_ASN1_T61STRING|, |
429 | | // |V_ASN1_VIDEOTEXSTRING|, |V_ASN1_IA5STRING|, |V_ASN1_GRAPHICSTRING|, |
430 | | // |V_ASN1_ISO64STRING|, |V_ASN1_VISIBLESTRING|, |V_ASN1_GENERALSTRING|, |
431 | | // |V_ASN1_UNIVERSALSTRING|, or |V_ASN1_BMPSTRING|, the object represents an |
432 | | // ASN.1 string type. The data contains the byte representation of the |
433 | | // string. |
434 | | // |
435 | | // If |type| is |V_ASN1_BIT_STRING|, the object represents a BIT STRING value. |
436 | | // See bit string documentation below for the data and flags. |
437 | | // |
438 | | // If |type| is one of |V_ASN1_INTEGER|, |V_ASN1_NEG_INTEGER|, |
439 | | // |V_ASN1_ENUMERATED|, or |V_ASN1_NEG_ENUMERATED|, the object represents an |
440 | | // INTEGER or ENUMERATED value. See integer documentation below for details. |
441 | | // |
442 | | // If |type| is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, the object |
443 | | // represents a GeneralizedTime or UTCTime value, respectively. The data |
444 | | // contains the DER encoding of the value. For example, the UNIX epoch would be |
445 | | // "19700101000000Z" for a GeneralizedTime and "700101000000Z" for a UTCTime. |
446 | | // |
447 | | // If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the object |
448 | | // represents a SEQUENCE, SET, or arbitrary ASN.1 value, respectively. Unlike |
449 | | // the above cases, the data contains the DER encoding of the entire structure, |
450 | | // including the header. If the value is explicitly or implicitly tagged, this |
451 | | // too will be reflected in the data field. As this case handles unknown types, |
452 | | // the contents are not checked when parsing or serializing. |
453 | | // |
454 | | // Other values of |type| do not represent a valid ASN.1 value, though |
455 | | // default-constructed objects may set |type| to -1. Such objects cannot be |
456 | | // serialized. |
457 | | // |
458 | | // |ASN1_STRING| additionally has the following typedefs: |ASN1_BIT_STRING|, |
459 | | // |ASN1_BMPSTRING|, |ASN1_ENUMERATED|, |ASN1_GENERALIZEDTIME|, |
460 | | // |ASN1_GENERALSTRING|, |ASN1_IA5STRING|, |ASN1_INTEGER|, |ASN1_OCTET_STRING|, |
461 | | // |ASN1_PRINTABLESTRING|, |ASN1_T61STRING|, |ASN1_TIME|, |
462 | | // |ASN1_UNIVERSALSTRING|, |ASN1_UTCTIME|, |ASN1_UTF8STRING|, and |
463 | | // |ASN1_VISIBLESTRING|. Other than |ASN1_TIME|, these correspond to universal |
464 | | // ASN.1 types. |ASN1_TIME| represents a CHOICE of UTCTime and GeneralizedTime, |
465 | | // with a cutoff of 2049, as used in Section 4.1.2.5 of RFC 5280. |
466 | | // |
467 | | // For clarity, callers are encouraged to use the appropriate typedef when |
468 | | // available. They are the same type as |ASN1_STRING|, so a caller may freely |
469 | | // pass them into functions expecting |ASN1_STRING|, such as |
470 | | // |ASN1_STRING_length|. |
471 | | // |
472 | | // If a function returns an |ASN1_STRING| where the typedef or ASN.1 structure |
473 | | // implies constraints on |type|, callers may assume that |type| is correct. |
474 | | // However, if a function takes an |ASN1_STRING| as input, callers must ensure |
475 | | // |type| matches. These invariants are not captured by the C type system and |
476 | | // may not be checked at runtime. For example, callers may assume the output of |
477 | | // |X509_get0_serialNumber| has type |V_ASN1_INTEGER| or |V_ASN1_NEG_INTEGER|. |
478 | | // Callers must not pass a string of type |V_ASN1_OCTET_STRING| to |
479 | | // |X509_set_serialNumber|. Doing so may break invariants on the |X509| object |
480 | | // and break the |X509_get0_serialNumber| invariant. |
481 | | // |
482 | | // TODO(https://crbug.com/boringssl/445): This is very unfriendly. Getting the |
483 | | // type field wrong should not cause memory errors, but it may do strange |
484 | | // things. We should add runtime checks to anything that consumes |ASN1_STRING|s |
485 | | // from the caller. |
486 | | struct asn1_string_st { |
487 | | int length; |
488 | | int type; |
489 | | unsigned char *data; |
490 | | long flags; |
491 | | }; |
492 | | |
493 | | // ASN1_STRING_FLAG_BITS_LEFT indicates, in a BIT STRING |ASN1_STRING|, that |
494 | | // flags & 0x7 contains the number of padding bits added to the BIT STRING |
495 | | // value. When not set, all trailing zero bits in the last byte are implicitly |
496 | | // treated as padding. This behavior is deprecated and should not be used. |
497 | 82.5k | #define ASN1_STRING_FLAG_BITS_LEFT 0x08 |
498 | | |
499 | | // ASN1_STRING_type_new returns a newly-allocated empty |ASN1_STRING| object of |
500 | | // type |type|, or NULL on error. |
501 | | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_type_new(int type); |
502 | | |
503 | | // ASN1_STRING_new returns a newly-allocated empty |ASN1_STRING| object with an |
504 | | // arbitrary type. Prefer one of the type-specific constructors, such as |
505 | | // |ASN1_OCTET_STRING_new|, or |ASN1_STRING_type_new|. |
506 | | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_new(void); |
507 | | |
508 | | // ASN1_STRING_free releases memory associated with |str|. |
509 | | OPENSSL_EXPORT void ASN1_STRING_free(ASN1_STRING *str); |
510 | | |
511 | | // ASN1_STRING_copy sets |dst| to a copy of |str|. It returns one on success and |
512 | | // zero on error. |
513 | | OPENSSL_EXPORT int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str); |
514 | | |
515 | | // ASN1_STRING_dup returns a newly-allocated copy of |str|, or NULL on error. |
516 | | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str); |
517 | | |
518 | | // ASN1_STRING_type returns the type of |str|. This value will be one of the |
519 | | // |V_ASN1_*| constants. |
520 | | OPENSSL_EXPORT int ASN1_STRING_type(const ASN1_STRING *str); |
521 | | |
522 | | // ASN1_STRING_get0_data returns a pointer to |str|'s contents. Callers should |
523 | | // use |ASN1_STRING_length| to determine the length of the string. The string |
524 | | // may have embedded NUL bytes and may not be NUL-terminated. |
525 | | // |
526 | | // The contents of an |ASN1_STRING| encode the value in some type-specific |
527 | | // representation that does not always correspond to the DER encoding of the |
528 | | // type. See the documentation for |ASN1_STRING| for details. |
529 | | OPENSSL_EXPORT const unsigned char *ASN1_STRING_get0_data( |
530 | | const ASN1_STRING *str); |
531 | | |
532 | | // ASN1_STRING_data returns a mutable pointer to |str|'s contents. Callers |
533 | | // should use |ASN1_STRING_length| to determine the length of the string. The |
534 | | // string may have embedded NUL bytes and may not be NUL-terminated. |
535 | | // |
536 | | // The contents of an |ASN1_STRING| encode the value in some type-specific |
537 | | // representation that does not always correspond to the DER encoding of the |
538 | | // type. See the documentation for |ASN1_STRING| for details. |
539 | | // |
540 | | // Prefer |ASN1_STRING_get0_data|. |
541 | | OPENSSL_EXPORT unsigned char *ASN1_STRING_data(ASN1_STRING *str); |
542 | | |
543 | | // ASN1_STRING_length returns the length of |str|, in bytes. |
544 | | // |
545 | | // The contents of an |ASN1_STRING| encode the value in some type-specific |
546 | | // representation that does not always correspond to the DER encoding of the |
547 | | // type. See the documentation for |ASN1_STRING| for details. |
548 | | OPENSSL_EXPORT int ASN1_STRING_length(const ASN1_STRING *str); |
549 | | |
550 | | // ASN1_STRING_cmp compares |a| and |b|'s type and contents. It returns an |
551 | | // integer equal to, less than, or greater than zero if |a| is equal to, less |
552 | | // than, or greater than |b|, respectively. This function compares by length, |
553 | | // then data, then type. Note the data compared is the |ASN1_STRING| internal |
554 | | // representation and the type order is arbitrary. While this comparison is |
555 | | // suitable for sorting, callers should not rely on the exact order when |a| |
556 | | // and |b| are different types. |
557 | | // |
558 | | // Note that, if |a| and |b| are INTEGERs, this comparison does not order the |
559 | | // values numerically. For a numerical comparison, use |ASN1_INTEGER_cmp|. |
560 | | OPENSSL_EXPORT int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b); |
561 | | |
562 | | // ASN1_STRING_set sets the contents of |str| to a copy of |len| bytes from |
563 | | // |data|. It returns one on success and zero on error. If |data| is NULL, it |
564 | | // updates the length and allocates the buffer as needed, but does not |
565 | | // initialize the contents. |
566 | | OPENSSL_EXPORT int ASN1_STRING_set(ASN1_STRING *str, const void *data, |
567 | | ossl_ssize_t len); |
568 | | |
569 | | // ASN1_STRING_set0 sets the contents of |str| to |len| bytes from |data|. It |
570 | | // takes ownership of |data|, which must have been allocated with |
571 | | // |OPENSSL_malloc|. |
572 | | OPENSSL_EXPORT void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len); |
573 | | |
574 | | // The following functions call |ASN1_STRING_type_new| with the corresponding |
575 | | // |V_ASN1_*| constant. |
576 | | OPENSSL_EXPORT ASN1_BMPSTRING *ASN1_BMPSTRING_new(void); |
577 | | OPENSSL_EXPORT ASN1_GENERALSTRING *ASN1_GENERALSTRING_new(void); |
578 | | OPENSSL_EXPORT ASN1_IA5STRING *ASN1_IA5STRING_new(void); |
579 | | OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_new(void); |
580 | | OPENSSL_EXPORT ASN1_PRINTABLESTRING *ASN1_PRINTABLESTRING_new(void); |
581 | | OPENSSL_EXPORT ASN1_T61STRING *ASN1_T61STRING_new(void); |
582 | | OPENSSL_EXPORT ASN1_UNIVERSALSTRING *ASN1_UNIVERSALSTRING_new(void); |
583 | | OPENSSL_EXPORT ASN1_UTF8STRING *ASN1_UTF8STRING_new(void); |
584 | | OPENSSL_EXPORT ASN1_VISIBLESTRING *ASN1_VISIBLESTRING_new(void); |
585 | | |
586 | | // The following functions call |ASN1_STRING_free|. |
587 | | OPENSSL_EXPORT void ASN1_BMPSTRING_free(ASN1_BMPSTRING *str); |
588 | | OPENSSL_EXPORT void ASN1_GENERALSTRING_free(ASN1_GENERALSTRING *str); |
589 | | OPENSSL_EXPORT void ASN1_IA5STRING_free(ASN1_IA5STRING *str); |
590 | | OPENSSL_EXPORT void ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *str); |
591 | | OPENSSL_EXPORT void ASN1_PRINTABLESTRING_free(ASN1_PRINTABLESTRING *str); |
592 | | OPENSSL_EXPORT void ASN1_T61STRING_free(ASN1_T61STRING *str); |
593 | | OPENSSL_EXPORT void ASN1_UNIVERSALSTRING_free(ASN1_UNIVERSALSTRING *str); |
594 | | OPENSSL_EXPORT void ASN1_UTF8STRING_free(ASN1_UTF8STRING *str); |
595 | | OPENSSL_EXPORT void ASN1_VISIBLESTRING_free(ASN1_VISIBLESTRING *str); |
596 | | |
597 | | // The following functions parse up to |len| bytes from |*inp| as a |
598 | | // DER-encoded ASN.1 value of the corresponding type, as described in |
599 | | // |d2i_SAMPLE|. |
600 | | OPENSSL_EXPORT ASN1_BMPSTRING *d2i_ASN1_BMPSTRING(ASN1_BMPSTRING **out, |
601 | | const uint8_t **inp, |
602 | | long len); |
603 | | OPENSSL_EXPORT ASN1_GENERALSTRING *d2i_ASN1_GENERALSTRING( |
604 | | ASN1_GENERALSTRING **out, const uint8_t **inp, long len); |
605 | | OPENSSL_EXPORT ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **out, |
606 | | const uint8_t **inp, |
607 | | long len); |
608 | | OPENSSL_EXPORT ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **out, |
609 | | const uint8_t **inp, |
610 | | long len); |
611 | | OPENSSL_EXPORT ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING( |
612 | | ASN1_PRINTABLESTRING **out, const uint8_t **inp, long len); |
613 | | OPENSSL_EXPORT ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **out, |
614 | | const uint8_t **inp, |
615 | | long len); |
616 | | OPENSSL_EXPORT ASN1_UNIVERSALSTRING *d2i_ASN1_UNIVERSALSTRING( |
617 | | ASN1_UNIVERSALSTRING **out, const uint8_t **inp, long len); |
618 | | OPENSSL_EXPORT ASN1_UTF8STRING *d2i_ASN1_UTF8STRING(ASN1_UTF8STRING **out, |
619 | | const uint8_t **inp, |
620 | | long len); |
621 | | OPENSSL_EXPORT ASN1_VISIBLESTRING *d2i_ASN1_VISIBLESTRING( |
622 | | ASN1_VISIBLESTRING **out, const uint8_t **inp, long len); |
623 | | |
624 | | // The following functions marshal |in| as a DER-encoded ASN.1 value of the |
625 | | // corresponding type, as described in |i2d_SAMPLE|. |
626 | | OPENSSL_EXPORT int i2d_ASN1_BMPSTRING(const ASN1_BMPSTRING *in, uint8_t **outp); |
627 | | OPENSSL_EXPORT int i2d_ASN1_GENERALSTRING(const ASN1_GENERALSTRING *in, |
628 | | uint8_t **outp); |
629 | | OPENSSL_EXPORT int i2d_ASN1_IA5STRING(const ASN1_IA5STRING *in, uint8_t **outp); |
630 | | OPENSSL_EXPORT int i2d_ASN1_OCTET_STRING(const ASN1_OCTET_STRING *in, |
631 | | uint8_t **outp); |
632 | | OPENSSL_EXPORT int i2d_ASN1_PRINTABLESTRING(const ASN1_PRINTABLESTRING *in, |
633 | | uint8_t **outp); |
634 | | OPENSSL_EXPORT int i2d_ASN1_T61STRING(const ASN1_T61STRING *in, uint8_t **outp); |
635 | | OPENSSL_EXPORT int i2d_ASN1_UNIVERSALSTRING(const ASN1_UNIVERSALSTRING *in, |
636 | | uint8_t **outp); |
637 | | OPENSSL_EXPORT int i2d_ASN1_UTF8STRING(const ASN1_UTF8STRING *in, |
638 | | uint8_t **outp); |
639 | | OPENSSL_EXPORT int i2d_ASN1_VISIBLESTRING(const ASN1_VISIBLESTRING *in, |
640 | | uint8_t **outp); |
641 | | |
642 | | // The following |ASN1_ITEM|s have the ASN.1 type referred to in their name and |
643 | | // C type |ASN1_STRING*|. The C type may also be written as the corresponding |
644 | | // typedef. |
645 | | DECLARE_ASN1_ITEM(ASN1_BMPSTRING) |
646 | | DECLARE_ASN1_ITEM(ASN1_GENERALSTRING) |
647 | | DECLARE_ASN1_ITEM(ASN1_IA5STRING) |
648 | | DECLARE_ASN1_ITEM(ASN1_OCTET_STRING) |
649 | | DECLARE_ASN1_ITEM(ASN1_PRINTABLESTRING) |
650 | | DECLARE_ASN1_ITEM(ASN1_T61STRING) |
651 | | DECLARE_ASN1_ITEM(ASN1_UNIVERSALSTRING) |
652 | | DECLARE_ASN1_ITEM(ASN1_UTF8STRING) |
653 | | DECLARE_ASN1_ITEM(ASN1_VISIBLESTRING) |
654 | | |
655 | | // ASN1_OCTET_STRING_dup calls |ASN1_STRING_dup|. |
656 | | OPENSSL_EXPORT ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup( |
657 | | const ASN1_OCTET_STRING *a); |
658 | | |
659 | | // ASN1_OCTET_STRING_cmp calls |ASN1_STRING_cmp|. |
660 | | OPENSSL_EXPORT int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, |
661 | | const ASN1_OCTET_STRING *b); |
662 | | |
663 | | // ASN1_OCTET_STRING_set calls |ASN1_STRING_set|. |
664 | | OPENSSL_EXPORT int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, |
665 | | const unsigned char *data, int len); |
666 | | |
667 | | // ASN1_STRING_to_UTF8 converts |in| to UTF-8. On success, sets |*out| to a |
668 | | // newly-allocated buffer containing the resulting string and returns the length |
669 | | // of the string. The caller must call |OPENSSL_free| to release |*out| when |
670 | | // done. On error, it returns a negative number. |
671 | | OPENSSL_EXPORT int ASN1_STRING_to_UTF8(unsigned char **out, |
672 | | const ASN1_STRING *in); |
673 | | |
674 | | // The following formats define encodings for use with functions like |
675 | | // |ASN1_mbstring_copy|. Note |MBSTRING_ASC| refers to Latin-1, not ASCII. |
676 | 116k | #define MBSTRING_FLAG 0x1000 |
677 | 24.3k | #define MBSTRING_UTF8 (MBSTRING_FLAG) |
678 | 51.4k | #define MBSTRING_ASC (MBSTRING_FLAG | 1) |
679 | 40.2k | #define MBSTRING_BMP (MBSTRING_FLAG | 2) |
680 | 18.4k | #define MBSTRING_UNIV (MBSTRING_FLAG | 4) |
681 | | |
682 | | // DIRSTRING_TYPE contains the valid string types in an X.509 DirectoryString. |
683 | | #define DIRSTRING_TYPE \ |
684 | 0 | (B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_BMPSTRING | \ |
685 | 0 | B_ASN1_UTF8STRING) |
686 | | |
687 | | // PKCS9STRING_TYPE contains the valid string types in a PKCS9String. |
688 | | #define PKCS9STRING_TYPE (DIRSTRING_TYPE | B_ASN1_IA5STRING) |
689 | | |
690 | | // ASN1_mbstring_copy converts |len| bytes from |in| to an ASN.1 string. If |
691 | | // |len| is -1, |in| must be NUL-terminated and the length is determined by |
692 | | // |strlen|. |in| is decoded according to |inform|, which must be one of |
693 | | // |MBSTRING_*|. |mask| determines the set of valid output types and is a |
694 | | // bitmask containing a subset of |B_ASN1_PRINTABLESTRING|, |B_ASN1_IA5STRING|, |
695 | | // |B_ASN1_T61STRING|, |B_ASN1_BMPSTRING|, |B_ASN1_UNIVERSALSTRING|, and |
696 | | // |B_ASN1_UTF8STRING|, in that preference order. This function chooses the |
697 | | // first output type in |mask| which can represent |in|. It interprets T61String |
698 | | // as Latin-1, rather than T.61. |
699 | | // |
700 | | // If |mask| is zero, |DIRSTRING_TYPE| is used by default. |
701 | | // |
702 | | // On success, this function returns the |V_ASN1_*| constant corresponding to |
703 | | // the selected output type and, if |out| and |*out| are both non-NULL, updates |
704 | | // the object at |*out| with the result. If |out| is non-NULL and |*out| is |
705 | | // NULL, it instead sets |*out| to a newly-allocated |ASN1_STRING| containing |
706 | | // the result. If |out| is NULL, it returns the selected output type without |
707 | | // constructing an |ASN1_STRING|. On error, this function returns -1. |
708 | | OPENSSL_EXPORT int ASN1_mbstring_copy(ASN1_STRING **out, const uint8_t *in, |
709 | | ossl_ssize_t len, int inform, |
710 | | unsigned long mask); |
711 | | |
712 | | // ASN1_mbstring_ncopy behaves like |ASN1_mbstring_copy| but returns an error if |
713 | | // the input is less than |minsize| or greater than |maxsize| codepoints long. A |
714 | | // |maxsize| value of zero is ignored. Note the sizes are measured in |
715 | | // codepoints, not output bytes. |
716 | | OPENSSL_EXPORT int ASN1_mbstring_ncopy(ASN1_STRING **out, const uint8_t *in, |
717 | | ossl_ssize_t len, int inform, |
718 | | unsigned long mask, ossl_ssize_t minsize, |
719 | | ossl_ssize_t maxsize); |
720 | | |
721 | | // ASN1_STRING_set_by_NID behaves like |ASN1_mbstring_ncopy|, but determines |
722 | | // |mask|, |minsize|, and |maxsize| based on |nid|. When |nid| is a recognized |
723 | | // X.509 attribute type, it will pick a suitable ASN.1 string type and bounds. |
724 | | // For most attribute types, it preferentially chooses UTF8String. If |nid| is |
725 | | // unrecognized, it uses UTF8String by default. |
726 | | // |
727 | | // Slightly unlike |ASN1_mbstring_ncopy|, this function interprets |out| and |
728 | | // returns its result as follows: If |out| is NULL, it returns a newly-allocated |
729 | | // |ASN1_STRING| containing the result. If |out| is non-NULL and |
730 | | // |*out| is NULL, it additionally sets |*out| to the result. If both |out| and |
731 | | // |*out| are non-NULL, it instead updates the object at |*out| and returns |
732 | | // |*out|. In all cases, it returns NULL on error. |
733 | | // |
734 | | // This function supports the following NIDs: |NID_countryName|, |
735 | | // |NID_dnQualifier|, |NID_domainComponent|, |NID_friendlyName|, |
736 | | // |NID_givenName|, |NID_initials|, |NID_localityName|, |NID_ms_csp_name|, |
737 | | // |NID_name|, |NID_organizationalUnitName|, |NID_organizationName|, |
738 | | // |NID_pkcs9_challengePassword|, |NID_pkcs9_emailAddress|, |
739 | | // |NID_pkcs9_unstructuredAddress|, |NID_pkcs9_unstructuredName|, |
740 | | // |NID_serialNumber|, |NID_stateOrProvinceName|, and |NID_surname|. Additional |
741 | | // NIDs may be registered with |ASN1_STRING_set_by_NID|, but it is recommended |
742 | | // to call |ASN1_mbstring_ncopy| directly instead. |
743 | | OPENSSL_EXPORT ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, |
744 | | const unsigned char *in, |
745 | | ossl_ssize_t len, int inform, |
746 | | int nid); |
747 | | |
748 | | // STABLE_NO_MASK causes |ASN1_STRING_TABLE_add| to allow types other than |
749 | | // UTF8String. |
750 | 0 | #define STABLE_NO_MASK 0x02 |
751 | | |
752 | | // ASN1_STRING_TABLE_add registers the corresponding parameters with |nid|, for |
753 | | // use with |ASN1_STRING_set_by_NID|. It returns one on success and zero on |
754 | | // error. It is an error to call this function if |nid| is a built-in NID, or |
755 | | // was already registered by a previous call. |
756 | | // |
757 | | // WARNING: This function affects global state in the library. If two libraries |
758 | | // in the same address space register information for the same OID, one call |
759 | | // will fail. Prefer directly passing the desired parametrs to |
760 | | // |ASN1_mbstring_copy| or |ASN1_mbstring_ncopy| instead. |
761 | | OPENSSL_EXPORT int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, |
762 | | unsigned long mask, |
763 | | unsigned long flags); |
764 | | |
765 | | |
766 | | // Multi-strings. |
767 | | // |
768 | | // A multi-string, or "MSTRING", is an |ASN1_STRING| that represents a CHOICE of |
769 | | // several string or string-like types, such as X.509's DirectoryString. The |
770 | | // |ASN1_STRING|'s type field determines which type is used. |
771 | | // |
772 | | // Multi-string types are associated with a bitmask, using the |B_ASN1_*| |
773 | | // constants, which defines which types are valid. |
774 | | |
775 | | // B_ASN1_DIRECTORYSTRING is a bitmask of types allowed in an X.509 |
776 | | // DirectoryString (RFC 5280). |
777 | | #define B_ASN1_DIRECTORYSTRING \ |
778 | | (B_ASN1_PRINTABLESTRING | B_ASN1_TELETEXSTRING | B_ASN1_BMPSTRING | \ |
779 | | B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING) |
780 | | |
781 | | // DIRECTORYSTRING_new returns a newly-allocated |ASN1_STRING| with type -1, or |
782 | | // NULL on error. The resulting |ASN1_STRING| is not a valid X.509 |
783 | | // DirectoryString until initialized with a value. |
784 | | OPENSSL_EXPORT ASN1_STRING *DIRECTORYSTRING_new(void); |
785 | | |
786 | | // DIRECTORYSTRING_free calls |ASN1_STRING_free|. |
787 | | OPENSSL_EXPORT void DIRECTORYSTRING_free(ASN1_STRING *str); |
788 | | |
789 | | // d2i_DIRECTORYSTRING parses up to |len| bytes from |*inp| as a DER-encoded |
790 | | // X.509 DirectoryString (RFC 5280), as described in |d2i_SAMPLE|. |
791 | | // |
792 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
793 | | // BER, but this will be removed in the future. |
794 | | // |
795 | | // TODO(https://crbug.com/boringssl/449): DirectoryString's non-empty string |
796 | | // requirement is not currently enforced. |
797 | | OPENSSL_EXPORT ASN1_STRING *d2i_DIRECTORYSTRING(ASN1_STRING **out, |
798 | | const uint8_t **inp, long len); |
799 | | |
800 | | // i2d_DIRECTORYSTRING marshals |in| as a DER-encoded X.509 DirectoryString (RFC |
801 | | // 5280), as described in |i2d_SAMPLE|. |
802 | | OPENSSL_EXPORT int i2d_DIRECTORYSTRING(const ASN1_STRING *in, uint8_t **outp); |
803 | | |
804 | | // DIRECTORYSTRING is an |ASN1_ITEM| whose ASN.1 type is X.509 DirectoryString |
805 | | // (RFC 5280) and C type is |ASN1_STRING*|. |
806 | | DECLARE_ASN1_ITEM(DIRECTORYSTRING) |
807 | | |
808 | | // B_ASN1_DISPLAYTEXT is a bitmask of types allowed in an X.509 DisplayText (RFC |
809 | | // 5280). |
810 | | #define B_ASN1_DISPLAYTEXT \ |
811 | | (B_ASN1_IA5STRING | B_ASN1_VISIBLESTRING | B_ASN1_BMPSTRING | \ |
812 | | B_ASN1_UTF8STRING) |
813 | | |
814 | | // DISPLAYTEXT_new returns a newly-allocated |ASN1_STRING| with type -1, or NULL |
815 | | // on error. The resulting |ASN1_STRING| is not a valid X.509 DisplayText until |
816 | | // initialized with a value. |
817 | | OPENSSL_EXPORT ASN1_STRING *DISPLAYTEXT_new(void); |
818 | | |
819 | | // DISPLAYTEXT_free calls |ASN1_STRING_free|. |
820 | | OPENSSL_EXPORT void DISPLAYTEXT_free(ASN1_STRING *str); |
821 | | |
822 | | // d2i_DISPLAYTEXT parses up to |len| bytes from |*inp| as a DER-encoded X.509 |
823 | | // DisplayText (RFC 5280), as described in |d2i_SAMPLE|. |
824 | | // |
825 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
826 | | // BER, but this will be removed in the future. |
827 | | // |
828 | | // TODO(https://crbug.com/boringssl/449): DisplayText's size limits are not |
829 | | // currently enforced. |
830 | | OPENSSL_EXPORT ASN1_STRING *d2i_DISPLAYTEXT(ASN1_STRING **out, |
831 | | const uint8_t **inp, long len); |
832 | | |
833 | | // i2d_DISPLAYTEXT marshals |in| as a DER-encoded X.509 DisplayText (RFC 5280), |
834 | | // as described in |i2d_SAMPLE|. |
835 | | OPENSSL_EXPORT int i2d_DISPLAYTEXT(const ASN1_STRING *in, uint8_t **outp); |
836 | | |
837 | | // DISPLAYTEXT is an |ASN1_ITEM| whose ASN.1 type is X.509 DisplayText (RFC |
838 | | // 5280) and C type is |ASN1_STRING*|. |
839 | | DECLARE_ASN1_ITEM(DISPLAYTEXT) |
840 | | |
841 | | |
842 | | // Bit strings. |
843 | | // |
844 | | // An ASN.1 BIT STRING type represents a string of bits. The string may not |
845 | | // necessarily be a whole number of bytes. BIT STRINGs occur in ASN.1 structures |
846 | | // in several forms: |
847 | | // |
848 | | // Some BIT STRINGs represent a bitmask of named bits, such as the X.509 key |
849 | | // usage extension in RFC 5280, section 4.2.1.3. For such bit strings, DER |
850 | | // imposes an additional restriction that trailing zero bits are removed. Some |
851 | | // functions like |ASN1_BIT_STRING_set_bit| help in maintaining this. |
852 | | // |
853 | | // Other BIT STRINGs are arbitrary strings of bits used as identifiers and do |
854 | | // not have this constraint, such as the X.509 issuerUniqueID field. |
855 | | // |
856 | | // Finally, some structures use BIT STRINGs as a container for byte strings. For |
857 | | // example, the signatureValue field in X.509 and the subjectPublicKey field in |
858 | | // SubjectPublicKeyInfo are defined as BIT STRINGs with a value specific to the |
859 | | // AlgorithmIdentifier. While some unknown algorithm could choose to store |
860 | | // arbitrary bit strings, all supported algorithms use a byte string, with bit |
861 | | // order matching the DER encoding. Callers interpreting a BIT STRING as a byte |
862 | | // string should use |ASN1_BIT_STRING_num_bytes| instead of |ASN1_STRING_length| |
863 | | // and reject bit strings that are not a whole number of bytes. |
864 | | // |
865 | | // This library represents BIT STRINGs as |ASN1_STRING|s with type |
866 | | // |V_ASN1_BIT_STRING|. The data contains the encoded form of the BIT STRING, |
867 | | // including any padding bits added to round to a whole number of bytes, but |
868 | | // excluding the leading byte containing the number of padding bits. If |
869 | | // |ASN1_STRING_FLAG_BITS_LEFT| is set, the bottom three bits contains the |
870 | | // number of padding bits. For example, DER encodes the BIT STRING {1, 0} as |
871 | | // {0x06, 0x80 = 0b10_000000}. The |ASN1_STRING| representation has data of |
872 | | // {0x80} and flags of ASN1_STRING_FLAG_BITS_LEFT | 6. If |
873 | | // |ASN1_STRING_FLAG_BITS_LEFT| is unset, trailing zero bits are implicitly |
874 | | // removed. Callers should not rely this representation when constructing bit |
875 | | // strings. The padding bits in the |ASN1_STRING| data must be zero. |
876 | | |
877 | | // ASN1_BIT_STRING_new calls |ASN1_STRING_type_new| with |V_ASN1_BIT_STRING|. |
878 | | OPENSSL_EXPORT ASN1_BIT_STRING *ASN1_BIT_STRING_new(void); |
879 | | |
880 | | // ASN1_BIT_STRING_free calls |ASN1_STRING_free|. |
881 | | OPENSSL_EXPORT void ASN1_BIT_STRING_free(ASN1_BIT_STRING *str); |
882 | | |
883 | | // d2i_ASN1_BIT_STRING parses up to |len| bytes from |*inp| as a DER-encoded |
884 | | // ASN.1 BIT STRING, as described in |d2i_SAMPLE|. |
885 | | OPENSSL_EXPORT ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, |
886 | | const uint8_t **inp, |
887 | | long len); |
888 | | |
889 | | // i2d_ASN1_BIT_STRING marshals |in| as a DER-encoded ASN.1 BIT STRING, as |
890 | | // described in |i2d_SAMPLE|. |
891 | | OPENSSL_EXPORT int i2d_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, |
892 | | uint8_t **outp); |
893 | | |
894 | | // c2i_ASN1_BIT_STRING decodes |len| bytes from |*inp| as the contents of a |
895 | | // DER-encoded BIT STRING, excluding the tag and length. It behaves like |
896 | | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
897 | | OPENSSL_EXPORT ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out, |
898 | | const uint8_t **inp, |
899 | | long len); |
900 | | |
901 | | // i2c_ASN1_BIT_STRING encodes |in| as the contents of a DER-encoded BIT STRING, |
902 | | // excluding the tag and length. If |outp| is non-NULL, it writes the result to |
903 | | // |*outp|, advances |*outp| just past the output, and returns the number of |
904 | | // bytes written. |*outp| must have space available for the result. If |outp| is |
905 | | // NULL, it returns the number of bytes without writing anything. On error, it |
906 | | // returns a value <= 0. |
907 | | // |
908 | | // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL |
909 | | // and |*outp| is NULL, it does not allocate a new buffer. |
910 | | // |
911 | | // TODO(davidben): This function currently returns zero on error instead of -1, |
912 | | // but it is also mostly infallible. I've currently documented <= 0 to suggest |
913 | | // callers work with both. |
914 | | OPENSSL_EXPORT int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *in, |
915 | | uint8_t **outp); |
916 | | |
917 | | // ASN1_BIT_STRING is an |ASN1_ITEM| with ASN.1 type BIT STRING and C type |
918 | | // |ASN1_BIT_STRING*|. |
919 | | DECLARE_ASN1_ITEM(ASN1_BIT_STRING) |
920 | | |
921 | | // ASN1_BIT_STRING_num_bytes computes the length of |str| in bytes. If |str|'s |
922 | | // bit length is a multiple of 8, it sets |*out| to the byte length and returns |
923 | | // one. Otherwise, it returns zero. |
924 | | // |
925 | | // This function may be used with |ASN1_STRING_get0_data| to interpret |str| as |
926 | | // a byte string. |
927 | | OPENSSL_EXPORT int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, |
928 | | size_t *out); |
929 | | |
930 | | // ASN1_BIT_STRING_set calls |ASN1_STRING_set|. It leaves flags unchanged, so |
931 | | // the caller must set the number of unused bits. |
932 | | // |
933 | | // TODO(davidben): Maybe it should? Wrapping a byte string in a bit string is a |
934 | | // common use case. |
935 | | OPENSSL_EXPORT int ASN1_BIT_STRING_set(ASN1_BIT_STRING *str, |
936 | | const unsigned char *d, |
937 | | ossl_ssize_t length); |
938 | | |
939 | | // ASN1_BIT_STRING_set_bit sets bit |n| of |str| to one if |value| is non-zero |
940 | | // and zero if |value| is zero, resizing |str| as needed. It then truncates |
941 | | // trailing zeros in |str| to align with the DER represention for a bit string |
942 | | // with named bits. It returns one on success and zero on error. |n| is indexed |
943 | | // beginning from zero. |
944 | | OPENSSL_EXPORT int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *str, int n, |
945 | | int value); |
946 | | |
947 | | // ASN1_BIT_STRING_get_bit returns one if bit |n| of |a| is in bounds and set, |
948 | | // and zero otherwise. |n| is indexed beginning from zero. |
949 | | OPENSSL_EXPORT int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *str, int n); |
950 | | |
951 | | // ASN1_BIT_STRING_check returns one if |str| only contains bits that are set in |
952 | | // the |flags_len| bytes pointed by |flags|. Otherwise it returns zero. Bits in |
953 | | // |flags| are arranged according to the DER representation, so bit 0 |
954 | | // corresponds to the MSB of |flags[0]|. |
955 | | OPENSSL_EXPORT int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *str, |
956 | | const unsigned char *flags, |
957 | | int flags_len); |
958 | | |
959 | | |
960 | | // Integers and enumerated values. |
961 | | // |
962 | | // INTEGER and ENUMERATED values are represented as |ASN1_STRING|s where the |
963 | | // data contains the big-endian encoding of the absolute value of the integer. |
964 | | // The sign bit is encoded in the type: non-negative values have a type of |
965 | | // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, while negative values have a type of |
966 | | // |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. Note this differs from DER's |
967 | | // two's complement representation. |
968 | | // |
969 | | // The data in the |ASN1_STRING| may not have leading zeros. Note this means |
970 | | // zero is represented as the empty string. Parsing functions will never return |
971 | | // invalid representations. If an invalid input is constructed, the marshaling |
972 | | // functions will skip leading zeros, however other functions, such as |
973 | | // |ASN1_INTEGER_cmp| or |ASN1_INTEGER_get|, may not return the correct result. |
974 | | |
975 | | DEFINE_STACK_OF(ASN1_INTEGER) |
976 | | |
977 | | // ASN1_INTEGER_new calls |ASN1_STRING_type_new| with |V_ASN1_INTEGER|. The |
978 | | // resulting object has value zero. |
979 | | OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_new(void); |
980 | | |
981 | | // ASN1_INTEGER_free calls |ASN1_STRING_free|. |
982 | | OPENSSL_EXPORT void ASN1_INTEGER_free(ASN1_INTEGER *str); |
983 | | |
984 | | // ASN1_INTEGER_dup calls |ASN1_STRING_dup|. |
985 | | OPENSSL_EXPORT ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x); |
986 | | |
987 | | // d2i_ASN1_INTEGER parses up to |len| bytes from |*inp| as a DER-encoded |
988 | | // ASN.1 INTEGER, as described in |d2i_SAMPLE|. |
989 | | OPENSSL_EXPORT ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **out, |
990 | | const uint8_t **inp, long len); |
991 | | |
992 | | // i2d_ASN1_INTEGER marshals |in| as a DER-encoded ASN.1 INTEGER, as |
993 | | // described in |i2d_SAMPLE|. |
994 | | OPENSSL_EXPORT int i2d_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); |
995 | | |
996 | | // c2i_ASN1_INTEGER decodes |len| bytes from |*inp| as the contents of a |
997 | | // DER-encoded INTEGER, excluding the tag and length. It behaves like |
998 | | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
999 | | OPENSSL_EXPORT ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **in, |
1000 | | const uint8_t **outp, long len); |
1001 | | |
1002 | | // i2c_ASN1_INTEGER encodes |in| as the contents of a DER-encoded INTEGER, |
1003 | | // excluding the tag and length. If |outp| is non-NULL, it writes the result to |
1004 | | // |*outp|, advances |*outp| just past the output, and returns the number of |
1005 | | // bytes written. |*outp| must have space available for the result. If |outp| is |
1006 | | // NULL, it returns the number of bytes without writing anything. On error, it |
1007 | | // returns a value <= 0. |
1008 | | // |
1009 | | // Note this function differs slightly from |i2d_SAMPLE|. If |outp| is non-NULL |
1010 | | // and |*outp| is NULL, it does not allocate a new buffer. |
1011 | | // |
1012 | | // TODO(davidben): This function currently returns zero on error instead of -1, |
1013 | | // but it is also mostly infallible. I've currently documented <= 0 to suggest |
1014 | | // callers work with both. |
1015 | | OPENSSL_EXPORT int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, uint8_t **outp); |
1016 | | |
1017 | | // ASN1_INTEGER is an |ASN1_ITEM| with ASN.1 type INTEGER and C type |
1018 | | // |ASN1_INTEGER*|. |
1019 | | DECLARE_ASN1_ITEM(ASN1_INTEGER) |
1020 | | |
1021 | | // ASN1_INTEGER_set_uint64 sets |a| to an INTEGER with value |v|. It returns one |
1022 | | // on success and zero on error. |
1023 | | OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v); |
1024 | | |
1025 | | // ASN1_INTEGER_set_int64 sets |a| to an INTEGER with value |v|. It returns one |
1026 | | // on success and zero on error. |
1027 | | OPENSSL_EXPORT int ASN1_INTEGER_set_int64(ASN1_INTEGER *out, int64_t v); |
1028 | | |
1029 | | // ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns |
1030 | | // one and sets |*out| to the result. If |a| did not fit or has the wrong type, |
1031 | | // it returns zero. |
1032 | | OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out, |
1033 | | const ASN1_INTEGER *a); |
1034 | | |
1035 | | // ASN1_INTEGER_get_int64 converts |a| to a |int64_t|. On success, it returns |
1036 | | // one and sets |*out| to the result. If |a| did not fit or has the wrong type, |
1037 | | // it returns zero. |
1038 | | OPENSSL_EXPORT int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a); |
1039 | | |
1040 | | // BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai| |
1041 | | // on success or NULL or error. If |ai| is NULL, it returns a newly-allocated |
1042 | | // |ASN1_INTEGER| on success instead, which the caller must release with |
1043 | | // |ASN1_INTEGER_free|. |
1044 | | OPENSSL_EXPORT ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, |
1045 | | ASN1_INTEGER *ai); |
1046 | | |
1047 | | // ASN1_INTEGER_to_BN sets |bn| to the value of |ai| and returns |bn| on success |
1048 | | // or NULL or error. If |bn| is NULL, it returns a newly-allocated |BIGNUM| on |
1049 | | // success instead, which the caller must release with |BN_free|. |
1050 | | OPENSSL_EXPORT BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn); |
1051 | | |
1052 | | // ASN1_INTEGER_cmp compares the values of |x| and |y|. It returns an integer |
1053 | | // equal to, less than, or greater than zero if |x| is equal to, less than, or |
1054 | | // greater than |y|, respectively. |
1055 | | OPENSSL_EXPORT int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, |
1056 | | const ASN1_INTEGER *y); |
1057 | | |
1058 | | // ASN1_ENUMERATED_new calls |ASN1_STRING_type_new| with |V_ASN1_ENUMERATED|. |
1059 | | // The resulting object has value zero. |
1060 | | OPENSSL_EXPORT ASN1_ENUMERATED *ASN1_ENUMERATED_new(void); |
1061 | | |
1062 | | // ASN1_ENUMERATED_free calls |ASN1_STRING_free|. |
1063 | | OPENSSL_EXPORT void ASN1_ENUMERATED_free(ASN1_ENUMERATED *str); |
1064 | | |
1065 | | // d2i_ASN1_ENUMERATED parses up to |len| bytes from |*inp| as a DER-encoded |
1066 | | // ASN.1 ENUMERATED, as described in |d2i_SAMPLE|. |
1067 | | OPENSSL_EXPORT ASN1_ENUMERATED *d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **out, |
1068 | | const uint8_t **inp, |
1069 | | long len); |
1070 | | |
1071 | | // i2d_ASN1_ENUMERATED marshals |in| as a DER-encoded ASN.1 ENUMERATED, as |
1072 | | // described in |i2d_SAMPLE|. |
1073 | | OPENSSL_EXPORT int i2d_ASN1_ENUMERATED(const ASN1_ENUMERATED *in, |
1074 | | uint8_t **outp); |
1075 | | |
1076 | | // ASN1_ENUMERATED is an |ASN1_ITEM| with ASN.1 type ENUMERATED and C type |
1077 | | // |ASN1_ENUMERATED*|. |
1078 | | DECLARE_ASN1_ITEM(ASN1_ENUMERATED) |
1079 | | |
1080 | | // ASN1_ENUMERATED_set_uint64 sets |a| to an ENUMERATED with value |v|. It |
1081 | | // returns one on success and zero on error. |
1082 | | OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v); |
1083 | | |
1084 | | // ASN1_ENUMERATED_set_int64 sets |a| to an ENUMERATED with value |v|. It |
1085 | | // returns one on success and zero on error. |
1086 | | OPENSSL_EXPORT int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *out, int64_t v); |
1087 | | |
1088 | | // ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it |
1089 | | // returns one and sets |*out| to the result. If |a| did not fit or has the |
1090 | | // wrong type, it returns zero. |
1091 | | OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out, |
1092 | | const ASN1_ENUMERATED *a); |
1093 | | |
1094 | | // ASN1_ENUMERATED_get_int64 converts |a| to a |int64_t|. On success, it |
1095 | | // returns one and sets |*out| to the result. If |a| did not fit or has the |
1096 | | // wrong type, it returns zero. |
1097 | | OPENSSL_EXPORT int ASN1_ENUMERATED_get_int64(int64_t *out, |
1098 | | const ASN1_ENUMERATED *a); |
1099 | | |
1100 | | // BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns |
1101 | | // |ai| on success or NULL or error. If |ai| is NULL, it returns a |
1102 | | // newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must |
1103 | | // release with |ASN1_ENUMERATED_free|. |
1104 | | OPENSSL_EXPORT ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, |
1105 | | ASN1_ENUMERATED *ai); |
1106 | | |
1107 | | // ASN1_ENUMERATED_to_BN sets |bn| to the value of |ai| and returns |bn| on |
1108 | | // success or NULL or error. If |bn| is NULL, it returns a newly-allocated |
1109 | | // |BIGNUM| on success instead, which the caller must release with |BN_free|. |
1110 | | OPENSSL_EXPORT BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, |
1111 | | BIGNUM *bn); |
1112 | | |
1113 | | |
1114 | | // Time. |
1115 | | // |
1116 | | // GeneralizedTime and UTCTime values are represented as |ASN1_STRING|s. The |
1117 | | // type field is |V_ASN1_GENERALIZEDTIME| or |V_ASN1_UTCTIME|, respectively. The |
1118 | | // data field contains the DER encoding of the value. For example, the UNIX |
1119 | | // epoch would be "19700101000000Z" for a GeneralizedTime and "700101000000Z" |
1120 | | // for a UTCTime. |
1121 | | // |
1122 | | // ASN.1 does not define how to interpret UTCTime's two-digit year. RFC 5280 |
1123 | | // defines it as a range from 1950 to 2049 for X.509. The library uses the |
1124 | | // RFC 5280 interpretation. It does not currently enforce the restrictions from |
1125 | | // BER, and the additional restrictions from RFC 5280, but future versions may. |
1126 | | // Callers should not rely on fractional seconds and non-UTC time zones. |
1127 | | // |
1128 | | // The |ASN1_TIME| typedef is a multi-string representing the X.509 Time type, |
1129 | | // which is a CHOICE of GeneralizedTime and UTCTime, using UTCTime when the |
1130 | | // value is in range. |
1131 | | |
1132 | | // ASN1_UTCTIME_new calls |ASN1_STRING_type_new| with |V_ASN1_UTCTIME|. The |
1133 | | // resulting object contains empty contents and must be initialized to be a |
1134 | | // valid UTCTime. |
1135 | | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_new(void); |
1136 | | |
1137 | | // ASN1_UTCTIME_free calls |ASN1_STRING_free|. |
1138 | | OPENSSL_EXPORT void ASN1_UTCTIME_free(ASN1_UTCTIME *str); |
1139 | | |
1140 | | // d2i_ASN1_UTCTIME parses up to |len| bytes from |*inp| as a DER-encoded |
1141 | | // ASN.1 UTCTime, as described in |d2i_SAMPLE|. |
1142 | | // |
1143 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
1144 | | // BER, but this will be removed in the future. |
1145 | | OPENSSL_EXPORT ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **out, |
1146 | | const uint8_t **inp, long len); |
1147 | | |
1148 | | // i2d_ASN1_UTCTIME marshals |in| as a DER-encoded ASN.1 UTCTime, as |
1149 | | // described in |i2d_SAMPLE|. |
1150 | | OPENSSL_EXPORT int i2d_ASN1_UTCTIME(const ASN1_UTCTIME *in, uint8_t **outp); |
1151 | | |
1152 | | // ASN1_UTCTIME is an |ASN1_ITEM| with ASN.1 type UTCTime and C type |
1153 | | // |ASN1_UTCTIME*|. |
1154 | | DECLARE_ASN1_ITEM(ASN1_UTCTIME) |
1155 | | |
1156 | | // ASN1_UTCTIME_check returns one if |a| is a valid UTCTime and zero otherwise. |
1157 | | OPENSSL_EXPORT int ASN1_UTCTIME_check(const ASN1_UTCTIME *a); |
1158 | | |
1159 | | // ASN1_UTCTIME_set represents |posix_time| as a UTCTime and writes the result |
1160 | | // to |s|. It returns |s| on success and NULL on error. If |s| is NULL, it |
1161 | | // returns a newly-allocated |ASN1_UTCTIME| instead. |
1162 | | // |
1163 | | // Note this function may fail if the time is out of range for UTCTime. |
1164 | | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, |
1165 | | int64_t posix_time); |
1166 | | |
1167 | | // ASN1_UTCTIME_adj adds |offset_day| days and |offset_sec| seconds to |
1168 | | // |posix_time| and writes the result to |s| as a UTCTime. It returns |s| on |
1169 | | // success and NULL on error. If |s| is NULL, it returns a newly-allocated |
1170 | | // |ASN1_UTCTIME| instead. |
1171 | | // |
1172 | | // Note this function may fail if the time overflows or is out of range for |
1173 | | // UTCTime. |
1174 | | OPENSSL_EXPORT ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, |
1175 | | int64_t posix_time, |
1176 | | int offset_day, long offset_sec); |
1177 | | |
1178 | | // ASN1_UTCTIME_set_string sets |s| to a UTCTime whose contents are a copy of |
1179 | | // |str|. It returns one on success and zero on error or if |str| is not a valid |
1180 | | // UTCTime. |
1181 | | // |
1182 | | // If |s| is NULL, this function validates |str| without copying it. |
1183 | | OPENSSL_EXPORT int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str); |
1184 | | |
1185 | | // ASN1_GENERALIZEDTIME_new calls |ASN1_STRING_type_new| with |
1186 | | // |V_ASN1_GENERALIZEDTIME|. The resulting object contains empty contents and |
1187 | | // must be initialized to be a valid GeneralizedTime. |
1188 | | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_new(void); |
1189 | | |
1190 | | // ASN1_GENERALIZEDTIME_free calls |ASN1_STRING_free|. |
1191 | | OPENSSL_EXPORT void ASN1_GENERALIZEDTIME_free(ASN1_GENERALIZEDTIME *str); |
1192 | | |
1193 | | // d2i_ASN1_GENERALIZEDTIME parses up to |len| bytes from |*inp| as a |
1194 | | // DER-encoded ASN.1 GeneralizedTime, as described in |d2i_SAMPLE|. |
1195 | | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME( |
1196 | | ASN1_GENERALIZEDTIME **out, const uint8_t **inp, long len); |
1197 | | |
1198 | | // i2d_ASN1_GENERALIZEDTIME marshals |in| as a DER-encoded ASN.1 |
1199 | | // GeneralizedTime, as described in |i2d_SAMPLE|. |
1200 | | OPENSSL_EXPORT int i2d_ASN1_GENERALIZEDTIME(const ASN1_GENERALIZEDTIME *in, |
1201 | | uint8_t **outp); |
1202 | | |
1203 | | // ASN1_GENERALIZEDTIME is an |ASN1_ITEM| with ASN.1 type GeneralizedTime and C |
1204 | | // type |ASN1_GENERALIZEDTIME*|. |
1205 | | DECLARE_ASN1_ITEM(ASN1_GENERALIZEDTIME) |
1206 | | |
1207 | | // ASN1_GENERALIZEDTIME_check returns one if |a| is a valid GeneralizedTime and |
1208 | | // zero otherwise. |
1209 | | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a); |
1210 | | |
1211 | | // ASN1_GENERALIZEDTIME_set represents |posix_time| as a GeneralizedTime and |
1212 | | // writes the result to |s|. It returns |s| on success and NULL on error. If |s| |
1213 | | // is NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| instead. |
1214 | | // |
1215 | | // Note this function may fail if the time is out of range for GeneralizedTime. |
1216 | | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set( |
1217 | | ASN1_GENERALIZEDTIME *s, int64_t posix_time); |
1218 | | |
1219 | | // ASN1_GENERALIZEDTIME_adj adds |offset_day| days and |offset_sec| seconds to |
1220 | | // |posix_time| and writes the result to |s| as a GeneralizedTime. It returns |
1221 | | // |s| on success and NULL on error. If |s| is NULL, it returns a |
1222 | | // newly-allocated |ASN1_GENERALIZEDTIME| instead. |
1223 | | // |
1224 | | // Note this function may fail if the time overflows or is out of range for |
1225 | | // GeneralizedTime. |
1226 | | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj( |
1227 | | ASN1_GENERALIZEDTIME *s, int64_t posix_time, int offset_day, |
1228 | | long offset_sec); |
1229 | | |
1230 | | // ASN1_GENERALIZEDTIME_set_string sets |s| to a GeneralizedTime whose contents |
1231 | | // are a copy of |str|. It returns one on success and zero on error or if |str| |
1232 | | // is not a valid GeneralizedTime. |
1233 | | // |
1234 | | // If |s| is NULL, this function validates |str| without copying it. |
1235 | | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, |
1236 | | const char *str); |
1237 | | |
1238 | | // B_ASN1_TIME is a bitmask of types allowed in an X.509 Time. |
1239 | | #define B_ASN1_TIME (B_ASN1_UTCTIME | B_ASN1_GENERALIZEDTIME) |
1240 | | |
1241 | | // ASN1_TIME_new returns a newly-allocated |ASN1_TIME| with type -1, or NULL on |
1242 | | // error. The resulting |ASN1_TIME| is not a valid X.509 Time until initialized |
1243 | | // with a value. |
1244 | | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_new(void); |
1245 | | |
1246 | | // ASN1_TIME_free releases memory associated with |str|. |
1247 | | OPENSSL_EXPORT void ASN1_TIME_free(ASN1_TIME *str); |
1248 | | |
1249 | | // d2i_ASN1_TIME parses up to |len| bytes from |*inp| as a DER-encoded X.509 |
1250 | | // Time (RFC 5280), as described in |d2i_SAMPLE|. |
1251 | | // |
1252 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
1253 | | // BER, but this will be removed in the future. |
1254 | | OPENSSL_EXPORT ASN1_TIME *d2i_ASN1_TIME(ASN1_TIME **out, const uint8_t **inp, |
1255 | | long len); |
1256 | | |
1257 | | // i2d_ASN1_TIME marshals |in| as a DER-encoded X.509 Time (RFC 5280), as |
1258 | | // described in |i2d_SAMPLE|. |
1259 | | OPENSSL_EXPORT int i2d_ASN1_TIME(const ASN1_TIME *in, uint8_t **outp); |
1260 | | |
1261 | | // ASN1_TIME is an |ASN1_ITEM| whose ASN.1 type is X.509 Time (RFC 5280) and C |
1262 | | // type is |ASN1_TIME*|. |
1263 | | DECLARE_ASN1_ITEM(ASN1_TIME) |
1264 | | |
1265 | | // ASN1_TIME_diff computes |to| - |from|. On success, it sets |*out_days| to the |
1266 | | // difference in days, rounded towards zero, sets |*out_seconds| to the |
1267 | | // remainder, and returns one. On error, it returns zero. |
1268 | | // |
1269 | | // If |from| is before |to|, both outputs will be <= 0, with at least one |
1270 | | // negative. If |from| is after |to|, both will be >= 0, with at least one |
1271 | | // positive. If they are equal, ignoring fractional seconds, both will be zero. |
1272 | | // |
1273 | | // Note this function may fail on overflow, or if |from| or |to| cannot be |
1274 | | // decoded. |
1275 | | OPENSSL_EXPORT int ASN1_TIME_diff(int *out_days, int *out_seconds, |
1276 | | const ASN1_TIME *from, const ASN1_TIME *to); |
1277 | | |
1278 | | // ASN1_TIME_set_posix represents |posix_time| as a GeneralizedTime or UTCTime |
1279 | | // and writes the result to |s|. As in RFC 5280, section 4.1.2.5, it uses |
1280 | | // UTCTime when the time fits and GeneralizedTime otherwise. It returns |s| on |
1281 | | // success and NULL on error. If |s| is NULL, it returns a newly-allocated |
1282 | | // |ASN1_TIME| instead. |
1283 | | // |
1284 | | // Note this function may fail if the time is out of range for GeneralizedTime. |
1285 | | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time); |
1286 | | |
1287 | | // ASN1_TIME_set is exactly the same as |ASN1_TIME_set_posix| but with a |
1288 | | // time_t as input for compatibility. |
1289 | | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time); |
1290 | | |
1291 | | // ASN1_TIME_adj adds |offset_day| days and |offset_sec| seconds to |
1292 | | // |posix_time| and writes the result to |s|. As in RFC 5280, section 4.1.2.5, |
1293 | | // it uses UTCTime when the time fits and GeneralizedTime otherwise. It returns |
1294 | | // |s| on success and NULL on error. If |s| is NULL, it returns a |
1295 | | // newly-allocated |ASN1_GENERALIZEDTIME| instead. |
1296 | | // |
1297 | | // Note this function may fail if the time overflows or is out of range for |
1298 | | // GeneralizedTime. |
1299 | | OPENSSL_EXPORT ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, |
1300 | | int offset_day, long offset_sec); |
1301 | | |
1302 | | // ASN1_TIME_check returns one if |t| is a valid UTCTime or GeneralizedTime, and |
1303 | | // zero otherwise. |t|'s type determines which check is performed. This |
1304 | | // function does not enforce that UTCTime was used when possible. |
1305 | | OPENSSL_EXPORT int ASN1_TIME_check(const ASN1_TIME *t); |
1306 | | |
1307 | | // ASN1_TIME_to_generalizedtime converts |t| to a GeneralizedTime. If |out| is |
1308 | | // NULL, it returns a newly-allocated |ASN1_GENERALIZEDTIME| on success, or NULL |
1309 | | // on error. If |out| is non-NULL and |*out| is NULL, it additionally sets |
1310 | | // |*out| to the result. If |out| and |*out| are non-NULL, it instead updates |
1311 | | // the object pointed by |*out| and returns |*out| on success or NULL on error. |
1312 | | OPENSSL_EXPORT ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime( |
1313 | | const ASN1_TIME *t, ASN1_GENERALIZEDTIME **out); |
1314 | | |
1315 | | // ASN1_TIME_set_string behaves like |ASN1_UTCTIME_set_string| if |str| is a |
1316 | | // valid UTCTime, and |ASN1_GENERALIZEDTIME_set_string| if |str| is a valid |
1317 | | // GeneralizedTime. If |str| is neither, it returns zero. |
1318 | | OPENSSL_EXPORT int ASN1_TIME_set_string(ASN1_TIME *s, const char *str); |
1319 | | |
1320 | | // ASN1_TIME_set_string_X509 behaves like |ASN1_TIME_set_string| except it |
1321 | | // additionally converts GeneralizedTime to UTCTime if it is in the range where |
1322 | | // UTCTime is used. See RFC 5280, section 4.1.2.5. |
1323 | | OPENSSL_EXPORT int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str); |
1324 | | |
1325 | | // ASN1_TIME_to_time_t converts |t| to a time_t value in |out|. On |
1326 | | // success, one is returned. On failure, zero is returned. This function |
1327 | | // will fail if the time can not be represented in a time_t. |
1328 | | OPENSSL_EXPORT int ASN1_TIME_to_time_t(const ASN1_TIME *t, time_t *out); |
1329 | | |
1330 | | // ASN1_TIME_to_posix converts |t| to a POSIX time value in |out|. On |
1331 | | // success, one is returned. On failure, zero is returned. |
1332 | | OPENSSL_EXPORT int ASN1_TIME_to_posix(const ASN1_TIME *t, int64_t *out); |
1333 | | |
1334 | | // ASN1_TIME_to_posix_nonstandard converts |t| to a POSIX time value in |
1335 | | // |out|. It is exactly the same as |ASN1_TIME_to_posix| but allows for |
1336 | | // non-standard four-digit timezone offsets on UTC times. On success, one is |
1337 | | // returned. On failure, zero is returned. |ASN1_TIME_to_posix| should normally |
1338 | | // be used instead of this function. |
1339 | | OPENSSL_EXPORT int ASN1_TIME_to_posix_nonstandard( |
1340 | | const ASN1_TIME *t, int64_t *out); |
1341 | | |
1342 | | // TODO(davidben): Expand and document function prototypes generated in macros. |
1343 | | |
1344 | | |
1345 | | // NULL values. |
1346 | | // |
1347 | | // This library represents the ASN.1 NULL value by a non-NULL pointer to the |
1348 | | // opaque type |ASN1_NULL|. An omitted OPTIONAL ASN.1 NULL value is a NULL |
1349 | | // pointer. Unlike other pointer types, it is not necessary to free |ASN1_NULL| |
1350 | | // pointers, but it is safe to do so. |
1351 | | |
1352 | | // ASN1_NULL_new returns an opaque, non-NULL pointer. It is safe to call |
1353 | | // |ASN1_NULL_free| on the result, but not necessary. |
1354 | | OPENSSL_EXPORT ASN1_NULL *ASN1_NULL_new(void); |
1355 | | |
1356 | | // ASN1_NULL_free does nothing. |
1357 | | OPENSSL_EXPORT void ASN1_NULL_free(ASN1_NULL *null); |
1358 | | |
1359 | | // d2i_ASN1_NULL parses a DER-encoded ASN.1 NULL value from up to |len| bytes |
1360 | | // at |*inp|, as described in |d2i_SAMPLE|. |
1361 | | OPENSSL_EXPORT ASN1_NULL *d2i_ASN1_NULL(ASN1_NULL **out, const uint8_t **inp, |
1362 | | long len); |
1363 | | |
1364 | | // i2d_ASN1_NULL marshals |in| as a DER-encoded ASN.1 NULL value, as described |
1365 | | // in |i2d_SAMPLE|. |
1366 | | OPENSSL_EXPORT int i2d_ASN1_NULL(const ASN1_NULL *in, uint8_t **outp); |
1367 | | |
1368 | | // ASN1_NULL is an |ASN1_ITEM| with ASN.1 type NULL and C type |ASN1_NULL*|. |
1369 | | DECLARE_ASN1_ITEM(ASN1_NULL) |
1370 | | |
1371 | | |
1372 | | // Object identifiers. |
1373 | | // |
1374 | | // An |ASN1_OBJECT| represents a ASN.1 OBJECT IDENTIFIER. See also obj.h for |
1375 | | // additional functions relating to |ASN1_OBJECT|. |
1376 | | // |
1377 | | // TODO(davidben): What's the relationship between asn1.h and obj.h? Most of |
1378 | | // obj.h deals with the large NID table, but then functions like |OBJ_get0_data| |
1379 | | // or |OBJ_dup| are general |ASN1_OBJECT| functions. |
1380 | | |
1381 | | DEFINE_STACK_OF(ASN1_OBJECT) |
1382 | | |
1383 | | // ASN1_OBJECT_create returns a newly-allocated |ASN1_OBJECT| with |len| bytes |
1384 | | // from |data| as the encoded OID, or NULL on error. |data| should contain the |
1385 | | // DER-encoded identifier, excluding the tag and length. |
1386 | | // |
1387 | | // |nid| should be |NID_undef|. Passing a NID value that does not match |data| |
1388 | | // will cause some functions to misbehave. |sn| and |ln| should be NULL. If |
1389 | | // non-NULL, they are stored as short and long names, respectively, but these |
1390 | | // values have no effect for |ASN1_OBJECT|s created through this function. |
1391 | | // |
1392 | | // TODO(davidben): Should we just ignore all those parameters? NIDs and names |
1393 | | // are only relevant for |ASN1_OBJECT|s in the obj.h table. |
1394 | | OPENSSL_EXPORT ASN1_OBJECT *ASN1_OBJECT_create(int nid, const uint8_t *data, |
1395 | | size_t len, const char *sn, |
1396 | | const char *ln); |
1397 | | |
1398 | | // ASN1_OBJECT_free releases memory associated with |a|. If |a| is a static |
1399 | | // |ASN1_OBJECT|, returned from |OBJ_nid2obj|, this function does nothing. |
1400 | | OPENSSL_EXPORT void ASN1_OBJECT_free(ASN1_OBJECT *a); |
1401 | | |
1402 | | // d2i_ASN1_OBJECT parses a DER-encoded ASN.1 OBJECT IDENTIFIER from up to |len| |
1403 | | // bytes at |*inp|, as described in |d2i_SAMPLE|. |
1404 | | OPENSSL_EXPORT ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, |
1405 | | const uint8_t **inp, long len); |
1406 | | |
1407 | | // i2d_ASN1_OBJECT marshals |in| as a DER-encoded ASN.1 OBJECT IDENTIFIER, as |
1408 | | // described in |i2d_SAMPLE|. |
1409 | | OPENSSL_EXPORT int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, uint8_t **outp); |
1410 | | |
1411 | | // c2i_ASN1_OBJECT decodes |len| bytes from |*inp| as the contents of a |
1412 | | // DER-encoded OBJECT IDENTIFIER, excluding the tag and length. It behaves like |
1413 | | // |d2i_SAMPLE| except, on success, it always consumes all |len| bytes. |
1414 | | OPENSSL_EXPORT ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, |
1415 | | const uint8_t **inp, long len); |
1416 | | |
1417 | | // ASN1_OBJECT is an |ASN1_ITEM| with ASN.1 type OBJECT IDENTIFIER and C type |
1418 | | // |ASN1_OBJECT*|. |
1419 | | DECLARE_ASN1_ITEM(ASN1_OBJECT) |
1420 | | |
1421 | | |
1422 | | // Arbitrary elements. |
1423 | | |
1424 | | // An asn1_type_st (aka |ASN1_TYPE|) represents an arbitrary ASN.1 element, |
1425 | | // typically used for ANY types. It contains a |type| field and a |value| union |
1426 | | // dependent on |type|. |
1427 | | // |
1428 | | // WARNING: This struct has a complex representation. Callers must not construct |
1429 | | // |ASN1_TYPE| values manually. Use |ASN1_TYPE_set| and |ASN1_TYPE_set1| |
1430 | | // instead. Additionally, callers performing non-trivial operations on this type |
1431 | | // are encouraged to use |CBS| and |CBB| from <openssl/bytestring.h>, and |
1432 | | // convert to or from |ASN1_TYPE| with |d2i_ASN1_TYPE| or |i2d_ASN1_TYPE|. |
1433 | | // |
1434 | | // The |type| field corresponds to the tag of the ASN.1 element being |
1435 | | // represented: |
1436 | | // |
1437 | | // If |type| is a |V_ASN1_*| constant for an ASN.1 string-like type, as defined |
1438 | | // by |ASN1_STRING|, the tag matches the constant. |value| contains an |
1439 | | // |ASN1_STRING| pointer (equivalently, one of the more specific typedefs). See |
1440 | | // |ASN1_STRING| for details on the representation. Unlike |ASN1_STRING|, |
1441 | | // |ASN1_TYPE| does not use the |V_ASN1_NEG| flag for negative INTEGER and |
1442 | | // ENUMERATE values. For a negative value, the |ASN1_TYPE|'s |type| will be |
1443 | | // |V_ASN1_INTEGER| or |V_ASN1_ENUMERATED|, but |value| will an |ASN1_STRING| |
1444 | | // whose |type| is |V_ASN1_NEG_INTEGER| or |V_ASN1_NEG_ENUMERATED|. |
1445 | | // |
1446 | | // If |type| is |V_ASN1_OBJECT|, the tag is OBJECT IDENTIFIER and |value| |
1447 | | // contains an |ASN1_OBJECT| pointer. |
1448 | | // |
1449 | | // If |type| is |V_ASN1_NULL|, the tag is NULL. |value| contains a NULL pointer. |
1450 | | // |
1451 | | // If |type| is |V_ASN1_BOOLEAN|, the tag is BOOLEAN. |value| contains an |
1452 | | // |ASN1_BOOLEAN|. |
1453 | | // |
1454 | | // If |type| is |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |V_ASN1_OTHER|, the tag is |
1455 | | // SEQUENCE, SET, or some arbitrary tag, respectively. |value| uses the |
1456 | | // corresponding |ASN1_STRING| representation. Although any type may be |
1457 | | // represented in |V_ASN1_OTHER|, the parser will always return the more |
1458 | | // specific encoding when available. |
1459 | | // |
1460 | | // Other values of |type| do not represent a valid ASN.1 value, though |
1461 | | // default-constructed objects may set |type| to -1. Such objects cannot be |
1462 | | // serialized. |
1463 | | struct asn1_type_st { |
1464 | | int type; |
1465 | | union { |
1466 | | char *ptr; |
1467 | | ASN1_BOOLEAN boolean; |
1468 | | ASN1_STRING *asn1_string; |
1469 | | ASN1_OBJECT *object; |
1470 | | ASN1_INTEGER *integer; |
1471 | | ASN1_ENUMERATED *enumerated; |
1472 | | ASN1_BIT_STRING *bit_string; |
1473 | | ASN1_OCTET_STRING *octet_string; |
1474 | | ASN1_PRINTABLESTRING *printablestring; |
1475 | | ASN1_T61STRING *t61string; |
1476 | | ASN1_IA5STRING *ia5string; |
1477 | | ASN1_GENERALSTRING *generalstring; |
1478 | | ASN1_BMPSTRING *bmpstring; |
1479 | | ASN1_UNIVERSALSTRING *universalstring; |
1480 | | ASN1_UTCTIME *utctime; |
1481 | | ASN1_GENERALIZEDTIME *generalizedtime; |
1482 | | ASN1_VISIBLESTRING *visiblestring; |
1483 | | ASN1_UTF8STRING *utf8string; |
1484 | | // set and sequence are left complete and still contain the entire element. |
1485 | | ASN1_STRING *set; |
1486 | | ASN1_STRING *sequence; |
1487 | | ASN1_VALUE *asn1_value; |
1488 | | } value; |
1489 | | }; |
1490 | | |
1491 | | DEFINE_STACK_OF(ASN1_TYPE) |
1492 | | |
1493 | | // ASN1_TYPE_new returns a newly-allocated |ASN1_TYPE|, or NULL on allocation |
1494 | | // failure. The resulting object has type -1 and must be initialized to be |
1495 | | // a valid ANY value. |
1496 | | OPENSSL_EXPORT ASN1_TYPE *ASN1_TYPE_new(void); |
1497 | | |
1498 | | // ASN1_TYPE_free releases memory associated with |a|. |
1499 | | OPENSSL_EXPORT void ASN1_TYPE_free(ASN1_TYPE *a); |
1500 | | |
1501 | | // d2i_ASN1_TYPE parses up to |len| bytes from |*inp| as an ASN.1 value of any |
1502 | | // type, as described in |d2i_SAMPLE|. Note this function only validates |
1503 | | // primitive, universal types supported by this library. Values of type |
1504 | | // |V_ASN1_SEQUENCE|, |V_ASN1_SET|, |V_ASN1_OTHER|, or an unsupported primitive |
1505 | | // type must be validated by the caller when interpreting. |
1506 | | // |
1507 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
1508 | | // BER, but this will be removed in the future. |
1509 | | OPENSSL_EXPORT ASN1_TYPE *d2i_ASN1_TYPE(ASN1_TYPE **out, const uint8_t **inp, |
1510 | | long len); |
1511 | | |
1512 | | // i2d_ASN1_TYPE marshals |in| as DER, as described in |i2d_SAMPLE|. |
1513 | | OPENSSL_EXPORT int i2d_ASN1_TYPE(const ASN1_TYPE *in, uint8_t **outp); |
1514 | | |
1515 | | // ASN1_ANY is an |ASN1_ITEM| with ASN.1 type ANY and C type |ASN1_TYPE*|. Note |
1516 | | // the |ASN1_ITEM| name and C type do not match. |
1517 | | DECLARE_ASN1_ITEM(ASN1_ANY) |
1518 | | |
1519 | | // ASN1_TYPE_get returns the type of |a|, which will be one of the |V_ASN1_*| |
1520 | | // constants, or zero if |a| is not fully initialized. |
1521 | | OPENSSL_EXPORT int ASN1_TYPE_get(const ASN1_TYPE *a); |
1522 | | |
1523 | | // ASN1_TYPE_set sets |a| to an |ASN1_TYPE| of type |type| and value |value|, |
1524 | | // releasing the previous contents of |a|. |
1525 | | // |
1526 | | // If |type| is |V_ASN1_BOOLEAN|, |a| is set to FALSE if |value| is NULL and |
1527 | | // TRUE otherwise. If setting |a| to TRUE, |value| may be an invalid pointer, |
1528 | | // such as (void*)1. |
1529 | | // |
1530 | | // If |type| is |V_ASN1_NULL|, |value| must be NULL. |
1531 | | // |
1532 | | // For other values of |type|, this function takes ownership of |value|, which |
1533 | | // must point to an object of the corresponding type. See |ASN1_TYPE| for |
1534 | | // details. |
1535 | | OPENSSL_EXPORT void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value); |
1536 | | |
1537 | | // ASN1_TYPE_set1 behaves like |ASN1_TYPE_set| except it does not take ownership |
1538 | | // of |value|. It returns one on success and zero on error. |
1539 | | OPENSSL_EXPORT int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value); |
1540 | | |
1541 | | // ASN1_TYPE_cmp returns zero if |a| and |b| are equal and some non-zero value |
1542 | | // otherwise. Note this function can only be used for equality checks, not an |
1543 | | // ordering. |
1544 | | OPENSSL_EXPORT int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b); |
1545 | | |
1546 | | typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY; |
1547 | | |
1548 | | // d2i_ASN1_SEQUENCE_ANY parses up to |len| bytes from |*inp| as a DER-encoded |
1549 | | // ASN.1 SEQUENCE OF ANY structure, as described in |d2i_SAMPLE|. The resulting |
1550 | | // |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with |
1551 | | // |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. |
1552 | | // |
1553 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
1554 | | // BER, but this will be removed in the future. |
1555 | | OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SEQUENCE_ANY(ASN1_SEQUENCE_ANY **out, |
1556 | | const uint8_t **inp, |
1557 | | long len); |
1558 | | |
1559 | | // i2d_ASN1_SEQUENCE_ANY marshals |in| as a DER-encoded SEQUENCE OF ANY |
1560 | | // structure, as described in |i2d_SAMPLE|. |
1561 | | OPENSSL_EXPORT int i2d_ASN1_SEQUENCE_ANY(const ASN1_SEQUENCE_ANY *in, |
1562 | | uint8_t **outp); |
1563 | | |
1564 | | // d2i_ASN1_SET_ANY parses up to |len| bytes from |*inp| as a DER-encoded ASN.1 |
1565 | | // SET OF ANY structure, as described in |d2i_SAMPLE|. The resulting |
1566 | | // |ASN1_SEQUENCE_ANY| owns its contents and thus must be released with |
1567 | | // |sk_ASN1_TYPE_pop_free| and |ASN1_TYPE_free|, not |sk_ASN1_TYPE_free|. |
1568 | | // |
1569 | | // TODO(https://crbug.com/boringssl/354): This function currently also accepts |
1570 | | // BER, but this will be removed in the future. |
1571 | | OPENSSL_EXPORT ASN1_SEQUENCE_ANY *d2i_ASN1_SET_ANY(ASN1_SEQUENCE_ANY **out, |
1572 | | const uint8_t **inp, |
1573 | | long len); |
1574 | | |
1575 | | // i2d_ASN1_SET_ANY marshals |in| as a DER-encoded SET OF ANY structure, as |
1576 | | // described in |i2d_SAMPLE|. |
1577 | | OPENSSL_EXPORT int i2d_ASN1_SET_ANY(const ASN1_SEQUENCE_ANY *in, |
1578 | | uint8_t **outp); |
1579 | | |
1580 | | |
1581 | | // Human-readable output. |
1582 | | // |
1583 | | // The following functions output types in some human-readable format. These |
1584 | | // functions may be used for debugging and logging. However, the output should |
1585 | | // not be consumed programmatically. They may be ambiguous or lose information. |
1586 | | |
1587 | | // ASN1_UTCTIME_print writes a human-readable representation of |a| to |out|. It |
1588 | | // returns one on success and zero on error. |
1589 | | OPENSSL_EXPORT int ASN1_UTCTIME_print(BIO *out, const ASN1_UTCTIME *a); |
1590 | | |
1591 | | // ASN1_GENERALIZEDTIME_print writes a human-readable representation of |a| to |
1592 | | // |out|. It returns one on success and zero on error. |
1593 | | OPENSSL_EXPORT int ASN1_GENERALIZEDTIME_print(BIO *out, |
1594 | | const ASN1_GENERALIZEDTIME *a); |
1595 | | |
1596 | | // ASN1_TIME_print writes a human-readable representation of |a| to |out|. It |
1597 | | // returns one on success and zero on error. |
1598 | | OPENSSL_EXPORT int ASN1_TIME_print(BIO *out, const ASN1_TIME *a); |
1599 | | |
1600 | | // ASN1_STRING_print writes a human-readable representation of |str| to |out|. |
1601 | | // It returns one on success and zero on error. Unprintable characters are |
1602 | | // replaced with '.'. |
1603 | | OPENSSL_EXPORT int ASN1_STRING_print(BIO *out, const ASN1_STRING *str); |
1604 | | |
1605 | | // The following flags must not collide with |XN_FLAG_*|. |
1606 | | |
1607 | | // ASN1_STRFLGS_ESC_2253 causes characters to be escaped as in RFC 2253, section |
1608 | | // 2.4. |
1609 | 3.76k | #define ASN1_STRFLGS_ESC_2253 1ul |
1610 | | |
1611 | | // ASN1_STRFLGS_ESC_CTRL causes all control characters to be escaped. |
1612 | 6.55k | #define ASN1_STRFLGS_ESC_CTRL 2ul |
1613 | | |
1614 | | // ASN1_STRFLGS_ESC_MSB causes all characters above 127 to be escaped. |
1615 | 11.8k | #define ASN1_STRFLGS_ESC_MSB 4ul |
1616 | | |
1617 | | // ASN1_STRFLGS_ESC_QUOTE causes the string to be surrounded by quotes, rather |
1618 | | // than using backslashes, when characters are escaped. Fewer characters will |
1619 | | // require escapes in this case. |
1620 | 1.24k | #define ASN1_STRFLGS_ESC_QUOTE 8ul |
1621 | | |
1622 | | // ASN1_STRFLGS_UTF8_CONVERT causes the string to be encoded as UTF-8, with each |
1623 | | // byte in the UTF-8 encoding treated as an individual character for purposes of |
1624 | | // escape sequences. If not set, each Unicode codepoint in the string is treated |
1625 | | // as a character, with wide characters escaped as "\Uxxxx" or "\Wxxxxxxxx". |
1626 | | // Note this can be ambiguous if |ASN1_STRFLGS_ESC_*| are all unset. In that |
1627 | | // case, backslashes are not escaped, but wide characters are. |
1628 | 9.04k | #define ASN1_STRFLGS_UTF8_CONVERT 0x10ul |
1629 | | |
1630 | | // ASN1_STRFLGS_IGNORE_TYPE causes the string type to be ignored. The |
1631 | | // |ASN1_STRING| in-memory representation will be printed directly. |
1632 | 1.18k | #define ASN1_STRFLGS_IGNORE_TYPE 0x20ul |
1633 | | |
1634 | | // ASN1_STRFLGS_SHOW_TYPE causes the string type to be included in the output. |
1635 | 1.18k | #define ASN1_STRFLGS_SHOW_TYPE 0x40ul |
1636 | | |
1637 | | // ASN1_STRFLGS_DUMP_ALL causes all strings to be printed as a hexdump, using |
1638 | | // RFC 2253 hexstring notation, such as "#0123456789ABCDEF". |
1639 | 1.18k | #define ASN1_STRFLGS_DUMP_ALL 0x80ul |
1640 | | |
1641 | | // ASN1_STRFLGS_DUMP_UNKNOWN behaves like |ASN1_STRFLGS_DUMP_ALL| but only |
1642 | | // applies to values of unknown type. If unset, unknown values will print |
1643 | | // their contents as single-byte characters with escape sequences. |
1644 | 776 | #define ASN1_STRFLGS_DUMP_UNKNOWN 0x100ul |
1645 | | |
1646 | | // ASN1_STRFLGS_DUMP_DER causes hexdumped strings (as determined by |
1647 | | // |ASN1_STRFLGS_DUMP_ALL| or |ASN1_STRFLGS_DUMP_UNKNOWN|) to print the entire |
1648 | | // DER element as in RFC 2253, rather than only the contents of the |
1649 | | // |ASN1_STRING|. |
1650 | 776 | #define ASN1_STRFLGS_DUMP_DER 0x200ul |
1651 | | |
1652 | | // ASN1_STRFLGS_RFC2253 causes the string to be escaped as in RFC 2253, |
1653 | | // additionally escaping control characters. |
1654 | | #define ASN1_STRFLGS_RFC2253 \ |
1655 | 241 | (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | \ |
1656 | 241 | ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_DUMP_UNKNOWN | \ |
1657 | 241 | ASN1_STRFLGS_DUMP_DER) |
1658 | | |
1659 | | // ASN1_STRING_print_ex writes a human-readable representation of |str| to |
1660 | | // |out|. It returns the number of bytes written on success and -1 on error. If |
1661 | | // |out| is NULL, it returns the number of bytes it would have written, without |
1662 | | // writing anything. |
1663 | | // |
1664 | | // The |flags| should be a combination of combination of |ASN1_STRFLGS_*| |
1665 | | // constants. See the documentation for each flag for how it controls the |
1666 | | // output. If unsure, use |ASN1_STRFLGS_RFC2253|. |
1667 | | OPENSSL_EXPORT int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, |
1668 | | unsigned long flags); |
1669 | | |
1670 | | // ASN1_STRING_print_ex_fp behaves like |ASN1_STRING_print_ex| but writes to a |
1671 | | // |FILE| rather than a |BIO|. |
1672 | | OPENSSL_EXPORT int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, |
1673 | | unsigned long flags); |
1674 | | |
1675 | | // i2a_ASN1_INTEGER writes a human-readable representation of |a| to |bp|. It |
1676 | | // returns the number of bytes written on success, or a negative number on |
1677 | | // error. On error, this function may have written a partial output to |bp|. |
1678 | | OPENSSL_EXPORT int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a); |
1679 | | |
1680 | | // i2a_ASN1_ENUMERATED writes a human-readable representation of |a| to |bp|. It |
1681 | | // returns the number of bytes written on success, or a negative number on |
1682 | | // error. On error, this function may have written a partial output to |bp|. |
1683 | | OPENSSL_EXPORT int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a); |
1684 | | |
1685 | | // i2a_ASN1_OBJECT writes a human-readable representation of |a| to |bp|. It |
1686 | | // returns the number of bytes written on success, or a negative number on |
1687 | | // error. On error, this function may have written a partial output to |bp|. |
1688 | | OPENSSL_EXPORT int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a); |
1689 | | |
1690 | | // i2a_ASN1_STRING writes a text representation of |a|'s contents to |bp|. It |
1691 | | // returns the number of bytes written on success, or a negative number on |
1692 | | // error. On error, this function may have written a partial output to |bp|. |
1693 | | // |type| is ignored. |
1694 | | // |
1695 | | // This function does not decode |a| into a Unicode string. It only hex-encodes |
1696 | | // the internal representation of |a|. This is suitable for printing an OCTET |
1697 | | // STRING, but may not be human-readable for any other string type. |
1698 | | OPENSSL_EXPORT int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type); |
1699 | | |
1700 | | // i2t_ASN1_OBJECT calls |OBJ_obj2txt| with |always_return_oid| set to zero. |
1701 | | OPENSSL_EXPORT int i2t_ASN1_OBJECT(char *buf, int buf_len, |
1702 | | const ASN1_OBJECT *a); |
1703 | | |
1704 | | |
1705 | | // Low-level encoding functions. |
1706 | | |
1707 | | // ASN1_get_object parses a BER element from up to |max_len| bytes at |*inp|. It |
1708 | | // returns |V_ASN1_CONSTRUCTED| if it successfully parsed a constructed element, |
1709 | | // zero if it successfully parsed a primitive element, and 0x80 on error. On |
1710 | | // success, it additionally advances |*inp| to the element body, sets |
1711 | | // |*out_length|, |*out_tag|, and |*out_class| to the element's length, tag |
1712 | | // number, and tag class, respectively, |
1713 | | // |
1714 | | // Unlike OpenSSL, this function only supports DER. Indefinite and non-minimal |
1715 | | // lengths are rejected. |
1716 | | // |
1717 | | // This function is difficult to use correctly. Use |CBS_get_asn1| and related |
1718 | | // functions from bytestring.h. |
1719 | | OPENSSL_EXPORT int ASN1_get_object(const unsigned char **inp, long *out_length, |
1720 | | int *out_tag, int *out_class, long max_len); |
1721 | | |
1722 | | // ASN1_put_object writes the header for a DER or BER element to |*outp| and |
1723 | | // advances |*outp| by the number of bytes written. The caller is responsible |
1724 | | // for ensuring |*outp| has enough space for the output. The header describes an |
1725 | | // element with length |length|, tag number |tag|, and class |xclass|. |xclass| |
1726 | | // should be one of the |V_ASN1_*| tag class constants. The element is primitive |
1727 | | // if |constructed| is zero and constructed if it is one or two. If |
1728 | | // |constructed| is two, |length| is ignored and the element uses |
1729 | | // indefinite-length encoding. |
1730 | | // |
1731 | | // Use |CBB_add_asn1| instead. |
1732 | | OPENSSL_EXPORT void ASN1_put_object(unsigned char **outp, int constructed, |
1733 | | int length, int tag, int xclass); |
1734 | | |
1735 | | // ASN1_put_eoc writes two zero bytes to |*outp|, advances |*outp| to point past |
1736 | | // those bytes, and returns two. |
1737 | | // |
1738 | | // Use definite-length encoding instead. |
1739 | | OPENSSL_EXPORT int ASN1_put_eoc(unsigned char **outp); |
1740 | | |
1741 | | // ASN1_object_size returns the number of bytes needed to encode a DER or BER |
1742 | | // value with length |length| and tag number |tag|, or -1 on error. |tag| should |
1743 | | // not include the constructed bit or tag class. If |constructed| is zero or |
1744 | | // one, the result uses a definite-length encoding with minimally-encoded |
1745 | | // length, as in DER. If |constructed| is two, the result uses BER |
1746 | | // indefinite-length encoding. |
1747 | | // |
1748 | | // Use |CBB_add_asn1| instead. |
1749 | | OPENSSL_EXPORT int ASN1_object_size(int constructed, int length, int tag); |
1750 | | |
1751 | | |
1752 | | // Function declaration macros. |
1753 | | // |
1754 | | // The following macros declare functions for ASN.1 types. Prefer writing the |
1755 | | // prototypes directly. Particularly when |type|, |itname|, or |name| differ, |
1756 | | // the macros can be difficult to understand. |
1757 | | |
1758 | | #define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type) |
1759 | | |
1760 | | #define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \ |
1761 | | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type) |
1762 | | |
1763 | | #define DECLARE_ASN1_FUNCTIONS_name(type, name) \ |
1764 | | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
1765 | | DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name) |
1766 | | |
1767 | | #define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \ |
1768 | | DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
1769 | | DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) |
1770 | | |
1771 | | #define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \ |
1772 | | OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \ |
1773 | | long len); \ |
1774 | | OPENSSL_EXPORT int i2d_##name(type *a, unsigned char **out); \ |
1775 | | DECLARE_ASN1_ITEM(itname) |
1776 | | |
1777 | | #define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \ |
1778 | | OPENSSL_EXPORT type *d2i_##name(type **a, const unsigned char **in, \ |
1779 | | long len); \ |
1780 | | OPENSSL_EXPORT int i2d_##name(const type *a, unsigned char **out); \ |
1781 | | DECLARE_ASN1_ITEM(name) |
1782 | | |
1783 | | #define DECLARE_ASN1_FUNCTIONS_const(name) \ |
1784 | | DECLARE_ASN1_ALLOC_FUNCTIONS(name) \ |
1785 | | DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name) |
1786 | | |
1787 | | #define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ |
1788 | | OPENSSL_EXPORT type *name##_new(void); \ |
1789 | | OPENSSL_EXPORT void name##_free(type *a); |
1790 | | |
1791 | | |
1792 | | // Deprecated functions. |
1793 | | |
1794 | | // ASN1_STRING_set_default_mask does nothing. |
1795 | | OPENSSL_EXPORT void ASN1_STRING_set_default_mask(unsigned long mask); |
1796 | | |
1797 | | // ASN1_STRING_set_default_mask_asc returns one. |
1798 | | OPENSSL_EXPORT int ASN1_STRING_set_default_mask_asc(const char *p); |
1799 | | |
1800 | | // ASN1_STRING_get_default_mask returns |B_ASN1_UTF8STRING|. |
1801 | | OPENSSL_EXPORT unsigned long ASN1_STRING_get_default_mask(void); |
1802 | | |
1803 | | // ASN1_STRING_TABLE_cleanup does nothing. |
1804 | | OPENSSL_EXPORT void ASN1_STRING_TABLE_cleanup(void); |
1805 | | |
1806 | | // M_ASN1_* are legacy aliases for various |ASN1_STRING| functions. Use the |
1807 | | // functions themselves. |
1808 | | #define M_ASN1_STRING_length(x) ASN1_STRING_length(x) |
1809 | | #define M_ASN1_STRING_type(x) ASN1_STRING_type(x) |
1810 | | #define M_ASN1_STRING_data(x) ASN1_STRING_data(x) |
1811 | | #define M_ASN1_BIT_STRING_new() ASN1_BIT_STRING_new() |
1812 | | #define M_ASN1_BIT_STRING_free(a) ASN1_BIT_STRING_free(a) |
1813 | | #define M_ASN1_BIT_STRING_dup(a) ASN1_STRING_dup(a) |
1814 | | #define M_ASN1_BIT_STRING_cmp(a, b) ASN1_STRING_cmp(a, b) |
1815 | | #define M_ASN1_BIT_STRING_set(a, b, c) ASN1_BIT_STRING_set(a, b, c) |
1816 | | #define M_ASN1_INTEGER_new() ASN1_INTEGER_new() |
1817 | | #define M_ASN1_INTEGER_free(a) ASN1_INTEGER_free(a) |
1818 | | #define M_ASN1_INTEGER_dup(a) ASN1_INTEGER_dup(a) |
1819 | | #define M_ASN1_INTEGER_cmp(a, b) ASN1_INTEGER_cmp(a, b) |
1820 | | #define M_ASN1_ENUMERATED_new() ASN1_ENUMERATED_new() |
1821 | | #define M_ASN1_ENUMERATED_free(a) ASN1_ENUMERATED_free(a) |
1822 | | #define M_ASN1_ENUMERATED_dup(a) ASN1_STRING_dup(a) |
1823 | | #define M_ASN1_ENUMERATED_cmp(a, b) ASN1_STRING_cmp(a, b) |
1824 | | #define M_ASN1_OCTET_STRING_new() ASN1_OCTET_STRING_new() |
1825 | | #define M_ASN1_OCTET_STRING_free(a) ASN1_OCTET_STRING_free() |
1826 | | #define M_ASN1_OCTET_STRING_dup(a) ASN1_OCTET_STRING_dup(a) |
1827 | | #define M_ASN1_OCTET_STRING_cmp(a, b) ASN1_OCTET_STRING_cmp(a, b) |
1828 | | #define M_ASN1_OCTET_STRING_set(a, b, c) ASN1_OCTET_STRING_set(a, b, c) |
1829 | | #define M_ASN1_OCTET_STRING_print(a, b) ASN1_STRING_print(a, b) |
1830 | | #define M_ASN1_PRINTABLESTRING_new() ASN1_PRINTABLESTRING_new() |
1831 | | #define M_ASN1_PRINTABLESTRING_free(a) ASN1_PRINTABLESTRING_free(a) |
1832 | | #define M_ASN1_IA5STRING_new() ASN1_IA5STRING_new() |
1833 | | #define M_ASN1_IA5STRING_free(a) ASN1_IA5STRING_free(a) |
1834 | | #define M_ASN1_IA5STRING_dup(a) ASN1_STRING_dup(a) |
1835 | | #define M_ASN1_UTCTIME_new() ASN1_UTCTIME_new() |
1836 | | #define M_ASN1_UTCTIME_free(a) ASN1_UTCTIME_free(a) |
1837 | | #define M_ASN1_UTCTIME_dup(a) ASN1_STRING_dup(a) |
1838 | | #define M_ASN1_T61STRING_new() ASN1_T61STRING_new() |
1839 | | #define M_ASN1_T61STRING_free(a) ASN1_T61STRING_free(a) |
1840 | | #define M_ASN1_GENERALIZEDTIME_new() ASN1_GENERALIZEDTIME_new() |
1841 | | #define M_ASN1_GENERALIZEDTIME_free(a) ASN1_GENERALIZEDTIME_free(a) |
1842 | | #define M_ASN1_GENERALIZEDTIME_dup(a) ASN1_STRING_dup(a) |
1843 | | #define M_ASN1_GENERALSTRING_new() ASN1_GENERALSTRING_new() |
1844 | | #define M_ASN1_GENERALSTRING_free(a) ASN1_GENERALSTRING_free(a) |
1845 | | #define M_ASN1_UNIVERSALSTRING_new() ASN1_UNIVERSALSTRING_new() |
1846 | | #define M_ASN1_UNIVERSALSTRING_free(a) ASN1_UNIVERSALSTRING_free(a) |
1847 | | #define M_ASN1_BMPSTRING_new() ASN1_BMPSTRING_new() |
1848 | | #define M_ASN1_BMPSTRING_free(a) ASN1_BMPSTRING_free(a) |
1849 | | #define M_ASN1_VISIBLESTRING_new() ASN1_VISIBLESTRING_new() |
1850 | | #define M_ASN1_VISIBLESTRING_free(a) ASN1_VISIBLESTRING_free(a) |
1851 | | #define M_ASN1_UTF8STRING_new() ASN1_UTF8STRING_new() |
1852 | | #define M_ASN1_UTF8STRING_free(a) ASN1_UTF8STRING_free(a) |
1853 | | |
1854 | | // ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on |
1855 | | // success and zero on error. |
1856 | | // |
1857 | | // Use |ASN1_INTEGER_set_uint64| and |ASN1_INTEGER_set_int64| instead. |
1858 | | OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); |
1859 | | |
1860 | | // ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one |
1861 | | // on success and zero on error. |
1862 | | // |
1863 | | // Use |ASN1_ENUMERATED_set_uint64| and |ASN1_ENUMERATED_set_int64| instead. |
1864 | | OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); |
1865 | | |
1866 | | // ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of |
1867 | | // range or the wrong type. |
1868 | | // |
1869 | | // WARNING: This function's return value cannot distinguish errors from -1. |
1870 | | // Use |ASN1_INTEGER_get_uint64| and |ASN1_INTEGER_get_int64| instead. |
1871 | | OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a); |
1872 | | |
1873 | | // ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out |
1874 | | // of range or the wrong type. |
1875 | | // |
1876 | | // WARNING: This function's return value cannot distinguish errors from -1. |
1877 | | // Use |ASN1_ENUMERATED_get_uint64| and |ASN1_ENUMERATED_get_int64| instead. |
1878 | | OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); |
1879 | | |
1880 | | |
1881 | | #if defined(__cplusplus) |
1882 | | } // extern C |
1883 | | |
1884 | | extern "C++" { |
1885 | | |
1886 | | BSSL_NAMESPACE_BEGIN |
1887 | | |
1888 | | BORINGSSL_MAKE_DELETER(ASN1_OBJECT, ASN1_OBJECT_free) |
1889 | | BORINGSSL_MAKE_DELETER(ASN1_STRING, ASN1_STRING_free) |
1890 | | BORINGSSL_MAKE_DELETER(ASN1_TYPE, ASN1_TYPE_free) |
1891 | | |
1892 | | BSSL_NAMESPACE_END |
1893 | | |
1894 | | } // extern C++ |
1895 | | |
1896 | | #endif |
1897 | | |
1898 | | #define ASN1_R_ASN1_LENGTH_MISMATCH 100 |
1899 | | #define ASN1_R_AUX_ERROR 101 |
1900 | | #define ASN1_R_BAD_GET_ASN1_OBJECT_CALL 102 |
1901 | | #define ASN1_R_BAD_OBJECT_HEADER 103 |
1902 | | #define ASN1_R_BMPSTRING_IS_WRONG_LENGTH 104 |
1903 | | #define ASN1_R_BN_LIB 105 |
1904 | | #define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 106 |
1905 | | #define ASN1_R_BUFFER_TOO_SMALL 107 |
1906 | | #define ASN1_R_CONTEXT_NOT_INITIALISED 108 |
1907 | | #define ASN1_R_DECODE_ERROR 109 |
1908 | | #define ASN1_R_DEPTH_EXCEEDED 110 |
1909 | | #define ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 111 |
1910 | | #define ASN1_R_ENCODE_ERROR 112 |
1911 | | #define ASN1_R_ERROR_GETTING_TIME 113 |
1912 | | #define ASN1_R_EXPECTING_AN_ASN1_SEQUENCE 114 |
1913 | | #define ASN1_R_EXPECTING_AN_INTEGER 115 |
1914 | | #define ASN1_R_EXPECTING_AN_OBJECT 116 |
1915 | | #define ASN1_R_EXPECTING_A_BOOLEAN 117 |
1916 | | #define ASN1_R_EXPECTING_A_TIME 118 |
1917 | | #define ASN1_R_EXPLICIT_LENGTH_MISMATCH 119 |
1918 | | #define ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED 120 |
1919 | | #define ASN1_R_FIELD_MISSING 121 |
1920 | | #define ASN1_R_FIRST_NUM_TOO_LARGE 122 |
1921 | | #define ASN1_R_HEADER_TOO_LONG 123 |
1922 | | #define ASN1_R_ILLEGAL_BITSTRING_FORMAT 124 |
1923 | | #define ASN1_R_ILLEGAL_BOOLEAN 125 |
1924 | | #define ASN1_R_ILLEGAL_CHARACTERS 126 |
1925 | | #define ASN1_R_ILLEGAL_FORMAT 127 |
1926 | | #define ASN1_R_ILLEGAL_HEX 128 |
1927 | | #define ASN1_R_ILLEGAL_IMPLICIT_TAG 129 |
1928 | | #define ASN1_R_ILLEGAL_INTEGER 130 |
1929 | | #define ASN1_R_ILLEGAL_NESTED_TAGGING 131 |
1930 | | #define ASN1_R_ILLEGAL_NULL 132 |
1931 | | #define ASN1_R_ILLEGAL_NULL_VALUE 133 |
1932 | | #define ASN1_R_ILLEGAL_OBJECT 134 |
1933 | | #define ASN1_R_ILLEGAL_OPTIONAL_ANY 135 |
1934 | | #define ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE 136 |
1935 | | #define ASN1_R_ILLEGAL_TAGGED_ANY 137 |
1936 | | #define ASN1_R_ILLEGAL_TIME_VALUE 138 |
1937 | | #define ASN1_R_INTEGER_NOT_ASCII_FORMAT 139 |
1938 | | #define ASN1_R_INTEGER_TOO_LARGE_FOR_LONG 140 |
1939 | | #define ASN1_R_INVALID_BIT_STRING_BITS_LEFT 141 |
1940 | 1.78k | #define ASN1_R_INVALID_BMPSTRING 142 |
1941 | | #define ASN1_R_INVALID_DIGIT 143 |
1942 | | #define ASN1_R_INVALID_MODIFIER 144 |
1943 | | #define ASN1_R_INVALID_NUMBER 145 |
1944 | | #define ASN1_R_INVALID_OBJECT_ENCODING 146 |
1945 | | #define ASN1_R_INVALID_SEPARATOR 147 |
1946 | | #define ASN1_R_INVALID_TIME_FORMAT 148 |
1947 | 891 | #define ASN1_R_INVALID_UNIVERSALSTRING 149 |
1948 | 2.33k | #define ASN1_R_INVALID_UTF8STRING 150 |
1949 | | #define ASN1_R_LIST_ERROR 151 |
1950 | | #define ASN1_R_MISSING_ASN1_EOS 152 |
1951 | | #define ASN1_R_MISSING_EOC 153 |
1952 | | #define ASN1_R_MISSING_SECOND_NUMBER 154 |
1953 | | #define ASN1_R_MISSING_VALUE 155 |
1954 | | #define ASN1_R_MSTRING_NOT_UNIVERSAL 156 |
1955 | | #define ASN1_R_MSTRING_WRONG_TAG 157 |
1956 | | #define ASN1_R_NESTED_ASN1_ERROR 158 |
1957 | | #define ASN1_R_NESTED_ASN1_STRING 159 |
1958 | | #define ASN1_R_NON_HEX_CHARACTERS 160 |
1959 | | #define ASN1_R_NOT_ASCII_FORMAT 161 |
1960 | | #define ASN1_R_NOT_ENOUGH_DATA 162 |
1961 | | #define ASN1_R_NO_MATCHING_CHOICE_TYPE 163 |
1962 | | #define ASN1_R_NULL_IS_WRONG_LENGTH 164 |
1963 | | #define ASN1_R_OBJECT_NOT_ASCII_FORMAT 165 |
1964 | | #define ASN1_R_ODD_NUMBER_OF_CHARS 166 |
1965 | | #define ASN1_R_SECOND_NUMBER_TOO_LARGE 167 |
1966 | | #define ASN1_R_SEQUENCE_LENGTH_MISMATCH 168 |
1967 | | #define ASN1_R_SEQUENCE_NOT_CONSTRUCTED 169 |
1968 | | #define ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG 170 |
1969 | | #define ASN1_R_SHORT_LINE 171 |
1970 | | #define ASN1_R_STREAMING_NOT_SUPPORTED 172 |
1971 | | #define ASN1_R_STRING_TOO_LONG 173 |
1972 | | #define ASN1_R_STRING_TOO_SHORT 174 |
1973 | | #define ASN1_R_TAG_VALUE_TOO_HIGH 175 |
1974 | | #define ASN1_R_TIME_NOT_ASCII_FORMAT 176 |
1975 | | #define ASN1_R_TOO_LONG 177 |
1976 | | #define ASN1_R_TYPE_NOT_CONSTRUCTED 178 |
1977 | | #define ASN1_R_TYPE_NOT_PRIMITIVE 179 |
1978 | | #define ASN1_R_UNEXPECTED_EOC 180 |
1979 | | #define ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH 181 |
1980 | | #define ASN1_R_UNKNOWN_FORMAT 182 |
1981 | | #define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 183 |
1982 | | #define ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM 184 |
1983 | | #define ASN1_R_UNKNOWN_TAG 185 |
1984 | | #define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE 186 |
1985 | | #define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 187 |
1986 | | #define ASN1_R_UNSUPPORTED_TYPE 188 |
1987 | | #define ASN1_R_WRONG_PUBLIC_KEY_TYPE 189 |
1988 | | #define ASN1_R_WRONG_TAG 190 |
1989 | | #define ASN1_R_WRONG_TYPE 191 |
1990 | | #define ASN1_R_NESTED_TOO_DEEP 192 |
1991 | | #define ASN1_R_BAD_TEMPLATE 193 |
1992 | | #define ASN1_R_INVALID_BIT_STRING_PADDING 194 |
1993 | | #define ASN1_R_WRONG_INTEGER_TYPE 195 |
1994 | | #define ASN1_R_INVALID_INTEGER 196 |
1995 | | |
1996 | | #endif // OPENSSL_HEADER_ASN1_H |