fast reduction for NIST curves
[openssl.git] / apps / ca.c
index d60001b0188e53047411e32cd6c79dfea98d3d9b..1242d373220046dc662f0d2deeea7fe196e465fc 100644 (file)
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -64,7 +64,6 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include "apps.h"
 #include <openssl/conf.h>
 #include <openssl/bio.h>
 #include <openssl/err.h>
@@ -99,6 +98,8 @@
 #  endif
 #endif
 
+#include "apps.h"
+
 #ifndef W_OK
 #  define F_OK 0
 #  define X_OK 1
@@ -1158,9 +1159,14 @@ bad:
                        }
                if (verbose)
                        {
-                       if ((f=BN_bn2hex(serial)) == NULL) goto err;
-                       BIO_printf(bio_err,"next serial number is %s\n",f);
-                       OPENSSL_free(f);
+                       if (BN_is_zero(serial))
+                               BIO_printf(bio_err,"next serial number is 00\n");
+                       else
+                               {
+                               if ((f=BN_bn2hex(serial)) == NULL) goto err;
+                               BIO_printf(bio_err,"next serial number is %s\n",f);
+                               OPENSSL_free(f);
+                               }
                        }
 
                if ((attribs=NCONF_get_section(conf,policy)) == NULL)
@@ -2094,7 +2100,10 @@ again2:
                        }
                }
 
-       row[DB_serial]=BN_bn2hex(serial);
+       if (BN_is_zero(serial))
+               row[DB_serial]=BUF_strdup("00");
+       else
+               row[DB_serial]=BN_bn2hex(serial);
        if (row[DB_serial] == NULL)
                {
                BIO_printf(bio_err,"Memory allocation failure\n");
@@ -2588,7 +2597,10 @@ static int do_revoke(X509 *x509, TXT_DB *db, int type, char *value)
                row[i]=NULL;
        row[DB_name]=X509_NAME_oneline(X509_get_subject_name(x509),NULL,0);
        bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509),NULL);
-       row[DB_serial]=BN_bn2hex(bn);
+       if (BN_is_zero(bn))
+               row[DB_serial]=BUF_strdup("00");
+       else
+               row[DB_serial]=BN_bn2hex(bn);
        BN_free(bn);
        if ((row[DB_name] == NULL) || (row[DB_serial] == NULL))
                {
@@ -3044,68 +3056,72 @@ X509_NAME *do_subject(char *subject, long chtype)
        int nid;
 
        if (!buf || !ne_types || !ne_values)
-       {
+               {
                BIO_printf(bio_err, "malloc error\n");
                goto error;
-       }
+               }       
 
        if (*subject != '/')
-       {
+               {
                BIO_printf(bio_err, "Subject does not start with '/'.\n");
                goto error;
-       }
+               }
        sp++; /* skip leading / */
 
        while (*sp)
-       {
+               {
                /* collect type */
                ne_types[ne_num] = bp;
                while (*sp)
-               {
+                       {
                        if (*sp == '\\') /* is there anything to escape in the type...? */
+                               {
                                if (*++sp)
                                        *bp++ = *sp++;
-                               else
-                               {
+                               else    
+                                       {
                                        BIO_printf(bio_err, "escape character at end of string\n");
                                        goto error;
-                               }
+                                       }
+                               }       
                        else if (*sp == '=')
-                       {
+                               {
                                sp++;
                                *bp++ = '\0';
                                break;
-                       }
+                               }
                        else
                                *bp++ = *sp++;
-               }
+                       }
                if (!*sp)
-               {
+                       {
                        BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
                        goto error;
-               }
+                       }
                ne_values[ne_num] = bp;
                while (*sp)
-               {
+                       {
                        if (*sp == '\\')
+                               {
                                if (*++sp)
                                        *bp++ = *sp++;
                                else
-                               {
+                                       {
                                        BIO_printf(bio_err, "escape character at end of string\n");
                                        goto error;
+                                       }
                                }
                        else if (*sp == '/')
-                       {
+                               {
                                sp++;
                                break;
-                       }
+                               }
                        else
                                *bp++ = *sp++;
-               }
+                       }
                *bp++ = '\0';
                ne_num++;
-       }
+               }       
 
        if (!(n = X509_NAME_new()))
                goto error;