From d02b48c63a58ea4367a0e905979f140b7d090f86 Mon Sep 17 00:00:00 2001 From: "Ralf S. Engelschall" Date: Mon, 21 Dec 1998 10:52:47 +0000 Subject: Import of old SSLeay release: SSLeay 0.8.1b --- crypto/asn1/Makefile.ssl | 118 +++++++ crypto/asn1/a_bitstr.c | 158 ++++++++++ crypto/asn1/a_bool.c | 121 ++++++++ crypto/asn1/a_bytes.c | 337 ++++++++++++++++++++ crypto/asn1/a_d2i_fp.c | 200 ++++++++++++ crypto/asn1/a_digest.c | 91 ++++++ crypto/asn1/a_dup.c | 86 ++++++ crypto/asn1/a_hdr.c | 130 ++++++++ crypto/asn1/a_i2d_fp.c | 119 +++++++ crypto/asn1/a_int.c | 304 ++++++++++++++++++ crypto/asn1/a_meth.c | 84 +++++ crypto/asn1/a_object.c | 369 ++++++++++++++++++++++ crypto/asn1/a_octet.c | 90 ++++++ crypto/asn1/a_print.c | 161 ++++++++++ crypto/asn1/a_set.c | 149 +++++++++ crypto/asn1/a_sign.c | 147 +++++++++ crypto/asn1/a_type.c | 317 +++++++++++++++++++ crypto/asn1/a_utctm.c | 190 ++++++++++++ crypto/asn1/a_verify.c | 121 ++++++++ crypto/asn1/asn1.err | 178 +++++++++++ crypto/asn1/asn1.h | 786 +++++++++++++++++++++++++++++++++++++++++++++++ crypto/asn1/asn1_err.c | 256 +++++++++++++++ crypto/asn1/asn1_lib.c | 433 ++++++++++++++++++++++++++ crypto/asn1/asn1_mac.h | 318 +++++++++++++++++++ crypto/asn1/asn1_par.c | 387 +++++++++++++++++++++++ crypto/asn1/d2i_dhp.c | 108 +++++++ crypto/asn1/d2i_dsap.c | 101 ++++++ crypto/asn1/d2i_pr.c | 117 +++++++ crypto/asn1/d2i_pu.c | 117 +++++++ crypto/asn1/d2i_r_pr.c | 129 ++++++++ crypto/asn1/d2i_r_pu.c | 100 ++++++ crypto/asn1/d2i_s_pr.c | 113 +++++++ crypto/asn1/d2i_s_pu.c | 124 ++++++++ crypto/asn1/f_int.c | 211 +++++++++++++ crypto/asn1/f_string.c | 210 +++++++++++++ crypto/asn1/i2d_dhp.c | 128 ++++++++ crypto/asn1/i2d_dsap.c | 121 ++++++++ crypto/asn1/i2d_pr.c | 86 ++++++ crypto/asn1/i2d_pu.c | 84 +++++ crypto/asn1/i2d_r_pr.c | 132 ++++++++ crypto/asn1/i2d_r_pu.c | 118 +++++++ crypto/asn1/i2d_s_pr.c | 128 ++++++++ crypto/asn1/i2d_s_pu.c | 133 ++++++++ crypto/asn1/n_pkey.c | 364 ++++++++++++++++++++++ crypto/asn1/p7_dgst.c | 130 ++++++++ crypto/asn1/p7_enc.c | 120 ++++++++ crypto/asn1/p7_enc_c.c | 127 ++++++++ crypto/asn1/p7_evp.c | 125 ++++++++ crypto/asn1/p7_i_s.c | 120 ++++++++ crypto/asn1/p7_lib.c | 299 ++++++++++++++++++ crypto/asn1/p7_recip.c | 130 ++++++++ crypto/asn1/p7_s_e.c | 146 +++++++++ crypto/asn1/p7_signd.c | 140 +++++++++ crypto/asn1/p7_signi.c | 149 +++++++++ crypto/asn1/pk.c | 117 +++++++ crypto/asn1/pkcs8.c | 139 +++++++++ crypto/asn1/t_pkey.c | 389 +++++++++++++++++++++++ crypto/asn1/t_req.c | 226 ++++++++++++++ crypto/asn1/t_x509.c | 386 +++++++++++++++++++++++ crypto/asn1/test.c | 253 +++++++++++++++ crypto/asn1/x_algor.c | 126 ++++++++ crypto/asn1/x_attrib.c | 152 +++++++++ crypto/asn1/x_cinf.c | 197 ++++++++++++ crypto/asn1/x_crl.c | 347 +++++++++++++++++++++ crypto/asn1/x_exten.c | 147 +++++++++ crypto/asn1/x_info.c | 108 +++++++ crypto/asn1/x_name.c | 295 ++++++++++++++++++ crypto/asn1/x_pkey.c | 153 +++++++++ crypto/asn1/x_pubkey.c | 252 +++++++++++++++ crypto/asn1/x_req.c | 244 +++++++++++++++ crypto/asn1/x_sig.c | 119 +++++++ crypto/asn1/x_spki.c | 181 +++++++++++ crypto/asn1/x_val.c | 118 +++++++ crypto/asn1/x_x509.c | 155 ++++++++++ 74 files changed, 13864 insertions(+) create mode 100644 crypto/asn1/Makefile.ssl create mode 100644 crypto/asn1/a_bitstr.c create mode 100644 crypto/asn1/a_bool.c create mode 100644 crypto/asn1/a_bytes.c create mode 100644 crypto/asn1/a_d2i_fp.c create mode 100644 crypto/asn1/a_digest.c create mode 100644 crypto/asn1/a_dup.c create mode 100644 crypto/asn1/a_hdr.c create mode 100644 crypto/asn1/a_i2d_fp.c create mode 100644 crypto/asn1/a_int.c create mode 100644 crypto/asn1/a_meth.c create mode 100644 crypto/asn1/a_object.c create mode 100644 crypto/asn1/a_octet.c create mode 100644 crypto/asn1/a_print.c create mode 100644 crypto/asn1/a_set.c create mode 100644 crypto/asn1/a_sign.c create mode 100644 crypto/asn1/a_type.c create mode 100644 crypto/asn1/a_utctm.c create mode 100644 crypto/asn1/a_verify.c create mode 100644 crypto/asn1/asn1.err create mode 100644 crypto/asn1/asn1.h create mode 100644 crypto/asn1/asn1_err.c create mode 100644 crypto/asn1/asn1_lib.c create mode 100644 crypto/asn1/asn1_mac.h create mode 100644 crypto/asn1/asn1_par.c create mode 100644 crypto/asn1/d2i_dhp.c create mode 100644 crypto/asn1/d2i_dsap.c create mode 100644 crypto/asn1/d2i_pr.c create mode 100644 crypto/asn1/d2i_pu.c create mode 100644 crypto/asn1/d2i_r_pr.c create mode 100644 crypto/asn1/d2i_r_pu.c create mode 100644 crypto/asn1/d2i_s_pr.c create mode 100644 crypto/asn1/d2i_s_pu.c create mode 100644 crypto/asn1/f_int.c create mode 100644 crypto/asn1/f_string.c create mode 100644 crypto/asn1/i2d_dhp.c create mode 100644 crypto/asn1/i2d_dsap.c create mode 100644 crypto/asn1/i2d_pr.c create mode 100644 crypto/asn1/i2d_pu.c create mode 100644 crypto/asn1/i2d_r_pr.c create mode 100644 crypto/asn1/i2d_r_pu.c create mode 100644 crypto/asn1/i2d_s_pr.c create mode 100644 crypto/asn1/i2d_s_pu.c create mode 100644 crypto/asn1/n_pkey.c create mode 100644 crypto/asn1/p7_dgst.c create mode 100644 crypto/asn1/p7_enc.c create mode 100644 crypto/asn1/p7_enc_c.c create mode 100644 crypto/asn1/p7_evp.c create mode 100644 crypto/asn1/p7_i_s.c create mode 100644 crypto/asn1/p7_lib.c create mode 100644 crypto/asn1/p7_recip.c create mode 100644 crypto/asn1/p7_s_e.c create mode 100644 crypto/asn1/p7_signd.c create mode 100644 crypto/asn1/p7_signi.c create mode 100644 crypto/asn1/pk.c create mode 100644 crypto/asn1/pkcs8.c create mode 100644 crypto/asn1/t_pkey.c create mode 100644 crypto/asn1/t_req.c create mode 100644 crypto/asn1/t_x509.c create mode 100644 crypto/asn1/test.c create mode 100644 crypto/asn1/x_algor.c create mode 100644 crypto/asn1/x_attrib.c create mode 100644 crypto/asn1/x_cinf.c create mode 100644 crypto/asn1/x_crl.c create mode 100644 crypto/asn1/x_exten.c create mode 100644 crypto/asn1/x_info.c create mode 100644 crypto/asn1/x_name.c create mode 100644 crypto/asn1/x_pkey.c create mode 100644 crypto/asn1/x_pubkey.c create mode 100644 crypto/asn1/x_req.c create mode 100644 crypto/asn1/x_sig.c create mode 100644 crypto/asn1/x_spki.c create mode 100644 crypto/asn1/x_val.c create mode 100644 crypto/asn1/x_x509.c (limited to 'crypto/asn1') diff --git a/crypto/asn1/Makefile.ssl b/crypto/asn1/Makefile.ssl new file mode 100644 index 0000000000..a39e009d27 --- /dev/null +++ b/crypto/asn1/Makefile.ssl @@ -0,0 +1,118 @@ +# +# SSLeay/crypto/asn1/Makefile +# + +DIR= asn1 +TOP= ../.. +CC= cc +INCLUDES= -I.. -I../../include +CFLAG=-g +INSTALLTOP=/usr/local/ssl +MAKE= make -f Makefile.ssl +MAKEDEPEND= makedepend -f Makefile.ssl +MAKEFILE= Makefile.ssl +AR= ar r + +CFLAGS= $(INCLUDES) $(CFLAG) + +ERR=asn1 +ERRC=asn1_err +GENERAL=Makefile README +TEST= +APPS= + +LIB=$(TOP)/libcrypto.a +LIBSRC= a_object.c a_bitstr.c a_utctm.c a_int.c a_octet.c a_print.c \ + a_type.c a_set.c a_dup.c a_d2i_fp.c a_i2d_fp.c \ + a_sign.c a_digest.c a_verify.c \ + x_algor.c x_val.c x_pubkey.c x_sig.c x_req.c x_attrib.c \ + x_name.c x_cinf.c x_x509.c x_crl.c x_info.c x_spki.c \ + d2i_r_pr.c i2d_r_pr.c d2i_r_pu.c i2d_r_pu.c \ + d2i_s_pr.c i2d_s_pr.c d2i_s_pu.c i2d_s_pu.c \ + d2i_pu.c d2i_pr.c i2d_pu.c i2d_pr.c\ + t_req.c t_x509.c t_pkey.c \ + p7_i_s.c p7_signi.c p7_signd.c p7_recip.c p7_enc_c.c p7_evp.c \ + p7_dgst.c p7_s_e.c p7_enc.c p7_lib.c \ + f_int.c f_string.c i2d_dhp.c i2d_dsap.c d2i_dhp.c d2i_dsap.c n_pkey.c \ + a_hdr.c x_pkey.c a_bool.c x_exten.c \ + asn1_par.c asn1_lib.c $(ERRC).c a_meth.c a_bytes.c +LIBOBJ= a_object.o a_bitstr.o a_utctm.o a_int.o a_octet.o a_print.o \ + a_type.o a_set.o a_dup.o a_d2i_fp.o a_i2d_fp.o \ + a_sign.o a_digest.o a_verify.o \ + x_algor.o x_val.o x_pubkey.o x_sig.o x_req.o x_attrib.o \ + x_name.o x_cinf.o x_x509.o x_crl.o x_info.o x_spki.o \ + d2i_r_pr.o i2d_r_pr.o d2i_r_pu.o i2d_r_pu.o \ + d2i_s_pr.o i2d_s_pr.o d2i_s_pu.o i2d_s_pu.o \ + d2i_pu.o d2i_pr.o i2d_pu.o i2d_pr.o \ + t_req.o t_x509.o t_pkey.o \ + p7_i_s.o p7_signi.o p7_signd.o p7_recip.o p7_enc_c.o p7_evp.o \ + p7_dgst.o p7_s_e.o p7_enc.o p7_lib.o \ + f_int.o f_string.o i2d_dhp.o i2d_dsap.o d2i_dhp.o d2i_dsap.o n_pkey.o \ + a_hdr.o x_pkey.o a_bool.o x_exten.o \ + asn1_par.o asn1_lib.o $(ERRC).o a_meth.o a_bytes.o + +SRC= $(LIBSRC) + +EXHEADER= asn1.h asn1_mac.h +HEADER= $(EXHEADER) + +ALL= $(GENERAL) $(SRC) $(HEADER) + +top: + (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) + +test: test.c + cc -g -I../../include -c test.c + cc -g -I../../include -o test test.o -L../.. -lcrypto + +pk: pk.c + cc -g -I../../include -c pk.c + cc -g -I../../include -o pk pk.o -L../.. -lcrypto + +all: lib + +lib: $(LIBOBJ) + $(AR) $(LIB) $(LIBOBJ) + sh $(TOP)/util/ranlib.sh $(LIB) + @touch lib + +files: + perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO + +links: + /bin/rm -f Makefile + $(TOP)/util/point.sh Makefile.ssl Makefile ; + $(TOP)/util/mklink.sh ../../include $(EXHEADER) + $(TOP)/util/mklink.sh ../../test $(TEST) + $(TOP)/util/mklink.sh ../../apps $(APPS) + +install: + @for i in $(EXHEADER) ; \ + do \ + (cp $$i $(INSTALLTOP)/include/$$i; \ + chmod 644 $(INSTALLTOP)/include/$$i ); \ + done; + +tags: + ctags $(SRC) + +tests: + +lint: + lint -DLINT $(INCLUDES) $(SRC)>fluff + +depend: + $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC) + +dclean: + perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new + mv -f Makefile.new $(MAKEFILE) + +clean: + /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff + +errors: + perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h + perl ../err/err_genc.pl $(ERR).h $(ERRC).c + +# DO NOT DELETE THIS LINE -- make depend depends on it. diff --git a/crypto/asn1/a_bitstr.c b/crypto/asn1/a_bitstr.c new file mode 100644 index 0000000000..eb99ebca2f --- /dev/null +++ b/crypto/asn1/a_bitstr.c @@ -0,0 +1,158 @@ +/* crypto/asn1/a_bitstr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_ASN1_STRING_NEW,ASN1_R_STRING_TOO_SHORT); + * ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,ASN1_R_EXPECTING_A_BIT_STRING); + */ + +int i2d_ASN1_BIT_STRING(a,pp) +ASN1_BIT_STRING *a; +unsigned char **pp; + { + int ret,j,r,bits; + unsigned char *p,*d; + + if (a == NULL) return(0); + + /* our bit strings are always a multiple of 8 :-) */ + bits=0; + ret=1+a->length; + r=ASN1_object_size(0,ret,V_ASN1_BIT_STRING); + if (pp == NULL) return(r); + p= *pp; + + ASN1_put_object(&p,0,ret,V_ASN1_BIT_STRING,V_ASN1_UNIVERSAL); + if (bits == 0) + j=0; + else j=8-bits; + *(p++)=(unsigned char)j; + d=a->data; + memcpy(p,d,a->length); + p+=a->length; + if (a->length > 0) p[-1]&=(0xff< 1) /* using one because of the bits left byte */ + { + s=(unsigned char *)Malloc((int)len); + if (s == NULL) + { + i=ERR_R_MALLOC_FAILURE; + goto err; + } + memcpy(s,p,(int)len); + s[len-1]&=(0xff<length=(int)len; + if (ret->data != NULL) Free((char *)ret->data); + ret->data=s; + ret->type=V_ASN1_BIT_STRING; + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + ASN1err(ASN1_F_D2I_ASN1_BIT_STRING,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_BIT_STRING_free(ret); + return(NULL); + } + diff --git a/crypto/asn1/a_bool.c b/crypto/asn1/a_bool.c new file mode 100644 index 0000000000..83607b58ef --- /dev/null +++ b/crypto/asn1/a_bool.c @@ -0,0 +1,121 @@ +/* crypto/asn1/a_bool.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_EXPECTING_A_BOOLEAN); + * ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,ASN1_R_BOOLEAN_IS_WRONG_LENGTH); + */ + +int i2d_ASN1_BOOLEAN(a,pp) +int a; +unsigned char **pp; + { + int r; + unsigned char *p; + + r=ASN1_object_size(0,1,V_ASN1_BOOLEAN); + if (pp == NULL) return(r); + p= *pp; + + ASN1_put_object(&p,0,1,V_ASN1_BOOLEAN,V_ASN1_UNIVERSAL); + *(p++)= (unsigned char)a; + *pp=p; + return(r); + } + +int d2i_ASN1_BOOLEAN(a, pp, length) +int *a; +unsigned char **pp; +long length; + { + int ret= -1; + unsigned char *p; + long len; + int inf,tag,xclass; + int i=0; + + p= *pp; + inf=ASN1_get_object(&p,&len,&tag,&xclass,length); + if (inf & 0x80) + { + i=ASN1_R_BAD_OBJECT_HEADER; + goto err; + } + + if (tag != V_ASN1_BOOLEAN) + { + i=ASN1_R_EXPECTING_A_BOOLEAN; + goto err; + } + + if (len != 1) + { + i=ASN1_R_BOOLEAN_IS_WRONG_LENGTH; + goto err; + } + ret= (int)*(p++); + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + ASN1err(ASN1_F_D2I_ASN1_BOOLEAN,i); + return(ret); + } diff --git a/crypto/asn1/a_bytes.c b/crypto/asn1/a_bytes.c new file mode 100644 index 0000000000..0c9132498c --- /dev/null +++ b/crypto/asn1/a_bytes.c @@ -0,0 +1,337 @@ +/* crypto/asn1/a_bytes.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK); + * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_ERROR_STACK); + * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_WRONG_TYPE); + * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG); + */ + +static unsigned long tag2bit[32]={ +0, 0, 0, 0, /* tags 0 - 3 */ +B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ +B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ +B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 12-15 */ +0, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, +B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,0, +0,B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, +B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN, + }; + +#ifndef NOPROTO +static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c); +#else +static int asn1_collate_primative(); +#endif + +/* type is a 'bitmap' of acceptable string types to be accepted. + */ +ASN1_STRING *d2i_ASN1_type_bytes(a, pp, length, type) +ASN1_STRING **a; +unsigned char **pp; +long length; +int type; + { + ASN1_STRING *ret=NULL; + unsigned char *p,*s; + long len; + int inf,tag,xclass; + int i=0; + + if ((a == NULL) || ((*a) == NULL)) + { + if ((ret=ASN1_STRING_new()) == NULL) return(NULL); + } + else + ret=(*a); + + p= *pp; + inf=ASN1_get_object(&p,&len,&tag,&xclass,length); + if (inf & 0x80) goto err; + + if (tag >= 32) + { + i=ASN1_R_TAG_VALUE_TOO_HIGH;; + goto err; + } + if (!(tag2bit[tag] & type)) + { + i=ASN1_R_WRONG_TYPE; + goto err; + } + if (len != 0) + { + s=(unsigned char *)Malloc((int)len+1); + if (s == NULL) + { + i=ERR_R_MALLOC_FAILURE; + goto err; + } + memcpy(s,p,(int)len); + s[len]='\0'; + p+=len; + } + else + s=NULL; + + if (ret->data != NULL) Free((char *)ret->data); + ret->length=(int)len; + ret->data=s; + ret->type=tag; + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_STRING_free(ret); + return(NULL); + } + +int i2d_ASN1_bytes(a, pp, tag, xclass) +ASN1_STRING *a; +unsigned char **pp; +int tag; +int xclass; + { + int ret,r,constructed; + unsigned char *p; + + if (a == NULL) return(0); + ret=a->length; + r=ASN1_object_size(0,ret,tag); + if (pp == NULL) return(r); + p= *pp; + + if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET)) + constructed=1; + else + constructed=0; + ASN1_put_object(&p,constructed,ret,tag,xclass); + memcpy(p,a->data,a->length); + p+=a->length; + *pp= p; + return(r); + } + +ASN1_STRING *d2i_ASN1_bytes(a, pp, length, Ptag, Pclass) +ASN1_STRING **a; +unsigned char **pp; +long length; +int Ptag; +int Pclass; + { + ASN1_STRING *ret=NULL; + unsigned char *p,*s; + long len; + int inf,tag,xclass; + int i=0; + + if ((a == NULL) || ((*a) == NULL)) + { + if ((ret=ASN1_STRING_new()) == NULL) return(NULL); + } + else + ret=(*a); + + p= *pp; + inf=ASN1_get_object(&p,&len,&tag,&xclass,length); + if (inf & 0x80) + { + i=ASN1_R_BAD_OBJECT_HEADER; + goto err; + } + + if (tag != Ptag) + { + i=ASN1_R_WRONG_TAG; + goto err; + } + + if (inf & V_ASN1_CONSTRUCTED) + { + ASN1_CTX c; + + c.pp=pp; + c.p=p; + c.inf=inf; + c.slen=len; + c.tag=Ptag; + c.xclass=Pclass; + c.max=(length == 0)?0:(p+length); + if (!asn1_collate_primative(ret,&c)) + goto err; + else + { + p=c.p; + } + } + else + { + if (len != 0) + { + if (ret->length < len) + { + if (ret->data != NULL) Free((char *)ret->data); + s=(unsigned char *)Malloc((int)len); + if (s == NULL) + { + i=ERR_R_MALLOC_FAILURE; + goto err; + } + } + else + s=ret->data; + memcpy(s,p,(int)len); + p+=len; + } + else + { + s=NULL; + if (ret->data != NULL) Free((char *)ret->data); + } + + ret->length=(int)len; + ret->data=s; + ret->type=Ptag; + } + + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_STRING_free(ret); + ASN1err(ASN1_F_D2I_ASN1_BYTES,i); + return(NULL); + } + + +/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapes + * them into the one struture that is then returned */ +/* There have been a few bug fixes for this function from + * Paul Keogh , many thanks to him */ +static int asn1_collate_primative(a,c) +ASN1_STRING *a; +ASN1_CTX *c; + { + ASN1_STRING *os=NULL; + BUF_MEM b; + int num; + + b.length=0; + b.max=0; + b.data=NULL; + + if (a == NULL) + { + c->error=ERR_R_PASSED_NULL_PARAMETER; + goto err; + } + + num=0; + for (;;) + { + if (c->inf & 1) + { + c->eos=ASN1_check_infinite_end(&c->p, + (long)(c->max-c->p)); + if (c->eos) break; + } + else + { + if (c->slen <= 0) break; + } + + c->q=c->p; + if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass) + == NULL) + { + c->error=ERR_R_ASN1_LIB; + goto err; + } + + if (!BUF_MEM_grow(&b,num+os->length)) + { + c->error=ERR_R_BUF_LIB; + goto err; + } + memcpy(&(b.data[num]),os->data,os->length); + if (!(c->inf & 1)) + c->slen-=(c->p-c->q); + num+=os->length; + } + + if (!asn1_Finish(c)) goto err; + + a->length=num; + if (a->data != NULL) Free(a->data); + a->data=(unsigned char *)b.data; + if (os != NULL) ASN1_STRING_free(os); + return(1); +err: + ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,c->error); + if (os != NULL) ASN1_STRING_free(os); + if (b.data != NULL) Free(b.data); + return(0); + } + diff --git a/crypto/asn1/a_d2i_fp.c b/crypto/asn1/a_d2i_fp.c new file mode 100644 index 0000000000..5c9c9cf09b --- /dev/null +++ b/crypto/asn1/a_d2i_fp.c @@ -0,0 +1,200 @@ +/* crypto/asn1/a_d2i_fp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "asn1_mac.h" + +#define HEADER_SIZE 8 + +#ifndef WIN16 +char *ASN1_d2i_fp(xnew,d2i,in,x) +char *(*xnew)(); +char *(*d2i)(); +FILE *in; +unsigned char **x; + { + BIO *b; + char *ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB); + return(NULL); + } + BIO_set_fp(b,in,BIO_NOCLOSE); + ret=ASN1_d2i_bio(xnew,d2i,b,x); + BIO_free(b); + return(ret); + } +#endif + +char *ASN1_d2i_bio(xnew,d2i,in,x) +char *(*xnew)(); +char *(*d2i)(); +BIO *in; +unsigned char **x; + { + BUF_MEM *b; + unsigned char *p; + int i; + char *ret=NULL; + ASN1_CTX c; + int want=HEADER_SIZE; + int eos=0; + int off=0; + int len=0; + + b=BUF_MEM_new(); + if (b == NULL) + { + ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE); + return(NULL); + } + + for (;;) + { + if (want >= (len-off)) + { + want-=(len-off); + + if (!BUF_MEM_grow(b,len+want)) + { + ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE); + goto err; + } + i=BIO_read(in,&(b->data[len]),want); + if ((i < 0) && ((len-off) == 0)) + { + ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA); + goto err; + } + if (i > 0) + len+=i; + } + /* else data already loaded */ + + p=(unsigned char *)&(b->data[off]); + c.p=p; + c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass), + len-off); + if (c.inf & 0x80) + { + unsigned long e; + + e=ERR_GET_REASON(ERR_peek_error()); + if (e != ASN1_R_TOO_LONG) + goto err; + else + ERR_get_error(); /* clear error */ + } + i=c.p-p;/* header length */ + off+=i; /* end of data */ + + if (c.inf & 1) + { + /* no data body so go round again */ + eos++; + want=HEADER_SIZE; + } + else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC)) + { + /* eos value, so go back and read another header */ + eos--; + if (eos <= 0) + break; + else + want=HEADER_SIZE; + } + else + { + /* suck in c.slen bytes of data */ + want=(int)c.slen; + if (want > (len-off)) + { + want-=(len-off); + if (!BUF_MEM_grow(b,len+want)) + { + ASN1err(ASN1_F_ASN1_D2I_BIO,ERR_R_MALLOC_FAILURE); + goto err; + } + i=BIO_read(in,&(b->data[len]),want); + if (i <= 0) + { + ASN1err(ASN1_F_ASN1_D2I_BIO,ASN1_R_NOT_ENOUGH_DATA); + goto err; + } + len+=i; + } + off+=(int)c.slen; + if (eos <= 0) + { + break; + } + else + want=HEADER_SIZE; + } + } + + p=(unsigned char *)b->data; + ret=d2i(x,&p,off); +err: + if (b != NULL) BUF_MEM_free(b); + return(ret); + } diff --git a/crypto/asn1/a_digest.c b/crypto/asn1/a_digest.c new file mode 100644 index 0000000000..8f395d958a --- /dev/null +++ b/crypto/asn1/a_digest.c @@ -0,0 +1,91 @@ +/* crypto/asn1/a_digest.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include +#include +#include + +#include "cryptlib.h" +#include "evp.h" +#include "x509.h" +#include "buffer.h" + +int ASN1_digest(i2d,type,data,md,len) +int (*i2d)(); +EVP_MD *type; +char *data; +unsigned char *md; +unsigned int *len; + { + EVP_MD_CTX ctx; + int i; + unsigned char *str,*p; + + i=i2d(data,NULL); + if ((str=(unsigned char *)Malloc(i)) == NULL) return(0); + p=str; + i2d(data,&p); + + EVP_DigestInit(&ctx,type); + EVP_DigestUpdate(&ctx,str,i); + EVP_DigestFinal(&ctx,md,len); + Free(str); + return(1); + } + diff --git a/crypto/asn1/a_dup.c b/crypto/asn1/a_dup.c new file mode 100644 index 0000000000..51ed105ff2 --- /dev/null +++ b/crypto/asn1/a_dup.c @@ -0,0 +1,86 @@ +/* crypto/asn1/a_dup.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +#define READ_CHUNK 2048 + +char *ASN1_dup(i2d,d2i,x) +int (*i2d)(); +char *(*d2i)(); +char *x; + { + unsigned char *b,*p; + long i; + char *ret; + + if (x == NULL) return(NULL); + + i=(long)i2d(x,NULL); + b=(unsigned char *)Malloc((unsigned int)i+10); + if (b == NULL) + { ASN1err(ASN1_F_ASN1_DUP,ERR_R_MALLOC_FAILURE); return(NULL); } + p= b; + i=i2d(x,&p); + p= b; + ret=d2i(NULL,&p,i); + Free((char *)b); + return(ret); + } diff --git a/crypto/asn1/a_hdr.c b/crypto/asn1/a_hdr.c new file mode 100644 index 0000000000..b7de335fc1 --- /dev/null +++ b/crypto/asn1/a_hdr.c @@ -0,0 +1,130 @@ +/* crypto/asn1/a_hdr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "asn1.h" + +/* + * ASN1err(ASN1_F_D2I_ASN1_HEADER,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_ASN1_HEADER,ASN1_R_BAD_GET_OBJECT); + * ASN1err(ASN1_F_I2D_ASN1_HEADER,ASN1_R_BAD_GET_OBJECT); + * ASN1err(ASN1_F_ASN1_HEADER_NEW,ASN1_R_BAD_GET_OBJECT); + */ + +int i2d_ASN1_HEADER(a,pp) +ASN1_HEADER *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->header, i2d_ASN1_OCTET_STRING); + M_ASN1_I2D_len(a->data, a->meth->i2d); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->header, i2d_ASN1_OCTET_STRING); + M_ASN1_I2D_put(a->data, a->meth->i2d); + + M_ASN1_I2D_finish(); + } + +ASN1_HEADER *d2i_ASN1_HEADER(a,pp,length) +ASN1_HEADER **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,ASN1_HEADER *,ASN1_HEADER_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->header,d2i_ASN1_OCTET_STRING); + if (ret->meth != NULL) + { + M_ASN1_D2I_get(ret->data,ret->meth->d2i); + } + else + { + if (a != NULL) (*a)=ret; + return(ret); + } + M_ASN1_D2I_Finish(a,ASN1_HEADER_free,ASN1_F_D2I_ASN1_HEADER); + } + +ASN1_HEADER *ASN1_HEADER_new() + { + ASN1_HEADER *ret=NULL; + + M_ASN1_New_Malloc(ret,ASN1_HEADER); + M_ASN1_New(ret->header,ASN1_OCTET_STRING_new); + ret->meth=NULL; + ret->data=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_ASN1_HEADER_NEW); + } + +void ASN1_HEADER_free(a) +ASN1_HEADER *a; + { + if (a == NULL) return; + ASN1_OCTET_STRING_free(a->header); + if (a->meth != NULL) + a->meth->destroy(a->data); + Free((char *)a); + } diff --git a/crypto/asn1/a_i2d_fp.c b/crypto/asn1/a_i2d_fp.c new file mode 100644 index 0000000000..eab127e6e0 --- /dev/null +++ b/crypto/asn1/a_i2d_fp.c @@ -0,0 +1,119 @@ +/* crypto/asn1/a_i2d_fp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "asn1_mac.h" + +#ifndef WIN16 +int ASN1_i2d_fp(i2d,out,x) +int (*i2d)(); +FILE *out; +unsigned char *x; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + ASN1err(ASN1_F_ASN1_I2D_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,out,BIO_NOCLOSE); + ret=ASN1_i2d_bio(i2d,b,x); + BIO_free(b); + return(ret); + } +#endif + +int ASN1_i2d_bio(i2d,out,x) +int (*i2d)(); +BIO *out; +unsigned char *x; + { + char *b; + unsigned char *p; + int i,j=0,n,ret=1; + + n=i2d(x,NULL); + b=(char *)Malloc(n); + if (b == NULL) + { + ASN1err(ASN1_F_ASN1_I2D_BIO,ERR_R_MALLOC_FAILURE); + return(0); + } + + p=(unsigned char *)b; + i2d(x,&p); + + for (;;) + { + i=BIO_write(out,&(b[j]),n); + if (i == n) break; + if (i <= 0) + { + ret=0; + break; + } + j+=i; + n-=i; + } + Free((char *)b); + return(ret); + } diff --git a/crypto/asn1/a_int.c b/crypto/asn1/a_int.c new file mode 100644 index 0000000000..ce0921d59a --- /dev/null +++ b/crypto/asn1/a_int.c @@ -0,0 +1,304 @@ +/* crypto/asn1/a_int.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_D2I_ASN1_INTEGER,ASN1_R_EXPECTING_AN_INTEGER); + */ + +int i2d_ASN1_INTEGER(a,pp) +ASN1_INTEGER *a; +unsigned char **pp; + { + int pad=0,ret,r,i,t; + unsigned char *p,*pt,*n,pb=0; + + if ((a == NULL) || (a->data == NULL)) return(0); + t=a->type; + if (a->length == 0) + ret=1; + else + { + ret=a->length; + i=a->data[0]; + if ((t == V_ASN1_INTEGER) && (i > 127)) + { + pad=1; + pb=0; + } + else if ((t == V_ASN1_NEG_INTEGER) && (i>128)) + { + pad=1; + pb=0xFF; + } + ret+=pad; + } + r=ASN1_object_size(0,ret,V_ASN1_INTEGER); + if (pp == NULL) return(r); + p= *pp; + + ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL); + if (pad) *(p++)=pb; + if (a->length == 0) + *(p++)=0; + else if (t == V_ASN1_INTEGER) + { + memcpy(p,a->data,(unsigned int)a->length); + p+=a->length; + } + else + { + n=a->data; + pt=p; + for (i=a->length; i>0; i--) + *(p++)= (*(n++)^0xFF)+1; + if (!pad) *pt|=0x80; + } + + *pp=p; + return(r); + } + +ASN1_INTEGER *d2i_ASN1_INTEGER(a, pp, length) +ASN1_INTEGER **a; +unsigned char **pp; +long length; + { + ASN1_INTEGER *ret=NULL; + unsigned char *p,*to,*s; + long len; + int inf,tag,xclass; + int i; + + if ((a == NULL) || ((*a) == NULL)) + { + if ((ret=ASN1_INTEGER_new()) == NULL) return(NULL); + ret->type=V_ASN1_INTEGER; + } + else + ret=(*a); + + p= *pp; + inf=ASN1_get_object(&p,&len,&tag,&xclass,length); + if (inf & 0x80) + { + i=ASN1_R_BAD_OBJECT_HEADER; + goto err; + } + + if (tag != V_ASN1_INTEGER) + { + i=ASN1_R_EXPECTING_AN_INTEGER; + goto err; + } + + /* We must Malloc stuff, even for 0 bytes otherwise it + * signifies a missing NULL parameter. */ + s=(unsigned char *)Malloc((int)len+1); + if (s == NULL) + { + i=ERR_R_MALLOC_FAILURE; + goto err; + } + to=s; + if (*p & 0x80) /* a negative number */ + { + ret->type=V_ASN1_NEG_INTEGER; + if (*p == 0xff) + { + p++; + len--; + } + for (i=(int)len; i>0; i--) + *(to++)= (*(p++)^0xFF)+1; + } + else + { + ret->type=V_ASN1_INTEGER; + if ((*p == 0) && (len != 1)) + { + p++; + len--; + } + memcpy(s,p,(int)len); + p+=len; + } + + if (ret->data != NULL) Free((char *)ret->data); + ret->data=s; + ret->length=(int)len; + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + ASN1err(ASN1_F_D2I_ASN1_INTEGER,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_INTEGER_free(ret); + return(NULL); + } + +int ASN1_INTEGER_set(a,v) +ASN1_INTEGER *a; +long v; + { + int i,j,k; + unsigned char buf[sizeof(long)+1]; + long d; + + a->type=V_ASN1_INTEGER; + if (a->length < (sizeof(long)+1)) + { + if (a->data != NULL) + Free((char *)a->data); + if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL) + memset((char *)a->data,0,sizeof(long)+1); + } + if (a->data == NULL) + { + ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE); + return(0); + } + d=v; + if (d < 0) + { + d= -d; + a->type=V_ASN1_NEG_INTEGER; + } + + for (i=0; i>=8; + } + j=0; + if (v < 0) a->data[j++]=0; + for (k=i-1; k >=0; k--) + a->data[j++]=buf[k]; + a->length=j; + return(1); + } + +long ASN1_INTEGER_get(a) +ASN1_INTEGER *a; + { + int neg=0,i; + long r=0; + + if (a == NULL) return(0L); + i=a->type; + if (i == V_ASN1_NEG_INTEGER) + neg=1; + else if (i != V_ASN1_INTEGER) + return(0); + + if (a->length > sizeof(long)) + { + return(0xFFFFFFFFL); + } + if (a->data == NULL) + return(0); + + for (i=0; ilength; i++) + { + r<<=8; + r|=(unsigned char)a->data[i]; + } + if (neg) r= -r; + return(r); + } + +ASN1_INTEGER *BN_to_ASN1_INTEGER(bn,ai) +BIGNUM *bn; +ASN1_INTEGER *ai; + { + ASN1_INTEGER *ret; + int len,j; + + if (ai == NULL) + ret=ASN1_INTEGER_new(); + else + ret=ai; + if (ret == NULL) + { + ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ASN1_R_ERROR_STACK); + goto err; + } + ret->type=V_ASN1_INTEGER; + j=BN_num_bits(bn); + len=((j == 0)?0:((j/8)+1)); + ret->data=(unsigned char *)Malloc(len+4); + ret->length=BN_bn2bin(bn,ret->data); + return(ret); +err: + if (ret != ai) ASN1_INTEGER_free(ret); + return(NULL); + } + +BIGNUM *ASN1_INTEGER_to_BN(ai,bn) +ASN1_INTEGER *ai; +BIGNUM *bn; + { + BIGNUM *ret; + + if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) + ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); + return(ret); + } diff --git a/crypto/asn1/a_meth.c b/crypto/asn1/a_meth.c new file mode 100644 index 0000000000..0beb958aa6 --- /dev/null +++ b/crypto/asn1/a_meth.c @@ -0,0 +1,84 @@ +/* crypto/asn1/a_meth.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "x509.h" + +static ASN1_METHOD ia5string_meth={ + (int (*)()) i2d_ASN1_IA5STRING, + (char *(*)()) d2i_ASN1_IA5STRING, + (char *(*)()) ASN1_STRING_new, + (void (*)()) ASN1_STRING_free}; + +static ASN1_METHOD bit_string_meth={ + (int (*)()) i2d_ASN1_BIT_STRING, + (char *(*)()) d2i_ASN1_BIT_STRING, + (char *(*)()) ASN1_STRING_new, + (void (*)()) ASN1_STRING_free}; + +ASN1_METHOD *ASN1_IA5STRING_asn1_meth() + { + return(&ia5string_meth); + } + +ASN1_METHOD *ASN1_BIT_STRING_asn1_meth() + { + return(&bit_string_meth); + } diff --git a/crypto/asn1/a_object.c b/crypto/asn1/a_object.c new file mode 100644 index 0000000000..897915cf49 --- /dev/null +++ b/crypto/asn1/a_object.c @@ -0,0 +1,369 @@ +/* crypto/asn1/a_object.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "asn1.h" +#include "objects.h" + +/* ASN1err(ASN1_F_ASN1_OBJECT_NEW,ASN1_R_EXPECTING_AN_OBJECT); + * ASN1err(ASN1_F_D2I_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER); + * ASN1err(ASN1_F_I2A_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER); + */ + +int i2d_ASN1_OBJECT(a, pp) +ASN1_OBJECT *a; +unsigned char **pp; + { + unsigned char *p; + + if ((a == NULL) || (a->data == NULL)) return(0); + + if (pp == NULL) + return(ASN1_object_size(0,a->length,V_ASN1_OBJECT)); + + p= *pp; + ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); + memcpy(p,a->data,a->length); + p+=a->length; + + *pp=p; + return(a->length); + } + +int a2d_ASN1_OBJECT(out,olen,buf,num) +unsigned char *out; +int olen; +char *buf; +int num; + { + int i,first,len=0,c; + char tmp[24],*p; + unsigned long l; + + if (num == 0) + return(0); + else if (num == -1) + num=strlen(buf); + + p=buf; + c= *(p++); + num--; + if ((c >= '0') && (c <= '2')) + { + first=(c-'0')*40; + } + else + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE); + goto err; + } + + if (num <= 0) + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER); + goto err; + } + c= *(p++); + num--; + for (;;) + { + if (num <= 0) break; + if ((c != '.') && (c != ' ')) + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR); + goto err; + } + l=0; + for (;;) + { + if (num <= 0) break; + num--; + c= *(p++); + if ((c == ' ') || (c == '.')) + break; + if ((c < '0') || (c > '9')) + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); + goto err; + } + l=l*10L+(long)(c-'0'); + } + if (len == 0) + { + if ((first < 2) && (l >= 40)) + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); + goto err; + } + l+=(long)first; + } + i=0; + for (;;) + { + tmp[i++]=(unsigned char)l&0x7f; + l>>=7L; + if (l == 0L) break; + } + if (out != NULL) + { + if (len+i > olen) + { + ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL); + goto err; + } + while (--i > 0) + out[len++]=tmp[i]|0x80; + out[len++]=tmp[0]; + } + else + len+=i; + } + return(len); +err: + return(0); + } + +int i2a_ASN1_OBJECT(bp,a) +BIO *bp; +ASN1_OBJECT *a; + { + int j,i,idx=0,n=0,len,nid,reason=ERR_R_BUF_LIB; + unsigned long l; + unsigned char *p; + char buf[20]; + char *s; + + if ((a == NULL) || (a->data == NULL)) + { + return(BIO_write(bp,"NULL",4)); + } + + nid=OBJ_obj2nid(a); + if (nid == NID_undef) + { + len=a->length; + p=a->data; + + idx=0; + l=0; + while (idx < a->length) + { + l|=(p[idx]&0x7f); + if (!(p[idx] & 0x80)) break; + l<<=7L; + idx++; + } + idx++; + i=(int)(l/40); + if (i > 2) i=2; + l-=(long)(i*40); + + sprintf(buf,"%d.%ld",i,l); + i=strlen(buf); + if (BIO_write(bp,buf,i) != i) + goto err; + n+=i; + + l=0; + for (; idxsn or ->ln */ + if ((a == NULL) || ((*a) == NULL) || + !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) + { + if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); + } + else ret=(*a); + + p= *pp; + + inf=ASN1_get_object(&p,&len,&tag,&xclass,length); + if (inf & 0x80) + { + i=ASN1_R_BAD_OBJECT_HEADER; + goto err; + } + + if (tag != V_ASN1_OBJECT) + { + i=ASN1_R_EXPECTING_AN_OBJECT; + goto err; + } + if ((ret->data == NULL) || (ret->length < len)) + { + if (ret->data != NULL) Free((char *)ret->data); + ret->data=(unsigned char *)Malloc((int)len); + ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; + if (ret->data == NULL) + { i=ERR_R_MALLOC_FAILURE; goto err; } + } + memcpy(ret->data,p,(int)len); + ret->length=(int)len; + ret->sn=NULL; + ret->ln=NULL; + /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ + p+=len; + + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + ASN1err(ASN1_F_D2I_ASN1_OBJECT,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_OBJECT_free(ret); + return(NULL); + } + +ASN1_OBJECT *ASN1_OBJECT_new() + { + ASN1_OBJECT *ret; + + ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT)); + if (ret == NULL) + { + ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE); + return(NULL); + } + ret->length=0; + ret->data=NULL; + ret->nid=0; + ret->sn=NULL; + ret->ln=NULL; + ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; + return(ret); + } + +void ASN1_OBJECT_free(a) +ASN1_OBJECT *a; + { + if (a == NULL) return; + if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) + { + if (a->sn != NULL) Free(a->sn); + if (a->ln != NULL) Free(a->ln); + a->sn=a->ln=NULL; + } + if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) + { + if (a->data != NULL) Free(a->data); + a->data=NULL; + a->length=0; + } + if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) + Free((char *)a); + } + +ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln) +int nid; +unsigned char *data; +int len; +char *sn,*ln; + { + ASN1_OBJECT o; + + o.sn=sn; + o.ln=ln; + o.data=data; + o.nid=nid; + o.length=len; + o.flags=ASN1_OBJECT_FLAG_DYNAMIC| + ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA; + return(OBJ_dup(&o)); + } + diff --git a/crypto/asn1/a_octet.c b/crypto/asn1/a_octet.c new file mode 100644 index 0000000000..e4ef15a995 --- /dev/null +++ b/crypto/asn1/a_octet.c @@ -0,0 +1,90 @@ +/* crypto/asn1/a_octet.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_EXPECTING_AN_OCTET_STRING); + */ + +int i2d_ASN1_OCTET_STRING(a, pp) +ASN1_OCTET_STRING *a; +unsigned char **pp; + { + return(i2d_ASN1_bytes((ASN1_STRING *)a,pp, + V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL)); + } + +ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(a, pp, length) +ASN1_OCTET_STRING **a; +unsigned char **pp; +long length; + { + ASN1_OCTET_STRING *ret=NULL; + + ret=(ASN1_OCTET_STRING *)d2i_ASN1_bytes((ASN1_STRING **)a, + pp,length,V_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL); + if (ret == NULL) + { + ASN1err(ASN1_F_D2I_ASN1_OCTET_STRING,ASN1_R_ERROR_STACK); + return(NULL); + } + return(ret); + } + diff --git a/crypto/asn1/a_print.c b/crypto/asn1/a_print.c new file mode 100644 index 0000000000..a1bbc35ca1 --- /dev/null +++ b/crypto/asn1/a_print.c @@ -0,0 +1,161 @@ +/* crypto/asn1/a_print.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_WRONG_PRINTABLE_TYPE); + * ASN1err(ASN1_F_D2I_ASN1_PRINT_TYPE,ASN1_R_TAG_VALUE_TOO_HIGH); + */ + +int i2d_ASN1_IA5STRING(a,pp) +ASN1_IA5STRING *a; +unsigned char **pp; + { return(M_i2d_ASN1_IA5STRING(a,pp)); } + +ASN1_IA5STRING *d2i_ASN1_IA5STRING(a,pp,l) +ASN1_IA5STRING **a; +unsigned char **pp; +long l; + { return(M_d2i_ASN1_IA5STRING(a,pp,l)); } + +ASN1_T61STRING *d2i_ASN1_T61STRING(a,pp,l) +ASN1_T61STRING **a; +unsigned char **pp; +long l; + { return(M_d2i_ASN1_T61STRING(a,pp,l)); } + +ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(a,pp,l) +ASN1_PRINTABLESTRING **a; +unsigned char **pp; +long l; + { return(M_d2i_ASN1_PRINTABLESTRING(a,pp,l)); } + +int i2d_ASN1_PRINTABLE(a,pp) +ASN1_STRING *a; +unsigned char **pp; + { return(M_i2d_ASN1_PRINTABLE(a,pp)); } + +ASN1_STRING *d2i_ASN1_PRINTABLE(a,pp,l) +ASN1_STRING **a; +unsigned char **pp; +long l; + { return(M_d2i_ASN1_PRINTABLE(a,pp,l)); } + +int ASN1_PRINTABLE_type(s,len) +unsigned char *s; +int len; + { + int c; + int ia5=0; + int t61=0; + + if (len <= 0) len= -1; + if (s == NULL) return(V_ASN1_PRINTABLESTRING); + + while ((*s) && (len-- != 0)) + { + c= *(s++); + if (!( ((c >= 'a') && (c <= 'z')) || + ((c >= 'A') && (c <= 'Z')) || + (c == ' ') || + ((c >= '0') && (c <= '9')) || + (c == ' ') || (c == '\'') || + (c == '(') || (c == ')') || + (c == '+') || (c == ',') || + (c == '-') || (c == '.') || + (c == '/') || (c == ':') || + (c == '=') || (c == '?'))) + ia5=1; + if (c&0x80) + t61=1; + } + if (t61) return(V_ASN1_T61STRING); + if (ia5) return(V_ASN1_IA5STRING); + return(V_ASN1_PRINTABLESTRING); + } + +int ASN1_UNIVERSALSTRING_to_string(s) +ASN1_UNIVERSALSTRING *s; + { + int i; + unsigned char *p; + + if (s->type != V_ASN1_UNIVERSALSTRING) return(0); + if ((s->length%4) != 0) return(0); + p=s->data; + for (i=0; ilength; i+=4) + { + if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0')) + break; + else + p+=4; + } + if (i < s->length) return(0); + p=s->data; + for (i=3; ilength; i+=4) + { + *(p++)=s->data[i]; + } + *(p)='\0'; + s->length/=4; + s->type=ASN1_PRINTABLE_type(s->data,s->length); + return(1); + } + diff --git a/crypto/asn1/a_set.c b/crypto/asn1/a_set.c new file mode 100644 index 0000000000..5752fbb5fd --- /dev/null +++ b/crypto/asn1/a_set.c @@ -0,0 +1,149 @@ +/* crypto/asn1/a_set.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ERR_R_MALLOC_FAILURE); + */ + +int i2d_ASN1_SET(a,pp,func,ex_tag,ex_class) +STACK *a; +unsigned char **pp; +int (*func)(); +int ex_tag; +int ex_class; + { + int ret=0,r; + int i; + unsigned char *p; + + if (a == NULL) return(0); + for (i=sk_num(a)-1; i>=0; i--) + ret+=func(sk_value(a,i),NULL); + r=ASN1_object_size(1,ret,ex_tag); + if (pp == NULL) return(r); + + p= *pp; + ASN1_put_object(&p,1,ret,ex_tag,ex_class); + for (i=0; i c.max) + { + ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR); + goto err; + } + /* check for infinite constructed - it can be as long + * as the amount of data passed to us */ + if (c.inf == (V_ASN1_CONSTRUCTED+1)) + c.slen=length+ *pp-c.p; + c.max=c.p+c.slen; + + while (c.p < c.max) + { + char *s; + + if (M_ASN1_D2I_end_sequence()) break; + if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) goto err; + if (!sk_push(ret,s)) goto err; + } + if (a != NULL) (*a)=ret; + *pp=c.p; + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) sk_free(ret); + return(NULL); + } + diff --git a/crypto/asn1/a_sign.c b/crypto/asn1/a_sign.c new file mode 100644 index 0000000000..2925ce3ad3 --- /dev/null +++ b/crypto/asn1/a_sign.c @@ -0,0 +1,147 @@ +/* crypto/asn1/a_sign.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include +#include +#include + +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "x509.h" +#include "objects.h" +#include "buffer.h" +#include "pem.h" + +int ASN1_sign(i2d,algor1,algor2,signature,data,pkey,type) +int (*i2d)(); +X509_ALGOR *algor1; +X509_ALGOR *algor2; +ASN1_BIT_STRING *signature; +char *data; +EVP_PKEY *pkey; +EVP_MD *type; + { + EVP_MD_CTX ctx; + unsigned char *p,*buf_in=NULL,*buf_out=NULL; + int i,inl=0,outl=0,outll=0; + X509_ALGOR *a; + + for (i=0; i<2; i++) + { + if (i == 0) + a=algor1; + else + a=algor2; + if (a == NULL) continue; + if ( (a->parameter == NULL) || + (a->parameter->type != V_ASN1_NULL)) + { + ASN1_TYPE_free(a->parameter); + if ((a->parameter=ASN1_TYPE_new()) == NULL) goto err; + a->parameter->type=V_ASN1_NULL; + } + ASN1_OBJECT_free(a->algorithm); + a->algorithm=OBJ_nid2obj(type->pkey_type); + if (a->algorithm == NULL) + { + ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_UNKNOWN_OBJECT_TYPE); + goto err; + } + if (a->algorithm->length == 0) + { + ASN1err(ASN1_F_ASN1_SIGN,ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); + goto err; + } + } + inl=i2d(data,NULL); + buf_in=(unsigned char *)Malloc((unsigned int)inl); + outll=outl=EVP_PKEY_size(pkey); + buf_out=(unsigned char *)Malloc((unsigned int)outl); + if ((buf_in == NULL) || (buf_out == NULL)) + { + outl=0; + ASN1err(ASN1_F_ASN1_SIGN,ERR_R_MALLOC_FAILURE); + goto err; + } + p=buf_in; + + i2d(data,&p); + EVP_SignInit(&ctx,type); + EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl); + if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out, + (unsigned int *)&outl,pkey)) + { + outl=0; + ASN1err(ASN1_F_ASN1_SIGN,ERR_R_EVP_LIB); + goto err; + } + if (signature->data != NULL) Free((char *)signature->data); + signature->data=buf_out; + buf_out=NULL; + signature->length=outl; + +err: + memset(&ctx,0,sizeof(ctx)); + if (buf_in != NULL) + { memset((char *)buf_in,0,(unsigned int)inl); Free((char *)buf_in); } + if (buf_out != NULL) + { memset((char *)buf_out,0,outll); Free((char *)buf_out); } + return(outl); + } diff --git a/crypto/asn1/a_type.c b/crypto/asn1/a_type.c new file mode 100644 index 0000000000..abaf70dcb8 --- /dev/null +++ b/crypto/asn1/a_type.c @@ -0,0 +1,317 @@ +/* crypto/asn1/a_type.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK); + * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_ERROR_STACK); + * ASN1err(ASN1_F_D2I_ASN1_BYTES,ASN1_R_WRONG_TAG); + * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG); + */ + +#ifndef NOPROTO +static void ASN1_TYPE_component_free(ASN1_TYPE *a); +#else +static void ASN1_TYPE_component_free(); +#endif + +int i2d_ASN1_TYPE(a,pp) +ASN1_TYPE *a; +unsigned char **pp; + { + int r=0; + + if (a == NULL) return(0); + + switch (a->type) + { + case V_ASN1_NULL: + if (pp != NULL) + ASN1_put_object(pp,0,0,V_ASN1_NULL,V_ASN1_UNIVERSAL); + r=2; + break; + case V_ASN1_INTEGER: + case V_ASN1_NEG_INTEGER: + r=i2d_ASN1_INTEGER(a->value.integer,pp); + break; + case V_ASN1_BIT_STRING: + r=i2d_ASN1_BIT_STRING(a->value.bit_string,pp); + break; + case V_ASN1_OCTET_STRING: + r=i2d_ASN1_OCTET_STRING(a->value.octet_string,pp); + break; + case V_ASN1_OBJECT: + r=i2d_ASN1_OBJECT(a->value.object,pp); + break; + case V_ASN1_PRINTABLESTRING: + r=M_i2d_ASN1_PRINTABLESTRING(a->value.printablestring,pp); + break; + case V_ASN1_T61STRING: + r=M_i2d_ASN1_T61STRING(a->value.t61string,pp); + break; + case V_ASN1_IA5STRING: + r=M_i2d_ASN1_IA5STRING(a->value.ia5string,pp); + break; + case V_ASN1_GENERALSTRING: + r=M_i2d_ASN1_GENERALSTRING(a->value.generalstring,pp); + break; + case V_ASN1_UNIVERSALSTRING: + r=M_i2d_ASN1_UNIVERSALSTRING(a->value.universalstring,pp); + break; + case V_ASN1_UTCTIME: + r=i2d_ASN1_UTCTIME(a->value.utctime,pp); + break; + case V_ASN1_SET: + case V_ASN1_SEQUENCE: + if (a->value.set == NULL) + r=0; + else + { + r=a->value.set->length; + if (pp != NULL) + { + memcpy(*pp,a->value.set->data,r); + *pp+=r; + } + } + break; + } + return(r); + } + +ASN1_TYPE *d2i_ASN1_TYPE(a,pp,length) +ASN1_TYPE **a; +unsigned char **pp; +long length; + { + ASN1_TYPE *ret=NULL; + unsigned char *q,*p,*max; + int inf,tag,xclass; + long len; + + if ((a == NULL) || ((*a) == NULL)) + { + if ((ret=ASN1_TYPE_new()) == NULL) goto err; + } + else + ret=(*a); + + p= *pp; + q=p; + max=(p+length); + + inf=ASN1_get_object(&q,&len,&tag,&xclass,length); + if (inf & 0x80) goto err; + + ASN1_TYPE_component_free(ret); + + switch (tag) + { + case V_ASN1_NULL: + p=q; + ret->value.ptr=NULL; + break; + case V_ASN1_INTEGER: + if ((ret->value.integer= + d2i_ASN1_INTEGER(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_BIT_STRING: + if ((ret->value.bit_string= + d2i_ASN1_BIT_STRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_OCTET_STRING: + if ((ret->value.octet_string= + d2i_ASN1_OCTET_STRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_OBJECT: + if ((ret->value.object= + d2i_ASN1_OBJECT(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_PRINTABLESTRING: + if ((ret->value.printablestring= + d2i_ASN1_PRINTABLESTRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_T61STRING: + if ((ret->value.t61string= + M_d2i_ASN1_T61STRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_IA5STRING: + if ((ret->value.ia5string= + M_d2i_ASN1_IA5STRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_GENERALSTRING: + if ((ret->value.generalstring= + M_d2i_ASN1_GENERALSTRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_UNIVERSALSTRING: + if ((ret->value.universalstring= + M_d2i_ASN1_UNIVERSALSTRING(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_UTCTIME: + if ((ret->value.utctime= + d2i_ASN1_UTCTIME(NULL,&p,max-p)) == NULL) + goto err; + break; + case V_ASN1_SET: + case V_ASN1_SEQUENCE: + /* Sets and sequences are left complete */ + if ((ret->value.set=ASN1_STRING_new()) == NULL) goto err; + ret->value.set->type=tag; + len+=(q-p); + if (!ASN1_STRING_set(ret->value.set,p,(int)len)) goto err; + p+=len; + break; + default: + ASN1err(ASN1_F_D2I_ASN1_TYPE,ASN1_R_BAD_TYPE); + goto err; + } + + ret->type=tag; + if (a != NULL) (*a)=ret; + *pp=p; + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) ASN1_TYPE_free(ret); + return(NULL); + } + +ASN1_TYPE *ASN1_TYPE_new() + { + ASN1_TYPE *ret=NULL; + + M_ASN1_New_Malloc(ret,ASN1_TYPE); + ret->type= -1; + ret->value.ptr=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_ASN1_TYPE_NEW); + } + +void ASN1_TYPE_free(a) +ASN1_TYPE *a; + { + if (a == NULL) return; + ASN1_TYPE_component_free(a); + Free((char *)(char *)a); + } + +int ASN1_TYPE_get(a) +ASN1_TYPE *a; + { + if (a->value.ptr != NULL) + return(a->type); + else + return(0); + } + +void ASN1_TYPE_set(a,type,value) +ASN1_TYPE *a; +int type; +char *value; + { + if (a->value.ptr != NULL) + ASN1_TYPE_component_free(a); + a->type=type; + a->value.ptr=value; + } + +static void ASN1_TYPE_component_free(a) +ASN1_TYPE *a; + { + if (a == NULL) return; + + if (a->value.ptr != NULL) + { + switch (a->type) + { + case V_ASN1_OBJECT: + ASN1_OBJECT_free(a->value.object); + break; + case V_ASN1_INTEGER: + case V_ASN1_NEG_INTEGER: + case V_ASN1_BIT_STRING: + case V_ASN1_OCTET_STRING: + case V_ASN1_PRINTABLESTRING: + case V_ASN1_T61STRING: + case V_ASN1_IA5STRING: + case V_ASN1_UNIVERSALSTRING: + case V_ASN1_GENERALSTRING: + case V_ASN1_UTCTIME: + case V_ASN1_SET: + case V_ASN1_SEQUENCE: + ASN1_STRING_free((ASN1_STRING *)a->value.ptr); + break; + default: + /* MEMORY LEAK */ + break; + } + a->type=0; + a->value.ptr=NULL; + } + } + diff --git a/crypto/asn1/a_utctm.c b/crypto/asn1/a_utctm.c new file mode 100644 index 0000000000..cc3c692fc9 --- /dev/null +++ b/crypto/asn1/a_utctm.c @@ -0,0 +1,190 @@ +/* crypto/asn1/a_utctm.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include +#include "cryptlib.h" +#include "asn1.h" + +/* ASN1err(ASN1_F_ASN1_UTCTIME_NEW,ASN1_R_UTCTIME_TOO_LONG); + * ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_EXPECTING_A_UTCTIME); + */ + +int i2d_ASN1_UTCTIME(a,pp) +ASN1_UTCTIME *a; +unsigned char **pp; + { + return(i2d_ASN1_bytes((ASN1_STRING *)a,pp, + V_ASN1_UTCTIME,V_ASN1_UNIVERSAL)); + } + + +ASN1_UTCTIME *d2i_ASN1_UTCTIME(a, pp, length) +ASN1_UTCTIME **a; +unsigned char **pp; +long length; + { + ASN1_UTCTIME *ret=NULL; + + ret=(ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length, + V_ASN1_UTCTIME,V_ASN1_UNIVERSAL); + if (ret == NULL) + { + ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_ERROR_STACK); + return(NULL); + } + if (!ASN1_UTCTIME_check(ret)) + { + ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_INVALID_TIME_FORMAT); + goto err; + } + + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) + ASN1_UTCTIME_free(ret); + return(NULL); + } + +int ASN1_UTCTIME_check(d) +ASN1_UTCTIME *d; + { + static int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0}; + static int max[8]={99,12,31,23,59,59,12,59}; + char *a; + int n,i,l,o; + + if (d->type != V_ASN1_UTCTIME) return(0); + l=d->length; + a=(char *)d->data; + o=0; + + if (l < 11) goto err; + for (i=0; i<6; i++) + { + if ((i == 5) && ((a[o] == 'Z') || + (a[o] == '+') || (a[o] == '-'))) + { i++; break; } + if ((a[o] < '0') || (a[o] > '9')) goto err; + n= a[o]-'0'; + if (++o > l) goto err; + + if ((a[o] < '0') || (a[o] > '9')) goto err; + n=(n*10)+ a[o]-'0'; + if (++o > l) goto err; + + if ((n < min[i]) || (n > max[i])) goto err; + } + if (a[o] == 'Z') + o++; + else if ((a[o] == '+') || (a[o] == '-')) + { + o++; + if (o+4 > l) goto err; + for (i=6; i<8; i++) + { + if ((a[o] < '0') || (a[o] > '9')) goto err; + n= a[o]-'0'; + o++; + if ((a[o] < '0') || (a[o] > '9')) goto err; + n=(n*10)+ a[o]-'0'; + if ((n < min[i]) || (n > max[i])) goto err; + o++; + } + } + return(o == l); +err: + return(0); + } + +ASN1_UTCTIME *ASN1_UTCTIME_set(s, t) +ASN1_UTCTIME *s; +time_t t; + { + char *p; + struct tm *ts; +#if defined(THREADS) + struct tm data; +#endif + + if (s == NULL) + s=ASN1_UTCTIME_new(); + if (s == NULL) + return(NULL); + +#if defined(THREADS) + ts=(struct tm *)gmtime_r(&t,&data); +#else + ts=(struct tm *)gmtime(&t); +#endif + p=(char *)s->data; + if ((p == NULL) || (s->length < 14)) + { + p=Malloc(20); + if (p == NULL) return(NULL); + if (s->data != NULL) + Free(s->data); + s->data=(unsigned char *)p; + } + + sprintf(p,"%02d%02d%02d%02d%02d%02dZ",ts->tm_year%100, + ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec); + s->length=strlen(p); + s->type=V_ASN1_UTCTIME; + return(s); + } diff --git a/crypto/asn1/a_verify.c b/crypto/asn1/a_verify.c new file mode 100644 index 0000000000..567552f782 --- /dev/null +++ b/crypto/asn1/a_verify.c @@ -0,0 +1,121 @@ +/* crypto/asn1/a_verify.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include +#include +#include + +#include "cryptlib.h" +#include "bn.h" +#include "x509.h" +#include "objects.h" +#include "buffer.h" +#include "evp.h" +#include "pem.h" + +int ASN1_verify(i2d,a,signature,data,pkey) +int (*i2d)(); +X509_ALGOR *a; +ASN1_BIT_STRING *signature; +char *data; +EVP_PKEY *pkey; + { + EVP_MD_CTX ctx; + EVP_MD *type; + unsigned char *p,*buf_in=NULL; + int ret= -1,i,inl; + + i=OBJ_obj2nid(a->algorithm); + type=EVP_get_digestbyname(OBJ_nid2sn(i)); + if (type == NULL) + { + ASN1err(ASN1_F_ASN1_VERIFY,ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM); + goto err; + } + + inl=i2d(data,NULL); + buf_in=(unsigned char *)Malloc((unsigned int)inl); + if (buf_in == NULL) + { + ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_MALLOC_FAILURE); + goto err; + } + p=buf_in; + + i2d(data,&p); + EVP_VerifyInit(&ctx,type); + EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl); + + memset(buf_in,0,(unsigned int)inl); + Free((char *)buf_in); + + if (EVP_VerifyFinal(&ctx,(unsigned char *)signature->data, + (unsigned int)signature->length,pkey) <= 0) + { + ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB); + ret=0; + goto err; + } + /* we don't need to zero the 'ctx' because we just checked + * public information */ + /* memset(&ctx,0,sizeof(ctx)); */ + ret=1; +err: + return(ret); + } diff --git a/crypto/asn1/asn1.err b/crypto/asn1/asn1.err new file mode 100644 index 0000000000..cbf27ab5fd --- /dev/null +++ b/crypto/asn1/asn1.err @@ -0,0 +1,178 @@ +/* Error codes for the ASN1 functions. */ + +/* Function codes. */ +#define ASN1_F_A2D_ASN1_OBJECT 100 +#define ASN1_F_A2I_ASN1_INTEGER 101 +#define ASN1_F_A2I_ASN1_STRING 102 +#define ASN1_F_ASN1_COLLATE_PRIMATIVE 103 +#define ASN1_F_ASN1_D2I_BIO 104 +#define ASN1_F_ASN1_D2I_FP 105 +#define ASN1_F_ASN1_DUP 106 +#define ASN1_F_ASN1_GET_OBJECT 107 +#define ASN1_F_ASN1_HEADER_NEW 108 +#define ASN1_F_ASN1_I2D_BIO 109 +#define ASN1_F_ASN1_I2D_FP 110 +#define ASN1_F_ASN1_INTEGER_SET 111 +#define ASN1_F_ASN1_INTEGER_TO_BN 112 +#define ASN1_F_ASN1_OBJECT_NEW 113 +#define ASN1_F_ASN1_SIGN 114 +#define ASN1_F_ASN1_STRING_NEW 115 +#define ASN1_F_ASN1_STRING_TYPE_NEW 116 +#define ASN1_F_ASN1_TYPE_NEW 117 +#define ASN1_F_ASN1_UTCTIME_NEW 118 +#define ASN1_F_ASN1_VERIFY 119 +#define ASN1_F_BN_TO_ASN1_INTEGER 120 +#define ASN1_F_D2I_ASN1_BIT_STRING 121 +#define ASN1_F_D2I_ASN1_BOOLEAN 122 +#define ASN1_F_D2I_ASN1_BYTES 123 +#define ASN1_F_D2I_ASN1_HEADER 124 +#define ASN1_F_D2I_ASN1_INTEGER 125 +#define ASN1_F_D2I_ASN1_OBJECT 126 +#define ASN1_F_D2I_ASN1_OCTET_STRING 127 +#define ASN1_F_D2I_ASN1_PRINT_TYPE 128 +#define ASN1_F_D2I_ASN1_SET 129 +#define ASN1_F_D2I_ASN1_TYPE 130 +#define ASN1_F_D2I_ASN1_TYPE_BYTES 131 +#define ASN1_F_D2I_ASN1_UTCTIME 132 +#define ASN1_F_D2I_DHPARAMS 133 +#define ASN1_F_D2I_DSAPARAMS 134 +#define ASN1_F_D2I_DSAPRIVATEKEY 135 +#define ASN1_F_D2I_DSAPUBLICKEY 136 +#define ASN1_F_D2I_NETSCAPE_PKEY 137 +#define ASN1_F_D2I_NETSCAPE_RSA 138 +#define ASN1_F_D2I_NETSCAPE_RSA_2 139 +#define ASN1_F_D2I_NETSCAPE_SPKAC 140 +#define ASN1_F_D2I_NETSCAPE_SPKI 141 +#define ASN1_F_D2I_PKCS7 142 +#define ASN1_F_D2I_PKCS7_DIGEST 143 +#define ASN1_F_D2I_PKCS7_ENCRYPT 144 +#define ASN1_F_D2I_PKCS7_ENC_CONTENT 145 +#define ASN1_F_D2I_PKCS7_ENVELOPE 146 +#define ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL 147 +#define ASN1_F_D2I_PKCS7_RECIP_INFO 148 +#define ASN1_F_D2I_PKCS7_SIGNED 149 +#define ASN1_F_D2I_PKCS7_SIGNER_INFO 150 +#define ASN1_F_D2I_PKCS7_SIGN_ENVELOPE 151 +#define ASN1_F_D2I_PRIVATEKEY 152 +#define ASN1_F_D2I_PUBLICKEY 153 +#define ASN1_F_D2I_RSAPRIVATEKEY 154 +#define ASN1_F_D2I_RSAPUBLICKEY 155 +#define ASN1_F_D2I_X509 156 +#define ASN1_F_D2I_X509_ALGOR 157 +#define ASN1_F_D2I_X509_ATTRIBUTE 158 +#define ASN1_F_D2I_X509_CINF 159 +#define ASN1_F_D2I_X509_CRL 160 +#define ASN1_F_D2I_X509_CRL_INFO 161 +#define ASN1_F_D2I_X509_EXTENSION 162 +#define ASN1_F_D2I_X509_KEY 163 +#define ASN1_F_D2I_X509_NAME 164 +#define ASN1_F_D2I_X509_NAME_ENTRY 165 +#define ASN1_F_D2I_X509_PKEY 166 +#define ASN1_F_D2I_X509_PUBKEY 167 +#define ASN1_F_D2I_X509_REQ 168 +#define ASN1_F_D2I_X509_REQ_INFO 169 +#define ASN1_F_D2I_X509_REVOKED 170 +#define ASN1_F_D2I_X509_SIG 171 +#define ASN1_F_D2I_X509_VAL 172 +#define ASN1_F_I2A_ASN1_OBJECT 173 +#define ASN1_F_I2D_ASN1_HEADER 174 +#define ASN1_F_I2D_DHPARAMS 175 +#define ASN1_F_I2D_DSAPARAMS 176 +#define ASN1_F_I2D_DSAPRIVATEKEY 177 +#define ASN1_F_I2D_DSAPUBLICKEY 178 +#define ASN1_F_I2D_NETSCAPE_RSA 179 +#define ASN1_F_I2D_PKCS7 180 +#define ASN1_F_I2D_PRIVATEKEY 181 +#define ASN1_F_I2D_PUBLICKEY 182 +#define ASN1_F_I2D_RSAPRIVATEKEY 183 +#define ASN1_F_I2D_RSAPUBLICKEY 184 +#define ASN1_F_I2D_X509_ATTRIBUTE 185 +#define ASN1_F_NETSCAPE_PKEY_NEW 186 +#define ASN1_F_NETSCAPE_SPKAC_NEW 187 +#define ASN1_F_NETSCAPE_SPKI_NEW 188 +#define ASN1_F_PKCS7_DIGEST_NEW 189 +#define ASN1_F_PKCS7_ENCRYPT_NEW 190 +#define ASN1_F_PKCS7_ENC_CONTENT_NEW 191 +#define ASN1_F_PKCS7_ENVELOPE_NEW 192 +#define ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW 193 +#define ASN1_F_PKCS7_NEW 194 +#define ASN1_F_PKCS7_RECIP_INFO_NEW 195 +#define ASN1_F_PKCS7_SIGNED_NEW 196 +#define ASN1_F_PKCS7_SIGNER_INFO_NEW 197 +#define ASN1_F_PKCS7_SIGN_ENVELOPE_NEW 198 +#define ASN1_F_X509_ALGOR_NEW 199 +#define ASN1_F_X509_ATTRIBUTE_NEW 200 +#define ASN1_F_X509_CINF_NEW 201 +#define ASN1_F_X509_CRL_INFO_NEW 202 +#define ASN1_F_X509_CRL_NEW 203 +#define ASN1_F_X509_DHPARAMS_NEW 204 +#define ASN1_F_X509_EXTENSION_NEW 205 +#define ASN1_F_X509_INFO_NEW 206 +#define ASN1_F_X509_KEY_NEW 207 +#define ASN1_F_X509_NAME_ENTRY_NEW 208 +#define ASN1_F_X509_NAME_NEW 209 +#define ASN1_F_X509_NEW 210 +#define ASN1_F_X509_PKEY_NEW 211 +#define ASN1_F_X509_PUBKEY_NEW 212 +#define ASN1_F_X509_REQ_INFO_NEW 213 +#define ASN1_F_X509_REQ_NEW 214 +#define ASN1_F_X509_REVOKED_NEW 215 +#define ASN1_F_X509_SIG_NEW 216 +#define ASN1_F_X509_VAL_FREE 217 +#define ASN1_F_X509_VAL_NEW 218 + +/* Reason codes. */ +#define ASN1_R_BAD_CLASS 100 +#define ASN1_R_BAD_GET_OBJECT 101 +#define ASN1_R_BAD_OBJECT_HEADER 102 +#define ASN1_R_BAD_PASSWORD_READ 103 +#define ASN1_R_BAD_PKCS7_CONTENT 104 +#define ASN1_R_BAD_PKCS7_TYPE 105 +#define ASN1_R_BAD_TAG 106 +#define ASN1_R_BAD_TYPE 107 +#define ASN1_R_BN_LIB 108 +#define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 109 +#define ASN1_R_BUFFER_TOO_SMALL 110 +#define ASN1_R_DECODING_ERROR 111 +#define ASN1_R_ERROR_STACK 112 +#define ASN1_R_EXPECTING_AN_INTEGER 113 +#define ASN1_R_EXPECTING_AN_OBJECT 114 +#define ASN1_R_EXPECTING_AN_OCTET_STRING 115 +#define ASN1_R_EXPECTING_A_BIT_STRING 116 +#define ASN1_R_EXPECTING_A_BOOLEAN 117 +#define ASN1_R_EXPECTING_A_SEQUENCE 118 +#define ASN1_R_EXPECTING_A_UTCTIME 119 +#define ASN1_R_FIRST_NUM_TOO_LARGE 120 +#define ASN1_R_HEADER_TOO_LONG 121 +#define ASN1_R_INVALID_DIGIT 122 +#define ASN1_R_INVALID_SEPARATOR 123 +#define ASN1_R_INVALID_TIME_FORMAT 124 +#define ASN1_R_IV_TOO_LARGE 125 +#define ASN1_R_LENGTH_ERROR 126 +#define ASN1_R_LENGTH_MISMATCH 127 +#define ASN1_R_MISSING_EOS 128 +#define ASN1_R_MISSING_SECOND_NUMBER 129 +#define ASN1_R_NON_HEX_CHARACTERS 130 +#define ASN1_R_NOT_ENOUGH_DATA 131 +#define ASN1_R_ODD_NUMBER_OF_CHARS 132 +#define ASN1_R_PARSING 133 +#define ASN1_R_PRIVATE_KEY_HEADER_MISSING 134 +#define ASN1_R_SECOND_NUMBER_TOO_LARGE 135 +#define ASN1_R_SHORT_LINE 136 +#define ASN1_R_STRING_TOO_SHORT 137 +#define ASN1_R_TAG_VALUE_TOO_HIGH 138 +#define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 139 +#define ASN1_R_TOO_LONG 140 +#define ASN1_R_UNABLE_TO_DECODE_RSA_KEY 141 +#define ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY 142 +#define ASN1_R_UNKNOWN_ATTRIBUTE_TYPE 143 +#define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 144 +#define ASN1_R_UNKNOWN_OBJECT_TYPE 145 +#define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 146 +#define ASN1_R_UNSUPPORTED_CIPHER 147 +#define ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM 148 +#define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 149 +#define ASN1_R_UTCTIME_TOO_LONG 150 +#define ASN1_R_WRONG_PRINTABLE_TYPE 151 +#define ASN1_R_WRONG_TAG 152 +#define ASN1_R_WRONG_TYPE 153 diff --git a/crypto/asn1/asn1.h b/crypto/asn1/asn1.h new file mode 100644 index 0000000000..cdc342946d --- /dev/null +++ b/crypto/asn1/asn1.h @@ -0,0 +1,786 @@ +/* crypto/asn1/asn1.h */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef HEADER_ASN1_H +#define HEADER_ASN1_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "bn.h" +#include "stack.h" + +#define V_ASN1_UNIVERSAL 0x00 +#define V_ASN1_APPLICATION 0x40 +#define V_ASN1_CONTEXT_SPECIFIC 0x80 +#define V_ASN1_PRIVATE 0xc0 + +#define V_ASN1_CONSTRUCTED 0x20 +#define V_ASN1_PRIMATIVE_TAG 0x1f + +#define V_ASN1_APP_CHOOSE -2 /* let the recipent choose */ + +#define V_ASN1_UNDEF -1 +#define V_ASN1_EOC 0 +#define V_ASN1_BOOLEAN 1 /**/ +#define V_ASN1_INTEGER 2 +#define V_ASN1_NEG_INTEGER (2+0x100) +#define V_ASN1_BIT_STRING 3 +#define V_ASN1_OCTET_STRING 4 +#define V_ASN1_NULL 5 +#define V_ASN1_OBJECT 6 +#define V_ASN1_REAL 9 +#define V_ASN1_ENUMERATED 10 /* microsoft weirdness */ +#define V_ASN1_SEQUENCE 16 +#define V_ASN1_SET 17 +#define V_ASN1_NUMERICSTRING 18 /**/ +#define V_ASN1_PRINTABLESTRING 19 +#define V_ASN1_T61STRING 20 +#define V_ASN1_TELETEXSTRING 20 /* alias */ +#define V_ASN1_VIDEOTEXSTRING 21 /**/ +#define V_ASN1_IA5STRING 22 +#define V_ASN1_UTCTIME 23 +#define V_ASN1_GENERALIZEDTIME 24 /**/ +#define V_ASN1_GRAPHICSTRING 25 /**/ +#define V_ASN1_ISO64STRING 26 /**/ +#define V_ASN1_VISIBLESTRING 26 /* alias */ +#define V_ASN1_GENERALSTRING 27 /**/ +#define V_ASN1_UNIVERSALSTRING 28 /**/ + +/* For use with d2i_ASN1_type_bytes() */ +#define B_ASN1_NUMERICSTRING 0x0001 +#define B_ASN1_PRINTABLESTRING 0x0002 +#define B_ASN1_T61STRING 0x0004 +#define B_ASN1_VIDEOTEXSTRING 0x0008 +#define B_ASN1_IA5STRING 0x0010 +#define B_ASN1_GRAPHICSTRING 0x0020 +#define B_ASN1_ISO64STRING 0x0040 +#define B_ASN1_GENERALSTRING 0x0080 +#define B_ASN1_UNIVERSALSTRING 0x0100 +#define B_ASN1_OCTET_STRING 0x0200 +#define B_ASN1_UNKNOWN 0x1000 + +#ifndef DEBUG + +#define ASN1_INTEGER ASN1_STRING +#define ASN1_BIT_STRING ASN1_STRING +#define ASN1_OCTET_STRING ASN1_STRING +#define ASN1_PRINTABLESTRING ASN1_STRING +#define ASN1_T61STRING ASN1_STRING +#define ASN1_IA5STRING ASN1_STRING +#define ASN1_UTCTIME ASN1_STRING +#define ASN1_GENERALSTRING ASN1_STRING +#define ASN1_UNIVERSALSTRING ASN1_STRING + +#else + +typedef struct asn1_integer_st + { + int length; + int type; + unsigned char *data; + } ASN1_INTEGER; + +typedef struct asn1_bit_string_st + { + int length; + int type; + unsigned char *data; + } ASN1_BIT_STRING; + +typedef struct asn1_octet_string_st + { + int length; + int type; + unsigned char *data; + } ASN1_OCTET_STRING; + +typedef struct asn1_printablestring_st + { + int length; + int type; + unsigned char *data; + } ASN1_PRINTABLESTRING; + +typedef struct asn1_t61string_st + { + int length; + int type; + unsigned char *data; + } ASN1_T61STRING; + +typedef struct asn1_ia5string_st + { + int length; + int type; + unsigned char *data; + } ASN1_IA5STRING; + +typedef struct asn1_generalstring_st + { + int length; + int type; + unsigned char *data; + } ASN1_GENERALSTRING; + +typedef struct asn1_universalstring_st + { + int length; + int type; + unsigned char *data; + } ASN1_UNIVERSALSTRING; + +typedef struct asn1_utctime_st + { + int length; + int type; + unsigned char *data; + } ASN1_UTCTIME; + +#endif + +typedef struct asn1_ctx_st + { + unsigned char *p;/* work char pointer */ + int eos; /* end of sequence read for indefinite encoding */ + int error; /* error code to use when returning an error */ + int inf; /* constructed if 0x20, indefinite is 0x21 */ + int tag; /* tag from last 'get object' */ + int xclass; /* class from last 'get object' */ + long slen; /* length of last 'get object' */ + unsigned char *max; /* largest value of p alowed */ + unsigned char *q;/* temporary variable */ + unsigned char **pp;/* variable */ + } ASN1_CTX; + +/* These are used internally in the ASN1_OBJECT to keep track of + * whether the names and data need to be free()ed */ +#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */ +#define ASN1_OBJECT_FLAG_CRITICAL 0x02 /* critical x509v3 object id */ +#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */ +#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */ +typedef struct asn1_object_st + { + char *sn,*ln; + int nid; + int length; + unsigned char *data; + int flags; /* Should we free this one */ + } ASN1_OBJECT; + +/* This is the base type that holds just about everything :-) */ +typedef struct asn1_string_st + { + int length; + int type; + unsigned char *data; + } ASN1_STRING; + +typedef struct asn1_type_st + { + int type; + union { + char *ptr; + ASN1_STRING * asn1_string; + ASN1_OBJECT * object; + ASN1_INTEGER * integer; + ASN1_BIT_STRING * bit_string; + ASN1_OCTET_STRING * octet_string; + ASN1_PRINTABLESTRING * printablestring; + ASN1_T61STRING * t61string; + ASN1_IA5STRING * ia5string; + ASN1_GENERALSTRING * generalstring; + ASN1_UNIVERSALSTRING * universalstring; + ASN1_UTCTIME * utctime; + /* set and sequence are left complete and still + * contain the set or sequence bytes */ + ASN1_STRING * set; + ASN1_STRING * sequence; + } value; + } ASN1_TYPE; + +typedef struct asn1_method_st + { + int (*i2d)(); + char *(*d2i)(); + char *(*create)(); + void (*destroy)(); + } ASN1_METHOD; + +/* This is used when parsing some Netscape objects */ +typedef struct asn1_header_st + { + ASN1_OCTET_STRING *header; + char *data; + ASN1_METHOD *meth; + } ASN1_HEADER; + +#define ASN1_STRING_type(x) ((x)->type) +#define ASN1_STRING_data(x) ((x)->data) + +/* Macros for string operations */ +#define ASN1_BIT_STRING_new() (ASN1_BIT_STRING *)\ + ASN1_STRING_type_new(V_ASN1_BIT_STRING) +#define ASN1_BIT_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define ASN1_BIT_STRING_dup(a) (ASN1_BIT_STRING *)\ + ASN1_STRING_dup((ASN1_STRING *)a) +#define ASN1_BIT_STRING_cmp(a,b) ASN1_STRING_cmp(\ + (ASN1_STRING *)a,(ASN1_STRING *)b) +#define ASN1_BIT_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c) +/* i2d_ASN1_BIT_STRING() is a function */ +/* d2i_ASN1_BIT_STRING() is a function */ + +#define ASN1_INTEGER_new() (ASN1_INTEGER *)\ + ASN1_STRING_type_new(V_ASN1_INTEGER) +#define ASN1_INTEGER_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define ASN1_INTEGER_dup(a) (ASN1_INTEGER *)ASN1_STRING_dup((ASN1_STRING *)a) +#define ASN1_INTEGER_cmp(a,b) ASN1_STRING_cmp(\ + (ASN1_STRING *)a,(ASN1_STRING *)b) +/* ASN1_INTEGER_set() is a function, also see BN_to_ASN1_INTEGER() */ +/* ASN1_INTEGER_get() is a function, also see ASN1_INTEGER_to_BN() */ +/* i2d_ASN1_INTEGER() is a function */ +/* d2i_ASN1_INTEGER() is a function */ + +#define ASN1_OCTET_STRING_new() (ASN1_OCTET_STRING *)\ + ASN1_STRING_type_new(V_ASN1_OCTET_STRING) +#define ASN1_OCTET_STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define ASN1_OCTET_STRING_dup(a) (ASN1_OCTET_STRING *)\ + ASN1_STRING_dup((ASN1_STRING *)a) +#define ASN1_OCTET_STRING_cmp(a,b) ASN1_STRING_cmp(\ + (ASN1_STRING *)a,(ASN1_STRING *)b) +#define ASN1_OCTET_STRING_set(a,b,c) ASN1_STRING_set((ASN1_STRING *)a,b,c) +#define ASN1_OCTET_STRING_print(a,b) ASN1_STRING_print(a,(ASN1_STRING *)b) +#define M_i2d_ASN1_OCTET_STRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_OCTET_STRING,\ + V_ASN1_OCTET_STRING) +/* d2i_ASN1_OCTET_STRING() is a function */ + +#define ASN1_PRINTABLE_new() ASN1_STRING_type_new(V_ASN1_T61STRING) +#define ASN1_PRINTABLE_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_PRINTABLE(a,pp) i2d_ASN1_bytes((ASN1_STRING *)a,\ + pp,a->type,V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_PRINTABLE(a,pp,l) \ + d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l, \ + B_ASN1_PRINTABLESTRING| \ + B_ASN1_T61STRING| \ + B_ASN1_IA5STRING| \ + B_ASN1_UNIVERSALSTRING|\ + B_ASN1_UNKNOWN) + +#define ASN1_PRINTABLESTRING_new() (ASN1_PRINTABLESTRING_STRING *)\ + ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING) +#define ASN1_PRINTABLESTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_PRINTABLESTRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_PRINTABLESTRING,\ + V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_PRINTABLESTRING(a,pp,l) \ + (ASN1_PRINTABLESTRING *)d2i_ASN1_type_bytes\ + ((ASN1_STRING **)a,pp,l,B_ASN1_PRINTABLESTRING) + +#define ASN1_T61STRING_new() (ASN1_T61STRING_STRING *)\ + ASN1_STRING_type_new(V_ASN1_T61STRING) +#define ASN1_T61STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_T61STRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_T61STRING,\ + V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_T61STRING(a,pp,l) \ + (ASN1_T61STRING *)d2i_ASN1_type_bytes\ + ((ASN1_STRING **)a,pp,l,B_ASN1_T61STRING) + +#define ASN1_IA5STRING_new() (ASN1_IA5STRING *)\ + ASN1_STRING_type_new(V_ASN1_IA5STRING) +#define ASN1_IA5STRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_IA5STRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_IA5STRING,\ + V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_IA5STRING(a,pp,l) \ + (ASN1_IA5STRING *)d2i_ASN1_type_bytes((ASN1_STRING **)a,pp,l,\ + B_ASN1_IA5STRING) + +#define ASN1_UTCTIME_new() (ASN1_UTCTIME *)\ + ASN1_STRING_type_new(V_ASN1_UTCTIME) +#define ASN1_UTCTIME_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define ASN1_UTCTIME_dup(a) (ASN1_UTCTIME *)ASN1_STRING_dup((ASN1_STRING *)a) +/* i2d_ASN1_UTCTIME() is a function */ +/* d2i_ASN1_UTCTIME() is a function */ +/* ASN1_UTCTIME_set() is a function */ +/* ASN1_UTCTIME_check() is a function */ +/* ASN1_UTCTIME_set() is a function */ + +#define ASN1_GENERALSTRING_new() (ASN1_GENERALSTRING *)\ + ASN1_STRING_type_new(V_ASN1_GENERALSTRING) +#define ASN1_GENERALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_GENERALSTRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_GENERALSTRING,\ + V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_GENERALSTRING(a,pp,l) \ + (ASN1_GENERALSTRING *)d2i_ASN1_type_bytes\ + ((ASN1_STRING **)a,pp,l,B_ASN1_GENERALSTRING) + +#define ASN1_UNIVERSALSTRING_new() (ASN1_UNIVERSALSTRING *)\ + ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING) +#define ASN1_UNIVERSALSTRING_free(a) ASN1_STRING_free((ASN1_STRING *)a) +#define M_i2d_ASN1_UNIVERSALSTRING(a,pp) \ + i2d_ASN1_bytes((ASN1_STRING *)a,pp,V_ASN1_UNIVERSALSTRING,\ + V_ASN1_UNIVERSAL) +#define M_d2i_ASN1_UNIVERSALSTRING(a,pp,l) \ + (ASN1_UNIVERSALSTRING *)d2i_ASN1_type_bytes\ + ((ASN1_STRING **)a,pp,l,B_ASN1_UNIVERSALSTRING) + +#ifndef NOPROTO +ASN1_TYPE * ASN1_TYPE_new(void ); +void ASN1_TYPE_free(ASN1_TYPE *a); +int i2d_ASN1_TYPE(ASN1_TYPE *a,unsigned char **pp); +ASN1_TYPE * d2i_ASN1_TYPE(ASN1_TYPE **a,unsigned char **pp,long length); +int ASN1_TYPE_get(ASN1_TYPE *a); +void ASN1_TYPE_set(ASN1_TYPE *a, int type, char *value); + +ASN1_OBJECT * ASN1_OBJECT_new(void ); +void ASN1_OBJECT_free(ASN1_OBJECT *a); +int i2d_ASN1_OBJECT(ASN1_OBJECT *a,unsigned char **pp); +ASN1_OBJECT * d2i_ASN1_OBJECT(ASN1_OBJECT **a,unsigned char **pp, + long length); + +ASN1_STRING * ASN1_STRING_new(void ); +void ASN1_STRING_free(ASN1_STRING *a); +ASN1_STRING * ASN1_STRING_dup(ASN1_STRING *a); +ASN1_STRING * ASN1_STRING_type_new(int type ); +int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b); +int ASN1_STRING_set(ASN1_STRING *str,unsigned char *data, int len); + +int i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a,unsigned char **pp); +ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,unsigned char **pp, + long length); + +int i2d_ASN1_BOOLEAN(int a,unsigned char **pp); +int d2i_ASN1_BOOLEAN(int *a,unsigned char **pp,long length); + +int i2d_ASN1_INTEGER(ASN1_INTEGER *a,unsigned char **pp); +ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a,unsigned char **pp, + long length); + +int ASN1_UTCTIME_check(ASN1_UTCTIME *a); +ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s,time_t t); + +int i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a,unsigned char **pp); +ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **a, + unsigned char **pp,long length); + +int i2d_ASN1_PRINTABLE(ASN1_STRING *a,unsigned char **pp); +ASN1_STRING *d2i_ASN1_PRINTABLE(ASN1_STRING **a, + unsigned char **pp, long l); +ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(ASN1_PRINTABLESTRING **a, + unsigned char **pp, long l); + +ASN1_T61STRING *d2i_ASN1_T61STRING(ASN1_T61STRING **a, + unsigned char **pp, long l); +int i2d_ASN1_IA5STRING(ASN1_IA5STRING *a,unsigned char **pp); +ASN1_IA5STRING *d2i_ASN1_IA5STRING(ASN1_IA5STRING **a, + unsigned char **pp, long l); + +int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a,unsigned char **pp); +ASN1_UTCTIME * d2i_ASN1_UTCTIME(ASN1_UTCTIME **a,unsigned char **pp, + long length); + +int i2d_ASN1_SET(STACK *a, unsigned char **pp, + int (*func)(), int ex_tag, int ex_class); +STACK * d2i_ASN1_SET(STACK **a, unsigned char **pp, long length, + char *(*func)(), int ex_tag, int ex_class); + +#ifdef HEADER_BIO_H +int i2a_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *a); +int a2i_ASN1_INTEGER(BIO *bp,ASN1_INTEGER *bs,char *buf,int size); +int i2a_ASN1_OBJECT(BIO *bp,ASN1_OBJECT *a); +int a2i_ASN1_STRING(BIO *bp,ASN1_STRING *bs,char *buf,int size); +int i2a_ASN1_STRING(BIO *bp, ASN1_STRING *a, int type); +#endif + +int a2d_ASN1_OBJECT(unsigned char *out,int olen, char *buf, int num); +ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data,int len, + char *sn, char *ln); + +int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); +long ASN1_INTEGER_get(ASN1_INTEGER *a); +ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai); +BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai,BIGNUM *bn); + +/* General */ +/* given a string, return the correct type, max is the maximum length */ +int ASN1_PRINTABLE_type(unsigned char *s, int max); + +int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass); +ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp, + long length, int Ptag, int Pclass); +/* type is one or more of the B_ASN1_ values. */ +ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a,unsigned char **pp, + long length,int type); + +/* PARSING */ +int asn1_Finish(ASN1_CTX *c); + +/* SPECIALS */ +int ASN1_get_object(unsigned char **pp, long *plength, int *ptag, + int *pclass, long omax); +int ASN1_check_infinite_end(unsigned char **p,long len); +void ASN1_put_object(unsigned char **pp, int constructed, int length, + int tag, int xclass); +int ASN1_object_size(int constructed, int length, int tag); + +/* Used to implement other functions */ +char *ASN1_dup(int (*i2d)(),char *(*d2i)(),char *x); + +#ifndef WIN16 +char *ASN1_d2i_fp(char *(*xnew)(),char *(*d2i)(),FILE *fp,unsigned char **x); +int ASN1_i2d_fp(int (*i2d)(),FILE *out,unsigned char *x); +#endif + +#ifdef HEADER_BIO_H +char *ASN1_d2i_bio(char *(*xnew)(),char *(*d2i)(),BIO *bp,unsigned char **x); +int ASN1_i2d_bio(int (*i2d)(),BIO *out,unsigned char *x); +int ASN1_UTCTIME_print(BIO *fp,ASN1_UTCTIME *a); +int ASN1_STRING_print(BIO *bp,ASN1_STRING *v); +int ASN1_parse(BIO *bp,unsigned char *pp,long len,int indent); +#endif + +/* Used to load and write netscape format cert/key */ +int i2d_ASN1_HEADER(ASN1_HEADER *a,unsigned char **pp); +ASN1_HEADER *d2i_ASN1_HEADER(ASN1_HEADER **a,unsigned char **pp, long length); +ASN1_HEADER *ASN1_HEADER_new(void ); +void ASN1_HEADER_free(ASN1_HEADER *a); + +int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s); + +void ERR_load_ASN1_strings(void); + +/* Not used that much at this point, except for the first two */ +ASN1_METHOD *X509_asn1_meth(void); +ASN1_METHOD *RSAPrivateKey_asn1_meth(void); +ASN1_METHOD *ASN1_IA5STRING_asn1_meth(void); +ASN1_METHOD *ASN1_BIT_STRING_asn1_meth(void); + +#else + +ASN1_TYPE * ASN1_TYPE_new(); +void ASN1_TYPE_free(); +int i2d_ASN1_TYPE(); +ASN1_TYPE * d2i_ASN1_TYPE(); +int ASN1_TYPE_get(); +void ASN1_TYPE_set(); + +ASN1_OBJECT * ASN1_OBJECT_new(); +void ASN1_OBJECT_free(); +int i2d_ASN1_OBJECT(); +ASN1_OBJECT * d2i_ASN1_OBJECT(); +ASN1_STRING * ASN1_STRING_new(); +void ASN1_STRING_free(); +ASN1_STRING * ASN1_STRING_dup(); +ASN1_STRING * ASN1_STRING_type_new(); +int ASN1_STRING_cmp(); +int ASN1_STRING_set(); +int i2d_ASN1_BIT_STRING(); +ASN1_BIT_STRING *d2i_ASN1_BIT_STRING(); +int i2d_ASN1_BOOLEAN(); +int d2i_ASN1_BOOLEAN(); +int i2d_ASN1_INTEGER(); +ASN1_INTEGER *d2i_ASN1_INTEGER(); +int ASN1_UTCTIME_check(); +ASN1_UTCTIME *ASN1_UTCTIME_set(); +int i2d_ASN1_OCTET_STRING(); +ASN1_OCTET_STRING *d2i_ASN1_OCTET_STRING(); +int i2d_ASN1_PRINTABLE(); +ASN1_STRING *d2i_ASN1_PRINTABLE(); +ASN1_PRINTABLESTRING *d2i_ASN1_PRINTABLESTRING(); +ASN1_T61STRING *d2i_ASN1_T61STRING(); +int i2d_ASN1_IA5STRING(); +ASN1_IA5STRING *d2i_ASN1_IA5STRING(); +int i2d_ASN1_UTCTIME(); +ASN1_UTCTIME * d2i_ASN1_UTCTIME(); +int i2d_ASN1_SET(); +STACK * d2i_ASN1_SET(); +int a2d_ASN1_OBJECT(); +ASN1_OBJECT *ASN1_OBJECT_create(); +int ASN1_INTEGER_set(); +long ASN1_INTEGER_get(); +ASN1_INTEGER *BN_to_ASN1_INTEGER(); +BIGNUM *ASN1_INTEGER_to_BN(); +int ASN1_PRINTABLE_type(); +int i2d_ASN1_bytes(); +ASN1_STRING *d2i_ASN1_bytes(); +ASN1_STRING *d2i_ASN1_type_bytes(); +int asn1_Finish(); +int ASN1_get_object(); +int ASN1_check_infinite_end(); +void ASN1_put_object(); +int ASN1_object_size(); +char *ASN1_dup(); +#ifndef WIN16 +char *ASN1_d2i_fp(); +int ASN1_i2d_fp(); +#endif + +char *ASN1_d2i_bio(); +int ASN1_i2d_bio(); +int ASN1_UTCTIME_print(); +int ASN1_STRING_print(); +int ASN1_parse(); +int i2a_ASN1_INTEGER(); +int a2i_ASN1_INTEGER(); +int i2a_ASN1_OBJECT(); +int a2i_ASN1_STRING(); +int i2a_ASN1_STRING(); + +int i2d_ASN1_HEADER(); +ASN1_HEADER *d2i_ASN1_HEADER(); +ASN1_HEADER *ASN1_HEADER_new(); +void ASN1_HEADER_free(); +void ERR_load_ASN1_strings(); +ASN1_METHOD *X509_asn1_meth(); +ASN1_METHOD *RSAPrivateKey_asn1_meth(); +ASN1_METHOD *ASN1_IA5STRING_asn1_meth(); +ASN1_METHOD *ASN1_BIT_STRING_asn1_meth(); + +int ASN1_UNIVERSALSTRING_to_string(); +#endif + +/* BEGIN ERROR CODES */ +/* Error codes for the ASN1 functions. */ + +/* Function codes. */ +#define ASN1_F_A2D_ASN1_OBJECT 100 +#define ASN1_F_A2I_ASN1_INTEGER 101 +#define ASN1_F_A2I_ASN1_STRING 102 +#define ASN1_F_ASN1_COLLATE_PRIMATIVE 103 +#define ASN1_F_ASN1_D2I_BIO 104 +#define ASN1_F_ASN1_D2I_FP 105 +#define ASN1_F_ASN1_DUP 106 +#define ASN1_F_ASN1_GET_OBJECT 107 +#define ASN1_F_ASN1_HEADER_NEW 108 +#define ASN1_F_ASN1_I2D_BIO 109 +#define ASN1_F_ASN1_I2D_FP 110 +#define ASN1_F_ASN1_INTEGER_SET 111 +#define ASN1_F_ASN1_INTEGER_TO_BN 112 +#define ASN1_F_ASN1_OBJECT_NEW 113 +#define ASN1_F_ASN1_SIGN 114 +#define ASN1_F_ASN1_STRING_NEW 115 +#define ASN1_F_ASN1_STRING_TYPE_NEW 116 +#define ASN1_F_ASN1_TYPE_NEW 117 +#define ASN1_F_ASN1_UTCTIME_NEW 118 +#define ASN1_F_ASN1_VERIFY 119 +#define ASN1_F_BN_TO_ASN1_INTEGER 120 +#define ASN1_F_D2I_ASN1_BIT_STRING 121 +#define ASN1_F_D2I_ASN1_BOOLEAN 122 +#define ASN1_F_D2I_ASN1_BYTES 123 +#define ASN1_F_D2I_ASN1_HEADER 124 +#define ASN1_F_D2I_ASN1_INTEGER 125 +#define ASN1_F_D2I_ASN1_OBJECT 126 +#define ASN1_F_D2I_ASN1_OCTET_STRING 127 +#define ASN1_F_D2I_ASN1_PRINT_TYPE 128 +#define ASN1_F_D2I_ASN1_SET 129 +#define ASN1_F_D2I_ASN1_TYPE 130 +#define ASN1_F_D2I_ASN1_TYPE_BYTES 131 +#define ASN1_F_D2I_ASN1_UTCTIME 132 +#define ASN1_F_D2I_DHPARAMS 133 +#define ASN1_F_D2I_DSAPARAMS 134 +#define ASN1_F_D2I_DSAPRIVATEKEY 135 +#define ASN1_F_D2I_DSAPUBLICKEY 136 +#define ASN1_F_D2I_NETSCAPE_PKEY 137 +#define ASN1_F_D2I_NETSCAPE_RSA 138 +#define ASN1_F_D2I_NETSCAPE_RSA_2 139 +#define ASN1_F_D2I_NETSCAPE_SPKAC 140 +#define ASN1_F_D2I_NETSCAPE_SPKI 141 +#define ASN1_F_D2I_PKCS7 142 +#define ASN1_F_D2I_PKCS7_DIGEST 143 +#define ASN1_F_D2I_PKCS7_ENCRYPT 144 +#define ASN1_F_D2I_PKCS7_ENC_CONTENT 145 +#define ASN1_F_D2I_PKCS7_ENVELOPE 146 +#define ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL 147 +#define ASN1_F_D2I_PKCS7_RECIP_INFO 148 +#define ASN1_F_D2I_PKCS7_SIGNED 149 +#define ASN1_F_D2I_PKCS7_SIGNER_INFO 150 +#define ASN1_F_D2I_PKCS7_SIGN_ENVELOPE 151 +#define ASN1_F_D2I_PRIVATEKEY 152 +#define ASN1_F_D2I_PUBLICKEY 153 +#define ASN1_F_D2I_RSAPRIVATEKEY 154 +#define ASN1_F_D2I_RSAPUBLICKEY 155 +#define ASN1_F_D2I_X509 156 +#define ASN1_F_D2I_X509_ALGOR 157 +#define ASN1_F_D2I_X509_ATTRIBUTE 158 +#define ASN1_F_D2I_X509_CINF 159 +#define ASN1_F_D2I_X509_CRL 160 +#define ASN1_F_D2I_X509_CRL_INFO 161 +#define ASN1_F_D2I_X509_EXTENSION 162 +#define ASN1_F_D2I_X509_KEY 163 +#define ASN1_F_D2I_X509_NAME 164 +#define ASN1_F_D2I_X509_NAME_ENTRY 165 +#define ASN1_F_D2I_X509_PKEY 166 +#define ASN1_F_D2I_X509_PUBKEY 167 +#define ASN1_F_D2I_X509_REQ 168 +#define ASN1_F_D2I_X509_REQ_INFO 169 +#define ASN1_F_D2I_X509_REVOKED 170 +#define ASN1_F_D2I_X509_SIG 171 +#define ASN1_F_D2I_X509_VAL 172 +#define ASN1_F_I2A_ASN1_OBJECT 173 +#define ASN1_F_I2D_ASN1_HEADER 174 +#define ASN1_F_I2D_DHPARAMS 175 +#define ASN1_F_I2D_DSAPARAMS 176 +#define ASN1_F_I2D_DSAPRIVATEKEY 177 +#define ASN1_F_I2D_DSAPUBLICKEY 178 +#define ASN1_F_I2D_NETSCAPE_RSA 179 +#define ASN1_F_I2D_PKCS7 180 +#define ASN1_F_I2D_PRIVATEKEY 181 +#define ASN1_F_I2D_PUBLICKEY 182 +#define ASN1_F_I2D_RSAPRIVATEKEY 183 +#define ASN1_F_I2D_RSAPUBLICKEY 184 +#define ASN1_F_I2D_X509_ATTRIBUTE 185 +#define ASN1_F_NETSCAPE_PKEY_NEW 186 +#define ASN1_F_NETSCAPE_SPKAC_NEW 187 +#define ASN1_F_NETSCAPE_SPKI_NEW 188 +#define ASN1_F_PKCS7_DIGEST_NEW 189 +#define ASN1_F_PKCS7_ENCRYPT_NEW 190 +#define ASN1_F_PKCS7_ENC_CONTENT_NEW 191 +#define ASN1_F_PKCS7_ENVELOPE_NEW 192 +#define ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW 193 +#define ASN1_F_PKCS7_NEW 194 +#define ASN1_F_PKCS7_RECIP_INFO_NEW 195 +#define ASN1_F_PKCS7_SIGNED_NEW 196 +#define ASN1_F_PKCS7_SIGNER_INFO_NEW 197 +#define ASN1_F_PKCS7_SIGN_ENVELOPE_NEW 198 +#define ASN1_F_X509_ALGOR_NEW 199 +#define ASN1_F_X509_ATTRIBUTE_NEW 200 +#define ASN1_F_X509_CINF_NEW 201 +#define ASN1_F_X509_CRL_INFO_NEW 202 +#define ASN1_F_X509_CRL_NEW 203 +#define ASN1_F_X509_DHPARAMS_NEW 204 +#define ASN1_F_X509_EXTENSION_NEW 205 +#define ASN1_F_X509_INFO_NEW 206 +#define ASN1_F_X509_KEY_NEW 207 +#define ASN1_F_X509_NAME_ENTRY_NEW 208 +#define ASN1_F_X509_NAME_NEW 209 +#define ASN1_F_X509_NEW 210 +#define ASN1_F_X509_PKEY_NEW 211 +#define ASN1_F_X509_PUBKEY_NEW 212 +#define ASN1_F_X509_REQ_INFO_NEW 213 +#define ASN1_F_X509_REQ_NEW 214 +#define ASN1_F_X509_REVOKED_NEW 215 +#define ASN1_F_X509_SIG_NEW 216 +#define ASN1_F_X509_VAL_FREE 217 +#define ASN1_F_X509_VAL_NEW 218 + +/* Reason codes. */ +#define ASN1_R_BAD_CLASS 100 +#define ASN1_R_BAD_GET_OBJECT 101 +#define ASN1_R_BAD_OBJECT_HEADER 102 +#define ASN1_R_BAD_PASSWORD_READ 103 +#define ASN1_R_BAD_PKCS7_CONTENT 104 +#define ASN1_R_BAD_PKCS7_TYPE 105 +#define ASN1_R_BAD_TAG 106 +#define ASN1_R_BAD_TYPE 107 +#define ASN1_R_BN_LIB 108 +#define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 109 +#define ASN1_R_BUFFER_TOO_SMALL 110 +#define ASN1_R_DECODING_ERROR 111 +#define ASN1_R_ERROR_STACK 112 +#define ASN1_R_EXPECTING_AN_INTEGER 113 +#define ASN1_R_EXPECTING_AN_OBJECT 114 +#define ASN1_R_EXPECTING_AN_OCTET_STRING 115 +#define ASN1_R_EXPECTING_A_BIT_STRING 116 +#define ASN1_R_EXPECTING_A_BOOLEAN 117 +#define ASN1_R_EXPECTING_A_SEQUENCE 118 +#define ASN1_R_EXPECTING_A_UTCTIME 119 +#define ASN1_R_FIRST_NUM_TOO_LARGE 120 +#define ASN1_R_HEADER_TOO_LONG 121 +#define ASN1_R_INVALID_DIGIT 122 +#define ASN1_R_INVALID_SEPARATOR 123 +#define ASN1_R_INVALID_TIME_FORMAT 124 +#define ASN1_R_IV_TOO_LARGE 125 +#define ASN1_R_LENGTH_ERROR 126 +#define ASN1_R_LENGTH_MISMATCH 127 +#define ASN1_R_MISSING_EOS 128 +#define ASN1_R_MISSING_SECOND_NUMBER 129 +#define ASN1_R_NON_HEX_CHARACTERS 130 +#define ASN1_R_NOT_ENOUGH_DATA 131 +#define ASN1_R_ODD_NUMBER_OF_CHARS 132 +#define ASN1_R_PARSING 133 +#define ASN1_R_PRIVATE_KEY_HEADER_MISSING 134 +#define ASN1_R_SECOND_NUMBER_TOO_LARGE 135 +#define ASN1_R_SHORT_LINE 136 +#define ASN1_R_STRING_TOO_SHORT 137 +#define ASN1_R_TAG_VALUE_TOO_HIGH 138 +#define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 139 +#define ASN1_R_TOO_LONG 140 +#define ASN1_R_UNABLE_TO_DECODE_RSA_KEY 141 +#define ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY 142 +#define ASN1_R_UNKNOWN_ATTRIBUTE_TYPE 143 +#define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 144 +#define ASN1_R_UNKNOWN_OBJECT_TYPE 145 +#define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 146 +#define ASN1_R_UNSUPPORTED_CIPHER 147 +#define ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM 148 +#define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 149 +#define ASN1_R_UTCTIME_TOO_LONG 150 +#define ASN1_R_WRONG_PRINTABLE_TYPE 151 +#define ASN1_R_WRONG_TAG 152 +#define ASN1_R_WRONG_TYPE 153 + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/crypto/asn1/asn1_err.c b/crypto/asn1/asn1_err.c new file mode 100644 index 0000000000..5e6a41b952 --- /dev/null +++ b/crypto/asn1/asn1_err.c @@ -0,0 +1,256 @@ +/* lib/asn1/asn1_err.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ +#include +#include "err.h" +#include "asn1.h" + +/* BEGIN ERROR CODES */ +static ERR_STRING_DATA ASN1_str_functs[]= + { +{ERR_PACK(0,ASN1_F_A2D_ASN1_OBJECT,0), "a2d_ASN1_OBJECT"}, +{ERR_PACK(0,ASN1_F_A2I_ASN1_INTEGER,0), "a2i_ASN1_INTEGER"}, +{ERR_PACK(0,ASN1_F_A2I_ASN1_STRING,0), "a2i_ASN1_STRING"}, +{ERR_PACK(0,ASN1_F_ASN1_COLLATE_PRIMATIVE,0), "ASN1_COLLATE_PRIMATIVE"}, +{ERR_PACK(0,ASN1_F_ASN1_D2I_BIO,0), "ASN1_d2i_bio"}, +{ERR_PACK(0,ASN1_F_ASN1_D2I_FP,0), "ASN1_d2i_fp"}, +{ERR_PACK(0,ASN1_F_ASN1_DUP,0), "ASN1_dup"}, +{ERR_PACK(0,ASN1_F_ASN1_GET_OBJECT,0), "ASN1_get_object"}, +{ERR_PACK(0,ASN1_F_ASN1_HEADER_NEW,0), "ASN1_HEADER_new"}, +{ERR_PACK(0,ASN1_F_ASN1_I2D_BIO,0), "ASN1_i2d_bio"}, +{ERR_PACK(0,ASN1_F_ASN1_I2D_FP,0), "ASN1_i2d_fp"}, +{ERR_PACK(0,ASN1_F_ASN1_INTEGER_SET,0), "ASN1_INTEGER_set"}, +{ERR_PACK(0,ASN1_F_ASN1_INTEGER_TO_BN,0), "ASN1_INTEGER_to_BN"}, +{ERR_PACK(0,ASN1_F_ASN1_OBJECT_NEW,0), "ASN1_OBJECT_new"}, +{ERR_PACK(0,ASN1_F_ASN1_SIGN,0), "ASN1_SIGN"}, +{ERR_PACK(0,ASN1_F_ASN1_STRING_NEW,0), "ASN1_STRING_new"}, +{ERR_PACK(0,ASN1_F_ASN1_STRING_TYPE_NEW,0), "ASN1_STRING_type_new"}, +{ERR_PACK(0,ASN1_F_ASN1_TYPE_NEW,0), "ASN1_TYPE_new"}, +{ERR_PACK(0,ASN1_F_ASN1_UTCTIME_NEW,0), "ASN1_UTCTIME_NEW"}, +{ERR_PACK(0,ASN1_F_ASN1_VERIFY,0), "ASN1_VERIFY"}, +{ERR_PACK(0,ASN1_F_BN_TO_ASN1_INTEGER,0), "BN_to_ASN1_INTEGER"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_BIT_STRING,0), "d2i_ASN1_BIT_STRING"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_BOOLEAN,0), "d2i_ASN1_BOOLEAN"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_BYTES,0), "d2i_ASN1_bytes"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_HEADER,0), "d2i_ASN1_HEADER"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_INTEGER,0), "d2i_ASN1_INTEGER"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_OBJECT,0), "d2i_ASN1_OBJECT"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_OCTET_STRING,0), "d2i_ASN1_OCTET_STRING"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_PRINT_TYPE,0), "D2I_ASN1_PRINT_TYPE"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_SET,0), "d2i_ASN1_SET"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE,0), "d2i_ASN1_TYPE"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_TYPE_BYTES,0), "d2i_ASN1_type_bytes"}, +{ERR_PACK(0,ASN1_F_D2I_ASN1_UTCTIME,0), "d2i_ASN1_UTCTIME"}, +{ERR_PACK(0,ASN1_F_D2I_DHPARAMS,0), "D2I_DHPARAMS"}, +{ERR_PACK(0,ASN1_F_D2I_DSAPARAMS,0), "D2I_DSAPARAMS"}, +{ERR_PACK(0,ASN1_F_D2I_DSAPRIVATEKEY,0), "D2I_DSAPRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_D2I_DSAPUBLICKEY,0), "D2I_DSAPUBLICKEY"}, +{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_PKEY,0), "D2I_NETSCAPE_PKEY"}, +{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA,0), "D2I_NETSCAPE_RSA"}, +{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_RSA_2,0), "D2I_NETSCAPE_RSA_2"}, +{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKAC,0), "D2I_NETSCAPE_SPKAC"}, +{ERR_PACK(0,ASN1_F_D2I_NETSCAPE_SPKI,0), "D2I_NETSCAPE_SPKI"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7,0), "D2I_PKCS7"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_DIGEST,0), "D2I_PKCS7_DIGEST"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENCRYPT,0), "D2I_PKCS7_ENCRYPT"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENC_CONTENT,0), "D2I_PKCS7_ENC_CONTENT"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_ENVELOPE,0), "D2I_PKCS7_ENVELOPE"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL,0), "D2I_PKCS7_ISSUER_AND_SERIAL"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_RECIP_INFO,0), "D2I_PKCS7_RECIP_INFO"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNED,0), "D2I_PKCS7_SIGNED"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGNER_INFO,0), "D2I_PKCS7_SIGNER_INFO"}, +{ERR_PACK(0,ASN1_F_D2I_PKCS7_SIGN_ENVELOPE,0), "D2I_PKCS7_SIGN_ENVELOPE"}, +{ERR_PACK(0,ASN1_F_D2I_PRIVATEKEY,0), "D2I_PRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_D2I_PUBLICKEY,0), "D2I_PUBLICKEY"}, +{ERR_PACK(0,ASN1_F_D2I_RSAPRIVATEKEY,0), "D2I_RSAPRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_D2I_RSAPUBLICKEY,0), "D2I_RSAPUBLICKEY"}, +{ERR_PACK(0,ASN1_F_D2I_X509,0), "D2I_X509"}, +{ERR_PACK(0,ASN1_F_D2I_X509_ALGOR,0), "D2I_X509_ALGOR"}, +{ERR_PACK(0,ASN1_F_D2I_X509_ATTRIBUTE,0), "D2I_X509_ATTRIBUTE"}, +{ERR_PACK(0,ASN1_F_D2I_X509_CINF,0), "D2I_X509_CINF"}, +{ERR_PACK(0,ASN1_F_D2I_X509_CRL,0), "D2I_X509_CRL"}, +{ERR_PACK(0,ASN1_F_D2I_X509_CRL_INFO,0), "D2I_X509_CRL_INFO"}, +{ERR_PACK(0,ASN1_F_D2I_X509_EXTENSION,0), "D2I_X509_EXTENSION"}, +{ERR_PACK(0,ASN1_F_D2I_X509_KEY,0), "D2I_X509_KEY"}, +{ERR_PACK(0,ASN1_F_D2I_X509_NAME,0), "D2I_X509_NAME"}, +{ERR_PACK(0,ASN1_F_D2I_X509_NAME_ENTRY,0), "D2I_X509_NAME_ENTRY"}, +{ERR_PACK(0,ASN1_F_D2I_X509_PKEY,0), "D2I_X509_PKEY"}, +{ERR_PACK(0,ASN1_F_D2I_X509_PUBKEY,0), "D2I_X509_PUBKEY"}, +{ERR_PACK(0,ASN1_F_D2I_X509_REQ,0), "D2I_X509_REQ"}, +{ERR_PACK(0,ASN1_F_D2I_X509_REQ_INFO,0), "D2I_X509_REQ_INFO"}, +{ERR_PACK(0,ASN1_F_D2I_X509_REVOKED,0), "D2I_X509_REVOKED"}, +{ERR_PACK(0,ASN1_F_D2I_X509_SIG,0), "D2I_X509_SIG"}, +{ERR_PACK(0,ASN1_F_D2I_X509_VAL,0), "D2I_X509_VAL"}, +{ERR_PACK(0,ASN1_F_I2A_ASN1_OBJECT,0), "i2a_ASN1_OBJECT"}, +{ERR_PACK(0,ASN1_F_I2D_ASN1_HEADER,0), "i2d_ASN1_HEADER"}, +{ERR_PACK(0,ASN1_F_I2D_DHPARAMS,0), "I2D_DHPARAMS"}, +{ERR_PACK(0,ASN1_F_I2D_DSAPARAMS,0), "I2D_DSAPARAMS"}, +{ERR_PACK(0,ASN1_F_I2D_DSAPRIVATEKEY,0), "I2D_DSAPRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_I2D_DSAPUBLICKEY,0), "I2D_DSAPUBLICKEY"}, +{ERR_PACK(0,ASN1_F_I2D_NETSCAPE_RSA,0), "I2D_NETSCAPE_RSA"}, +{ERR_PACK(0,ASN1_F_I2D_PKCS7,0), "I2D_PKCS7"}, +{ERR_PACK(0,ASN1_F_I2D_PRIVATEKEY,0), "I2D_PRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_I2D_PUBLICKEY,0), "I2D_PUBLICKEY"}, +{ERR_PACK(0,ASN1_F_I2D_RSAPRIVATEKEY,0), "I2D_RSAPRIVATEKEY"}, +{ERR_PACK(0,ASN1_F_I2D_RSAPUBLICKEY,0), "I2D_RSAPUBLICKEY"}, +{ERR_PACK(0,ASN1_F_I2D_X509_ATTRIBUTE,0), "I2D_X509_ATTRIBUTE"}, +{ERR_PACK(0,ASN1_F_NETSCAPE_PKEY_NEW,0), "NETSCAPE_PKEY_NEW"}, +{ERR_PACK(0,ASN1_F_NETSCAPE_SPKAC_NEW,0), "NETSCAPE_SPKAC_NEW"}, +{ERR_PACK(0,ASN1_F_NETSCAPE_SPKI_NEW,0), "NETSCAPE_SPKI_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_DIGEST_NEW,0), "PKCS7_DIGEST_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_ENCRYPT_NEW,0), "PKCS7_ENCRYPT_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_ENC_CONTENT_NEW,0), "PKCS7_ENC_CONTENT_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_ENVELOPE_NEW,0), "PKCS7_ENVELOPE_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW,0), "PKCS7_ISSUER_AND_SERIAL_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_NEW,0), "PKCS7_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_RECIP_INFO_NEW,0), "PKCS7_RECIP_INFO_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_SIGNED_NEW,0), "PKCS7_SIGNED_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_SIGNER_INFO_NEW,0), "PKCS7_SIGNER_INFO_NEW"}, +{ERR_PACK(0,ASN1_F_PKCS7_SIGN_ENVELOPE_NEW,0), "PKCS7_SIGN_ENVELOPE_NEW"}, +{ERR_PACK(0,ASN1_F_X509_ALGOR_NEW,0), "X509_ALGOR_NEW"}, +{ERR_PACK(0,ASN1_F_X509_ATTRIBUTE_NEW,0), "X509_ATTRIBUTE_NEW"}, +{ERR_PACK(0,ASN1_F_X509_CINF_NEW,0), "X509_CINF_NEW"}, +{ERR_PACK(0,ASN1_F_X509_CRL_INFO_NEW,0), "X509_CRL_INFO_NEW"}, +{ERR_PACK(0,ASN1_F_X509_CRL_NEW,0), "X509_CRL_NEW"}, +{ERR_PACK(0,ASN1_F_X509_DHPARAMS_NEW,0), "X509_DHPARAMS_NEW"}, +{ERR_PACK(0,ASN1_F_X509_EXTENSION_NEW,0), "X509_EXTENSION_NEW"}, +{ERR_PACK(0,ASN1_F_X509_INFO_NEW,0), "X509_INFO_NEW"}, +{ERR_PACK(0,ASN1_F_X509_KEY_NEW,0), "X509_KEY_NEW"}, +{ERR_PACK(0,ASN1_F_X509_NAME_ENTRY_NEW,0), "X509_NAME_ENTRY_NEW"}, +{ERR_PACK(0,ASN1_F_X509_NAME_NEW,0), "X509_NAME_NEW"}, +{ERR_PACK(0,ASN1_F_X509_NEW,0), "X509_NEW"}, +{ERR_PACK(0,ASN1_F_X509_PKEY_NEW,0), "X509_PKEY_NEW"}, +{ERR_PACK(0,ASN1_F_X509_PUBKEY_NEW,0), "X509_PUBKEY_NEW"}, +{ERR_PACK(0,ASN1_F_X509_REQ_INFO_NEW,0), "X509_REQ_INFO_NEW"}, +{ERR_PACK(0,ASN1_F_X509_REQ_NEW,0), "X509_REQ_NEW"}, +{ERR_PACK(0,ASN1_F_X509_REVOKED_NEW,0), "X509_REVOKED_NEW"}, +{ERR_PACK(0,ASN1_F_X509_SIG_NEW,0), "X509_SIG_NEW"}, +{ERR_PACK(0,ASN1_F_X509_VAL_FREE,0), "X509_VAL_FREE"}, +{ERR_PACK(0,ASN1_F_X509_VAL_NEW,0), "X509_VAL_NEW"}, +{0,NULL}, + }; + +static ERR_STRING_DATA ASN1_str_reasons[]= + { +{ASN1_R_BAD_CLASS ,"bad class"}, +{ASN1_R_BAD_GET_OBJECT ,"bad get object"}, +{ASN1_R_BAD_OBJECT_HEADER ,"bad object header"}, +{ASN1_R_BAD_PASSWORD_READ ,"bad password read"}, +{ASN1_R_BAD_PKCS7_CONTENT ,"bad pkcs7 content"}, +{ASN1_R_BAD_PKCS7_TYPE ,"bad pkcs7 type"}, +{ASN1_R_BAD_TAG ,"bad tag"}, +{ASN1_R_BAD_TYPE ,"bad type"}, +{ASN1_R_BN_LIB ,"bn lib"}, +{ASN1_R_BOOLEAN_IS_WRONG_LENGTH ,"boolean is wrong length"}, +{ASN1_R_BUFFER_TOO_SMALL ,"buffer too small"}, +{ASN1_R_DECODING_ERROR ,"decoding error"}, +{ASN1_R_ERROR_STACK ,"error stack"}, +{ASN1_R_EXPECTING_AN_INTEGER ,"expecting an integer"}, +{ASN1_R_EXPECTING_AN_OBJECT ,"expecting an object"}, +{ASN1_R_EXPECTING_AN_OCTET_STRING ,"expecting an octet string"}, +{ASN1_R_EXPECTING_A_BIT_STRING ,"expecting a bit string"}, +{ASN1_R_EXPECTING_A_BOOLEAN ,"expecting a boolean"}, +{ASN1_R_EXPECTING_A_SEQUENCE ,"expecting a sequence"}, +{ASN1_R_EXPECTING_A_UTCTIME ,"expecting a utctime"}, +{ASN1_R_FIRST_NUM_TOO_LARGE ,"first num too large"}, +{ASN1_R_HEADER_TOO_LONG ,"header too long"}, +{ASN1_R_INVALID_DIGIT ,"invalid digit"}, +{ASN1_R_INVALID_SEPARATOR ,"invalid separator"}, +{ASN1_R_INVALID_TIME_FORMAT ,"invalid time format"}, +{ASN1_R_IV_TOO_LARGE ,"iv too large"}, +{ASN1_R_LENGTH_ERROR ,"length error"}, +{ASN1_R_LENGTH_MISMATCH ,"length mismatch"}, +{ASN1_R_MISSING_EOS ,"missing eos"}, +{ASN1_R_MISSING_SECOND_NUMBER ,"missing second number"}, +{ASN1_R_NON_HEX_CHARACTERS ,"non hex characters"}, +{ASN1_R_NOT_ENOUGH_DATA ,"not enough data"}, +{ASN1_R_ODD_NUMBER_OF_CHARS ,"odd number of chars"}, +{ASN1_R_PARSING ,"parsing"}, +{ASN1_R_PRIVATE_KEY_HEADER_MISSING ,"private key header missing"}, +{ASN1_R_SECOND_NUMBER_TOO_LARGE ,"second number too large"}, +{ASN1_R_SHORT_LINE ,"short line"}, +{ASN1_R_STRING_TOO_SHORT ,"string too short"}, +{ASN1_R_TAG_VALUE_TOO_HIGH ,"tag value too high"}, +{ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"}, +{ASN1_R_TOO_LONG ,"too long"}, +{ASN1_R_UNABLE_TO_DECODE_RSA_KEY ,"unable to decode rsa key"}, +{ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY ,"unable to decode rsa private key"}, +{ASN1_R_UNKNOWN_ATTRIBUTE_TYPE ,"unknown attribute type"}, +{ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM ,"unknown message digest algorithm"}, +{ASN1_R_UNKNOWN_OBJECT_TYPE ,"unknown object type"}, +{ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE ,"unknown public key type"}, +{ASN1_R_UNSUPPORTED_CIPHER ,"unsupported cipher"}, +{ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM ,"unsupported encryption algorithm"}, +{ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE ,"unsupported public key type"}, +{ASN1_R_UTCTIME_TOO_LONG ,"utctime too long"}, +{ASN1_R_WRONG_PRINTABLE_TYPE ,"wrong printable type"}, +{ASN1_R_WRONG_TAG ,"wrong tag"}, +{ASN1_R_WRONG_TYPE ,"wrong type"}, +{0,NULL}, + }; + +void ERR_load_ASN1_strings() + { + static int init=1; + + if (init) + { + init=0; + ERR_load_strings(ERR_LIB_ASN1,ASN1_str_functs); + ERR_load_strings(ERR_LIB_ASN1,ASN1_str_reasons); + } + } diff --git a/crypto/asn1/asn1_lib.c b/crypto/asn1/asn1_lib.c new file mode 100644 index 0000000000..bdd706bcf1 --- /dev/null +++ b/crypto/asn1/asn1_lib.c @@ -0,0 +1,433 @@ +/* crypto/asn1/asn1_lib.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1.h" +#include "asn1_mac.h" + +#ifndef NOPROTO +static int asn1_get_length(unsigned char **pp,int *inf,long *rl,int max); +static void asn1_put_length(unsigned char **pp, int length); +#else +static int asn1_get_length(); +static void asn1_put_length(); +#endif + +char *ASN1_version="ASN1 part of SSLeay 0.8.1b 29-Jun-1998"; + +int ASN1_check_infinite_end(p,len) +unsigned char **p; +long len; + { + /* If there is 0 or 1 byte left, the length check should pick + * things up */ + if (len <= 0) + return(1); + else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) + { + (*p)+=2; + return(1); + } + return(0); + } + + +int ASN1_get_object(pp, plength, ptag, pclass, omax) +unsigned char **pp; +long *plength; +int *ptag; +int *pclass; +long omax; + { + int i,ret; + long l; + unsigned char *p= *pp; + int tag,xclass,inf; + long max=omax; + + if (!max) goto err; + ret=(*p&V_ASN1_CONSTRUCTED); + xclass=(*p&V_ASN1_PRIVATE); + i= *p&V_ASN1_PRIMATIVE_TAG; + if (i == V_ASN1_PRIMATIVE_TAG) + { /* high-tag */ + p++; + if (--max == 0) goto err; + l=0; + while (*p&0x80) + { + l<<=7L; + l|= *(p++)&0x7f; + if (--max == 0) goto err; + } + l<<=7L; + l|= *(p++)&0x7f; + tag=(int)l; + } + else + { + tag=i; + p++; + if (--max == 0) goto err; + } + *ptag=tag; + *pclass=xclass; + if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err; + +#ifdef undef + fprintf(stderr,"p=%d + *plength=%d > omax=%d + *pp=%d (%d > %d)\n", + p,*plength,omax,*pp,(p+ *plength),omax+ *pp); +#endif + + if ((p+ *plength) > (omax+ *pp)) + { + ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG); + /* Set this so that even if things are not long enough + * the values are set correctly */ + ret|=0x80; + } + *pp=p; + return(ret+inf); +err: + ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG); + return(0x80); + } + +static int asn1_get_length(pp,inf,rl,max) +unsigned char **pp; +int *inf; +long *rl; +int max; + { + unsigned char *p= *pp; + long ret=0; + int i; + + if (max-- < 1) return(0); + if (*p == 0x80) + { + *inf=1; + ret=0; + p++; + } + else + { + *inf=0; + i= *p&0x7f; + if (*(p++) & 0x80) + { + if (max-- == 0) return(0); + while (i-- > 0) + { + ret<<=8L; + ret|= *(p++); + if (max-- == 0) return(0); + } + } + else + ret=i; + } + *pp=p; + *rl=ret; + return(1); + } + +/* class 0 is constructed + * constructed == 2 for indefinitle length constructed */ +void ASN1_put_object(pp,constructed,length,tag,xclass) +unsigned char **pp; +int constructed; +int length; +int tag; +int xclass; + { + unsigned char *p= *pp; + int i; + + i=(constructed)?V_ASN1_CONSTRUCTED:0; + i|=(xclass&V_ASN1_PRIVATE); + if (tag < 31) + *(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG); + else + { + *(p++)=i|V_ASN1_PRIMATIVE_TAG; + while (tag > 0x7f) + { + *(p++)=(tag&0x7f)|0x80; + tag>>=7; + } + *(p++)=(tag&0x7f); + } + if ((constructed == 2) && (length == 0)) + *(p++)=0x80; /* der_put_length would output 0 instead */ + else + asn1_put_length(&p,length); + *pp=p; + } + +static void asn1_put_length(pp, length) +unsigned char **pp; +int length; + { + unsigned char *p= *pp; + int i,l; + if (length <= 127) + *(p++)=(unsigned char)length; + else + { + l=length; + for (i=0; l > 0; i++) + l>>=8; + *(p++)=i|0x80; + l=i; + while (i-- > 0) + { + p[i]=length&0xff; + length>>=8; + } + p+=l; + } + *pp=p; + } + +int ASN1_object_size(constructed, length, tag) +int constructed; +int length; +int tag; + { + int ret; + + ret=length; + ret++; + if (tag >= 31) + { + while (tag > 0) + { + tag>>=7; + ret++; + } + } + if ((length == 0) && (constructed == 2)) + ret+=2; + ret++; + if (length > 127) + { + while (length > 0) + { + length>>=8; + ret++; + } + } + return(ret); + } + +int asn1_Finish(c) +ASN1_CTX *c; + { + if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) + { + if (!ASN1_check_infinite_end(&c->p,c->slen)) + { + c->error=ASN1_R_MISSING_EOS; + return(0); + } + } + if ( ((c->slen != 0) && !(c->inf & 1)) || + ((c->slen < 0) && (c->inf & 1))) + { + c->error=ASN1_R_LENGTH_MISMATCH; + return(0); + } + return(1); + } + +int asn1_GetSequence(c,length) +ASN1_CTX *c; +long *length; + { + unsigned char *q; + + q=c->p; + c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass), + *length); + if (c->inf & 0x80) + { + c->error=ASN1_R_BAD_GET_OBJECT; + return(0); + } + if (c->tag != V_ASN1_SEQUENCE) + { + c->error=ASN1_R_EXPECTING_A_SEQUENCE; + return(0); + } + (*length)-=(c->p-q); + if (c->max && (*length < 0)) + { + c->error=ASN1_R_LENGTH_MISMATCH; + return(0); + } + if (c->inf == (1|V_ASN1_CONSTRUCTED)) + c->slen= *length+ *(c->pp)-c->p; + c->eos=0; + return(1); + } + +ASN1_STRING *ASN1_STRING_dup(str) +ASN1_STRING *str; + { + ASN1_STRING *ret; + + if (str == NULL) return(NULL); + if ((ret=ASN1_STRING_type_new(str->type)) == NULL) + return(NULL); + if (!ASN1_STRING_set(ret,str->data,str->length)) + { + ASN1_STRING_free(ret); + return(NULL); + } + return(ret); + } + +int ASN1_STRING_set(str,data,len) +ASN1_STRING *str; +unsigned char *data; +int len; + { + char *c; + + if (len < 0) + { + if (data == NULL) + return(0); + else + len=strlen((char *)data); + } + if ((str->length < len) || (str->data == NULL)) + { + c=(char *)str->data; + if (c == NULL) + str->data=(unsigned char *)Malloc(len+1); + else + str->data=(unsigned char *)Realloc(c,len+1); + + if (str->data == NULL) + { + str->data=(unsigned char *)c; + return(0); + } + } + str->length=len; + if (data != NULL) + { + memcpy(str->data,data,len); + /* an alowance for strings :-) */ + str->data[len]='\0'; + } + return(1); + } + +ASN1_STRING *ASN1_STRING_new() + { + return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); + } + + +ASN1_STRING *ASN1_STRING_type_new(type) +int type; + { + ASN1_STRING *ret; + + ret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING)); + if (ret == NULL) + { + ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE); + return(NULL); + } + ret->length=0; + ret->type=type; + ret->data=NULL; + return(ret); + } + +void ASN1_STRING_free(a) +ASN1_STRING *a; + { + if (a == NULL) return; + if (a->data != NULL) Free((char *)a->data); + Free((char *)a); + } + +int ASN1_STRING_cmp(a,b) +ASN1_STRING *a,*b; + { + int i; + + i=(a->length-b->length); + if (i == 0) + { + i=memcmp(a->data,b->data,a->length); + if (i == 0) + return(a->type-b->type); + else + return(i); + } + else + return(i); + } + diff --git a/crypto/asn1/asn1_mac.h b/crypto/asn1/asn1_mac.h new file mode 100644 index 0000000000..e4ce0aaa7c --- /dev/null +++ b/crypto/asn1/asn1_mac.h @@ -0,0 +1,318 @@ +/* crypto/asn1/asn1_mac.h */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#ifndef HEADER_ASN1_MAC_H +#define HEADER_ASN1_MAC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "asn1.h" +#include "x509.h" +#include "pkcs7.h" + +#define M_ASN1_D2I_vars(a,type,func) \ + ASN1_CTX c; \ + type ret=NULL; \ + \ + c.pp=pp; \ + c.error=ASN1_R_ERROR_STACK; \ + if ((a == NULL) || ((*a) == NULL)) \ + { if ((ret=(type)func()) == NULL) goto err; } \ + else ret=(*a); + +#define M_ASN1_D2I_Init() \ + c.p= *pp; \ + c.max=(length == 0)?0:(c.p+length); + +#define M_ASN1_D2I_Finish_2(a) \ + if (!asn1_Finish(&c)) goto err; \ + *pp=c.p; \ + if (a != NULL) (*a)=ret; \ + return(ret); + +#define M_ASN1_D2I_Finish(a,func,e) \ + M_ASN1_D2I_Finish_2(a); \ +err:\ + ASN1err((e),c.error); \ + if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \ + return(NULL) + +#define M_ASN1_D2I_start_sequence() \ + if (!asn1_GetSequence(&c,&length)) goto err; + +#define M_ASN1_D2I_end_sequence() \ + (((c.inf&1) == 0)?(c.slen <= 0): \ + (c.eos=ASN1_check_infinite_end(&c.p,c.slen))) + +#define M_ASN1_D2I_get(b,func) \ + c.q=c.p; \ + if (func(&(b),&c.p,c.slen) == NULL) goto err; \ + c.slen-=(c.p-c.q); + +#define M_ASN1_D2I_get_opt(b,func,type) \ + if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \ + == (V_ASN1_UNIVERSAL|(type)))) \ + { \ + M_ASN1_D2I_get(b,func); \ + } + +#define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \ + if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \ + (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \ + { \ + unsigned char tmp; \ + tmp=M_ASN1_next; \ + M_ASN1_next=(tmp& ~V_ASN1_PRIMATIVE_TAG)|type; \ + M_ASN1_D2I_get(b,func); \ + M_ASN1_next_prev=tmp; \ + } + +#define M_ASN1_D2I_get_set(r,func) \ + M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SET,V_ASN1_UNIVERSAL); + +#define M_ASN1_D2I_get_IMP_set_opt(b,func,tag) \ + if ((c.slen != 0) && \ + (M_ASN1_next == \ + (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\ + { \ + M_ASN1_D2I_get_imp_set(b,func,tag,V_ASN1_CONTEXT_SPECIFIC); \ + } + +#define M_ASN1_D2I_get_seq(r,func) \ + M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + +#define M_ASN1_D2I_get_seq_opt(r,func) \ + if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ + V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\ + { M_ASN1_D2I_get_seq(r,func); } + +#define M_ASN1_D2I_get_IMP_set(r,func,x) \ + M_ASN1_D2I_get_imp_set(r,func,x,V_ASN1_CONTEXT_SPECIFIC); + +#define M_ASN1_D2I_get_imp_set(r,func,a,b) \ + c.q=c.p; \ + if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,a,b) == NULL) \ + goto err; \ + c.slen-=(c.p-c.q); + +#define M_ASN1_D2I_get_set_strings(r,func,a,b) \ + c.q=c.p; \ + if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \ + goto err; \ + c.slen-=(c.p-c.q); + +#define M_ASN1_D2I_get_EXP_opt(r,func,tag) \ + if ((c.slen != 0L) && (M_ASN1_next == \ + (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \ + { \ + int Tinf,Ttag,Tclass; \ + long Tlen; \ + \ + c.q=c.p; \ + Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \ + if (Tinf & 0x80) \ + { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \ + if (func(&(r),&c.p,Tlen) == NULL) \ + goto err; \ + c.slen-=(c.p-c.q); \ + } + +#define M_ASN1_D2I_get_EXP_set_opt(r,func,tag,b) \ + if ((c.slen != 0) && (M_ASN1_next == \ + (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \ + { \ + int Tinf,Ttag,Tclass; \ + long Tlen; \ + \ + c.q=c.p; \ + Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \ + if (Tinf & 0x80) \ + { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \ + if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \ + b,V_ASN1_UNIVERSAL) == NULL) \ + goto err; \ + c.slen-=(c.p-c.q); \ + } + +/* New macros */ +#define M_ASN1_New_Malloc(ret,type) \ + if ((ret=(type *)Malloc(sizeof(type))) == NULL) goto err2; + +#define M_ASN1_New(arg,func) \ + if (((arg)=func()) == NULL) return(NULL) + +#define M_ASN1_New_Error(a) \ +/* err: ASN1err((a),ASN1_R_ERROR_STACK); \ + return(NULL);*/ \ + err2: ASN1err((a),ERR_R_MALLOC_FAILURE); \ + return(NULL) + + +#define M_ASN1_next (*c.p) +#define M_ASN1_next_prev (*c.q) + +/*************************************************/ + +#define M_ASN1_I2D_vars(a) int r=0,ret=0; \ + unsigned char *p; \ + if (a == NULL) return(0) + +/* Length Macros */ +#define M_ASN1_I2D_len(a,f) ret+=f(a,NULL) +#define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f) + +#define M_ASN1_I2D_len_SET(a,f) \ + ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL); + +#define M_ASN1_I2D_len_SEQ(a,f) \ + ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + +#define M_ASN1_I2D_len_SEQ_opt(a,f) \ + if ((a != NULL) && (sk_num(a) != 0)) \ + M_ASN1_I2D_len_SEQ(a,f); + +#define M_ASN1_I2D_len_IMP_set(a,f,x) \ + ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC); + +#define M_ASN1_I2D_len_IMP_set_opt(a,f,x) \ + if ((a != NULL) && (sk_num(a) != 0)) \ + ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC); + +#define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \ + if (a != NULL)\ + { \ + v=f(a,NULL); \ + ret+=ASN1_object_size(1,v,mtag); \ + } + +#define M_ASN1_I2D_len_EXP_set_opt(a,f,mtag,tag,v) \ + if ((a != NULL) && (sk_num(a) != 0))\ + { \ + v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL); \ + ret+=ASN1_object_size(1,v,mtag); \ + } + +/* Put Macros */ +#define M_ASN1_I2D_put(a,f) f(a,&p) + +#define M_ASN1_I2D_put_IMP_opt(a,f,t) \ + if (a != NULL) \ + { \ + unsigned char *q=p; \ + f(a,&p); \ + *q=(V_ASN1_CONTEXT_SPECIFIC|t); \ + } + +#define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\ + V_ASN1_UNIVERSAL) +#define M_ASN1_I2D_put_IMP_set(a,f,x) i2d_ASN1_SET(a,&p,f,x,\ + V_ASN1_CONTEXT_SPECIFIC) + +#define M_ASN1_I2D_put_SEQ(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\ + V_ASN1_UNIVERSAL) + +#define M_ASN1_I2D_put_SEQ_opt(a,f) \ + if ((a != NULL) && (sk_num(a) != 0)) \ + M_ASN1_I2D_put_SEQ(a,f); + +#define M_ASN1_I2D_put_IMP_set_opt(a,f,x) \ + if ((a != NULL) && (sk_num(a) != 0)) \ + { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC); } + +#define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \ + if (a != NULL) \ + { \ + ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \ + f(a,&p); \ + } + +#define M_ASN1_I2D_put_EXP_set_opt(a,f,mtag,tag,v) \ + if ((a != NULL) && (sk_num(a) != 0)) \ + { \ + ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \ + i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL); \ + } + +#define M_ASN1_I2D_seq_total() \ + r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \ + if (pp == NULL) return(r); \ + p= *pp; \ + ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL) + +#define M_ASN1_I2D_INF_seq_start(tag,ctx) \ + *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \ + *(p++)=0x80 + +#define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00 + +#define M_ASN1_I2D_finish() *pp=p; \ + return(r); + +#ifndef NOPROTO +int asn1_GetSequence(ASN1_CTX *c, long *length); +#else +int asn1_GetSequence(); +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/crypto/asn1/asn1_par.c b/crypto/asn1/asn1_par.c new file mode 100644 index 0000000000..c6cbe1dfa5 --- /dev/null +++ b/crypto/asn1/asn1_par.c @@ -0,0 +1,387 @@ +/* crypto/asn1/asn1_par.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "objects.h" +#include "x509.h" + +#ifndef NOPROTO +static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed, + int indent); +static int asn1_parse2(BIO *bp, unsigned char **pp, long length, + int offset, int depth, int indent); +#else +static int asn1_print_info(); +static int asn1_parse2(); +#endif + +static int asn1_print_info(bp, tag, xclass, constructed,indent) +BIO *bp; +int tag; +int xclass; +int constructed; +int indent; + { + static char *fmt="%-18s"; + static char *fmt2="%2d %-15s"; + char *p,str[128],*p2=NULL; + + if (constructed & V_ASN1_CONSTRUCTED) + p="cons: "; + else + p="prim: "; + if (BIO_write(bp,p,6) < 6) goto err; + if (indent) + { + if (indent > 128) indent=128; + memset(str,' ',indent); + if (BIO_write(bp,str,indent) < indent) goto err; + } + + p=str; + if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE) + sprintf(str,"priv [ %d ] ",tag); + else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC) + sprintf(str,"cont [ %d ]",tag); + else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION) + sprintf(str,"appl [ %d ]",tag); + else if ((tag == V_ASN1_EOC) /* && (xclass == V_ASN1_UNIVERSAL) */) + p="EOC"; + else if (tag == V_ASN1_BOOLEAN) + p="BOOLEAN"; + else if (tag == V_ASN1_INTEGER) + p="INTEGER"; + else if (tag == V_ASN1_BIT_STRING) + p="BIT STRING"; + else if (tag == V_ASN1_OCTET_STRING) + p="OCTET STRING"; + else if (tag == V_ASN1_NULL) + p="NULL"; + else if (tag == V_ASN1_OBJECT) + p="OBJECT"; + else if (tag == V_ASN1_SEQUENCE) + p="SEQUENCE"; + else if (tag == V_ASN1_SET) + p="SET"; + else if (tag == V_ASN1_PRINTABLESTRING) + p="PRINTABLESTRING"; + else if (tag == V_ASN1_T61STRING) + p="T61STRING"; + else if (tag == V_ASN1_IA5STRING) + p="IA5STRING"; + else if (tag == V_ASN1_UTCTIME) + p="UTCTIME"; + + /* extras */ + else if (tag == V_ASN1_NUMERICSTRING) + p="NUMERICSTRING"; + else if (tag == V_ASN1_VIDEOTEXSTRING) + p="VIDEOTEXSTRING"; + else if (tag == V_ASN1_GENERALIZEDTIME) + p="GENERALIZEDTIME"; + else if (tag == V_ASN1_GRAPHICSTRING) + p="GRAPHICSTRING"; + else if (tag == V_ASN1_ISO64STRING) + p="ISO64STRING"; + else if (tag == V_ASN1_GENERALSTRING) + p="GENERALSTRING"; + else if (tag == V_ASN1_UNIVERSALSTRING) + p="UNIVERSALSTRING"; + + else + p2="(unknown)"; + + if (p2 != NULL) + { + if (BIO_printf(bp,fmt2,tag,p2) <= 0) goto err; + } + else + { + if (BIO_printf(bp,fmt,p) <= 0) goto err; + } + return(1); +err: + return(0); + } + +int ASN1_parse(bp, pp, len, indent) +BIO *bp; +unsigned char *pp; +long len; +int indent; + { + return(asn1_parse2(bp,&pp,len,0,0,indent)); + } + +static int asn1_parse2(bp, pp, length, offset, depth, indent) +BIO *bp; +unsigned char **pp; +long length; +int offset; +int depth; +int indent; + { + unsigned char *p,*ep,*tot,*op,*opp; + long len; + int tag,xclass,ret=0; + int nl,hl,j,r; + ASN1_OBJECT *o=NULL; + ASN1_OCTET_STRING *os=NULL; + + p= *pp; + tot=p+length; + op=p-1; + while ((p < tot) && (op < p)) + { + op=p; + j=ASN1_get_object(&p,&len,&tag,&xclass,length); +#ifdef LINT + j=j; +#endif + if (j & 0x80) + { + if (BIO_write(bp,"Error in encoding\n",18) <= 0) + goto end; + ret=0; + goto end; + } + hl=(p-op); + length-=hl; + /* if j == 0x21 it is a constructed indefinite length object */ + if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp)) + <= 0) goto end; + + if (j != (V_ASN1_CONSTRUCTED | 1)) + { + if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ", + depth,(long)hl,len) <= 0) + goto end; + } + else + { + if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ", + depth,(long)hl) <= 0) + goto end; + } + if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0)) + goto end; + if (j & V_ASN1_CONSTRUCTED) + { + ep=p+len; + if (BIO_write(bp,"\n",1) <= 0) goto end; + if (len > length) + { + BIO_printf(bp, + "length is greater than %ld\n",length); + ret=0; + goto end; + } + if ((j == 0x21) && (len == 0)) + { + for (;;) + { + r=asn1_parse2(bp,&p,(long)(tot-p), + offset+(p - *pp),depth+1, + indent); + if (r == 0) { ret=0; goto end; } + if ((r == 2) || (p >= tot)) break; + } + } + else + while (p < ep) + { + r=asn1_parse2(bp,&p,(long)len, + offset+(p - *pp),depth+1, + indent); + if (r == 0) { ret=0; goto end; } + } + } + else if (xclass != 0) + { + p+=len; + if (BIO_write(bp,"\n",1) <= 0) goto end; + } + else + { + nl=0; + if ( (tag == V_ASN1_PRINTABLESTRING) || + (tag == V_ASN1_T61STRING) || + (tag == V_ASN1_IA5STRING) || + (tag == V_ASN1_UTCTIME)) + { + if (BIO_write(bp,":",1) <= 0) goto end; + if ((len > 0) && + BIO_write(bp,(char *)p,(int)len) + != (int)len) + goto end; + } + else if (tag == V_ASN1_OBJECT) + { + opp=op; + if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL) + { + if (BIO_write(bp,":",1) <= 0) goto end; + i2a_ASN1_OBJECT(bp,o); + } + else + { + if (BIO_write(bp,":BAD OBJECT",11) <= 0) + goto end; + } + } + else if (tag == V_ASN1_BOOLEAN) + { + int ii; + + opp=op; + ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl); + if (ii < 0) + { + if (BIO_write(bp,"Bad boolean\n",12)) + goto end; + } + BIO_printf(bp,":%d",ii); + } + else if (tag == V_ASN1_OCTET_STRING) + { + int i,printable=1; + + opp=op; + os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl); + if (os != NULL) + { + opp=os->data; + for (i=0; ilength; i++) + { + if (( (opp[i] < ' ') && + (opp[i] != '\n') && + (opp[i] != '\r') && + (opp[i] != '\t')) || + (opp[i] > '~')) + { + printable=0; + break; + } + } + if (printable && (os->length > 0)) + { + if (BIO_write(bp,":",1) <= 0) + goto end; + if (BIO_write(bp,(char *)opp, + os->length) <= 0) + goto end; + } + ASN1_OCTET_STRING_free(os); + os=NULL; + } + } + else if (tag == V_ASN1_INTEGER) + { + ASN1_INTEGER *bs; + int i; + + opp=op; + bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl); + if (bs != NULL) + { + if (BIO_write(bp,":",1) <= 0) goto end; + if (bs->type == V_ASN1_NEG_INTEGER) + if (BIO_write(bp,"-",1) <= 0) + goto end; + for (i=0; ilength; i++) + { + if (BIO_printf(bp,"%02X", + bs->data[i]) <= 0) + goto end; + } + if (bs->length == 0) + { + if (BIO_write(bp,"00",2) <= 0) + goto end; + } + } + else + { + if (BIO_write(bp,"BAD INTEGER",11) <= 0) + goto end; + } + ASN1_INTEGER_free(bs); + } + + if (!nl) + { + if (BIO_write(bp,"\n",1) <= 0) goto end; + } + p+=len; + if ((tag == V_ASN1_EOC) && (xclass == 0)) + { + ret=2; /* End of sequence */ + goto end; + } + } + length-=len; + } + ret=1; +end: + if (o != NULL) ASN1_OBJECT_free(o); + if (os != NULL) ASN1_OCTET_STRING_free(os); + *pp=p; + return(ret); + } diff --git a/crypto/asn1/d2i_dhp.c b/crypto/asn1/d2i_dhp.c new file mode 100644 index 0000000000..6ae3e0efa3 --- /dev/null +++ b/crypto/asn1/d2i_dhp.c @@ -0,0 +1,108 @@ +/* crypto/asn1/d2i_dhp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dh.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_DHPARAMS,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_DHPARAMS,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +DH *d2i_DHparams(a,pp,length) +DH **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_ERROR_STACK; + ASN1_INTEGER *bs=NULL; + long v=0; + M_ASN1_D2I_vars(a,DH *,DH_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn; + + if (!M_ASN1_D2I_end_sequence()) + { + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + for (i=0; ilength; i++) + v=(v<<8)|(bs->data[i]); + ret->length=(int)v; + } + + ASN1_BIT_STRING_free(bs); + + M_ASN1_D2I_Finish_2(a); + +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_DHPARAMS,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) DH_free(ret); + if (bs != NULL) ASN1_BIT_STRING_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/d2i_dsap.c b/crypto/asn1/d2i_dsap.c new file mode 100644 index 0000000000..d0732af23a --- /dev/null +++ b/crypto/asn1/d2i_dsap.c @@ -0,0 +1,101 @@ +/* crypto/asn1/d2i_dsap.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_DSAPARAMS,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_DSAPARAMS,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +DSA *d2i_DSAparams(a,pp,length) +DSA **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_ERROR_STACK; + ASN1_INTEGER *bs=NULL; + M_ASN1_D2I_vars(a,DSA *,DSA_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn; + + ASN1_BIT_STRING_free(bs); + + M_ASN1_D2I_Finish_2(a); + +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_DSAPARAMS,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret); + if (bs != NULL) ASN1_BIT_STRING_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/d2i_pr.c b/crypto/asn1/d2i_pr.c new file mode 100644 index 0000000000..0b60aee4c0 --- /dev/null +++ b/crypto/asn1/d2i_pr.c @@ -0,0 +1,117 @@ +/* crypto/asn1/d2i_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "objects.h" +#include "x509.h" + +EVP_PKEY *d2i_PrivateKey(type,a,pp,length) +int type; +EVP_PKEY **a; +unsigned char **pp; +long length; + { + EVP_PKEY *ret; + + if ((a == NULL) || (*a == NULL)) + { + if ((ret=EVP_PKEY_new()) == NULL) + { + ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_EVP_LIB); + return(NULL); + } + } + else ret= *a; + + ret->save_type=type; + ret->type=EVP_PKEY_type(type); + switch (ret->type) + { +#ifndef NO_RSA + case EVP_PKEY_RSA: + if ((ret->pkey.rsa=d2i_RSAPrivateKey(NULL,pp,length)) == NULL) + { + ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB); + goto err; + } + break; +#endif +#ifndef NO_DSA + case EVP_PKEY_DSA: + if ((ret->pkey.dsa=d2i_DSAPrivateKey(NULL,pp,length)) == NULL) + { + ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB); + goto err; + } + break; +#endif + default: + ASN1err(ASN1_F_D2I_PRIVATEKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE); + goto err; + break; + } + if (a != NULL) (*a)=ret; + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret); + return(NULL); + } + diff --git a/crypto/asn1/d2i_pu.c b/crypto/asn1/d2i_pu.c new file mode 100644 index 0000000000..142742e844 --- /dev/null +++ b/crypto/asn1/d2i_pu.c @@ -0,0 +1,117 @@ +/* crypto/asn1/d2i_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "objects.h" +#include "x509.h" + +EVP_PKEY *d2i_PublicKey(type,a,pp,length) +int type; +EVP_PKEY **a; +unsigned char **pp; +long length; + { + EVP_PKEY *ret; + + if ((a == NULL) || (*a == NULL)) + { + if ((ret=EVP_PKEY_new()) == NULL) + { + ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB); + return(NULL); + } + } + else ret= *a; + + ret->save_type=type; + ret->type=EVP_PKEY_type(type); + switch (ret->type) + { +#ifndef NO_RSA + case EVP_PKEY_RSA: + if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,pp,length)) == NULL) + { + ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB); + goto err; + } + break; +#endif +#ifndef NO_DSA + case EVP_PKEY_DSA: + if ((ret->pkey.dsa=d2i_DSAPublicKey(NULL,pp,length)) == NULL) + { + ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB); + goto err; + } + break; +#endif + default: + ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE); + goto err; + break; + } + if (a != NULL) (*a)=ret; + return(ret); +err: + if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret); + return(NULL); + } + diff --git a/crypto/asn1/d2i_r_pr.c b/crypto/asn1/d2i_r_pr.c new file mode 100644 index 0000000000..af95f30a56 --- /dev/null +++ b/crypto/asn1/d2i_r_pr.c @@ -0,0 +1,129 @@ +/* crypto/asn1/d2i_r_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "rsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_RSAPRIVATEKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_RSAPRIVATEKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + * ASN1err(ASN1_F_I2D_RSAPRIVATEKEY,ASN1_R_PARSING); + */ + +static ASN1_METHOD method={ + (int (*)()) i2d_RSAPrivateKey, + (char *(*)())d2i_RSAPrivateKey, + (char *(*)())RSA_new, + (void (*)()) RSA_free}; + +ASN1_METHOD *RSAPrivateKey_asn1_meth() + { + return(&method); + } + +RSA *d2i_RSAPrivateKey(a,pp,length) +RSA **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_PARSING; + ASN1_INTEGER *bs=NULL; + M_ASN1_D2I_vars(a,RSA *,RSA_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if (bs->length == 0) + ret->version=0; + else ret->version=bs->data[0]; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->n=BN_bin2bn(bs->data,bs->length,ret->n)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->e=BN_bin2bn(bs->data,bs->length,ret->e)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->d=BN_bin2bn(bs->data,bs->length,ret->d)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->dmp1=BN_bin2bn(bs->data,bs->length,ret->dmp1)) == NULL) + goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->dmq1=BN_bin2bn(bs->data,bs->length,ret->dmq1)) == NULL) + goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->iqmp=BN_bin2bn(bs->data,bs->length,ret->iqmp)) == NULL) + goto err_bn; + + ASN1_INTEGER_free(bs); + + M_ASN1_D2I_Finish_2(a); +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_RSAPRIVATEKEY,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) RSA_free(ret); + if (bs != NULL) ASN1_INTEGER_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/d2i_r_pu.c b/crypto/asn1/d2i_r_pu.c new file mode 100644 index 0000000000..0febef6b36 --- /dev/null +++ b/crypto/asn1/d2i_r_pu.c @@ -0,0 +1,100 @@ +/* crypto/asn1/d2i_r_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "rsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_RSAPUBLICKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_RSAPUBLICKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +RSA *d2i_RSAPublicKey(a,pp,length) +RSA **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_PARSING; + ASN1_INTEGER *bs=NULL; + M_ASN1_D2I_vars(a,RSA *,RSA_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->n=BN_bin2bn(bs->data,bs->length,ret->n)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->e=BN_bin2bn(bs->data,bs->length,ret->e)) == NULL) goto err_bn; + + ASN1_INTEGER_free(bs); + bs=NULL; + + M_ASN1_D2I_Finish_2(a); + +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_RSAPUBLICKEY,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) RSA_free(ret); + if (bs != NULL) ASN1_INTEGER_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/d2i_s_pr.c b/crypto/asn1/d2i_s_pr.c new file mode 100644 index 0000000000..987db4e3c2 --- /dev/null +++ b/crypto/asn1/d2i_s_pr.c @@ -0,0 +1,113 @@ +/* crypto/asn1/d2i_s_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +/* Origional version from Steven Schoch */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_DSAPRIVATEKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_DSAPRIVATEKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + * ASN1err(ASN1_F_I2D_DSAPRIVATEKEY,ASN1_R_PARSING); + */ + +DSA *d2i_DSAPrivateKey(a,pp,length) +DSA **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_PARSING; + ASN1_INTEGER *bs=NULL; + M_ASN1_D2I_vars(a,DSA *,DSA_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if (bs->length == 0) + ret->version=0; + else ret->version=bs->data[0]; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->pub_key=BN_bin2bn(bs->data,bs->length,ret->pub_key)) + == NULL) goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->priv_key=BN_bin2bn(bs->data,bs->length,ret->priv_key)) + == NULL) goto err_bn; + + ASN1_INTEGER_free(bs); + + M_ASN1_D2I_Finish_2(a); +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_DSAPRIVATEKEY,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret); + if (bs != NULL) ASN1_INTEGER_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/d2i_s_pu.c b/crypto/asn1/d2i_s_pu.c new file mode 100644 index 0000000000..dfffa82030 --- /dev/null +++ b/crypto/asn1/d2i_s_pu.c @@ -0,0 +1,124 @@ +/* crypto/asn1/d2i_s_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +/* Origional version from Steven Schoch */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_DSAPUBLICKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_DSAPUBLICKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +DSA *d2i_DSAPublicKey(a,pp,length) +DSA **a; +unsigned char **pp; +long length; + { + int i=ASN1_R_PARSING; + ASN1_INTEGER *bs=NULL; + M_ASN1_D2I_vars(a,DSA *,DSA_new); + + M_ASN1_D2I_Init(); + if ((length != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) + == (V_ASN1_UNIVERSAL|(V_ASN1_INTEGER)))) + { + c.slen=length; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->pub_key=BN_bin2bn(bs->data,bs->length,ret->pub_key)) + == NULL) + goto err_bn; + ret->write_params=0; + } + else + { + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->pub_key=BN_bin2bn(bs->data,bs->length,ret->pub_key)) + == NULL) + goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) + goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) + goto err_bn; + M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); + if ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) + goto err_bn; + + ret->write_params=1; + } + + ASN1_INTEGER_free(bs); + bs=NULL; + M_ASN1_D2I_Finish_2(a); +err_bn: + i=ERR_R_BN_LIB; +err: + ASN1err(ASN1_F_D2I_DSAPUBLICKEY,i); + if ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret); + if (bs != NULL) ASN1_INTEGER_free(bs); + return(NULL); + } + diff --git a/crypto/asn1/f_int.c b/crypto/asn1/f_int.c new file mode 100644 index 0000000000..f786b12d68 --- /dev/null +++ b/crypto/asn1/f_int.c @@ -0,0 +1,211 @@ +/* crypto/asn1/f_int.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "x509.h" + +int i2a_ASN1_INTEGER(bp, a) +BIO *bp; +ASN1_INTEGER *a; + { + int i,n=0; + static char *h="0123456789ABCDEF"; + char buf[2]; + + if (a == NULL) return(0); + + if (a->length == 0) + { + if (BIO_write(bp,"00",2) != 2) goto err; + n=2; + } + else + { + for (i=0; ilength; i++) + { + if ((i != 0) && (i%35 == 0)) + { + if (BIO_write(bp,"\\\n",2) != 2) goto err; + n+=2; + } + buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; + buf[1]=h[((unsigned char)a->data[i] )&0x0f]; + if (BIO_write(bp,buf,2) != 2) goto err; + n+=2; + } + } + return(n); +err: + return(-1); + } + +int a2i_ASN1_INTEGER(bp,bs,buf,size) +BIO *bp; +ASN1_INTEGER *bs; +char *buf; +int size; + { + int ret=0; + int i,j,k,m,n,again,bufsize; + unsigned char *s=NULL,*sp; + unsigned char *bufp; + int num=0,slen=0,first=1; + + bs->type=V_ASN1_INTEGER; + + bufsize=BIO_gets(bp,buf,size); + for (;;) + { + if (bufsize < 1) goto err_sl; + i=bufsize; + if (buf[i-1] == '\n') buf[--i]='\0'; + if (i == 0) goto err_sl; + if (buf[i-1] == '\r') buf[--i]='\0'; + if (i == 0) goto err_sl; + again=(buf[i-1] == '\\'); + + for (j=0; j= '0') && (buf[j] <= '9')) || + ((buf[j] >= 'a') && (buf[j] <= 'f')) || + ((buf[j] >= 'A') && (buf[j] <= 'F')))) + { + i=j; + break; + } + } + buf[i]='\0'; + /* We have now cleared all the crap off the end of the + * line */ + if (i < 2) goto err_sl; + + bufp=(unsigned char *)buf; + if (first) + { + first=0; + if ((bufp[0] == '0') && (buf[1] == '0')) + { + bufp+=2; + i-=2; + } + } + k=0; + i-=again; + if (i%2 != 0) + { + ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_ODD_NUMBER_OF_CHARS); + goto err; + } + i/=2; + if (num+i > slen) + { + if (s == NULL) + sp=(unsigned char *)Malloc( + (unsigned int)num+i*2); + else + sp=(unsigned char *)Realloc(s, + (unsigned int)num+i*2); + if (sp == NULL) + { + ASN1err(ASN1_F_A2I_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); + if (s != NULL) Free((char *)s); + goto err; + } + s=sp; + slen=num+i*2; + } + for (j=0; j= '0') && (m <= '9')) + m-='0'; + else if ((m >= 'a') && (m <= 'f')) + m=m-'a'+10; + else if ((m >= 'A') && (m <= 'F')) + m=m-'A'+10; + else + { + ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_NON_HEX_CHARACTERS); + goto err; + } + s[num+j]<<=4; + s[num+j]|=m; + } + } + num+=i; + if (again) + bufsize=BIO_gets(bp,buf,size); + else + break; + } + bs->length=num; + bs->data=s; + ret=1; +err: + if (0) + { +err_sl: + ASN1err(ASN1_F_A2I_ASN1_INTEGER,ASN1_R_SHORT_LINE); + } + return(ret); + } + diff --git a/crypto/asn1/f_string.c b/crypto/asn1/f_string.c new file mode 100644 index 0000000000..68001c3245 --- /dev/null +++ b/crypto/asn1/f_string.c @@ -0,0 +1,210 @@ +/* crypto/asn1/f_string.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "x509.h" + +int i2a_ASN1_STRING(bp, a, type) +BIO *bp; +ASN1_STRING *a; +int type; + { + int i,n=0; + static char *h="0123456789ABCDEF"; + char buf[2]; + + if (a == NULL) return(0); + + if (a->length == 0) + { + if (BIO_write(bp,"0",1) != 1) goto err; + n=1; + } + else + { + for (i=0; ilength; i++) + { + if ((i != 0) && (i%35 == 0)) + { + if (BIO_write(bp,"\\\n",2) != 2) goto err; + n+=2; + } + buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; + buf[1]=h[((unsigned char)a->data[i] )&0x0f]; + if (BIO_write(bp,buf,2) != 2) goto err; + n+=2; + } + } + return(n); +err: + return(-1); + } + +int a2i_ASN1_STRING(bp,bs,buf,size) +BIO *bp; +ASN1_STRING *bs; +char *buf; +int size; + { + int ret=0; + int i,j,k,m,n,again,bufsize; + unsigned char *s=NULL,*sp; + unsigned char *bufp; + int num=0,slen=0,first=1; + + bufsize=BIO_gets(bp,buf,size); + for (;;) + { + if (bufsize < 1) + { + if (first) + break; + else + goto err_sl; + } + first=0; + + i=bufsize; + if (buf[i-1] == '\n') buf[--i]='\0'; + if (i == 0) goto err_sl; + if (buf[i-1] == '\r') buf[--i]='\0'; + if (i == 0) goto err_sl; + again=(buf[i-1] == '\\'); + + for (j=i-1; j>0; j--) + { + if (!( ((buf[j] >= '0') && (buf[j] <= '9')) || + ((buf[j] >= 'a') && (buf[j] <= 'f')) || + ((buf[j] >= 'A') && (buf[j] <= 'F')))) + { + i=j; + break; + } + } + buf[i]='\0'; + /* We have now cleared all the crap off the end of the + * line */ + if (i < 2) goto err_sl; + + bufp=(unsigned char *)buf; + + k=0; + i-=again; + if (i%2 != 0) + { + ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_ODD_NUMBER_OF_CHARS); + goto err; + } + i/=2; + if (num+i > slen) + { + if (s == NULL) + sp=(unsigned char *)Malloc( + (unsigned int)num+i*2); + else + sp=(unsigned char *)Realloc(s, + (unsigned int)num+i*2); + if (sp == NULL) + { + ASN1err(ASN1_F_A2I_ASN1_STRING,ERR_R_MALLOC_FAILURE); + if (s != NULL) Free((char *)s); + goto err; + } + s=sp; + slen=num+i*2; + } + for (j=0; j= '0') && (m <= '9')) + m-='0'; + else if ((m >= 'a') && (m <= 'f')) + m=m-'a'+10; + else if ((m >= 'A') && (m <= 'F')) + m=m-'A'+10; + else + { + ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_NON_HEX_CHARACTERS); + goto err; + } + s[num+j]<<=4; + s[num+j]|=m; + } + } + num+=i; + if (again) + bufsize=BIO_gets(bp,buf,size); + else + break; + } + bs->length=num; + bs->data=s; + ret=1; +err: + if (0) + { +err_sl: + ASN1err(ASN1_F_A2I_ASN1_STRING,ASN1_R_SHORT_LINE); + } + return(ret); + } + diff --git a/crypto/asn1/i2d_dhp.c b/crypto/asn1/i2d_dhp.c new file mode 100644 index 0000000000..087b6b8f6c --- /dev/null +++ b/crypto/asn1/i2d_dhp.c @@ -0,0 +1,128 @@ +/* crypto/asn1/i2d_dhp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "asn1_mac.h" +#include "dh.h" + +/* + * ASN1err(ASN1_F_D2I_DHPARAMS,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_DHPARAMS_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_DHparams(a,pp) +DH *a; +unsigned char **pp; + { + BIGNUM *num[3]; + ASN1_INTEGER bs; + unsigned int j,i,tot=0,len,max=0; + int t,ret= -1; + unsigned char *p; + + if (a == NULL) return(0); + num[0]=a->p; + num[1]=a->g; + if (a->length != 0) + { + if ((num[2]=BN_new()) == NULL) goto err; + if (!BN_set_word(num[2],a->length)) goto err; + } + else + num[2]=NULL; + + for (i=0; i<3; i++) + { + if (num[i] == NULL) continue; + j=BN_num_bits(num[i]); + len=((j == 0)?0:((j/8)+1)); + if (len > max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + + p= *pp; + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + bs.type=V_ASN1_INTEGER; + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_DHPARAMS,ERR_R_MALLOC_FAILURE); + goto err; + } + + for (i=0; i<3; i++) + { + if (num[i] == NULL) continue; + bs.length=BN_bn2bin(num[i],bs.data); + i2d_ASN1_INTEGER(&bs,&p); + } + Free((char *)bs.data); + ret=t; +err: + if (num[2] != NULL) BN_free(num[2]); + *pp=p; + return(ret); + } diff --git a/crypto/asn1/i2d_dsap.c b/crypto/asn1/i2d_dsap.c new file mode 100644 index 0000000000..e051c99886 --- /dev/null +++ b/crypto/asn1/i2d_dsap.c @@ -0,0 +1,121 @@ +/* crypto/asn1/i2d_dsap.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "asn1_mac.h" +#include "dsa.h" + +/* + * ASN1err(ASN1_F_D2I_DSAPARAMS,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_DSAparams(a,pp) +DSA *a; +unsigned char **pp; + { + BIGNUM *num[3]; + ASN1_INTEGER bs; + unsigned int j,i,tot=0,len,max=0; + int t,ret= -1; + unsigned char *p; + + if (a == NULL) return(0); + num[0]=a->p; + num[1]=a->q; + num[2]=a->g; + + for (i=0; i<3; i++) + { + if (num[i] == NULL) continue; + j=BN_num_bits(num[i]); + len=((j == 0)?0:((j/8)+1)); + if (len > max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + + p= *pp; + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + bs.type=V_ASN1_INTEGER; + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_DSAPARAMS,ERR_R_MALLOC_FAILURE); + goto err; + } + + for (i=0; i<3; i++) + { + if (num[i] == NULL) continue; + bs.length=BN_bn2bin(num[i],bs.data); + i2d_ASN1_INTEGER(&bs,&p); + } + Free((char *)bs.data); + ret=t; +err: + *pp=p; + return(ret); + } + diff --git a/crypto/asn1/i2d_pr.c b/crypto/asn1/i2d_pr.c new file mode 100644 index 0000000000..361beb9fdd --- /dev/null +++ b/crypto/asn1/i2d_pr.c @@ -0,0 +1,86 @@ +/* crypto/asn1/i2d_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "objects.h" + +int i2d_PrivateKey(a,pp) +EVP_PKEY *a; +unsigned char **pp; + { +#ifndef NO_RSA + if (a->type == EVP_PKEY_RSA) + { + return(i2d_RSAPrivateKey(a->pkey.rsa,pp)); + } + else +#endif +#ifndef NO_DSA + if (a->type == EVP_PKEY_DSA) + { + return(i2d_DSAPrivateKey(a->pkey.dsa,pp)); + } +#endif + + ASN1err(ASN1_F_I2D_PRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE); + return(-1); + } + diff --git a/crypto/asn1/i2d_pu.c b/crypto/asn1/i2d_pu.c new file mode 100644 index 0000000000..2694cd4423 --- /dev/null +++ b/crypto/asn1/i2d_pu.c @@ -0,0 +1,84 @@ +/* crypto/asn1/i2d_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "objects.h" + +int i2d_PublicKey(a,pp) +EVP_PKEY *a; +unsigned char **pp; + { + switch (a->type) + { +#ifndef NO_RSA + case EVP_PKEY_RSA: + return(i2d_RSAPublicKey(a->pkey.rsa,pp)); +#endif +#ifndef NO_DSA + case EVP_PKEY_DSA: + return(i2d_DSAPublicKey(a->pkey.dsa,pp)); +#endif + default: + ASN1err(ASN1_F_I2D_PUBLICKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE); + return(-1); + } + } + diff --git a/crypto/asn1/i2d_r_pr.c b/crypto/asn1/i2d_r_pr.c new file mode 100644 index 0000000000..fa9389760a --- /dev/null +++ b/crypto/asn1/i2d_r_pr.c @@ -0,0 +1,132 @@ +/* crypto/asn1/i2d_r_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "rsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_RSAPRIVATEKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_RSAPRIVATEKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +int i2d_RSAPrivateKey(a,pp) +RSA *a; +unsigned char **pp; + { + BIGNUM *num[9]; + unsigned char data[1]; + ASN1_INTEGER bs; + unsigned int j,i,tot,t,len,max=0; + unsigned char *p; + + if (a == NULL) return(0); + + num[1]=a->n; + num[2]=a->e; + num[3]=a->d; + num[4]=a->p; + num[5]=a->q; + num[6]=a->dmp1; + num[7]=a->dmq1; + num[8]=a->iqmp; + + bs.length=1; + bs.data=data; + bs.type=V_ASN1_INTEGER; + data[0]=a->version&0x7f; + + tot=i2d_ASN1_INTEGER(&(bs),NULL); + for (i=1; i<9; i++) + { + j=BN_num_bits(num[i]); + len=((j == 0)?0:((j/8)+1)); + if (len > max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + + p= *pp; + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + i2d_ASN1_INTEGER(&bs,&p); + + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE); + return(-1); + } + + for (i=1; i<9; i++) + { + bs.length=BN_bn2bin(num[i],bs.data); + i2d_ASN1_INTEGER(&bs,&p); + } + Free((char *)bs.data); + *pp=p; + return(t); + } + diff --git a/crypto/asn1/i2d_r_pu.c b/crypto/asn1/i2d_r_pu.c new file mode 100644 index 0000000000..31dc8363ba --- /dev/null +++ b/crypto/asn1/i2d_r_pu.c @@ -0,0 +1,118 @@ +/* crypto/asn1/i2d_r_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "rsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_RSAPUBLICKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_I2D_RSAPUBLICKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +int i2d_RSAPublicKey(a,pp) +RSA *a; +unsigned char **pp; + { + BIGNUM *num[2]; + ASN1_INTEGER bs; + unsigned int j,i,tot=0,len,max=0,t; + unsigned char *p; + + if (a == NULL) return(0); + + num[0]=a->n; + num[1]=a->e; + + for (i=0; i<2; i++) + { + j=BN_num_bits(num[i]); + len=((j == 0)?0:((j/8)+1)); + if (len > max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + + p= *pp; + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + bs.type=V_ASN1_INTEGER; + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_RSAPUBLICKEY,ERR_R_MALLOC_FAILURE); + return(-1); + } + + for (i=0; i<2; i++) + { + bs.length=BN_bn2bin(num[i],bs.data); + i2d_ASN1_INTEGER(&bs,&p); + } + Free((char *)bs.data); + *pp=p; + return(t); + } + diff --git a/crypto/asn1/i2d_s_pr.c b/crypto/asn1/i2d_s_pr.c new file mode 100644 index 0000000000..0c0a5c6f57 --- /dev/null +++ b/crypto/asn1/i2d_s_pr.c @@ -0,0 +1,128 @@ +/* crypto/asn1/i2d_s_pr.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_I2D_DSAPRIVATEKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +int i2d_DSAPrivateKey(a,pp) +DSA *a; +unsigned char **pp; + { + BIGNUM *num[6]; + unsigned char data[1]; + ASN1_INTEGER bs; + unsigned int j,i,tot,t,len,max=0; + unsigned char *p; + + if (a == NULL) return(0); + + num[1]=a->p; + num[2]=a->q; + num[3]=a->g; + num[4]=a->pub_key; + num[5]=a->priv_key; + + bs.length=1; + bs.data=data; + bs.type=V_ASN1_INTEGER; + data[0]=a->version&0x7f; + + tot=i2d_ASN1_INTEGER(&(bs),NULL); + for (i=1; i<6; i++) + { + j=BN_num_bits(num[i]); + len=((j == 0)?0:((j/8)+1)); + if (len > max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + + p= *pp; + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + i2d_ASN1_INTEGER(&bs,&p); + + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_DSAPRIVATEKEY,ERR_R_MALLOC_FAILURE); + return(-1); + } + + for (i=1; i<6; i++) + { + bs.length=BN_bn2bin(num[i],bs.data); + i2d_ASN1_INTEGER(&bs,&p); + } + Free((char *)bs.data); + *pp=p; + return(t); + } + diff --git a/crypto/asn1/i2d_s_pu.c b/crypto/asn1/i2d_s_pu.c new file mode 100644 index 0000000000..cfb7c11f8b --- /dev/null +++ b/crypto/asn1/i2d_s_pu.c @@ -0,0 +1,133 @@ +/* crypto/asn1/i2d_s_pu.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "bn.h" +#include "dsa.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_I2D_DSAPUBLICKEY,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +int i2d_DSAPublicKey(a,pp) +DSA *a; +unsigned char **pp; + { + BIGNUM *num[4]; + ASN1_INTEGER bs; + unsigned int j,i,tot=0,len,max=0,t=0,all,n=1; + unsigned char *p; + + if (a == NULL) return(0); + + all=a->write_params; + + num[0]=a->pub_key; + if (all) + { + num[1]=a->p; + num[2]=a->q; + num[3]=a->g; + n=4; + } + + for (i=0; i max) max=len; + len=ASN1_object_size(0,len, + (num[i]->neg)?V_ASN1_NEG_INTEGER:V_ASN1_INTEGER); + tot+=len; + } + + if (all) + { + t=ASN1_object_size(1,tot,V_ASN1_SEQUENCE); + if (pp == NULL) return(t); + } + else + { + if (pp == NULL) return(tot); + } + + p= *pp; + if (all) + ASN1_put_object(&p,1,tot,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + bs.type=V_ASN1_INTEGER; + bs.data=(unsigned char *)Malloc(max+4); + if (bs.data == NULL) + { + ASN1err(ASN1_F_I2D_DSAPUBLICKEY,ERR_R_MALLOC_FAILURE); + return(-1); + } + + for (i=0; i +#include "cryptlib.h" +#include "rsa.h" +#include "objects.h" +#include "asn1_mac.h" +#include "evp.h" +#include "x509.h" + + +#ifndef NO_RC4 + +typedef struct netscape_pkey_st + { + ASN1_INTEGER *version; + X509_ALGOR *algor; + ASN1_OCTET_STRING *private_key; + } NETSCAPE_PKEY; + +/* + * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_DECODING_ERROR); + * ASN1err(ASN1_F_D2I_NETSCAPE_PKEY,ASN1_R_DECODING_ERROR); + * ASN1err(ASN1_F_NETSCAPE_PKEY_NEW,ASN1_R_DECODING_ERROR); + */ +#ifndef NOPROTO +static RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length, + int (*cb)()); +static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp); +static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length); +static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void); +static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *); +#else +static RSA *d2i_Netscape_RSA_2(); +static int i2d_NETSCAPE_PKEY(); +static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(); +static NETSCAPE_PKEY *NETSCAPE_PKEY_new(); +static void NETSCAPE_PKEY_free(); +#endif + +int i2d_Netscape_RSA(a,pp,cb) +RSA *a; +unsigned char **pp; +int (*cb)(); + { + int i,j,l[6]; + NETSCAPE_PKEY *pkey; + unsigned char buf[256],*zz; + unsigned char key[EVP_MAX_KEY_LENGTH]; + EVP_CIPHER_CTX ctx; + X509_ALGOR *alg=NULL; + ASN1_OCTET_STRING os,os2; + M_ASN1_I2D_vars(a); + + if (a == NULL) return(0); + +#ifdef WIN32 + r=r; /* shut the damn compiler up :-) */ +#endif + + os.data=os2.data=NULL; + if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err; + if (!ASN1_INTEGER_set(pkey->version,0)) goto err; + + if (pkey->algor->algorithm != NULL) + ASN1_OBJECT_free(pkey->algor->algorithm); + pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption); + if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err; + pkey->algor->parameter->type=V_ASN1_NULL; + + l[0]=i2d_RSAPrivateKey(a,NULL); + pkey->private_key->length=l[0]; + + os2.length=i2d_NETSCAPE_PKEY(pkey,NULL); + l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL); + + if ((alg=X509_ALGOR_new()) == NULL) goto err; + if (alg->algorithm != NULL) + ASN1_OBJECT_free(alg->algorithm); + alg->algorithm=OBJ_nid2obj(NID_rc4); + if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err; + alg->parameter->type=V_ASN1_NULL; + + l[2]=i2d_X509_ALGOR(alg,NULL); + l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE); + + os.data=(unsigned char *)"private-key"; + os.length=11; + l[4]=i2d_ASN1_OCTET_STRING(&os,NULL); + + l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE); + + if (pp == NULL) + { + if (pkey != NULL) NETSCAPE_PKEY_free(pkey); + if (alg != NULL) X509_ALGOR_free(alg); + return(l[5]); + } + + if (pkey->private_key->data != NULL) + Free((char *)pkey->private_key->data); + if ((pkey->private_key->data=(unsigned char *)Malloc(l[0])) == NULL) + { + ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE); + goto err; + } + zz=pkey->private_key->data; + i2d_RSAPrivateKey(a,&zz); + + if ((os2.data=(unsigned char *)Malloc(os2.length)) == NULL) + { + ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE); + goto err; + } + zz=os2.data; + i2d_NETSCAPE_PKEY(pkey,&zz); + + if (cb == NULL) + cb=EVP_read_pw_string; + i=cb(buf,256,"Enter Private Key password:",1); + if (i != 0) + { + ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ); + goto err; + } + EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf, + strlen((char *)buf),1,key,NULL); + memset(buf,0,256); + EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL); + EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length); + EVP_EncryptFinal(&ctx,&(os2.data[i]),&j); + EVP_CIPHER_CTX_cleanup(&ctx); + + p= *pp; + ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + i2d_ASN1_OCTET_STRING(&os,&p); + ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + i2d_X509_ALGOR(alg,&p); + i2d_ASN1_OCTET_STRING(&os2,&p); + ret=l[5]; +err: + if (os2.data != NULL) Free((char *)os2.data); + if (alg != NULL) X509_ALGOR_free(alg); + if (pkey != NULL) NETSCAPE_PKEY_free(pkey); + r=r; + return(ret); + } + +RSA *d2i_Netscape_RSA(a,pp,length,cb) +RSA **a; +unsigned char **pp; +long length; +int (*cb)(); + { + RSA *ret=NULL; + ASN1_OCTET_STRING *os=NULL; + ASN1_CTX c; + + c.pp=pp; + c.error=ASN1_R_DECODING_ERROR; + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING); + if ((os->length != 11) || (strncmp("private-key", + (char *)os->data,os->length) != 0)) + { + ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING); + ASN1_BIT_STRING_free(os); + goto err; + } + ASN1_BIT_STRING_free(os); + c.q=c.p; + if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err; + c.slen-=(c.p-c.q); + + M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA); + } + +static RSA *d2i_Netscape_RSA_2(a,pp,length,cb) +RSA **a; +unsigned char **pp; +long length; +int (*cb)(); + { + NETSCAPE_PKEY *pkey=NULL; + RSA *ret=NULL; + int i,j; + unsigned char buf[256],*zz; + unsigned char key[EVP_MAX_KEY_LENGTH]; + EVP_CIPHER_CTX ctx; + X509_ALGOR *alg=NULL; + ASN1_OCTET_STRING *os=NULL; + ASN1_CTX c; + + c.error=ASN1_R_ERROR_STACK; + c.pp=pp; + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(alg,d2i_X509_ALGOR); + if (OBJ_obj2nid(alg->algorithm) != NID_rc4) + { + ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM); + goto err; + } + M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING); + if (cb == NULL) + cb=EVP_read_pw_string; + i=cb(buf,256,"Enter Private Key password:",0); + if (i != 0) + { + ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ); + goto err; + } + + EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf, + strlen((char *)buf),1,key,NULL); + memset(buf,0,256); + EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL); + EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length); + EVP_DecryptFinal(&ctx,&(os->data[i]),&j); + EVP_CIPHER_CTX_cleanup(&ctx); + os->length=i+j; + + zz=os->data; + + if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL) + { + ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY); + goto err; + } + + zz=pkey->private_key->data; + if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL) + { + ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY); + goto err; + } + if (!asn1_Finish(&c)) goto err; + *pp=c.p; +err: + if (pkey != NULL) NETSCAPE_PKEY_free(pkey); + if (os != NULL) ASN1_BIT_STRING_free(os); + if (alg != NULL) X509_ALGOR_free(alg); + return(ret); + } + +static int i2d_NETSCAPE_PKEY(a,pp) +NETSCAPE_PKEY *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + + M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->private_key, i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->private_key, i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_finish(); + } + +static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(a,pp,length) +NETSCAPE_PKEY **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING); + M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY); + } + +static NETSCAPE_PKEY *NETSCAPE_PKEY_new() + { + NETSCAPE_PKEY *ret=NULL; + + M_ASN1_New_Malloc(ret,NETSCAPE_PKEY); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->algor,X509_ALGOR_new); + M_ASN1_New(ret->private_key,ASN1_OCTET_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW); + } + +static void NETSCAPE_PKEY_free(a) +NETSCAPE_PKEY *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + X509_ALGOR_free(a->algor); + ASN1_OCTET_STRING_free(a->private_key); + Free((char *)a); + } + +#endif /* NO_RC4 */ + diff --git a/crypto/asn1/p7_dgst.c b/crypto/asn1/p7_dgst.c new file mode 100644 index 0000000000..206c2a6bb3 --- /dev/null +++ b/crypto/asn1/p7_dgst.c @@ -0,0 +1,130 @@ +/* crypto/asn1/p7_dgst.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_DIGEST_NEW,ASN1_R_MISSING_EOS); + * ASN1err(ASN1_F_D2I_PKCS7_DIGEST,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_DIGEST(a,pp) +PKCS7_DIGEST *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->md,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->contents,i2d_PKCS7); + M_ASN1_I2D_len(a->digest,i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->md,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->contents,i2d_PKCS7); + M_ASN1_I2D_put(a->digest,i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_finish(); + } + +PKCS7_DIGEST *d2i_PKCS7_DIGEST(a,pp,length) +PKCS7_DIGEST **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_DIGEST *,PKCS7_DIGEST_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->md,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->contents,d2i_PKCS7); + M_ASN1_D2I_get(ret->digest,d2i_ASN1_OCTET_STRING); + + M_ASN1_D2I_Finish(a,PKCS7_DIGEST_free,ASN1_F_D2I_PKCS7_DIGEST); + } + +PKCS7_DIGEST *PKCS7_DIGEST_new() + { + PKCS7_DIGEST *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_DIGEST); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->md,X509_ALGOR_new); + M_ASN1_New(ret->contents,PKCS7_new); + M_ASN1_New(ret->digest,ASN1_OCTET_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_DIGEST_NEW); + } + +void PKCS7_DIGEST_free(a) +PKCS7_DIGEST *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + X509_ALGOR_free(a->md); + PKCS7_free(a->contents); + ASN1_OCTET_STRING_free(a->digest); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_enc.c b/crypto/asn1/p7_enc.c new file mode 100644 index 0000000000..ce4bedb113 --- /dev/null +++ b/crypto/asn1/p7_enc.c @@ -0,0 +1,120 @@ +/* crypto/asn1/p7_enc.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_ENCRYPT_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_ENCRYPT,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_ENCRYPT(a,pp) +PKCS7_ENCRYPT *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->enc_data,i2d_PKCS7_ENC_CONTENT); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->enc_data,i2d_PKCS7_ENC_CONTENT); + + M_ASN1_I2D_finish(); + } + +PKCS7_ENCRYPT *d2i_PKCS7_ENCRYPT(a,pp,length) +PKCS7_ENCRYPT **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_ENCRYPT *,PKCS7_ENCRYPT_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->enc_data,d2i_PKCS7_ENC_CONTENT); + + M_ASN1_D2I_Finish(a,PKCS7_ENCRYPT_free,ASN1_F_D2I_PKCS7_ENCRYPT); + } + +PKCS7_ENCRYPT *PKCS7_ENCRYPT_new() + { + PKCS7_ENCRYPT *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_ENCRYPT); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_ENCRYPT_NEW); + } + +void PKCS7_ENCRYPT_free(a) +PKCS7_ENCRYPT *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + PKCS7_ENC_CONTENT_free(a->enc_data); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_enc_c.c b/crypto/asn1/p7_enc_c.c new file mode 100644 index 0000000000..b27d443533 --- /dev/null +++ b/crypto/asn1/p7_enc_c.c @@ -0,0 +1,127 @@ +/* crypto/asn1/p7_enc_c.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_ENC_CONTENT_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_ENC_CONTENT,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_ENC_CONTENT(a,pp) +PKCS7_ENC_CONTENT *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->content_type,i2d_ASN1_OBJECT); + M_ASN1_I2D_len(a->algorithm,i2d_X509_ALGOR); + M_ASN1_I2D_len_IMP_opt(a->enc_data,i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->content_type,i2d_ASN1_OBJECT); + M_ASN1_I2D_put(a->algorithm,i2d_X509_ALGOR); + M_ASN1_I2D_put_IMP_opt(a->enc_data,i2d_ASN1_OCTET_STRING,0); + + M_ASN1_I2D_finish(); + } + +PKCS7_ENC_CONTENT *d2i_PKCS7_ENC_CONTENT(a,pp,length) +PKCS7_ENC_CONTENT **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_ENC_CONTENT *,PKCS7_ENC_CONTENT_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->content_type,d2i_ASN1_OBJECT); + M_ASN1_D2I_get(ret->algorithm,d2i_X509_ALGOR); + M_ASN1_D2I_get_IMP_opt(ret->enc_data,d2i_ASN1_OCTET_STRING,0, + V_ASN1_OCTET_STRING); + + M_ASN1_D2I_Finish(a,PKCS7_ENC_CONTENT_free, + ASN1_F_D2I_PKCS7_ENC_CONTENT); + } + +PKCS7_ENC_CONTENT *PKCS7_ENC_CONTENT_new() + { + PKCS7_ENC_CONTENT *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_ENC_CONTENT); + M_ASN1_New(ret->content_type,ASN1_OBJECT_new); + M_ASN1_New(ret->algorithm,X509_ALGOR_new); + ret->enc_data=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_ENC_CONTENT_NEW); + } + +void PKCS7_ENC_CONTENT_free(a) +PKCS7_ENC_CONTENT *a; + { + if (a == NULL) return; + ASN1_OBJECT_free(a->content_type); + X509_ALGOR_free(a->algorithm); + ASN1_OCTET_STRING_free(a->enc_data); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_evp.c b/crypto/asn1/p7_evp.c new file mode 100644 index 0000000000..a41d09db77 --- /dev/null +++ b/crypto/asn1/p7_evp.c @@ -0,0 +1,125 @@ +/* crypto/asn1/p7_evp.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_ENVELOPE_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_ENVELOPE,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_ENVELOPE(a,pp) +PKCS7_ENVELOPE *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len_SET(a->recipientinfo,i2d_PKCS7_RECIP_INFO); + M_ASN1_I2D_len(a->enc_data,i2d_PKCS7_ENC_CONTENT); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put_SET(a->recipientinfo,i2d_PKCS7_RECIP_INFO); + M_ASN1_I2D_put(a->enc_data,i2d_PKCS7_ENC_CONTENT); + + M_ASN1_I2D_finish(); + } + +PKCS7_ENVELOPE *d2i_PKCS7_ENVELOPE(a,pp,length) +PKCS7_ENVELOPE **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_ENVELOPE *,PKCS7_ENVELOPE_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get_set(ret->recipientinfo,d2i_PKCS7_RECIP_INFO); + M_ASN1_D2I_get(ret->enc_data,d2i_PKCS7_ENC_CONTENT); + + M_ASN1_D2I_Finish(a,PKCS7_ENVELOPE_free,ASN1_F_D2I_PKCS7_ENVELOPE); + } + +PKCS7_ENVELOPE *PKCS7_ENVELOPE_new() + { + PKCS7_ENVELOPE *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_ENVELOPE); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->recipientinfo,sk_new_null); + M_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_ENVELOPE_NEW); + } + +void PKCS7_ENVELOPE_free(a) +PKCS7_ENVELOPE *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + sk_pop_free(a->recipientinfo,PKCS7_RECIP_INFO_free); + PKCS7_ENC_CONTENT_free(a->enc_data); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_i_s.c b/crypto/asn1/p7_i_s.c new file mode 100644 index 0000000000..413c7e1e69 --- /dev/null +++ b/crypto/asn1/p7_i_s.c @@ -0,0 +1,120 @@ +/* crypto/asn1/p7_i_s.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_ISSUER_AND_SERIAL(a,pp) +PKCS7_ISSUER_AND_SERIAL *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->issuer,i2d_X509_NAME); + M_ASN1_I2D_len(a->serial,i2d_ASN1_INTEGER); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->issuer,i2d_X509_NAME); + M_ASN1_I2D_put(a->serial,i2d_ASN1_INTEGER); + + M_ASN1_I2D_finish(); + } + +PKCS7_ISSUER_AND_SERIAL *d2i_PKCS7_ISSUER_AND_SERIAL(a,pp,length) +PKCS7_ISSUER_AND_SERIAL **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_ISSUER_AND_SERIAL *,PKCS7_ISSUER_AND_SERIAL_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME); + M_ASN1_D2I_get(ret->serial,d2i_ASN1_INTEGER); + M_ASN1_D2I_Finish(a,PKCS7_ISSUER_AND_SERIAL_free, + ASN1_F_D2I_PKCS7_ISSUER_AND_SERIAL); + } + +PKCS7_ISSUER_AND_SERIAL *PKCS7_ISSUER_AND_SERIAL_new() + { + PKCS7_ISSUER_AND_SERIAL *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_ISSUER_AND_SERIAL); + M_ASN1_New(ret->issuer,X509_NAME_new); + M_ASN1_New(ret->serial,ASN1_INTEGER_new); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_ISSUER_AND_SERIAL_NEW); + } + +void PKCS7_ISSUER_AND_SERIAL_free(a) +PKCS7_ISSUER_AND_SERIAL *a; + { + if (a == NULL) return; + X509_NAME_free(a->issuer); + ASN1_INTEGER_free(a->serial); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_lib.c b/crypto/asn1/p7_lib.c new file mode 100644 index 0000000000..b745df0944 --- /dev/null +++ b/crypto/asn1/p7_lib.c @@ -0,0 +1,299 @@ +/* crypto/asn1/p7_lib.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "objects.h" + +/* ASN1err(ASN1_F_D2I_PKCS7,ASN1_R_BAD_PKCS7_CONTENT); + * ASN1err(ASN1_F_I2D_PKCS7,ASN1_R_BAD_PKCS7_TYPE); + * ASN1err(ASN1_F_PKCS7_NEW,ASN1_R_BAD_PKCS7_TYPE); + */ + +int i2d_PKCS7(a,pp) +PKCS7 *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + if (a->asn1 != NULL) + { + if (pp == NULL) + return((int)a->length); + memcpy(*pp,a->asn1,(int)a->length); + *pp+=a->length; + return((int)a->length); + } + + ret+=4; /* sequence, BER header plus '0 0' end padding */ + M_ASN1_I2D_len(a->type,i2d_ASN1_OBJECT); + if (a->d.ptr != NULL) + { + ret+=4; /* explicit tag [ 0 ] BER plus '0 0' */ + switch (OBJ_obj2nid(a->type)) + { + case NID_pkcs7_data: + M_ASN1_I2D_len(a->d.data,i2d_ASN1_OCTET_STRING); + break; + case NID_pkcs7_signed: + M_ASN1_I2D_len(a->d.sign,i2d_PKCS7_SIGNED); + break; + case NID_pkcs7_enveloped: + M_ASN1_I2D_len(a->d.enveloped,i2d_PKCS7_ENVELOPE); + break; + case NID_pkcs7_signedAndEnveloped: + M_ASN1_I2D_len(a->d.signed_and_enveloped, + i2d_PKCS7_SIGN_ENVELOPE); + break; + case NID_pkcs7_digest: + M_ASN1_I2D_len(a->d.digest,i2d_PKCS7_DIGEST); + break; + case NID_pkcs7_encrypted: + M_ASN1_I2D_len(a->d.encrypted,i2d_PKCS7_ENCRYPT); + break; + default: + break; + } + } + r=ret; + if (pp == NULL) return(r); + p= *pp; + M_ASN1_I2D_INF_seq_start(V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + M_ASN1_I2D_put(a->type,i2d_ASN1_OBJECT); + + if (a->d.ptr != NULL) + { + M_ASN1_I2D_INF_seq_start(0,V_ASN1_CONTEXT_SPECIFIC); + switch (OBJ_obj2nid(a->type)) + { + case NID_pkcs7_data: + M_ASN1_I2D_put(a->d.data,i2d_ASN1_OCTET_STRING); + break; + case NID_pkcs7_signed: + M_ASN1_I2D_put(a->d.sign,i2d_PKCS7_SIGNED); + break; + case NID_pkcs7_enveloped: + M_ASN1_I2D_put(a->d.enveloped,i2d_PKCS7_ENVELOPE); + break; + case NID_pkcs7_signedAndEnveloped: + M_ASN1_I2D_put(a->d.signed_and_enveloped, + i2d_PKCS7_SIGN_ENVELOPE); + break; + case NID_pkcs7_digest: + M_ASN1_I2D_put(a->d.digest,i2d_PKCS7_DIGEST); + break; + case NID_pkcs7_encrypted: + M_ASN1_I2D_put(a->d.encrypted,i2d_PKCS7_ENCRYPT); + break; + default: + break; + } + M_ASN1_I2D_INF_seq_end(); + } + M_ASN1_I2D_INF_seq_end(); + M_ASN1_I2D_finish(); + } + +PKCS7 *d2i_PKCS7(a,pp,length) +PKCS7 **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7 *,PKCS7_new); + + if ((a != NULL) && ((*a) != NULL)) + { + if ((*a)->asn1 != NULL) + { + Free((char *)(*a)->asn1); + (*a)->asn1=NULL; + } + (*a)->length=0; + } + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->type,d2i_ASN1_OBJECT); + if (!M_ASN1_D2I_end_sequence()) + { + int Tinf,Ttag,Tclass; + long Tlen; + + if (M_ASN1_next != (V_ASN1_CONSTRUCTED| + V_ASN1_CONTEXT_SPECIFIC|0)) + { + c.error=ASN1_R_BAD_PKCS7_CONTENT; + goto err; + } + + ret->detached=0; + + c.q=c.p; + Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass, + (c.inf & 1)?(length+ *pp-c.q):c.slen); + if (Tinf & 0x80) goto err; + c.slen-=(c.p-c.q); + + switch (OBJ_obj2nid(ret->type)) + { + case NID_pkcs7_data: + M_ASN1_D2I_get(ret->d.data,d2i_ASN1_OCTET_STRING); + break; + case NID_pkcs7_signed: + M_ASN1_D2I_get(ret->d.sign,d2i_PKCS7_SIGNED); + if (ret->d.sign->contents->d.ptr == NULL) + ret->detached=1; + break; + case NID_pkcs7_enveloped: + M_ASN1_D2I_get(ret->d.enveloped,d2i_PKCS7_ENVELOPE); + break; + case NID_pkcs7_signedAndEnveloped: + M_ASN1_D2I_get(ret->d.signed_and_enveloped, + d2i_PKCS7_SIGN_ENVELOPE); + break; + case NID_pkcs7_digest: + M_ASN1_D2I_get(ret->d.digest,d2i_PKCS7_DIGEST); + break; + case NID_pkcs7_encrypted: + M_ASN1_D2I_get(ret->d.encrypted,d2i_PKCS7_ENCRYPT); + break; + default: + c.error=ASN1_R_BAD_PKCS7_TYPE; + goto err; + break; + } + if (Tinf == (1|V_ASN1_CONSTRUCTED)) + { + if (!ASN1_check_infinite_end(&c.p,c.slen)) + { + c.error=ASN1_R_MISSING_EOS; + goto err; + } + } + } + else + ret->detached=1; + + M_ASN1_D2I_Finish(a,PKCS7_free,ASN1_F_D2I_PKCS7); + } + +PKCS7 *PKCS7_new() + { + PKCS7 *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7); + ret->type=ASN1_OBJECT_new(); + ret->asn1=NULL; + ret->length=0; + ret->detached=0; + ret->d.ptr=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_NEW); + } + +void PKCS7_free(a) +PKCS7 *a; + { + if (a == NULL) return; + + PKCS7_content_free(a); + if (a->type != NULL) + { + ASN1_OBJECT_free(a->type); + } + Free((char *)(char *)a); + } + +void PKCS7_content_free(a) +PKCS7 *a; + { + if (a->asn1 != NULL) Free((char *)a->asn1); + + if (a->d.ptr != NULL) + { + if (a->type == NULL) return; + + switch (OBJ_obj2nid(a->type)) + { + case NID_pkcs7_data: + ASN1_OCTET_STRING_free(a->d.data); + break; + case NID_pkcs7_signed: + PKCS7_SIGNED_free(a->d.sign); + break; + case NID_pkcs7_enveloped: + PKCS7_ENVELOPE_free(a->d.enveloped); + break; + case NID_pkcs7_signedAndEnveloped: + PKCS7_SIGN_ENVELOPE_free(a->d.signed_and_enveloped); + break; + case NID_pkcs7_digest: + PKCS7_DIGEST_free(a->d.digest); + break; + case NID_pkcs7_encrypted: + PKCS7_ENCRYPT_free(a->d.encrypted); + break; + default: + /* MEMORY LEAK */ + break; + } + } + a->d.ptr=NULL; + } + diff --git a/crypto/asn1/p7_recip.c b/crypto/asn1/p7_recip.c new file mode 100644 index 0000000000..e4ebda0060 --- /dev/null +++ b/crypto/asn1/p7_recip.c @@ -0,0 +1,130 @@ +/* crypto/asn1/p7_recip.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_RECIP_INFO_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_RECIP_INFO,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_RECIP_INFO(a,pp) +PKCS7_RECIP_INFO *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->issuer_and_serial,i2d_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_I2D_len(a->key_enc_algor,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->enc_key,i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->issuer_and_serial,i2d_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_I2D_put(a->key_enc_algor,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->enc_key,i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_finish(); + } + +PKCS7_RECIP_INFO *d2i_PKCS7_RECIP_INFO(a,pp,length) +PKCS7_RECIP_INFO **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_RECIP_INFO *,PKCS7_RECIP_INFO_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->issuer_and_serial,d2i_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_D2I_get(ret->key_enc_algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->enc_key,d2i_ASN1_OCTET_STRING); + + M_ASN1_D2I_Finish(a,PKCS7_RECIP_INFO_free,ASN1_F_D2I_PKCS7_RECIP_INFO); + } + +PKCS7_RECIP_INFO *PKCS7_RECIP_INFO_new() + { + PKCS7_RECIP_INFO *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_RECIP_INFO); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->issuer_and_serial,PKCS7_ISSUER_AND_SERIAL_new); + M_ASN1_New(ret->key_enc_algor,X509_ALGOR_new); + M_ASN1_New(ret->enc_key,ASN1_OCTET_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_RECIP_INFO_NEW); + } + +void PKCS7_RECIP_INFO_free(a) +PKCS7_RECIP_INFO *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + PKCS7_ISSUER_AND_SERIAL_free(a->issuer_and_serial); + X509_ALGOR_free(a->key_enc_algor); + ASN1_OCTET_STRING_free(a->enc_key); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_s_e.c b/crypto/asn1/p7_s_e.c new file mode 100644 index 0000000000..df5fa3ff8c --- /dev/null +++ b/crypto/asn1/p7_s_e.c @@ -0,0 +1,146 @@ +/* crypto/asn1/p7_s_e.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_SIGN_ENVELOPE,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_SIGN_ENVELOPE(a,pp) +PKCS7_SIGN_ENVELOPE *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len_SET(a->recipientinfo,i2d_PKCS7_RECIP_INFO); + M_ASN1_I2D_len_SET(a->md_algs,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->enc_data,i2d_PKCS7_ENC_CONTENT); + M_ASN1_I2D_len_IMP_set_opt(a->cert,i2d_X509,0); + M_ASN1_I2D_len_IMP_set_opt(a->crl,i2d_X509_CRL,1); + M_ASN1_I2D_len_SET(a->signer_info,i2d_PKCS7_SIGNER_INFO); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put_SET(a->recipientinfo,i2d_PKCS7_RECIP_INFO); + M_ASN1_I2D_put_SET(a->md_algs,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->enc_data,i2d_PKCS7_ENC_CONTENT); + M_ASN1_I2D_put_IMP_set_opt(a->cert,i2d_X509,0); + M_ASN1_I2D_put_IMP_set_opt(a->crl,i2d_X509_CRL,1); + M_ASN1_I2D_put_SET(a->signer_info,i2d_PKCS7_SIGNER_INFO); + + M_ASN1_I2D_finish(); + } + +PKCS7_SIGN_ENVELOPE *d2i_PKCS7_SIGN_ENVELOPE(a,pp,length) +PKCS7_SIGN_ENVELOPE **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_SIGN_ENVELOPE *,PKCS7_SIGN_ENVELOPE_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get_set(ret->recipientinfo,d2i_PKCS7_RECIP_INFO); + M_ASN1_D2I_get_set(ret->md_algs,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->enc_data,d2i_PKCS7_ENC_CONTENT); + M_ASN1_D2I_get_IMP_set_opt(ret->cert,d2i_X509,0); + M_ASN1_D2I_get_IMP_set_opt(ret->crl,d2i_X509_CRL,1); + M_ASN1_D2I_get_set(ret->signer_info,d2i_PKCS7_SIGNER_INFO); + + M_ASN1_D2I_Finish(a,PKCS7_SIGN_ENVELOPE_free, + ASN1_F_D2I_PKCS7_SIGN_ENVELOPE); + } + +PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new() + { + PKCS7_SIGN_ENVELOPE *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_SIGN_ENVELOPE); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->recipientinfo,sk_new_null); + M_ASN1_New(ret->md_algs,sk_new_null); + M_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new); + ret->cert=NULL; + ret->crl=NULL; + M_ASN1_New(ret->signer_info,sk_new_null); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW); + } + +void PKCS7_SIGN_ENVELOPE_free(a) +PKCS7_SIGN_ENVELOPE *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + sk_pop_free(a->recipientinfo,PKCS7_RECIP_INFO_free); + sk_pop_free(a->md_algs,X509_ALGOR_free); + PKCS7_ENC_CONTENT_free(a->enc_data); + sk_pop_free(a->cert,X509_free); + sk_pop_free(a->crl,X509_CRL_free); + sk_pop_free(a->signer_info,PKCS7_SIGNER_INFO_free); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_signd.c b/crypto/asn1/p7_signd.c new file mode 100644 index 0000000000..23dc7ce268 --- /dev/null +++ b/crypto/asn1/p7_signd.c @@ -0,0 +1,140 @@ +/* crypto/asn1/p7_signd.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_SIGNED_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_SIGNED,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_SIGNED(a,pp) +PKCS7_SIGNED *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len_SET(a->md_algs,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->contents,i2d_PKCS7); + M_ASN1_I2D_len_IMP_set_opt(a->cert,i2d_X509,0); + M_ASN1_I2D_len_IMP_set_opt(a->crl,i2d_X509_CRL,1); + M_ASN1_I2D_len_SET(a->signer_info,i2d_PKCS7_SIGNER_INFO); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put_SET(a->md_algs,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->contents,i2d_PKCS7); + M_ASN1_I2D_put_IMP_set_opt(a->cert,i2d_X509,0); + M_ASN1_I2D_put_IMP_set_opt(a->crl,i2d_X509_CRL,1); + M_ASN1_I2D_put_SET(a->signer_info,i2d_PKCS7_SIGNER_INFO); + + M_ASN1_I2D_finish(); + } + +PKCS7_SIGNED *d2i_PKCS7_SIGNED(a,pp,length) +PKCS7_SIGNED **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_SIGNED *,PKCS7_SIGNED_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get_set(ret->md_algs,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->contents,d2i_PKCS7); + M_ASN1_D2I_get_IMP_set_opt(ret->cert,d2i_X509,0); + M_ASN1_D2I_get_IMP_set_opt(ret->crl,d2i_X509_CRL,1); + M_ASN1_D2I_get_set(ret->signer_info,d2i_PKCS7_SIGNER_INFO); + + M_ASN1_D2I_Finish(a,PKCS7_SIGNED_free,ASN1_F_D2I_PKCS7_SIGNED); + } + +PKCS7_SIGNED *PKCS7_SIGNED_new() + { + PKCS7_SIGNED *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_SIGNED); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->md_algs,sk_new_null); + M_ASN1_New(ret->contents,PKCS7_new); + ret->cert=NULL; + ret->crl=NULL; + M_ASN1_New(ret->signer_info,sk_new_null); + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_SIGNED_NEW); + } + +void PKCS7_SIGNED_free(a) +PKCS7_SIGNED *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + sk_pop_free(a->md_algs,X509_ALGOR_free); + PKCS7_free(a->contents); + sk_pop_free(a->cert,X509_free); + sk_pop_free(a->crl,X509_CRL_free); + sk_pop_free(a->signer_info,PKCS7_SIGNER_INFO_free); + Free((char *)a); + } + diff --git a/crypto/asn1/p7_signi.c b/crypto/asn1/p7_signi.c new file mode 100644 index 0000000000..d6682e0359 --- /dev/null +++ b/crypto/asn1/p7_signi.c @@ -0,0 +1,149 @@ +/* crypto/asn1/p7_signi.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_PKCS7_SIGNER_INFO_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_PKCS7_SIGNER_INFO,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_PKCS7_SIGNER_INFO(a,pp) +PKCS7_SIGNER_INFO *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->issuer_and_serial,i2d_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_I2D_len(a->digest_alg,i2d_X509_ALGOR); + M_ASN1_I2D_len_IMP_set_opt(a->auth_attr,i2d_X509_ATTRIBUTE,0); + M_ASN1_I2D_len(a->digest_enc_alg,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->enc_digest,i2d_ASN1_OCTET_STRING); + M_ASN1_I2D_len_IMP_set_opt(a->unauth_attr,i2d_X509_ATTRIBUTE,1); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->issuer_and_serial,i2d_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_I2D_put(a->digest_alg,i2d_X509_ALGOR); + M_ASN1_I2D_put_IMP_set_opt(a->auth_attr,i2d_X509_ATTRIBUTE,0); + M_ASN1_I2D_put(a->digest_enc_alg,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->enc_digest,i2d_ASN1_OCTET_STRING); + M_ASN1_I2D_put_IMP_set_opt(a->unauth_attr,i2d_X509_ATTRIBUTE,1); + + M_ASN1_I2D_finish(); + } + +PKCS7_SIGNER_INFO *d2i_PKCS7_SIGNER_INFO(a,pp,length) +PKCS7_SIGNER_INFO **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,PKCS7_SIGNER_INFO *,PKCS7_SIGNER_INFO_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->issuer_and_serial,d2i_PKCS7_ISSUER_AND_SERIAL); + M_ASN1_D2I_get(ret->digest_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get_IMP_set_opt(ret->auth_attr,d2i_X509_ATTRIBUTE,0); + M_ASN1_D2I_get(ret->digest_enc_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->enc_digest,d2i_ASN1_OCTET_STRING); + M_ASN1_D2I_get_IMP_set_opt(ret->unauth_attr,d2i_X509_ATTRIBUTE,1); + + M_ASN1_D2I_Finish(a,PKCS7_SIGNER_INFO_free, + ASN1_F_D2I_PKCS7_SIGNER_INFO); + } + +PKCS7_SIGNER_INFO *PKCS7_SIGNER_INFO_new() + { + PKCS7_SIGNER_INFO *ret=NULL; + + M_ASN1_New_Malloc(ret,PKCS7_SIGNER_INFO); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->issuer_and_serial,PKCS7_ISSUER_AND_SERIAL_new); + M_ASN1_New(ret->digest_alg,X509_ALGOR_new); + ret->auth_attr=NULL; + M_ASN1_New(ret->digest_enc_alg,X509_ALGOR_new); + M_ASN1_New(ret->enc_digest,ASN1_OCTET_STRING_new); + ret->unauth_attr=NULL; + ret->pkey=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_PKCS7_SIGNER_INFO_NEW); + } + +void PKCS7_SIGNER_INFO_free(a) +PKCS7_SIGNER_INFO *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + PKCS7_ISSUER_AND_SERIAL_free(a->issuer_and_serial); + X509_ALGOR_free(a->digest_alg); + sk_pop_free(a->auth_attr,X509_ATTRIBUTE_free); + X509_ALGOR_free(a->digest_enc_alg); + ASN1_OCTET_STRING_free(a->enc_digest); + sk_pop_free(a->unauth_attr,X509_ATTRIBUTE_free); + if (a->pkey != NULL) + EVP_PKEY_free(a->pkey); + Free((char *)a); + } + diff --git a/crypto/asn1/pk.c b/crypto/asn1/pk.c new file mode 100644 index 0000000000..b96f22d139 --- /dev/null +++ b/crypto/asn1/pk.c @@ -0,0 +1,117 @@ +/* crypto/asn1/pk.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "../error/err.h" +#include "./asn1.h" +#include "rsa.h" +#include "x509.h" +#include "pkcs7.h" + +main() + { + PKCS7 *x; + FILE *in; + unsigned char buf[10240],buf2[10240],*p; + int num,i; + + PKCS7 *nx=NULL,*mx=NULL; + + in=fopen("pkcs7.der","r"); + if (in == NULL) + { + perror("pkcs7.der"); + exit(1); + } + num=fread(buf,1,10240,in); + fclose(in); + + + p=buf; + if (d2i_PKCS7(&nx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf); + +exit(0); + p=buf2; + num=i2d_PKCS7(nx,&p); + printf("num=%d p-buf=%d\n",num,p-buf2); + + if (memcmp(buf,buf2,num) != 0) + { + fprintf(stderr,"data difference\n"); + for (i=0; i\n", + (buf[i] == buf2[i])?' ':'*',i, + buf[i],buf2[i]); + fprintf(stderr,"\n"); + exit(1); + } + + p=buf2; + if (d2i_PKCS7(&mx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf2); + +/* X509_print(stdout,mx);*/ + + exit(0); +err: + ERR_load_crypto_strings(); + ERR_print_errors(stderr); + exit(1); + } + diff --git a/crypto/asn1/pkcs8.c b/crypto/asn1/pkcs8.c new file mode 100644 index 0000000000..bd0d191530 --- /dev/null +++ b/crypto/asn1/pkcs8.c @@ -0,0 +1,139 @@ +/* crypto/asn1/pkcs8.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "objects.h" + +/* + * ASN1err(ASN1_F_D2I_X509_KEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_KEY_NEW,ASN1_R_BAD_GET_OBJECT); + */ + +int i2d_X509_KEY(a,pp) +X509 *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->cert_info, i2d_X509_CINF); + M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->cert_info, i2d_X509_CINF); + M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +X509 *d2i_X509_KEY(a,pp,length) +X509 **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509 *,X509_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->cert_info,d2i_X509_CINF); + M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING); + M_ASN1_D2I_Finish(a,X509_free,ASN1_F_D2I_X509); + } + +X509 *X509_KEY_new() + { + X509_KEY *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_KEY); + ret->references=1; + ret->type=NID + M_ASN1_New(ret->cert_info,X509_CINF_new); + M_ASN1_New(ret->sig_alg,X509_ALGOR_new); + M_ASN1_New(ret->signature,ASN1_BIT_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_NEW); + } + +void X509_KEY_free(a) +X509 *a; + { + int i; + + if (a == NULL) return; + + i=CRYPTO_add_lock(&a->references,-1,CRYPTO_LOCK_X509_KEY); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_KEY_free, bad reference count\n"); + abort(); + } +#endif + + X509_CINF_free(a->cert_info); + X509_ALGOR_free(a->sig_alg); + ASN1_BIT_STRING_free(a->signature); + Free((char *)a); + } + diff --git a/crypto/asn1/t_pkey.c b/crypto/asn1/t_pkey.c new file mode 100644 index 0000000000..09a5abec67 --- /dev/null +++ b/crypto/asn1/t_pkey.c @@ -0,0 +1,389 @@ +/* crypto/asn1/t_pkey.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "bn.h" +#ifndef NO_RSA +#include "rsa.h" +#endif +#ifndef NO_DH +#include "dh.h" +#endif +#ifndef NO_DSA +#include "dsa.h" +#endif + +/* DHerr(DH_F_DHPARAMS_PRINT,ERR_R_MALLOC_FAILURE); + * DSAerr(DSA_F_DSAPARAMS_PRINT,ERR_R_MALLOC_FAILURE); + */ + +#ifndef NOPROTO +static int print(BIO *fp,char *str,BIGNUM *num, + unsigned char *buf,int off); +#else +static int print(); +#endif + +#ifndef NO_RSA +#ifndef WIN16 +int RSA_print_fp(fp,x,off) +FILE *fp; +RSA *x; +int off; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,fp,BIO_NOCLOSE); + ret=RSA_print(b,x,off); + BIO_free(b); + return(ret); + } +#endif + +int RSA_print(bp,x,off) +BIO *bp; +RSA *x; +int off; + { + char str[128],*s; + unsigned char *m=NULL; + int i,ret=0; + + i=RSA_size(x); + m=(unsigned char *)Malloc((unsigned int)i+10); + if (m == NULL) + { + RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE); + goto err; + } + + if (off) + { + if (off > 128) off=128; + memset(str,' ',off); + } + if (x->d != NULL) + { + if (off && (BIO_write(bp,str,off) <= 0)) goto err; + if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n)) + <= 0) goto err; + } + + if (x->d == NULL) + sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n)); + else + strcpy(str,"modulus:"); + if (!print(bp,str,x->n,m,off)) goto err; + s=(x->d == NULL)?"Exponent:":"publicExponent:"; + if (!print(bp,s,x->e,m,off)) goto err; + if (!print(bp,"privateExponent:",x->d,m,off)) goto err; + if (!print(bp,"prime1:",x->p,m,off)) goto err; + if (!print(bp,"prime2:",x->q,m,off)) goto err; + if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err; + if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err; + if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err; + ret=1; +err: + if (m != NULL) Free((char *)m); + return(ret); + } +#endif /* NO_RSA */ + +#ifndef NO_DSA +#ifndef WIN16 +int DSA_print_fp(fp,x,off) +FILE *fp; +DSA *x; +int off; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,fp,BIO_NOCLOSE); + ret=DSA_print(b,x,off); + BIO_free(b); + return(ret); + } +#endif + +int DSA_print(bp,x,off) +BIO *bp; +DSA *x; +int off; + { + char str[128]; + unsigned char *m=NULL; + int i,ret=0; + BIGNUM *bn=NULL; + + if (x->p != NULL) + bn=x->p; + else if (x->priv_key != NULL) + bn=x->priv_key; + else if (x->pub_key != NULL) + bn=x->pub_key; + + /* larger than needed but what the hell :-) */ + if (bn != NULL) + i=BN_num_bytes(bn)*2; + else + i=256; + m=(unsigned char *)Malloc((unsigned int)i+10); + if (m == NULL) + { + DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE); + goto err; + } + + if (off) + { + if (off > 128) off=128; + memset(str,' ',off); + } + if (x->priv_key != NULL) + { + if (off && (BIO_write(bp,str,off) <= 0)) goto err; + if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p)) + <= 0) goto err; + } + + if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off)) + goto err; + if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off)) + goto err; + if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err; + if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err; + if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err; + ret=1; +err: + if (m != NULL) Free((char *)m); + return(ret); + } +#endif /* !NO_DSA */ + +static int print(bp,number,num,buf,off) +BIO *bp; +char *number; +BIGNUM *num; +unsigned char *buf; +int off; + { + int n,i; + char str[128],*neg; + + if (num == NULL) return(1); + neg=(num->neg)?"-":""; + if (off) + { + if (off > 128) off=128; + memset(str,' ',off); + if (BIO_write(bp,str,off) <= 0) return(0); + } + + if (BN_num_bytes(num) <= BN_BYTES) + { + if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg, + (unsigned long)num->d[0],neg,(unsigned long)num->d[0]) + <= 0) return(0); + } + else + { + buf[0]=0; + if (BIO_printf(bp,"%s%s",number, + (neg[0] == '-')?" (Negative)":"") <= 0) + return(0); + n=BN_bn2bin(num,&buf[1]); + + if (buf[1] & 0x80) + n++; + else buf++; + + for (i=0; ip); + m=(unsigned char *)Malloc((unsigned int)i+10); + if (m == NULL) + { + reason=ERR_R_MALLOC_FAILURE; + goto err; + } + + if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n", + BN_num_bits(x->p)) <= 0) + goto err; + if (!print(bp,"prime:",x->p,m,4)) goto err; + if (!print(bp,"generator:",x->g,m,4)) goto err; + if (x->length != 0) + { + if (BIO_printf(bp," recomented-private-length: %d bits\n", + (int)x->length) <= 0) goto err; + } + ret=1; +err: + if (m != NULL) Free((char *)m); + DHerr(DH_F_DHPARAMS_PRINT,reason); + return(ret); + } +#endif + +#ifndef NO_DSA +#ifndef WIN16 +int DSAparams_print_fp(fp,x) +FILE *fp; +DSA *x; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,fp,BIO_NOCLOSE); + ret=DSAparams_print(b, x); + BIO_free(b); + return(ret); + } +#endif + +int DSAparams_print(bp,x) +BIO *bp; +DSA *x; + { + unsigned char *m=NULL; + int reason=ERR_R_BUF_LIB,i,ret=0; + + i=BN_num_bytes(x->p); + m=(unsigned char *)Malloc((unsigned int)i+10); + if (m == NULL) + { + reason=ERR_R_MALLOC_FAILURE; + goto err; + } + + if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n", + BN_num_bits(x->p)) <= 0) + goto err; + if (!print(bp,"p:",x->p,m,4)) goto err; + if (!print(bp,"q:",x->q,m,4)) goto err; + if (!print(bp,"g:",x->g,m,4)) goto err; + ret=1; +err: + if (m != NULL) Free((char *)m); + DSAerr(DSA_F_DSAPARAMS_PRINT,reason); + return(ret); + } + +#endif /* !NO_DSA */ + diff --git a/crypto/asn1/t_req.c b/crypto/asn1/t_req.c new file mode 100644 index 0000000000..922f6b3923 --- /dev/null +++ b/crypto/asn1/t_req.c @@ -0,0 +1,226 @@ +/* crypto/asn1/t_req.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "buffer.h" +#include "bn.h" +#include "objects.h" +#include "x509.h" + +#ifndef WIN16 +int X509_REQ_print_fp(fp,x) +FILE *fp; +X509_REQ *x; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,fp,BIO_NOCLOSE); + ret=X509_REQ_print(b, x); + BIO_free(b); + return(ret); + } +#endif + +int X509_REQ_print(bp,x) +BIO *bp; +X509_REQ *x; + { + unsigned long l; + int i,n; + char *s,*neg; + X509_REQ_INFO *ri; + EVP_PKEY *pkey; + STACK *sk; + char str[128]; + + ri=x->req_info; + sprintf(str,"Certificate Request:\n"); + if (BIO_puts(bp,str) <= 0) goto err; + sprintf(str,"%4sData:\n",""); + if (BIO_puts(bp,str) <= 0) goto err; + + neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":""; + l=0; + for (i=0; iversion->length; i++) + { l<<=8; l+=ri->version->data[i]; } + sprintf(str,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,l); + if (BIO_puts(bp,str) <= 0) goto err; + sprintf(str,"%8sSubject: ",""); + if (BIO_puts(bp,str) <= 0) goto err; + + X509_NAME_print(bp,ri->subject,16); + sprintf(str,"\n%8sSubject Public Key Info:\n",""); + if (BIO_puts(bp,str) <= 0) goto err; + i=OBJ_obj2nid(ri->pubkey->algor->algorithm); + sprintf(str,"%12sPublic Key Algorithm: %s\n","", + (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)); + if (BIO_puts(bp,str) <= 0) goto err; + + pkey=X509_REQ_get_pubkey(x); +#ifndef NO_RSA + if (pkey->type == EVP_PKEY_RSA) + { + BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","", + BN_num_bits(pkey->pkey.rsa->n)); + RSA_print(bp,pkey->pkey.rsa,16); + } + else +#endif +#ifndef NO_DSA + if (pkey->type == EVP_PKEY_DSA) + { + BIO_printf(bp,"%12sDSA Public Key:\n",""); + DSA_print(bp,pkey->pkey.dsa,16); + } + else +#endif + BIO_printf(bp,"%12sUnknown Public Key:\n",""); + + /* may not be */ + sprintf(str,"%8sAttributes:\n",""); + if (BIO_puts(bp,str) <= 0) goto err; + + sk=x->req_info->attributes; + if ((sk == NULL) || (sk_num(sk) == 0)) + { + if (!x->req_info->req_kludge) + { + sprintf(str,"%12sa0:00\n",""); + if (BIO_puts(bp,str) <= 0) goto err; + } + } + else + { + for (i=0; iobject)) > 0) + + if (a->set) + { + ii=0; + count=sk_num(a->value.set); +get_next: + at=(ASN1_TYPE *)sk_value(a->value.set,ii); + type=at->type; + bs=at->value.asn1_string; + } + else + { + t=a->value.single; + type=t->type; + bs=t->value.bit_string; + } + for (j=25-j; j>0; j--) + if (BIO_write(bp," ",1) != 1) goto err; + if (BIO_puts(bp,":") <= 0) goto err; + if ( (type == V_ASN1_PRINTABLESTRING) || + (type == V_ASN1_T61STRING) || + (type == V_ASN1_IA5STRING)) + { + if (BIO_write(bp,(char *)bs->data,bs->length) + != bs->length) + goto err; + BIO_puts(bp,"\n"); + } + else + { + BIO_puts(bp,"unable to print attribute\n"); + } + if (++ii < count) goto get_next; + } + } + + i=OBJ_obj2nid(x->sig_alg->algorithm); + sprintf(str,"%4sSignature Algorithm: %s","", + (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)); + if (BIO_puts(bp,str) <= 0) goto err; + + n=x->signature->length; + s=(char *)x->signature->data; + for (i=0; i +#include "cryptlib.h" +#include "buffer.h" +#include "bn.h" +#ifndef NO_RSA +#include "rsa.h" +#endif +#ifndef NO_DSA +#include "dsa.h" +#endif +#include "objects.h" +#include "x509.h" + +#ifndef WIN16 +int X509_print_fp(fp,x) +FILE *fp; +X509 *x; + { + BIO *b; + int ret; + + if ((b=BIO_new(BIO_s_file())) == NULL) + { + X509err(X509_F_X509_PRINT_FP,ERR_R_BUF_LIB); + return(0); + } + BIO_set_fp(b,fp,BIO_NOCLOSE); + ret=X509_print(b, x); + BIO_free(b); + return(ret); + } +#endif + +int X509_print(bp,x) +BIO *bp; +X509 *x; + { + long l; + int ret=0,i,j,n; + char *m=NULL,*s; + X509_CINF *ci; + ASN1_INTEGER *bs; + EVP_PKEY *pkey=NULL; + char *neg; + X509_EXTENSION *ex; + ASN1_STRING *str=NULL; + + ci=x->cert_info; + if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err; + if (BIO_write(bp," Data:\n",10) <= 0) goto err; + l=X509_get_version(x); + if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err; + if (BIO_write(bp," Serial Number:",22) <= 0) goto err; + + bs=X509_get_serialNumber(x); + if (bs->length <= 4) + { + l=ASN1_INTEGER_get(bs); + if (l < 0) + { + l= -l; + neg="-"; + } + else + neg=""; + if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0) + goto err; + } + else + { + neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":""; + if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err; + + for (i=0; ilength; i++) + { + if (BIO_printf(bp,"%02x%c",bs->data[i], + ((i+1 == bs->length)?'\n':':')) <= 0) + goto err; + } + } + + i=OBJ_obj2nid(ci->signature->algorithm); + if (BIO_printf(bp,"%8sSignature Algorithm: %s\n","", + (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) + goto err; + + if (BIO_write(bp," Issuer: ",16) <= 0) goto err; + if (!X509_NAME_print(bp,X509_get_issuer_name(x),16)) goto err; + if (BIO_write(bp,"\n Validity\n",18) <= 0) goto err; + if (BIO_write(bp," Not Before: ",24) <= 0) goto err; + if (!ASN1_UTCTIME_print(bp,X509_get_notBefore(x))) goto err; + if (BIO_write(bp,"\n Not After : ",25) <= 0) goto err; + if (!ASN1_UTCTIME_print(bp,X509_get_notAfter(x))) goto err; + if (BIO_write(bp,"\n Subject: ",18) <= 0) goto err; + if (!X509_NAME_print(bp,X509_get_subject_name(x),16)) goto err; + if (BIO_write(bp,"\n Subject Public Key Info:\n",34) <= 0) + goto err; + i=OBJ_obj2nid(ci->key->algor->algorithm); + if (BIO_printf(bp,"%12sPublic Key Algorithm: %s\n","", + (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err; + + pkey=X509_get_pubkey(x); +#ifndef NO_RSA + if (pkey->type == EVP_PKEY_RSA) + { + BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","", + BN_num_bits(pkey->pkey.rsa->n)); + RSA_print(bp,pkey->pkey.rsa,16); + } + else +#endif +#ifndef NO_DSA + if (pkey->type == EVP_PKEY_DSA) + { + BIO_printf(bp,"%12sDSA Public Key:\n",""); + DSA_print(bp,pkey->pkey.dsa,16); + } + else +#endif + BIO_printf(bp,"%12sDSA Public Key:\n",""); + + n=X509_get_ext_count(x); + if (n > 0) + { + BIO_printf(bp,"%8sX509v3 extensions:\n",""); + for (i=0; idata, + str->length) <= 0) + goto err; + } + else if (data_type == V_ASN1_BIT_STRING) + { + BIO_printf(bp,"0x"); + for (j=0; jlength; j++) + { + BIO_printf(bp,"%02X", + str->data[j]); + } + } + } + else + { + ASN1_OCTET_STRING_print(bp,ex->value); + } + if (BIO_write(bp,"\n",1) <= 0) goto err; + } + } + + i=OBJ_obj2nid(x->sig_alg->algorithm); + if (BIO_printf(bp,"%4sSignature Algorithm: %s","", + (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err; + + n=x->signature->length; + s=(char *)x->signature->data; + for (i=0; idata; + for (i=0; ilength; i++) + { + if ((p[i] > '~') || ((p[i] < ' ') && + (p[i] != '\n') && (p[i] != '\r'))) + buf[n]='.'; + else + buf[n]=p[i]; + n++; + if (n >= 80) + { + if (BIO_write(bp,buf,n) <= 0) + return(0); + n=0; + } + } + if (n > 0) + if (BIO_write(bp,buf,n) <= 0) + return(0); + return(1); + } + +int ASN1_UTCTIME_print(bp,tm) +BIO *bp; +ASN1_UTCTIME *tm; + { + char *v; + int gmt=0; + static char *mon[12]={ + "Jan","Feb","Mar","Apr","May","Jun", + "Jul","Aug","Sep","Oct","Nov","Dec"}; + int i; + int y=0,M=0,d=0,h=0,m=0,s=0; + + i=tm->length; + v=(char *)tm->data; + + if (i < 10) goto err; + if (v[i-1] == 'Z') gmt=1; + for (i=0; i<10; i++) + if ((v[i] > '9') || (v[i] < '0')) goto err; + y= (v[0]-'0')*10+(v[1]-'0'); + if (y < 70) y+=100; + M= (v[2]-'0')*10+(v[3]-'0'); + if ((M > 12) || (M < 1)) goto err; + d= (v[4]-'0')*10+(v[5]-'0'); + h= (v[6]-'0')*10+(v[7]-'0'); + m= (v[8]-'0')*10+(v[9]-'0'); + if ( (v[10] >= '0') && (v[10] <= '9') && + (v[11] >= '0') && (v[11] <= '9')) + s= (v[10]-'0')*10+(v[11]-'0'); + + if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", + mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0) + return(0); + else + return(1); +err: + BIO_write(bp,"Bad time value",14); + return(0); + } + +int X509_NAME_print(bp,name,obase) +BIO *bp; +X509_NAME *name; +int obase; + { + char *s,*c; + int ret=0,l,ll,i,first=1; + char buf[256]; + + ll=80-2-obase; + + s=X509_NAME_oneline(name,buf,256); + s++; /* skip the first slash */ + + l=ll; + c=s; + for (;;) + { + if ( ((*s == '/') && + ((s[1] >= 'A') && (s[1] <= 'Z') && ( + (s[2] == '=') || + ((s[2] >= 'A') && (s[2] <= 'Z') && + (s[3] == '=')) + ))) || + (*s == '\0')) + { + if ((l <= 0) && !first) + { + first=0; + if (BIO_write(bp,"\n",1) != 1) goto err; + for (i=0; i +#include "../error/err.h" +#include "./asn1.h" +#include "rsa.h" +#include "../x509/x509.h" +#include "x509.h" + +main() + { + main1(); + main2(); + main3(); + main4(); + } + +main1() + { + FILE *in; + unsigned char buf[10240],buf2[10240],*p; + int num,i; + + X509 *nx=NULL,*mx=NULL; + + in=fopen("x.der","r"); + if (in == NULL) + { + perror("x.der"); + exit(1); + } + num=fread(buf,1,10240,in); + fclose(in); + + + p=buf; + if (d2i_X509(&nx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf); + + p=buf2; + num=i2d_X509(nx,&p); + printf("num=%d p-buf=%d\n",num,p-buf2); + + if (memcmp(buf,buf2,num) != 0) + { + fprintf(stderr,"data difference\n"); + for (i=0; i\n", + (buf[i] == buf2[i])?' ':'*',i, + buf[i],buf2[i]); + fprintf(stderr,"\n"); + exit(1); + } + + p=buf2; + if (d2i_X509(&mx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf2); + + return(1); +err: + ERR_load_crypto_strings(); + ERR_print_errors(stderr); + return(0); + } + +main2() + { + FILE *in; + unsigned char buf[10240],buf2[10240],*p; + int num,i; + + X509_CRL *nx=NULL,*mx=NULL; + + in=fopen("crl.der","r"); + if (in == NULL) + { + perror("crl.der"); + exit(1); + } + num=fread(buf,1,10240,in); + fclose(in); + + + p=buf; + if (d2i_X509_CRL(&nx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf); + + p=buf2; + num=i2d_X509_CRL(nx,&p); + printf("num=%d p-buf=%d\n",num,p-buf2); + + if (memcmp(buf,buf2,num) != 0) + { + fprintf(stderr,"data difference\n"); + for (i=0; i\n", + (buf[i] == buf2[i])?' ':'*',i, + buf[i],buf2[i]); + fprintf(stderr,"\n"); + exit(1); + } + + return(1); +err: + ERR_load_crypto_strings(); + ERR_print_errors(stderr); + return(0); + } + +main3() + { + FILE *in; + unsigned char buf[10240],buf2[10240],*p; + int num,i; + + X509_REQ *nx=NULL,*mx=NULL; + + in=fopen("req.der","r"); + if (in == NULL) + { + perror("req.der"); + exit(1); + } + num=fread(buf,1,10240,in); + fclose(in); + + + p=buf; + if (d2i_X509_REQ(&nx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf); + + p=buf2; + num=i2d_X509_REQ(nx,&p); + printf("num=%d p-buf=%d\n",num,p-buf2); + + if (memcmp(buf,buf2,num) != 0) + { + fprintf(stderr,"data difference\n"); + for (i=0; i\n", + (buf[i] == buf2[i])?' ':'*',i, + buf[i],buf2[i]); + fprintf(stderr,"\n"); + exit(1); + } + + return(1); +err: + ERR_load_crypto_strings(); + ERR_print_errors(stderr); + return(0); + } + +main4() + { + FILE *in; + unsigned char buf[10240],buf2[10240],*p; + int num,i; + + RSA *nx=NULL,*mx=NULL; + + in=fopen("rsa.der","r"); + if (in == NULL) + { + perror("rsa.der"); + exit(1); + } + num=fread(buf,1,10240,in); + fclose(in); + + + p=buf; + if (d2i_RSAPrivateKey(&nx,&p,num) == NULL) goto err; + printf("num=%d p-buf=%d\n",num,p-buf); + + p=buf2; + num=i2d_RSAPrivateKey(nx,&p); + printf("num=%d p-buf=%d\n",num,p-buf2); + + if (memcmp(buf,buf2,num) != 0) + { + fprintf(stderr,"data difference\n"); + for (i=0; i\n", + (buf[i] == buf2[i])?' ':'*',i, + buf[i],buf2[i]); + fprintf(stderr,"\n"); + exit(1); + } + + return(1); +err: + ERR_load_crypto_strings(); + ERR_print_errors(stderr); + return(0); + } + diff --git a/crypto/asn1/x_algor.c b/crypto/asn1/x_algor.c new file mode 100644 index 0000000000..1a23812c96 --- /dev/null +++ b/crypto/asn1/x_algor.c @@ -0,0 +1,126 @@ +/* crypto/asn1/x_algor.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_ALGOR_NEW,ASN1_R_EXPECTING_A_SEQUENCE); + * ASN1err(ASN1_F_D2I_X509_ALGOR,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_ALGOR(a,pp) +X509_ALGOR *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->algorithm,i2d_ASN1_OBJECT); + if (a->parameter != NULL) + { M_ASN1_I2D_len(a->parameter,i2d_ASN1_TYPE); } + + M_ASN1_I2D_seq_total(); + M_ASN1_I2D_put(a->algorithm,i2d_ASN1_OBJECT); + if (a->parameter != NULL) + { M_ASN1_I2D_put(a->parameter,i2d_ASN1_TYPE); } + + M_ASN1_I2D_finish(); + } + +X509_ALGOR *d2i_X509_ALGOR(a,pp,length) +X509_ALGOR **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_ALGOR *,X509_ALGOR_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->algorithm,d2i_ASN1_OBJECT); + if (!M_ASN1_D2I_end_sequence()) + { M_ASN1_D2I_get(ret->parameter,d2i_ASN1_TYPE); } + else + { + ASN1_TYPE_free(ret->parameter); + ret->parameter=NULL; + } + M_ASN1_D2I_Finish(a,X509_ALGOR_free,ASN1_F_D2I_X509_ALGOR); + } + +X509_ALGOR *X509_ALGOR_new() + { + X509_ALGOR *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_ALGOR); + M_ASN1_New(ret->algorithm,ASN1_OBJECT_new); + ret->parameter=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_ALGOR_NEW); + } + +void X509_ALGOR_free(a) +X509_ALGOR *a; + { + if (a == NULL) return; + ASN1_OBJECT_free(a->algorithm); + ASN1_TYPE_free(a->parameter); + Free((char *)a); + } + diff --git a/crypto/asn1/x_attrib.c b/crypto/asn1/x_attrib.c new file mode 100644 index 0000000000..bce6f3aad5 --- /dev/null +++ b/crypto/asn1/x_attrib.c @@ -0,0 +1,152 @@ +/* crypto/asn1/x_attrib.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_ATTRIBUTE,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_ATTRIBUTE_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + * ASN1err(ASN1_F_I2D_X509_ATTRIBUTE,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +/* sequence */ +int i2d_X509_ATTRIBUTE(a,pp) +X509_ATTRIBUTE *a; +unsigned char **pp; + { + int k=0; + int r=0,ret=0; + unsigned char **p=NULL; + + if (a == NULL) return(0); + + p=NULL; + for (;;) + { + if (k) + { + r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); + if (pp == NULL) return(r); + p=pp; + ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE, + V_ASN1_UNIVERSAL); + } + + ret+=i2d_ASN1_OBJECT(a->object,p); + if (a->set) + ret+=i2d_ASN1_SET(a->value.set,p,i2d_ASN1_TYPE, + V_ASN1_SET,V_ASN1_UNIVERSAL); + else + ret+=i2d_ASN1_TYPE(a->value.single,p); + if (k++) return(r); + } + } + +X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(a,pp,length) +X509_ATTRIBUTE **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_ATTRIBUTE *,X509_ATTRIBUTE_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT); + + if ((c.slen != 0) && + (M_ASN1_next == (V_ASN1_CONSTRUCTED|V_ASN1_UNIVERSAL|V_ASN1_SET))) + { + ret->set=1; + M_ASN1_D2I_get_set(ret->value.set,d2i_ASN1_TYPE); + } + else + { + ret->set=0; + M_ASN1_D2I_get(ret->value.single,d2i_ASN1_TYPE); + } + + M_ASN1_D2I_Finish(a,X509_ATTRIBUTE_free,ASN1_F_D2I_X509_ATTRIBUTE); + } + +X509_ATTRIBUTE *X509_ATTRIBUTE_new() + { + X509_ATTRIBUTE *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_ATTRIBUTE); + M_ASN1_New(ret->object,ASN1_OBJECT_new); + ret->set=0; + ret->value.ptr=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_ATTRIBUTE_NEW); + } + +void X509_ATTRIBUTE_free(a) +X509_ATTRIBUTE *a; + { + if (a == NULL) return; + ASN1_OBJECT_free(a->object); + if (a->set) + sk_pop_free(a->value.set,ASN1_TYPE_free); + else + ASN1_TYPE_free(a->value.single); + Free((char *)a); + } + diff --git a/crypto/asn1/x_cinf.c b/crypto/asn1/x_cinf.c new file mode 100644 index 0000000000..e5cc2af722 --- /dev/null +++ b/crypto/asn1/x_cinf.c @@ -0,0 +1,197 @@ +/* crypto/asn1/x_cinf.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_CINF,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_CINF_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_CINF(a,pp) +X509_CINF *a; +unsigned char **pp; + { + int v1=0,v2=0; + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1); + M_ASN1_I2D_len(a->serialNumber, i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->signature, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->issuer, i2d_X509_NAME); + M_ASN1_I2D_len(a->validity, i2d_X509_VAL); + M_ASN1_I2D_len(a->subject, i2d_X509_NAME); + M_ASN1_I2D_len(a->key, i2d_X509_PUBKEY); + M_ASN1_I2D_len_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING); + M_ASN1_I2D_len_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING); + M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1); + M_ASN1_I2D_put(a->serialNumber, i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->signature, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->issuer, i2d_X509_NAME); + M_ASN1_I2D_put(a->validity, i2d_X509_VAL); + M_ASN1_I2D_put(a->subject, i2d_X509_NAME); + M_ASN1_I2D_put(a->key, i2d_X509_PUBKEY); + M_ASN1_I2D_put_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING,1); + M_ASN1_I2D_put_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING,2); + M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2); + + M_ASN1_I2D_finish(); + } + +X509_CINF *d2i_X509_CINF(a,pp,length) +X509_CINF **a; +unsigned char **pp; +long length; + { + int ver=0; + M_ASN1_D2I_vars(a,X509_CINF *,X509_CINF_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + /* we have the optional version field */ + if (M_ASN1_next == (V_ASN1_CONTEXT_SPECIFIC | V_ASN1_CONSTRUCTED | 0)) + { + M_ASN1_D2I_get_EXP_opt(ret->version,d2i_ASN1_INTEGER,0); + if (ret->version->data != NULL) + ver=ret->version->data[0]; + } + else + { + if (ret->version != NULL) + { + ASN1_INTEGER_free(ret->version); + ret->version=NULL; + } + } + M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->signature,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME); + M_ASN1_D2I_get(ret->validity,d2i_X509_VAL); + M_ASN1_D2I_get(ret->subject,d2i_X509_NAME); + M_ASN1_D2I_get(ret->key,d2i_X509_PUBKEY); + if (ver >= 1) /* version 2 extensions */ + { + if (ret->issuerUID != NULL) + { + ASN1_BIT_STRING_free(ret->issuerUID); + ret->issuerUID=NULL; + } + if (ret->subjectUID != NULL) + { + ASN1_BIT_STRING_free(ret->subjectUID); + ret->issuerUID=NULL; + } + M_ASN1_D2I_get_IMP_opt(ret->issuerUID,d2i_ASN1_BIT_STRING, 1, + V_ASN1_BIT_STRING); + M_ASN1_D2I_get_IMP_opt(ret->subjectUID,d2i_ASN1_BIT_STRING, 2, + V_ASN1_BIT_STRING); + } + if (ver >= 2) /* version 3 extensions */ + { + if (ret->extensions != NULL) + while (sk_num(ret->extensions)) + X509_EXTENSION_free((X509_EXTENSION *) + sk_pop(ret->extensions)); + M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION,3, + V_ASN1_SEQUENCE); + } + M_ASN1_D2I_Finish(a,X509_CINF_free,ASN1_F_D2I_X509_CINF); + } + +X509_CINF *X509_CINF_new() + { + X509_CINF *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_CINF); + ret->version=NULL; + M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new); + M_ASN1_New(ret->signature,X509_ALGOR_new); + M_ASN1_New(ret->issuer,X509_NAME_new); + M_ASN1_New(ret->validity,X509_VAL_new); + M_ASN1_New(ret->subject,X509_NAME_new); + M_ASN1_New(ret->key,X509_PUBKEY_new); + ret->issuerUID=NULL; + ret->subjectUID=NULL; + ret->extensions=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_CINF_NEW); + } + +void X509_CINF_free(a) +X509_CINF *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + ASN1_INTEGER_free(a->serialNumber); + X509_ALGOR_free(a->signature); + X509_NAME_free(a->issuer); + X509_VAL_free(a->validity); + X509_NAME_free(a->subject); + X509_PUBKEY_free(a->key); + ASN1_BIT_STRING_free(a->issuerUID); + ASN1_BIT_STRING_free(a->subjectUID); + sk_pop_free(a->extensions,X509_EXTENSION_free); + Free((char *)a); + } + diff --git a/crypto/asn1/x_crl.c b/crypto/asn1/x_crl.c new file mode 100644 index 0000000000..22cb99fb00 --- /dev/null +++ b/crypto/asn1/x_crl.c @@ -0,0 +1,347 @@ +/* crypto/asn1/x_crl.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_D2I_X509_CRL,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_X509_CRL_INFO,ASN1_R_EXPECTING_A_SEQUENCE); + * ASN1err(ASN1_F_D2I_X509_REVOKED,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_CRL_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_CRL_INFO_NEW,ASN1_R_EXPECTING_A_SEQUENCE); + * ASN1err(ASN1_F_X509_REVOKED_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +#ifndef NOPROTO +static int X509_REVOKED_cmp(X509_REVOKED **a,X509_REVOKED **b); +static int X509_REVOKED_seq_cmp(X509_REVOKED **a,X509_REVOKED **b); +#else +static int X509_REVOKED_cmp(); +static int X509_REVOKED_seq_cmp(); +#endif + +int i2d_X509_REVOKED(a,pp) +X509_REVOKED *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->serialNumber,i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->revocationDate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_len_SEQ_opt(a->extensions,i2d_X509_EXTENSION); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->serialNumber,i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->revocationDate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_put_SEQ_opt(a->extensions,i2d_X509_EXTENSION); + + M_ASN1_I2D_finish(); + } + +X509_REVOKED *d2i_X509_REVOKED(a,pp,length) +X509_REVOKED **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_REVOKED *,X509_REVOKED_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->revocationDate,d2i_ASN1_UTCTIME); + M_ASN1_D2I_get_seq_opt(ret->extensions,d2i_X509_EXTENSION); + M_ASN1_D2I_Finish(a,X509_REVOKED_free,ASN1_F_D2I_X509_REVOKED); + } + +int i2d_X509_CRL_INFO(a,pp) +X509_CRL_INFO *a; +unsigned char **pp; + { + int v1=0; + long l=0; + M_ASN1_I2D_vars(a); + + if (sk_num(a->revoked) != 0) + qsort((char *)a->revoked->data,sk_num(a->revoked), + sizeof(X509_REVOKED *),(int (*)(P_CC_CC))X509_REVOKED_seq_cmp); + if ((a->version != NULL) && ((l=ASN1_INTEGER_get(a->version)) != 0)) + { + M_ASN1_I2D_len(a->version,i2d_ASN1_INTEGER); + } + M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->issuer,i2d_X509_NAME); + M_ASN1_I2D_len(a->lastUpdate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_len(a->nextUpdate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_len_SEQ_opt(a->revoked,i2d_X509_REVOKED); + M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0, + V_ASN1_SEQUENCE,v1); + + M_ASN1_I2D_seq_total(); + + if ((a->version != NULL) && (l != 0)) + { + M_ASN1_I2D_put(a->version,i2d_ASN1_INTEGER); + } + M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->issuer,i2d_X509_NAME); + M_ASN1_I2D_put(a->lastUpdate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_put(a->nextUpdate,i2d_ASN1_UTCTIME); + M_ASN1_I2D_put_SEQ_opt(a->revoked,i2d_X509_REVOKED); + M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,0, + V_ASN1_SEQUENCE,v1); + + M_ASN1_I2D_finish(); + } + +X509_CRL_INFO *d2i_X509_CRL_INFO(a,pp,length) +X509_CRL_INFO **a; +unsigned char **pp; +long length; + { + int i,ver=0; + M_ASN1_D2I_vars(a,X509_CRL_INFO *,X509_CRL_INFO_new); + + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get_opt(ret->version,d2i_ASN1_INTEGER,V_ASN1_INTEGER); + if (ret->version != NULL) + ver=ret->version->data[0]; + + if ((ver == 0) && (ret->version != NULL)) + { + ASN1_INTEGER_free(ret->version); + ret->version=NULL; + } + M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME); + M_ASN1_D2I_get(ret->lastUpdate,d2i_ASN1_UTCTIME); + M_ASN1_D2I_get(ret->nextUpdate,d2i_ASN1_UTCTIME); + if (ret->revoked != NULL) + { + while (sk_num(ret->revoked)) + X509_REVOKED_free((X509_REVOKED *)sk_pop(ret->revoked)); + } + M_ASN1_D2I_get_seq_opt(ret->revoked,d2i_X509_REVOKED); + + if (ret->revoked != NULL) + { + for (i=0; irevoked); i++) + { + ((X509_REVOKED *)sk_value(ret->revoked,i))->sequence=i; + } + } + + if (ver >= 1) + { + if (ret->extensions != NULL) + { + while (sk_num(ret->extensions)) + X509_EXTENSION_free((X509_EXTENSION *) + sk_pop(ret->extensions)); + } + + M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION, + 0,V_ASN1_SEQUENCE); + } + + M_ASN1_D2I_Finish(a,X509_CRL_INFO_free,ASN1_F_D2I_X509_CRL_INFO); + } + +int i2d_X509_CRL(a,pp) +X509_CRL *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->crl,i2d_X509_CRL_INFO); + M_ASN1_I2D_len(a->sig_alg,i2d_X509_ALGOR); + M_ASN1_I2D_len(a->signature,i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->crl,i2d_X509_CRL_INFO); + M_ASN1_I2D_put(a->sig_alg,i2d_X509_ALGOR); + M_ASN1_I2D_put(a->signature,i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +X509_CRL *d2i_X509_CRL(a,pp,length) +X509_CRL **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_CRL *,X509_CRL_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->crl,d2i_X509_CRL_INFO); + M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING); + + M_ASN1_D2I_Finish(a,X509_CRL_free,ASN1_F_D2I_X509_CRL); + } + + +X509_REVOKED *X509_REVOKED_new() + { + X509_REVOKED *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_REVOKED); + M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new); + M_ASN1_New(ret->revocationDate,ASN1_UTCTIME_new); + ret->extensions=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_REVOKED_NEW); + } + +X509_CRL_INFO *X509_CRL_INFO_new() + { + X509_CRL_INFO *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_CRL_INFO); + ret->version=NULL; + M_ASN1_New(ret->sig_alg,X509_ALGOR_new); + M_ASN1_New(ret->issuer,X509_NAME_new); + M_ASN1_New(ret->lastUpdate,ASN1_UTCTIME_new); + M_ASN1_New(ret->nextUpdate,ASN1_UTCTIME_new); + M_ASN1_New(ret->revoked,sk_new_null); + M_ASN1_New(ret->extensions,sk_new_null); + ret->revoked->comp=(int (*)())X509_REVOKED_cmp; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_CRL_INFO_NEW); + } + +X509_CRL *X509_CRL_new() + { + X509_CRL *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_CRL); + ret->references=1; + M_ASN1_New(ret->crl,X509_CRL_INFO_new); + M_ASN1_New(ret->sig_alg,X509_ALGOR_new); + M_ASN1_New(ret->signature,ASN1_BIT_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_CRL_NEW); + } + +void X509_REVOKED_free(a) +X509_REVOKED *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->serialNumber); + ASN1_UTCTIME_free(a->revocationDate); + sk_pop_free(a->extensions,X509_EXTENSION_free); + Free((char *)a); + } + +void X509_CRL_INFO_free(a) +X509_CRL_INFO *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + X509_ALGOR_free(a->sig_alg); + X509_NAME_free(a->issuer); + ASN1_UTCTIME_free(a->lastUpdate); + ASN1_UTCTIME_free(a->nextUpdate); + sk_pop_free(a->revoked,X509_REVOKED_free); + sk_pop_free(a->extensions,X509_EXTENSION_free); + Free((char *)a); + } + +void X509_CRL_free(a) +X509_CRL *a; + { + int i; + + if (a == NULL) return; + + i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_CRL); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_CRL_free, bad reference count\n"); + abort(); + } +#endif + + X509_CRL_INFO_free(a->crl); + X509_ALGOR_free(a->sig_alg); + ASN1_BIT_STRING_free(a->signature); + Free((char *)a); + } + +static int X509_REVOKED_cmp(a,b) +X509_REVOKED **a,**b; + { + return(ASN1_STRING_cmp( + (ASN1_STRING *)(*a)->serialNumber, + (ASN1_STRING *)(*b)->serialNumber)); + } + +static int X509_REVOKED_seq_cmp(a,b) +X509_REVOKED **a,**b; + { + return((*a)->sequence-(*b)->sequence); + } diff --git a/crypto/asn1/x_exten.c b/crypto/asn1/x_exten.c new file mode 100644 index 0000000000..89daf3f226 --- /dev/null +++ b/crypto/asn1/x_exten.c @@ -0,0 +1,147 @@ +/* crypto/asn1/x_exten.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_EXTENSION,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_EXTENSION_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_EXTENSION(a,pp) +X509_EXTENSION *a; +unsigned char **pp; + { + int k=0; + int r=0,ret=0; + unsigned char **p=NULL; + + if (a == NULL) return(0); + + p=NULL; + for (;;) + { + if (k) + { + r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); + if (pp == NULL) return(r); + p=pp; + ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE, + V_ASN1_UNIVERSAL); + } + + ret+=i2d_ASN1_OBJECT(a->object,p); + if ((a->critical) || a->netscape_hack) + ret+=i2d_ASN1_BOOLEAN(a->critical,p); + ret+=i2d_ASN1_OCTET_STRING(a->value,p); + if (k++) return(r); + } + } + +X509_EXTENSION *d2i_X509_EXTENSION(a,pp,length) +X509_EXTENSION **a; +unsigned char **pp; +long length; + { + int i; + M_ASN1_D2I_vars(a,X509_EXTENSION *,X509_EXTENSION_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT); + + ret->netscape_hack=0; + if ((c.slen != 0) && + (M_ASN1_next == (V_ASN1_UNIVERSAL|V_ASN1_BOOLEAN))) + { + c.q=c.p; + if (d2i_ASN1_BOOLEAN(&i,&c.p,c.slen) < 0) goto err; + ret->critical=i; + c.slen-=(c.p-c.q); + if (ret->critical == 0) ret->netscape_hack=1; + } + M_ASN1_D2I_get(ret->value,d2i_ASN1_OCTET_STRING); + + M_ASN1_D2I_Finish(a,X509_EXTENSION_free,ASN1_F_D2I_X509_EXTENSION); + } + +X509_EXTENSION *X509_EXTENSION_new() + { + X509_EXTENSION *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_EXTENSION); + M_ASN1_New(ret->object,ASN1_OBJECT_new); + M_ASN1_New(ret->value,ASN1_OCTET_STRING_new); + ret->critical=0; + ret->netscape_hack=0; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_EXTENSION_NEW); + } + +void X509_EXTENSION_free(a) +X509_EXTENSION *a; + { + if (a == NULL) return; + ASN1_OBJECT_free(a->object); + ASN1_OCTET_STRING_free(a->value); + Free((char *)a); + } + diff --git a/crypto/asn1/x_info.c b/crypto/asn1/x_info.c new file mode 100644 index 0000000000..e46c67298f --- /dev/null +++ b/crypto/asn1/x_info.c @@ -0,0 +1,108 @@ +/* crypto/asn1/x_info.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "evp.h" +#include "asn1_mac.h" +#include "x509.h" + +X509_INFO *X509_INFO_new() + { + X509_INFO *ret=NULL; + + ret=(X509_INFO *)Malloc(sizeof(X509_INFO)); + if (ret == NULL) + { + ASN1err(ASN1_F_X509_INFO_NEW,ERR_R_MALLOC_FAILURE); + return(NULL); + } + + ret->enc_cipher.cipher=NULL; + ret->enc_len=0; + ret->enc_data=NULL; + + ret->references=1; + ret->x509=NULL; + ret->crl=NULL; + ret->x_pkey=NULL; + return(ret); + } + +void X509_INFO_free(x) +X509_INFO *x; + { + int i; + + if (x == NULL) return; + + i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_INFO); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_INFO_free, bad reference count\n"); + abort(); + } +#endif + + if (x->x509 != NULL) X509_free(x->x509); + if (x->crl != NULL) X509_CRL_free(x->crl); + if (x->x_pkey != NULL) X509_PKEY_free(x->x_pkey); + Free((char *)x); + } diff --git a/crypto/asn1/x_name.c b/crypto/asn1/x_name.c new file mode 100644 index 0000000000..c03f9169cb --- /dev/null +++ b/crypto/asn1/x_name.c @@ -0,0 +1,295 @@ +/* crypto/asn1/x_name.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "objects.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_NAME,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_NAME_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + * ASN1err(ASN1_F_D2I_X509_NAME_ENTRY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_NAME_ENTRY_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE); + */ + +#ifndef NOPROTO +static int i2d_X509_NAME_entries(X509_NAME *a); +#else +static int i2d_X509_NAME_entries(); +#endif + +int i2d_X509_NAME_ENTRY(a,pp) +X509_NAME_ENTRY *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->object,i2d_ASN1_OBJECT); + M_ASN1_I2D_len(a->value,i2d_ASN1_PRINTABLE); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->object,i2d_ASN1_OBJECT); + M_ASN1_I2D_put(a->value,i2d_ASN1_PRINTABLE); + + M_ASN1_I2D_finish(); + } + +X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(a,pp,length) +X509_NAME_ENTRY **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_NAME_ENTRY *,X509_NAME_ENTRY_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->object,d2i_ASN1_OBJECT); + M_ASN1_D2I_get(ret->value,d2i_ASN1_PRINTABLE); + ret->set=0; + M_ASN1_D2I_Finish(a,X509_NAME_ENTRY_free,ASN1_F_D2I_X509_NAME_ENTRY); + } + +int i2d_X509_NAME(a,pp) +X509_NAME *a; +unsigned char **pp; + { + int ret; + + if (a == NULL) return(0); + if (a->modified) + { + ret=i2d_X509_NAME_entries(a); + if (ret < 0) return(ret); + } + + ret=a->bytes->length; + if (pp != NULL) + { + memcpy(*pp,a->bytes->data,ret); + *pp+=ret; + } + return(ret); + } + +static int i2d_X509_NAME_entries(a) +X509_NAME *a; + { + X509_NAME_ENTRY *ne,*fe=NULL; + STACK *sk; + BUF_MEM *buf=NULL; + int set=0,r,ret=0; + int i; + unsigned char *p; + int size=0; + + sk=a->entries; + for (i=0; iset != set) + { + ret+=ASN1_object_size(1,size,V_ASN1_SET); + fe->size=size; + fe=ne; + size=0; + set=ne->set; + } + size+=i2d_X509_NAME_ENTRY(ne,NULL); + } + + ret+=ASN1_object_size(1,size,V_ASN1_SET); + if (fe != NULL) + fe->size=size; + + r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); + + buf=a->bytes; + if (!BUF_MEM_grow(buf,r)) goto err; + p=(unsigned char *)buf->data; + + ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); + + set= -1; + for (i=0; iset) + { + set=ne->set; + ASN1_put_object(&p,1,ne->size, + V_ASN1_SET,V_ASN1_UNIVERSAL); + } + i2d_X509_NAME_ENTRY(ne,&p); + } + a->modified=0; + return(r); +err: + return(-1); + } + +X509_NAME *d2i_X509_NAME(a,pp,length) +X509_NAME **a; +unsigned char **pp; +long length; + { + int set=0,i; + int idx=0; + unsigned char *orig; + M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new); + + orig= *pp; + if (sk_num(ret->entries) > 0) + { + while (sk_num(ret->entries) > 0) + X509_NAME_ENTRY_free((X509_NAME_ENTRY *) + sk_pop(ret->entries)); + } + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + for (;;) + { + if (M_ASN1_D2I_end_sequence()) break; + M_ASN1_D2I_get_set(ret->entries,d2i_X509_NAME_ENTRY); + for (; idx < sk_num(ret->entries); idx++) + { + ((X509_NAME_ENTRY *)sk_value(ret->entries,idx))->set= + set; + } + set++; + } + + i=(int)(c.p-orig); + if (!BUF_MEM_grow(ret->bytes,i)) goto err; + memcpy(ret->bytes->data,orig,i); + ret->bytes->length=i; + ret->modified=0; + + M_ASN1_D2I_Finish(a,X509_NAME_free,ASN1_F_D2I_X509_NAME); + } + +X509_NAME *X509_NAME_new() + { + X509_NAME *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_NAME); + if ((ret->entries=sk_new(NULL)) == NULL) goto err2; + M_ASN1_New(ret->bytes,BUF_MEM_new); + ret->modified=1; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_NAME_NEW); + } + +X509_NAME_ENTRY *X509_NAME_ENTRY_new() + { + X509_NAME_ENTRY *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_NAME_ENTRY); +/* M_ASN1_New(ret->object,ASN1_OBJECT_new);*/ + ret->object=NULL; + ret->set=0; + M_ASN1_New(ret->value,ASN1_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_NAME_ENTRY_NEW); + } + +void X509_NAME_free(a) +X509_NAME *a; + { + BUF_MEM_free(a->bytes); + sk_pop_free(a->entries,X509_NAME_ENTRY_free); + Free((char *)a); + } + +void X509_NAME_ENTRY_free(a) +X509_NAME_ENTRY *a; + { + if (a == NULL) return; + ASN1_OBJECT_free(a->object); + ASN1_BIT_STRING_free(a->value); + Free((char *)a); + } + +int X509_NAME_set(xn,name) +X509_NAME **xn; +X509_NAME *name; + { + X509_NAME *in; + + if (*xn == NULL) return(0); + + if (*xn != name) + { + in=X509_NAME_dup(name); + if (in != NULL) + { + X509_NAME_free(*xn); + *xn=in; + } + } + return(*xn != NULL); + } + diff --git a/crypto/asn1/x_pkey.c b/crypto/asn1/x_pkey.c new file mode 100644 index 0000000000..ced975b40e --- /dev/null +++ b/crypto/asn1/x_pkey.c @@ -0,0 +1,153 @@ +/* crypto/asn1/x_pkey.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "evp.h" +#include "objects.h" +#include "asn1_mac.h" + +/* ASN1err(ASN1_F_D2I_X509_PKEY,ASN1_R_UNSUPPORTED_CIPHER); */ +/* ASN1err(ASN1_F_X509_PKEY_NEW,ASN1_R_IV_TOO_LARGE); */ + +/* need to implement */ +int i2d_X509_PKEY(a,pp) +X509_PKEY *a; +unsigned char **pp; + { + return(0); + } + +X509_PKEY *d2i_X509_PKEY(a,pp,length) +X509_PKEY **a; +unsigned char **pp; +long length; + { + int i; + M_ASN1_D2I_vars(a,X509_PKEY *,X509_PKEY_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->enc_algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->enc_pkey,d2i_ASN1_OCTET_STRING); + + ret->cipher.cipher=EVP_get_cipherbyname( + OBJ_nid2ln(OBJ_obj2nid(ret->enc_algor->algorithm))); + if (ret->cipher.cipher == NULL) + { + c.error=ASN1_R_UNSUPPORTED_CIPHER; + goto err; + } + if (ret->enc_algor->parameter->type == V_ASN1_OCTET_STRING) + { + i=ret->enc_algor->parameter->value.octet_string->length; + if (i > EVP_MAX_IV_LENGTH) + { + c.error=ASN1_R_IV_TOO_LARGE; + goto err; + } + memcpy(ret->cipher.iv, + ret->enc_algor->parameter->value.octet_string->data,i); + } + else + memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH); + M_ASN1_D2I_Finish(a,X509_PKEY_free,ASN1_F_D2I_X509_PKEY); + } + +X509_PKEY *X509_PKEY_new() + { + X509_PKEY *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_PKEY); + ret->version=0; + M_ASN1_New(ret->enc_algor,X509_ALGOR_new); + M_ASN1_New(ret->enc_pkey,ASN1_OCTET_STRING_new); + ret->dec_pkey=NULL; + ret->key_length=0; + ret->key_data=NULL; + ret->key_free=0; + ret->cipher.cipher=NULL; + memset(ret->cipher.iv,0,EVP_MAX_IV_LENGTH); + ret->references=1; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_PKEY_NEW); + } + +void X509_PKEY_free(x) +X509_PKEY *x; + { + int i; + + if (x == NULL) return; + + i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_X509_PKEY); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_PKEY_free, bad reference count\n"); + abort(); + } +#endif + + if (x->enc_algor != NULL) X509_ALGOR_free(x->enc_algor); + if (x->enc_pkey != NULL) ASN1_OCTET_STRING_free(x->enc_pkey); + if (x->dec_pkey != NULL)EVP_PKEY_free(x->dec_pkey); + if ((x->key_data != NULL) && (x->key_free)) Free((char *)x->key_data); + Free((char *)(char *)x); + } diff --git a/crypto/asn1/x_pubkey.c b/crypto/asn1/x_pubkey.c new file mode 100644 index 0000000000..b1a2499d34 --- /dev/null +++ b/crypto/asn1/x_pubkey.c @@ -0,0 +1,252 @@ +/* crypto/asn1/x_pubkey.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_PUBKEY,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_PUBKEY_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_PUBKEY(a,pp) +X509_PUBKEY *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->public_key, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->public_key, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +X509_PUBKEY *d2i_X509_PUBKEY(a,pp,length) +X509_PUBKEY **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_PUBKEY *,X509_PUBKEY_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->public_key,d2i_ASN1_BIT_STRING); + if (ret->pkey != NULL) + { + EVP_PKEY_free(ret->pkey); + ret->pkey=NULL; + } + M_ASN1_D2I_Finish(a,X509_PUBKEY_free,ASN1_F_D2I_X509_PUBKEY); + } + +X509_PUBKEY *X509_PUBKEY_new() + { + X509_PUBKEY *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_PUBKEY); + M_ASN1_New(ret->algor,X509_ALGOR_new); + M_ASN1_New(ret->public_key,ASN1_BIT_STRING_new); + ret->pkey=NULL; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_PUBKEY_NEW); + } + +void X509_PUBKEY_free(a) +X509_PUBKEY *a; + { + if (a == NULL) return; + X509_ALGOR_free(a->algor); + ASN1_BIT_STRING_free(a->public_key); + if (a->pkey != NULL) EVP_PKEY_free(a->pkey); + Free((char *)a); + } + +int X509_PUBKEY_set(x,pkey) +X509_PUBKEY **x; +EVP_PKEY *pkey; + { + int ok=0; + X509_PUBKEY *pk; + X509_ALGOR *a; + ASN1_OBJECT *o; + unsigned char *s,*p; + int i; + + if (x == NULL) return(0); + + if ((pk=X509_PUBKEY_new()) == NULL) goto err; + a=pk->algor; + + /* set the algorithm id */ + if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err; + ASN1_OBJECT_free(a->algorithm); + a->algorithm=o; + + /* Set the parameter list */ + if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA)) + { + if ((a->parameter == NULL) || + (a->parameter->type != V_ASN1_NULL)) + { + ASN1_TYPE_free(a->parameter); + a->parameter=ASN1_TYPE_new(); + a->parameter->type=V_ASN1_NULL; + } + } + else +#ifndef NO_DSA + if (pkey->type == EVP_PKEY_DSA) + { + unsigned char *pp; + DSA *dsa; + + dsa=pkey->pkey.dsa; + dsa->write_params=0; + ASN1_TYPE_free(a->parameter); + i=i2d_DSAparams(dsa,NULL); + p=(unsigned char *)Malloc(i); + pp=p; + i2d_DSAparams(dsa,&pp); + a->parameter=ASN1_TYPE_new(); + a->parameter->type=V_ASN1_SEQUENCE; + a->parameter->value.sequence=ASN1_STRING_new(); + ASN1_STRING_set(a->parameter->value.sequence,p,i); + Free(p); + } + else +#endif + { + X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM); + goto err; + } + + i=i2d_PublicKey(pkey,NULL); + if ((s=(unsigned char *)Malloc(i+1)) == NULL) goto err; + p=s; + i2d_PublicKey(pkey,&p); + if (!ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err; + Free(s); + + CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); + pk->pkey=pkey; + + if (*x != NULL) + X509_PUBKEY_free(*x); + + *x=pk; + pk=NULL; + + ok=1; +err: + if (pk != NULL) X509_PUBKEY_free(pk); + return(ok); + } + +EVP_PKEY *X509_PUBKEY_get(key) +X509_PUBKEY *key; + { + EVP_PKEY *ret=NULL; + long j; + int type; + unsigned char *p; + X509_ALGOR *a; + + if (key->pkey != NULL) + { + return(key->pkey); + } + type=OBJ_obj2nid(key->algor->algorithm); + p=key->public_key->data; + j=key->public_key->length; + if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL) + { + X509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB); + goto err; + } + ret->save_parameters=0; + +#ifndef NO_DSA + a=key->algor; + if (ret->type == EVP_PKEY_DSA) + { + if (a->parameter->type == V_ASN1_SEQUENCE) + { + ret->pkey.dsa->write_params=0; + p=a->parameter->value.sequence->data; + j=a->parameter->value.sequence->length; + if (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j)) + goto err; + } + ret->save_parameters=1; + } +#endif + key->pkey=ret; + return(ret); +err: + if (ret != NULL) + EVP_PKEY_free(ret); + return(NULL); + } + diff --git a/crypto/asn1/x_req.c b/crypto/asn1/x_req.c new file mode 100644 index 0000000000..02b31add17 --- /dev/null +++ b/crypto/asn1/x_req.c @@ -0,0 +1,244 @@ +/* crypto/asn1/x_req.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" +#include "x509.h" + +/* + * ASN1err(ASN1_F_D2I_X509_REQ,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_X509_REQ_INFO,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_REQ_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_REQ_INFO_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_REQ_INFO(a,pp) +X509_REQ_INFO *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER); + M_ASN1_I2D_len(a->subject, i2d_X509_NAME); + M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY); + + /* this is a *nasty* hack reported to be required to + * allow some CA Software to accept the cert request. + * It is not following the PKCS standards ... + * PKCS#10 pg 5 + * attributes [0] IMPLICIT Attibutes + * NOTE: no OPTIONAL ... so it *must* be there + */ + if (a->req_kludge) + { + M_ASN1_I2D_len_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0); + } + else + { + M_ASN1_I2D_len_IMP_set(a->attributes, i2d_X509_ATTRIBUTE,0); + } + + M_ASN1_I2D_seq_total(); + M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER); + M_ASN1_I2D_put(a->subject, i2d_X509_NAME); + M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY); + + /* this is a *nasty* hack reported to be required by some CA's. + * It is not following the PKCS standards ... + * PKCS#10 pg 5 + * attributes [0] IMPLICIT Attibutes + * NOTE: no OPTIONAL ... so it *must* be there + */ + if (a->req_kludge) + { + M_ASN1_I2D_put_IMP_set_opt(a->attributes,i2d_X509_ATTRIBUTE,0); + } + else + { + M_ASN1_I2D_put_IMP_set(a->attributes,i2d_X509_ATTRIBUTE,0); + } + + M_ASN1_I2D_finish(); + } + +X509_REQ_INFO *d2i_X509_REQ_INFO(a,pp,length) +X509_REQ_INFO **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_REQ_INFO *,X509_REQ_INFO_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER); + M_ASN1_D2I_get(ret->subject,d2i_X509_NAME); + M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY); + + /* this is a *nasty* hack to allow for some CA's that + * have been reported as requiring it. + * It is not following the PKCS standards ... + * PKCS#10 pg 5 + * attributes [0] IMPLICIT Attibutes + * NOTE: no OPTIONAL ... so it *must* be there + */ + if (asn1_Finish(&c)) + ret->req_kludge=1; + else + { + M_ASN1_D2I_get_IMP_set(ret->attributes,d2i_X509_ATTRIBUTE,0); + } + + M_ASN1_D2I_Finish(a,X509_REQ_INFO_free,ASN1_F_D2I_X509_REQ_INFO); + } + +X509_REQ_INFO *X509_REQ_INFO_new() + { + X509_REQ_INFO *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_REQ_INFO); + M_ASN1_New(ret->version,ASN1_INTEGER_new); + M_ASN1_New(ret->subject,X509_NAME_new); + M_ASN1_New(ret->pubkey,X509_PUBKEY_new); + M_ASN1_New(ret->attributes,sk_new_null); + ret->req_kludge=0; + return(ret); + M_ASN1_New_Error(ASN1_F_X509_REQ_INFO_NEW); + } + +void X509_REQ_INFO_free(a) +X509_REQ_INFO *a; + { + if (a == NULL) return; + ASN1_INTEGER_free(a->version); + X509_NAME_free(a->subject); + X509_PUBKEY_free(a->pubkey); + sk_pop_free(a->attributes,X509_ATTRIBUTE_free); + Free((char *)a); + } + +int i2d_X509_REQ(a,pp) +X509_REQ *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + M_ASN1_I2D_len(a->req_info, i2d_X509_REQ_INFO); + M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->req_info, i2d_X509_REQ_INFO); + M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +X509_REQ *d2i_X509_REQ(a,pp,length) +X509_REQ **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_REQ *,X509_REQ_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->req_info,d2i_X509_REQ_INFO); + M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING); + M_ASN1_D2I_Finish(a,X509_REQ_free,ASN1_F_D2I_X509_REQ); + } + +X509_REQ *X509_REQ_new() + { + X509_REQ *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_REQ); + ret->references=1; + M_ASN1_New(ret->req_info,X509_REQ_INFO_new); + M_ASN1_New(ret->sig_alg,X509_ALGOR_new); + M_ASN1_New(ret->signature,ASN1_BIT_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_REQ_NEW); + } + +void X509_REQ_free(a) +X509_REQ *a; + { + int i; + + if (a == NULL) return; + + i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509_REQ); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_REQ_free, bad reference count\n"); + abort(); + } +#endif + + X509_REQ_INFO_free(a->req_info); + X509_ALGOR_free(a->sig_alg); + ASN1_BIT_STRING_free(a->signature); + Free((char *)a); + } + + diff --git a/crypto/asn1/x_sig.c b/crypto/asn1/x_sig.c new file mode 100644 index 0000000000..186c404e86 --- /dev/null +++ b/crypto/asn1/x_sig.c @@ -0,0 +1,119 @@ +/* crypto/asn1/x_sig.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509_SIG,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_SIG_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_X509_SIG(a,pp) +X509_SIG *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->digest, i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->digest, i2d_ASN1_OCTET_STRING); + + M_ASN1_I2D_finish(); + } + +X509_SIG *d2i_X509_SIG(a,pp,length) +X509_SIG **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_SIG *,X509_SIG_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->digest,d2i_ASN1_OCTET_STRING); + M_ASN1_D2I_Finish(a,X509_SIG_free,ASN1_F_D2I_X509_SIG); + } + +X509_SIG *X509_SIG_new() + { + X509_SIG *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_SIG); + M_ASN1_New(ret->algor,X509_ALGOR_new); + M_ASN1_New(ret->digest,ASN1_OCTET_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_SIG_NEW); + } + +void X509_SIG_free(a) +X509_SIG *a; + { + if (a == NULL) return; + X509_ALGOR_free(a->algor); + ASN1_OCTET_STRING_free(a->digest); + Free((char *)a); + } + + diff --git a/crypto/asn1/x_spki.c b/crypto/asn1/x_spki.c new file mode 100644 index 0000000000..bfeb0659a6 --- /dev/null +++ b/crypto/asn1/x_spki.c @@ -0,0 +1,181 @@ +/* crypto/asn1/x_spki.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + + /* This module was send to me my Pat Richards who + * wrote it. It is under my Copyright with his permision + */ + +#include +#include "cryptlib.h" +#include "x509.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_NETSCAPE_SPKAC,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_NETSCAPE_SPKAC_NEW,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_D2I_NETSCAPE_SPKI,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_NETSCAPE_SPKI_NEW,ASN1_R_LENGTH_MISMATCH); + */ + +int i2d_NETSCAPE_SPKAC(a,pp) +NETSCAPE_SPKAC *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->pubkey, i2d_X509_PUBKEY); + M_ASN1_I2D_len(a->challenge, i2d_ASN1_IA5STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->pubkey, i2d_X509_PUBKEY); + M_ASN1_I2D_put(a->challenge, i2d_ASN1_IA5STRING); + + M_ASN1_I2D_finish(); + } + +NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(a,pp,length) +NETSCAPE_SPKAC **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,NETSCAPE_SPKAC *,NETSCAPE_SPKAC_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->pubkey,d2i_X509_PUBKEY); + M_ASN1_D2I_get(ret->challenge,d2i_ASN1_IA5STRING); + M_ASN1_D2I_Finish(a,NETSCAPE_SPKAC_free,ASN1_F_D2I_NETSCAPE_SPKAC); + } + +NETSCAPE_SPKAC *NETSCAPE_SPKAC_new() + { + NETSCAPE_SPKAC *ret=NULL; + + M_ASN1_New_Malloc(ret,NETSCAPE_SPKAC); + M_ASN1_New(ret->pubkey,X509_PUBKEY_new); + M_ASN1_New(ret->challenge,ASN1_IA5STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKAC_NEW); + } + +void NETSCAPE_SPKAC_free(a) +NETSCAPE_SPKAC *a; + { + if (a == NULL) return; + X509_PUBKEY_free(a->pubkey); + ASN1_IA5STRING_free(a->challenge); + Free((char *)a); + } + +int i2d_NETSCAPE_SPKI(a,pp) +NETSCAPE_SPKI *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->spkac, i2d_NETSCAPE_SPKAC); + M_ASN1_I2D_len(a->sig_algor, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->spkac, i2d_NETSCAPE_SPKAC); + M_ASN1_I2D_put(a->sig_algor, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +NETSCAPE_SPKI *d2i_NETSCAPE_SPKI(a,pp,length) +NETSCAPE_SPKI **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,NETSCAPE_SPKI *,NETSCAPE_SPKI_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->spkac,d2i_NETSCAPE_SPKAC); + M_ASN1_D2I_get(ret->sig_algor,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING); + M_ASN1_D2I_Finish(a,NETSCAPE_SPKI_free,ASN1_F_D2I_NETSCAPE_SPKI); + } + +NETSCAPE_SPKI *NETSCAPE_SPKI_new() + { + NETSCAPE_SPKI *ret=NULL; + + M_ASN1_New_Malloc(ret,NETSCAPE_SPKI); + M_ASN1_New(ret->spkac,NETSCAPE_SPKAC_new); + M_ASN1_New(ret->sig_algor,X509_ALGOR_new); + M_ASN1_New(ret->signature,ASN1_BIT_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_NETSCAPE_SPKI_NEW); + } + +void NETSCAPE_SPKI_free(a) +NETSCAPE_SPKI *a; + { + if (a == NULL) return; + NETSCAPE_SPKAC_free(a->spkac); + X509_ALGOR_free(a->sig_algor); + ASN1_BIT_STRING_free(a->signature); + Free((char *)a); + } + diff --git a/crypto/asn1/x_val.c b/crypto/asn1/x_val.c new file mode 100644 index 0000000000..e2e4d00436 --- /dev/null +++ b/crypto/asn1/x_val.c @@ -0,0 +1,118 @@ +/* crypto/asn1/x_val.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "asn1_mac.h" + +/* ASN1err(ASN1_F_X509_VAL_NEW,ERR_R_MALLOC_FAILURE); + * ASN1err(ASN1_F_X509_VAL_FREE,ERR_R_MALLOC_FAILURE); + * ASN1err(ASN1_F_D2I_X509_VAL,ERR_R_MALLOC_FAILURE); + */ + +int i2d_X509_VAL(a,pp) +X509_VAL *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->notBefore,i2d_ASN1_UTCTIME); + M_ASN1_I2D_len(a->notAfter,i2d_ASN1_UTCTIME); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->notBefore,i2d_ASN1_UTCTIME); + M_ASN1_I2D_put(a->notAfter,i2d_ASN1_UTCTIME); + + M_ASN1_I2D_finish(); + } + +X509_VAL *d2i_X509_VAL(a,pp,length) +X509_VAL **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509_VAL *,X509_VAL_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->notBefore,d2i_ASN1_UTCTIME); + M_ASN1_D2I_get(ret->notAfter,d2i_ASN1_UTCTIME); + M_ASN1_D2I_Finish(a,X509_VAL_free,ASN1_F_D2I_X509_VAL); + } + +X509_VAL *X509_VAL_new() + { + X509_VAL *ret=NULL; + + M_ASN1_New_Malloc(ret,X509_VAL); + M_ASN1_New(ret->notBefore,ASN1_UTCTIME_new); + M_ASN1_New(ret->notAfter,ASN1_UTCTIME_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_VAL_NEW); + } + +void X509_VAL_free(a) +X509_VAL *a; + { + if (a == NULL) return; + ASN1_UTCTIME_free(a->notBefore); + ASN1_UTCTIME_free(a->notAfter); + Free((char *)a); + } + diff --git a/crypto/asn1/x_x509.c b/crypto/asn1/x_x509.c new file mode 100644 index 0000000000..d80a5cbc01 --- /dev/null +++ b/crypto/asn1/x_x509.c @@ -0,0 +1,155 @@ +/* crypto/asn1/x_x509.c */ +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +#include +#include "cryptlib.h" +#include "evp.h" +#include "asn1_mac.h" + +/* + * ASN1err(ASN1_F_D2I_X509,ASN1_R_LENGTH_MISMATCH); + * ASN1err(ASN1_F_X509_NEW,ASN1_R_BAD_GET_OBJECT); + */ + +static ASN1_METHOD meth={ + (int (*)()) i2d_X509, + (char *(*)())d2i_X509, + (char *(*)())X509_new, + (void (*)()) X509_free}; + +ASN1_METHOD *X509_asn1_meth() + { + return(&meth); + } + +int i2d_X509(a,pp) +X509 *a; +unsigned char **pp; + { + M_ASN1_I2D_vars(a); + + M_ASN1_I2D_len(a->cert_info, i2d_X509_CINF); + M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_seq_total(); + + M_ASN1_I2D_put(a->cert_info, i2d_X509_CINF); + M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR); + M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING); + + M_ASN1_I2D_finish(); + } + +X509 *d2i_X509(a,pp,length) +X509 **a; +unsigned char **pp; +long length; + { + M_ASN1_D2I_vars(a,X509 *,X509_new); + + M_ASN1_D2I_Init(); + M_ASN1_D2I_start_sequence(); + M_ASN1_D2I_get(ret->cert_info,d2i_X509_CINF); + M_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR); + M_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING); +if (ret->name != NULL) Free(ret->name); +ret->name=X509_NAME_oneline(ret->cert_info->subject,NULL,0); + + M_ASN1_D2I_Finish(a,X509_free,ASN1_F_D2I_X509); + } + +X509 *X509_new() + { + X509 *ret=NULL; + + M_ASN1_New_Malloc(ret,X509); + ret->references=1; + ret->valid=0; + ret->name=NULL; + M_ASN1_New(ret->cert_info,X509_CINF_new); + M_ASN1_New(ret->sig_alg,X509_ALGOR_new); + M_ASN1_New(ret->signature,ASN1_BIT_STRING_new); + return(ret); + M_ASN1_New_Error(ASN1_F_X509_NEW); + } + +void X509_free(a) +X509 *a; + { + int i; + + if (a == NULL) return; + + i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_X509); + if (i > 0) return; +#ifdef REF_CHECK + if (i < 0) + { + fprintf(stderr,"X509_free, bad reference count\n"); + abort(); + } +#endif + + X509_CINF_free(a->cert_info); + X509_ALGOR_free(a->sig_alg); + ASN1_BIT_STRING_free(a->signature); + if (a->name != NULL) Free(a->name); + Free((char *)a); + } + -- cgit v1.2.3