]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/tasn_utl.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / asn1 / tasn_utl.c
CommitLineData
0f113f3e 1/*
28428130 2 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
9d6b1ce6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
9d6b1ce6
DSH
8 */
9
9d6b1ce6 10#include <stddef.h>
3ebac273 11#include <string.h>
5f8dd0f8 12#include "internal/cryptlib.h"
cd420b0b 13#include "internal/refcount.h"
9d6b1ce6
DSH
14#include <openssl/asn1.h>
15#include <openssl/asn1t.h>
16#include <openssl/objects.h>
17#include <openssl/err.h>
706457b7 18#include "asn1_local.h"
9d6b1ce6
DSH
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
0f113f3e
MC
25/*
26 * Given an ASN1_ITEM CHOICE type return the selector value
9d6b1ce6
DSH
27 */
28
29int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e
MC
30{
31 int *sel = offset2ptr(*pval, it->utype);
9fdcc21f
DO
32
33 return *sel;
34}
35
36int asn1_get_choice_selector_const(const ASN1_VALUE **pval, const ASN1_ITEM *it)
37{
38 int *sel = offset2ptr(*pval, it->utype);
39
0f113f3e
MC
40 return *sel;
41}
9d6b1ce6 42
0f113f3e
MC
43/*
44 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
9d6b1ce6
DSH
45 */
46
0f113f3e
MC
47int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
48 const ASN1_ITEM *it)
49{
50 int *sel, ret;
9fdcc21f 51
0f113f3e
MC
52 sel = offset2ptr(*pval, it->utype);
53 ret = *sel;
54 *sel = value;
55 return ret;
56}
9d6b1ce6 57
0f113f3e 58/*
687b4868
F
59 * Do atomic reference counting. The value 'op' decides what to do.
60 * If it is +1 then the count is incremented.
61 * If |op| is 0, lock is initialised and count is set to 1.
62 * If |op| is -1, count is decremented and the return value is the current
63 * reference count or 0 if no reference count is active.
64 * It returns -1 on initialisation error.
65 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
9d6b1ce6 66 */
9d6b1ce6 67int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
0f113f3e
MC
68{
69 const ASN1_AUX *aux;
680b9d45 70 CRYPTO_REF_COUNT *lck;
c001ce33 71 CRYPTO_RWLOCK **lock;
680b9d45
AP
72 int ret = -1;
73
0f113f3e
MC
74 if ((it->itype != ASN1_ITYPE_SEQUENCE)
75 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
76 return 0;
77 aux = it->funcs;
9fdcc21f 78 if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
0f113f3e
MC
79 return 0;
80 lck = offset2ptr(*pval, aux->ref_offset);
c001ce33 81 lock = offset2ptr(*pval, aux->ref_lock);
680b9d45
AP
82
83 switch (op) {
84 case 0:
85 *lck = ret = 1;
c001ce33 86 *lock = CRYPTO_THREAD_lock_new();
b2b361f6 87 if (*lock == NULL) {
687b4868
F
88 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
89 return -1;
b2b361f6 90 }
680b9d45
AP
91 break;
92 case 1:
93 if (!CRYPTO_UP_REF(lck, &ret, *lock))
94 return -1;
95 break;
96 case -1:
97 if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
98 return -1; /* failed */
9d6b1ce6 99#ifdef REF_PRINT
680b9d45 100 fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname);
9d6b1ce6 101#endif
680b9d45
AP
102 REF_ASSERT_ISNT(ret < 0);
103 if (ret == 0) {
104 CRYPTO_THREAD_lock_free(*lock);
105 *lock = NULL;
106 }
107 break;
687b4868 108 }
680b9d45 109
0f113f3e
MC
110 return ret;
111}
9d6b1ce6
DSH
112
113static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e
MC
114{
115 const ASN1_AUX *aux;
9fdcc21f
DO
116
117 if (pval == NULL || *pval == NULL)
0f113f3e
MC
118 return NULL;
119 aux = it->funcs;
9fdcc21f
DO
120 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
121 return NULL;
122 return offset2ptr(*pval, aux->enc_offset);
123}
124
125static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
126 const ASN1_ITEM *it)
127{
128 const ASN1_AUX *aux;
129
130 if (pval == NULL || *pval == NULL)
131 return NULL;
132 aux = it->funcs;
133 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
0f113f3e
MC
134 return NULL;
135 return offset2ptr(*pval, aux->enc_offset);
136}
9d6b1ce6
DSH
137
138void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e 139{
9fdcc21f
DO
140 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
141
142 if (enc != NULL) {
0f113f3e
MC
143 enc->enc = NULL;
144 enc->len = 0;
145 enc->modified = 1;
146 }
147}
9d6b1ce6
DSH
148
149void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e 150{
9fdcc21f
DO
151 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
152
153 if (enc != NULL) {
b548a1f1 154 OPENSSL_free(enc->enc);
0f113f3e
MC
155 enc->enc = NULL;
156 enc->len = 0;
157 enc->modified = 1;
158 }
159}
9d6b1ce6 160
f3f52d7f 161int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
0f113f3e
MC
162 const ASN1_ITEM *it)
163{
9fdcc21f
DO
164 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
165
166 if (enc == NULL)
0f113f3e
MC
167 return 1;
168
b548a1f1 169 OPENSSL_free(enc->enc);
cdb10bae
RS
170 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
171 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
0f113f3e 172 return 0;
cdb10bae 173 }
0f113f3e
MC
174 memcpy(enc->enc, in, inlen);
175 enc->len = inlen;
176 enc->modified = 0;
9d6b1ce6 177
0f113f3e
MC
178 return 1;
179}
9d6b1ce6 180
9fdcc21f 181int asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
0f113f3e
MC
182 const ASN1_ITEM *it)
183{
9fdcc21f
DO
184 const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
185
186 if (enc == NULL || enc->modified)
0f113f3e
MC
187 return 0;
188 if (out) {
189 memcpy(*out, enc->enc, enc->len);
190 *out += enc->len;
191 }
9fdcc21f 192 if (len != NULL)
0f113f3e
MC
193 *len = enc->len;
194 return 1;
195}
9d6b1ce6
DSH
196
197/* Given an ASN1_TEMPLATE get a pointer to a field */
0f113f3e
MC
198ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
199{
9fdcc21f
DO
200 ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
201
0f113f3e
MC
202 /*
203 * NOTE for BOOLEAN types the field is just a plain int so we can't
204 * return int **, so settle for (int *).
205 */
206 return pvaltmp;
207}
9d6b1ce6 208
9fdcc21f
DO
209/* Given an ASN1_TEMPLATE get a const pointer to a field */
210const ASN1_VALUE **asn1_get_const_field_ptr(const ASN1_VALUE **pval,
211 const ASN1_TEMPLATE *tt)
212{
213 return offset2ptr(*pval, tt->offset);
214}
215
0f113f3e
MC
216/*
217 * Handle ANY DEFINED BY template, find the selector, look up the relevant
218 * ASN1_TEMPLATE in the table and return it.
9d6b1ce6
DSH
219 */
220
9fdcc21f
DO
221const ASN1_TEMPLATE *asn1_do_adb(const ASN1_VALUE *val,
222 const ASN1_TEMPLATE *tt,
0f113f3e
MC
223 int nullerr)
224{
225 const ASN1_ADB *adb;
226 const ASN1_ADB_TABLE *atbl;
227 long selector;
9fdcc21f 228 const ASN1_VALUE **sfld;
0f113f3e 229 int i;
9fdcc21f
DO
230
231 if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
0f113f3e
MC
232 return tt;
233
234 /* Else ANY DEFINED BY ... get the table */
235 adb = ASN1_ADB_ptr(tt->item);
9d6b1ce6 236
0f113f3e 237 /* Get the selector field */
9fdcc21f 238 sfld = offset2ptr(val, adb->offset);
9d6b1ce6 239
0f113f3e 240 /* Check if NULL */
7c46746b 241 if (*sfld == NULL) {
9fdcc21f 242 if (adb->null_tt == NULL)
0f113f3e
MC
243 goto err;
244 return adb->null_tt;
245 }
9d6b1ce6 246
0f113f3e
MC
247 /*
248 * Convert type to a long: NB: don't check for NID_undef here because it
249 * might be a legitimate value in the table
250 */
9fdcc21f 251 if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
0f113f3e
MC
252 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
253 else
254 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
9d6b1ce6 255
5b70372d
DSH
256 /* Let application callback translate value */
257 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
258 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
259 return NULL;
260 }
261
0f113f3e
MC
262 /*
263 * Try to find matching entry in table Maybe should check application
264 * types first to allow application override? Might also be useful to
265 * have a flag which indicates table is sorted and we can do a binary
266 * search. For now stick to a linear search.
267 */
9d6b1ce6 268
0f113f3e
MC
269 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
270 if (atbl->value == selector)
271 return &atbl->tt;
9d6b1ce6 272
0f113f3e 273 /* FIXME: need to search application table too */
9d6b1ce6 274
0f113f3e
MC
275 /* No match, return default type */
276 if (!adb->default_tt)
277 goto err;
278 return adb->default_tt;
9d6b1ce6 279
0f113f3e
MC
280 err:
281 /* FIXME: should log the value or OID of unsupported type */
282 if (nullerr)
283 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
284 return NULL;
285}