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