1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay@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@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@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@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 <openssl/asn1t.h> 60#include <openssl/mem.h> 61#include <openssl/obj.h> 62#include <openssl/err.h> 63 64 65/* Utility functions for manipulating fields and offsets */ 66 67/* Add 'offset' to 'addr' */ 68#define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) 69 70/* Given an ASN1_ITEM CHOICE type return the selector value */ 71int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) { 72 int *sel = offset2ptr(*pval, it->utype); 73 return *sel; 74} 75 76/* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */ 77int asn1_set_choice_selector(ASN1_VALUE **pval, int value, 78 const ASN1_ITEM *it) { 79 int *sel, ret; 80 sel = offset2ptr(*pval, it->utype); 81 ret = *sel; 82 *sel = value; 83 return ret; 84} 85 86/* Do reference counting. The value 'op' decides what to do. if it is +1 then 87 * the count is incremented. If op is 0 count is set to 1. If op is -1 count is 88 * decremented and the return value is the current refrence count or 0 if no 89 * reference count exists. */ 90int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) { 91 const ASN1_AUX *aux; 92 int *lck, ret; 93 if (it->itype != ASN1_ITYPE_SEQUENCE && 94 it->itype != ASN1_ITYPE_NDEF_SEQUENCE) { 95 return 0; 96 } 97 aux = it->funcs; 98 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) { 99 return 0; 100 } 101 lck = offset2ptr(*pval, aux->ref_offset); 102 if (op == 0) { 103 *lck = 1; 104 return 1; 105 } 106 ret = CRYPTO_add(lck, op, aux->ref_lock); 107 return ret; 108} 109 110static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) { 111 const ASN1_AUX *aux; 112 if (!pval || !*pval) { 113 return NULL; 114 } 115 aux = it->funcs; 116 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) { 117 return NULL; 118 } 119 return offset2ptr(*pval, aux->enc_offset); 120} 121 122void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) { 123 ASN1_ENCODING *enc; 124 enc = asn1_get_enc_ptr(pval, it); 125 if (enc) { 126 enc->enc = NULL; 127 enc->len = 0; 128 enc->modified = 1; 129 } 130} 131 132void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { 133 ASN1_ENCODING *enc; 134 enc = asn1_get_enc_ptr(pval, it); 135 if (enc) { 136 if (enc->enc) { 137 OPENSSL_free(enc->enc); 138 } 139 enc->enc = NULL; 140 enc->len = 0; 141 enc->modified = 1; 142 } 143} 144 145int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 146 const ASN1_ITEM *it) { 147 ASN1_ENCODING *enc; 148 enc = asn1_get_enc_ptr(pval, it); 149 if (!enc) { 150 return 1; 151 } 152 153 if (enc->enc) { 154 OPENSSL_free(enc->enc); 155 } 156 enc->enc = OPENSSL_malloc(inlen); 157 if (!enc->enc) { 158 return 0; 159 } 160 memcpy(enc->enc, in, inlen); 161 enc->len = inlen; 162 enc->modified = 0; 163 164 return 1; 165} 166 167int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 168 const ASN1_ITEM *it) { 169 ASN1_ENCODING *enc; 170 enc = asn1_get_enc_ptr(pval, it); 171 if (!enc || enc->modified) { 172 return 0; 173 } 174 if (out) { 175 memcpy(*out, enc->enc, enc->len); 176 *out += enc->len; 177 } 178 if (len) { 179 *len = enc->len; 180 } 181 return 1; 182} 183 184/* Given an ASN1_TEMPLATE get a pointer to a field */ 185ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { 186 ASN1_VALUE **pvaltmp; 187 if (tt->flags & ASN1_TFLG_COMBINE) { 188 return pval; 189 } 190 pvaltmp = offset2ptr(*pval, tt->offset); 191 /* NOTE for BOOLEAN types the field is just a plain int so we can't return 192 * int **, so settle for (int *). */ 193 return pvaltmp; 194} 195 196/* Handle ANY DEFINED BY template, find the selector, look up the relevant 197 * ASN1_TEMPLATE in the table and return it. */ 198const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 199 int nullerr) { 200 const ASN1_ADB *adb; 201 const ASN1_ADB_TABLE *atbl; 202 long selector; 203 ASN1_VALUE **sfld; 204 int i; 205 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) { 206 return tt; 207 } 208 209 /* Else ANY DEFINED BY ... get the table */ 210 adb = ASN1_ADB_ptr(tt->item); 211 212 /* Get the selector field */ 213 sfld = offset2ptr(*pval, adb->offset); 214 215 /* Check if NULL */ 216 if (!sfld) { 217 if (!adb->null_tt) { 218 goto err; 219 } 220 return adb->null_tt; 221 } 222 223 /* Convert type to a long: 224 * NB: don't check for NID_undef here because it 225 * might be a legitimate value in the table */ 226 if (tt->flags & ASN1_TFLG_ADB_OID) { 227 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 228 } else { 229 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 230 } 231 232 /* Try to find matching entry in table Maybe should check application types 233 * first to allow application override? Might also be useful to have a flag 234 * which indicates table is sorted and we can do a binary search. For now 235 * stick to a linear search. */ 236 237 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) { 238 if (atbl->value == selector) { 239 return &atbl->tt; 240 } 241 } 242 243 /* FIXME: need to search application table too */ 244 245 /* No match, return default type */ 246 if (!adb->default_tt) { 247 goto err; 248 } 249 return adb->default_tt; 250 251err: 252 /* FIXME: should log the value or OID of unsupported type */ 253 if (nullerr) { 254 OPENSSL_PUT_ERROR(ASN1, asn1_do_adb, 255 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 256 } 257 return NULL; 258} 259