1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) cryptsoft.com). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson (tjh (at) cryptsoft.com). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young (eay (at) cryptsoft.com)" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] */ 56 57 #include <openssl/asn1.h> 58 59 #include <string.h> 60 61 #include <openssl/asn1t.h> 62 #include <openssl/mem.h> 63 #include <openssl/obj.h> 64 #include <openssl/err.h> 65 #include <openssl/thread.h> 66 67 #include "../internal.h" 68 69 70 /* Utility functions for manipulating fields and offsets */ 71 72 /* Add 'offset' to 'addr' */ 73 #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) 74 75 /* Given an ASN1_ITEM CHOICE type return the selector value */ 76 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) { 77 int *sel = offset2ptr(*pval, it->utype); 78 return *sel; 79 } 80 81 /* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */ 82 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, 83 const ASN1_ITEM *it) { 84 int *sel, ret; 85 sel = offset2ptr(*pval, it->utype); 86 ret = *sel; 87 *sel = value; 88 return ret; 89 } 90 91 static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval, 92 const ASN1_ITEM *it) { 93 if (it->itype != ASN1_ITYPE_SEQUENCE && 94 it->itype != ASN1_ITYPE_NDEF_SEQUENCE) { 95 return NULL; 96 } 97 const ASN1_AUX *aux = it->funcs; 98 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) { 99 return NULL; 100 } 101 return offset2ptr(*pval, aux->ref_offset); 102 } 103 104 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) { 105 CRYPTO_refcount_t *references = asn1_get_references(pval, it); 106 if (references != NULL) { 107 *references = 1; 108 } 109 } 110 111 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) { 112 CRYPTO_refcount_t *references = asn1_get_references(pval, it); 113 if (references != NULL) { 114 return CRYPTO_refcount_dec_and_test_zero(references); 115 } 116 return 1; 117 } 118 119 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) { 120 const ASN1_AUX *aux; 121 if (!pval || !*pval) { 122 return NULL; 123 } 124 aux = it->funcs; 125 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) { 126 return NULL; 127 } 128 return offset2ptr(*pval, aux->enc_offset); 129 } 130 131 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) { 132 ASN1_ENCODING *enc; 133 enc = asn1_get_enc_ptr(pval, it); 134 if (enc) { 135 enc->enc = NULL; 136 enc->len = 0; 137 enc->modified = 1; 138 } 139 } 140 141 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { 142 ASN1_ENCODING *enc; 143 enc = asn1_get_enc_ptr(pval, it); 144 if (enc) { 145 if (enc->enc) { 146 OPENSSL_free(enc->enc); 147 } 148 enc->enc = NULL; 149 enc->len = 0; 150 enc->modified = 1; 151 } 152 } 153 154 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 155 const ASN1_ITEM *it) { 156 ASN1_ENCODING *enc; 157 enc = asn1_get_enc_ptr(pval, it); 158 if (!enc) { 159 return 1; 160 } 161 162 if (enc->enc) { 163 OPENSSL_free(enc->enc); 164 } 165 enc->enc = OPENSSL_malloc(inlen); 166 if (!enc->enc) { 167 return 0; 168 } 169 memcpy(enc->enc, in, inlen); 170 enc->len = inlen; 171 enc->modified = 0; 172 173 return 1; 174 } 175 176 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 177 const ASN1_ITEM *it) { 178 ASN1_ENCODING *enc; 179 enc = asn1_get_enc_ptr(pval, it); 180 if (!enc || enc->modified) { 181 return 0; 182 } 183 if (out) { 184 memcpy(*out, enc->enc, enc->len); 185 *out += enc->len; 186 } 187 if (len) { 188 *len = enc->len; 189 } 190 return 1; 191 } 192 193 /* Given an ASN1_TEMPLATE get a pointer to a field */ 194 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { 195 ASN1_VALUE **pvaltmp; 196 if (tt->flags & ASN1_TFLG_COMBINE) { 197 return pval; 198 } 199 pvaltmp = offset2ptr(*pval, tt->offset); 200 /* NOTE for BOOLEAN types the field is just a plain int so we can't return 201 * int **, so settle for (int *). */ 202 return pvaltmp; 203 } 204 205 /* Handle ANY DEFINED BY template, find the selector, look up the relevant 206 * ASN1_TEMPLATE in the table and return it. */ 207 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 208 int nullerr) { 209 const ASN1_ADB *adb; 210 const ASN1_ADB_TABLE *atbl; 211 long selector; 212 ASN1_VALUE **sfld; 213 int i; 214 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) { 215 return tt; 216 } 217 218 /* Else ANY DEFINED BY ... get the table */ 219 adb = ASN1_ADB_ptr(tt->item); 220 221 /* Get the selector field */ 222 sfld = offset2ptr(*pval, adb->offset); 223 224 /* Check if NULL */ 225 if (!sfld) { 226 if (!adb->null_tt) { 227 goto err; 228 } 229 return adb->null_tt; 230 } 231 232 /* Convert type to a long: 233 * NB: don't check for NID_undef here because it 234 * might be a legitimate value in the table */ 235 if (tt->flags & ASN1_TFLG_ADB_OID) { 236 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 237 } else { 238 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 239 } 240 241 /* Try to find matching entry in table Maybe should check application types 242 * first to allow application override? Might also be useful to have a flag 243 * which indicates table is sorted and we can do a binary search. For now 244 * stick to a linear search. */ 245 246 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) { 247 if (atbl->value == selector) { 248 return &atbl->tt; 249 } 250 } 251 252 /* FIXME: need to search application table too */ 253 254 /* No match, return default type */ 255 if (!adb->default_tt) { 256 goto err; 257 } 258 return adb->default_tt; 259 260 err: 261 /* FIXME: should log the value or OID of unsupported type */ 262 if (nullerr) { 263 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 264 } 265 return NULL; 266 } 267