1109998Smarkm/* tasn_utl.c */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280304Sjkim * 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 14280304Sjkim * 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 72280304Sjkim/* 73280304Sjkim * Given an ASN1_ITEM CHOICE type return the selector value 74109998Smarkm */ 75109998Smarkm 76109998Smarkmint asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) 77280304Sjkim{ 78280304Sjkim int *sel = offset2ptr(*pval, it->utype); 79280304Sjkim return *sel; 80280304Sjkim} 81109998Smarkm 82280304Sjkim/* 83280304Sjkim * Given an ASN1_ITEM CHOICE type set the selector value, return old value. 84109998Smarkm */ 85109998Smarkm 86280304Sjkimint asn1_set_choice_selector(ASN1_VALUE **pval, int value, 87280304Sjkim const ASN1_ITEM *it) 88280304Sjkim{ 89280304Sjkim int *sel, ret; 90280304Sjkim sel = offset2ptr(*pval, it->utype); 91280304Sjkim ret = *sel; 92280304Sjkim *sel = value; 93280304Sjkim return ret; 94280304Sjkim} 95109998Smarkm 96280304Sjkim/* 97280304Sjkim * Do reference counting. The value 'op' decides what to do. if it is +1 98280304Sjkim * then the count is incremented. If op is 0 count is set to 1. If op is -1 99280304Sjkim * count is decremented and the return value is the current refrence count or 100280304Sjkim * 0 if no reference count exists. 101109998Smarkm */ 102109998Smarkm 103109998Smarkmint asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 104280304Sjkim{ 105280304Sjkim const ASN1_AUX *aux; 106280304Sjkim int *lck, ret; 107280304Sjkim if ((it->itype != ASN1_ITYPE_SEQUENCE) 108280304Sjkim && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) 109280304Sjkim return 0; 110280304Sjkim aux = it->funcs; 111280304Sjkim if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) 112280304Sjkim return 0; 113280304Sjkim lck = offset2ptr(*pval, aux->ref_offset); 114280304Sjkim if (op == 0) { 115280304Sjkim *lck = 1; 116280304Sjkim return 1; 117280304Sjkim } 118280304Sjkim ret = CRYPTO_add(lck, op, aux->ref_lock); 119109998Smarkm#ifdef REF_PRINT 120280304Sjkim fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck); 121109998Smarkm#endif 122109998Smarkm#ifdef REF_CHECK 123280304Sjkim if (ret < 0) 124280304Sjkim fprintf(stderr, "%s, bad reference count\n", it->sname); 125109998Smarkm#endif 126280304Sjkim return ret; 127280304Sjkim} 128109998Smarkm 129109998Smarkmstatic ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) 130280304Sjkim{ 131280304Sjkim const ASN1_AUX *aux; 132280304Sjkim if (!pval || !*pval) 133280304Sjkim return NULL; 134280304Sjkim aux = it->funcs; 135280304Sjkim if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) 136280304Sjkim return NULL; 137280304Sjkim return offset2ptr(*pval, aux->enc_offset); 138280304Sjkim} 139109998Smarkm 140109998Smarkmvoid asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) 141280304Sjkim{ 142280304Sjkim ASN1_ENCODING *enc; 143280304Sjkim enc = asn1_get_enc_ptr(pval, it); 144280304Sjkim if (enc) { 145280304Sjkim enc->enc = NULL; 146280304Sjkim enc->len = 0; 147280304Sjkim enc->modified = 1; 148280304Sjkim } 149280304Sjkim} 150109998Smarkm 151109998Smarkmvoid asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 152280304Sjkim{ 153280304Sjkim ASN1_ENCODING *enc; 154280304Sjkim enc = asn1_get_enc_ptr(pval, it); 155280304Sjkim if (enc) { 156280304Sjkim if (enc->enc) 157280304Sjkim OPENSSL_free(enc->enc); 158280304Sjkim enc->enc = NULL; 159280304Sjkim enc->len = 0; 160280304Sjkim enc->modified = 1; 161280304Sjkim } 162280304Sjkim} 163109998Smarkm 164160814Ssimonint asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 165280304Sjkim const ASN1_ITEM *it) 166280304Sjkim{ 167280304Sjkim ASN1_ENCODING *enc; 168280304Sjkim enc = asn1_get_enc_ptr(pval, it); 169280304Sjkim if (!enc) 170280304Sjkim return 1; 171109998Smarkm 172280304Sjkim if (enc->enc) 173280304Sjkim OPENSSL_free(enc->enc); 174280304Sjkim enc->enc = OPENSSL_malloc(inlen); 175280304Sjkim if (!enc->enc) 176280304Sjkim return 0; 177280304Sjkim memcpy(enc->enc, in, inlen); 178280304Sjkim enc->len = inlen; 179280304Sjkim enc->modified = 0; 180109998Smarkm 181280304Sjkim return 1; 182280304Sjkim} 183280304Sjkim 184160814Ssimonint asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 185280304Sjkim const ASN1_ITEM *it) 186280304Sjkim{ 187280304Sjkim ASN1_ENCODING *enc; 188280304Sjkim enc = asn1_get_enc_ptr(pval, it); 189280304Sjkim if (!enc || enc->modified) 190280304Sjkim return 0; 191280304Sjkim if (out) { 192280304Sjkim memcpy(*out, enc->enc, enc->len); 193280304Sjkim *out += enc->len; 194280304Sjkim } 195280304Sjkim if (len) 196280304Sjkim *len = enc->len; 197280304Sjkim return 1; 198280304Sjkim} 199109998Smarkm 200109998Smarkm/* Given an ASN1_TEMPLATE get a pointer to a field */ 201280304SjkimASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 202280304Sjkim{ 203280304Sjkim ASN1_VALUE **pvaltmp; 204280304Sjkim if (tt->flags & ASN1_TFLG_COMBINE) 205280304Sjkim return pval; 206280304Sjkim pvaltmp = offset2ptr(*pval, tt->offset); 207280304Sjkim /* 208280304Sjkim * NOTE for BOOLEAN types the field is just a plain int so we can't 209280304Sjkim * return int **, so settle for (int *). 210280304Sjkim */ 211280304Sjkim return pvaltmp; 212280304Sjkim} 213109998Smarkm 214280304Sjkim/* 215280304Sjkim * Handle ANY DEFINED BY template, find the selector, look up the relevant 216280304Sjkim * ASN1_TEMPLATE in the table and return it. 217109998Smarkm */ 218109998Smarkm 219160814Ssimonconst ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 220280304Sjkim int nullerr) 221280304Sjkim{ 222280304Sjkim const ASN1_ADB *adb; 223280304Sjkim const ASN1_ADB_TABLE *atbl; 224280304Sjkim long selector; 225280304Sjkim ASN1_VALUE **sfld; 226280304Sjkim int i; 227280304Sjkim if (!(tt->flags & ASN1_TFLG_ADB_MASK)) 228280304Sjkim return tt; 229109998Smarkm 230280304Sjkim /* Else ANY DEFINED BY ... get the table */ 231280304Sjkim adb = ASN1_ADB_ptr(tt->item); 232109998Smarkm 233280304Sjkim /* Get the selector field */ 234280304Sjkim sfld = offset2ptr(*pval, adb->offset); 235109998Smarkm 236280304Sjkim /* Check if NULL */ 237280304Sjkim if (!sfld) { 238280304Sjkim if (!adb->null_tt) 239280304Sjkim goto err; 240280304Sjkim return adb->null_tt; 241280304Sjkim } 242109998Smarkm 243280304Sjkim /* 244280304Sjkim * Convert type to a long: NB: don't check for NID_undef here because it 245280304Sjkim * might be a legitimate value in the table 246280304Sjkim */ 247280304Sjkim if (tt->flags & ASN1_TFLG_ADB_OID) 248280304Sjkim selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 249280304Sjkim else 250280304Sjkim selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 251109998Smarkm 252280304Sjkim /* 253280304Sjkim * Try to find matching entry in table Maybe should check application 254280304Sjkim * types first to allow application override? Might also be useful to 255280304Sjkim * have a flag which indicates table is sorted and we can do a binary 256280304Sjkim * search. For now stick to a linear search. 257280304Sjkim */ 258109998Smarkm 259280304Sjkim for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) 260280304Sjkim if (atbl->value == selector) 261280304Sjkim return &atbl->tt; 262109998Smarkm 263280304Sjkim /* FIXME: need to search application table too */ 264109998Smarkm 265280304Sjkim /* No match, return default type */ 266280304Sjkim if (!adb->default_tt) 267280304Sjkim goto err; 268280304Sjkim return adb->default_tt; 269280304Sjkim 270280304Sjkim err: 271280304Sjkim /* FIXME: should log the value or OID of unsupported type */ 272280304Sjkim if (nullerr) 273280304Sjkim ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 274280304Sjkim return NULL; 275280304Sjkim} 276