A general spring-cleaning (in autumn) to fix up signed/unsigned warnings.
[openssl.git] / apps / ca.c
index 34b1507aeedda8e11b95ceddc2b2e1462762e61a..15211b84436e265e0308e53e465235d499c04246 100644 (file)
--- a/apps/ca.c
+++ b/apps/ca.c
 #include <openssl/ocsp.h>
 #include <openssl/pem.h>
 
-#ifdef OPENSSL_SYS_WINDOWS
-#define strcasecmp _stricmp
-#else
-#  ifdef NO_STRINGS_H
-    int        strcasecmp();
-#  else
-#    include <strings.h>
-#  endif /* NO_STRINGS_H */
-#endif
-
 #ifndef W_OK
 #  ifdef OPENSSL_SYS_VMS
 #    if defined(__DECC)
 #define ENV_NEW_CERTS_DIR      "new_certs_dir"
 #define ENV_CERTIFICATE        "certificate"
 #define ENV_SERIAL             "serial"
+#define ENV_CRLNUMBER          "crlnumber"
 #define ENV_CRL                        "crl"
 #define ENV_PRIVATE_KEY                "private_key"
 #define ENV_RANDFILE           "RANDFILE"
@@ -277,6 +268,7 @@ int MAIN(int argc, char **argv)
        char *outfile=NULL;
        char *outdir=NULL;
        char *serialfile=NULL;
+       char *crlnumberfile=NULL;
        char *extensions=NULL;
        char *extfile=NULL;
        char *subj=NULL;
@@ -285,6 +277,7 @@ int MAIN(int argc, char **argv)
        int rev_type = REV_NONE;
        char *rev_arg = NULL;
        BIGNUM *serial=NULL;
+       BIGNUM *crlnumber=NULL;
        char *startdate=NULL;
        char *enddate=NULL;
        long days=0;
@@ -1243,21 +1236,7 @@ bad:
 
                        BIO_printf(bio_err,"Write out database with %d new entries\n",sk_X509_num(cert_sk));
 
-                       if(strlen(serialfile) > BSIZE-5 || strlen(dbfile) > BSIZE-5)
-                               {
-                               BIO_printf(bio_err,"file name too long\n");
-                               goto err;
-                               }
-
-                       strcpy(buf[0],serialfile);
-
-#ifdef OPENSSL_SYS_VMS
-                       strcat(buf[0],"-new");
-#else
-                       strcat(buf[0],".new");
-#endif
-
-                       if (!save_serial(buf[0],serial,NULL)) goto err;
+                       if (!save_serial(serialfile,"new",serial,NULL)) goto err;
 
                        if (!save_index(dbfile, "new", db)) goto err;
                        }
@@ -1317,34 +1296,7 @@ bad:
                if (sk_X509_num(cert_sk))
                        {
                        /* Rename the database and the serial file */
-                       strncpy(buf[2],serialfile,BSIZE-4);
-                       buf[2][BSIZE-4]='\0';
-
-#ifdef OPENSSL_SYS_VMS
-                       strcat(buf[2],"-old");
-#else
-                       strcat(buf[2],".old");
-#endif
-
-                       BIO_free(in);
-                       BIO_free_all(out);
-                       in=NULL;
-                       out=NULL;
-                       if (rename(serialfile,buf[2]) < 0)
-                               {
-                               BIO_printf(bio_err,"unable to rename %s to %s\n",
-                                       serialfile,buf[2]);
-                               perror("reason");
-                               goto err;
-                               }
-                       if (rename(buf[0],serialfile) < 0)
-                               {
-                               BIO_printf(bio_err,"unable to rename %s to %s\n",
-                                       buf[0],serialfile);
-                               perror("reason");
-                               rename(buf[2],serialfile);
-                               goto err;
-                               }
+                       if (!rotate_serial(serialfile,"new","old")) goto err;
 
                        if (!rotate_index(dbfile,"new","old")) goto err;
 
@@ -1378,6 +1330,14 @@ bad:
                                }
                        }
 
+               if ((crlnumberfile=NCONF_get_string(conf,section,ENV_CRLNUMBER))
+                       != NULL)
+                       if ((crlnumber=load_serial(crlnumberfile,0,NULL)) == NULL)
+                               {
+                               BIO_printf(bio_err,"error while loading CRL number\n");
+                               goto err;
+                               }
+
                if (!crldays && !crlhours)
                        {
                        if (!NCONF_get_number(conf,section,
@@ -1459,14 +1419,24 @@ bad:
 
                /* Add any extensions asked for */
 
-               if (crl_ext)
+               if (crl_ext || crlnumberfile != NULL)
                        {
                        X509V3_CTX crlctx;
                        X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
                        X509V3_set_nconf(&crlctx, conf);
 
-                       if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
-                               crl_ext, crl)) goto err;
+                       if (crl_ext)
+                               if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
+                                       crl_ext, crl)) goto err;
+                       if (crlnumberfile != NULL)
+                               {
+                               tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
+                               if (!tmpser) goto err;
+                               X509_CRL_add1_ext_i2d(crl,NID_crl_number,tmpser,0,0);
+                               ASN1_INTEGER_free(tmpser);
+                               crl_v2 = 1;
+                               if (!BN_add_word(crlnumber,1)) goto err;
+                               }
                        }
                if (crl_ext || crl_v2)
                        {
@@ -1474,9 +1444,17 @@ bad:
                                goto err; /* version 2 CRL */
                        }
 
+               
+               if (crlnumberfile != NULL)      /* we have a CRL number that need updating */
+                       if (!save_serial(crlnumberfile,"new",crlnumber,NULL)) goto err;
+
                if (!X509_CRL_sign(crl,pkey,dgst)) goto err;
 
                PEM_write_bio_X509_CRL(Sout,crl);
+
+               if (crlnumberfile != NULL)      /* Rename the crlnumber file */
+                       if (!rotate_serial(crlnumberfile,"new","old")) goto err;
+
                }
        /*****************************************************************/
        if (dorevoke)
@@ -3027,7 +3005,8 @@ int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_G
        char *tmp = NULL;
        char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
        int reason_code = -1;
-       int i, ret = 0;
+       int ret = 0;
+       unsigned int i;
        ASN1_OBJECT *hold = NULL;
        ASN1_GENERALIZEDTIME *comp_time = NULL;
        tmp = BUF_strdup(str);
@@ -3127,4 +3106,3 @@ int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_G
 
        return ret;
        }
-