diff options
author | Ralf S. Engelschall <rse@openssl.org> | 1998-12-21 10:52:47 +0000 |
---|---|---|
committer | Ralf S. Engelschall <rse@openssl.org> | 1998-12-21 10:52:47 +0000 |
commit | d02b48c63a58ea4367a0e905979f140b7d090f86 (patch) | |
tree | 504f62ed3d84799f785b9cd9fab255a21b0e1b0e /crypto/x509 | |
download | openssl-d02b48c63a58ea4367a0e905979f140b7d090f86.tar.gz |
Import of old SSLeay release: SSLeay 0.8.1b
Diffstat (limited to 'crypto/x509')
29 files changed, 6925 insertions, 0 deletions
diff --git a/crypto/x509/Makefile.ssl b/crypto/x509/Makefile.ssl new file mode 100644 index 0000000000..e54a74d1e8 --- /dev/null +++ b/crypto/x509/Makefile.ssl @@ -0,0 +1,96 @@ +# +# SSLeay/crypto/x509/Makefile +# + +DIR= x509 +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=x509 +ERRC=x509_err +GENERAL=Makefile README +TEST= +APPS= + +LIB=$(TOP)/libcrypto.a +LIBSRC= x509_def.c x509_d2.c x509_r2x.c x509_cmp.c \ + x509_obj.c x509_req.c x509_vfy.c \ + x509_set.c x509rset.c $(ERRC).c \ + x509name.c x509_v3.c x509_ext.c x509pack.c \ + x509type.c x509_lu.c x_all.c x509_txt.c \ + by_file.c by_dir.c \ + v3_net.c v3_x509.c +LIBOBJ= x509_def.o x509_d2.o x509_r2x.o x509_cmp.o \ + x509_obj.o x509_req.o x509_vfy.o \ + x509_set.o x509rset.o $(ERRC).o \ + x509name.o x509_v3.o x509_ext.o x509pack.o \ + x509type.o x509_lu.o x_all.o x509_txt.o \ + by_file.o by_dir.o \ + v3_net.o v3_x509.o + +SRC= $(LIBSRC) + +EXHEADER= x509.h x509_vfy.h +HEADER= $(EXHEADER) + +ALL= $(GENERAL) $(SRC) $(HEADER) + +top: + (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) + +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/x509/attrib b/crypto/x509/attrib new file mode 100644 index 0000000000..37f6cd755f --- /dev/null +++ b/crypto/x509/attrib @@ -0,0 +1,38 @@ + +PKCS7 + STACK of X509_ATTRIBUTES + ASN1_OBJECT + STACK of ASN1_TYPE + +So it is + +p7.xa[].obj +p7.xa[].data[] + +get_obj_by_nid(STACK , nid) +get_num_by_nid(STACK , nid) +get_data_by_nid(STACK , nid, index) + +X509_ATTRIBUTE *X509_ATTRIBUTE_new(void ); +void X509_ATTRIBUTE_free(X509_ATTRIBUTE *a); + +X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **ex, + int nid, STACK *value); + +X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **ex, + int nid, STACK *value); + +int X509_ATTRIBUTE_set_object(X509_ATTRIBUTE *ex,ASN1_OBJECT *obj); +int X509_ATTRIBUTE_add_data(X509_ATTRIBUTE *ex, int index, + ASN1_TYPE *value); + +ASN1_OBJECT * X509_ATTRIBUTE_get_object(X509_ATTRIBUTE *ex); +int X509_ATTRIBUTE_get_num(X509_ATTRIBUTE *ne); +ASN1_TYPE * X509_ATTRIBUTE_get_data(X509_ATTRIBUTE *ne,int index); + +ASN1_TYPE * X509_ATTRIBUTE_get_data_by_NID(X509_ATTRIBUTE *ne, + ASN1_OBJECT *obj); + +X509_ATTRUBUTE *PKCS7_get_s_att_by_NID(PKCS7 *p7,int nid); +X509_ATTRUBUTE *PKCS7_get_u_att_by_NID(PKCS7 *p7,int nid); + diff --git a/crypto/x509/by_dir.c b/crypto/x509/by_dir.c new file mode 100644 index 0000000000..6676a2e404 --- /dev/null +++ b/crypto/x509/by_dir.c @@ -0,0 +1,320 @@ +/* crypto/x509/by_dir.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 <stdio.h> +#include <time.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include "cryptlib.h" +#include "lhash.h" +#include "x509.h" +#include "pem.h" + +typedef struct lookup_dir_st + { + BUF_MEM *buffer; + int num_dirs; + char **dirs; + int *dirs_type; + int num_dirs_alloced; + } BY_DIR; + +#ifndef NOPROTO +static int dir_ctrl(X509_LOOKUP *ctx,int cmd,char *argp,long argl,char **ret); +static int new_dir(X509_LOOKUP *lu); +static void free_dir(X509_LOOKUP *lu); +static int add_cert_dir(BY_DIR *ctx,char *dir,int type); +static int get_cert_by_subject(X509_LOOKUP *xl,int type,X509_NAME *name, + X509_OBJECT *ret); +#else +static int dir_ctrl(); +static int new_dir(); +static void free_dir(); +static int add_cert_dir(); +static int get_cert_by_subject(); +#endif + +X509_LOOKUP_METHOD x509_dir_lookup= + { + "Load certs from files in a directory", + new_dir, /* new */ + free_dir, /* free */ + NULL, /* init */ + NULL, /* shutdown */ + dir_ctrl, /* ctrl */ + get_cert_by_subject, /* get_by_subject */ + NULL, /* get_by_issuer_serial */ + NULL, /* get_by_fingerprint */ + NULL, /* get_by_alias */ + }; + +X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir() + { + return(&x509_dir_lookup); + } + +static int dir_ctrl(ctx,cmd,argp,argl,retp) +X509_LOOKUP *ctx; +int cmd; +long argl; +char *argp; +char **retp; + { + int ret=0; + BY_DIR *ld; + char *dir; + + ld=(BY_DIR *)ctx->method_data; + + switch (cmd) + { + case X509_L_ADD_DIR: + if (argl == X509_FILETYPE_DEFAULT) + { + ret=add_cert_dir(ld,X509_get_default_cert_dir(), + X509_FILETYPE_PEM); + if (!ret) + { + X509err(X509_F_DIR_CTRL,X509_R_LOADING_CERT_DIR); + } + else + { + dir=(char *)Getenv(X509_get_default_cert_dir_env()); + ret=add_cert_dir(ld,dir,X509_FILETYPE_PEM); + } + } + else + ret=add_cert_dir(ld,argp,(int)argl); + break; + } + return(ret); + } + +static int new_dir(lu) +X509_LOOKUP *lu; + { + BY_DIR *a; + + if ((a=(BY_DIR *)Malloc(sizeof(BY_DIR))) == NULL) + return(0); + if ((a->buffer=BUF_MEM_new()) == NULL) + { + Free(a); + return(0); + } + a->num_dirs=0; + a->dirs=NULL; + a->dirs_type=NULL; + a->num_dirs_alloced=0; + lu->method_data=(char *)a; + return(1); + } + +static void free_dir(lu) +X509_LOOKUP *lu; + { + BY_DIR *a; + int i; + + a=(BY_DIR *)lu->method_data; + for (i=0; i<a->num_dirs; i++) + if (a->dirs[i] != NULL) Free(a->dirs[i]); + if (a->dirs != NULL) Free(a->dirs); + if (a->dirs_type != NULL) Free(a->dirs_type); + if (a->buffer != NULL) BUF_MEM_free(a->buffer); + Free(a); + } + +static int add_cert_dir(ctx,dir, type) +BY_DIR *ctx; +char *dir; +int type; + { + int j,len; + int *ip; + char *s,*ss,*p; + char **pp; + + if (dir == NULL) return(0); + + s=dir; + p=s; + for (;;) + { + if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) + { + ss=s; + s=p+1; + len=(int)(p-ss); + if (len == 0) continue; + for (j=0; j<ctx->num_dirs; j++) + if (strncmp(ctx->dirs[j],ss,(unsigned int)len) == 0) + continue; + if (ctx->num_dirs_alloced < (ctx->num_dirs+1)) + { + ctx->num_dirs_alloced+=10; + pp=(char **)Malloc(ctx->num_dirs_alloced* + sizeof(char *)); + ip=(int *)Malloc(ctx->num_dirs_alloced* + sizeof(int)); + if ((pp == NULL) || (ip == NULL)) + { + X509err(X509_F_ADD_CERT_DIR,ERR_R_MALLOC_FAILURE); + return(0); + } + memcpy(pp,ctx->dirs,(ctx->num_dirs_alloced-10)* + sizeof(char *)); + memcpy(ip,ctx->dirs_type,(ctx->num_dirs_alloced-10)* + sizeof(int)); + if (ctx->dirs != NULL) + Free((char *)ctx->dirs); + if (ctx->dirs_type != NULL) + Free((char *)ctx->dirs_type); + ctx->dirs=pp; + ctx->dirs_type=ip; + } + ctx->dirs_type[ctx->num_dirs]=type; + ctx->dirs[ctx->num_dirs]=(char *)Malloc((unsigned int)len+1); + if (ctx->dirs[ctx->num_dirs] == NULL) return(0); + strncpy(ctx->dirs[ctx->num_dirs],ss,(unsigned int)len); + ctx->dirs[ctx->num_dirs][len]='\0'; + ctx->num_dirs++; + } + if (*p == '\0') break; + p++; + } + return(1); + } + +static int get_cert_by_subject(xl,type,name,ret) +X509_LOOKUP *xl; +int type; +X509_NAME *name; +X509_OBJECT *ret; + { + BY_DIR *ctx; + X509 st_x509; + X509_CINF st_x509_cinf; + int ok=0; + int i,j,k; + unsigned long h; + BUF_MEM *b=NULL; + struct stat st; + X509_OBJECT stmp,*tmp; + + if (name == NULL) return(0); + + st_x509.cert_info= &st_x509_cinf; + st_x509_cinf.subject=name; + stmp.data.x509= &st_x509; + stmp.type=type; + + if ((b=BUF_MEM_new()) == NULL) + { + X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_BUF_LIB); + goto finish; + } + + ctx=(BY_DIR *)xl->method_data; + + h=X509_NAME_hash(name); + for (i=0; i<ctx->num_dirs; i++) + { + j=strlen(ctx->dirs[i])+1+8+6+1; + if (!BUF_MEM_grow(b,j)) + { + X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_MALLOC_FAILURE); + goto finish; + } + k=0; + for (;;) + { + sprintf(b->data,"%s/%08lx.%d",ctx->dirs[i],h,k); + k++; + if (stat(b->data,&st) < 0) + break; + /* found one. */ + if ((X509_load_cert_file(xl,b->data, + ctx->dirs_type[i])) == 0) + break; + } + + /* we have added it to the cache so now pull + * it out again */ + CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE); + tmp=(X509_OBJECT *)lh_retrieve(xl->store_ctx->certs, + (char *)&stmp); + CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE); + + if (tmp != NULL) + { + ok=1; + ret->type=tmp->type; + ret->data.x509=tmp->data.x509; + /* CRYPTO_add(&tmp->data.x509->references,1, + CRYPTO_LOCK_X509);*/ + goto finish; + } + } +finish: + if (b != NULL) BUF_MEM_free(b); + return(ok); + } + diff --git a/crypto/x509/by_file.c b/crypto/x509/by_file.c new file mode 100644 index 0000000000..2dac28f542 --- /dev/null +++ b/crypto/x509/by_file.c @@ -0,0 +1,205 @@ +/* crypto/x509/by_file.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 <stdio.h> +#include <time.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include "cryptlib.h" +#include "lhash.h" +#include "buffer.h" +#include "x509.h" +#include "pem.h" + +#ifndef NOPROTO +static int by_file_ctrl(X509_LOOKUP *ctx,int cmd,char *argc, + long argl,char **ret); +#else +static int by_file_ctrl(); +#endif + +X509_LOOKUP_METHOD x509_file_lookup= + { + "Load file into cache", + NULL, /* new */ + NULL, /* free */ + NULL, /* init */ + NULL, /* shutdown */ + by_file_ctrl, /* ctrl */ + NULL, /* get_by_subject */ + NULL, /* get_by_issuer_serial */ + NULL, /* get_by_fingerprint */ + NULL, /* get_by_alias */ + }; + +X509_LOOKUP_METHOD *X509_LOOKUP_file() + { + return(&x509_file_lookup); + } + +static int by_file_ctrl(ctx,cmd,argp,argl,ret) +X509_LOOKUP *ctx; +int cmd; +char *argp; +long argl; +char **ret; + { + int ok=0; + char *file; + + switch (cmd) + { + case X509_L_FILE_LOAD: + if (argl == X509_FILETYPE_DEFAULT) + { + ok=X509_load_cert_file(ctx,X509_get_default_cert_file(), + X509_FILETYPE_PEM); + if (!ok) + { + X509err(X509_F_BY_FILE_CTRL,X509_R_LOADING_DEFAULTS); + } + else + { + file=(char *)Getenv(X509_get_default_cert_file_env()); + ok=X509_load_cert_file(ctx,file, + X509_FILETYPE_PEM); + } + } + else + ok=X509_load_cert_file(ctx,argp,(int)argl); + break; + } + return(ok); + } + +int X509_load_cert_file(ctx,file,type) +X509_LOOKUP *ctx; +char *file; +int type; + { + int ret=0; + BIO *in=NULL; + int i,count=0; + X509 *x=NULL; + + if (file == NULL) return(1); +#ifndef WIN16 + in=BIO_new(BIO_s_file()); +#else + in=BIO_new(BIO_s_file_internal_w16()); +#endif + + if ((in == NULL) || (BIO_read_filename(in,file) <= 0)) + { + X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_SYS_LIB); + goto err; + } + + if (type == X509_FILETYPE_PEM) + { + for (;;) + { + x=PEM_read_bio_X509(in,NULL,NULL); + if (x == NULL) + { + if ((ERR_GET_REASON(ERR_peek_error()) == + PEM_R_NO_START_LINE) && (count > 0)) + { + ERR_clear_error(); + break; + } + else + { + X509err(X509_F_X509_LOAD_CERT_FILE, + ERR_R_PEM_LIB); + goto err; + } + } + i=X509_STORE_add_cert(ctx->store_ctx,x); + if (!i) goto err; + count++; + X509_free(x); + x=NULL; + } + ret=count; + } + else if (type == X509_FILETYPE_ASN1) + { + x=d2i_X509_bio(in,NULL); + if (x == NULL) + { + X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_ASN1_LIB); + goto err; + } + i=X509_STORE_add_cert(ctx->store_ctx,x); + if (!i) goto err; + ret=i; + } + else + { + X509err(X509_F_X509_LOAD_CERT_FILE,X509_R_BAD_X509_FILETYPE); + goto err; + } +err: + if (x != NULL) X509_free(x); + if (in != NULL) BIO_free(in); + return(ret); + } + diff --git a/crypto/x509/f b/crypto/x509/f new file mode 100644 index 0000000000..6ec986db87 --- /dev/null +++ b/crypto/x509/f @@ -0,0 +1,465 @@ +*** x509name.c Wed Jul 2 09:35:35 1997 +--- /home/eay/play/x Sat Jul 5 01:39:56 1997 +*************** +*** 1,202 **** +! /* crypto/x509/x509name.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 <stdio.h> +! #include "stack.h" +! #include "cryptlib.h" +! #include "asn1.h" +! #include "objects.h" +! #include "evp.h" +! #include "x509.h" +! +! int X509_NAME_get_text_by_NID(name,nid,buf,len) +! X509_NAME *name; +! int nid; +! char *buf; +! int len; + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); +! if (obj == NULL) return(-1); +! return(X509_NAME_get_text_by_OBJ(name,obj,buf,len)); + } + +- int X509_NAME_get_text_by_OBJ(name,obj,buf,len) +- X509_NAME *name; +- ASN1_OBJECT *obj; +- char *buf; +- int len; +- { +- int i; +- ASN1_STRING *data; + +! i=X509_NAME_get_index_by_OBJ(name,obj,0); +! if (i < 0) return(-1); +! data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i)); +! i=(data->length > (len-1))?(len-1):data->length; +! if (buf == NULL) return(data->length); +! memcpy(buf,data->data,i); +! buf[i]='\0'; +! return(i); +! } + +! int X509_NAME_entry_count(name) +! X509_NAME *name; + { +! if (name == NULL) return(0); +! return(sk_num(name->entries)); + } + +! int X509_NAME_get_index_by_NID(name,nid,oldpos) +! X509_NAME *name; +! int nid; +! int oldpos; +! { +! ASN1_OBJECT *obj; + +! obj=OBJ_nid2obj(nid); +! if (obj == NULL) return(-2); +! return(X509_NAME_get_index_by_OBJ(name,obj,oldpos)); + } + +- int X509_NAME_get_index_by_OBJ(name,obj,oldpos) +- X509_NAME *name; +- ASN1_OBJECT *obj; +- int oldpos; +- { +- int n; +- X509_NAME_ENTRY *ne; +- STACK *sk; + +! if (name == NULL) return(-1); +! if (oldpos < 0) +! oldpos= -1; +! sk=name->entries; +! n=sk_num(sk); +! for (oldpos++; oldpos < n; oldpos++) + { +! ne=(X509_NAME_ENTRY *)sk_value(sk,oldpos); +! if (OBJ_cmp(ne->object,obj) == 0) +! return(oldpos); + } +! return(-1); + } + +- X509_NAME_ENTRY *X509_NAME_get_entry(name,loc) +- X509_NAME *name; +- int loc; +- { +- if ( (name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) +- return(NULL); +- else +- return((X509_NAME_ENTRY *)sk_value(name->entries,loc)); +- } + +! X509_NAME_ENTRY *X509_NAME_delete_entry(name,loc) +! X509_NAME *name; +! int loc; + { +! X509_NAME_ENTRY *ret; +! int i,j,n,set_prev,set_next; +! STACK *sk; +! +! if ((name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) +! return(NULL); +! sk=name->entries; +! ret=(X509_NAME_ENTRY *)sk_delete(sk,loc); +! n=sk_num(sk); +! name->modified=1; +! if (loc == n) return(ret); +! +! /* else we need to fixup the set field */ +! if (loc != 0) +! set_prev=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set; +! else +! set_prev=ret->set-1; +! set_next=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; + +! /* set_prev is the previous set +! * set is the current set +! * set_next is the following +! * prev 1 1 1 1 1 1 1 1 +! * set 1 1 2 2 +! * next 1 1 2 2 2 2 3 2 +! * so basically only if prev and next differ by 2, then +! * re-number down by 1 */ +! if (set_prev+1 < set_next) +! { +! j=set_next-set_prev-1; +! for (i=loc; i<n; i++) +! ((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set-=j; +! } +! return(ret); + } + + /* if set is -1, append to previous set, 0 'a new one', and 1, + * prepend to the guy we are about to stomp on. */ +! int X509_NAME_add_entry(name,ne,loc,set) +! X509_NAME *name; +! X509_NAME_ENTRY *ne; +! int loc; +! int set; + { +! X509_NAME_ENTRY *new_name=NULL; + int n,i,inc; + STACK *sk; + +--- 1,77 ---- +! X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, +! int type,unsigned char *bytes, int len) + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); +! if (obj == NULL) +! { +! X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID); +! return(NULL); +! } +! return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len)); + } + + +! X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, +! ASN1_OBJECT *obj, int type,unsigned char *bytes, +! int len) +! { +! X509_NAME_ENTRY *ret; + +! if ((ne == NULL) || (*ne == NULL)) + { +! if ((ret=X509_NAME_ENTRY_new()) == NULL) +! return(NULL); + } ++ else ++ ret= *ne; + +! if (!X509_NAME_ENTRY_set_object(ret,obj)) +! goto err; +! if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len)) +! goto err; + +! if ((ne != NULL) && (*ne == NULL)) *ne=ret; +! return(ret); +! err: +! if ((ne == NULL) || (ret != *ne)) +! X509_NAME_ENTRY_free(ret); +! return(NULL); + } + + +! int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, ASN1_OBJECT *obj) +! { +! if ((ne == NULL) || (obj == NULL)) + { +! X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER); +! return(0); + } +! ASN1_OBJECT_free(ne->object); +! ne->object=OBJ_dup(obj); +! return((ne->object == NULL)?0:1); + } + + +! int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne,int type,unsigned char *bytes,int len) + { +! int i; + +! if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0); +! if (len < 0) len=strlen((char *)bytes); +! i=ASN1_STRING_set(ne->value,bytes,len); +! if (!i) return(0); +! ne->value->type=ASN1_PRINTABLE_type(bytes,len); +! return(1); + } + + /* if set is -1, append to previous set, 0 'a new one', and 1, + * prepend to the guy we are about to stomp on. */ +! int X509_NAME_add_entry(X509_NAME *name,X509_NAME_ENTRY *ne,int loc,int set) + { +! /* ERIC: renamed new to nenew for C++ users --tjh */ +! X509_NAME_ENTRY *nenew; + int n,i,inc; + STACK *sk; + +*************** +*** 206,213 **** + if (loc > n) loc=n; + else if (loc < 0) loc=n; + +- name->modified=1; +- + if (set == -1) + { + if (loc == 0) +--- 81,86 ---- +*************** +*** 223,245 **** + } + else /* if (set >= 0) */ + { +- inc=(set == 0)?1:0; + if (loc >= n) + { + if (loc != 0) + set=((X509_NAME_ENTRY *) +! sk_value(sk,n-1))->set+1; + else + set=0; + } + else + set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; + } + +! if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL) + goto err; +! new_name->set=set; +! if (!sk_insert(sk,(char *)new_name,loc)) + { + X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE); + goto err; +--- 96,122 ---- + } + else /* if (set >= 0) */ + { + if (loc >= n) + { + if (loc != 0) + set=((X509_NAME_ENTRY *) +! sk_value(sk,loc-1))->set+1; + else + set=0; + } + else + set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; ++ inc=(set == 0)?1:0; + } + +! if ((nenew=X509_NAME_ENTRY_dup(ne)) == NULL) + goto err; +! /* eric forgot to put this in when he cut the nice +! * interface so that I don't have to do the icky things +! * that req.c does --tjh :-) +! */ +! nenew->set=set; +! if (!sk_insert(sk,(char *)nenew,loc)) + { + X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE); + goto err; +*************** +*** 252,357 **** + } + return(1); + err: +! if (new_name != NULL) + X509_NAME_ENTRY_free(ne); + return(0); +- } +- +- X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(ne,nid,type,bytes,len) +- X509_NAME_ENTRY **ne; +- int nid; +- int type; +- unsigned char *bytes; +- int len; +- { +- ASN1_OBJECT *obj; +- +- obj=OBJ_nid2obj(nid); +- if (obj == NULL) +- { +- X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID); +- return(NULL); +- } +- return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len)); +- } +- +- X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len) +- X509_NAME_ENTRY **ne; +- ASN1_OBJECT *obj; +- int type; +- unsigned char *bytes; +- int len; +- { +- X509_NAME_ENTRY *ret; +- +- if ((ne == NULL) || (*ne == NULL)) +- { +- if ((ret=X509_NAME_ENTRY_new()) == NULL) +- return(NULL); +- } +- else +- ret= *ne; +- +- if (!X509_NAME_ENTRY_set_object(ret,obj)) +- goto err; +- if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len)) +- goto err; +- +- if ((ne != NULL) && (*ne == NULL)) *ne=ret; +- return(ret); +- err: +- if ((ne == NULL) || (ret != *ne)) +- X509_NAME_ENTRY_free(ret); +- return(NULL); +- } +- +- int X509_NAME_ENTRY_set_object(ne,obj) +- X509_NAME_ENTRY *ne; +- ASN1_OBJECT *obj; +- { +- if ((ne == NULL) || (obj == NULL)) +- { +- X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER); +- return(0); +- } +- ASN1_OBJECT_free(ne->object); +- ne->object=OBJ_dup(obj); +- return((ne->object == NULL)?0:1); +- } +- +- int X509_NAME_ENTRY_set_data(ne,type,bytes,len) +- X509_NAME_ENTRY *ne; +- int type; +- unsigned char *bytes; +- int len; +- { +- int i; +- +- if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0); +- if (len < 0) len=strlen((char *)bytes); +- i=ASN1_STRING_set(ne->value,bytes,len); +- if (!i) return(0); +- if (type != V_ASN1_UNDEF) +- { +- if (type == V_ASN1_APP_CHOOSE) +- ne->value->type=ASN1_PRINTABLE_type(bytes,len); +- else +- ne->value->type=type; +- } +- return(1); +- } +- +- ASN1_OBJECT *X509_NAME_ENTRY_get_object(ne) +- X509_NAME_ENTRY *ne; +- { +- if (ne == NULL) return(NULL); +- return(ne->object); +- } +- +- ASN1_STRING *X509_NAME_ENTRY_get_data(ne) +- X509_NAME_ENTRY *ne; +- { +- if (ne == NULL) return(NULL); +- return(ne->value); + } + +--- 129,136 ---- + } + return(1); + err: +! if (nenew != NULL) + X509_NAME_ENTRY_free(ne); + return(0); + } + diff --git a/crypto/x509/v3_net.c b/crypto/x509/v3_net.c new file mode 100644 index 0000000000..5e79a57f03 --- /dev/null +++ b/crypto/x509/v3_net.c @@ -0,0 +1,87 @@ +/* crypto/x509/v3_net.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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "x509.h" + +#define NETSCAPE_X509_EXT_NUM 8 + +static X509_EXTENSION_METHOD netscape_x509_ext[NETSCAPE_X509_EXT_NUM]={ +{NID_netscape_ca_policy_url,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_ssl_server_name,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_revocation_url,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_base_url,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_cert_type,V_ASN1_BIT_STRING,X509_EXT_PACK_STRING}, +{NID_netscape_ca_revocation_url,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_renewal_url,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_netscape_comment,V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, + }; + +int X509v3_add_netscape_extensions() + { + int i; + + for (i=0; i<NETSCAPE_X509_EXT_NUM; i++) + if (!X509v3_add_extension(&(netscape_x509_ext[i]))) + return(0); + return(1); + } diff --git a/crypto/x509/v3_x509.c b/crypto/x509/v3_x509.c new file mode 100644 index 0000000000..57a7b17df1 --- /dev/null +++ b/crypto/x509/v3_x509.c @@ -0,0 +1,88 @@ +/* crypto/x509/v3_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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "x509.h" + +#define STD_X509_EXT_NUM 9 + +static X509_EXTENSION_METHOD std_x509_ext[STD_X509_EXT_NUM]={ +{NID_subject_key_identifier, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_key_usage, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_private_key_usage_period, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_subject_alt_name, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_issuer_alt_name, V_ASN1_BIT_STRING,X509_EXT_PACK_STRING}, +{NID_basic_constraints, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_crl_number, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_certificate_policies, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, +{NID_authority_key_identifier, V_ASN1_IA5STRING,X509_EXT_PACK_STRING}, + }; + +int X509v3_add_standard_extensions() + { + int i; + + for (i=0; i<STD_X509_EXT_NUM; i++) + if (!X509v3_add_extension(&(std_x509_ext[i]))) + return(0); + return(1); + } diff --git a/crypto/x509/x509.doc b/crypto/x509/x509.doc new file mode 100644 index 0000000000..73cfc9f034 --- /dev/null +++ b/crypto/x509/x509.doc @@ -0,0 +1,27 @@ +X509_verify() +X509_sign() + +X509_get_version() +X509_get_serialNumber() +X509_get_issuer() +X509_get_subject() +X509_get_notBefore() +X509_get_notAfter() +X509_get_pubkey() + +X509_set_version() +X509_set_serialNumber() +X509_set_issuer() +X509_set_subject() +X509_set_notBefore() +X509_set_notAfter() +X509_set_pubkey() + +X509_get_extensions() +X509_set_extensions() + +X509_EXTENSIONS_clear() +X509_EXTENSIONS_retrieve() +X509_EXTENSIONS_add() +X509_EXTENSIONS_delete() + diff --git a/crypto/x509/x509.err b/crypto/x509/x509.err new file mode 100644 index 0000000000..7f8b33ed89 --- /dev/null +++ b/crypto/x509/x509.err @@ -0,0 +1,43 @@ +/* Error codes for the X509 functions. */ + +/* Function codes. */ +#define X509_F_ADD_CERT_DIR 100 +#define X509_F_BY_FILE_CTRL 101 +#define X509_F_DIR_CTRL 102 +#define X509_F_GET_CERT_BY_SUBJECT 103 +#define X509_F_X509V3_ADD_EXT 104 +#define X509_F_X509V3_ADD_EXTENSION 105 +#define X509_F_X509V3_PACK_STRING 106 +#define X509_F_X509V3_UNPACK_STRING 107 +#define X509_F_X509_EXTENSION_CREATE_BY_NID 108 +#define X509_F_X509_EXTENSION_CREATE_BY_OBJ 109 +#define X509_F_X509_GET_PUBKEY_PARAMETERS 110 +#define X509_F_X509_LOAD_CERT_FILE 111 +#define X509_F_X509_NAME_ADD_ENTRY 112 +#define X509_F_X509_NAME_ENTRY_CREATE_BY_NID 113 +#define X509_F_X509_NAME_ENTRY_SET_OBJECT 114 +#define X509_F_X509_NAME_ONELINE 115 +#define X509_F_X509_NAME_PRINT 116 +#define X509_F_X509_PRINT_FP 117 +#define X509_F_X509_PUBKEY_GET 118 +#define X509_F_X509_PUBKEY_SET 119 +#define X509_F_X509_REQ_PRINT 120 +#define X509_F_X509_REQ_PRINT_FP 121 +#define X509_F_X509_REQ_TO_X509 122 +#define X509_F_X509_STORE_ADD_CERT 123 +#define X509_F_X509_TO_X509_REQ 124 +#define X509_F_X509_VERIFY_CERT 125 + +/* Reason codes. */ +#define X509_R_BAD_X509_FILETYPE 100 +#define X509_R_CERT_ALREADY_IN_HASH_TABLE 101 +#define X509_R_ERR_ASN1_LIB 102 +#define X509_R_LOADING_CERT_DIR 103 +#define X509_R_LOADING_DEFAULTS 104 +#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 105 +#define X509_R_SHOULD_RETRY 106 +#define X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN 107 +#define X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY 108 +#define X509_R_UNKNOWN_NID 109 +#define X509_R_UNKNOWN_STRING_TYPE 110 +#define X509_R_UNSUPPORTED_ALGORITHM 111 diff --git a/crypto/x509/x509.h b/crypto/x509/x509.h new file mode 100644 index 0000000000..a1f7556818 --- /dev/null +++ b/crypto/x509/x509.h @@ -0,0 +1,1113 @@ +/* crypto/x509/x509.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_X509_H +#define HEADER_X509_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stack.h" +#include "asn1.h" + +#ifndef NO_RSA +#include "rsa.h" +#else +#define RSA long +#endif + +#ifndef NO_DSA +#include "dsa.h" +#else +#define DSA long +#endif + +#ifndef NO_DH +#include "dh.h" +#else +#define DH long +#endif + +#include "evp.h" + +#define X509_FILETYPE_PEM 1 +#define X509_FILETYPE_ASN1 2 +#define X509_FILETYPE_DEFAULT 3 + +typedef struct X509_algor_st + { + ASN1_OBJECT *algorithm; + ASN1_TYPE *parameter; + } X509_ALGOR; + +typedef struct X509_val_st + { + ASN1_UTCTIME *notBefore; + ASN1_UTCTIME *notAfter; + } X509_VAL; + +typedef struct X509_pubkey_st + { + X509_ALGOR *algor; + ASN1_BIT_STRING *public_key; + struct evp_pkey_st /* EVP_PKEY*/ *pkey; + } X509_PUBKEY; + +typedef struct X509_sig_st + { + X509_ALGOR *algor; + ASN1_OCTET_STRING *digest; + } X509_SIG; + +typedef struct X509_name_entry_st + { + ASN1_OBJECT *object; + ASN1_STRING *value; + int set; + int size; /* temp variable */ + } X509_NAME_ENTRY; + +/* we always keep X509_NAMEs in 2 forms. */ +typedef struct X509_name_st + { + STACK *entries; /* of X509_NAME_ENTRY */ + int modified; /* true if 'bytes' needs to be built */ +#ifdef HEADER_BUFFER_H + BUF_MEM *bytes; +#else + char *bytes; +#endif + } X509_NAME; + +typedef struct X509_extension_st + { + ASN1_OBJECT *object; + short critical; + short netscape_hack; + ASN1_OCTET_STRING *value; + } X509_EXTENSION; + +#define X509_EXT_PACK_UNKNOWN 0 +#define X509_EXT_PACK_STRING 1 /* X509v3_pack_string() */ + +typedef struct x509_extension_method_st + { + int nid; + int data_type; + int pack_type; + } X509_EXTENSION_METHOD; + +typedef struct X509_req_info_st + { + ASN1_INTEGER *version; + X509_NAME *subject; + X509_PUBKEY *pubkey; + /* d=2 hl=2 l= 0 cons: cont: 00 */ + STACK /* X509_ATTRIBUTE */ *attributes; /* [ 0 ] */ + int req_kludge; + } X509_REQ_INFO; + +typedef struct X509_req_st + { + X509_REQ_INFO *req_info; + X509_ALGOR *sig_alg; + ASN1_BIT_STRING *signature; + int references; + } X509_REQ; + +typedef struct x509_cinf_st + { + ASN1_INTEGER *version; /* [ 0 ] default of v1 */ + ASN1_INTEGER *serialNumber; + X509_ALGOR *signature; + X509_NAME *issuer; + X509_VAL *validity; + X509_NAME *subject; + X509_PUBKEY *key; + ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */ + ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */ + STACK /* X509_EXTENSION */ *extensions; /* [ 3 ] optional in v3 */ + } X509_CINF; + +typedef struct x509_st + { + X509_CINF *cert_info; + X509_ALGOR *sig_alg; + ASN1_BIT_STRING *signature; + int valid; + int references; + char *name; + } X509; + +typedef struct X509_revoked_st + { + ASN1_INTEGER *serialNumber; + ASN1_UTCTIME *revocationDate; + STACK /* optional X509_EXTENSION */ *extensions; + int sequence; /* load sequence */ + } X509_REVOKED; + +typedef struct X509_crl_info_st + { + ASN1_INTEGER *version; + X509_ALGOR *sig_alg; + X509_NAME *issuer; + ASN1_UTCTIME *lastUpdate; + ASN1_UTCTIME *nextUpdate; + STACK /* X509_REVOKED */ *revoked; + STACK /* [0] X509_EXTENSION */ *extensions; + } X509_CRL_INFO; + +typedef struct X509_crl_st + { + /* actual signature */ + X509_CRL_INFO *crl; + X509_ALGOR *sig_alg; + ASN1_BIT_STRING *signature; + int references; + } X509_CRL; + +/* a sequence of these are used */ +typedef struct x509_attributes_st + { + ASN1_OBJECT *object; + int set; /* 1 for a set, 0 for a single item (which is wrong) */ + union { + char *ptr; +/* 1 */ STACK /* ASN1_TYPE */ *set; +/* 0 */ ASN1_TYPE *single; + } value; + } X509_ATTRIBUTE; + +typedef struct private_key_st + { + int version; + /* The PKCS#8 data types */ + X509_ALGOR *enc_algor; + ASN1_OCTET_STRING *enc_pkey; /* encrypted pub key */ + + /* When decrypted, the following will not be NULL */ + EVP_PKEY *dec_pkey; + + /* used to encrypt and decrypt */ + int key_length; + char *key_data; + int key_free; /* true if we should auto free key_data */ + + /* expanded version of 'enc_algor' */ + EVP_CIPHER_INFO cipher; + + int references; + } X509_PKEY; + +#ifdef HEADER_ENVELOPE_H +typedef struct X509_info_st + { + X509 *x509; + X509_CRL *crl; + X509_PKEY *x_pkey; + + EVP_CIPHER_INFO enc_cipher; + int enc_len; + char *enc_data; + + int references; + } X509_INFO; +#endif + +/* The next 2 structures and their 8 routines were sent to me by + * Pat Richard <patr@x509.com> and are used to manipulate + * Netscapes spki strucutres - usefull if you are writing a CA web page + */ +typedef struct Netscape_spkac_st + { + X509_PUBKEY *pubkey; + ASN1_IA5STRING *challenge; /* challenge sent in atlas >= PR2 */ + } NETSCAPE_SPKAC; + +typedef struct Netscape_spki_st + { + NETSCAPE_SPKAC *spkac; /* signed public key and challenge */ + X509_ALGOR *sig_algor; + ASN1_BIT_STRING *signature; + } NETSCAPE_SPKI; + +#ifndef HEADER_BN_H +#define BIGNUM char +#endif + +typedef struct CBCParameter_st + { + unsigned char iv[8]; + } CBC_PARAM; + +#include "x509_vfy.h" +#include "pkcs7.h" + +#ifdef SSLEAY_MACROS +#define X509_verify(a,r) ASN1_verify((int (*)())i2d_X509_CINF,a->sig_alg,\ + a->signature,(char *)a->cert_info,r) +#define X509_REQ_verify(a,r) ASN1_verify((int (*)())i2d_X509_REQ_INFO, \ + a->sig_alg,a->signature,(char *)a->req_info,r) +#define X509_CRL_verify(a,r) ASN1_verify((int (*)())i2d_X509_CRL_INFO, \ + a->sig_alg, a->signature,(char *)a->crl,r) +#define NETSCAPE_SPKI_verify(a,r) ASN1_verify((int (*)())i2d_NETSCAPE_SPKAC, \ + a->sig_algor,a->signature, (char *)a->spkac,r) + +#define X509_sign(x,pkey,md) \ + ASN1_sign((int (*)())i2d_X509_CINF, x->cert_info->signature, \ + x->sig_alg, x->signature, (char *)x->cert_info,pkey,md) +#define X509_REQ_sign(x,pkey,md) \ + ASN1_sign((int (*)())i2d_X509_REQ_INFO,x->sig_alg, NULL, \ + x->signature, (char *)x->req_info,pkey,md) +#define X509_CRL_sign(x,pkey,md) \ + ASN1_sign((int (*)())i2d_X509_CRL_INFO,x->crl->sig_alg,x->sig_alg, \ + x->signature, (char *)x->crl,pkey,md) +#define NETSCAPE_SPKI_sign(x,pkey,md) \ + ASN1_sign((int (*)())i2d_NETSCAPE_SPKAC, x->sig_algor,NULL, \ + x->signature, (char *)x->spkac,pkey,md) + +#define X509_dup(x509) (X509 *)ASN1_dup((int (*)())i2d_X509, \ + (char *(*)())d2i_X509,(char *)x509) +#define X509_EXTENSION_dup(ex) (X509_EXTENSION *)ASN1_dup( \ + (int (*)())i2d_X509_EXTENSION, \ + (char *(*)())d2i_X509_EXTENSION,(char *)ex) +#define d2i_X509_fp(fp,x509) (X509 *)ASN1_d2i_fp((char *(*)())X509_new, \ + (char *(*)())d2i_X509, (fp),(unsigned char **)(x509)) +#define i2d_X509_fp(fp,x509) ASN1_i2d_fp(i2d_X509,fp,(unsigned char *)x509) +#define d2i_X509_bio(bp,x509) (X509 *)ASN1_d2i_bio((char *(*)())X509_new, \ + (char *(*)())d2i_X509, (bp),(unsigned char **)(x509)) +#define i2d_X509_bio(bp,x509) ASN1_i2d_bio(i2d_X509,bp,(unsigned char *)x509) + +#define X509_CRL_dup(crl) (X509_CRL *)ASN1_dup((int (*)())i2d_X509_CRL, \ + (char *(*)())d2i_X509_CRL,(char *)crl) +#define d2i_X509_CRL_fp(fp,crl) (X509_CRL *)ASN1_d2i_fp((char *(*)()) \ + X509_CRL_new,(char *(*)())d2i_X509_CRL, (fp),\ + (unsigned char **)(crl)) +#define i2d_X509_CRL_fp(fp,crl) ASN1_i2d_fp(i2d_X509_CRL,fp,\ + (unsigned char *)crl) +#define d2i_X509_CRL_bio(bp,crl) (X509_CRL *)ASN1_d2i_bio((char *(*)()) \ + X509_CRL_new,(char *(*)())d2i_X509_CRL, (bp),\ + (unsigned char **)(crl)) +#define i2d_X509_CRL_bio(bp,crl) ASN1_i2d_bio(i2d_X509_CRL,bp,\ + (unsigned char *)crl) + +#define PKCS7_dup(p7) (PKCS7 *)ASN1_dup((int (*)())i2d_PKCS7, \ + (char *(*)())d2i_PKCS7,(char *)p7) +#define d2i_PKCS7_fp(fp,p7) (PKCS7 *)ASN1_d2i_fp((char *(*)()) \ + PKCS7_new,(char *(*)())d2i_PKCS7, (fp),\ + (unsigned char **)(p7)) +#define i2d_PKCS7_fp(fp,p7) ASN1_i2d_fp(i2d_PKCS7,fp,\ + (unsigned char *)p7) +#define d2i_PKCS7_bio(bp,p7) (PKCS7 *)ASN1_d2i_bio((char *(*)()) \ + PKCS7_new,(char *(*)())d2i_PKCS7, (bp),\ + (unsigned char **)(p7)) +#define i2d_PKCS7_bio(bp,p7) ASN1_i2d_bio(i2d_PKCS7,bp,\ + (unsigned char *)p7) + +#define X509_REQ_dup(req) (X509_REQ *)ASN1_dup((int (*)())i2d_X509_REQ, \ + (char *(*)())d2i_X509_REQ,(char *)req) +#define d2i_X509_REQ_fp(fp,req) (X509_REQ *)ASN1_d2i_fp((char *(*)())\ + X509_REQ_new, (char *(*)())d2i_X509_REQ, (fp),\ + (unsigned char **)(req)) +#define i2d_X509_REQ_fp(fp,req) ASN1_i2d_fp(i2d_X509_REQ,fp,\ + (unsigned char *)req) +#define d2i_X509_REQ_bio(bp,req) (X509_REQ *)ASN1_d2i_bio((char *(*)())\ + X509_REQ_new, (char *(*)())d2i_X509_REQ, (bp),\ + (unsigned char **)(req)) +#define i2d_X509_REQ_bio(bp,req) ASN1_i2d_bio(i2d_X509_REQ,bp,\ + (unsigned char *)req) + +#define RSAPublicKey_dup(rsa) (RSA *)ASN1_dup((int (*)())i2d_RSAPublicKey, \ + (char *(*)())d2i_RSAPublicKey,(char *)rsa) +#define RSAPrivateKey_dup(rsa) (RSA *)ASN1_dup((int (*)())i2d_RSAPrivateKey, \ + (char *(*)())d2i_RSAPrivateKey,(char *)rsa) + +#define d2i_RSAPrivateKey_fp(fp,rsa) (RSA *)ASN1_d2i_fp((char *(*)())\ + RSA_new,(char *(*)())d2i_RSAPrivateKey, (fp), \ + (unsigned char **)(rsa)) +#define i2d_RSAPrivateKey_fp(fp,rsa) ASN1_i2d_fp(i2d_RSAPrivateKey,fp, \ + (unsigned char *)rsa) +#define d2i_RSAPrivateKey_bio(bp,rsa) (RSA *)ASN1_d2i_bio((char *(*)())\ + RSA_new,(char *(*)())d2i_RSAPrivateKey, (bp), \ + (unsigned char **)(rsa)) +#define i2d_RSAPrivateKey_bio(bp,rsa) ASN1_i2d_bio(i2d_RSAPrivateKey,bp, \ + (unsigned char *)rsa) + +#define d2i_RSAPublicKey_fp(fp,rsa) (RSA *)ASN1_d2i_fp((char *(*)())\ + RSA_new,(char *(*)())d2i_RSAPublicKey, (fp), \ + (unsigned char **)(rsa)) +#define i2d_RSAPublicKey_fp(fp,rsa) ASN1_i2d_fp(i2d_RSAPublicKey,fp, \ + (unsigned char *)rsa) +#define d2i_RSAPublicKey_bio(bp,rsa) (RSA *)ASN1_d2i_bio((char *(*)())\ + RSA_new,(char *(*)())d2i_RSAPublicKey, (bp), \ + (unsigned char **)(rsa)) +#define i2d_RSAPublicKey_bio(bp,rsa) ASN1_i2d_bio(i2d_RSAPublicKey,bp, \ + (unsigned char *)rsa) + +#define d2i_DSAPrivateKey_fp(fp,dsa) (DSA *)ASN1_d2i_fp((char *(*)())\ + DSA_new,(char *(*)())d2i_DSAPrivateKey, (fp), \ + (unsigned char **)(dsa)) +#define i2d_DSAPrivateKey_fp(fp,dsa) ASN1_i2d_fp(i2d_DSAPrivateKey,fp, \ + (unsigned char *)dsa) +#define d2i_DSAPrivateKey_bio(bp,dsa) (DSA *)ASN1_d2i_bio((char *(*)())\ + DSA_new,(char *(*)())d2i_DSAPrivateKey, (bp), \ + (unsigned char **)(dsa)) +#define i2d_DSAPrivateKey_bio(bp,dsa) ASN1_i2d_bio(i2d_DSAPrivateKey,bp, \ + (unsigned char *)dsa) + +#define X509_NAME_dup(xn) (X509_NAME *)ASN1_dup((int (*)())i2d_X509_NAME, \ + (char *(*)())d2i_X509_NAME,(char *)xn) +#define X509_NAME_ENTRY_dup(ne) (X509_NAME_ENTRY *)ASN1_dup( \ + (int (*)())i2d_X509_NAME_ENTRY, \ + (char *(*)())d2i_X509_NAME_ENTRY,\ + (char *)ne) + +#define X509_digest(data,type,md,len) \ + ASN1_digest((int (*)())i2d_X509,type,(char *)data,md,len) +#define X509_NAME_digest(data,type,md,len) \ + ASN1_digest((int (*)())i2d_X509_NAME,type,(char *)data,md,len) +#define PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) \ + ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type,\ + (char *)data,md,len) +#endif + +#define X509_get_version(x) ASN1_INTEGER_get((x)->cert_info->version) +/* #define X509_get_serialNumber(x) ((x)->cert_info->serialNumber) */ +#define X509_get_notBefore(x) ((x)->cert_info->validity->notBefore) +#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter) +#define X509_extract_key(x) X509_get_pubkey(x) /*****/ +#define X509_REQ_get_version(x) ASN1_INTEGER_get((x)->req_info->version) +#define X509_REQ_get_subject_name(x) ((x)->req_info->subject) +#define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a) +#define X509_name_cmp(a,b) X509_NAME_cmp((a),(b)) +#define X509_get_signature_type(x) EVP_PKEY_type(OBJ_obj2nid((x)->sig_alg->algorithm)) + +/* This one is only used so that a binary form can output, as in + * i2d_X509_NAME(X509_get_X509_PUBKEY(x),&buf) */ +#define X509_get_X509_PUBKEY(x) ((x)->cert_info->key) + +#ifndef NOPROTO + +#ifndef SSLEAY_MACROS +#ifdef HEADER_ENVELOPE_H +int X509_verify(X509 *a, EVP_PKEY *r); +char *X509_verify_cert_error_string(int n); + +int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r); +int X509_CRL_verify(X509_CRL *a, EVP_PKEY *r); +int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r); + +int X509_sign(X509 *x, EVP_PKEY *pkey, EVP_MD *md); +int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, EVP_MD *md); +int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, EVP_MD *md); +int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, EVP_MD *md); + +int X509_digest(X509 *data,EVP_MD *type,unsigned char *md,unsigned int *len); +int X509_NAME_digest(X509_NAME *data,EVP_MD *type, + unsigned char *md,unsigned int *len); +#endif + +#ifndef WIN16 +X509 *d2i_X509_fp(FILE *fp, X509 *x509); +int i2d_X509_fp(FILE *fp,X509 *x509); +X509_CRL *d2i_X509_CRL_fp(FILE *fp,X509_CRL *crl); +int i2d_X509_CRL_fp(FILE *fp,X509_CRL *crl); +X509_REQ *d2i_X509_REQ_fp(FILE *fp,X509_REQ *req); +int i2d_X509_REQ_fp(FILE *fp,X509_REQ *req); +RSA *d2i_RSAPrivateKey_fp(FILE *fp,RSA *rsa); +int i2d_RSAPrivateKey_fp(FILE *fp,RSA *rsa); +DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA *dsa); +int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa); +RSA *d2i_RSAPublicKey_fp(FILE *fp,RSA *rsa); +int i2d_RSAPublicKey_fp(FILE *fp,RSA *rsa); +#endif + +#ifdef HEADER_BIO_H +X509 *d2i_X509_bio(BIO *bp,X509 *x509); +int i2d_X509_bio(BIO *bp,X509 *x509); +X509_CRL *d2i_X509_CRL_bio(BIO *bp,X509_CRL *crl); +int i2d_X509_CRL_bio(BIO *bp,X509_CRL *crl); +X509_REQ *d2i_X509_REQ_bio(BIO *bp,X509_REQ *req); +int i2d_X509_REQ_bio(BIO *bp,X509_REQ *req); +RSA *d2i_RSAPrivateKey_bio(BIO *bp,RSA *rsa); +int i2d_RSAPrivateKey_bio(BIO *bp,RSA *rsa); +DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA *dsa); +int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa); +RSA *d2i_RSAPublicKey_bio(BIO *bp,RSA *rsa); +int i2d_RSAPublicKey_bio(BIO *bp,RSA *rsa); +#endif + +X509 *X509_dup(X509 *x509); +X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *ex); +X509_CRL *X509_CRL_dup(X509_CRL *crl); +X509_REQ *X509_REQ_dup(X509_REQ *req); +X509_NAME *X509_NAME_dup(X509_NAME *xn); +X509_NAME_ENTRY *X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne); +RSA *RSAPublicKey_dup(RSA *rsa); +RSA *RSAPrivateKey_dup(RSA *rsa); + +#endif /* !SSLEAY_MACROS */ + +int X509_cmp_current_time(ASN1_UTCTIME *s); +ASN1_UTCTIME * X509_gmtime_adj(ASN1_UTCTIME *s, long adj); + +char * X509_get_default_cert_area(void ); +char * X509_get_default_cert_dir(void ); +char * X509_get_default_cert_file(void ); +char * X509_get_default_cert_dir_env(void ); +char * X509_get_default_cert_file_env(void ); +char * X509_get_default_private_dir(void ); + +X509_REQ * X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey); +X509 * X509_REQ_to_X509(X509_REQ *r, int days,EVP_PKEY *pkey); +void ERR_load_X509_strings(void ); + +X509_ALGOR * X509_ALGOR_new(void ); +void X509_ALGOR_free(X509_ALGOR *a); +int i2d_X509_ALGOR(X509_ALGOR *a,unsigned char **pp); +X509_ALGOR * d2i_X509_ALGOR(X509_ALGOR **a,unsigned char **pp, + long length); + +X509_VAL * X509_VAL_new(void ); +void X509_VAL_free(X509_VAL *a); +int i2d_X509_VAL(X509_VAL *a,unsigned char **pp); +X509_VAL * d2i_X509_VAL(X509_VAL **a,unsigned char **pp, + long length); + +X509_PUBKEY * X509_PUBKEY_new(void ); +void X509_PUBKEY_free(X509_PUBKEY *a); +int i2d_X509_PUBKEY(X509_PUBKEY *a,unsigned char **pp); +X509_PUBKEY * d2i_X509_PUBKEY(X509_PUBKEY **a,unsigned char **pp, + long length); +int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey); +EVP_PKEY * X509_PUBKEY_get(X509_PUBKEY *key); +int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK *chain); + + +X509_SIG * X509_SIG_new(void ); +void X509_SIG_free(X509_SIG *a); +int i2d_X509_SIG(X509_SIG *a,unsigned char **pp); +X509_SIG * d2i_X509_SIG(X509_SIG **a,unsigned char **pp,long length); + +X509_REQ_INFO *X509_REQ_INFO_new(void); +void X509_REQ_INFO_free(X509_REQ_INFO *a); +int i2d_X509_REQ_INFO(X509_REQ_INFO *a,unsigned char **pp); +X509_REQ_INFO *d2i_X509_REQ_INFO(X509_REQ_INFO **a,unsigned char **pp, + long length); + +X509_REQ * X509_REQ_new(void); +void X509_REQ_free(X509_REQ *a); +int i2d_X509_REQ(X509_REQ *a,unsigned char **pp); +X509_REQ * d2i_X509_REQ(X509_REQ **a,unsigned char **pp,long length); + +X509_ATTRIBUTE *X509_ATTRIBUTE_new(void ); +void X509_ATTRIBUTE_free(X509_ATTRIBUTE *a); +int i2d_X509_ATTRIBUTE(X509_ATTRIBUTE *a,unsigned char **pp); +X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(X509_ATTRIBUTE **a,unsigned char **pp, + long length); + +X509_EXTENSION *X509_EXTENSION_new(void ); +void X509_EXTENSION_free(X509_EXTENSION *a); +int i2d_X509_EXTENSION(X509_EXTENSION *a,unsigned char **pp); +X509_EXTENSION *d2i_X509_EXTENSION(X509_EXTENSION **a,unsigned char **pp, + long length); + +X509_NAME_ENTRY *X509_NAME_ENTRY_new(void); +void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a); +int i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a,unsigned char **pp); +X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **a,unsigned char **pp, + long length); + +X509_NAME * X509_NAME_new(void); +void X509_NAME_free(X509_NAME *a); +int i2d_X509_NAME(X509_NAME *a,unsigned char **pp); +X509_NAME * d2i_X509_NAME(X509_NAME **a,unsigned char **pp,long length); +int X509_NAME_set(X509_NAME **xn, X509_NAME *name); + + +X509_CINF * X509_CINF_new(void); +void X509_CINF_free(X509_CINF *a); +int i2d_X509_CINF(X509_CINF *a,unsigned char **pp); +X509_CINF * d2i_X509_CINF(X509_CINF **a,unsigned char **pp,long length); + +X509 * X509_new(void); +void X509_free(X509 *a); +int i2d_X509(X509 *a,unsigned char **pp); +X509 * d2i_X509(X509 **a,unsigned char **pp,long length); + +X509_REVOKED * X509_REVOKED_new(void); +void X509_REVOKED_free(X509_REVOKED *a); +int i2d_X509_REVOKED(X509_REVOKED *a,unsigned char **pp); +X509_REVOKED * d2i_X509_REVOKED(X509_REVOKED **a,unsigned char **pp,long length); + +X509_CRL_INFO *X509_CRL_INFO_new(void); +void X509_CRL_INFO_free(X509_CRL_INFO *a); +int i2d_X509_CRL_INFO(X509_CRL_INFO *a,unsigned char **pp); +X509_CRL_INFO *d2i_X509_CRL_INFO(X509_CRL_INFO **a,unsigned char **pp, + long length); + +X509_CRL * X509_CRL_new(void); +void X509_CRL_free(X509_CRL *a); +int i2d_X509_CRL(X509_CRL *a,unsigned char **pp); +X509_CRL * d2i_X509_CRL(X509_CRL **a,unsigned char **pp,long length); + +X509_PKEY * X509_PKEY_new(void ); +void X509_PKEY_free(X509_PKEY *a); +int i2d_X509_PKEY(X509_PKEY *a,unsigned char **pp); +X509_PKEY * d2i_X509_PKEY(X509_PKEY **a,unsigned char **pp,long length); + +NETSCAPE_SPKI * NETSCAPE_SPKI_new(void ); +void NETSCAPE_SPKI_free(NETSCAPE_SPKI *a); +int i2d_NETSCAPE_SPKI(NETSCAPE_SPKI *a,unsigned char **pp); +NETSCAPE_SPKI * d2i_NETSCAPE_SPKI(NETSCAPE_SPKI **a,unsigned char **pp, + long length); + +NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(void ); +void NETSCAPE_SPKAC_free(NETSCAPE_SPKAC *a); +int i2d_NETSCAPE_SPKAC(NETSCAPE_SPKAC *a,unsigned char **pp); +NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(NETSCAPE_SPKAC **a,unsigned char **pp, + long length); + +#ifdef HEADER_ENVELOPE_H +X509_INFO * X509_INFO_new(void); +void X509_INFO_free(X509_INFO *a); +char * X509_NAME_oneline(X509_NAME *a,char *buf,int size); + +int ASN1_verify(int (*i2d)(), X509_ALGOR *algor1, + ASN1_BIT_STRING *signature,char *data,EVP_PKEY *pkey); + +int ASN1_digest(int (*i2d)(),EVP_MD *type,char *data, + unsigned char *md,unsigned int *len); + +int ASN1_sign(int (*i2d)(), X509_ALGOR *algor1, X509_ALGOR *algor2, + ASN1_BIT_STRING *signature, + char *data,EVP_PKEY *pkey, EVP_MD *type); +#endif + +int X509_set_version(X509 *x,long version); +int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial); +ASN1_INTEGER * X509_get_serialNumber(X509 *x); +int X509_set_issuer_name(X509 *x, X509_NAME *name); +X509_NAME * X509_get_issuer_name(X509 *a); +int X509_set_subject_name(X509 *x, X509_NAME *name); +X509_NAME * X509_get_subject_name(X509 *a); +int X509_set_notBefore(X509 *x, ASN1_UTCTIME *tm); +int X509_set_notAfter(X509 *x, ASN1_UTCTIME *tm); +int X509_set_pubkey(X509 *x, EVP_PKEY *pkey); +EVP_PKEY * X509_get_pubkey(X509 *x); +int X509_certificate_type(X509 *x,EVP_PKEY *pubkey /* optional */); + +int X509_REQ_set_version(X509_REQ *x,long version); +int X509_REQ_set_subject_name(X509_REQ *req,X509_NAME *name); +int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey); +EVP_PKEY * X509_REQ_get_pubkey(X509_REQ *req); + +int X509_check_private_key(X509 *x509,EVP_PKEY *pkey); + +int X509_issuer_and_serial_cmp(X509 *a, X509 *b); +unsigned long X509_issuer_and_serial_hash(X509 *a); + +int X509_issuer_name_cmp(X509 *a, X509 *b); +unsigned long X509_issuer_name_hash(X509 *a); + +int X509_subject_name_cmp(X509 *a,X509 *b); +unsigned long X509_subject_name_hash(X509 *x); + +int X509_NAME_cmp (X509_NAME *a, X509_NAME *b); +unsigned long X509_NAME_hash(X509_NAME *x); + +int X509_CRL_cmp(X509_CRL *a,X509_CRL *b); +#ifndef WIN16 +int X509_print_fp(FILE *bp,X509 *x); +int X509_REQ_print_fp(FILE *bp,X509_REQ *req); +#endif + +#ifdef HEADER_BIO_H +int X509_NAME_print(BIO *bp, X509_NAME *name, int obase); +int X509_print(BIO *bp,X509 *x); +int X509_REQ_print(BIO *bp,X509_REQ *req); +#endif + +int X509_NAME_entry_count(X509_NAME *name); +int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, + char *buf,int len); +int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, + char *buf,int len); + +int X509_NAME_get_index_by_NID(X509_NAME *name,int nid,int oldpos); +int X509_NAME_get_index_by_OBJ(X509_NAME *name,ASN1_OBJECT *obj, + int oldpos); +X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc); +X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc); +int X509_NAME_add_entry(X509_NAME *name,X509_NAME_ENTRY *ne, + int loc, int set); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, + int type,unsigned char *bytes, int len); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, + ASN1_OBJECT *obj, int type,unsigned char *bytes, + int len); +int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, + ASN1_OBJECT *obj); +int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type, + unsigned char *bytes, int len); +ASN1_OBJECT * X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne); +ASN1_STRING * X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne); + +int X509v3_get_ext_count(STACK *x); +int X509v3_get_ext_by_NID(STACK *x, int nid, int oldpos); +int X509v3_get_ext_by_OBJ(STACK *x,ASN1_OBJECT *obj,int oldpos); +int X509v3_get_ext_by_critical(STACK *x, int crit, int oldpos); +X509_EXTENSION *X509v3_get_ext(STACK *x, int loc); +X509_EXTENSION *X509v3_delete_ext(STACK *x, int loc); +STACK * X509v3_add_ext(STACK **x, X509_EXTENSION *ex, int loc); +int X509v3_data_type_by_OBJ(ASN1_OBJECT *obj); +int X509v3_data_type_by_NID(int nid); +int X509v3_pack_type_by_OBJ(ASN1_OBJECT *obj); +int X509v3_pack_type_by_NID(int nid); + +int X509_get_ext_count(X509 *x); +int X509_get_ext_by_NID(X509 *x, int nid, int oldpos); +int X509_get_ext_by_OBJ(X509 *x,ASN1_OBJECT *obj,int oldpos); +int X509_get_ext_by_critical(X509 *x, int crit, int oldpos); +X509_EXTENSION *X509_get_ext(X509 *x, int loc); +X509_EXTENSION *X509_delete_ext(X509 *x, int loc); +int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc); + +int X509_CRL_get_ext_count(X509_CRL *x); +int X509_CRL_get_ext_by_NID(X509_CRL *x, int nid, int oldpos); +int X509_CRL_get_ext_by_OBJ(X509_CRL *x,ASN1_OBJECT *obj,int oldpos); +int X509_CRL_get_ext_by_critical(X509_CRL *x, int crit, int oldpos); +X509_EXTENSION *X509_CRL_get_ext(X509_CRL *x, int loc); +X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc); +int X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc); + +int X509_REVOKED_get_ext_count(X509_REVOKED *x); +int X509_REVOKED_get_ext_by_NID(X509_REVOKED *x, int nid, int oldpos); +int X509_REVOKED_get_ext_by_OBJ(X509_REVOKED *x,ASN1_OBJECT *obj,int oldpos); +int X509_REVOKED_get_ext_by_critical(X509_REVOKED *x, int crit, int oldpos); +X509_EXTENSION *X509_REVOKED_get_ext(X509_REVOKED *x, int loc); +X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc); +int X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc); + +X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, + int nid, int crit, ASN1_OCTET_STRING *data); +X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, + ASN1_OBJECT *obj,int crit,ASN1_OCTET_STRING *data); +int X509_EXTENSION_set_object(X509_EXTENSION *ex,ASN1_OBJECT *obj); +int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit); +int X509_EXTENSION_set_data(X509_EXTENSION *ex, + ASN1_OCTET_STRING *data); +ASN1_OBJECT * X509_EXTENSION_get_object(X509_EXTENSION *ex); +ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ne); +int X509_EXTENSION_get_critical(X509_EXTENSION *ex); +ASN1_OCTET_STRING *X509v3_pack_string(ASN1_OCTET_STRING **ex,int type, + unsigned char *bytes, int len); +ASN1_STRING * X509v3_unpack_string(ASN1_STRING **ex,int type, + ASN1_OCTET_STRING *os); + +int X509_verify_cert(X509_STORE_CTX *ctx); + +/* lookup a cert from a X509 STACK */ +X509 *X509_find_by_issuer_and_serial(STACK *sk,X509_NAME *name, + ASN1_INTEGER *serial); +X509 *X509_find_by_subject(STACK *sk,X509_NAME *name); + +#else + +#ifndef SSLEAY_MACROS +#ifdef HEADER_ENVELOPE_H +int X509_verify(); +int X509_REQ_verify(); +int X509_CRL_verify(); +int NETSCAPE_SPKI_verify(); + +int X509_sign(); +int X509_REQ_sign(); +int X509_CRL_sign(); +int NETSCAPE_SPKI_sign(); + +int X509_digest(); +int X509_NAME_digest(); +#endif + +#ifndef WIN16 +X509 *d2i_X509_fp(); +int i2d_X509_fp(); +X509_CRL *d2i_X509_CRL_fp(); +int i2d_X509_CRL_fp(); +X509_REQ *d2i_X509_REQ_fp(); +int i2d_X509_REQ_fp(); +RSA *d2i_RSAPrivateKey_fp(); +int i2d_RSAPrivateKey_fp(); +DSA *d2i_DSAPrivateKey_fp(); +int i2d_DSAPrivateKey_fp(); +RSA *d2i_RSAPublicKey_fp(); +int i2d_RSAPublicKey_fp(); +#endif + +X509 *d2i_X509_bio(); +int i2d_X509_bio(); +X509_CRL *d2i_X509_CRL_bio(); +int i2d_X509_CRL_bio(); +X509_REQ *d2i_X509_REQ_bio(); +int i2d_X509_REQ_bio(); +RSA *d2i_RSAPrivateKey_bio(); +int i2d_RSAPrivateKey_bio(); +DSA *d2i_DSAPrivateKey_bio(); +int i2d_DSAPrivateKey_bio(); +RSA *d2i_RSAPublicKey_bio(); +int i2d_RSAPublicKey_bio(); + +X509 *X509_dup(); +X509_EXTENSION *X509_EXTENSION_dup(); +X509_CRL *X509_CRL_dup(); +X509_REQ *X509_REQ_dup(); +X509_NAME *X509_NAME_dup(); +X509_NAME_ENTRY *X509_NAME_ENTRY_dup(); +RSA *RSAPublicKey_dup(); +RSA *RSAPrivateKey_dup(); + +#endif /* !SSLEAY_MACROS */ + +int X509_cmp_current_time(); +ASN1_UTCTIME * X509_gmtime_adj(); + +char * X509_get_default_cert_area(); +char * X509_get_default_cert_dir(); +char * X509_get_default_cert_file(); +char * X509_get_default_cert_dir_env(); +char * X509_get_default_cert_file_env(); +char * X509_get_default_private_dir(); + +X509_REQ * X509_to_X509_REQ(); +X509 * X509_REQ_to_X509(); +void ERR_load_X509_strings(); + +X509_ALGOR * X509_ALGOR_new(); +void X509_ALGOR_free(); +int i2d_X509_ALGOR(); +X509_ALGOR * d2i_X509_ALGOR(); + +X509_VAL * X509_VAL_new(); +void X509_VAL_free(); +int i2d_X509_VAL(); +X509_VAL * d2i_X509_VAL(); + +X509_PUBKEY * X509_PUBKEY_new(); +void X509_PUBKEY_free(); +int i2d_X509_PUBKEY(); +X509_PUBKEY * d2i_X509_PUBKEY(); +int X509_PUBKEY_set(); +EVP_PKEY * X509_PUBKEY_get(); +int X509_get_pubkey_parameters(); + +X509_SIG * X509_SIG_new(); +void X509_SIG_free(); +int i2d_X509_SIG(); +X509_SIG * d2i_X509_SIG(); + +X509_REQ_INFO *X509_REQ_INFO_new(); +void X509_REQ_INFO_free(); +int i2d_X509_REQ_INFO(); +X509_REQ_INFO *d2i_X509_REQ_INFO(); + +X509_REQ * X509_REQ_new(); +void X509_REQ_free(); +int i2d_X509_REQ(); +X509_REQ * d2i_X509_REQ(); + +X509_ATTRIBUTE *X509_ATTRIBUTE_new(); +void X509_ATTRIBUTE_free(); +int i2d_X509_ATTRIBUTE(); +X509_ATTRIBUTE *d2i_X509_ATTRIBUTE(); + +X509_EXTENSION *X509_EXTENSION_new(); +void X509_EXTENSION_free(); +int i2d_X509_EXTENSION(); +X509_EXTENSION *d2i_X509_EXTENSION(); + +X509_NAME_ENTRY *X509_NAME_ENTRY_new(); +void X509_NAME_ENTRY_free(); +int i2d_X509_NAME_ENTRY(); +X509_NAME_ENTRY *d2i_X509_NAME_ENTRY(); + +X509_NAME * X509_NAME_new(); +void X509_NAME_free(); +int i2d_X509_NAME(); +X509_NAME * d2i_X509_NAME(); +int X509_NAME_set(); + + +X509_CINF * X509_CINF_new(); +void X509_CINF_free(); +int i2d_X509_CINF(); +X509_CINF * d2i_X509_CINF(); + +X509 * X509_new(); +void X509_free(); +int i2d_X509(); +X509 * d2i_X509(); + +X509_REVOKED * X509_REVOKED_new(); +void X509_REVOKED_free(); +int i2d_X509_REVOKED(); +X509_REVOKED * d2i_X509_REVOKED(); + +X509_CRL_INFO *X509_CRL_INFO_new(); +void X509_CRL_INFO_free(); +int i2d_X509_CRL_INFO(); +X509_CRL_INFO *d2i_X509_CRL_INFO(); + +X509_CRL * X509_CRL_new(); +void X509_CRL_free(); +int i2d_X509_CRL(); +X509_CRL * d2i_X509_CRL(); + +X509_PKEY * X509_PKEY_new(); +void X509_PKEY_free(); +int i2d_X509_PKEY(); +X509_PKEY * d2i_X509_PKEY(); + +NETSCAPE_SPKI * NETSCAPE_SPKI_new(); +void NETSCAPE_SPKI_free(); +int i2d_NETSCAPE_SPKI(); +NETSCAPE_SPKI * d2i_NETSCAPE_SPKI(); + +NETSCAPE_SPKAC *NETSCAPE_SPKAC_new(); +void NETSCAPE_SPKAC_free(); +int i2d_NETSCAPE_SPKAC(); +NETSCAPE_SPKAC *d2i_NETSCAPE_SPKAC(); + +#ifdef HEADER_ENVELOPE_H +X509_INFO * X509_INFO_new(); +void X509_INFO_free(); +#endif + +char * X509_NAME_oneline(); + +int ASN1_verify(); +int ASN1_digest(); +int ASN1_sign(); + +int X509_set_version(); +int X509_set_serialNumber(); +ASN1_INTEGER * X509_get_serialNumber(); +int X509_set_issuer_name(); +X509_NAME * X509_get_issuer_name(); +int X509_set_subject_name(); +X509_NAME * X509_get_subject_name(); +int X509_set_notBefore(); +int X509_set_notAfter(); +int X509_set_pubkey(); +EVP_PKEY * X509_get_pubkey(); +int X509_certificate_type(); + +int X509_REQ_set_version(); +int X509_REQ_set_subject_name(); +int X509_REQ_set_pubkey(); +EVP_PKEY * X509_REQ_get_pubkey(); + +int X509_check_private_key(); + +int X509_issuer_and_serial_cmp(); +unsigned long X509_issuer_and_serial_hash(); + +int X509_issuer_name_cmp(); +unsigned long X509_issuer_name_hash(); + +int X509_subject_name_cmp(); +unsigned long X509_subject_name_hash(); + +int X509_NAME_cmp (); +unsigned long X509_NAME_hash(); + +int X509_CRL_cmp(); +#ifndef WIN16 +int X509_print_fp(); +int X509_REQ_print_fp(); +#endif + +int X509_NAME_print(); +int X509_print(); +int X509_REQ_print(); + +int X509_NAME_entry_count(); +int X509_NAME_get_text_by_NID(); +int X509_NAME_get_text_by_OBJ(); + +int X509_NAME_get_index_by_NID(); +int X509_NAME_get_index_by_OBJ(); +X509_NAME_ENTRY *X509_NAME_get_entry(); +X509_NAME_ENTRY *X509_NAME_delete_entry(); +int X509_NAME_add_entry(); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(); +int X509_NAME_ENTRY_set_object(); +int X509_NAME_ENTRY_set_data(); +ASN1_OBJECT * X509_NAME_ENTRY_get_object(); +ASN1_STRING * X509_NAME_ENTRY_get_data(); + +int X509v3_get_ext_count(); +int X509v3_get_ext_by_NID(); +int X509v3_get_ext_by_OBJ(); +int X509v3_get_ext_by_critical(); +X509_EXTENSION *X509v3_get_ext(); +X509_EXTENSION *X509v3_delete_ext(); +STACK * X509v3_add_ext(); +int X509v3_data_type_by_OBJ(); +int X509v3_data_type_by_NID(); +int X509v3_pack_type_by_OBJ(); +int X509v3_pack_type_by_NID(); + + +int X509_get_ext_count(); +int X509_get_ext_by_NID(); +int X509_get_ext_by_OBJ(); +int X509_get_ext_by_critical(); +X509_EXTENSION *X509_get_ext(); +X509_EXTENSION *X509_delete_ext(); +int X509_add_ext(); + +int X509_CRL_get_ext_count(); +int X509_CRL_get_ext_by_NID(); +int X509_CRL_get_ext_by_OBJ(); +int X509_CRL_get_ext_by_critical(); +X509_EXTENSION *X509_CRL_get_ext(); +X509_EXTENSION *X509_CRL_delete_ext(); +int X509_CRL_add_ext(); + +int X509_REVOKED_get_ext_count(); +int X509_REVOKED_get_ext_by_NID(); +int X509_REVOKED_get_ext_by_OBJ(); +int X509_REVOKED_get_ext_by_critical(); +X509_EXTENSION *X509_REVOKED_get_ext(); +X509_EXTENSION *X509_REVOKED_delete_ext(); +int X509_REVOKED_add_ext(); + +X509_EXTENSION *X509_EXTENSION_create_by_NID(); +X509_EXTENSION *X509_EXTENSION_create_by_OBJ(); +int X509_EXTENSION_set_object(); +int X509_EXTENSION_set_critical(); +int X509_EXTENSION_set_data(); +ASN1_OBJECT * X509_EXTENSION_get_object(); +ASN1_OCTET_STRING *X509_EXTENSION_get_data(); +int X509_EXTENSION_get_critical(); +ASN1_OCTET_STRING *X509v3_pack_string(); +ASN1_STRING * X509v3_unpack_string(); + +int X509_verify_cert(); +char * X509_verify_cert_error_string(); + +/* lookup a cert from a X509 STACK */ +X509 *X509_find_by_issuer_and_serial(); +X509 *X509_find_by_subject(); + +#endif + +/* BEGIN ERROR CODES */ +/* Error codes for the X509 functions. */ + +/* Function codes. */ +#define X509_F_ADD_CERT_DIR 100 +#define X509_F_BY_FILE_CTRL 101 +#define X509_F_DIR_CTRL 102 +#define X509_F_GET_CERT_BY_SUBJECT 103 +#define X509_F_X509V3_ADD_EXT 104 +#define X509_F_X509V3_ADD_EXTENSION 105 +#define X509_F_X509V3_PACK_STRING 106 +#define X509_F_X509V3_UNPACK_STRING 107 +#define X509_F_X509_EXTENSION_CREATE_BY_NID 108 +#define X509_F_X509_EXTENSION_CREATE_BY_OBJ 109 +#define X509_F_X509_GET_PUBKEY_PARAMETERS 110 +#define X509_F_X509_LOAD_CERT_FILE 111 +#define X509_F_X509_NAME_ADD_ENTRY 112 +#define X509_F_X509_NAME_ENTRY_CREATE_BY_NID 113 +#define X509_F_X509_NAME_ENTRY_SET_OBJECT 114 +#define X509_F_X509_NAME_ONELINE 115 +#define X509_F_X509_NAME_PRINT 116 +#define X509_F_X509_PRINT_FP 117 +#define X509_F_X509_PUBKEY_GET 118 +#define X509_F_X509_PUBKEY_SET 119 +#define X509_F_X509_REQ_PRINT 120 +#define X509_F_X509_REQ_PRINT_FP 121 +#define X509_F_X509_REQ_TO_X509 122 +#define X509_F_X509_STORE_ADD_CERT 123 +#define X509_F_X509_TO_X509_REQ 124 +#define X509_F_X509_VERIFY_CERT 125 + +/* Reason codes. */ +#define X509_R_BAD_X509_FILETYPE 100 +#define X509_R_CERT_ALREADY_IN_HASH_TABLE 101 +#define X509_R_ERR_ASN1_LIB 102 +#define X509_R_LOADING_CERT_DIR 103 +#define X509_R_LOADING_DEFAULTS 104 +#define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 105 +#define X509_R_SHOULD_RETRY 106 +#define X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN 107 +#define X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY 108 +#define X509_R_UNKNOWN_NID 109 +#define X509_R_UNKNOWN_STRING_TYPE 110 +#define X509_R_UNSUPPORTED_ALGORITHM 111 + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/crypto/x509/x509_cmp.c b/crypto/x509/x509_cmp.c new file mode 100644 index 0000000000..14eebbfcb8 --- /dev/null +++ b/crypto/x509/x509_cmp.c @@ -0,0 +1,247 @@ +/* crypto/x509/x509_cmp.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 <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "x509.h" + +int X509_issuer_and_serial_cmp(a,b) +X509 *a; +X509 *b; + { + int i; + X509_CINF *ai,*bi; + + ai=a->cert_info; + bi=b->cert_info; + i=ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber); + if (i) return(i); + return(X509_NAME_cmp(ai->issuer,bi->issuer)); + } + +#ifndef NO_MD5 +unsigned long X509_issuer_and_serial_hash(a) +X509 *a; + { + unsigned long ret=0; + MD5_CTX ctx; + unsigned char md[16]; + char str[256]; + + X509_NAME_oneline(a->cert_info->issuer,str,256); + ret=strlen(str); + MD5_Init(&ctx); + MD5_Update(&ctx,(unsigned char *)str,ret); + MD5_Update(&ctx,(unsigned char *)a->cert_info->serialNumber->data, + (unsigned long)a->cert_info->serialNumber->length); + MD5_Final(&(md[0]),&ctx); + ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| + ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) + )&0xffffffffL; + return(ret); + } +#endif + +int X509_issuer_name_cmp(a, b) +X509 *a; +X509 *b; + { + return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); + } + +int X509_subject_name_cmp(a, b) +X509 *a; +X509 *b; + { + return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); + } + +int X509_CRL_cmp(a, b) +X509_CRL *a; +X509_CRL *b; + { + return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); + } + +X509_NAME *X509_get_issuer_name(a) +X509 *a; + { + return(a->cert_info->issuer); + } + +unsigned long X509_issuer_name_hash(x) +X509 *x; + { + return(X509_NAME_hash(x->cert_info->issuer)); + } + +X509_NAME *X509_get_subject_name(a) +X509 *a; + { + return(a->cert_info->subject); + } + +ASN1_INTEGER *X509_get_serialNumber(a) +X509 *a; + { + return(a->cert_info->serialNumber); + } + +unsigned long X509_subject_name_hash(x) +X509 *x; + { + return(X509_NAME_hash(x->cert_info->subject)); + } + +int X509_NAME_cmp(a, b) +X509_NAME *a; +X509_NAME *b; + { + int i,j; + X509_NAME_ENTRY *na,*nb; + + if (sk_num(a->entries) != sk_num(b->entries)) + return(sk_num(a->entries)-sk_num(b->entries)); + for (i=sk_num(a->entries)-1; i>=0; i--) + { + na=(X509_NAME_ENTRY *)sk_value(a->entries,i); + nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); + j=na->value->length-nb->value->length; + if (j) return(j); + j=memcmp(na->value->data,nb->value->data, + na->value->length); + if (j) return(j); + j=na->set-nb->set; + if (j) return(j); + } + + /* We will check the object types after checking the values + * since the values will more often be different than the object + * types. */ + for (i=sk_num(a->entries)-1; i>=0; i--) + { + na=(X509_NAME_ENTRY *)sk_value(a->entries,i); + nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); + j=OBJ_cmp(na->object,nb->object); + if (j) return(j); + } + return(0); + } + +#ifndef NO_MD5 +/* I should do a DER encoding of the name and then hash it. */ +unsigned long X509_NAME_hash(x) +X509_NAME *x; + { + unsigned long ret=0; + unsigned char md[16]; + char str[256]; + + X509_NAME_oneline(x,str,256); + ret=strlen(str); + MD5((unsigned char *)str,ret,&(md[0])); + ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| + ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) + )&0xffffffffL; + return(ret); + } +#endif + +/* Search a stack of X509 for a match */ +X509 *X509_find_by_issuer_and_serial(sk,name,serial) +STACK *sk; +X509_NAME *name; +ASN1_INTEGER *serial; + { + int i; + X509_CINF cinf; + X509 x,*x509=NULL; + + x.cert_info= &cinf; + cinf.serialNumber=serial; + cinf.issuer=name; + + for (i=0; i<sk_num(sk); i++) + { + x509=(X509 *)sk_value(sk,i); + if (X509_issuer_and_serial_cmp(x509,&x) == 0) + return(x509); + } + return(x509); + } + +X509 *X509_find_by_subject(sk,name) +STACK *sk; +X509_NAME *name; + { + X509 *x509; + int i; + + for (i=0; i<sk_num(sk); i++) + { + x509=(X509 *)sk_value(sk,i); + if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) + return(x509); + } + return(NULL); + } + diff --git a/crypto/x509/x509_d2.c b/crypto/x509/x509_d2.c new file mode 100644 index 0000000000..235d70f8d7 --- /dev/null +++ b/crypto/x509/x509_d2.c @@ -0,0 +1,106 @@ +/* crypto/x509/x509_d2.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 <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "cryptlib.h" +#include "crypto.h" +#include "x509.h" + +int X509_STORE_set_default_paths(ctx) +X509_STORE *ctx; + { + X509_LOOKUP *lookup; + + lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_file()); + if (lookup == NULL) return(0); + X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT); + + lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_hash_dir()); + if (lookup == NULL) return(0); + X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); + + /* clear any errors */ + ERR_clear_error(); + + return(1); + } + +int X509_STORE_load_locations(ctx,file,path) +X509_STORE *ctx; +char *file; +char *path; + { + X509_LOOKUP *lookup; + + if (file != NULL) + { + lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_file()); + if (lookup == NULL) return(0); + X509_LOOKUP_load_file(lookup,file,X509_FILETYPE_PEM); + } + if (path != NULL) + { + lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_hash_dir()); + if (lookup == NULL) return(0); + X509_LOOKUP_add_dir(lookup,path,X509_FILETYPE_PEM); + } + return(1); + } + diff --git a/crypto/x509/x509_def.c b/crypto/x509/x509_def.c new file mode 100644 index 0000000000..497923828c --- /dev/null +++ b/crypto/x509/x509_def.c @@ -0,0 +1,83 @@ +/* crypto/x509/x509_def.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 <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "cryptlib.h" +#include "crypto.h" +#include "x509.h" + +char *X509_get_default_private_dir() + { return(X509_PRIVATE_DIR); } + +char *X509_get_default_cert_area() + { return(X509_CERT_AREA); } + +char *X509_get_default_cert_dir() + { return(X509_CERT_DIR); } + +char *X509_get_default_cert_file() + { return(X509_CERT_FILE); } + +char *X509_get_default_cert_dir_env() + { return(X509_CERT_DIR_EVP); } + +char *X509_get_default_cert_file_env() + { return(X509_CERT_FILE_EVP); } + diff --git a/crypto/x509/x509_err.c b/crypto/x509/x509_err.c new file mode 100644 index 0000000000..bfcc47fe03 --- /dev/null +++ b/crypto/x509/x509_err.c @@ -0,0 +1,121 @@ +/* lib/x509/x509_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 <stdio.h> +#include "err.h" +#include "x509.h" + +/* BEGIN ERROR CODES */ +static ERR_STRING_DATA X509_str_functs[]= + { +{ERR_PACK(0,X509_F_ADD_CERT_DIR,0), "ADD_CERT_DIR"}, +{ERR_PACK(0,X509_F_BY_FILE_CTRL,0), "BY_FILE_CTRL"}, +{ERR_PACK(0,X509_F_DIR_CTRL,0), "DIR_CTRL"}, +{ERR_PACK(0,X509_F_GET_CERT_BY_SUBJECT,0), "GET_CERT_BY_SUBJECT"}, +{ERR_PACK(0,X509_F_X509V3_ADD_EXT,0), "X509v3_add_ext"}, +{ERR_PACK(0,X509_F_X509V3_ADD_EXTENSION,0), "X509V3_ADD_EXTENSION"}, +{ERR_PACK(0,X509_F_X509V3_PACK_STRING,0), "X509v3_pack_string"}, +{ERR_PACK(0,X509_F_X509V3_UNPACK_STRING,0), "X509v3_unpack_string"}, +{ERR_PACK(0,X509_F_X509_EXTENSION_CREATE_BY_NID,0), "X509_EXTENSION_create_by_NID"}, +{ERR_PACK(0,X509_F_X509_EXTENSION_CREATE_BY_OBJ,0), "X509_EXTENSION_create_by_OBJ"}, +{ERR_PACK(0,X509_F_X509_GET_PUBKEY_PARAMETERS,0), "X509_get_pubkey_parameters"}, +{ERR_PACK(0,X509_F_X509_LOAD_CERT_FILE,0), "X509_LOAD_CERT_FILE"}, +{ERR_PACK(0,X509_F_X509_NAME_ADD_ENTRY,0), "X509_NAME_add_entry"}, +{ERR_PACK(0,X509_F_X509_NAME_ENTRY_CREATE_BY_NID,0), "X509_NAME_ENTRY_create_by_NID"}, +{ERR_PACK(0,X509_F_X509_NAME_ENTRY_SET_OBJECT,0), "X509_NAME_ENTRY_set_object"}, +{ERR_PACK(0,X509_F_X509_NAME_ONELINE,0), "X509_NAME_oneline"}, +{ERR_PACK(0,X509_F_X509_NAME_PRINT,0), "X509_NAME_print"}, +{ERR_PACK(0,X509_F_X509_PRINT_FP,0), "X509_print_fp"}, +{ERR_PACK(0,X509_F_X509_PUBKEY_GET,0), "X509_PUBKEY_get"}, +{ERR_PACK(0,X509_F_X509_PUBKEY_SET,0), "X509_PUBKEY_set"}, +{ERR_PACK(0,X509_F_X509_REQ_PRINT,0), "X509_REQ_print"}, +{ERR_PACK(0,X509_F_X509_REQ_PRINT_FP,0), "X509_REQ_print_fp"}, +{ERR_PACK(0,X509_F_X509_REQ_TO_X509,0), "X509_REQ_to_X509"}, +{ERR_PACK(0,X509_F_X509_STORE_ADD_CERT,0), "X509_STORE_ADD_CERT"}, +{ERR_PACK(0,X509_F_X509_TO_X509_REQ,0), "X509_to_X509_REQ"}, +{ERR_PACK(0,X509_F_X509_VERIFY_CERT,0), "X509_verify_cert"}, +{0,NULL}, + }; + +static ERR_STRING_DATA X509_str_reasons[]= + { +{X509_R_BAD_X509_FILETYPE ,"bad x509 filetype"}, +{X509_R_CERT_ALREADY_IN_HASH_TABLE ,"cert already in hash table"}, +{X509_R_ERR_ASN1_LIB ,"err asn1 lib"}, +{X509_R_LOADING_CERT_DIR ,"loading cert dir"}, +{X509_R_LOADING_DEFAULTS ,"loading defaults"}, +{X509_R_NO_CERT_SET_FOR_US_TO_VERIFY ,"no cert set for us to verify"}, +{X509_R_SHOULD_RETRY ,"should retry"}, +{X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN,"unable to find parameters in chain"}, +{X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY ,"unable to get certs public key"}, +{X509_R_UNKNOWN_NID ,"unknown nid"}, +{X509_R_UNKNOWN_STRING_TYPE ,"unknown string type"}, +{X509_R_UNSUPPORTED_ALGORITHM ,"unsupported algorithm"}, +{0,NULL}, + }; + +void ERR_load_X509_strings() + { + static int init=1; + + if (init) + { + init=0; + ERR_load_strings(ERR_LIB_X509,X509_str_functs); + ERR_load_strings(ERR_LIB_X509,X509_str_reasons); + } + } diff --git a/crypto/x509/x509_ext.c b/crypto/x509/x509_ext.c new file mode 100644 index 0000000000..7495183e88 --- /dev/null +++ b/crypto/x509/x509_ext.c @@ -0,0 +1,222 @@ +/* crypto/x509/x509_ext.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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +int X509_CRL_get_ext_count(x) +X509_CRL *x; + { + return(X509v3_get_ext_count(x->crl->extensions)); + } + +int X509_CRL_get_ext_by_NID(x,nid,oldpos) +X509_CRL *x; +int nid; +int oldpos; + { + return(X509v3_get_ext_by_NID(x->crl->extensions,nid,oldpos)); + } + +int X509_CRL_get_ext_by_OBJ(x,obj,oldpos) +X509_CRL *x; +ASN1_OBJECT *obj; +int oldpos; + { + return(X509v3_get_ext_by_OBJ(x->crl->extensions,obj,oldpos)); + } + +int X509_CRL_get_ext_by_critical(x,crit,oldpos) +X509_CRL *x; +int crit; +int oldpos; + { + return(X509v3_get_ext_by_critical(x->crl->extensions,crit,oldpos)); + } + +X509_EXTENSION *X509_CRL_get_ext(x,loc) +X509_CRL *x; +int loc; + { + return(X509v3_get_ext(x->crl->extensions,loc)); + } + +X509_EXTENSION *X509_CRL_delete_ext(x,loc) +X509_CRL *x; +int loc; + { + return(X509v3_delete_ext(x->crl->extensions,loc)); + } + +int X509_CRL_add_ext(x,ex,loc) +X509_CRL *x; +X509_EXTENSION *ex; +int loc; + { + return(X509v3_add_ext(&(x->crl->extensions),ex,loc) != NULL); + } + +int X509_get_ext_count(x) +X509 *x; + { + return(X509v3_get_ext_count(x->cert_info->extensions)); + } + +int X509_get_ext_by_NID(x,nid,oldpos) +X509 *x; +int nid; +int oldpos; + { + return(X509v3_get_ext_by_NID(x->cert_info->extensions,nid,oldpos)); + } + +int X509_get_ext_by_OBJ(x,obj,oldpos) +X509 *x; +ASN1_OBJECT *obj; +int oldpos; + { + return(X509v3_get_ext_by_OBJ(x->cert_info->extensions,obj,oldpos)); + } + +int X509_get_ext_by_critical(x,crit,oldpos) +X509 *x; +int crit; +int oldpos; + { + return(X509v3_get_ext_by_critical(x->cert_info->extensions,crit,oldpos)); + } + +X509_EXTENSION *X509_get_ext(x,loc) +X509 *x; +int loc; + { + return(X509v3_get_ext(x->cert_info->extensions,loc)); + } + +X509_EXTENSION *X509_delete_ext(x,loc) +X509 *x; +int loc; + { + return(X509v3_delete_ext(x->cert_info->extensions,loc)); + } + +int X509_add_ext(x,ex,loc) +X509 *x; +X509_EXTENSION *ex; +int loc; + { + return(X509v3_add_ext(&(x->cert_info->extensions),ex,loc) != NULL); + } + +int X509_REVOKED_get_ext_count(x) +X509_REVOKED *x; + { + return(X509v3_get_ext_count(x->extensions)); + } + +int X509_REVOKED_get_ext_by_NID(x,nid,oldpos) +X509_REVOKED *x; +int nid; +int oldpos; + { + return(X509v3_get_ext_by_NID(x->extensions,nid,oldpos)); + } + +int X509_REVOKED_get_ext_by_OBJ(x,obj,oldpos) +X509_REVOKED *x; +ASN1_OBJECT *obj; +int oldpos; + { + return(X509v3_get_ext_by_OBJ(x->extensions,obj,oldpos)); + } + +int X509_REVOKED_get_ext_by_critical(x,crit,oldpos) +X509_REVOKED *x; +int crit; +int oldpos; + { + return(X509v3_get_ext_by_critical(x->extensions,crit,oldpos)); + } + +X509_EXTENSION *X509_REVOKED_get_ext(x,loc) +X509_REVOKED *x; +int loc; + { + return(X509v3_get_ext(x->extensions,loc)); + } + +X509_EXTENSION *X509_REVOKED_delete_ext(x,loc) +X509_REVOKED *x; +int loc; + { + return(X509v3_delete_ext(x->extensions,loc)); + } + +int X509_REVOKED_add_ext(x,ex,loc) +X509_REVOKED *x; +X509_EXTENSION *ex; +int loc; + { + return(X509v3_add_ext(&(x->extensions),ex,loc) != NULL); + } + diff --git a/crypto/x509/x509_lu.c b/crypto/x509/x509_lu.c new file mode 100644 index 0000000000..455ad5af45 --- /dev/null +++ b/crypto/x509/x509_lu.c @@ -0,0 +1,439 @@ +/* crypto/x509/x509_lu.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 <stdio.h> +#include "cryptlib.h" +#include "lhash.h" +#include "x509.h" + +X509_LOOKUP *X509_LOOKUP_new(method) +X509_LOOKUP_METHOD *method; + { + X509_LOOKUP *ret; + + ret=(X509_LOOKUP *)Malloc(sizeof(X509_LOOKUP)); + if (ret == NULL) return(NULL); + + ret->init=0; + ret->skip=0; + ret->method=method; + ret->method_data=NULL; + ret->store_ctx=NULL; + if ((method->new_item != NULL) && !method->new_item(ret)) + { + Free(ret); + return(NULL); + } + return(ret); + } + +void X509_LOOKUP_free(ctx) +X509_LOOKUP *ctx; + { + if (ctx == NULL) return; + if ( (ctx->method != NULL) && + (ctx->method->free != NULL)) + ctx->method->free(ctx); + Free(ctx); + } + +int X509_LOOKUP_init(ctx) +X509_LOOKUP *ctx; + { + if (ctx->method == NULL) return(0); + if (ctx->method->init != NULL) + return(ctx->method->init(ctx)); + else + return(1); + } + +int X509_LOOKUP_shutdown(ctx) +X509_LOOKUP *ctx; + { + if (ctx->method == NULL) return(0); + if (ctx->method->init != NULL) + return(ctx->method->shutdown(ctx)); + else + return(1); + } + +int X509_LOOKUP_ctrl(ctx,cmd,argc,argl,ret) +X509_LOOKUP *ctx; +int cmd; +char *argc; +long argl; +char **ret; + { + if (ctx->method == NULL) return(-1); + if (ctx->method->ctrl != NULL) + return(ctx->method->ctrl(ctx,cmd,argc,argl,ret)); + else + return(1); + } + +int X509_LOOKUP_by_subject(ctx,type,name,ret) +X509_LOOKUP *ctx; +int type; +X509_NAME *name; +X509_OBJECT *ret; + { + if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL)) + return(X509_LU_FAIL); + if (ctx->skip) return(0); + return(ctx->method->get_by_subject(ctx,type,name,ret)); + } + +int X509_LOOKUP_by_issuer_serial(ctx,type,name,serial,ret) +X509_LOOKUP *ctx; +int type; +X509_NAME *name; +ASN1_INTEGER *serial; +X509_OBJECT *ret; + { + if ((ctx->method == NULL) || + (ctx->method->get_by_issuer_serial == NULL)) + return(X509_LU_FAIL); + return(ctx->method->get_by_issuer_serial(ctx,type,name,serial,ret)); + } + +int X509_LOOKUP_by_fingerprint(ctx,type,bytes,len,ret) +X509_LOOKUP *ctx; +int type; +unsigned char *bytes; +int len; +X509_OBJECT *ret; + { + if ((ctx->method == NULL) || (ctx->method->get_by_fingerprint == NULL)) + return(X509_LU_FAIL); + return(ctx->method->get_by_fingerprint(ctx,type,bytes,len,ret)); + } + +int X509_LOOKUP_by_alias(ctx,type,str,len,ret) +X509_LOOKUP *ctx; +int type; +char *str; +int len; +X509_OBJECT *ret; + { + if ((ctx->method == NULL) || (ctx->method->get_by_alias)) + return(X509_LU_FAIL); + return(ctx->method->get_by_alias(ctx,str,len,ret)); + } + +static unsigned long x509_object_hash(a) +X509_OBJECT *a; + { + unsigned long h; + + switch (a->type) + { + case X509_LU_X509: + h=X509_NAME_hash(a->data.x509->cert_info->subject); + break; + case X509_LU_CRL: + h=X509_NAME_hash(a->data.crl->crl->issuer); + break; + default: + abort(); + } + return(h); + } + +static int x509_object_cmp(a,b) +X509_OBJECT *a,*b; + { + int ret; + + ret=(a->type - b->type); + if (ret) return(ret); + switch (a->type) + { + case X509_LU_X509: + ret=X509_subject_name_cmp(a->data.x509,b->data.x509); + break; + case X509_LU_CRL: + ret=X509_CRL_cmp(a->data.crl,b->data.crl); + break; + default: + abort(); + } + return(ret); + } + +X509_STORE *X509_STORE_new() + { + X509_STORE *ret; + + if ((ret=(X509_STORE *)Malloc(sizeof(X509_STORE))) == NULL) + return(NULL); + ret->certs=lh_new(x509_object_hash,x509_object_cmp); + ret->cache=1; + ret->get_cert_methods=sk_new_null(); + ret->verify=NULL; + ret->verify_cb=NULL; + ret->app_data=NULL; + ret->references=1; + return(ret); + } + +static void cleanup(a) +X509_OBJECT *a; + { + if (a->type == X509_LU_X509) + { + X509_free(a->data.x509); + } + else if (a->type == X509_LU_CRL) + { + X509_CRL_free(a->data.crl); + } + else + abort(); + + Free(a); + } + +void X509_STORE_free(vfy) +X509_STORE *vfy; + { + int i; + STACK *sk; + X509_LOOKUP *lu; + + sk=vfy->get_cert_methods; + for (i=0; i<sk_num(sk); i++) + { + lu=(X509_LOOKUP *)sk_value(sk,i); + X509_LOOKUP_shutdown(lu); + X509_LOOKUP_free(lu); + } + sk_free(sk); + + lh_doall(vfy->certs,cleanup); + lh_free(vfy->certs); + Free(vfy); + } + +X509_LOOKUP *X509_STORE_add_lookup(v,m) +X509_STORE *v; +X509_LOOKUP_METHOD *m; + { + int i; + STACK *sk; + X509_LOOKUP *lu; + + sk=v->get_cert_methods; + for (i=0; i<sk_num(sk); i++) + { + lu=(X509_LOOKUP *)sk_value(sk,i); + if (m == lu->method) + { + return(lu); + } + } + /* a new one */ + lu=X509_LOOKUP_new(m); + if (lu == NULL) + return(NULL); + else + { + lu->store_ctx=v; + if (sk_push(v->get_cert_methods,(char *)lu)) + return(lu); + else + { + X509_LOOKUP_free(lu); + return(NULL); + } + } + } + +int X509_STORE_get_by_subject(vs,type,name,ret) +X509_STORE_CTX *vs; +int type; +X509_NAME *name; +X509_OBJECT *ret; + { + X509_STORE *ctx=vs->ctx; + X509_LOOKUP *lu; + X509_OBJECT stmp,*tmp; + int i,j; + + tmp=X509_OBJECT_retrive_by_subject(ctx->certs,type,name); + + if (tmp == NULL) + { + for (i=vs->current_method; i<sk_num(ctx->get_cert_methods); i++) + { + lu=(X509_LOOKUP *)sk_value(ctx->get_cert_methods,i); + j=X509_LOOKUP_by_subject(lu,type,name,&stmp); + if (j < 0) + { + vs->current_method=j; + return(j); + } + else if (j) + { + tmp= &stmp; + break; + } + } + vs->current_method=0; + if (tmp == NULL) + return(0); + } + +/* if (ret->data.ptr != NULL) + X509_OBJECT_free_contents(ret); */ + + ret->type=tmp->type; + ret->data.ptr=tmp->data.ptr; + + X509_OBJECT_up_ref_count(ret); + + return(1); + } + +void X509_OBJECT_up_ref_count(a) +X509_OBJECT *a; + { + switch (a->type) + { + case X509_LU_X509: + CRYPTO_add(&a->data.x509->references,1,CRYPTO_LOCK_X509); + break; + case X509_LU_CRL: + CRYPTO_add(&a->data.crl->references,1,CRYPTO_LOCK_X509_CRL); + break; + } + } + +void X509_OBJECT_free_contents(a) +X509_OBJECT *a; + { + switch (a->type) + { + case X509_LU_X509: + X509_free(a->data.x509); + break; + case X509_LU_CRL: + X509_CRL_free(a->data.crl); + break; + } + } + +X509_OBJECT *X509_OBJECT_retrive_by_subject(h,type,name) +LHASH *h; +int type; +X509_NAME *name; + { + X509_OBJECT stmp,*tmp; + X509 x509_s; + X509_CINF cinf_s; + X509_CRL crl_s; + X509_CRL_INFO crl_info_s; + + stmp.type=type; + switch (type) + { + case X509_LU_X509: + stmp.data.x509= &x509_s; + x509_s.cert_info= &cinf_s; + cinf_s.subject=name; + break; + case X509_LU_CRL: + stmp.data.crl= &crl_s; + crl_s.crl= &crl_info_s; + crl_info_s.issuer=name; + break; + default: + abort(); + } + + tmp=(X509_OBJECT *)lh_retrieve(h,(char *)&stmp); + return(tmp); + } + +void X509_STORE_CTX_init(ctx,store,x509,chain) +X509_STORE_CTX *ctx; +X509_STORE *store; +X509 *x509; +STACK *chain; + { + ctx->ctx=store; + ctx->current_method=0; + ctx->cert=x509; + ctx->untrusted=chain; + ctx->last_untrusted=0; + ctx->valid=0; + ctx->chain=NULL; + ctx->depth=10; + ctx->error=0; + ctx->current_cert=NULL; + } + +void X509_STORE_CTX_cleanup(ctx) +X509_STORE_CTX *ctx; + { + if (ctx->chain != NULL) + { + sk_pop_free(ctx->chain,X509_free); + ctx->chain=NULL; + } + } + diff --git a/crypto/x509/x509_obj.c b/crypto/x509/x509_obj.c new file mode 100644 index 0000000000..2b53af1e61 --- /dev/null +++ b/crypto/x509/x509_obj.c @@ -0,0 +1,180 @@ +/* crypto/x509/x509_obj.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 <stdio.h> +#include "cryptlib.h" +#include "lhash.h" +#include "objects.h" +#include "x509.h" +#include "buffer.h" + +char *X509_NAME_oneline(a,buf,len) +X509_NAME *a; +char *buf; +int len; + { + X509_NAME_ENTRY *ne; + unsigned int i; + int n,lold,l,l1,l2,num,j,type; + char *s,*p; + unsigned char *q; + BUF_MEM *b=NULL; + static char hex[17]="0123456789ABCDEF"; + int gs_doit[4]; + + if (a == NULL) return("NO X509_NAME"); + if (buf == NULL) + { + if ((b=BUF_MEM_new()) == NULL) goto err; + if (!BUF_MEM_grow(b,200)) goto err; + b->data[0]='\0'; + len=200; + } + + len--; /* space for '\0' */ + l=0; + for (i=0; (int)i<sk_num(a->entries); i++) + { + ne=(X509_NAME_ENTRY *)sk_value(a->entries,i); + n=OBJ_obj2nid(ne->object); + if (n == NID_undef) + s="UNKNOWN"; + else + { + s=OBJ_nid2sn(n); + if (s == NULL) s="UNKNOWN2"; + } + l1=strlen(s); + + type=ne->value->type; + num=ne->value->length; + q=ne->value->data; + + if ((type == V_ASN1_GENERALSTRING) && ((num%4) == 0)) + { + gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=0; + for (j=0; j<num; j++) + if (q[j] != 0) gs_doit[j&3]=1; + + if (gs_doit[0]|gs_doit[1]|gs_doit[2]) + gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1; + else + { + gs_doit[0]=gs_doit[1]=gs_doit[2]=0; + gs_doit[3]=1; + } + } + else + gs_doit[0]=gs_doit[1]=gs_doit[2]=gs_doit[3]=1; + + for (l2=j=0; j<num; j++) + { + if (!gs_doit[j&3]) continue; + l2++; + if ((q[j] < ' ') || (q[j] > '~')) l2+=3; + } + + lold=l; + l+=1+l1+1+l2; + if (b != NULL) + { + if (!BUF_MEM_grow(b,l+1)) goto err; + p= &(b->data[lold]); + } + else if (l > len) + { + break; + } + else + p= &(buf[lold]); + *(p++)='/'; + memcpy(p,s,(unsigned int)l1); p+=l1; + *(p++)='='; + + q=ne->value->data; + + for (j=0; j<num; j++) + { + if (!gs_doit[j&3]) continue; + n=q[j]; + if ((n < ' ') || (n > '~')) + { + *(p++)='\\'; + *(p++)='x'; + *(p++)=hex[(n>>4)&0x0f]; + *(p++)=hex[n&0x0f]; + } + else + *(p++)=n; + } + *p='\0'; + } + if (b != NULL) + { + p=b->data; + Free((char *)b); + } + else + p=buf; + return(p); +err: + X509err(X509_F_X509_NAME_ONELINE,ERR_R_MALLOC_FAILURE); + if (b != NULL) BUF_MEM_free(b); + return(NULL); + } + diff --git a/crypto/x509/x509_r2x.c b/crypto/x509/x509_r2x.c new file mode 100644 index 0000000000..7e79959c8a --- /dev/null +++ b/crypto/x509/x509_r2x.c @@ -0,0 +1,122 @@ +/* crypto/x509/x509_r2x.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 <stdio.h> +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "asn1.h" +#include "x509.h" +#include "objects.h" +#include "buffer.h" +#include "pem.h" + +X509 *X509_REQ_to_X509(r,days,pkey) +X509_REQ *r; +int days; +EVP_PKEY *pkey; + { + X509 *ret=NULL; + int er=1; + X509_REQ_INFO *ri=NULL; + X509_CINF *xi=NULL; + X509_NAME *xn; + + if ((ret=X509_new()) == NULL) + { + X509err(X509_F_X509_REQ_TO_X509,ERR_R_MALLOC_FAILURE); + goto err; + } + + /* duplicate the request */ + ri=(X509_REQ_INFO *)ASN1_dup(i2d_X509_REQ_INFO, + (char *(*)())d2i_X509_REQ_INFO,(char *)r->req_info); + if (ri == NULL) goto err; + + xi=ret->cert_info; + + if (sk_num(ri->attributes) != 0) + { + if ((xi->version=ASN1_INTEGER_new()) == NULL) goto err; + if (!ASN1_INTEGER_set(xi->version,2)) goto err; +/* xi->extensions=ri->attributes; <- bad, should not ever be done + ri->attributes=NULL; */ + } + + xn=X509_REQ_get_subject_name(r); + X509_set_subject_name(ret,X509_NAME_dup(xn)); + X509_set_issuer_name(ret,X509_NAME_dup(xn)); + + X509_gmtime_adj(xi->validity->notBefore,0); + X509_gmtime_adj(xi->validity->notAfter,(long)60*60*24*days); + + X509_set_pubkey(ret,X509_REQ_get_pubkey(r)); + + if (!X509_sign(ret,pkey,EVP_md5())) + goto err; + er=0; +err: + if (er) + { + X509_free(ret); + X509_REQ_INFO_free(ri); + return(NULL); + } + return(ret); + } + diff --git a/crypto/x509/x509_req.c b/crypto/x509/x509_req.c new file mode 100644 index 0000000000..c1f10c2428 --- /dev/null +++ b/crypto/x509/x509_req.c @@ -0,0 +1,111 @@ +/* crypto/x509/x509_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 <stdio.h> +#include "cryptlib.h" +#include "bn.h" +#include "evp.h" +#include "asn1.h" +#include "x509.h" +#include "objects.h" +#include "buffer.h" +#include "pem.h" + +X509_REQ *X509_to_X509_REQ(x,pkey) +X509 *x; +EVP_PKEY *pkey; + { + X509_REQ *ret; + X509_REQ_INFO *ri; + int i; + + ret=X509_REQ_new(); + if (ret == NULL) + { + X509err(X509_F_X509_TO_X509_REQ,ERR_R_MALLOC_FAILURE); + goto err; + } + + ri=ret->req_info; + + ri->version->length=1; + ri->version->data=(unsigned char *)Malloc(1); + if (ri->version->data == NULL) goto err; + ri->version->data[0]=0; /* version == 0 */ + + if (!X509_REQ_set_subject_name(ret,X509_get_subject_name(x))) + goto err; + + i=X509_REQ_set_pubkey(ret,X509_get_pubkey(x)); + if (!i) goto err; + +/* NEEDS FIXING EAY EAY EAY */ + if (!X509_REQ_sign(ret,pkey,EVP_md5())) + goto err; + return(ret); +err: + X509_REQ_free(ret); + return(NULL); + } + +EVP_PKEY *X509_REQ_get_pubkey(req) +X509_REQ *req; + { + return(X509_PUBKEY_get(req->req_info->pubkey)); + } + diff --git a/crypto/x509/x509_set.c b/crypto/x509/x509_set.c new file mode 100644 index 0000000000..f65ae91f53 --- /dev/null +++ b/crypto/x509/x509_set.c @@ -0,0 +1,164 @@ +/* crypto/x509/x509_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 <stdio.h> +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +int X509_set_version(x,version) +X509 *x; +long version; + { + if (x == NULL) return(0); + if (x->cert_info->version == NULL) + { + if ((x->cert_info->version=ASN1_INTEGER_new()) == NULL) + return(0); + } + return(ASN1_INTEGER_set(x->cert_info->version,version)); + } + +int X509_set_serialNumber(x,serial) +X509 *x; +ASN1_INTEGER *serial; + { + ASN1_INTEGER *in; + + if (x == NULL) return(0); + in=x->cert_info->serialNumber; + if (in != serial) + { + in=ASN1_INTEGER_dup(serial); + if (in != NULL) + { + ASN1_INTEGER_free(x->cert_info->serialNumber); + x->cert_info->serialNumber=in; + } + } + return(in != NULL); + } + +int X509_set_issuer_name(x,name) +X509 *x; +X509_NAME *name; + { + if ((x == NULL) || (x->cert_info == NULL)) return(0); + return(X509_NAME_set(&x->cert_info->issuer,name)); + } + +int X509_set_subject_name(x,name) +X509 *x; +X509_NAME *name; + { + if ((x == NULL) || (x->cert_info == NULL)) return(0); + return(X509_NAME_set(&x->cert_info->subject,name)); + } + +int X509_set_notBefore(x,tm) +X509 *x; +ASN1_UTCTIME *tm; + { + ASN1_UTCTIME *in; + + if ((x == NULL) || (x->cert_info->validity == NULL)) return(0); + in=x->cert_info->validity->notBefore; + if (in != tm) + { + in=ASN1_UTCTIME_dup(tm); + if (in != NULL) + { + ASN1_UTCTIME_free(x->cert_info->validity->notBefore); + x->cert_info->validity->notBefore=in; + } + } + return(in != NULL); + } + +int X509_set_notAfter(x,tm) +X509 *x; +ASN1_UTCTIME *tm; + { + ASN1_UTCTIME *in; + + if ((x == NULL) || (x->cert_info->validity == NULL)) return(0); + in=x->cert_info->validity->notAfter; + if (in != tm) + { + in=ASN1_UTCTIME_dup(tm); + if (in != NULL) + { + ASN1_UTCTIME_free(x->cert_info->validity->notAfter); + x->cert_info->validity->notAfter=in; + } + } + return(in != NULL); + } + +int X509_set_pubkey(x,pkey) +X509 *x; +EVP_PKEY *pkey; + { + if ((x == NULL) || (x->cert_info == NULL)) return(0); + return(X509_PUBKEY_set(&(x->cert_info->key),pkey)); + } + + + diff --git a/crypto/x509/x509_txt.c b/crypto/x509/x509_txt.c new file mode 100644 index 0000000000..d5fc5839a1 --- /dev/null +++ b/crypto/x509/x509_txt.c @@ -0,0 +1,130 @@ +/* crypto/x509/x509_txt.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 <stdio.h> +#include <time.h> +#include <errno.h> +#include <sys/types.h> + +#include "cryptlib.h" +#include "lhash.h" +#include "buffer.h" +#include "evp.h" +#include "asn1.h" +#include "x509.h" +#include "objects.h" +#include "pem.h" + +char *X509_verify_cert_error_string(n) +int n; + { + static char buf[100]; + + switch (n) + { + case X509_V_OK: + return("ok"); + case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: + return("unable to get issuer certificate"); + case X509_V_ERR_UNABLE_TO_GET_CRL: + return("unable to get certificate CRL"); + case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: + return("unable to decrypt certificate's signature"); + case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: + return("unable to decrypt CRL's's signature"); + case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: + return("unable to decode issuer public key"); + case X509_V_ERR_CERT_SIGNATURE_FAILURE: + return("certificate signature failure"); + case X509_V_ERR_CRL_SIGNATURE_FAILURE: + return("CRL signature failure"); + case X509_V_ERR_CERT_NOT_YET_VALID: + return("certificate is not yet valid"); + case X509_V_ERR_CRL_NOT_YET_VALID: + return("CRL is not yet valid"); + case X509_V_ERR_CERT_HAS_EXPIRED: + return("Certificate has expired"); + case X509_V_ERR_CRL_HAS_EXPIRED: + return("CRL has expired"); + case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: + return("format error in certificate's notBefore field"); + case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: + return("format error in certificate's notAfter field"); + case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FILED: + return("format error in CRL's lastUpdate field"); + case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FILED: + return("format error in CRL's nextUpdate field"); + case X509_V_ERR_OUT_OF_MEM: + return("out of memory"); + case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: + return("self signed certificate"); + case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: + return("self signed certificate in certificate chain"); + case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: + return("unable to get local issuer certificate"); + case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: + return("unable to verify the first certificate"); + case X509_V_ERR_CERT_CHAIN_TOO_LONG: + return("certificate chain too long"); + default: + sprintf(buf,"error number %d",n); + return(buf); + } + } + + diff --git a/crypto/x509/x509_v3.c b/crypto/x509/x509_v3.c new file mode 100644 index 0000000000..388130b1d7 --- /dev/null +++ b/crypto/x509/x509_v3.c @@ -0,0 +1,409 @@ +/* crypto/x509/x509_v3.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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +#ifndef NOPROTO +static X509_EXTENSION_METHOD *find_by_nid(int nid); +static int xem_cmp(X509_EXTENSION_METHOD **a, X509_EXTENSION_METHOD **b); +#else +static X509_EXTENSION_METHOD *find_by_nid(); +static int xem_cmp(); +#endif + +static STACK *extensions=NULL; + +int X509v3_get_ext_count(x) +STACK *x; + { + if (x == NULL) return(0); + return(sk_num(x)); + } + +int X509v3_get_ext_by_NID(x,nid,oldpos) +STACK *x; +int nid; +int oldpos; + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); + if (obj == NULL) return(-2); + return(X509v3_get_ext_by_OBJ(x,obj,oldpos)); + } + +int X509v3_get_ext_by_OBJ(sk,obj,oldpos) +STACK *sk; +ASN1_OBJECT *obj; +int oldpos; + { + int n; + X509_EXTENSION *ex; + + if (sk == NULL) return(-1); + oldpos++; + if (oldpos < 0) + oldpos=0; + n=sk_num(sk); + for ( ; oldpos < n; oldpos++) + { + ex=(X509_EXTENSION *)sk_value(sk,oldpos); + if (OBJ_cmp(ex->object,obj) == 0) + return(oldpos); + } + return(-1); + } + +int X509v3_get_ext_by_critical(sk,crit,oldpos) +STACK *sk; +int crit; +int oldpos; + { + int n; + X509_EXTENSION *ex; + + if (sk == NULL) return(-1); + oldpos++; + if (oldpos < 0) + oldpos=0; + n=sk_num(sk); + for ( ; oldpos < n; oldpos++) + { + ex=(X509_EXTENSION *)sk_value(sk,oldpos); + if ( (ex->critical && crit) || + (!ex->critical && !crit)) + return(oldpos); + } + return(-1); + } + +X509_EXTENSION *X509v3_get_ext(x,loc) +STACK *x; +int loc; + { + if ((x == NULL) || (sk_num(x) <= loc) || (loc < 0)) + return(NULL); + else + return((X509_EXTENSION *)sk_value(x,loc)); + } + +X509_EXTENSION *X509v3_delete_ext(x,loc) +STACK *x; +int loc; + { + X509_EXTENSION *ret; + + if ((x == NULL) || (sk_num(x) <= loc) || (loc < 0)) + return(NULL); + ret=(X509_EXTENSION *)sk_delete(x,loc); + return(ret); + } + +STACK *X509v3_add_ext(x,ex,loc) +STACK **x; +X509_EXTENSION *ex; +int loc; + { + X509_EXTENSION *new_ex=NULL; + int n; + STACK *sk=NULL; + + if ((x != NULL) && (*x == NULL)) + { + if ((sk=sk_new_null()) == NULL) + goto err; + } + else + sk= *x; + + n=sk_num(sk); + if (loc > n) loc=n; + else if (loc < 0) loc=n; + + if ((new_ex=X509_EXTENSION_dup(ex)) == NULL) + goto err2; + if (!sk_insert(sk,(char *)new_ex,loc)) + goto err; + if ((x != NULL) && (*x == NULL)) + *x=sk; + return(sk); +err: + X509err(X509_F_X509V3_ADD_EXT,ERR_R_MALLOC_FAILURE); +err2: + if (new_ex != NULL) X509_EXTENSION_free(new_ex); + if (sk != NULL) sk_free(sk); + return(NULL); + } + +X509_EXTENSION *X509_EXTENSION_create_by_NID(ex,nid,crit,data) +X509_EXTENSION **ex; +int nid; +int crit; +ASN1_OCTET_STRING *data; + { + ASN1_OBJECT *obj; + X509_EXTENSION *ret; + + obj=OBJ_nid2obj(nid); + if (obj == NULL) + { + X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,X509_R_UNKNOWN_NID); + return(NULL); + } + ret=X509_EXTENSION_create_by_OBJ(ex,obj,crit,data); + if (ret == NULL) ASN1_OBJECT_free(obj); + return(ret); + } + +X509_EXTENSION *X509_EXTENSION_create_by_OBJ(ex,obj,crit,data) +X509_EXTENSION **ex; +ASN1_OBJECT *obj; +int crit; +ASN1_OCTET_STRING *data; + { + X509_EXTENSION *ret; + + if ((ex == NULL) || (*ex == NULL)) + { + if ((ret=X509_EXTENSION_new()) == NULL) + { + X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE); + return(NULL); + } + } + else + ret= *ex; + + if (!X509_EXTENSION_set_object(ret,obj)) + goto err; + if (!X509_EXTENSION_set_critical(ret,crit)) + goto err; + if (!X509_EXTENSION_set_data(ret,data)) + goto err; + + if ((ex != NULL) && (*ex == NULL)) *ex=ret; + return(ret); +err: + if ((ex == NULL) || (ret != *ex)) + X509_EXTENSION_free(ret); + return(NULL); + } + +int X509_EXTENSION_set_object(ex,obj) +X509_EXTENSION *ex; +ASN1_OBJECT *obj; + { + if ((ex == NULL) || (obj == NULL)) + return(0); + ASN1_OBJECT_free(ex->object); + ex->object=OBJ_dup(obj); + return(1); + } + +int X509_EXTENSION_set_critical(ex,crit) +X509_EXTENSION *ex; +int crit; + { + if (ex == NULL) return(0); + ex->critical=(crit)?0xFF:0; + return(1); + } + +int X509_EXTENSION_set_data(ex,data) +X509_EXTENSION *ex; +ASN1_OCTET_STRING *data; + { + int i; + + if (ex == NULL) return(0); + i=ASN1_OCTET_STRING_set(ex->value,data->data,data->length); + if (!i) return(0); + return(1); + } + +ASN1_OBJECT *X509_EXTENSION_get_object(ex) +X509_EXTENSION *ex; + { + if (ex == NULL) return(NULL); + return(ex->object); + } + +ASN1_OCTET_STRING *X509_EXTENSION_get_data(ex) +X509_EXTENSION *ex; + { + if (ex == NULL) return(NULL); + return(ex->value); + } + +int X509_EXTENSION_get_critical(ex) +X509_EXTENSION *ex; + { + if (ex == NULL) return(0); + return(ex->critical); + } + +int X509v3_data_type_by_OBJ(obj) +ASN1_OBJECT *obj; + { + int nid; + + nid=OBJ_obj2nid(obj); + if (nid == V_ASN1_UNDEF) return(V_ASN1_UNDEF); + return(X509v3_data_type_by_NID(nid)); + } + +int X509v3_data_type_by_NID(nid) +int nid; + { + X509_EXTENSION_METHOD *x; + + x=find_by_nid(nid); + if (x == NULL) + return(V_ASN1_UNDEF); + else + return(x->data_type); + } + +int X509v3_pack_type_by_OBJ(obj) +ASN1_OBJECT *obj; + { + int nid; + + nid=OBJ_obj2nid(obj); + if (nid == NID_undef) return(X509_EXT_PACK_UNKNOWN); + return(X509v3_pack_type_by_NID(nid)); + } + +int X509v3_pack_type_by_NID(nid) +int nid; + { + X509_EXTENSION_METHOD *x; + + x=find_by_nid(nid); + if (x == NULL) + return(X509_EXT_PACK_UNKNOWN); + else + return(x->pack_type); + } + +static X509_EXTENSION_METHOD *find_by_nid(nid) +int nid; + { + X509_EXTENSION_METHOD x; + int i; + + x.nid=nid; + if (extensions == NULL) return(NULL); + i=sk_find(extensions,(char *)&x); + if (i < 0) + return(NULL); + else + return((X509_EXTENSION_METHOD *)sk_value(extensions,i)); + } + +static int xem_cmp(a,b) +X509_EXTENSION_METHOD **a,**b; + { + return((*a)->nid-(*b)->nid); + } + +void X509v3_cleanup_extensions() + { + int i; + + if (extensions != NULL) + { + for (i=0; i<sk_num(extensions); i++) + Free(sk_value(extensions,i)); + sk_free(extensions); + extensions=NULL; + } + } + +int X509v3_add_extension(x) +X509_EXTENSION_METHOD *x; + { + X509_EXTENSION_METHOD *newx; + + if (extensions == NULL) + { + extensions=sk_new(xem_cmp); + if (extensions == NULL) goto err; + } + newx=(X509_EXTENSION_METHOD *)Malloc(sizeof(X509_EXTENSION_METHOD)); + if (newx == NULL) goto err; + newx->nid=x->nid; + newx->data_type=x->data_type; + newx->pack_type=x->pack_type; + if (!sk_push(extensions,(char *)newx)) + { + Free(newx); + goto err; + } + return(1); +err: + X509err(X509_F_X509V3_ADD_EXTENSION,ERR_R_MALLOC_FAILURE); + return(0); + } + diff --git a/crypto/x509/x509_vfy.c b/crypto/x509/x509_vfy.c new file mode 100644 index 0000000000..f6dba6f9ef --- /dev/null +++ b/crypto/x509/x509_vfy.c @@ -0,0 +1,585 @@ +/* crypto/x509/x509_vfy.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 <stdio.h> +#include <time.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include "cryptlib.h" +#include "lhash.h" +#include "buffer.h" +#include "evp.h" +#include "asn1.h" +#include "x509.h" +#include "objects.h" +#include "pem.h" + +#ifndef NOPROTO +static int null_callback(int ok,X509_STORE_CTX *e); +static int internal_verify(X509_STORE_CTX *ctx); +#else +static int null_callback(); +static int internal_verify(); +#endif + +char *X509_version="X509 part of SSLeay 0.8.1b 29-Jun-1998"; + +static int null_callback(ok,e) +int ok; +X509_STORE_CTX *e; + { + return(ok); + } + +#if 0 +static int x509_subject_cmp(a,b) +X509 **a,**b; + { + return(X509_subject_name_cmp(*a,*b)); + } +#endif + +int X509_verify_cert(ctx) +X509_STORE_CTX *ctx; + { + X509 *x,*xtmp,*chain_ss=NULL; + X509_NAME *xn; + X509_OBJECT obj; + int depth,i,ok=0; + int num; + int (*cb)(); + STACK *sktmp=NULL; + + if (ctx->cert == NULL) + { + X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); + return(-1); + } + + cb=ctx->ctx->verify_cb; + if (cb == NULL) cb=null_callback; + + /* first we make sure the chain we are going to build is + * present and that the first entry is in place */ + if (ctx->chain == NULL) + { + if ( ((ctx->chain=sk_new_null()) == NULL) || + (!sk_push(ctx->chain,(char *)ctx->cert))) + { + X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); + goto end; + } + CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509); + ctx->last_untrusted=1; + } + + /* We use a temporary so we can chop and hack at it */ + if ((ctx->untrusted != NULL) && (sktmp=sk_dup(ctx->untrusted)) == NULL) + { + X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); + goto end; + } + + num=sk_num(ctx->chain); + x=(X509 *)sk_value(ctx->chain,num-1); + depth=ctx->depth; + + + for (;;) + { + /* If we have enough, we break */ + if (depth <= num) break; + + /* If we are self signed, we break */ + xn=X509_get_issuer_name(x); + if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0) + break; + + /* If we were passed a cert chain, use it first */ + if (ctx->untrusted != NULL) + { + xtmp=X509_find_by_subject(sktmp,xn); + if (xtmp != NULL) + { + if (!sk_push(ctx->chain,(char *)xtmp)) + { + X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); + goto end; + } + CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509); + sk_delete_ptr(sktmp,(char *)xtmp); + ctx->last_untrusted++; + x=xtmp; + num++; + /* reparse the full chain for + * the next one */ + continue; + } + } + break; + } + + /* at this point, chain should contain a list of untrusted + * certificates. We now need to add at least one trusted one, + * if possible, otherwise we complain. */ + + i=sk_num(ctx->chain); + x=(X509 *)sk_value(ctx->chain,i-1); + if (X509_NAME_cmp(X509_get_subject_name(x),X509_get_issuer_name(x)) + == 0) + { + /* we have a self signed certificate */ + if (sk_num(ctx->chain) == 1) + { + ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; + ctx->current_cert=x; + ctx->error_depth=i-1; + ok=cb(0,ctx); + if (!ok) goto end; + } + else + { + /* worry more about this one elsewhere */ + chain_ss=(X509 *)sk_pop(ctx->chain); + ctx->last_untrusted--; + num--; + x=(X509 *)sk_value(ctx->chain,num-1); + } + } + + /* We now lookup certs from the certificate store */ + for (;;) + { + /* If we have enough, we break */ + if (depth <= num) break; + + /* If we are self signed, we break */ + xn=X509_get_issuer_name(x); + if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0) + break; + + ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); + if (ok != X509_LU_X509) + { + if (ok == X509_LU_RETRY) + { + X509_OBJECT_free_contents(&obj); + X509err(X509_F_X509_VERIFY_CERT,X509_R_SHOULD_RETRY); + return(ok); + } + else if (ok != X509_LU_FAIL) + { + X509_OBJECT_free_contents(&obj); + /* not good :-(, break anyway */ + return(ok); + } + break; + } + x=obj.data.x509; + if (!sk_push(ctx->chain,(char *)obj.data.x509)) + { + X509_OBJECT_free_contents(&obj); + X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); + return(0); + } + num++; + } + + /* we now have our chain, lets check it... */ + xn=X509_get_issuer_name(x); + if (X509_NAME_cmp(X509_get_subject_name(x),xn) != 0) + { + if ((chain_ss == NULL) || (X509_NAME_cmp(X509_get_subject_name(chain_ss),xn) != 0)) + { + if (ctx->last_untrusted >= num) + ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; + else + ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; + ctx->current_cert=x; + } + else + { + + sk_push(ctx->chain,(char *)chain_ss); + num++; + ctx->last_untrusted=num; + ctx->current_cert=chain_ss; + ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; + chain_ss=NULL; + } + + ctx->error_depth=num-1; + ok=cb(0,ctx); + if (!ok) goto end; + } + + /* We may as well copy down any DSA parameters that are required */ + X509_get_pubkey_parameters(NULL,ctx->chain); + + /* At this point, we have a chain and just need to verify it */ + if (ctx->ctx->verify != NULL) + ok=ctx->ctx->verify(ctx); + else + ok=internal_verify(ctx); +end: + if (sktmp != NULL) sk_free(sktmp); + if (chain_ss != NULL) X509_free(chain_ss); + return(ok); + } + +static int internal_verify(ctx) +X509_STORE_CTX *ctx; + { + int i,ok=0,n; + X509 *xs,*xi; + EVP_PKEY *pkey=NULL; + int (*cb)(); + + cb=ctx->ctx->verify_cb; + if (cb == NULL) cb=null_callback; + + n=sk_num(ctx->chain); + ctx->error_depth=n-1; + n--; + xi=(X509 *)sk_value(ctx->chain,n); + if (X509_NAME_cmp(X509_get_subject_name(xi), + X509_get_issuer_name(xi)) == 0) + xs=xi; + else + { + if (n <= 0) + { + ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; + ctx->current_cert=xi; + ok=cb(0,ctx); + goto end; + } + else + { + n--; + ctx->error_depth=n; + xs=(X509 *)sk_value(ctx->chain,n); + } + } + +/* ctx->error=0; not needed */ + while (n >= 0) + { + ctx->error_depth=n; + if (!xs->valid) + { + if ((pkey=X509_get_pubkey(xi)) == NULL) + { + ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; + ctx->current_cert=xi; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + if (X509_verify(xs,pkey) <= 0) + { + ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE; + ctx->current_cert=xs; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + pkey=NULL; + + i=X509_cmp_current_time(X509_get_notBefore(xs)); + if (i == 0) + { + ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; + ctx->current_cert=xs; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + if (i > 0) + { + ctx->error=X509_V_ERR_CERT_NOT_YET_VALID; + ctx->current_cert=xs; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + xs->valid=1; + } + + i=X509_cmp_current_time(X509_get_notAfter(xs)); + if (i == 0) + { + ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; + ctx->current_cert=xs; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + + if (i < 0) + { + ctx->error=X509_V_ERR_CERT_HAS_EXPIRED; + ctx->current_cert=xs; + ok=(*cb)(0,ctx); + if (!ok) goto end; + } + + /* CRL CHECK */ + + /* The last error (if any) is still in the error value */ + ctx->current_cert=xs; + ok=(*cb)(1,ctx); + if (!ok) goto end; + + n--; + if (n >= 0) + { + xi=xs; + xs=(X509 *)sk_value(ctx->chain,n); + } + } + ok=1; +end: + return(ok); + } + +int X509_cmp_current_time(ctm) +ASN1_UTCTIME *ctm; + { + char *str; + ASN1_UTCTIME atm; + time_t offset; + char buff1[24],buff2[24],*p; + int i,j; + + p=buff1; + i=ctm->length; + str=(char *)ctm->data; + if ((i < 11) || (i > 17)) return(0); + memcpy(p,str,10); + p+=10; + str+=10; + + if ((*str == 'Z') || (*str == '-') || (*str == '+')) + { *(p++)='0'; *(p++)='0'; } + else { *(p++)= *(str++); *(p++)= *(str++); } + *(p++)='Z'; + *(p++)='\0'; + + if (*str == 'Z') + offset=0; + else + { + if ((*str != '+') && (str[5] != '-')) + return(0); + offset=((str[1]-'0')*10+(str[2]-'0'))*60; + offset+=(str[3]-'0')*10+(str[4]-'0'); + if (*str == '-') + offset-=offset; + } + atm.type=V_ASN1_UTCTIME; + atm.length=sizeof(buff2); + atm.data=(unsigned char *)buff2; + + X509_gmtime_adj(&atm,offset); + + i=(buff1[0]-'0')*10+(buff1[1]-'0'); + if (i < 70) i+=100; + j=(buff2[0]-'0')*10+(buff2[1]-'0'); + if (j < 70) j+=100; + + if (i < j) return (-1); + if (i > j) return (1); + i=strcmp(buff1,buff2); + if (i == 0) /* wait a second then return younger :-) */ + return(-1); + else + return(i); + } + +ASN1_UTCTIME *X509_gmtime_adj(s, adj) +ASN1_UTCTIME *s; +long adj; + { + time_t t; + + time(&t); + t+=adj; + return(ASN1_UTCTIME_set(s,t)); + } + +int X509_get_pubkey_parameters(pkey,chain) +EVP_PKEY *pkey; +STACK *chain; + { + EVP_PKEY *ktmp=NULL,*ktmp2; + int i,j; + + if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return(1); + + for (i=0; i<sk_num(chain); i++) + { + ktmp=X509_get_pubkey((X509 *)sk_value(chain,i)); + if (ktmp == NULL) + { + X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); + return(0); + } + if (!EVP_PKEY_missing_parameters(ktmp)) + break; + else + { + ktmp=NULL; + } + } + if (ktmp == NULL) + { + X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN); + return(0); + } + + /* first, populate the other certs */ + for (j=i-1; j >= 0; j--) + { + ktmp2=X509_get_pubkey((X509 *)sk_value(chain,j)); + EVP_PKEY_copy_parameters(ktmp2,ktmp); + } + + if (pkey != NULL) + EVP_PKEY_copy_parameters(pkey,ktmp); + return(1); + } + +EVP_PKEY *X509_get_pubkey(x) +X509 *x; + { + return(X509_PUBKEY_get(x->cert_info->key)); + } + +int X509_check_private_key(x,k) +X509 *x; +EVP_PKEY *k; + { + EVP_PKEY *xk=NULL; + int ok=0; + + xk=X509_get_pubkey(x); + if (xk->type != k->type) goto err; + switch (k->type) + { +#ifndef NO_RSA + case EVP_PKEY_RSA: + if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0) goto err; + if (BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) goto err; + break; +#endif +#ifndef NO_DSA + case EVP_PKEY_DSA: + if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0) + goto err; + break; +#endif +#ifndef NO_DH + case EVP_PKEY_DH: + /* No idea */ + goto err; +#endif + default: + goto err; + } + + ok=1; +err: + return(ok); + } + +int X509_STORE_add_cert(ctx,x) +X509_STORE *ctx; +X509 *x; + { + X509_OBJECT *obj,*r; + int ret=1; + + if (x == NULL) return(0); + obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT)); + if (obj == NULL) + { + X509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE); + return(0); + } + obj->type=X509_LU_X509; + obj->data.x509=x; + + CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); + + X509_OBJECT_up_ref_count(obj); + + r=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj); + if (r != NULL) + { /* oops, put it back */ + lh_delete(ctx->certs,(char *)obj); + X509_OBJECT_free_contents(obj); + Free(obj); + lh_insert(ctx->certs,(char *)r); + X509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE); + ret=0; + } + + CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); + + return(ret); + } + + diff --git a/crypto/x509/x509_vfy.h b/crypto/x509/x509_vfy.h new file mode 100644 index 0000000000..b92e1ba432 --- /dev/null +++ b/crypto/x509/x509_vfy.h @@ -0,0 +1,341 @@ +/* crypto/x509/x509_vfy.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_X509_VFY_H +#define HEADER_X509_VFY_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Outer object */ +typedef struct x509_hash_dir_st + { + int num_dirs; + char **dirs; + int *dirs_type; + int num_dirs_alloced; + } X509_HASH_DIR_CTX; + +typedef struct x509_file_st + { + int num_paths; /* number of paths to files or directories */ + int num_alloced; + char **paths; /* the list of paths or directories */ + int *path_type; + } X509_CERT_FILE_CTX; + +/*******************************/ +/* +SSL_CTX -> X509_STORE + -> X509_LOOKUP + ->X509_LOOKUP_METHOD + -> X509_LOOKUP + ->X509_LOOKUP_METHOD + +SSL -> X509_STORE_CTX + ->X509_STORE + +The X509_STORE holds the tables etc for verification stuff. +A X509_STORE_CTX is used while validating a single certificate. +The X509_STORE has X509_LOOKUPs for looking up certs. +The X509_STORE then calls a function to actually verify the +certificate chain. +*/ + +#define X509_LU_RETRY -1 +#define X509_LU_FAIL 0 +#define X509_LU_X509 1 +#define X509_LU_CRL 2 +#define X509_LU_PKEY 3 + +typedef struct x509_object_st + { + /* one of the above types */ + int type; + union { + char *ptr; + X509 *x509; + X509_CRL *crl; + EVP_PKEY *pkey; + } data; + } X509_OBJECT; + +/* This is a static that defines the function interface */ +typedef struct x509_lookup_method_st + { + char *name; + int (*new_item)(); + void (*free)(); + int (*init)(/* meth, char ** */); + int (*shutdown)( /* meth, char ** */); + int (*ctrl)( /* meth, char **, int cmd, char *argp, int argi */); + int (*get_by_subject)(/* meth, char **, XNAME *, X509 **ret */); + int (*get_by_issuer_serial)(); + int (*get_by_fingerprint)(); + int (*get_by_alias)(); + } X509_LOOKUP_METHOD; + +/* This is used to hold everything. It is used for all certificate + * validation. Once we have a certificate chain, the 'verify' + * function is then called to actually check the cert chain. */ +typedef struct x509_store_st + { + /* The following is a cache of trusted certs */ + int cache; /* if true, stash any hits */ +#ifdef HEADER_LHASH_H + LHASH *certs; /* cached certs; */ +#else + char *certs; +#endif + + /* These are external lookup methods */ + STACK *get_cert_methods;/* X509_LOOKUP */ + int (*verify)(); /* called to verify a certificate */ + int (*verify_cb)(); /* error callback */ + + char *app_data; + int references; + int depth; /* how deep to look */ + } X509_STORE; + +#define X509_STORE_set_depth(ctx,depth) ((ctx)->depth=(depth)) + +#define X509_STORE_set_verify_cb_func(ctx,func) ((ctx)->verify_cb=(func)) +#define X509_STORE_set_verify_func(ctx,func) ((ctx)->verify=(func)) + +/* This is the functions plus an instance of the local variables. */ +typedef struct x509_lookup_st + { + int init; /* have we been started */ + int skip; /* don't use us. */ + X509_LOOKUP_METHOD *method; /* the functions */ + char *method_data; /* method data */ + + X509_STORE *store_ctx; /* who owns us */ + } X509_LOOKUP; + +/* This is a temporary used when processing cert chains. Since the + * gathering of the cert chain can take some time (and have to be + * 'retried', this needs to be kept and passed around. */ +typedef struct x509_store_state_st + { + X509_STORE *ctx; + int current_method; /* used when looking up certs */ + + /* The following are set by the caller */ + X509 *cert; /* The cert to check */ + STACK *untrusted; /* chain of X509s - untrusted - passed in */ + + /* The following is built up */ + int depth; /* how far to go looking up certs */ + int valid; /* if 0, rebuild chain */ + int last_untrusted; /* index of last untrusted cert */ + STACK *chain; /* chain of X509s - built up and trusted */ + + /* When something goes wrong, this is why */ + int error_depth; + int error; + X509 *current_cert; + + char *app_data; + } X509_STORE_CTX; + +#define X509_STORE_CTX_set_app_data(ctx,data) ((ctx)->app_data=(data)) +#define X509_STORE_CTX_get_app_data(ctx) ((ctx)->app_data) +#define X509_STORE_CTX_get_error(ctx) ((ctx)->error) +#define X509_STORE_CTX_set_error(ctx,s) ((ctx)->error=(s)) +#define X509_STORE_CTX_get_error_depth(ctx) ((ctx)->error_depth) +#define X509_STORE_CTX_get_current_cert(ctx) ((ctx)->current_cert) +#define X509_STORE_CTX_get_chain(ctx) ((ctx)->chain) + +#define X509_STORE_CTX_set_cert(c,ch) ((c)->cert=(ch)) +#define X509_STORE_CTX_set_chain(c,ch) ((c)->untrusted=(ch)) + +#define X509_L_FILE_LOAD 1 +#define X509_L_ADD_DIR 2 + +X509_LOOKUP_METHOD *X509_LOOKUP_file(); +#define X509_LOOKUP_load_file(x,name,type) \ + X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL) + +X509_LOOKUP_METHOD *X509_LOOKUP_dir(); +#define X509_LOOKUP_add_dir(x,name,type) \ + X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL) + +#define X509_V_OK 0 + +#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2 +#define X509_V_ERR_UNABLE_TO_GET_CRL 3 +#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4 +#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5 +#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6 +#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7 +#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8 +#define X509_V_ERR_CERT_NOT_YET_VALID 9 +#define X509_V_ERR_CERT_HAS_EXPIRED 10 +#define X509_V_ERR_CRL_NOT_YET_VALID 11 +#define X509_V_ERR_CRL_HAS_EXPIRED 12 +#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13 +#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14 +#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FILED 15 +#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FILED 16 +#define X509_V_ERR_OUT_OF_MEM 17 +#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18 +#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19 +#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20 +#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21 +#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22 + +#ifndef NOPROTO +#ifdef HEADER_LHASH_H +X509_OBJECT *X509_OBJECT_retrive_by_subject(LHASH *h,int type,X509_NAME *name); +#endif +void X509_OBJECT_up_ref_count(X509_OBJECT *a); +void X509_OBJECT_free_contents(X509_OBJECT *a); +X509_STORE *X509_STORE_new(void ); +void X509_STORE_free(X509_STORE *v); + +void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, + X509 *x509, STACK *chain); +void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx); + +X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m); + +X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void); +X509_LOOKUP_METHOD *X509_LOOKUP_file(void); + +int X509_STORE_add_cert(X509_STORE *ctx, X509 *x); + +int X509_STORE_get_by_subject(X509_STORE_CTX *vs,int type,X509_NAME *name, + X509_OBJECT *ret); + +int X509_LOOKUP_ctrl(X509_LOOKUP *ctx,int cmd,char *argc,long argl,char **ret); +int X509_load_cert_file(X509_LOOKUP *ctx, char *file, int type); + +void X509v3_cleanup_extensions(void ); +int X509v3_add_extension(X509_EXTENSION_METHOD *x); +int X509v3_add_netscape_extensions(void ); +int X509v3_add_standard_extensions(void ); + +X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method); +void X509_LOOKUP_free(X509_LOOKUP *ctx); +int X509_LOOKUP_init(X509_LOOKUP *ctx); +int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, + X509_OBJECT *ret); +int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, int type, X509_NAME *name, + ASN1_INTEGER *serial, X509_OBJECT *ret); +int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, int type, + unsigned char *bytes, int len, X509_OBJECT *ret); +int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, int type, char *str, + int len, X509_OBJECT *ret); +int X509_LOOKUP_shutdown(X509_LOOKUP *ctx); + +int X509_STORE_load_locations (X509_STORE *ctx, + char *file, char *dir); +int X509_STORE_set_default_paths(X509_STORE *ctx); + +#else + +#ifdef HEADER_LHASH_H +X509_OBJECT *X509_OBJECT_retrive_by_subject(); +#endif +void X509_OBJECT_up_ref_count(); +void X509_OBJECT_free_contents(); +X509_STORE *X509_STORE_new(); +void X509_STORE_free(); + +void X509_STORE_CTX_init(); +void X509_STORE_CTX_cleanup(); + +X509_LOOKUP *X509_STORE_add_lookup(); + +X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(); +X509_LOOKUP_METHOD *X509_LOOKUP_file(); + +int X509_STORE_add_cert(); + +int X509_STORE_get_by_subject(); + +int X509_LOOKUP_ctrl(); +int X509_load_cert_file(); + +void X509v3_cleanup_extensions(); +int X509v3_add_extension(); +int X509v3_add_netscape_extensions(); +int X509v3_add_standard_extensions(); + +X509_LOOKUP *X509_LOOKUP_new(); +void X509_LOOKUP_free(); +int X509_LOOKUP_init(); +int X509_LOOKUP_by_subject(); +int X509_LOOKUP_by_issuer_serial(); +int X509_LOOKUP_by_fingerprint(); +int X509_LOOKUP_by_alias(); +int X509_LOOKUP_shutdown(); + +int X509_STORE_load_locations (); +int X509_STORE_set_default_paths(); + +#endif + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/crypto/x509/x509name.c b/crypto/x509/x509name.c new file mode 100644 index 0000000000..8223ec069d --- /dev/null +++ b/crypto/x509/x509name.c @@ -0,0 +1,357 @@ +/* crypto/x509/x509name.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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +int X509_NAME_get_text_by_NID(name,nid,buf,len) +X509_NAME *name; +int nid; +char *buf; +int len; + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); + if (obj == NULL) return(-1); + return(X509_NAME_get_text_by_OBJ(name,obj,buf,len)); + } + +int X509_NAME_get_text_by_OBJ(name,obj,buf,len) +X509_NAME *name; +ASN1_OBJECT *obj; +char *buf; +int len; + { + int i; + ASN1_STRING *data; + + i=X509_NAME_get_index_by_OBJ(name,obj,-1); + if (i < 0) return(-1); + data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i)); + i=(data->length > (len-1))?(len-1):data->length; + if (buf == NULL) return(data->length); + memcpy(buf,data->data,i); + buf[i]='\0'; + return(i); + } + +int X509_NAME_entry_count(name) +X509_NAME *name; + { + if (name == NULL) return(0); + return(sk_num(name->entries)); + } + +int X509_NAME_get_index_by_NID(name,nid,oldpos) +X509_NAME *name; +int nid; +int oldpos; + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); + if (obj == NULL) return(-2); + return(X509_NAME_get_index_by_OBJ(name,obj,oldpos)); + } + +int X509_NAME_get_index_by_OBJ(name,obj,oldpos) +X509_NAME *name; +ASN1_OBJECT *obj; +int oldpos; + { + int n; + X509_NAME_ENTRY *ne; + STACK *sk; + + if (name == NULL) return(-1); + if (oldpos < 0) + oldpos= -1; + sk=name->entries; + n=sk_num(sk); + for (oldpos++; oldpos < n; oldpos++) + { + ne=(X509_NAME_ENTRY *)sk_value(sk,oldpos); + if (OBJ_cmp(ne->object,obj) == 0) + return(oldpos); + } + return(-1); + } + +X509_NAME_ENTRY *X509_NAME_get_entry(name,loc) +X509_NAME *name; +int loc; + { + if ( (name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) + return(NULL); + else + return((X509_NAME_ENTRY *)sk_value(name->entries,loc)); + } + +X509_NAME_ENTRY *X509_NAME_delete_entry(name,loc) +X509_NAME *name; +int loc; + { + X509_NAME_ENTRY *ret; + int i,j,n,set_prev,set_next; + STACK *sk; + + if ((name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) + return(NULL); + sk=name->entries; + ret=(X509_NAME_ENTRY *)sk_delete(sk,loc); + n=sk_num(sk); + name->modified=1; + if (loc == n) return(ret); + + /* else we need to fixup the set field */ + if (loc != 0) + set_prev=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set; + else + set_prev=ret->set-1; + set_next=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; + + /* set_prev is the previous set + * set is the current set + * set_next is the following + * prev 1 1 1 1 1 1 1 1 + * set 1 1 2 2 + * next 1 1 2 2 2 2 3 2 + * so basically only if prev and next differ by 2, then + * re-number down by 1 */ + if (set_prev+1 < set_next) + { + j=set_next-set_prev-1; + for (i=loc; i<n; i++) + ((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set-=j; + } + return(ret); + } + +/* if set is -1, append to previous set, 0 'a new one', and 1, + * prepend to the guy we are about to stomp on. */ +int X509_NAME_add_entry(name,ne,loc,set) +X509_NAME *name; +X509_NAME_ENTRY *ne; +int loc; +int set; + { + X509_NAME_ENTRY *new_name=NULL; + int n,i,inc; + STACK *sk; + + if (name == NULL) return(0); + sk=name->entries; + n=sk_num(sk); + if (loc > n) loc=n; + else if (loc < 0) loc=n; + + name->modified=1; + + if (set == -1) + { + if (loc == 0) + { + set=0; + inc=1; + } + else + { + set=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set; + inc=0; + } + } + else /* if (set >= 0) */ + { + if (loc >= n) + { + if (loc != 0) + set=((X509_NAME_ENTRY *) + sk_value(sk,loc-1))->set+1; + else + set=0; + } + else + set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; + inc=(set == 0)?1:0; + } + + if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL) + goto err; + new_name->set=set; + if (!sk_insert(sk,(char *)new_name,loc)) + { + X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE); + goto err; + } + if (inc) + { + n=sk_num(sk); + for (i=loc+1; i<n; i++) + ((X509_NAME_ENTRY *)sk_value(sk,i-1))->set+=1; + } + return(1); +err: + if (new_name != NULL) + X509_NAME_ENTRY_free(ne); + return(0); + } + +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(ne,nid,type,bytes,len) +X509_NAME_ENTRY **ne; +int nid; +int type; +unsigned char *bytes; +int len; + { + ASN1_OBJECT *obj; + + obj=OBJ_nid2obj(nid); + if (obj == NULL) + { + X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID); + return(NULL); + } + return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len)); + } + +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len) +X509_NAME_ENTRY **ne; +ASN1_OBJECT *obj; +int type; +unsigned char *bytes; +int len; + { + X509_NAME_ENTRY *ret; + + if ((ne == NULL) || (*ne == NULL)) + { + if ((ret=X509_NAME_ENTRY_new()) == NULL) + return(NULL); + } + else + ret= *ne; + + if (!X509_NAME_ENTRY_set_object(ret,obj)) + goto err; + if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len)) + goto err; + + if ((ne != NULL) && (*ne == NULL)) *ne=ret; + return(ret); +err: + if ((ne == NULL) || (ret != *ne)) + X509_NAME_ENTRY_free(ret); + return(NULL); + } + +int X509_NAME_ENTRY_set_object(ne,obj) +X509_NAME_ENTRY *ne; +ASN1_OBJECT *obj; + { + if ((ne == NULL) || (obj == NULL)) + { + X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER); + return(0); + } + ASN1_OBJECT_free(ne->object); + ne->object=OBJ_dup(obj); + return((ne->object == NULL)?0:1); + } + +int X509_NAME_ENTRY_set_data(ne,type,bytes,len) +X509_NAME_ENTRY *ne; +int type; +unsigned char *bytes; +int len; + { + int i; + + if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0); + if (len < 0) len=strlen((char *)bytes); + i=ASN1_STRING_set(ne->value,bytes,len); + if (!i) return(0); + if (type != V_ASN1_UNDEF) + { + if (type == V_ASN1_APP_CHOOSE) + ne->value->type=ASN1_PRINTABLE_type(bytes,len); + else + ne->value->type=type; + } + return(1); + } + +ASN1_OBJECT *X509_NAME_ENTRY_get_object(ne) +X509_NAME_ENTRY *ne; + { + if (ne == NULL) return(NULL); + return(ne->object); + } + +ASN1_STRING *X509_NAME_ENTRY_get_data(ne) +X509_NAME_ENTRY *ne; + { + if (ne == NULL) return(NULL); + return(ne->value); + } + diff --git a/crypto/x509/x509pack.c b/crypto/x509/x509pack.c new file mode 100644 index 0000000000..949943fca5 --- /dev/null +++ b/crypto/x509/x509pack.c @@ -0,0 +1,157 @@ +/* crypto/x509/x509pack.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 <stdio.h> +#include "stack.h" +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +ASN1_OCTET_STRING *X509v3_pack_string(ex,type,bytes,len) +ASN1_OCTET_STRING **ex; +int type; +unsigned char *bytes; +int len; + { + ASN1_OCTET_STRING *os; + ASN1_STRING str; + unsigned char *p; + int i; + + if ((ex == NULL) || (*ex == NULL)) + os=ASN1_OCTET_STRING_new(); + else + os= *ex; + + if (len < 0) len=strlen((char *)bytes); + str.length=len; + str.type=type; + str.data=bytes; + + /* str now holds the data, we just have to copy it into ->value */ + + switch (type) + { + case V_ASN1_BIT_STRING: + i=i2d_ASN1_BIT_STRING((ASN1_BIT_STRING *)&str,NULL); + if (!ASN1_STRING_set((ASN1_STRING *)os,NULL,i)) + goto err; + p=(unsigned char *)os->data; + i2d_ASN1_BIT_STRING((ASN1_BIT_STRING *)&str,&p); + break; + case V_ASN1_OCTET_STRING: + i=i2d_ASN1_OCTET_STRING((ASN1_OCTET_STRING *)&str,NULL); + if (!ASN1_STRING_set((ASN1_STRING *)os,NULL,i)) + goto err; + p=(unsigned char *)os->data; + i2d_ASN1_OCTET_STRING((ASN1_OCTET_STRING *)&str,&p); + break; + case V_ASN1_IA5STRING: + case V_ASN1_PRINTABLESTRING: + case V_ASN1_T61STRING: + i=i2d_ASN1_bytes(&str,NULL,type,V_ASN1_UNIVERSAL); + if (!ASN1_STRING_set((ASN1_STRING *)os,NULL,i)) + goto err; + p=(unsigned char *)os->data; + i=i2d_ASN1_bytes(&str,&p,type,V_ASN1_UNIVERSAL); + break; + default: + X509err(X509_F_X509V3_PACK_STRING,X509_R_UNKNOWN_STRING_TYPE); + goto err; + } + os->length=i; + + if ((ex != NULL) && (os != *ex)) + *ex=os; + return(os); +err: + return(NULL); + } + +ASN1_STRING *X509v3_unpack_string(ex,type,os) +ASN1_STRING **ex; +int type; +ASN1_OCTET_STRING *os; + { + unsigned char *p; + ASN1_STRING *ret=NULL; + + p=os->data; + switch (type) + { + case V_ASN1_BIT_STRING: + ret=(ASN1_STRING *)d2i_ASN1_BIT_STRING( + (ASN1_BIT_STRING **)ex,&p,os->length); + break; + case V_ASN1_OCTET_STRING: + ret=(ASN1_STRING *)d2i_ASN1_BIT_STRING( + (ASN1_BIT_STRING **)ex,&p,os->length); + break; + case V_ASN1_IA5STRING: + case V_ASN1_PRINTABLESTRING: + case V_ASN1_T61STRING: + ret=(ASN1_STRING *)d2i_ASN1_PRINTABLE(ex,&p,os->length); + break; + default: + X509err(X509_F_X509V3_UNPACK_STRING,X509_R_UNKNOWN_STRING_TYPE); + } + return(ret); + } + diff --git a/crypto/x509/x509rset.c b/crypto/x509/x509rset.c new file mode 100644 index 0000000000..2ff456f2ee --- /dev/null +++ b/crypto/x509/x509rset.c @@ -0,0 +1,89 @@ +/* crypto/x509/x509rset.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 <stdio.h> +#include "cryptlib.h" +#include "asn1.h" +#include "objects.h" +#include "evp.h" +#include "x509.h" + +int X509_REQ_set_version(x,version) +X509_REQ *x; +long version; + { + if (x == NULL) return(0); + return(ASN1_INTEGER_set(x->req_info->version,version)); + } + +int X509_REQ_set_subject_name(x,name) +X509_REQ *x; +X509_NAME *name; + { + if ((x == NULL) || (x->req_info == NULL)) return(0); + return(X509_NAME_set(&x->req_info->subject,name)); + } + +int X509_REQ_set_pubkey(x,pkey) +X509_REQ *x; +EVP_PKEY *pkey; + { + if ((x == NULL) || (x->req_info == NULL)) return(0); + return(X509_PUBKEY_set(&x->req_info->pubkey,pkey)); + } + diff --git a/crypto/x509/x509type.c b/crypto/x509/x509type.c new file mode 100644 index 0000000000..05d6919d6a --- /dev/null +++ b/crypto/x509/x509type.c @@ -0,0 +1,115 @@ +/* crypto/x509/x509type.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 <stdio.h> +#include "cryptlib.h" +#include "evp.h" +#include "objects.h" +#include "x509.h" + +int X509_certificate_type(x,pkey) +X509 *x; +EVP_PKEY *pkey; + { + EVP_PKEY *pk; + int ret=0,i; + + if (x == NULL) return(0); + + if (pkey == NULL) + pk=X509_get_pubkey(x); + else + pk=pkey; + + if (pk == NULL) return(0); + + switch (pk->type) + { + case EVP_PKEY_RSA: + ret=EVP_PK_RSA|EVP_PKT_SIGN; +/* if (!sign only extension) */ + ret|=EVP_PKT_ENC; + break; + case EVP_PKEY_DSA: + ret=EVP_PK_DSA|EVP_PKT_SIGN; + break; + case EVP_PKEY_DH: + ret=EVP_PK_DH|EVP_PKT_EXCH; + break; + default: + break; + } + + i=X509_get_signature_type(x); + switch (i) + { + case EVP_PKEY_RSA: + ret|=EVP_PKS_RSA; + break; + case EVP_PKS_DSA: + ret|=EVP_PKS_DSA; + break; + default: + break; + } + + if (EVP_PKEY_size(pkey) <= 512) + ret|=EVP_PKT_EXP; + return(ret); + } + diff --git a/crypto/x509/x_all.c b/crypto/x509/x_all.c new file mode 100644 index 0000000000..2f554f597b --- /dev/null +++ b/crypto/x509/x_all.c @@ -0,0 +1,465 @@ +/* crypto/x509/x_all.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 <stdio.h> +#undef SSLEAY_MACROS +#include "stack.h" +#include "cryptlib.h" +#include "buffer.h" +#include "asn1.h" +#include "evp.h" +#include "x509.h" + +int X509_verify(a,r) +X509 *a; +EVP_PKEY *r; + { + return(ASN1_verify((int (*)())i2d_X509_CINF,a->sig_alg, + a->signature,(char *)a->cert_info,r)); + } + +int X509_REQ_verify(a,r) +X509_REQ *a; +EVP_PKEY *r; + { + return( ASN1_verify((int (*)())i2d_X509_REQ_INFO, + a->sig_alg,a->signature,(char *)a->req_info,r)); + } + +int X509_CRL_verify(a,r) +X509_CRL *a; +EVP_PKEY *r; + { + return(ASN1_verify((int (*)())i2d_X509_CRL_INFO, + a->sig_alg, a->signature,(char *)a->crl,r)); + } + +int NETSCAPE_SPKI_verify(a,r) +NETSCAPE_SPKI *a; +EVP_PKEY *r; + { + return(ASN1_verify((int (*)())i2d_NETSCAPE_SPKAC, + a->sig_algor,a->signature, (char *)a->spkac,r)); + } + +int X509_sign(x,pkey,md) +X509 *x; +EVP_PKEY *pkey; +EVP_MD *md; + { + return(ASN1_sign((int (*)())i2d_X509_CINF, x->cert_info->signature, + x->sig_alg, x->signature, (char *)x->cert_info,pkey,md)); + } + +int X509_REQ_sign(x,pkey,md) +X509_REQ *x; +EVP_PKEY *pkey; +EVP_MD *md; + { + return(ASN1_sign((int (*)())i2d_X509_REQ_INFO,x->sig_alg, NULL, + x->signature, (char *)x->req_info,pkey,md)); + } + +int X509_CRL_sign(x,pkey,md) +X509_CRL *x; +EVP_PKEY *pkey; +EVP_MD *md; + { + return(ASN1_sign((int (*)())i2d_X509_CRL_INFO,x->crl->sig_alg, + x->sig_alg, x->signature, (char *)x->crl,pkey,md)); + } + +int NETSCAPE_SPKI_sign(x,pkey,md) +NETSCAPE_SPKI *x; +EVP_PKEY *pkey; +EVP_MD *md; + { + return(ASN1_sign((int (*)())i2d_NETSCAPE_SPKAC, x->sig_algor,NULL, + x->signature, (char *)x->spkac,pkey,md)); + } + +X509 *X509_dup(x509) +X509 *x509; + { + return((X509 *)ASN1_dup((int (*)())i2d_X509, + (char *(*)())d2i_X509,(char *)x509)); + } + +X509_EXTENSION *X509_EXTENSION_dup(ex) +X509_EXTENSION *ex; + { + return((X509_EXTENSION *)ASN1_dup( + (int (*)())i2d_X509_EXTENSION, + (char *(*)())d2i_X509_EXTENSION,(char *)ex)); + } + +#ifndef WIN16 +X509 *d2i_X509_fp(fp,x509) +FILE *fp; +X509 *x509; + { + return((X509 *)ASN1_d2i_fp((char *(*)())X509_new, + (char *(*)())d2i_X509, (fp),(unsigned char **)(x509))); + } + +int i2d_X509_fp(fp,x509) +FILE *fp; +X509 *x509; + { + return(ASN1_i2d_fp(i2d_X509,fp,(unsigned char *)x509)); + } +#endif + +X509 *d2i_X509_bio(bp,x509) +BIO *bp; +X509 *x509; + { + return((X509 *)ASN1_d2i_bio((char *(*)())X509_new, + (char *(*)())d2i_X509, (bp),(unsigned char **)(x509))); + } + +int i2d_X509_bio(bp,x509) +BIO *bp; +X509 *x509; + { + return(ASN1_i2d_bio(i2d_X509,bp,(unsigned char *)x509)); + } + +X509_CRL *X509_CRL_dup(crl) +X509_CRL *crl; + { + return((X509_CRL *)ASN1_dup((int (*)())i2d_X509_CRL, + (char *(*)())d2i_X509_CRL,(char *)crl)); + } + +#ifndef WIN16 +X509_CRL *d2i_X509_CRL_fp(fp,crl) +FILE *fp; +X509_CRL *crl; + { + return((X509_CRL *)ASN1_d2i_fp((char *(*)()) + X509_CRL_new,(char *(*)())d2i_X509_CRL, (fp), + (unsigned char **)(crl))); + } + +int i2d_X509_CRL_fp(fp,crl) +FILE *fp; +X509_CRL *crl; + { + return(ASN1_i2d_fp(i2d_X509_CRL,fp,(unsigned char *)crl)); + } +#endif + +X509_CRL *d2i_X509_CRL_bio(bp,crl) +BIO *bp; +X509_CRL *crl; + { + return((X509_CRL *)ASN1_d2i_bio((char *(*)()) + X509_CRL_new,(char *(*)())d2i_X509_CRL, (bp), + (unsigned char **)(crl))); + } + +int i2d_X509_CRL_bio(bp,crl) +BIO *bp; +X509_CRL *crl; + { + return(ASN1_i2d_bio(i2d_X509_CRL,bp,(unsigned char *)crl)); + } + +PKCS7 *PKCS7_dup(p7) +PKCS7 *p7; + { + return((PKCS7 *)ASN1_dup((int (*)())i2d_PKCS7, + (char *(*)())d2i_PKCS7,(char *)p7)); + } + +#ifndef WIN16 +PKCS7 *d2i_PKCS7_fp(fp,p7) +FILE *fp; +PKCS7 *p7; + { + return((PKCS7 *)ASN1_d2i_fp((char *(*)()) + PKCS7_new,(char *(*)())d2i_PKCS7, (fp), + (unsigned char **)(p7))); + } + +int i2d_PKCS7_fp(fp,p7) +FILE *fp; +PKCS7 *p7; + { + return(ASN1_i2d_fp(i2d_PKCS7,fp,(unsigned char *)p7)); + } +#endif + +PKCS7 *d2i_PKCS7_bio(bp,p7) +BIO *bp; +PKCS7 *p7; + { + return((PKCS7 *)ASN1_d2i_bio((char *(*)()) + PKCS7_new,(char *(*)())d2i_PKCS7, (bp), + (unsigned char **)(p7))); + } + +int i2d_PKCS7_bio(bp,p7) +BIO *bp; +PKCS7 *p7; + { + return(ASN1_i2d_bio(i2d_PKCS7,bp,(unsigned char *)p7)); + } + +X509_REQ *X509_REQ_dup(req) +X509_REQ *req; + { + return((X509_REQ *)ASN1_dup((int (*)())i2d_X509_REQ, + (char *(*)())d2i_X509_REQ,(char *)req)); + } + +#ifndef WIN16 +X509_REQ *d2i_X509_REQ_fp(fp,req) +FILE *fp; +X509_REQ *req; + { + return((X509_REQ *)ASN1_d2i_fp((char *(*)()) + X509_REQ_new, (char *(*)())d2i_X509_REQ, (fp), + (unsigned char **)(req))); + } + +int i2d_X509_REQ_fp(fp,req) +FILE *fp; +X509_REQ *req; + { + return(ASN1_i2d_fp(i2d_X509_REQ,fp,(unsigned char *)req)); + } +#endif + +X509_REQ *d2i_X509_REQ_bio(bp,req) +BIO *bp; +X509_REQ *req; + { + return((X509_REQ *)ASN1_d2i_bio((char *(*)()) + X509_REQ_new, (char *(*)())d2i_X509_REQ, (bp), + (unsigned char **)(req))); + } + +int i2d_X509_REQ_bio(bp,req) +BIO *bp; +X509_REQ *req; + { + return(ASN1_i2d_bio(i2d_X509_REQ,bp,(unsigned char *)req)); + } + +#ifndef NO_RSA +RSA *RSAPublicKey_dup(rsa) +RSA *rsa; + { + return((RSA *)ASN1_dup((int (*)())i2d_RSAPublicKey, + (char *(*)())d2i_RSAPublicKey,(char *)rsa)); + } + +RSA *RSAPrivateKey_dup(rsa) +RSA *rsa; + { + return((RSA *)ASN1_dup((int (*)())i2d_RSAPrivateKey, + (char *(*)())d2i_RSAPrivateKey,(char *)rsa)); + } + +#ifndef WIN16 +RSA *d2i_RSAPrivateKey_fp(fp,rsa) +FILE *fp; +RSA *rsa; + { + return((RSA *)ASN1_d2i_fp((char *(*)()) + RSA_new,(char *(*)())d2i_RSAPrivateKey, (fp), + (unsigned char **)(rsa))); + } + +int i2d_RSAPrivateKey_fp(fp,rsa) +FILE *fp; +RSA *rsa; + { + return(ASN1_i2d_fp(i2d_RSAPrivateKey,fp,(unsigned char *)rsa)); + } + +RSA *d2i_RSAPublicKey_fp(fp,rsa) +FILE *fp; +RSA *rsa; + { + return((RSA *)ASN1_d2i_fp((char *(*)()) + RSA_new,(char *(*)())d2i_RSAPublicKey, (fp), + (unsigned char **)(rsa))); + } + +int i2d_RSAPublicKey_fp(fp,rsa) +FILE *fp; +RSA *rsa; + { + return(ASN1_i2d_fp(i2d_RSAPublicKey,fp,(unsigned char *)rsa)); + } +#endif + +RSA *d2i_RSAPrivateKey_bio(bp,rsa) +BIO *bp; +RSA *rsa; + { + return((RSA *)ASN1_d2i_bio((char *(*)()) + RSA_new,(char *(*)())d2i_RSAPrivateKey, (bp), + (unsigned char **)(rsa))); + } + +int i2d_RSAPrivateKey_bio(bp,rsa) +BIO *bp; +RSA *rsa; + { + return(ASN1_i2d_bio(i2d_RSAPrivateKey,bp,(unsigned char *)rsa)); + } + +RSA *d2i_RSAPublicKey_bio(bp,rsa) +BIO *bp; +RSA *rsa; + { + return((RSA *)ASN1_d2i_bio((char *(*)()) + RSA_new,(char *(*)())d2i_RSAPublicKey, (bp), + (unsigned char **)(rsa))); + } + +int i2d_RSAPublicKey_bio(bp,rsa) +BIO *bp; +RSA *rsa; + { + return(ASN1_i2d_bio(i2d_RSAPublicKey,bp,(unsigned char *)rsa)); + } +#endif + +#ifndef NO_DSA +#ifndef WIN16 +DSA *d2i_DSAPrivateKey_fp(fp,dsa) +FILE *fp; +DSA *dsa; + { + return((DSA *)ASN1_d2i_fp((char *(*)()) + DSA_new,(char *(*)())d2i_DSAPrivateKey, (fp), + (unsigned char **)(dsa))); + } + +int i2d_DSAPrivateKey_fp(fp,dsa) +FILE *fp; +DSA *dsa; + { + return(ASN1_i2d_fp(i2d_DSAPrivateKey,fp,(unsigned char *)dsa)); + } +#endif + +DSA *d2i_DSAPrivateKey_bio(bp,dsa) +BIO *bp; +DSA *dsa; + { + return((DSA *)ASN1_d2i_bio((char *(*)()) + DSA_new,(char *(*)())d2i_DSAPrivateKey, (bp), + (unsigned char **)(dsa))); + } + +int i2d_DSAPrivateKey_bio(bp,dsa) +BIO *bp; +DSA *dsa; + { + return(ASN1_i2d_bio(i2d_DSAPrivateKey,bp,(unsigned char *)dsa)); + } +#endif + +X509_NAME *X509_NAME_dup(xn) +X509_NAME *xn; + { + return((X509_NAME *)ASN1_dup((int (*)())i2d_X509_NAME, + (char *(*)())d2i_X509_NAME,(char *)xn)); + } + +X509_NAME_ENTRY *X509_NAME_ENTRY_dup(ne) +X509_NAME_ENTRY *ne; + { + return((X509_NAME_ENTRY *)ASN1_dup((int (*)())i2d_X509_NAME_ENTRY, + (char *(*)())d2i_X509_NAME_ENTRY,(char *)ne)); + } + +int X509_digest(data,type,md,len) +X509 *data; +EVP_MD *type; +unsigned char *md; +unsigned int *len; + { + return(ASN1_digest((int (*)())i2d_X509,type,(char *)data,md,len)); + } + +int X509_NAME_digest(data,type,md,len) +X509_NAME *data; +EVP_MD *type; +unsigned char *md; +unsigned int *len; + { + return(ASN1_digest((int (*)())i2d_X509_NAME,type,(char *)data,md,len)); + } + +int PKCS7_ISSUER_AND_SERIAL_digest(data,type,md,len) +PKCS7_ISSUER_AND_SERIAL *data; +EVP_MD *type; +unsigned char *md; +unsigned int *len; + { + return(ASN1_digest((int (*)())i2d_PKCS7_ISSUER_AND_SERIAL,type, + (char *)data,md,len)); + } + |