blob: f79d7d6b447cf2ed5d3eb19ebbbfedcd0c8495f3 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz2039c422016-05-17 14:51:34 -04002 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +00003 *
Rich Salz2039c422016-05-17 14:51:34 -04004 * 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
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +00008 */
9
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000010#include <stddef.h>
Richard Levitte3ebac272001-02-20 12:43:11 +000011#include <string.h>
David Woodhouse984d6c62015-09-11 14:56:32 -040012#include <internal/cryptlib.h>
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000013#include <openssl/asn1.h>
14#include <openssl/asn1t.h>
15#include <openssl/objects.h>
16#include <openssl/err.h>
Dr. Stephen Hensonc1ee50a2015-03-25 15:42:56 +000017#include "asn1_locl.h"
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000018
19/* Utility functions for manipulating fields and offsets */
20
21/* Add 'offset' to 'addr' */
22#define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
23
Matt Caswell0f113f32015-01-22 03:40:55 +000024/*
25 * Given an ASN1_ITEM CHOICE type return the selector value
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000026 */
27
28int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
Matt Caswell0f113f32015-01-22 03:40:55 +000029{
30 int *sel = offset2ptr(*pval, it->utype);
31 return *sel;
32}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000033
Matt Caswell0f113f32015-01-22 03:40:55 +000034/*
35 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000036 */
37
Matt Caswell0f113f32015-01-22 03:40:55 +000038int 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}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000047
Matt Caswell0f113f32015-01-22 03:40:55 +000048/*
FdaSilvaYY687b4862016-05-29 14:32:23 +020049 * Do atomic reference counting. The value 'op' decides what to do.
50 * If it is +1 then the count is incremented.
51 * If |op| is 0, lock is initialised and count is set to 1.
52 * If |op| is -1, count is decremented and the return value is the current
53 * reference count or 0 if no reference count is active.
54 * It returns -1 on initialisation error.
55 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000056 */
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000057int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
Matt Caswell0f113f32015-01-22 03:40:55 +000058{
59 const ASN1_AUX *aux;
60 int *lck, ret;
Alessandro Ghedinic001ce32016-03-01 18:06:15 +000061 CRYPTO_RWLOCK **lock;
Matt Caswell0f113f32015-01-22 03:40:55 +000062 if ((it->itype != ASN1_ITYPE_SEQUENCE)
63 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
64 return 0;
65 aux = it->funcs;
66 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
67 return 0;
68 lck = offset2ptr(*pval, aux->ref_offset);
Alessandro Ghedinic001ce32016-03-01 18:06:15 +000069 lock = offset2ptr(*pval, aux->ref_lock);
Matt Caswell0f113f32015-01-22 03:40:55 +000070 if (op == 0) {
71 *lck = 1;
Alessandro Ghedinic001ce32016-03-01 18:06:15 +000072 *lock = CRYPTO_THREAD_lock_new();
FdaSilvaYYb2b361f2016-04-30 16:23:33 +020073 if (*lock == NULL) {
FdaSilvaYY687b4862016-05-29 14:32:23 +020074 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
75 return -1;
FdaSilvaYYb2b361f2016-04-30 16:23:33 +020076 }
Matt Caswell0f113f32015-01-22 03:40:55 +000077 return 1;
78 }
FdaSilvaYY687b4862016-05-29 14:32:23 +020079 if (CRYPTO_atomic_add(lck, op, &ret, *lock) < 0)
80 return -1; /* failed */
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000081#ifdef REF_PRINT
Rich Salzf3f1cf82016-01-30 12:04:25 -050082 fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000083#endif
Rich Salzf3f1cf82016-01-30 12:04:25 -050084 REF_ASSERT_ISNT(ret < 0);
FdaSilvaYY687b4862016-05-29 14:32:23 +020085 if (ret == 0) {
Alessandro Ghedinic001ce32016-03-01 18:06:15 +000086 CRYPTO_THREAD_lock_free(*lock);
FdaSilvaYY687b4862016-05-29 14:32:23 +020087 *lock = NULL;
88 }
Matt Caswell0f113f32015-01-22 03:40:55 +000089 return ret;
90}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +000091
92static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
Matt Caswell0f113f32015-01-22 03:40:55 +000093{
94 const ASN1_AUX *aux;
95 if (!pval || !*pval)
96 return NULL;
97 aux = it->funcs;
98 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
99 return NULL;
100 return offset2ptr(*pval, aux->enc_offset);
101}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000102
103void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
Matt Caswell0f113f32015-01-22 03:40:55 +0000104{
105 ASN1_ENCODING *enc;
106 enc = asn1_get_enc_ptr(pval, it);
107 if (enc) {
108 enc->enc = NULL;
109 enc->len = 0;
110 enc->modified = 1;
111 }
112}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000113
114void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
Matt Caswell0f113f32015-01-22 03:40:55 +0000115{
116 ASN1_ENCODING *enc;
117 enc = asn1_get_enc_ptr(pval, it);
118 if (enc) {
Rich Salzb548a1f2015-05-01 10:02:07 -0400119 OPENSSL_free(enc->enc);
Matt Caswell0f113f32015-01-22 03:40:55 +0000120 enc->enc = NULL;
121 enc->len = 0;
122 enc->modified = 1;
123 }
124}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000125
Dr. Stephen Hensonf3f52d72004-04-25 12:46:39 +0000126int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
Matt Caswell0f113f32015-01-22 03:40:55 +0000127 const ASN1_ITEM *it)
128{
129 ASN1_ENCODING *enc;
130 enc = asn1_get_enc_ptr(pval, it);
131 if (!enc)
132 return 1;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000133
Rich Salzb548a1f2015-05-01 10:02:07 -0400134 OPENSSL_free(enc->enc);
Matt Caswell0f113f32015-01-22 03:40:55 +0000135 enc->enc = OPENSSL_malloc(inlen);
Matt Caswell90945fa2015-10-30 11:12:26 +0000136 if (enc->enc == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000137 return 0;
138 memcpy(enc->enc, in, inlen);
139 enc->len = inlen;
140 enc->modified = 0;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000141
Matt Caswell0f113f32015-01-22 03:40:55 +0000142 return 1;
143}
144
Dr. Stephen Hensonf3f52d72004-04-25 12:46:39 +0000145int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
Matt Caswell0f113f32015-01-22 03:40:55 +0000146 const ASN1_ITEM *it)
147{
148 ASN1_ENCODING *enc;
149 enc = asn1_get_enc_ptr(pval, it);
150 if (!enc || enc->modified)
151 return 0;
152 if (out) {
153 memcpy(*out, enc->enc, enc->len);
154 *out += enc->len;
155 }
156 if (len)
157 *len = enc->len;
158 return 1;
159}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000160
161/* Given an ASN1_TEMPLATE get a pointer to a field */
Matt Caswell0f113f32015-01-22 03:40:55 +0000162ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
163{
164 ASN1_VALUE **pvaltmp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000165 pvaltmp = offset2ptr(*pval, tt->offset);
166 /*
167 * NOTE for BOOLEAN types the field is just a plain int so we can't
168 * return int **, so settle for (int *).
169 */
170 return pvaltmp;
171}
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000172
Matt Caswell0f113f32015-01-22 03:40:55 +0000173/*
174 * Handle ANY DEFINED BY template, find the selector, look up the relevant
175 * ASN1_TEMPLATE in the table and return it.
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000176 */
177
Dr. Stephen Hensonf3f52d72004-04-25 12:46:39 +0000178const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
Matt Caswell0f113f32015-01-22 03:40:55 +0000179 int nullerr)
180{
181 const ASN1_ADB *adb;
182 const ASN1_ADB_TABLE *atbl;
183 long selector;
184 ASN1_VALUE **sfld;
185 int i;
186 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
187 return tt;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000188
Matt Caswell0f113f32015-01-22 03:40:55 +0000189 /* Else ANY DEFINED BY ... get the table */
190 adb = ASN1_ADB_ptr(tt->item);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000191
Matt Caswell0f113f32015-01-22 03:40:55 +0000192 /* Get the selector field */
193 sfld = offset2ptr(*pval, adb->offset);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000194
Matt Caswell0f113f32015-01-22 03:40:55 +0000195 /* Check if NULL */
Dr. Stephen Henson7c467462016-06-14 17:44:22 +0100196 if (*sfld == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000197 if (!adb->null_tt)
198 goto err;
199 return adb->null_tt;
200 }
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000201
Matt Caswell0f113f32015-01-22 03:40:55 +0000202 /*
203 * Convert type to a long: NB: don't check for NID_undef here because it
204 * might be a legitimate value in the table
205 */
206 if (tt->flags & ASN1_TFLG_ADB_OID)
207 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
208 else
209 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000210
Dr. Stephen Henson5b703722016-03-04 14:55:24 +0000211 /* Let application callback translate value */
212 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
213 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
214 return NULL;
215 }
216
Matt Caswell0f113f32015-01-22 03:40:55 +0000217 /*
218 * Try to find matching entry in table Maybe should check application
219 * types first to allow application override? Might also be useful to
220 * have a flag which indicates table is sorted and we can do a binary
221 * search. For now stick to a linear search.
222 */
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000223
Matt Caswell0f113f32015-01-22 03:40:55 +0000224 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
225 if (atbl->value == selector)
226 return &atbl->tt;
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000227
Matt Caswell0f113f32015-01-22 03:40:55 +0000228 /* FIXME: need to search application table too */
Dr. Stephen Henson9d6b1ce2000-12-08 19:09:35 +0000229
Matt Caswell0f113f32015-01-22 03:40:55 +0000230 /* No match, return default type */
231 if (!adb->default_tt)
232 goto err;
233 return adb->default_tt;
234
235 err:
236 /* FIXME: should log the value or OID of unsupported type */
237 if (nullerr)
238 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
239 return NULL;
240}