Fix omitted selector handling.
[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 reference counting. The value 'op' decides what to do. if it is +1
50  * then the count is incremented. If op is 0 count is set to 1. If op is -1
51  * count is decremented and the return value is the current reference count
52  * or 0 if no reference count exists.
53  * FIXME: return and manage any error from inside this method
54  */
55
56 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
57 {
58     const ASN1_AUX *aux;
59     int *lck, ret;
60     CRYPTO_RWLOCK **lock;
61     if ((it->itype != ASN1_ITYPE_SEQUENCE)
62         && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
63         return 0;
64     aux = it->funcs;
65     if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
66         return 0;
67     lck = offset2ptr(*pval, aux->ref_offset);
68     lock = offset2ptr(*pval, aux->ref_lock);
69     if (op == 0) {
70         *lck = 1;
71         *lock = CRYPTO_THREAD_lock_new();
72         if (*lock == NULL) {
73             /* FIXME: should report an error (-1) at this point */
74             return 0;
75         }
76         return 1;
77     }
78     CRYPTO_atomic_add(lck, op, &ret, *lock);
79 #ifdef REF_PRINT
80     fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
81 #endif
82     REF_ASSERT_ISNT(ret < 0);
83     if (ret == 0)
84         CRYPTO_THREAD_lock_free(*lock);
85     return ret;
86 }
87
88 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
89 {
90     const ASN1_AUX *aux;
91     if (!pval || !*pval)
92         return NULL;
93     aux = it->funcs;
94     if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
95         return NULL;
96     return offset2ptr(*pval, aux->enc_offset);
97 }
98
99 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
100 {
101     ASN1_ENCODING *enc;
102     enc = asn1_get_enc_ptr(pval, it);
103     if (enc) {
104         enc->enc = NULL;
105         enc->len = 0;
106         enc->modified = 1;
107     }
108 }
109
110 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
111 {
112     ASN1_ENCODING *enc;
113     enc = asn1_get_enc_ptr(pval, it);
114     if (enc) {
115         OPENSSL_free(enc->enc);
116         enc->enc = NULL;
117         enc->len = 0;
118         enc->modified = 1;
119     }
120 }
121
122 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
123                   const ASN1_ITEM *it)
124 {
125     ASN1_ENCODING *enc;
126     enc = asn1_get_enc_ptr(pval, it);
127     if (!enc)
128         return 1;
129
130     OPENSSL_free(enc->enc);
131     enc->enc = OPENSSL_malloc(inlen);
132     if (enc->enc == NULL)
133         return 0;
134     memcpy(enc->enc, in, inlen);
135     enc->len = inlen;
136     enc->modified = 0;
137
138     return 1;
139 }
140
141 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
142                      const ASN1_ITEM *it)
143 {
144     ASN1_ENCODING *enc;
145     enc = asn1_get_enc_ptr(pval, it);
146     if (!enc || enc->modified)
147         return 0;
148     if (out) {
149         memcpy(*out, enc->enc, enc->len);
150         *out += enc->len;
151     }
152     if (len)
153         *len = enc->len;
154     return 1;
155 }
156
157 /* Given an ASN1_TEMPLATE get a pointer to a field */
158 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
159 {
160     ASN1_VALUE **pvaltmp;
161     pvaltmp = offset2ptr(*pval, tt->offset);
162     /*
163      * NOTE for BOOLEAN types the field is just a plain int so we can't
164      * return int **, so settle for (int *).
165      */
166     return pvaltmp;
167 }
168
169 /*
170  * Handle ANY DEFINED BY template, find the selector, look up the relevant
171  * ASN1_TEMPLATE in the table and return it.
172  */
173
174 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
175                                  int nullerr)
176 {
177     const ASN1_ADB *adb;
178     const ASN1_ADB_TABLE *atbl;
179     long selector;
180     ASN1_VALUE **sfld;
181     int i;
182     if (!(tt->flags & ASN1_TFLG_ADB_MASK))
183         return tt;
184
185     /* Else ANY DEFINED BY ... get the table */
186     adb = ASN1_ADB_ptr(tt->item);
187
188     /* Get the selector field */
189     sfld = offset2ptr(*pval, adb->offset);
190
191     /* Check if NULL */
192     if (*sfld == NULL) {
193         if (!adb->null_tt)
194             goto err;
195         return adb->null_tt;
196     }
197
198     /*
199      * Convert type to a long: NB: don't check for NID_undef here because it
200      * might be a legitimate value in the table
201      */
202     if (tt->flags & ASN1_TFLG_ADB_OID)
203         selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
204     else
205         selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
206
207     /* Let application callback translate value */
208     if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
209         ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
210         return NULL;
211     }
212
213     /*
214      * Try to find matching entry in table Maybe should check application
215      * types first to allow application override? Might also be useful to
216      * have a flag which indicates table is sorted and we can do a binary
217      * search. For now stick to a linear search.
218      */
219
220     for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
221         if (atbl->value == selector)
222             return &atbl->tt;
223
224     /* FIXME: need to search application table too */
225
226     /* No match, return default type */
227     if (!adb->default_tt)
228         goto err;
229     return adb->default_tt;
230
231  err:
232     /* FIXME: should log the value or OID of unsupported type */
233     if (nullerr)
234         ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
235     return NULL;
236 }