Move to beta 3.
[openssl.git] / apps / ca.c
index 4225158dfa57d3c52376e6610c3f8e9aa22db07d..65209eeee3f7d01da328078198bba2d38fe44d34 100644 (file)
--- a/apps/ca.c
+++ b/apps/ca.c
 #include <sys/types.h>
 #include <sys/stat.h>
 #include "apps.h"
-#include "bio.h"
-#include "err.h"
-#include "bn.h"
-#include "txt_db.h"
-#include "evp.h"
-#include "x509.h"
-#include "x509v3.h"
-#include "objects.h"
-#include "pem.h"
-#include "conf.h"
+#include <openssl/conf.h>
+#include <openssl/bio.h>
+#include <openssl/err.h>
+#include <openssl/bn.h>
+#include <openssl/txt_db.h>
+#include <openssl/evp.h>
+#include <openssl/x509.h>
+#include <openssl/x509v3.h>
+#include <openssl/objects.h>
+#include <openssl/pem.h>
 
 #ifndef W_OK
-#include <sys/file.h>
+#  ifdef VMS
+#    if defined(__DECC)
+#      include <unistd.h>
+#    else
+#      include <unixlib.h>
+#    endif
+#  else
+#    include <sys/file.h>
+#  endif
+#endif
+
+#ifndef W_OK
+#  define F_OK 0
+#  define X_OK 1
+#  define W_OK 2
+#  define R_OK 4
 #endif
 
 #undef PROG
 #define PROG ca_main
 
 #define BASE_SECTION   "ca"
-#define CONFIG_FILE "lib/openssl.cnf"
+#define CONFIG_FILE "openssl.cnf"
 
 #define ENV_DEFAULT_CA         "default_ca"
 
@@ -156,7 +171,6 @@ extern int EF_PROTECT_BELOW;
 extern int EF_ALIGNMENT;
 #endif
 
-#ifndef NOPROTO
 static int add_oid_section(LHASH *conf);
 static void lookup_fail(char *name,char *tag);
 static int MS_CALLBACK key_callback(char *buf,int len,int verify);
@@ -187,27 +201,6 @@ static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
        LHASH *conf);
 static int do_revoke(X509 *x509, TXT_DB *db);
 static int check_time_format(char *str);
-#else
-static int add_oid_section();
-static void lookup_fail();
-static int MS_CALLBACK key_callback();
-static unsigned long index_serial_hash();
-static int index_serial_cmp();
-static unsigned long index_name_hash();
-static int index_name_qual();
-static int index_name_cmp();
-static int fix_data();
-static BIGNUM *load_serial();
-static int save_serial();
-static int certify();
-static int certify_cert();
-static int certify_spkac();
-static void write_new_certificate();
-static int do_body();
-static int do_revoke();
-static int check_time_format();
-#endif
-
 static LHASH *conf;
 static char *key=NULL;
 static char *section=NULL;
@@ -215,9 +208,7 @@ static char *section=NULL;
 static int preserve=0;
 static int msie_hack=0;
 
-int MAIN(argc, argv)
-int argc;
-char **argv;
+int MAIN(int argc, char **argv)
        {
        int total=0;
        int total_done=0;
@@ -414,12 +405,19 @@ bad:
        ERR_load_crypto_strings();
 
        /*****************************************************************/
+       if (configfile == NULL) configfile = getenv("OPENSSL_CONF");
+       if (configfile == NULL) configfile = getenv("SSLEAY_CONF");
        if (configfile == NULL)
                {
                /* We will just use 'buf[0]' as a temporary buffer.  */
+#ifdef VMS
+               strncpy(buf[0],X509_get_default_cert_area(),
+                       sizeof(buf[0])-1-sizeof(CONFIG_FILE));
+#else
                strncpy(buf[0],X509_get_default_cert_area(),
                        sizeof(buf[0])-2-sizeof(CONFIG_FILE));
                strcat(buf[0],"/");
+#endif
                strcat(buf[0],CONFIG_FILE);
                configfile=buf[0];
                }
@@ -559,7 +557,12 @@ bad:
                        BIO_printf(bio_err,"there needs to be defined a directory for new certificate to be placed in\n");
                        goto err;
                        }
+#ifdef VMS
+               /* For technical reasons, VMS misbehaves with X_OK */
+               if (access(outdir,R_OK|W_OK) != 0)
+#else
                if (access(outdir,R_OK|W_OK|X_OK) != 0)
