a_print.c revision 59191
134840Sjlemon/* crypto/asn1/a_print.c */
234840Sjlemon/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
334840Sjlemon * All rights reserved.
434840Sjlemon *
534840Sjlemon * This package is an SSL implementation written
634840Sjlemon * by Eric Young (eay@cryptsoft.com).
734840Sjlemon * The implementation was written so as to conform with Netscapes SSL.
834840Sjlemon *
934840Sjlemon * This library is free for commercial and non-commercial use as long as
1034840Sjlemon * the following conditions are aheared to.  The following conditions
1134840Sjlemon * apply to all code found in this distribution, be it the RC4, RSA,
1234840Sjlemon * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1334840Sjlemon * included with this distribution is covered by the same copyright terms
1434840Sjlemon * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1534840Sjlemon *
1634840Sjlemon * Copyright remains Eric Young's, and as such any Copyright notices in
1734840Sjlemon * the code are not to be removed.
1834840Sjlemon * If this package is used in a product, Eric Young should be given attribution
1934840Sjlemon * as the author of the parts of the library used.
2034840Sjlemon * This can be in the form of a textual message at program startup or
2134840Sjlemon * in documentation (online or textual) provided with the package.
2234840Sjlemon *
2334840Sjlemon * Redistribution and use in source and binary forms, with or without
2434840Sjlemon * modification, are permitted provided that the following conditions
2534840Sjlemon * are met:
2647080Sluoqi * 1. Redistributions of source code must retain the copyright
2734840Sjlemon *    notice, this list of conditions and the following disclaimer.
2834840Sjlemon * 2. Redistributions in binary form must reproduce the above copyright
2934840Sjlemon *    notice, this list of conditions and the following disclaimer in the
3034840Sjlemon *    documentation and/or other materials provided with the distribution.
3134840Sjlemon * 3. All advertising materials mentioning features or use of this software
3234840Sjlemon *    must display the following acknowledgement:
3334840Sjlemon *    "This product includes cryptographic software written by
3434840Sjlemon *     Eric Young (eay@cryptsoft.com)"
3534840Sjlemon *    The word 'cryptographic' can be left out if the rouines from the library
3637889Sjlemon *    being used are not cryptographic related :-).
3737889Sjlemon * 4. If you include any Windows specific code (or a derivative thereof) from
3837889Sjlemon *    the apps directory (application code) you must include an acknowledgement:
3937889Sjlemon *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4037889Sjlemon *
4146129Sluoqi * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4246129Sluoqi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4337889Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4434840Sjlemon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4534840Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4634840Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4734840Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4834840Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4934840Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5034840Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5134840Sjlemon * SUCH DAMAGE.
5234840Sjlemon *
5334840Sjlemon * The licence and distribution terms for any publically available version or
5434840Sjlemon * derivative of this code cannot be changed.  i.e. this code cannot simply be
5534840Sjlemon * copied and put under another distribution licence
5634840Sjlemon * [including the GNU Public Licence.]
5734840Sjlemon */
5837889Sjlemon
5934840Sjlemon#include <stdio.h>
6037889Sjlemon#include "cryptlib.h"
6134840Sjlemon#include <openssl/asn1.h>
6234840Sjlemon
6334840SjlemonASN1_IA5STRING *ASN1_IA5STRING_new(void)
6434840Sjlemon{ return M_ASN1_IA5STRING_new();}
6534840Sjlemon
6634840Sjlemonvoid ASN1_IA5STRING_free(ASN1_IA5STRING *x)
6734840Sjlemon{ M_ASN1_IA5STRING_free(x);}
6834840Sjlemon
6934840Sjlemonint i2d_ASN1_IA5STRING(ASN1_IA5STRING *a, unsigned char **pp)
7034840Sjlemon	{ return(M_i2d_ASN1_IA5STRING(a,pp)); }
7134840Sjlemon
7234840SjlemonASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **a, unsigned char **pp,
7334840Sjlemon	     long l)
7439775Sjlemon	{ return(M_d2i_ASN1_IA5STRING(a,pp,l)); }
7534840Sjlemon
7634840SjlemonASN1_T61STRING *ASN1_T61STRING_new(void)
7739872Sjlemon{ return M_ASN1_T61STRING_new();}
7839872Sjlemon
7939046Syokotavoid ASN1_T61STRING_free(ASN1_T61STRING *x)
8034840Sjlemon{ M_ASN1_T61STRING_free(x);}
8134840Sjlemon
8239046SyokotaASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **a, unsigned char **pp,
8334840Sjlemon	     long l)
8439046Syokota	{ return(M_d2i_ASN1_T61STRING(a,pp,l)); }
8534840Sjlemon
8634840SjlemonASN1_PRINTABLESTRING *ASN1_PRINTABLESTRING_new(void)
8734840Sjlemon{ return M_ASN1_PRINTABLESTRING_new();}
8834840Sjlemon
8937889Sjlemonvoid ASN1_PRINTABLESTRING_free(ASN1_PRINTABLESTRING *x)
9034840Sjlemon{ M_ASN1_PRINTABLESTRING_free(x);}
9137889Sjlemon
9247080SluoqiASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(ASN1_PRINTABLESTRING **a,
9334840Sjlemon	     unsigned char **pp, long l)
9434840Sjlemon	{ return(M_d2i_ASN1_PRINTABLESTRING(a,pp,
9534840Sjlemon	     l)); }
9634840Sjlemon
9734840Sjlemonint i2d_ASN1_PRINTABLESTRING(ASN1_PRINTABLESTRING *a, unsigned char **pp)
9834840Sjlemon	{ return(M_i2d_ASN1_PRINTABLESTRING(a,pp)); }
9934840Sjlemon
10034840Sjlemonint i2d_ASN1_PRINTABLE(ASN1_STRING *a, unsigned char **pp)
10146129Sluoqi	{ return(M_i2d_ASN1_PRINTABLE(a,pp)); }
10234840Sjlemon
10334840SjlemonASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **a, unsigned char **pp,
10437889Sjlemon	     long l)
10534840Sjlemon	{ return(M_d2i_ASN1_PRINTABLE(a,pp,l)); }
10634840Sjlemon
10737889Sjlemonint ASN1_PRINTABLE_type(unsigned char *s, int len)
10834840Sjlemon	{
10934840Sjlemon	int c;
11034840Sjlemon	int ia5=0;
11134840Sjlemon	int t61=0;
11234840Sjlemon
11334840Sjlemon	if (len <= 0) len= -1;
11434840Sjlemon	if (s == NULL) return(V_ASN1_PRINTABLESTRING);
11534840Sjlemon
11634840Sjlemon	while ((*s) && (len-- != 0))
11734840Sjlemon		{
11834840Sjlemon		c= *(s++);
11934840Sjlemon#ifndef CHARSET_EBCDIC
12034840Sjlemon		if (!(	((c >= 'a') && (c <= 'z')) ||
12134840Sjlemon			((c >= 'A') && (c <= 'Z')) ||
12234840Sjlemon			(c == ' ') ||
12334840Sjlemon			((c >= '0') && (c <= '9')) ||
12434840Sjlemon			(c == ' ') || (c == '\'') ||
12534840Sjlemon			(c == '(') || (c == ')') ||
12637889Sjlemon			(c == '+') || (c == ',') ||
12734840Sjlemon			(c == '-') || (c == '.') ||
12837889Sjlemon			(c == '/') || (c == ':') ||
12934840Sjlemon			(c == '=') || (c == '?')))
13034840Sjlemon			ia5=1;
13134840Sjlemon		if (c&0x80)
13237889Sjlemon			t61=1;
13334840Sjlemon#else
13434840Sjlemon		if (!isalnum(c) && (c != ' ') &&
13534840Sjlemon		    strchr("'()+,-./:=?", c) == NULL)
13634840Sjlemon			ia5=1;
13734840Sjlemon		if (os_toascii[c] & 0x80)
13834840Sjlemon			t61=1;
13934840Sjlemon#endif
14034840Sjlemon		}
14134840Sjlemon	if (t61) return(V_ASN1_T61STRING);
14234840Sjlemon	if (ia5) return(V_ASN1_IA5STRING);
14334840Sjlemon	return(V_ASN1_PRINTABLESTRING);
14434840Sjlemon	}
14534840Sjlemon
14634840Sjlemonint ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
14734840Sjlemon	{
14834840Sjlemon	int i;
14934840Sjlemon	unsigned char *p;
15034840Sjlemon
15134840Sjlemon	if (s->type != V_ASN1_UNIVERSALSTRING) return(0);
15234840Sjlemon	if ((s->length%4) != 0) return(0);
15334840Sjlemon	p=s->data;
15434840Sjlemon	for (i=0; i<s->length; i+=4)
15534840Sjlemon		{
15634840Sjlemon		if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
15734840Sjlemon			break;
15834840Sjlemon		else
15934840Sjlemon			p+=4;
16034840Sjlemon		}
16134840Sjlemon	if (i < s->length) return(0);
16234840Sjlemon	p=s->data;
16334840Sjlemon	for (i=3; i<s->length; i+=4)
16437889Sjlemon		{
16547080Sluoqi		*(p++)=s->data[i];
16634840Sjlemon		}
16734840Sjlemon	*(p)='\0';
16834840Sjlemon	s->length/=4;
16934840Sjlemon	s->type=ASN1_PRINTABLE_type(s->data,s->length);
17037889Sjlemon	return(1);
17134840Sjlemon	}
17234840Sjlemon
17334840SjlemonASN1_STRING *DIRECTORYSTRING_new(void)
17434840Sjlemon{ return M_DIRECTORYSTRING_new();}
17534840Sjlemon
17634840Sjlemonvoid DIRECTORYSTRING_free(ASN1_STRING *x)
17734840Sjlemon{ M_DIRECTORYSTRING_free(x);}
17834840Sjlemon
17946129Sluoqiint i2d_DIRECTORYSTRING(ASN1_STRING *a, unsigned char **pp)
18034840Sjlemon	{ return(M_i2d_DIRECTORYSTRING(a,pp)); }
18137889Sjlemon
18234840SjlemonASN1_STRING *d2i_DIRECTORYSTRING(ASN1_STRING **a, unsigned char **pp,
18337889Sjlemon	     long l)
18434840Sjlemon	{ return(M_d2i_DIRECTORYSTRING(a,pp,l)); }
18534840Sjlemon
18634840SjlemonASN1_STRING *DISPLAYTEXT_new(void)
18734840Sjlemon{ return M_DISPLAYTEXT_new();}
18834840Sjlemon
18937889Sjlemonvoid DISPLAYTEXT_free(ASN1_STRING *x)
19034840Sjlemon{ M_DISPLAYTEXT_free(x);}
19134840Sjlemon
19234840Sjlemonint i2d_DISPLAYTEXT(ASN1_STRING *a, unsigned char **pp)
19334840Sjlemon	{ return(M_i2d_DISPLAYTEXT(a,pp)); }
19434840Sjlemon
19534840SjlemonASN1_STRING *d2i_DISPLAYTEXT(ASN1_STRING **a, unsigned char **pp,
19634840Sjlemon	     long l)
19734840Sjlemon	{ return(M_d2i_DISPLAYTEXT(a,pp,l)); }
198