Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/asn1.h

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