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