• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 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 #ifndef OPENSSL_HEADER_ASN1_ASN1_LOCL_H
60 #define OPENSSL_HEADER_ASN1_ASN1_LOCL_H
61 
62 #include <time.h>
63 
64 #include <openssl/asn1.h>
65 #include <openssl/asn1t.h>
66 
67 #if defined(__cplusplus)
68 extern "C" {
69 #endif
70 
71 
72 /* Wrapper functions for time functions. */
73 
74 /* OPENSSL_gmtime wraps |gmtime_r|. See the manual page for that function. */
75 struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result);
76 
77 /* OPENSSL_gmtime_adj updates |tm| by adding |offset_day| days and |offset_sec|
78  * seconds. */
79 int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
80 
81 /* OPENSSL_gmtime_diff calculates the difference between |from| and |to| and
82  * outputs the difference as a number of days and seconds in |*out_days| and
83  * |*out_secs|. */
84 int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
85                         const struct tm *to);
86 
87 
88 /* Internal ASN1 structures and functions: not for application use */
89 
90 /* These are used internally in the ASN1_OBJECT to keep track of
91  * whether the names and data need to be free()ed */
92 #define ASN1_OBJECT_FLAG_DYNAMIC 0x01         /* internal use */
93 #define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
94 #define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08    /* internal use */
95 
96 /* An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
97  * Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
98  * library maintains a table of static |ASN1_OBJECT|s, which may be referenced
99  * by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
100  * pointer externally must assume it is immutable, even if the pointer is not
101  * const. */
102 struct asn1_object_st {
103   const char *sn, *ln;
104   int nid;
105   int length;
106   const unsigned char *data; /* data remains const after init */
107   int flags;                 /* Should we free this one */
108 };
109 
110 ASN1_OBJECT *ASN1_OBJECT_new(void);
111 
112 // ASN1_ENCODING structure: this is used to save the received
113 // encoding of an ASN1 type. This is useful to get round
114 // problems with invalid encodings which can break signatures.
115 typedef struct ASN1_ENCODING_st {
116   unsigned char *enc;  // DER encoding
117   long len;            // Length of encoding
118   int modified;        // set to 1 if 'enc' is invalid
119   // alias_only is zero if |enc| owns the buffer that it points to
120   // (although |enc| may still be NULL). If one, |enc| points into a
121   // buffer that is owned elsewhere.
122   unsigned alias_only : 1;
123   // alias_only_on_next_parse is one iff the next parsing operation
124   // should avoid taking a copy of the input and rather set
125   // |alias_only|.
126   unsigned alias_only_on_next_parse : 1;
127 } ASN1_ENCODING;
128 
129 int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
130 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d);
131 
132 void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
133                             int combine);
134 
135 int UTF8_getc(const unsigned char *str, int len, uint32_t *val);
136 int UTF8_putc(unsigned char *str, int len, uint32_t value);
137 
138 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
139 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
140 
141 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
142 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
143                      const ASN1_ITEM *it, int tag, int aclass, char opt,
144                      ASN1_TLC *ctx);
145 
146 /* ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
147  * i2d output convention. It returns a non-zero length on success and -1 on
148  * error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
149  * number is |tag| and the class is |aclass|. This is used for implicit tagging.
150  * This function treats a missing value as an error, not an optional field. */
151 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
152                      const ASN1_ITEM *it, int tag, int aclass);
153 
154 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
155 
156 /* asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
157  * must of type |it|. */
158 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
159 
160 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
161 
162 /* asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
163  * |tt|. */
164 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
165 
166 /* asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
167  * based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
168  * it returns |tt|. If the selector does not match any value, it returns NULL.
169  * If |nullerr| is non-zero, it will additionally push an error to the error
170  * queue when there is no match. */
171 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
172                                  int nullerr);
173 
174 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
175 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
176 
177 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
178 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
179 
180 /* asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
181  * the i2d output convention, sets |*len| to the length, and returns one. If it
182  * has no saved encoding, it returns zero. */
183 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
184                      const ASN1_ITEM *it);
185 
186 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
187                   const ASN1_ITEM *it);
188 
189 /* asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
190  * usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
191  * a pointer. */
192 const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
193 
194 /* asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
195  * ASN.1 PrintableString, and zero otherwise. */
196 int asn1_is_printable(uint32_t value);
197 
198 /* asn1_bit_string_length returns the number of bytes in |str| and sets
199  * |*out_padding_bits| to the number of padding bits.
200  *
201  * This function should be used instead of |ASN1_STRING_length| to correctly
202  * handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case. */
203 int asn1_bit_string_length(const ASN1_BIT_STRING *str,
204                            uint8_t *out_padding_bits);
205 
206 typedef struct {
207   int nid;
208   long minsize;
209   long maxsize;
210   unsigned long mask;
211   unsigned long flags;
212 } ASN1_STRING_TABLE;
213 
214 /* asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
215  * of built-in |ASN1_STRING_TABLE| values. It is exported for testing. */
216 OPENSSL_EXPORT void asn1_get_string_table_for_testing(
217     const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
218 
219 
220 #if defined(__cplusplus)
221 }  /* extern C */
222 #endif
223 
224 #endif  /* OPENSSL_HEADER_ASN1_ASN1_LOCL_H */
225