a_enum.c revision 225736
1159248Srwatson/* crypto/asn1/a_enum.c */ 2159248Srwatson/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3159248Srwatson * All rights reserved. 4159248Srwatson * 5159248Srwatson * This package is an SSL implementation written 6159248Srwatson * by Eric Young (eay@cryptsoft.com). 7159248Srwatson * The implementation was written so as to conform with Netscapes SSL. 8159248Srwatson * 9159248Srwatson * This library is free for commercial and non-commercial use as long as 10159248Srwatson * the following conditions are aheared to. The following conditions 11159248Srwatson * apply to all code found in this distribution, be it the RC4, RSA, 12159248Srwatson * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13168777Srwatson * included with this distribution is covered by the same copyright terms 14159248Srwatson * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15159248Srwatson * 16159248Srwatson * Copyright remains Eric Young's, and as such any Copyright notices in 17159248Srwatson * the code are not to be removed. 18159248Srwatson * If this package is used in a product, Eric Young should be given attribution 19159248Srwatson * as the author of the parts of the library used. 20159248Srwatson * This can be in the form of a textual message at program startup or 21159248Srwatson * in documentation (online or textual) provided with the package. 22159248Srwatson * 23159248Srwatson * Redistribution and use in source and binary forms, with or without 24159248Srwatson * modification, are permitted provided that the following conditions 25159248Srwatson * are met: 26168777Srwatson * 1. Redistributions of source code must retain the copyright 27159248Srwatson * notice, this list of conditions and the following disclaimer. 28159248Srwatson * 2. Redistributions in binary form must reproduce the above copyright 29159248Srwatson * notice, this list of conditions and the following disclaimer in the 30159248Srwatson * documentation and/or other materials provided with the distribution. 31159248Srwatson * 3. All advertising materials mentioning features or use of this software 32162503Srwatson * must display the following acknowledgement: 33162503Srwatson * "This product includes cryptographic software written by 34162503Srwatson * Eric Young (eay@cryptsoft.com)" 35159248Srwatson * The word 'cryptographic' can be left out if the rouines from the library 36162503Srwatson * being used are not cryptographic related :-). 37168777Srwatson * 4. If you include any Windows specific code (or a derivative thereof) from 38159248Srwatson * the apps directory (application code) you must include an acknowledgement: 39159248Srwatson * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40159248Srwatson * 41168777Srwatson * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42159248Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43168777Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44159248Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45159248Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46159248Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47159248Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48159248Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49159248Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50159248Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51159248Srwatson * SUCH DAMAGE. 52159248Srwatson * 53159248Srwatson * The licence and distribution terms for any publically available version or 54159248Srwatson * derivative of this code cannot be changed. i.e. this code cannot simply be 55159248Srwatson * copied and put under another distribution licence 56159248Srwatson * [including the GNU Public Licence.] 57159248Srwatson */ 58159248Srwatson 59159248Srwatson#include <stdio.h> 60159248Srwatson#include "cryptlib.h" 61159248Srwatson#include <openssl/asn1.h> 62159248Srwatson#include <openssl/bn.h> 63159248Srwatson 64159248Srwatson/* 65159248Srwatson * Code for ENUMERATED type: identical to INTEGER apart from a different tag. 66159248Srwatson * for comments on encoding see a_int.c 67159248Srwatson */ 68159248Srwatson 69159248Srwatsonint ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) 70159248Srwatson { 71159248Srwatson int j,k; 72159248Srwatson unsigned int i; 73159248Srwatson unsigned char buf[sizeof(long)+1]; 74159248Srwatson long d; 75159248Srwatson 76168777Srwatson a->type=V_ASN1_ENUMERATED; 77159248Srwatson if (a->length < (int)(sizeof(long)+1)) 78159248Srwatson { 79159248Srwatson if (a->data != NULL) 80159248Srwatson OPENSSL_free(a->data); 81159248Srwatson if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL) 82159248Srwatson memset((char *)a->data,0,sizeof(long)+1); 83159248Srwatson } 84168777Srwatson if (a->data == NULL) 85159248Srwatson { 86159248Srwatson ASN1err(ASN1_F_ASN1_ENUMERATED_SET,ERR_R_MALLOC_FAILURE); 87159248Srwatson return(0); 88159248Srwatson } 89159248Srwatson d=v; 90159248Srwatson if (d < 0) 91168777Srwatson { 92159248Srwatson d= -d; 93159248Srwatson a->type=V_ASN1_NEG_ENUMERATED; 94168777Srwatson } 95159248Srwatson 96159248Srwatson for (i=0; i<sizeof(long); i++) 97159248Srwatson { 98159248Srwatson if (d == 0) break; 99159248Srwatson buf[i]=(int)d&0xff; 100168777Srwatson d>>=8; 101159248Srwatson } 102168777Srwatson j=0; 103159248Srwatson for (k=i-1; k >=0; k--) 104168777Srwatson a->data[j++]=buf[k]; 105168777Srwatson a->length=j; 106159248Srwatson return(1); 107168777Srwatson } 108159248Srwatson 109159248Srwatsonlong ASN1_ENUMERATED_get(ASN1_ENUMERATED *a) 110159248Srwatson { 111159248Srwatson int neg=0,i; 112159248Srwatson long r=0; 113159248Srwatson 114159248Srwatson if (a == NULL) return(0L); 115159248Srwatson i=a->type; 116159248Srwatson if (i == V_ASN1_NEG_ENUMERATED) 117159248Srwatson neg=1; 118159248Srwatson else if (i != V_ASN1_ENUMERATED) 119159248Srwatson return -1; 120159248Srwatson 121159248Srwatson if (a->length > (int)sizeof(long)) 122159248Srwatson { 123159248Srwatson /* hmm... a bit ugly */ 124159248Srwatson return(0xffffffffL); 125159248Srwatson } 126162503Srwatson if (a->data == NULL) 127159248Srwatson return 0; 128168777Srwatson 129168777Srwatson for (i=0; i<a->length; i++) 130168777Srwatson { 131168777Srwatson r<<=8; 132168777Srwatson r|=(unsigned char)a->data[i]; 133159248Srwatson } 134168777Srwatson if (neg) r= -r; 135168777Srwatson return(r); 136168777Srwatson } 137168777Srwatson 138168777SrwatsonASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(BIGNUM *bn, ASN1_ENUMERATED *ai) 139168777Srwatson { 140168777Srwatson ASN1_ENUMERATED *ret; 141159248Srwatson int len,j; 142159248Srwatson 143159248Srwatson if (ai == NULL) 144159248Srwatson ret=M_ASN1_ENUMERATED_new(); 145159248Srwatson else 146159248Srwatson ret=ai; 147159248Srwatson if (ret == NULL) 148159248Srwatson { 149159248Srwatson ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED,ERR_R_NESTED_ASN1_ERROR); 150159248Srwatson goto err; 151159248Srwatson } 152159248Srwatson if(BN_is_negative(bn)) ret->type = V_ASN1_NEG_ENUMERATED; 153159248Srwatson else ret->type=V_ASN1_ENUMERATED; 154159248Srwatson j=BN_num_bits(bn); 155159248Srwatson len=((j == 0)?0:((j/8)+1)); 156159248Srwatson if (ret->length < len+4) 157159248Srwatson { 158159248Srwatson unsigned char *new_data=OPENSSL_realloc(ret->data, len+4); 159 if (!new_data) 160 { 161 ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE); 162 goto err; 163 } 164 ret->data=new_data; 165 } 166 167 ret->length=BN_bn2bin(bn,ret->data); 168 return(ret); 169err: 170 if (ret != ai) M_ASN1_ENUMERATED_free(ret); 171 return(NULL); 172 } 173 174BIGNUM *ASN1_ENUMERATED_to_BN(ASN1_ENUMERATED *ai, BIGNUM *bn) 175 { 176 BIGNUM *ret; 177 178 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) 179 ASN1err(ASN1_F_ASN1_ENUMERATED_TO_BN,ASN1_R_BN_LIB); 180 else if(ai->type == V_ASN1_NEG_ENUMERATED) BN_set_negative(ret,1); 181 return(ret); 182 } 183