f_string.c revision 55714
1139823Simp/* crypto/asn1/f_string.c */ 21541Srgrimes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 31541Srgrimes * All rights reserved. 41541Srgrimes * 51541Srgrimes * This package is an SSL implementation written 61541Srgrimes * by Eric Young (eay@cryptsoft.com). 71541Srgrimes * The implementation was written so as to conform with Netscapes SSL. 81541Srgrimes * 91541Srgrimes * This library is free for commercial and non-commercial use as long as 101541Srgrimes * the following conditions are aheared to. The following conditions 111541Srgrimes * apply to all code found in this distribution, be it the RC4, RSA, 121541Srgrimes * lhash, DES, etc., code; not just the SSL code. The SSL documentation 131541Srgrimes * included with this distribution is covered by the same copyright terms 141541Srgrimes * except that the holder is Tim Hudson (tjh@cryptsoft.com). 151541Srgrimes * 161541Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in 171541Srgrimes * the code are not to be removed. 181541Srgrimes * If this package is used in a product, Eric Young should be given attribution 191541Srgrimes * as the author of the parts of the library used. 201541Srgrimes * This can be in the form of a textual message at program startup or 211541Srgrimes * in documentation (online or textual) provided with the package. 221541Srgrimes * 231541Srgrimes * Redistribution and use in source and binary forms, with or without 241541Srgrimes * modification, are permitted provided that the following conditions 251541Srgrimes * are met: 261541Srgrimes * 1. Redistributions of source code must retain the copyright 271541Srgrimes * notice, this list of conditions and the following disclaimer. 281541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 291541Srgrimes * notice, this list of conditions and the following disclaimer in the 301541Srgrimes * documentation and/or other materials provided with the distribution. 311541Srgrimes * 3. All advertising materials mentioning features or use of this software 3236503Speter * must display the following acknowledgement: 331541Srgrimes * "This product includes cryptographic software written by 341541Srgrimes * Eric Young (eay@cryptsoft.com)" 3583651Speter * The word 'cryptographic' can be left out if the rouines from the library 3683651Speter * being used are not cryptographic related :-). 3783651Speter * 4. If you include any Windows specific code (or a derivative thereof) from 381541Srgrimes * the apps directory (application code) you must include an acknowledgement: 391541Srgrimes * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 401541Srgrimes * 411541Srgrimes * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 421541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4383651Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44100134Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45100134Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 461541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4748274Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4848274Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4960041Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5031886Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 511541Srgrimes * SUCH DAMAGE. 521541Srgrimes * 531541Srgrimes * The licence and distribution terms for any publically available version or 541541Srgrimes * derivative of this code cannot be changed. i.e. this code cannot simply be 551541Srgrimes * copied and put under another distribution licence 56150634Sjhb * [including the GNU Public Licence.] 571541Srgrimes */ 581541Srgrimes 599336Sdfr#include <stdio.h> 6083700Speter#include "cryptlib.h" 612997Swollman#include <openssl/buffer.h> 622997Swollman#include <openssl/asn1.h> 6383651Speter 641541Srgrimesint i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type) 653305Sphk { 6612662Sdg int i,n=0; 6712662Sdg static const char *h="0123456789ABCDEF"; 6892783Sjeff char buf[2]; 693305Sphk 70195202Sdfr if (a == NULL) return(0); 71195202Sdfr 729336Sdfr if (a->length == 0) 7383651Speter { 741541Srgrimes if (BIO_write(bp,"0",1) != 1) goto err; 7583651Speter n=1; 761541Srgrimes } 771541Srgrimes else 781541Srgrimes { 791541Srgrimes for (i=0; i<a->length; i++) 801541Srgrimes { 811541Srgrimes if ((i != 0) && (i%35 == 0)) 821541Srgrimes { 8389094Smsmith if (BIO_write(bp,"\\\n",2) != 2) goto err; 84195202Sdfr n+=2; 851541Srgrimes } 861541Srgrimes buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; 87129639Srwatson buf[1]=h[((unsigned char)a->data[i] )&0x0f]; 88129639Srwatson if (BIO_write(bp,buf,2) != 2) goto err; 8983651Speter n+=2; 9083651Speter } 9112911Sphk } 9289094Smsmith return(n); 939336Sdfrerr: 94129639Srwatson return(-1); 95129639Srwatson } 969336Sdfr 979336Sdfrint a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size) 989336Sdfr { 99129639Srwatson int ret=0; 1009336Sdfr int i,j,k,m,n,again,bufsize; 1019336Sdfr unsigned char *s=NULL,*sp; 1029336Sdfr unsigned char *bufp; 1039336Sdfr int num=0,slen=0,first=1; 1049336Sdfr 1059336Sdfr bufsize=BIO_gets(bp,buf,size); 1069336Sdfr for (;;) 1079336Sdfr { 1089336Sdfr if (bufsize < 1) 1099336Sdfr { 1109336Sdfr if (first) 1119336Sdfr break; 1129336Sdfr else 1139336Sdfr goto err_sl; 1149336Sdfr } 1159336Sdfr first=0; 1169336Sdfr 1179336Sdfr i=bufsize; 1189336Sdfr if (buf[i-1] == '\n') buf[--i]='\0'; 1199336Sdfr if (i == 0) goto err_sl; 1209336Sdfr if (buf[i-1] == '\r') buf[--i]='\0'; 1219336Sdfr if (i == 0) goto err_sl; 1229336Sdfr again=(buf[i-1] == '\\'); 1239336Sdfr 1249336Sdfr for (j=i-1; j>0; j--) 1259336Sdfr { 1269336Sdfr#ifndef CHARSET_EBCDIC 1279336Sdfr if (!( ((buf[j] >= '0') && (buf[j] <= '9')) || 128129639Srwatson ((buf[j] >= 'a') && (buf[j] <= 'f')) || 1299336Sdfr ((buf[j] >= 'A') && (buf[j] <= 'F')))) 1309336Sdfr#else 1319336Sdfr /* This #ifdef is not strictly necessary, since 1329336Sdfr * the characters A...F a...f 0...9 are contiguous 1339336Sdfr * (yes, even in EBCDIC - but not the whole alphabet). 1349336Sdfr * Nevertheless, isxdigit() is faster. 1359336Sdfr */ 1369336Sdfr if (!isxdigit(buf[j])) 1379336Sdfr#endif 1389336Sdfr { 1399336Sdfr i=j; 1409336Sdfr break; 1419336Sdfr } 1429336Sdfr } 1439336Sdfr buf[i]='\0'; 1449336Sdfr /* We have now cleared all the crap off the end of the 1459336Sdfr * line */ 1469336Sdfr if (i < 2) goto err_sl; 1479336Sdfr 1489336Sdfr bufp=(unsigned char *)buf; 1499336Sdfr 1509336Sdfr k=0; 1519336Sdfr i-=again; 1529336Sdfr if (i%2 != 0) 1539336Sdfr { 1549336Sdfr ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS); 1559336Sdfr goto err; 156102236Sphk } 157102236Sphk i/=2; 1589336Sdfr if (num+i > slen) 159129639Srwatson { 160102236Sphk if (s == NULL) 161102236Sphk sp=(unsigned char *)Malloc( 162102236Sphk (unsigned int)num+i*2); 163102236Sphk else 164102236Sphk sp=(unsigned char *)Realloc(s, 165102236Sphk (unsigned int)num+i*2); 166102236Sphk if (sp == NULL) 167102236Sphk { 168102236Sphk ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE); 169102236Sphk if (s != NULL) Free((char *)s); 170102236Sphk goto err; 171102236Sphk } 172102236Sphk s=sp; 173102236Sphk slen=num+i*2; 174102236Sphk } 1759336Sdfr for (j=0; j<i; j++,k+=2) 1769336Sdfr { 1779336Sdfr for (n=0; n<2; n++) 1789336Sdfr { 1799336Sdfr m=bufp[k+n]; 1809336Sdfr if ((m >= '0') && (m <= '9')) 1819336Sdfr m-='0'; 1829336Sdfr else if ((m >= 'a') && (m <= 'f')) 1839336Sdfr m=m-'a'+10; 1849336Sdfr else if ((m >= 'A') && (m <= 'F')) 1859336Sdfr m=m-'A'+10; 1869336Sdfr else 1879336Sdfr { 188129639Srwatson ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS); 1899336Sdfr goto err; 1909336Sdfr } 1919336Sdfr s[num+j]<<=4; 1929336Sdfr s[num+j]|=m; 193129639Srwatson } 1949336Sdfr } 1959336Sdfr num+=i; 1969336Sdfr if (again) 1979336Sdfr bufsize=BIO_gets(bp,buf,size); 1989336Sdfr else 1999336Sdfr break; 2009336Sdfr } 2019336Sdfr bs->length=num; 202129639Srwatson bs->data=s; 2039336Sdfr ret=1; 2049336Sdfrerr: 2059336Sdfr if (0) 2069336Sdfr { 2079336Sdfrerr_sl: 2089336Sdfr ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE); 2099336Sdfr } 2109336Sdfr return(ret); 2119336Sdfr } 2129336Sdfr 2139336Sdfr