f79d7d6b447cf2ed5d3eb19ebbbfedcd0c8495f3
[openssl.git] / crypto / asn1 / tasn_utl.c
1 /*
2  * Copyright 2000-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 #include <stddef.h>
11 #include <string.h>
12 #include <internal/cryptlib.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/objects.h>
16 #include <openssl/err.h>
17 #include "asn1_locl.h"
18
19 /* Utility functions for manipulating fields and offsets */
20
21 /* Add 'offset' to 'addr' */
22 #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
23
24 /*
25  * Given an ASN1_ITEM CHOICE type return the selector value
26  */
27
28 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
29 {
30     int *sel = offset2ptr(*pval, it->utype);
31     return *sel;
32 }
33
34 /*
35  * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
36  */
37
38 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
39                              const ASN1_ITEM *it)
40 {
41     int *sel, ret;
42     sel = offset2ptr(*pval, it->utype);
43     ret = *sel;
44     *sel = value;
45     return ret;
46 }
47
48 /*
49  * Do atomic reference counting. The value 'op' decides what to do.
50  * If it is +1 then the count is incremented.
51  * If |op| is 0, lock is initialised and count is set to 1.
52  * If |op| is -1, count is decremented and the return value is the current
53  * reference count or 0 if no reference count is active.
54  * It returns -1 on initialisation error.
55  * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
56  */
57 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
58 {
59     const ASN1_AUX *aux;
60     int *lck, ret;
61     CRYPTO_RWLOCK **lock;
62     if ((it->itype != ASN1_ITYPE_SEQUENCE)
63         && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
64         return 0;
65     aux = it->funcs;
66     if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
67         return 0;
68     lck = offset2ptr(*pval, aux->ref_offset);
69     lock = offset2ptr(*pval, aux->ref_lock);
70     if (op == 0) {
71         *lck = 1;
72         *lock = CRYPTO_THREAD_lock_new();
73         if (*lock == NULL) {
74             ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
75             return -1;
76         }
77         return 1;
78     }
79     if (CRYPTO_atomic_add(lck, op, &ret, *lock) < 0)
80         return -1;  /* failed */
81 #ifdef REF_PRINT
82     fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
83 #endif
84     REF_ASSERT_ISNT(ret < 0);
85     if (ret == 0) {
86         CRYPTO_THREAD_lock_free(*lock);
87         *lock = NULL;
88     }
89     return ret;
90 }
91
92 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
93 {
94     const ASN1_AUX *aux;
95     if (!pval || !*pval)
96         return NULL;
97     aux = it->funcs;
98     if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
99         return NULL;
100     return offset2ptr(*pval, aux->enc_offset);
101 }
102
103 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
104 {
105     ASN1_ENCODING *enc;
106     enc = asn1_get_enc_ptr(pval, it);
107     if (enc) {
108         enc->enc = NULL;
109         enc->len = 0;
110         enc->modified = 1;
111     }
112 }
113
114 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
115 {
116     ASN1_ENCODING *enc;
117     enc = asn1_get_enc_ptr(pval, it);
118     if (enc) {
119         OPENSSL_free(enc->enc);
120         enc->enc = NULL;
121         enc->len = 0;
122         enc->modified = 1;
123     }
124 }
125
126 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
127                   const ASN1_ITEM *it)
128 {
129     ASN1_ENCODING *enc;
130     enc = asn1_get_enc_ptr(pval, it);
131     if (!enc)
132         return 1;
133
134     OPENSSL_free(enc->enc);
135     enc->enc = OPENSSL_malloc(inlen);
136     if (enc->enc == NULL)
137         return 0;
138     memcpy(enc->enc, in, inlen);
139     enc->len = inlen;
140     enc->modified = 0;
141
142     return 1;
143 }
144
145 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
146                      const ASN1_ITEM *it)
147 {
148     ASN1_ENCODING *enc;
149     enc = asn1_get_enc_ptr(pval, it);
150     if (!enc || enc->modified)
151         return 0;
152     if (out) {
153         memcpy(*out, enc->enc, enc->len);
154         *out += enc->len;
155     }
156     if (len)
157         *len = enc->len;
158     return 1;
159 }
160
161 /* Given an ASN1_TEMPLATE get a pointer to a field */
162 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
163 {
164     ASN1_VALUE **pvaltmp;
165     pvaltmp = offset2ptr(*pval, tt->offset);
166     /*
167      * NOTE for BOOLEAN types the field is just a plain int so we can't
168      * return int **, so settle for (int *).
169      */
170     return pvaltmp;
171 }
172
173 /*
174  * Handle ANY DEFINED BY template, find the selector, look up the relevant
175  * ASN1_TEMPLATE in the table and return it.
176  */
177
178 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
179                                  int nullerr)
180 {
181     const ASN1_ADB *adb;
182     const ASN1_ADB_TABLE *atbl;
183     long selector;
184     ASN1_VALUE **sfld;
185     int i;
186     if (!(tt->flags & ASN1_TFLG_ADB_MASK))
187         return tt;
188
189     /* Else ANY DEFINED BY ... get the table */
190     adb = ASN1_ADB_ptr(tt->item);
191
192     /* Get the selector field */
193     sfld = offset2ptr(*pval, adb->offset);
194
195     /* Check if NULL */
196     if (*sfld == NULL) {
197         if (!adb->null_tt)
198             goto err;
199         return adb->null_tt;
200     }
201
202     /*
203      * Convert type to a long: NB: don't check for NID_undef here because it
204      * might be a legitimate value in the table
205      */
206     if (tt->flags & ASN1_TFLG_ADB_OID)
207         selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
208     else
209         selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
210
211     /* Let application callback translate value */
212     if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
213         ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
214         return NULL;
215     }
216
217     /*
218      * Try to find matching entry in table Maybe should check application
219      * types first to allow application override? Might also be useful to
220      * have a flag which indicates table is sorted and we can do a binary
221      * search. For now stick to a linear search.
222      */
223
224     for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
225         if (atbl->value == selector)
226             return &atbl->tt;
227
228     /* FIXME: need to search application table too */
229
230     /* No match, return default type */
231     if (!adb->default_tt)
232         goto err;
233     return adb->default_tt;
234
235  err:
236     /* FIXME: should log the value or OID of unsupported type */
237     if (nullerr)
238         ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
239     return NULL;
240 }