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