f_string.c revision 55714
155714Skris/* crypto/asn1/f_string.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/buffer.h>
6255714Skris#include <openssl/asn1.h>
6355714Skris
6455714Skrisint i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type)
6555714Skris	{
6655714Skris	int i,n=0;
6755714Skris	static const char *h="0123456789ABCDEF";
6855714Skris	char buf[2];
6955714Skris
7055714Skris	if (a == NULL) return(0);
7155714Skris
7255714Skris	if (a->length == 0)
7355714Skris		{
7455714Skris		if (BIO_write(bp,"0",1) != 1) goto err;
7555714Skris		n=1;
7655714Skris		}
7755714Skris	else
7855714Skris		{
7955714Skris		for (i=0; i<a->length; i++)
8055714Skris			{
8155714Skris			if ((i != 0) && (i%35 == 0))
8255714Skris				{
8355714Skris				if (BIO_write(bp,"\\\n",2) != 2) goto err;
8455714Skris				n+=2;
8555714Skris				}
8655714Skris			buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f];
8755714Skris			buf[1]=h[((unsigned char)a->data[i]   )&0x0f];
8855714Skris			if (BIO_write(bp,buf,2) != 2) goto err;
8955714Skris			n+=2;
9055714Skris			}
9155714Skris		}
9255714Skris	return(n);
9355714Skriserr:
9455714Skris	return(-1);
9555714Skris	}
9655714Skris
9755714Skrisint a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
9855714Skris	{
9955714Skris	int ret=0;
10055714Skris	int i,j,k,m,n,again,bufsize;
10155714Skris	unsigned char *s=NULL,*sp;
10255714Skris	unsigned char *bufp;
10355714Skris	int num=0,slen=0,first=1;
10455714Skris
10555714Skris	bufsize=BIO_gets(bp,buf,size);
10655714Skris	for (;;)
10755714Skris		{
10855714Skris		if (bufsize < 1)
10955714Skris			{
11055714Skris			if (first)
11155714Skris				break;
11255714Skris			else
11355714Skris				goto err_sl;
11455714Skris			}
11555714Skris		first=0;
11655714Skris
11755714Skris		i=bufsize;
11855714Skris		if (buf[i-1] == '\n') buf[--i]='\0';
11955714Skris		if (i == 0) goto err_sl;
12055714Skris		if (buf[i-1] == '\r') buf[--i]='\0';
12155714Skris		if (i == 0) goto err_sl;
12255714Skris		again=(buf[i-1] == '\\');
12355714Skris
12455714Skris		for (j=i-1; j>0; j--)
12555714Skris			{
12655714Skris#ifndef CHARSET_EBCDIC
12755714Skris			if (!(	((buf[j] >= '0') && (buf[j] <= '9')) ||
12855714Skris				((buf[j] >= 'a') && (buf[j] <= 'f')) ||
12955714Skris				((buf[j] >= 'A') && (buf[j] <= 'F'))))
13055714Skris#else
13155714Skris			/* This #ifdef is not strictly necessary, since
13255714Skris			 * the characters A...F a...f 0...9 are contiguous
13355714Skris			 * (yes, even in EBCDIC - but not the whole alphabet).
13455714Skris			 * Nevertheless, isxdigit() is faster.
13555714Skris			 */
13655714Skris			if (!isxdigit(buf[j]))
13755714Skris#endif
13855714Skris				{
13955714Skris				i=j;
14055714Skris				break;
14155714Skris				}
14255714Skris			}
14355714Skris		buf[i]='\0';
14455714Skris		/* We have now cleared all the crap off the end of the
14555714Skris		 * line */
14655714Skris		if (i < 2) goto err_sl;
14755714Skris
14855714Skris		bufp=(unsigned char *)buf;
14955714Skris
15055714Skris		k=0;
15155714Skris		i-=again;
15255714Skris		if (i%2 != 0)
15355714Skris			{
15455714Skris			ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS);
15555714Skris			goto err;
15655714Skris			}
15755714Skris		i/=2;
15855714Skris		if (num+i > slen)
15955714Skris			{
16055714Skris			if (s == NULL)
16155714Skris				sp=(unsigned char *)Malloc(
16255714Skris					(unsigned int)num+i*2);
16355714Skris			else
16455714Skris				sp=(unsigned char *)Realloc(s,
16555714Skris					(unsigned int)num+i*2);
16655714Skris			if (sp == NULL)
16755714Skris				{
16855714Skris				ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE);
16955714Skris				if (s != NULL) Free((char *)s);
17055714Skris				goto err;
17155714Skris				}
17255714Skris			s=sp;
17355714Skris			slen=num+i*2;
17455714Skris			}
17555714Skris		for (j=0; j<i; j++,k+=2)
17655714Skris			{
17755714Skris			for (n=0; n<2; n++)
17855714Skris				{
17955714Skris				m=bufp[k+n];
18055714Skris				if ((m >= '0') && (m <= '9'))
18155714Skris					m-='0';
18255714Skris				else if ((m >= 'a') && (m <= 'f'))
18355714Skris					m=m-'a'+10;
18455714Skris				else if ((m >= 'A') && (m <= 'F'))
18555714Skris					m=m-'A'+10;
18655714Skris				else
18755714Skris					{
18855714Skris					ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS);
18955714Skris					goto err;
19055714Skris					}
19155714Skris				s[num+j]<<=4;
19255714Skris				s[num+j]|=m;
19355714Skris				}
19455714Skris			}
19555714Skris		num+=i;
19655714Skris		if (again)
19755714Skris			bufsize=BIO_gets(bp,buf,size);
19855714Skris		else
19955714Skris			break;
20055714Skris		}
20155714Skris	bs->length=num;
20255714Skris	bs->data=s;
20355714Skris	ret=1;
20455714Skriserr:
20555714Skris	if (0)
20655714Skris		{
20755714Skriserr_sl:
20855714Skris		ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE);
20955714Skris		}
21055714Skris	return(ret);
21155714Skris	}
21255714Skris
213