xref: /openbsd/lib/libcrypto/asn1/asn1_local.h (revision 27e5e86b)
1 /* $OpenBSD: asn1_local.h,v 1.10 2024/03/02 09:10:42 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include "bytestring.h"
60 
61 __BEGIN_HIDDEN_DECLS
62 
63 /* Internal ASN1 structures and functions: not for application use */
64 
65 ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t);
66 void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t);
67 
68 /* These are used internally in the ASN1_OBJECT to keep track of
69  * whether the names and data need to be free()ed */
70 #define ASN1_OBJECT_FLAG_DYNAMIC	 0x01	/* internal use */
71 #define ASN1_OBJECT_FLAG_CRITICAL	 0x02	/* critical x509v3 object id */
72 #define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04	/* internal use */
73 #define ASN1_OBJECT_FLAG_DYNAMIC_DATA	 0x08	/* internal use */
74 struct asn1_object_st {
75 	const char *sn, *ln;
76 	int nid;
77 	int length;
78 	const unsigned char *data;	/* data remains const after init */
79 	int flags;	/* Should we free this one */
80 } /* ASN1_OBJECT */;
81 
82 /* ASN1 print context structure */
83 
84 struct asn1_pctx_st {
85 	unsigned long flags;
86 	unsigned long nm_flags;
87 	unsigned long cert_flags;
88 	unsigned long oid_flags;
89 	unsigned long str_flags;
90 } /* ASN1_PCTX */;
91 
92 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
93 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
94 
95 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
96 
97 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
98 
99 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
100 
101 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
102 void asn1_enc_cleanup(ASN1_VALUE **pval, const ASN1_ITEM *it);
103 int asn1_enc_save(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it);
104 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
105 
106 int i2d_ASN1_BOOLEAN(int a, unsigned char **pp);
107 int d2i_ASN1_BOOLEAN(int *a, const unsigned char **pp, long length);
108 
109 /*
110  * Unicode codepoint constants
111  */
112 #define	UNICODE_MAX		0x10FFFF
113 #define	UNICODE_SURROGATE_MIN	0x00D800
114 #define	UNICODE_SURROGATE_MAX	0x00DFFF
115 
116 #define	UNICODE_IS_SURROGATE(x) \
117 	((x) >= UNICODE_SURROGATE_MIN && (x) <= UNICODE_SURROGATE_MAX)
118 
119 int UTF8_getc(const unsigned char *str, int len, unsigned long *val);
120 int UTF8_putc(unsigned char *str, int len, unsigned long value);
121 
122 int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
123 
124 int asn1_get_identifier_cbs(CBS *cbs, int der_mode, uint8_t *out_class,
125     int *out_constructed, uint32_t *out_tag_number);
126 int asn1_get_length_cbs(CBS *cbs, int der_mode, int *out_indefinite,
127     size_t *out_length);
128 int asn1_get_object_cbs(CBS *cbs, int der_mode, uint8_t *out_class,
129     int *out_constructed, uint32_t *out_tag_number, int *out_indefinite,
130     size_t *out_length);
131 int asn1_get_primitive(CBS *cbs, int der_mode, uint32_t *out_tag_number,
132     CBS *out_content);
133 
134 int asn1_must_be_constructed(int tag);
135 int asn1_must_be_primitive(int tag);
136 int asn1_tag2charwidth(int tag);
137 
138 int asn1_abs_set_unused_bits(ASN1_BIT_STRING *abs, uint8_t unused_bits);
139 int c2i_ASN1_BIT_STRING_cbs(ASN1_BIT_STRING **out_abs, CBS *cbs);
140 
141 int c2i_ASN1_ENUMERATED_cbs(ASN1_ENUMERATED **out_aenum, CBS *cbs);
142 
143 int asn1_aint_get_uint64(CBS *cbs, uint64_t *out_val);
144 int asn1_aint_set_uint64(uint64_t val, uint8_t **out_data, int *out_len);
145 int asn1_aint_get_int64(CBS *cbs, int negative, int64_t *out_val);
146 int c2i_ASN1_INTEGER_cbs(ASN1_INTEGER **out_aint, CBS *cbs);
147 
148 int c2i_ASN1_OBJECT_cbs(ASN1_OBJECT **out_aobj, CBS *content);
149 int i2t_ASN1_OBJECT_internal(const ASN1_OBJECT *aobj, char *buf, int buf_len,
150     int no_name);
151 ASN1_OBJECT *t2i_ASN1_OBJECT_internal(const char *oid);
152 
153 int asn1_time_parse_cbs(const CBS *cbs, int is_gentime, struct tm *out_tm);
154 
155 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
156     long length);
157 int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp);
158 ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
159     const unsigned char **pp, long length);
160 int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp);
161 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
162     long length);
163 int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, int64_t offset_sec);
164 int OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
165     const struct tm *to);
166 int asn1_time_time_t_to_tm(const time_t *time, struct tm *out_tm);
167 int asn1_time_tm_to_time_t(const struct tm *tm, time_t *out);
168 
169 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it);
170 
171 const BIO_METHOD *BIO_f_asn1(void);
172 
173 BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it);
174 
175 int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
176     const ASN1_ITEM *it);
177 int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
178     const char *hdr, const ASN1_ITEM *it);
179 int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
180     int ctype_nid, int econt_nid, STACK_OF(X509_ALGOR) *mdalgs,
181     const ASN1_ITEM *it);
182 ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it);
183 
184 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
185 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
186 
187 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
188 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
189 
190 int ASN1_time_parse(const char *_bytes, size_t _len, struct tm *_tm, int _mode);
191 int ASN1_time_tm_cmp(struct tm *_tm1, struct tm *_tm2);
192 
193 __END_HIDDEN_DECLS
194