+#endif
                        {
                        BIO_printf(bio_err,"I am unable to acces the %s directory\n",outdir);
                        perror(outdir);
@@ -712,7 +715,10 @@ bad:
                extensions=CONF_get_string(conf,section,ENV_EXTENSIONS);
                if(extensions) {
                        /* Check syntax of file */
-                       if(!X509V3_EXT_check_conf(conf, extensions)) {
+                       X509V3_CTX ctx;
+                       X509V3_set_ctx_test(&ctx);
+                       X509V3_set_conf_lhash(&ctx, conf);
+                       if(!X509V3_EXT_add_conf(conf, &ctx, extensions, NULL)) {
                                BIO_printf(bio_err,
                                 "Error Loading extension section %s\n",
                                                                 extensions);
@@ -874,12 +880,23 @@ bad:
                        BIO_printf(bio_err,"Write out database with %d new entries\n",sk_num(cert_sk));
 
                        strncpy(buf[0],serialfile,BSIZE-4);
+
+#ifdef VMS
+                       strcat(buf[0],"-new");
+#else
                        strcat(buf[0],".new");
+#endif
 
                        if (!save_serial(buf[0],serial)) goto err;
 
                        strncpy(buf[1],dbfile,BSIZE-4);
+
+#ifdef VMS
+                       strcat(buf[1],"-new");
+#else
                        strcat(buf[1],".new");
+#endif
+
                        if (BIO_write_filename(out,buf[1]) <= 0)
                                {
                                perror(dbfile);
@@ -903,7 +920,11 @@ bad:
                        p=(char *)x->cert_info->serialNumber->data;
                        
                        strncpy(buf[2],outdir,BSIZE-(j*2)-6);
+
+#ifndef VMS
                        strcat(buf[2],"/");
+#endif
+
                        n=(unsigned char *)&(buf[2][strlen(buf[2])]);
                        if (j > 0)
                                {
@@ -936,7 +957,13 @@ bad:
                        {
                        /* Rename the database and the serial file */
                        strncpy(buf[2],serialfile,BSIZE-4);
+
+#ifdef VMS
+                       strcat(buf[2],"-old");
+#else
                        strcat(buf[2],".old");
+#endif
+
                        BIO_free(in);
                        BIO_free(out);
                        in=NULL;
@@ -958,7 +985,13 @@ bad:
                                }
 
                        strncpy(buf[2],dbfile,BSIZE-4);
+
+#ifdef VMS
+                       strcat(buf[2],"-old");
+#else
                        strcat(buf[2],".old");
+#endif
+
                        if (rename(dbfile,buf[2]) < 0)
                                {
                                BIO_printf(bio_err,"unabel to rename %s to %s\n",
@@ -984,7 +1017,10 @@ bad:
                crl_ext=CONF_get_string(conf,section,ENV_CRLEXT);
                if(crl_ext) {
                        /* Check syntax of file */
-                       if(!X509V3_EXT_check_conf(conf, crl_ext)) {
+                       X509V3_CTX ctx;
+                       X509V3_set_ctx_test(&ctx);
+                       X509V3_set_conf_lhash(&ctx, conf);
+                       if(!X509V3_EXT_add_conf(conf, &ctx, crl_ext, NULL)) {
                                BIO_printf(bio_err,
                                 "Error Loading CRL extension section %s\n",
                                                                 crl_ext);
@@ -1173,16 +1209,12 @@ err:
        EXIT(ret);
        }
 
-static void lookup_fail(name,tag)
-char *name;
-char *tag;
+static void lookup_fail(char *name, char *tag)
        {
        BIO_printf(bio_err,"variable lookup failed for %s::%s\n",name,tag);
        }
 
-static int MS_CALLBACK key_callback(buf,len,verify)
-char *buf;
-int len,verify;
+static int MS_CALLBACK key_callback(char *buf, int len, int verify)
        {
        int i;
 
@@ -1193,8 +1225,7 @@ int len,verify;
        return(i);
        }
 
-static unsigned long index_serial_hash(a)
-char **a;
+static unsigned long index_serial_hash(char **a)
        {
        char *n;
 
@@ -1203,9 +1234,7 @@ char **a;
        return(lh_strhash(n));
        }
 
-static int index_serial_cmp(a,b)
-char **a;
-char **b;
+static int index_serial_cmp(char **a, char **b)
        {
        char *aa,*bb;
 
@@ -1214,21 +1243,17 @@ char **b;
        return(strcmp(aa,bb));
        }
 
-static unsigned long index_name_hash(a)
-char **a;
+static unsigned long index_name_hash(char **a)
        { return(lh_strhash(a[DB_name])); }
 
-static int index_name_qual(a)
-char **a;
+static int index_name_qual(char **a)
        { return(a[0][0] == 'V'); }
 
-static int index_name_cmp(a,b)
-char **a;
-char **b;
-       { return(strcmp(a[DB_name],b[DB_name])); }
+static int index_name_cmp(char **a, char **b)
+       { return(strcmp(a[DB_name],
+            b[DB_name])); }
 
-static BIGNUM *load_serial(serialfile)
-char *serialfile;
+static BIGNUM *load_serial(char *serialfile)
        {
        BIO *in=NULL;
        BIGNUM *ret=NULL;
@@ -1266,9 +1291,7 @@ err:
        return(ret);
        }
 
-static int save_serial(serialfile,serial)
-char *serialfile;
-BIGNUM *serial;
+static int save_serial(char *serialfile, BIGNUM *serial)
        {
        BIO *out;
        int ret=0;
@@ -1300,22 +1323,10 @@ err:
        return(ret);
        }
 
-static int certify(xret,infile,pkey,x509,dgst,policy,db,serial,startdate,days,
-                  batch,ext_sect,lconf,verbose)
-X509 **xret;
-char *infile;
-EVP_PKEY *pkey;
-X509 *x509;
-const EVP_MD *dgst;
-STACK *policy;
-TXT_DB *db;
-BIGNUM *serial;
-char *startdate;
-int days;
-int batch;
-char *ext_sect;
-LHASH *lconf;
-int verbose;
+static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
+            const EVP_MD *dgst, STACK *policy, TXT_DB *db, BIGNUM *serial,
+            char *startdate, int days, int batch, char *ext_sect, LHASH *lconf,
+                int verbose)
        {
        X509_REQ *req=NULL;
        BIO *in=NULL;
@@ -1371,22 +1382,11 @@ err:
        return(ok);
        }
 
-static int certify_cert(xret,infile,pkey,x509,dgst,policy,db,serial,startdate,
-                       days,batch,ext_sect,lconf,verbose)
-X509 **xret;
-char *infile;
-EVP_PKEY *pkey;
-X509 *x509;
-const EVP_MD *dgst;
-STACK *policy;
-TXT_DB *db;
-BIGNUM *serial;
-char *startdate;
-int days;
-int batch;
-char *ext_sect;
-LHASH *lconf;
-int verbose;
+static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
+            const EVP_MD *dgst, STACK *policy, TXT_DB *db, BIGNUM *serial,
+            char *startdate, int days, int batch, char *ext_sect, LHASH *lconf,
+                int verbose)
+
        {
        X509 *req=NULL;
        X509_REQ *rreq=NULL;
@@ -1446,22 +1446,9 @@ err:
        return(ok);
        }
 
-static int do_body(xret,pkey,x509,dgst,policy,db,serial,startdate,days,
-                  batch,verbose,req,ext_sect,lconf)
-X509 **xret;
-EVP_PKEY *pkey;
-X509 *x509;
-const EVP_MD *dgst;
-STACK *policy;
-TXT_DB *db;
-BIGNUM *serial;
-char *startdate;
-int days;
-int batch;
-int verbose;
-X509_REQ *req;
-char *ext_sect;
-LHASH *lconf;
+static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
+            STACK *policy, TXT_DB *db, BIGNUM *serial, char *startdate, int days,
+            int batch, int verbose, X509_REQ *req, char *ext_sect, LHASH *lconf)
        {
        X509_NAME *name=NULL,*CAname=NULL,*subject=NULL;
        ASN1_UTCTIME *tm,*tmptm;
@@ -1788,7 +1775,8 @@ again2:
                /* Free the current entries if any, there should not
                 * be any I belive */
                if (ci->extensions != NULL)
-                       sk_pop_free(ci->extensions,X509_EXTENSION_free);
+                       sk_X509_EXTENSION_pop_free(ci->extensions,
+                                                  X509_EXTENSION_free);
 
                ci->extensions = NULL;
 
@@ -1889,10 +1877,7 @@ err:
        return(ok);
        }
 
-static void write_new_certificate(bp,x, output_der)
-BIO *bp;
-X509 *x;
-int output_der;
+static void write_new_certificate(BIO *bp, X509 *x, int output_der)
        {
        char *f;
        char buf[256];
@@ -1918,21 +1903,9 @@ int output_der;
        BIO_puts(bp,"\n");
        }
 
-static int certify_spkac(xret,infile,pkey,x509,dgst,policy,db,serial,
-                        startdate,days,ext_sect,lconf,verbose)
-X509 **xret;
-char *infile;
-EVP_PKEY *pkey;
-X509 *x509;
-const EVP_MD *dgst;
-STACK *policy;
-TXT_DB *db;
-BIGNUM *serial;
-char *startdate;
-int days;
-char *ext_sect;
-LHASH *lconf;
-int verbose;
+static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
+            const EVP_MD *dgst, STACK *policy, TXT_DB *db, BIGNUM *serial,
+            char *startdate, int days, char *ext_sect, LHASH *lconf, int verbose)
        {
        STACK *sk=NULL;
        LHASH *parms=NULL;
@@ -2088,9 +2061,7 @@ err:
        return(ok);
        }
 
-static int fix_data(nid,type)
-int nid;
-int *type;
+static int fix_data(int nid, int *type)
        {
        if (nid == NID_pkcs9_emailAddress)
                *type=V_ASN1_IA5STRING;
@@ -2105,8 +2076,7 @@ int *type;
        return(1);
        }
 
-static int check_time_format(str)
-char *str;
+static int check_time_format(char *str)
        {
        ASN1_UTCTIME tm;
 
@@ -2116,8 +2086,7 @@ char *str;
        return(ASN1_UTCTIME_check(&tm));
        }
 
-static int add_oid_section(hconf)
-LHASH *hconf;
+static int add_oid_section(LHASH *hconf)
 {      
        char *p;
        STACK *sktmp;
@@ -2139,9 +2108,7 @@ LHASH *hconf;
        return 1;
 }
 
-static int do_revoke(x509,db)
-X509 *x509;
-TXT_DB *db;
+static int do_revoke(X509 *x509, TXT_DB *db)
 {
         ASN1_UTCTIME *tm=NULL;
         char *row[DB_NUMBER],**rrow,**irow;