1109998Smarkm/* tasn_utl.c */ 2280297Sjkim/* 3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280297Sjkim * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7160814Ssimon * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280297Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60109998Smarkm#include <stddef.h> 61109998Smarkm#include <string.h> 62109998Smarkm#include <openssl/asn1.h> 63109998Smarkm#include <openssl/asn1t.h> 64109998Smarkm#include <openssl/objects.h> 65109998Smarkm#include <openssl/err.h> 66109998Smarkm 67109998Smarkm/* Utility functions for manipulating fields and offsets */ 68109998Smarkm 69109998Smarkm/* Add 'offset' to 'addr' */ 70109998Smarkm#define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) 71109998Smarkm 72280297Sjkim/* 73280297Sjkim * Given an ASN1_ITEM CHOICE type return the selector value 74109998Smarkm */ 75109998Smarkm 76109998Smarkmint asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) 77280297Sjkim{ 78280297Sjkim int *sel = offset2ptr(*pval, it->utype); 79280297Sjkim return *sel; 80280297Sjkim} 81109998Smarkm 82280297Sjkim/* 83280297Sjkim * Given an ASN1_ITEM CHOICE type set the selector value, return old value. 84109998Smarkm */ 85109998Smarkm 86280297Sjkimint asn1_set_choice_selector(ASN1_VALUE **pval, int value, 87280297Sjkim const ASN1_ITEM *it) 88280297Sjkim{ 89280297Sjkim int *sel, ret; 90280297Sjkim sel = offset2ptr(*pval, it->utype); 91280297Sjkim ret = *sel; 92280297Sjkim *sel = value; 93280297Sjkim return ret; 94280297Sjkim} 95109998Smarkm 96280297Sjkim/* 97280297Sjkim * Do reference counting. The value 'op' decides what to do. if it is +1 98280297Sjkim * then the count is incremented. If op is 0 count is set to 1. If op is -1 99280297Sjkim * count is decremented and the return value is the current refrence count or 100280297Sjkim * 0 if no reference count exists. 101109998Smarkm */ 102109998Smarkm 103109998Smarkmint asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 104280297Sjkim{ 105280297Sjkim const ASN1_AUX *aux; 106280297Sjkim int *lck, ret; 107280297Sjkim if ((it->itype != ASN1_ITYPE_SEQUENCE) 108280297Sjkim && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) 109280297Sjkim return 0; 110280297Sjkim aux = it->funcs; 111280297Sjkim if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) 112280297Sjkim return 0; 113280297Sjkim lck = offset2ptr(*pval, aux->ref_offset); 114280297Sjkim if (op == 0) { 115280297Sjkim *lck = 1; 116280297Sjkim return 1; 117280297Sjkim } 118280297Sjkim ret = CRYPTO_add(lck, op, aux->ref_lock); 119109998Smarkm#ifdef REF_PRINT 120280297Sjkim fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck); 121109998Smarkm#endif 122109998Smarkm#ifdef REF_CHECK 123280297Sjkim if (ret < 0) 124280297Sjkim fprintf(stderr, "%s, bad reference count\n", it->sname); 125109998Smarkm#endif 126280297Sjkim return ret; 127280297Sjkim} 128109998Smarkm 129109998Smarkmstatic ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) 130280297Sjkim{ 131280297Sjkim const ASN1_AUX *aux; 132280297Sjkim if (!pval || !*pval) 133280297Sjkim return NULL; 134280297Sjkim aux = it->funcs; 135280297Sjkim if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) 136280297Sjkim return NULL; 137280297Sjkim return offset2ptr(*pval, aux->enc_offset); 138280297Sjkim} 139109998Smarkm 140109998Smarkmvoid asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) 141280297Sjkim{ 142280297Sjkim ASN1_ENCODING *enc; 143280297Sjkim enc = asn1_get_enc_ptr(pval, it); 144280297Sjkim if (enc) { 145280297Sjkim enc->enc = NULL; 146280297Sjkim enc->len = 0; 147280297Sjkim enc->modified = 1; 148280297Sjkim } 149280297Sjkim} 150109998Smarkm 151109998Smarkmvoid asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 152280297Sjkim{ 153280297Sjkim ASN1_ENCODING *enc; 154280297Sjkim enc = asn1_get_enc_ptr(pval, it); 155280297Sjkim if (enc) { 156280297Sjkim if (enc->enc) 157280297Sjkim OPENSSL_free(enc->enc); 158280297Sjkim enc->enc = NULL; 159280297Sjkim enc->len = 0; 160280297Sjkim enc->modified = 1; 161280297Sjkim } 162280297Sjkim} 163109998Smarkm 164160814Ssimonint asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 165280297Sjkim const ASN1_ITEM *it) 166280297Sjkim{ 167280297Sjkim ASN1_ENCODING *enc; 168280297Sjkim enc = asn1_get_enc_ptr(pval, it); 169280297Sjkim if (!enc) 170280297Sjkim return 1; 171109998Smarkm 172280297Sjkim if (enc->enc) 173280297Sjkim OPENSSL_free(enc->enc); 174280297Sjkim enc->enc = OPENSSL_malloc(inlen); 175280297Sjkim if (!enc->enc) 176280297Sjkim return 0; 177280297Sjkim memcpy(enc->enc, in, inlen); 178280297Sjkim enc->len = inlen; 179280297Sjkim enc->modified = 0; 180109998Smarkm 181280297Sjkim return 1; 182280297Sjkim} 183280297Sjkim 184160814Ssimonint asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 185280297Sjkim const ASN1_ITEM *it) 186280297Sjkim{ 187280297Sjkim ASN1_ENCODING *enc; 188280297Sjkim enc = asn1_get_enc_ptr(pval, it); 189280297Sjkim if (!enc || enc->modified) 190280297Sjkim return 0; 191280297Sjkim if (out) { 192280297Sjkim memcpy(*out, enc->enc, enc->len); 193280297Sjkim *out += enc->len; 194280297Sjkim } 195280297Sjkim if (len) 196280297Sjkim *len = enc->len; 197280297Sjkim return 1; 198280297Sjkim} 199109998Smarkm 200109998Smarkm/* Given an ASN1_TEMPLATE get a pointer to a field */ 201280297SjkimASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 202280297Sjkim{ 203280297Sjkim ASN1_VALUE **pvaltmp; 204280297Sjkim if (tt->flags & ASN1_TFLG_COMBINE) 205280297Sjkim return pval; 206280297Sjkim pvaltmp = offset2ptr(*pval, tt->offset); 207280297Sjkim /* 208280297Sjkim * NOTE for BOOLEAN types the field is just a plain int so we can't 209280297Sjkim * return int **, so settle for (int *). 210280297Sjkim */ 211280297Sjkim return pvaltmp; 212280297Sjkim} 213109998Smarkm 214280297Sjkim/* 215280297Sjkim * Handle ANY DEFINED BY template, find the selector, look up the relevant 216280297Sjkim * ASN1_TEMPLATE in the table and return it. 217109998Smarkm */ 218109998Smarkm 219160814Ssimonconst ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 220280297Sjkim int nullerr) 221280297Sjkim{ 222280297Sjkim const ASN1_ADB *adb; 223280297Sjkim const ASN1_ADB_TABLE *atbl; 224280297Sjkim long selector; 225280297Sjkim ASN1_VALUE **sfld; 226280297Sjkim int i; 227280297Sjkim if (!(tt->flags & ASN1_TFLG_ADB_MASK)) 228280297Sjkim return tt; 229109998Smarkm 230280297Sjkim /* Else ANY DEFINED BY ... get the table */ 231280297Sjkim adb = ASN1_ADB_ptr(tt->item); 232109998Smarkm 233280297Sjkim /* Get the selector field */ 234280297Sjkim sfld = offset2ptr(*pval, adb->offset); 235109998Smarkm 236280297Sjkim /* Check if NULL */ 237306195Sjkim if (*sfld == NULL) { 238280297Sjkim if (!adb->null_tt) 239280297Sjkim goto err; 240280297Sjkim return adb->null_tt; 241280297Sjkim } 242109998Smarkm 243280297Sjkim /* 244280297Sjkim * Convert type to a long: NB: don't check for NID_undef here because it 245280297Sjkim * might be a legitimate value in the table 246280297Sjkim */ 247280297Sjkim if (tt->flags & ASN1_TFLG_ADB_OID) 248280297Sjkim selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 249280297Sjkim else 250280297Sjkim selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 251109998Smarkm 252280297Sjkim /* 253280297Sjkim * Try to find matching entry in table Maybe should check application 254280297Sjkim * types first to allow application override? Might also be useful to 255280297Sjkim * have a flag which indicates table is sorted and we can do a binary 256280297Sjkim * search. For now stick to a linear search. 257280297Sjkim */ 258109998Smarkm 259280297Sjkim for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) 260280297Sjkim if (atbl->value == selector) 261280297Sjkim return &atbl->tt; 262109998Smarkm 263280297Sjkim /* FIXME: need to search application table too */ 264109998Smarkm 265280297Sjkim /* No match, return default type */ 266280297Sjkim if (!adb->default_tt) 267280297Sjkim goto err; 268280297Sjkim return adb->default_tt; 269280297Sjkim 270280297Sjkim err: 271280297Sjkim /* FIXME: should log the value or OID of unsupported type */ 272280297Sjkim if (nullerr) 273280297Sjkim ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 274280297Sjkim return NULL; 275280297Sjkim} 276