]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/tasn_utl.c
Set error code on alloc failures
[thirdparty/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 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
137 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
138 return 0;
139 }
140 memcpy(enc->enc, in, inlen);
141 enc->len = inlen;
142 enc->modified = 0;
143
144 return 1;
145 }
146
147 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
148 const ASN1_ITEM *it)
149 {
150 ASN1_ENCODING *enc;
151 enc = asn1_get_enc_ptr(pval, it);
152 if (!enc || enc->modified)
153 return 0;
154 if (out) {
155 memcpy(*out, enc->enc, enc->len);
156 *out += enc->len;
157 }
158 if (len)
159 *len = enc->len;
160 return 1;
161 }
162
163 /* Given an ASN1_TEMPLATE get a pointer to a field */
164 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
165 {
166 ASN1_VALUE **pvaltmp;
167 pvaltmp = offset2ptr(*pval, tt->offset);
168 /*
169 * NOTE for BOOLEAN types the field is just a plain int so we can't
170 * return int **, so settle for (int *).
171 */
172 return pvaltmp;
173 }
174
175 /*
176 * Handle ANY DEFINED BY template, find the selector, look up the relevant
177 * ASN1_TEMPLATE in the table and return it.
178 */
179
180 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
181 int nullerr)
182 {
183 const ASN1_ADB *adb;
184 const ASN1_ADB_TABLE *atbl;
185 long selector;
186 ASN1_VALUE **sfld;
187 int i;
188 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
189 return tt;
190
191 /* Else ANY DEFINED BY ... get the table */
192 adb = ASN1_ADB_ptr(tt->item);
193
194 /* Get the selector field */
195 sfld = offset2ptr(*pval, adb->offset);
196
197 /* Check if NULL */
198 if (*sfld == NULL) {
199 if (!adb->null_tt)
200 goto err;
201 return adb->null_tt;
202 }
203
204 /*
205 * Convert type to a long: NB: don't check for NID_undef here because it
206 * might be a legitimate value in the table
207 */
208 if (tt->flags & ASN1_TFLG_ADB_OID)
209 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
210 else
211 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
212
213 /* Let application callback translate value */
214 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
215 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
216 return NULL;
217 }
218
219 /*
220 * Try to find matching entry in table Maybe should check application
221 * types first to allow application override? Might also be useful to
222 * have a flag which indicates table is sorted and we can do a binary
223 * search. For now stick to a linear search.
224 */
225
226 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
227 if (atbl->value == selector)
228 return &atbl->tt;
229
230 /* FIXME: need to search application table too */
231
232 /* No match, return default type */
233 if (!adb->default_tt)
234 goto err;
235 return adb->default_tt;
236
237 err:
238 /* FIXME: should log the value or OID of unsupported type */
239 if (nullerr)
240 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
241 return NULL;
242 }