• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OPENSSL_HEADER_ASN1_INTERNAL_H
11 #define OPENSSL_HEADER_ASN1_INTERNAL_H
12 
13 #include <time.h>
14 
15 #include <openssl/asn1.h>
16 #include <openssl/asn1t.h>
17 
18 #if defined(__cplusplus)
19 extern "C" {
20 #endif
21 
22 
23 // Wrapper functions for time functions.
24 
25 // OPENSSL_gmtime converts a time_t value in |time| which must be in the range
26 // of year 0000 to 9999 to a broken out time value in |tm|. On success |tm| is
27 // returned. On failure NULL is returned.
28 OPENSSL_EXPORT struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result);
29 
30 // OPENSSL_gmtime_adj returns one on success, and updates |tm| by adding
31 // |offset_day| days and |offset_sec| seconds. It returns zero on failure. |tm|
32 // must be in the range of year 0000 to 9999 both before and after the update or
33 // a failure will be returned.
34 OPENSSL_EXPORT int OPENSSL_gmtime_adj(struct tm *tm, int offset_day,
35                                       int64_t offset_sec);
36 
37 // OPENSSL_gmtime_diff calculates the difference between |from| and |to|. It
38 // returns one, and outputs the difference as a number of days and seconds in
39 // |*out_days| and |*out_secs| on success. It returns zero on failure.  Both
40 // |from| and |to| must be in the range of year 0000 to 9999 or a failure will
41 // be returned.
42 OPENSSL_EXPORT int OPENSSL_gmtime_diff(int *out_days, int *out_secs,
43                                        const struct tm *from,
44                                        const struct tm *to);
45 
46 // Internal ASN1 structures and functions: not for application use
47 
48 // These are used internally in the ASN1_OBJECT to keep track of
49 // whether the names and data need to be free()ed
50 #define ASN1_OBJECT_FLAG_DYNAMIC 0x01          // internal use
51 #define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04  // internal use
52 #define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08     // internal use
53 
54 // An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
55 // Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
56 // library maintains a table of static |ASN1_OBJECT|s, which may be referenced
57 // by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
58 // pointer externally must assume it is immutable, even if the pointer is not
59 // const.
60 struct asn1_object_st {
61   const char *sn, *ln;
62   int nid;
63   int length;
64   const unsigned char *data;  // data remains const after init
65   int flags;                  // Should we free this one
66 };
67 
68 ASN1_OBJECT *ASN1_OBJECT_new(void);
69 
70 // ASN1_ENCODING is used to save the received encoding of an ASN.1 type. This
71 // avoids problems with invalid encodings that break signatures.
72 typedef struct ASN1_ENCODING_st {
73   // enc is the saved DER encoding. Its ownership is determined by |buf|.
74   uint8_t *enc;
75   // len is the length of |enc|. If zero, there is no saved encoding.
76   size_t len;
77   // buf, if non-NULL, is the |CRYPTO_BUFFER| that |enc| points into. If NULL,
78   // |enc| must be released with |OPENSSL_free|.
79   CRYPTO_BUFFER *buf;
80 } ASN1_ENCODING;
81 
82 OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
83                                       int allow_timezone_offset);
84 OPENSSL_EXPORT int asn1_generalizedtime_to_tm(struct tm *tm,
85                                               const ASN1_GENERALIZEDTIME *d);
86 
87 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
88 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
89 
90 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
91 
92 // ASN1_item_ex_d2i parses |len| bytes from |*in| as a structure of type |it|
93 // and writes the result to |*pval|. If |tag| is non-negative, |it| is
94 // implicitly tagged with the tag specified by |tag| and |aclass|. If |opt| is
95 // non-zero, the value is optional. If |buf| is non-NULL, |*in| must point into
96 // |buf|.
97 //
98 // This function returns one and advances |*in| if an object was successfully
99 // parsed, -1 if an optional value was successfully skipped, and zero on error.
100 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
101                      const ASN1_ITEM *it, int tag, int aclass, char opt,
102                      CRYPTO_BUFFER *buf);
103 
104 // ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
105 // i2d output convention. It returns a non-zero length on success and -1 on
106 // error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
107 // number is |tag| and the class is |aclass|. This is used for implicit tagging.
108 // This function treats a missing value as an error, not an optional field.
109 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
110                      const ASN1_ITEM *it, int tag, int aclass);
111 
112 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
113 
114 // asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
115 // must of type |it|.
116 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
117 
118 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
119 
120 // asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
121 // |tt|.
122 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
123 
124 // asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
125 // based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
126 // it returns |tt|. If the selector does not match any value, it returns NULL.
127 // If |nullerr| is non-zero, it will additionally push an error to the error
128 // queue when there is no match.
129 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
130                                  int nullerr);
131 
132 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
133 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
134 
135 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
136 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
137 
138 // asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
139 // the i2d output convention, sets |*len| to the length, and returns one. If it
140 // has no saved encoding, it returns zero.
141 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
142                      const ASN1_ITEM *it);
143 
144 // asn1_enc_save saves |inlen| bytes from |in| as |*pval|'s saved encoding. It
145 // returns one on success and zero on error. If |buf| is non-NULL, |in| must
146 // point into |buf|.
147 int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t inlen,
148                   const ASN1_ITEM *it, CRYPTO_BUFFER *buf);
149 
150 // asn1_encoding_clear clears the cached encoding in |enc|.
151 void asn1_encoding_clear(ASN1_ENCODING *enc);
152 
153 // asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
154 // usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
155 // a pointer.
156 const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
157 
158 // asn1_type_set0_string sets |a|'s value to the object represented by |str| and
159 // takes ownership of |str|.
160 void asn1_type_set0_string(ASN1_TYPE *a, ASN1_STRING *str);
161 
162 // asn1_type_cleanup releases memory associated with |a|'s value, without
163 // freeing |a| itself.
164 void asn1_type_cleanup(ASN1_TYPE *a);
165 
166 // asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
167 // ASN.1 PrintableString, and zero otherwise.
168 int asn1_is_printable(uint32_t value);
169 
170 // asn1_bit_string_length returns the number of bytes in |str| and sets
171 // |*out_padding_bits| to the number of padding bits.
172 //
173 // This function should be used instead of |ASN1_STRING_length| to correctly
174 // handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case.
175 int asn1_bit_string_length(const ASN1_BIT_STRING *str,
176                            uint8_t *out_padding_bits);
177 
178 typedef struct {
179   int nid;
180   long minsize;
181   long maxsize;
182   unsigned long mask;
183   unsigned long flags;
184 } ASN1_STRING_TABLE;
185 
186 // asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
187 // of built-in |ASN1_STRING_TABLE| values. It is exported for testing.
188 OPENSSL_EXPORT void asn1_get_string_table_for_testing(
189     const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
190 
191 typedef ASN1_VALUE *ASN1_new_func(void);
192 typedef void ASN1_free_func(ASN1_VALUE *a);
193 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
194                                   long length);
195 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
196 
197 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
198                         const ASN1_ITEM *it, int opt, ASN1_TLC *ctx);
199 
200 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
201                         const ASN1_ITEM *it);
202 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
203 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
204 
205 typedef struct ASN1_EXTERN_FUNCS_st {
206   ASN1_ex_new_func *asn1_ex_new;
207   ASN1_ex_free_func *asn1_ex_free;
208   ASN1_ex_d2i *asn1_ex_d2i;
209   ASN1_ex_i2d *asn1_ex_i2d;
210 } ASN1_EXTERN_FUNCS;
211 
212 
213 #if defined(__cplusplus)
214 }  // extern C
215 #endif
216 
217 #endif  // OPENSSL_HEADER_ASN1_INTERNAL_H
218