2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
65 #include <sys/types.h>
67 #include <openssl/conf.h>
68 #include <openssl/bio.h>
69 #include <openssl/err.h>
70 #include <openssl/bn.h>
71 #include <openssl/txt_db.h>
72 #include <openssl/evp.h>
73 #include <openssl/x509.h>
74 #include <openssl/x509v3.h>
75 #include <openssl/objects.h>
76 #include <openssl/ocsp.h>
77 #include <openssl/pem.h>
80 # ifdef OPENSSL_SYS_VMS
86 # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS)
87 # include <sys/file.h>
103 #define BASE_SECTION "ca"
104 #define CONFIG_FILE "openssl.cnf"
106 #define ENV_DEFAULT_CA "default_ca"
108 #define ENV_DIR "dir"
109 #define ENV_CERTS "certs"
110 #define ENV_CRL_DIR "crl_dir"
111 #define ENV_CA_DB "CA_DB"
112 #define ENV_NEW_CERTS_DIR "new_certs_dir"
113 #define ENV_CERTIFICATE "certificate"
114 #define ENV_SERIAL "serial"
115 #define ENV_CRLNUMBER "crlnumber"
116 #define ENV_CRL "crl"
117 #define ENV_PRIVATE_KEY "private_key"
118 #define ENV_RANDFILE "RANDFILE"
119 #define ENV_DEFAULT_DAYS "default_days"
120 #define ENV_DEFAULT_STARTDATE "default_startdate"
121 #define ENV_DEFAULT_ENDDATE "default_enddate"
122 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
123 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
124 #define ENV_DEFAULT_MD "default_md"
125 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
126 #define ENV_PRESERVE "preserve"
127 #define ENV_POLICY "policy"
128 #define ENV_EXTENSIONS "x509_extensions"
129 #define ENV_CRLEXT "crl_extensions"
130 #define ENV_MSIE_HACK "msie_hack"
131 #define ENV_NAMEOPT "name_opt"
132 #define ENV_CERTOPT "cert_opt"
133 #define ENV_EXTCOPY "copy_extensions"
135 #define ENV_DATABASE "database"
137 /* Additional revocation information types */
139 #define REV_NONE 0 /* No addditional information */
140 #define REV_CRL_REASON 1 /* Value is CRL reason code */
141 #define REV_HOLD 2 /* Value is hold instruction */
142 #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
143 #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
145 static char *ca_usage[]={
148 " -verbose - Talk alot while doing things\n",
149 " -config file - A config file\n",
150 " -name arg - The particular CA definition to use\n",
151 " -gencrl - Generate a new CRL\n",
152 " -crldays days - Days is when the next CRL is due\n",
153 " -crlhours hours - Hours is when the next CRL is due\n",
154 " -startdate YYMMDDHHMMSSZ - certificate validity notBefore\n",
155 " -enddate YYMMDDHHMMSSZ - certificate validity notAfter (overrides -days)\n",
156 " -days arg - number of days to certify the certificate for\n",
157 " -md arg - md to use, one of md2, md5, sha or sha1\n",
158 " -policy arg - The CA 'policy' to support\n",
159 " -keyfile arg - private key file\n",
160 " -keyform arg - private key file format (PEM or ENGINE)\n",
161 " -key arg - key to decode the private key if it is encrypted\n",
162 " -cert file - The CA certificate\n",
163 " -in file - The input PEM encoded certificate request(s)\n",
164 " -out file - Where to put the output file(s)\n",
165 " -outdir dir - Where to put output certificates\n",
166 " -infiles .... - The last argument, requests to process\n",
167 " -spkac file - File contains DN and signed public key and challenge\n",
168 " -ss_cert file - File contains a self signed cert to sign\n",
169 " -preserveDN - Don't re-order the DN\n",
170 " -noemailDN - Don't add the EMAIL field into certificate' subject\n",
171 " -batch - Don't ask questions\n",
172 " -msie_hack - msie modifications to handle all those universal strings\n",
173 " -revoke file - Revoke a certificate (given in file)\n",
174 " -subj arg - Use arg instead of request's subject\n",
175 " -extensions .. - Extension section (override value in config file)\n",
176 " -extfile file - Configuration file with X509v3 extentions to add\n",
177 " -crlexts .. - CRL extension section (override value in config file)\n",
178 #ifndef OPENSSL_NO_ENGINE
179 " -engine e - use engine e, possibly a hardware device.\n",
181 " -status serial - Shows certificate status given the serial number\n",
182 " -updatedb - Updates db for expired certificates\n",
187 extern int EF_PROTECT_FREE;
188 extern int EF_PROTECT_BELOW;
189 extern int EF_ALIGNMENT;
192 static void lookup_fail(char *name,char *tag);
193 static int certify(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
194 const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,CA_DB *db,
195 BIGNUM *serial, char *subj, int email_dn, char *startdate,
196 char *enddate, long days, int batch, char *ext_sect, CONF *conf,
197 int verbose, unsigned long certopt, unsigned long nameopt,
198 int default_op, int ext_copy);
199 static int certify_cert(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
200 const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
201 CA_DB *db, BIGNUM *serial, char *subj, int email_dn,
202 char *startdate, char *enddate, long days, int batch,
203 char *ext_sect, CONF *conf,int verbose, unsigned long certopt,
204 unsigned long nameopt, int default_op, int ext_copy,
206 static int certify_spkac(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
207 const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
208 CA_DB *db, BIGNUM *serial,char *subj, int email_dn,
209 char *startdate, char *enddate, long days, char *ext_sect,
210 CONF *conf, int verbose, unsigned long certopt,
211 unsigned long nameopt, int default_op, int ext_copy);
212 static int fix_data(int nid, int *type);
213 static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext);
214 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
215 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,char *subj,
216 int email_dn, char *startdate, char *enddate, long days, int batch,
217 int verbose, X509_REQ *req, char *ext_sect, CONF *conf,
218 unsigned long certopt, unsigned long nameopt, int default_op,
220 static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
221 static int get_certificate_status(const char *ser_status, CA_DB *db);
222 static int do_updatedb(CA_DB *db);
223 static int check_time_format(char *str);
224 char *make_revocation_str(int rev_type, char *rev_arg);
225 int make_revoked(X509_REVOKED *rev, char *str);
226 int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
227 static CONF *conf=NULL;
228 static CONF *extconf=NULL;
229 static char *section=NULL;
231 static int preserve=0;
232 static int msie_hack=0;
235 int MAIN(int, char **);
237 int MAIN(int argc, char **argv)
240 char *key=NULL,*passargin=NULL;
256 char *configfile=NULL;
261 int keyform=FORMAT_PEM;
263 char *spkac_file=NULL;
264 char *ss_cert_file=NULL;
265 char *ser_status=NULL;
270 char *serialfile=NULL;
271 char *crlnumberfile=NULL;
272 char *extensions=NULL;
275 char *tmp_email_dn=NULL;
277 int rev_type = REV_NONE;
278 char *rev_arg = NULL;
280 BIGNUM *crlnumber=NULL;
281 char *startdate=NULL;
286 unsigned long nameopt = 0, certopt = 0;
288 int ext_copy = EXT_COPY_NONE;
291 BIO *in=NULL,*out=NULL,*Sout=NULL,*Cout=NULL;
295 X509_REVOKED *r=NULL;
297 ASN1_INTEGER *tmpser;
300 const EVP_MD *dgst=NULL;
301 STACK_OF(CONF_VALUE) *attribs=NULL;
302 STACK_OF(X509) *cert_sk=NULL;
305 MS_STATIC char buf[3][BSIZE];
307 #ifndef OPENSSL_NO_ENGINE
328 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
329 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
335 if (strcmp(*argv,"-verbose") == 0)
337 else if (strcmp(*argv,"-config") == 0)
339 if (--argc < 1) goto bad;
340 configfile= *(++argv);
342 else if (strcmp(*argv,"-name") == 0)
344 if (--argc < 1) goto bad;
347 else if (strcmp(*argv,"-subj") == 0)
349 if (--argc < 1) goto bad;
353 else if (strcmp(*argv,"-startdate") == 0)
355 if (--argc < 1) goto bad;
356 startdate= *(++argv);
358 else if (strcmp(*argv,"-enddate") == 0)
360 if (--argc < 1) goto bad;
363 else if (strcmp(*argv,"-days") == 0)
365 if (--argc < 1) goto bad;
366 days=atoi(*(++argv));
368 else if (strcmp(*argv,"-md") == 0)
370 if (--argc < 1) goto bad;
373 else if (strcmp(*argv,"-policy") == 0)
375 if (--argc < 1) goto bad;
378 else if (strcmp(*argv,"-keyfile") == 0)
380 if (--argc < 1) goto bad;
383 else if (strcmp(*argv,"-keyform") == 0)
385 if (--argc < 1) goto bad;
386 keyform=str2fmt(*(++argv));
388 else if (strcmp(*argv,"-passin") == 0)
390 if (--argc < 1) goto bad;
391 passargin= *(++argv);
393 else if (strcmp(*argv,"-key") == 0)
395 if (--argc < 1) goto bad;
398 else if (strcmp(*argv,"-cert") == 0)
400 if (--argc < 1) goto bad;
403 else if (strcmp(*argv,"-in") == 0)
405 if (--argc < 1) goto bad;
409 else if (strcmp(*argv,"-out") == 0)
411 if (--argc < 1) goto bad;
414 else if (strcmp(*argv,"-outdir") == 0)
416 if (--argc < 1) goto bad;
419 else if (strcmp(*argv,"-notext") == 0)
421 else if (strcmp(*argv,"-batch") == 0)
423 else if (strcmp(*argv,"-preserveDN") == 0)
425 else if (strcmp(*argv,"-noemailDN") == 0)
427 else if (strcmp(*argv,"-gencrl") == 0)
429 else if (strcmp(*argv,"-msie_hack") == 0)
431 else if (strcmp(*argv,"-crldays") == 0)
433 if (--argc < 1) goto bad;
434 crldays= atol(*(++argv));
436 else if (strcmp(*argv,"-crlhours") == 0)
438 if (--argc < 1) goto bad;
439 crlhours= atol(*(++argv));
441 else if (strcmp(*argv,"-infiles") == 0)
448 else if (strcmp(*argv, "-ss_cert") == 0)
450 if (--argc < 1) goto bad;
451 ss_cert_file = *(++argv);
454 else if (strcmp(*argv, "-spkac") == 0)
456 if (--argc < 1) goto bad;
457 spkac_file = *(++argv);
460 else if (strcmp(*argv,"-revoke") == 0)
462 if (--argc < 1) goto bad;
466 else if (strcmp(*argv,"-extensions") == 0)
468 if (--argc < 1) goto bad;
469 extensions= *(++argv);
471 else if (strcmp(*argv,"-extfile") == 0)
473 if (--argc < 1) goto bad;
476 else if (strcmp(*argv,"-status") == 0)
478 if (--argc < 1) goto bad;
479 ser_status= *(++argv);
481 else if (strcmp(*argv,"-updatedb") == 0)
485 else if (strcmp(*argv,"-crlexts") == 0)
487 if (--argc < 1) goto bad;
490 else if (strcmp(*argv,"-crl_reason") == 0)
492 if (--argc < 1) goto bad;
494 rev_type = REV_CRL_REASON;
496 else if (strcmp(*argv,"-crl_hold") == 0)
498 if (--argc < 1) goto bad;
502 else if (strcmp(*argv,"-crl_compromise") == 0)
504 if (--argc < 1) goto bad;
506 rev_type = REV_KEY_COMPROMISE;
508 else if (strcmp(*argv,"-crl_CA_compromise") == 0)
510 if (--argc < 1) goto bad;
512 rev_type = REV_CA_COMPROMISE;
514 #ifndef OPENSSL_NO_ENGINE
515 else if (strcmp(*argv,"-engine") == 0)
517 if (--argc < 1) goto bad;
524 BIO_printf(bio_err,"unknown option %s\n",*argv);
534 for (pp=ca_usage; (*pp != NULL); pp++)
535 BIO_printf(bio_err,"%s",*pp);
539 ERR_load_crypto_strings();
541 /*****************************************************************/
543 if (configfile == NULL) configfile = getenv("OPENSSL_CONF");
544 if (configfile == NULL) configfile = getenv("SSLEAY_CONF");
545 if (configfile == NULL)
547 const char *s=X509_get_default_cert_area();
550 #ifdef OPENSSL_SYS_VMS
551 len = strlen(s)+sizeof(CONFIG_FILE);
552 tofree=OPENSSL_malloc(len);
555 len = strlen(s)+sizeof(CONFIG_FILE)+1;
556 tofree=OPENSSL_malloc(len);
557 BUF_strlcpy(tofree,s,len);
558 BUF_strlcat(tofree,"/",len);
560 BUF_strlcat(tofree,CONFIG_FILE,len);
564 BIO_printf(bio_err,"Using configuration from %s\n",configfile);
565 conf = NCONF_new(NULL);
566 if (NCONF_load(conf,configfile,&errorline) <= 0)
569 BIO_printf(bio_err,"error loading the config file '%s'\n",
572 BIO_printf(bio_err,"error on line %ld of config file '%s'\n"
573 ,errorline,configfile);
578 OPENSSL_free(tofree);
582 if (!load_config(bio_err, conf))
585 #ifndef OPENSSL_NO_ENGINE
586 e = setup_engine(bio_err, engine, 0);
589 /* Lets get the config section we are using */
592 section=NCONF_get_string(conf,BASE_SECTION,ENV_DEFAULT_CA);
595 lookup_fail(BASE_SECTION,ENV_DEFAULT_CA);
602 p=NCONF_get_string(conf,NULL,"oid_file");
609 oid_bio=BIO_new_file(p,"r");
613 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
614 ERR_print_errors(bio_err);
620 OBJ_create_objects(oid_bio);
624 if (!add_oid_section(bio_err,conf))
626 ERR_print_errors(bio_err);
631 randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
632 if (randfile == NULL)
634 app_RAND_load_file(randfile, bio_err, 0);
636 db_attr.unique_subject = 1;
637 p = NCONF_get_string(conf, section, "unique_subject");
641 BIO_printf(bio_err, "DEBUG: unique_subject = \"%s\"\n", p);
645 case 'f': /* false */
646 case 'F': /* FALSE */
649 db_attr.unique_subject = 0;
656 db_attr.unique_subject = 1;
664 BIO_printf(bio_err, "DEBUG: unique_subject undefined\n", p);
667 BIO_printf(bio_err, "DEBUG: configured unique_subject is %d\n",
668 db_attr.unique_subject);
671 in=BIO_new(BIO_s_file());
672 out=BIO_new(BIO_s_file());
673 Sout=BIO_new(BIO_s_file());
674 Cout=BIO_new(BIO_s_file());
675 if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL))
677 ERR_print_errors(bio_err);
681 /*****************************************************************/
682 /* report status of cert with serial number given on command line */
685 if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
687 lookup_fail(section,ENV_DATABASE);
690 db = load_index(dbfile,&db_attr);
691 if (db == NULL) goto err;
693 if (!index_index(db)) goto err;
695 if (get_certificate_status(ser_status,db) != 1)
696 BIO_printf(bio_err,"Error verifying serial %s!\n",
701 /*****************************************************************/
702 /* we definitely need a public key, so let's get it */
704 if ((keyfile == NULL) && ((keyfile=NCONF_get_string(conf,
705 section,ENV_PRIVATE_KEY)) == NULL))
707 lookup_fail(section,ENV_PRIVATE_KEY);
713 if (!app_passwd(bio_err, passargin, NULL, &key, NULL))
715 BIO_printf(bio_err,"Error getting password\n");
719 pkey = load_key(bio_err, keyfile, keyform, 0, key, e,
721 if (key) OPENSSL_cleanse(key,strlen(key));
724 /* load_key() has already printed an appropriate message */
728 /*****************************************************************/
729 /* we need a certificate */
730 if ((certfile == NULL) && ((certfile=NCONF_get_string(conf,
731 section,ENV_CERTIFICATE)) == NULL))
733 lookup_fail(section,ENV_CERTIFICATE);
736 x509=load_cert(bio_err, certfile, FORMAT_PEM, NULL, e,
741 if (!X509_check_private_key(x509,pkey))
743 BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
747 f=NCONF_get_string(conf,BASE_SECTION,ENV_PRESERVE);
750 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
752 f=NCONF_get_string(conf,BASE_SECTION,ENV_MSIE_HACK);
755 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
758 f=NCONF_get_string(conf,section,ENV_NAMEOPT);
762 if (!set_name_ex(&nameopt, f))
764 BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
772 f=NCONF_get_string(conf,section,ENV_CERTOPT);
776 if (!set_cert_ex(&certopt, f))
778 BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
786 f=NCONF_get_string(conf,section,ENV_EXTCOPY);
790 if (!set_ext_copy(&ext_copy, f))
792 BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
799 /*****************************************************************/
800 /* lookup where to write new certificates */
801 if ((outdir == NULL) && (req))
805 if ((outdir=NCONF_get_string(conf,section,ENV_NEW_CERTS_DIR))
808 BIO_printf(bio_err,"there needs to be defined a directory for new certificate to be placed in\n");
811 #ifndef OPENSSL_SYS_VMS
812 /* outdir is a directory spec, but access() for VMS demands a
813 filename. In any case, stat(), below, will catch the problem
814 if outdir is not a directory spec, and the fopen() or open()
815 will catch an error if there is no write access.
817 Presumably, this problem could also be solved by using the DEC
818 C routines to convert the directory syntax to Unixly, and give
819 that to access(). However, time's too short to do that just
822 if (access(outdir,R_OK|W_OK|X_OK) != 0)
824 BIO_printf(bio_err,"I am unable to access the %s directory\n",outdir);
829 if (stat(outdir,&sb) != 0)
831 BIO_printf(bio_err,"unable to stat(%s)\n",outdir);
836 if (!(sb.st_mode & S_IFDIR))
838 BIO_printf(bio_err,"%s need to be a directory\n",outdir);
846 /*****************************************************************/
847 /* we need to load the database file */
848 if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
850 lookup_fail(section,ENV_DATABASE);
853 db = load_index(dbfile, &db_attr);
854 if (db == NULL) goto err;
856 /* Lets check some fields */
857 for (i=0; i<sk_num(db->db->data); i++)
859 pp=(char **)sk_value(db->db->data,i);
860 if ((pp[DB_type][0] != DB_TYPE_REV) &&
861 (pp[DB_rev_date][0] != '\0'))
863 BIO_printf(bio_err,"entry %d: not revoked yet, but has a revocation date\n",i+1);
866 if ((pp[DB_type][0] == DB_TYPE_REV) &&
867 !make_revoked(NULL, pp[DB_rev_date]))
869 BIO_printf(bio_err," in entry %d\n", i+1);
872 if (!check_time_format(pp[DB_exp_date]))
874 BIO_printf(bio_err,"entry %d: invalid expiry date\n",i+1);
884 if ((j&1) || (j < 2))
886 BIO_printf(bio_err,"entry %d: bad serial number length (%d)\n",i+1,j);
891 if (!( ((*p >= '0') && (*p <= '9')) ||
892 ((*p >= 'A') && (*p <= 'F')) ||
893 ((*p >= 'a') && (*p <= 'f'))) )
895 BIO_printf(bio_err,"entry %d: bad serial number characters, char pos %ld, char is '%c'\n",i+1,(long)(p-pp[DB_serial]),*p);
903 BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT); /* cannot fail */
904 #ifdef OPENSSL_SYS_VMS
906 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
907 out = BIO_push(tmpbio, out);
910 TXT_DB_write(out,db->db);
911 BIO_printf(bio_err,"%d entries loaded from the database\n",
913 BIO_printf(bio_err,"generating index\n");
916 if (!index_index(db)) goto err;
918 /*****************************************************************/
919 /* Update the db file for expired certificates */
923 BIO_printf(bio_err, "Updating %s ...\n",
929 BIO_printf(bio_err,"Malloc failure\n");
934 if (verbose) BIO_printf(bio_err,
935 "No entries found to mark expired\n");
939 if (!save_index(dbfile,"new",db)) goto err;
941 if (!rotate_index(dbfile,"new","old")) goto err;
943 if (verbose) BIO_printf(bio_err,
944 "Done. %d entries marked as expired\n",i);
948 /*****************************************************************/
949 /* Read extentions config file */
952 extconf = NCONF_new(NULL);
953 if (NCONF_load(extconf,extfile,&errorline) <= 0)
956 BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
959 BIO_printf(bio_err, "ERROR: on line %ld of config file '%s'\n",
966 BIO_printf(bio_err, "Successfully loaded extensions file %s\n", extfile);
968 /* We can have sections in the ext file */
969 if (!extensions && !(extensions = NCONF_get_string(extconf, "default", "extensions")))
970 extensions = "default";
973 /*****************************************************************/
978 if (BIO_write_filename(Sout,outfile) <= 0)
986 BIO_set_fp(Sout,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
987 #ifdef OPENSSL_SYS_VMS
989 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
990 Sout = BIO_push(tmpbio, Sout);
996 if ((md == NULL) && ((md=NCONF_get_string(conf,
997 section,ENV_DEFAULT_MD)) == NULL))
999 lookup_fail(section,ENV_DEFAULT_MD);
1003 if ((dgst=EVP_get_digestbyname(md)) == NULL)
1005 BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
1011 if ((email_dn == 1) && ((tmp_email_dn=NCONF_get_string(conf,
1012 section,ENV_DEFAULT_EMAIL_DN)) != NULL ))
1014 if(strcmp(tmp_email_dn,"no") == 0)
1018 BIO_printf(bio_err,"message digest is %s\n",
1019 OBJ_nid2ln(dgst->type));
1020 if ((policy == NULL) && ((policy=NCONF_get_string(conf,
1021 section,ENV_POLICY)) == NULL))
1023 lookup_fail(section,ENV_POLICY);
1027 BIO_printf(bio_err,"policy is %s\n",policy);
1029 if ((serialfile=NCONF_get_string(conf,section,ENV_SERIAL))
1032 lookup_fail(section,ENV_SERIAL);
1038 /* no '-extfile' option, so we look for extensions
1039 * in the main configuration file */
1042 extensions=NCONF_get_string(conf,section,
1049 /* Check syntax of file */
1051 X509V3_set_ctx_test(&ctx);
1052 X509V3_set_nconf(&ctx, conf);
1053 if (!X509V3_EXT_add_nconf(conf, &ctx, extensions,
1057 "Error Loading extension section %s\n",
1065 if (startdate == NULL)
1067 startdate=NCONF_get_string(conf,section,
1068 ENV_DEFAULT_STARTDATE);
1069 if (startdate == NULL)
1072 if (startdate && !ASN1_UTCTIME_set_string(NULL,startdate))
1074 BIO_printf(bio_err,"start date is invalid, it should be YYMMDDHHMMSSZ\n");
1077 if (startdate == NULL) startdate="today";
1079 if (enddate == NULL)
1081 enddate=NCONF_get_string(conf,section,
1082 ENV_DEFAULT_ENDDATE);
1083 if (enddate == NULL)
1086 if (enddate && !ASN1_UTCTIME_set_string(NULL,enddate))
1088 BIO_printf(bio_err,"end date is invalid, it should be YYMMDDHHMMSSZ\n");
1094 if(!NCONF_get_number(conf,section, ENV_DEFAULT_DAYS, &days))
1097 if (!enddate && (days == 0))
1099 BIO_printf(bio_err,"cannot lookup how many days to certify for\n");
1103 if ((serial=load_serial(serialfile, create_ser, NULL)) == NULL)
1105 BIO_printf(bio_err,"error while loading serial number\n");
1110 if (BN_is_zero(serial))
1111 BIO_printf(bio_err,"next serial number is 00\n");
1114 if ((f=BN_bn2hex(serial)) == NULL) goto err;
1115 BIO_printf(bio_err,"next serial number is %s\n",f);
1120 if ((attribs=NCONF_get_section(conf,policy)) == NULL)
1122 BIO_printf(bio_err,"unable to find 'section' for %s\n",policy);
1126 if ((cert_sk=sk_X509_new_null()) == NULL)
1128 BIO_printf(bio_err,"Memory allocation failure\n");
1131 if (spkac_file != NULL)
1134 j=certify_spkac(&x,spkac_file,pkey,x509,dgst,attribs,db,
1135 serial,subj,email_dn,startdate,enddate,days,extensions,
1136 conf,verbose,certopt,nameopt,default_op,ext_copy);
1137 if (j < 0) goto err;
1141 BIO_printf(bio_err,"\n");
1142 if (!BN_add_word(serial,1)) goto err;
1143 if (!sk_X509_push(cert_sk,x))
1145 BIO_printf(bio_err,"Memory allocation failure\n");
1155 if (ss_cert_file != NULL)
1158 j=certify_cert(&x,ss_cert_file,pkey,x509,dgst,attribs,
1159 db,serial,subj,email_dn,startdate,enddate,days,batch,
1160 extensions,conf,verbose, certopt, nameopt,
1161 default_op, ext_copy, e);
1162 if (j < 0) goto err;
1166 BIO_printf(bio_err,"\n");
1167 if (!BN_add_word(serial,1)) goto err;
1168 if (!sk_X509_push(cert_sk,x))
1170 BIO_printf(bio_err,"Memory allocation failure\n");
1178 j=certify(&x,infile,pkey,x509,dgst,attribs,db,
1179 serial,subj,email_dn,startdate,enddate,days,batch,
1180 extensions,conf,verbose, certopt, nameopt,
1181 default_op, ext_copy);
1182 if (j < 0) goto err;
1186 BIO_printf(bio_err,"\n");
1187 if (!BN_add_word(serial,1)) goto err;
1188 if (!sk_X509_push(cert_sk,x))
1190 BIO_printf(bio_err,"Memory allocation failure\n");
1195 for (i=0; i<argc; i++)
1198 j=certify(&x,argv[i],pkey,x509,dgst,attribs,db,
1199 serial,subj,email_dn,startdate,enddate,days,batch,
1200 extensions,conf,verbose, certopt, nameopt,
1201 default_op, ext_copy);
1202 if (j < 0) goto err;
1206 BIO_printf(bio_err,"\n");
1207 if (!BN_add_word(serial,1)) goto err;
1208 if (!sk_X509_push(cert_sk,x))
1210 BIO_printf(bio_err,"Memory allocation failure\n");
1215 /* we have a stack of newly certified certificates
1216 * and a data base and serial number that need
1219 if (sk_X509_num(cert_sk) > 0)
1223 BIO_printf(bio_err,"\n%d out of %d certificate requests certified, commit? [y/n]",total_done,total);
1224 (void)BIO_flush(bio_err);
1226 fgets(buf[0],10,stdin);
1227 if ((buf[0][0] != 'y') && (buf[0][0] != 'Y'))
1229 BIO_printf(bio_err,"CERTIFICATION CANCELED\n");
1235 BIO_printf(bio_err,"Write out database with %d new entries\n",sk_X509_num(cert_sk));
1237 if (!save_serial(serialfile,"new",serial,NULL)) goto err;
1239 if (!save_index(dbfile, "new", db)) goto err;
1243 BIO_printf(bio_err,"writing new certificates\n");
1244 for (i=0; i<sk_X509_num(cert_sk); i++)
1249 x=sk_X509_value(cert_sk,i);
1251 j=x->cert_info->serialNumber->length;
1252 p=(char *)x->cert_info->serialNumber->data;
1254 if(strlen(outdir) >= (size_t)(j ? BSIZE-j*2-6 : BSIZE-8))
1256 BIO_printf(bio_err,"certificate file name too long\n");
1260 strcpy(buf[2],outdir);
1262 #ifndef OPENSSL_SYS_VMS
1263 BUF_strlcat(buf[2],"/",sizeof(buf[2]));
1266 n=(char *)&(buf[2][strlen(buf[2])]);
1271 if (n >= &(buf[2][sizeof(buf[2])]))
1274 &buf[2][0] + sizeof(buf[2]) - n,
1275 "%02X",(unsigned char)*(p++));
1284 *(n++)='.'; *(n++)='p'; *(n++)='e'; *(n++)='m';
1287 BIO_printf(bio_err,"writing %s\n",buf[2]);
1289 if (BIO_write_filename(Cout,buf[2]) <= 0)
1294 write_new_certificate(Cout,x, 0, notext);
1295 write_new_certificate(Sout,x, output_der, notext);
1298 if (sk_X509_num(cert_sk))
1300 /* Rename the database and the serial file */
1301 if (!rotate_serial(serialfile,"new","old")) goto err;
1303 if (!rotate_index(dbfile,"new","old")) goto err;
1305 BIO_printf(bio_err,"Data Base Updated\n");
1309 /*****************************************************************/
1315 crl_ext=NCONF_get_string(conf,section,ENV_CRLEXT);
1321 /* Check syntax of file */
1323 X509V3_set_ctx_test(&ctx);
1324 X509V3_set_nconf(&ctx, conf);
1325 if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL))
1328 "Error Loading CRL extension section %s\n",
1335 if ((crlnumberfile=NCONF_get_string(conf,section,ENV_CRLNUMBER))
1337 if ((crlnumber=load_serial(crlnumberfile,0,NULL)) == NULL)
1339 BIO_printf(bio_err,"error while loading CRL number\n");
1343 if (!crldays && !crlhours)
1345 if (!NCONF_get_number(conf,section,
1346 ENV_DEFAULT_CRL_DAYS, &crldays))
1348 if (!NCONF_get_number(conf,section,
1349 ENV_DEFAULT_CRL_HOURS, &crlhours))
1352 if ((crldays == 0) && (crlhours == 0))
1354 BIO_printf(bio_err,"cannot lookup how long until the next CRL is issued\n");
1358 if (verbose) BIO_printf(bio_err,"making CRL\n");
1359 if ((crl=X509_CRL_new()) == NULL) goto err;
1360 if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) goto err;
1362 tmptm = ASN1_TIME_new();
1363 if (!tmptm) goto err;
1364 X509_gmtime_adj(tmptm,0);
1365 X509_CRL_set_lastUpdate(crl, tmptm);
1366 X509_gmtime_adj(tmptm,(crldays*24+crlhours)*60*60);
1367 X509_CRL_set_nextUpdate(crl, tmptm);
1369 ASN1_TIME_free(tmptm);
1371 for (i=0; i<sk_num(db->db->data); i++)
1373 pp=(char **)sk_value(db->db->data,i);
1374 if (pp[DB_type][0] == DB_TYPE_REV)
1376 if ((r=X509_REVOKED_new()) == NULL) goto err;
1377 j = make_revoked(r, pp[DB_rev_date]);
1379 if (j == 2) crl_v2 = 1;
1380 if (!BN_hex2bn(&serial, pp[DB_serial]))
1382 tmpser = BN_to_ASN1_INTEGER(serial, NULL);
1387 X509_REVOKED_set_serialNumber(r, tmpser);
1388 ASN1_INTEGER_free(tmpser);
1389 X509_CRL_add0_revoked(crl,r);
1393 /* sort the data so it will be written in serial
1397 /* we now have a CRL */
1398 if (verbose) BIO_printf(bio_err,"signing CRL\n");
1399 #ifndef OPENSSL_NO_DSA
1400 if (pkey->type == EVP_PKEY_DSA)
1404 /* Add any extensions asked for */
1406 if (crl_ext || crlnumberfile != NULL)
1409 X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
1410 X509V3_set_nconf(&crlctx, conf);
1413 if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
1414 crl_ext, crl)) goto err;
1415 if (crlnumberfile != NULL)
1417 tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
1418 if (!tmpser) goto err;
1419 X509_CRL_add1_ext_i2d(crl,NID_crl_number,tmpser,0,0);
1420 ASN1_INTEGER_free(tmpser);
1422 if (!BN_add_word(crlnumber,1)) goto err;
1425 if (crl_ext || crl_v2)
1427 if (!X509_CRL_set_version(crl, 1))
1428 goto err; /* version 2 CRL */
1432 if (crlnumberfile != NULL) /* we have a CRL number that need updating */
1433 if (!save_serial(crlnumberfile,"new",crlnumber,NULL)) goto err;
1435 if (!X509_CRL_sign(crl,pkey,dgst)) goto err;
1437 PEM_write_bio_X509_CRL(Sout,crl);
1439 if (crlnumberfile != NULL) /* Rename the crlnumber file */
1440 if (!rotate_serial(crlnumberfile,"new","old")) goto err;
1443 /*****************************************************************/
1448 BIO_printf(bio_err,"no input files\n");
1454 revcert=load_cert(bio_err, infile, FORMAT_PEM,
1456 if (revcert == NULL)
1458 j=do_revoke(revcert,db, rev_type, rev_arg);
1459 if (j <= 0) goto err;
1462 if (!save_index(dbfile, "new", db)) goto err;
1464 if (!rotate_index(dbfile, "new", "old")) goto err;
1466 BIO_printf(bio_err,"Data Base Updated\n");
1469 /*****************************************************************/
1473 OPENSSL_free(tofree);
1480 sk_X509_pop_free(cert_sk,X509_free);
1482 if (ret) ERR_print_errors(bio_err);
1483 app_RAND_write_file(randfile, bio_err);
1484 if (free_key && key)
1488 EVP_PKEY_free(pkey);
1497 static void lookup_fail(char *name, char *tag)
1499 BIO_printf(bio_err,"variable lookup failed for %s::%s\n",name,tag);
1502 static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1503 const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1504 BIGNUM *serial, char *subj, int email_dn, char *startdate, char *enddate,
1505 long days, int batch, char *ext_sect, CONF *lconf, int verbose,
1506 unsigned long certopt, unsigned long nameopt, int default_op,
1511 EVP_PKEY *pktmp=NULL;
1514 in=BIO_new(BIO_s_file());
1516 if (BIO_read_filename(in,infile) <= 0)
1521 if ((req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL)) == NULL)
1523 BIO_printf(bio_err,"Error reading certificate request in %s\n",
1528 X509_REQ_print(bio_err,req);
1530 BIO_printf(bio_err,"Check that the request matches the signature\n");
1532 if ((pktmp=X509_REQ_get_pubkey(req)) == NULL)
1534 BIO_printf(bio_err,"error unpacking public key\n");
1537 i=X509_REQ_verify(req,pktmp);
1538 EVP_PKEY_free(pktmp);
1542 BIO_printf(bio_err,"Signature verification problems....\n");
1548 BIO_printf(bio_err,"Signature did not match the certificate request\n");
1552 BIO_printf(bio_err,"Signature ok\n");
1554 ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj, email_dn,
1555 startdate,enddate,days,batch,verbose,req,ext_sect,lconf,
1556 certopt, nameopt, default_op, ext_copy);
1559 if (req != NULL) X509_REQ_free(req);
1560 if (in != NULL) BIO_free(in);
1564 static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1565 const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1566 BIGNUM *serial, char *subj, int email_dn, char *startdate, char *enddate,
1567 long days, int batch, char *ext_sect, CONF *lconf, int verbose,
1568 unsigned long certopt, unsigned long nameopt, int default_op,
1569 int ext_copy, ENGINE *e)
1572 X509_REQ *rreq=NULL;
1573 EVP_PKEY *pktmp=NULL;
1576 if ((req=load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile)) == NULL)
1579 X509_print(bio_err,req);
1581 BIO_printf(bio_err,"Check that the request matches the signature\n");
1583 if ((pktmp=X509_get_pubkey(req)) == NULL)
1585 BIO_printf(bio_err,"error unpacking public key\n");
1588 i=X509_verify(req,pktmp);
1589 EVP_PKEY_free(pktmp);
1593 BIO_printf(bio_err,"Signature verification problems....\n");
1599 BIO_printf(bio_err,"Signature did not match the certificate\n");
1603 BIO_printf(bio_err,"Signature ok\n");
1605 if ((rreq=X509_to_X509_REQ(req,NULL,EVP_md5())) == NULL)
1608 ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,email_dn,startdate,enddate,
1609 days,batch,verbose,rreq,ext_sect,lconf, certopt, nameopt, default_op,
1613 if (rreq != NULL) X509_REQ_free(rreq);
1614 if (req != NULL) X509_free(req);
1618 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
1619 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
1620 int email_dn, char *startdate, char *enddate, long days, int batch,
1621 int verbose, X509_REQ *req, char *ext_sect, CONF *lconf,
1622 unsigned long certopt, unsigned long nameopt, int default_op,
1625 X509_NAME *name=NULL,*CAname=NULL,*subject=NULL, *dn_subject=NULL;
1626 ASN1_UTCTIME *tm,*tmptm;
1627 ASN1_STRING *str,*str2;
1631 X509_NAME_ENTRY *ne;
1632 X509_NAME_ENTRY *tne,*push;
1634 int ok= -1,i,j,last,nid;
1637 char *row[DB_NUMBER],**rrow=NULL,**irow=NULL;
1640 tmptm=ASN1_UTCTIME_new();
1643 BIO_printf(bio_err,"malloc error\n");
1647 for (i=0; i<DB_NUMBER; i++)
1652 X509_NAME *n = do_subject(subj, MBSTRING_ASC);
1656 ERR_print_errors(bio_err);
1659 X509_REQ_set_subject_name(req,n);
1660 req->req_info->enc.modified = 1;
1665 BIO_printf(bio_err,"The Subject's Distinguished Name is as follows\n");
1667 name=X509_REQ_get_subject_name(req);
1668 for (i=0; i<X509_NAME_entry_count(name); i++)
1670 ne= X509_NAME_get_entry(name,i);
1671 str=X509_NAME_ENTRY_get_data(ne);
1672 obj=X509_NAME_ENTRY_get_object(ne);
1676 /* assume all type should be strings */
1677 nid=OBJ_obj2nid(ne->object);
1679 if (str->type == V_ASN1_UNIVERSALSTRING)
1680 ASN1_UNIVERSALSTRING_to_string(str);
1682 if ((str->type == V_ASN1_IA5STRING) &&
1683 (nid != NID_pkcs9_emailAddress))
1684 str->type=V_ASN1_T61STRING;
1686 if ((nid == NID_pkcs9_emailAddress) &&
1687 (str->type == V_ASN1_PRINTABLESTRING))
1688 str->type=V_ASN1_IA5STRING;
1691 /* If no EMAIL is wanted in the subject */
1692 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
1695 /* check some things */
1696 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
1697 (str->type != V_ASN1_IA5STRING))
1699 BIO_printf(bio_err,"\nemailAddress type needs to be of type IA5STRING\n");
1702 if ((str->type != V_ASN1_BMPSTRING) && (str->type != V_ASN1_UTF8STRING))
1704 j=ASN1_PRINTABLE_type(str->data,str->length);
1705 if ( ((j == V_ASN1_T61STRING) &&
1706 (str->type != V_ASN1_T61STRING)) ||
1707 ((j == V_ASN1_IA5STRING) &&
1708 (str->type == V_ASN1_PRINTABLESTRING)))
1710 BIO_printf(bio_err,"\nThe string contains characters that are illegal for the ASN.1 type\n");
1716 old_entry_print(bio_err, obj, str);
1719 /* Ok, now we check the 'policy' stuff. */
1720 if ((subject=X509_NAME_new()) == NULL)
1722 BIO_printf(bio_err,"Memory allocation failure\n");
1726 /* take a copy of the issuer name before we mess with it. */
1727 CAname=X509_NAME_dup(x509->cert_info->subject);
1728 if (CAname == NULL) goto err;
1731 for (i=0; i<sk_CONF_VALUE_num(policy); i++)
1733 cv=sk_CONF_VALUE_value(policy,i); /* get the object id */
1734 if ((j=OBJ_txt2nid(cv->name)) == NID_undef)
1736 BIO_printf(bio_err,"%s:unknown object type in 'policy' configuration\n",cv->name);
1744 /* lookup the object in the supplied name list */
1745 j=X509_NAME_get_index_by_OBJ(name,obj,last);
1748 if (last != -1) break;
1753 tne=X509_NAME_get_entry(name,j);
1757 /* depending on the 'policy', decide what to do. */
1759 if (strcmp(cv->value,"optional") == 0)
1764 else if (strcmp(cv->value,"supplied") == 0)
1768 BIO_printf(bio_err,"The %s field needed to be supplied and was missing\n",cv->name);
1774 else if (strcmp(cv->value,"match") == 0)
1780 BIO_printf(bio_err,"The mandatory %s field was missing\n",cv->name);
1787 j=X509_NAME_get_index_by_OBJ(CAname,obj,last2);
1788 if ((j < 0) && (last2 == -1))
1790 BIO_printf(bio_err,"The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",cv->name);
1795 push=X509_NAME_get_entry(CAname,j);
1796 str=X509_NAME_ENTRY_get_data(tne);
1797 str2=X509_NAME_ENTRY_get_data(push);
1799 if (ASN1_STRING_cmp(str,str2) != 0)
1804 BIO_printf(bio_err,"The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",cv->name,((str2 == NULL)?"NULL":(char *)str2->data),((str == NULL)?"NULL":(char *)str->data));
1810 BIO_printf(bio_err,"%s:invalid type in 'policy' configuration\n",cv->value);
1816 if (!X509_NAME_add_entry(subject,push, -1, 0))
1819 X509_NAME_ENTRY_free(push);
1820 BIO_printf(bio_err,"Memory allocation failure\n");
1830 X509_NAME_free(subject);
1831 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1832 subject=X509_NAME_dup(name);
1833 if (subject == NULL) goto err;
1837 BIO_printf(bio_err,"The subject name appears to be ok, checking data base for clashes\n");
1839 /* Build the correct Subject if no e-mail is wanted in the subject */
1840 /* and add it later on because of the method extensions are added (altName) */
1843 dn_subject = subject;
1846 X509_NAME_ENTRY *tmpne;
1847 /* Its best to dup the subject DN and then delete any email
1848 * addresses because this retains its structure.
1850 if (!(dn_subject = X509_NAME_dup(subject)))
1852 BIO_printf(bio_err,"Memory allocation failure\n");
1855 while((i = X509_NAME_get_index_by_NID(dn_subject,
1856 NID_pkcs9_emailAddress, -1)) >= 0)
1858 tmpne = X509_NAME_get_entry(dn_subject, i);
1859 X509_NAME_delete_entry(dn_subject, i);
1860 X509_NAME_ENTRY_free(tmpne);
1864 if (BN_is_zero(serial))
1865 row[DB_serial]=BUF_strdup("00");
1867 row[DB_serial]=BN_bn2hex(serial);
1868 if (row[DB_serial] == NULL)
1870 BIO_printf(bio_err,"Memory allocation failure\n");
1874 if (db->attributes.unique_subject)
1876 rrow=TXT_DB_get_by_index(db->db,DB_name,row);
1880 "ERROR:There is already a certificate for %s\n",
1886 rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
1889 BIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\n",
1891 BIO_printf(bio_err," check the database/serial_file for corruption\n");
1898 "The matching entry has the following details\n");
1899 if (rrow[DB_type][0] == 'E')
1901 else if (rrow[DB_type][0] == 'R')
1903 else if (rrow[DB_type][0] == 'V')
1906 p="\ninvalid type, Data base error\n";
1907 BIO_printf(bio_err,"Type :%s\n",p);;
1908 if (rrow[DB_type][0] == 'R')
1910 p=rrow[DB_exp_date]; if (p == NULL) p="undef";
1911 BIO_printf(bio_err,"Was revoked on:%s\n",p);
1913 p=rrow[DB_exp_date]; if (p == NULL) p="undef";
1914 BIO_printf(bio_err,"Expires on :%s\n",p);
1915 p=rrow[DB_serial]; if (p == NULL) p="undef";
1916 BIO_printf(bio_err,"Serial Number :%s\n",p);
1917 p=rrow[DB_file]; if (p == NULL) p="undef";
1918 BIO_printf(bio_err,"File name :%s\n",p);
1919 p=rrow[DB_name]; if (p == NULL) p="undef";
1920 BIO_printf(bio_err,"Subject Name :%s\n",p);
1921 ok= -1; /* This is now a 'bad' error. */
1925 /* We are now totally happy, lets make and sign the certificate */
1927 BIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\n");
1929 if ((ret=X509_new()) == NULL) goto err;
1933 /* Make it an X509 v3 certificate. */
1934 if (!X509_set_version(ret,2)) goto err;
1937 if (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL)
1939 if (!X509_set_issuer_name(ret,X509_get_subject_name(x509)))
1942 if (strcmp(startdate,"today") == 0)
1943 X509_gmtime_adj(X509_get_notBefore(ret),0);
1944 else ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate);
1946 if (enddate == NULL)
1947 X509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days);
1948 else ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate);
1950 if (!X509_set_subject_name(ret,subject)) goto err;
1952 pktmp=X509_REQ_get_pubkey(req);
1953 i = X509_set_pubkey(ret,pktmp);
1954 EVP_PKEY_free(pktmp);
1957 /* Lets add the extensions, if there are any */
1961 if (ci->version == NULL)
1962 if ((ci->version=ASN1_INTEGER_new()) == NULL)
1964 ASN1_INTEGER_set(ci->version,2); /* version 3 certificate */
1966 /* Free the current entries if any, there should not
1967 * be any I believe */
1968 if (ci->extensions != NULL)
1969 sk_X509_EXTENSION_pop_free(ci->extensions,
1970 X509_EXTENSION_free);
1972 ci->extensions = NULL;
1974 /* Initialize the context structure */
1975 X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
1980 BIO_printf(bio_err, "Extra configuration file found\n");
1982 /* Use the extconf configuration db LHASH */
1983 X509V3_set_nconf(&ctx, extconf);
1985 /* Test the structure (needed?) */
1986 /* X509V3_set_ctx_test(&ctx); */
1988 /* Adds exts contained in the configuration file */
1989 if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect,ret))
1992 "ERROR: adding extensions in section %s\n",
1994 ERR_print_errors(bio_err);
1998 BIO_printf(bio_err, "Successfully added extensions from file.\n");
2002 /* We found extensions to be set from config file */
2003 X509V3_set_nconf(&ctx, lconf);
2005 if(!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret))
2007 BIO_printf(bio_err, "ERROR: adding extensions in section %s\n", ext_sect);
2008 ERR_print_errors(bio_err);
2013 BIO_printf(bio_err, "Successfully added extensions from config\n");
2017 /* Copy extensions from request (if any) */
2019 if (!copy_extensions(ret, req, ext_copy))
2021 BIO_printf(bio_err, "ERROR: adding extensions from request\n");
2022 ERR_print_errors(bio_err);
2026 /* Set the right value for the noemailDN option */
2029 if (!X509_set_subject_name(ret,dn_subject)) goto err;
2034 BIO_printf(bio_err, "Certificate Details:\n");
2035 /* Never print signature details because signature not present */
2036 certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
2037 X509_print_ex(bio_err, ret, nameopt, certopt);
2040 BIO_printf(bio_err,"Certificate is to be certified until ");
2041 ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret));
2042 if (days) BIO_printf(bio_err," (%d days)",days);
2043 BIO_printf(bio_err, "\n");
2048 BIO_printf(bio_err,"Sign the certificate? [y/n]:");
2049 (void)BIO_flush(bio_err);
2051 fgets(buf,sizeof(buf)-1,stdin);
2052 if (!((buf[0] == 'y') || (buf[0] == 'Y')))
2054 BIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\n");
2061 #ifndef OPENSSL_NO_DSA
2062 if (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1();
2063 pktmp=X509_get_pubkey(ret);
2064 if (EVP_PKEY_missing_parameters(pktmp) &&
2065 !EVP_PKEY_missing_parameters(pkey))
2066 EVP_PKEY_copy_parameters(pktmp,pkey);
2067 EVP_PKEY_free(pktmp);
2070 if (!X509_sign(ret,pkey,dgst))
2073 /* We now just add it to the database */
2074 row[DB_type]=(char *)OPENSSL_malloc(2);
2076 tm=X509_get_notAfter(ret);
2077 row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
2078 memcpy(row[DB_exp_date],tm->data,tm->length);
2079 row[DB_exp_date][tm->length]='\0';
2081 row[DB_rev_date]=NULL;
2083 /* row[DB_serial] done already */
2084 row[DB_file]=(char *)OPENSSL_malloc(8);
2085 row[DB_name]=X509_NAME_oneline(X509_get_subject_name(ret),NULL,0);
2087 if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2088 (row[DB_file] == NULL) || (row[DB_name] == NULL))
2090 BIO_printf(bio_err,"Memory allocation failure\n");
2093 BUF_strlcpy(row[DB_file],"unknown",8);
2094 row[DB_type][0]='V';
2095 row[DB_type][1]='\0';
2097 if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
2099 BIO_printf(bio_err,"Memory allocation failure\n");
2103 for (i=0; i<DB_NUMBER; i++)
2108 irow[DB_NUMBER]=NULL;
2110 if (!TXT_DB_insert(db->db,irow))
2112 BIO_printf(bio_err,"failed to update database\n");
2113 BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
2118 for (i=0; i<DB_NUMBER; i++)
2119 if (row[i] != NULL) OPENSSL_free(row[i]);
2122 X509_NAME_free(CAname);
2123 if (subject != NULL)
2124 X509_NAME_free(subject);
2125 if ((dn_subject != NULL) && !email_dn)
2126 X509_NAME_free(dn_subject);
2128 ASN1_UTCTIME_free(tmptm);
2131 if (ret != NULL) X509_free(ret);
2139 static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
2144 (void)i2d_X509_bio(bp,x);
2148 /* ??? Not needed since X509_print prints all this stuff anyway */
2149 f=X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
2150 BIO_printf(bp,"issuer :%s\n",f);
2152 f=X509_NAME_oneline(X509_get_subject_name(x),buf,256);
2153 BIO_printf(bp,"subject:%s\n",f);
2155 BIO_puts(bp,"serial :");
2156 i2a_ASN1_INTEGER(bp,x->cert_info->serialNumber);
2157 BIO_puts(bp,"\n\n");
2159 if (!notext)X509_print(bp,x);
2160 PEM_write_bio_X509(bp,x);
2163 static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
2164 const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
2165 BIGNUM *serial, char *subj, int email_dn, char *startdate, char *enddate,
2166 long days, char *ext_sect, CONF *lconf, int verbose, unsigned long certopt,
2167 unsigned long nameopt, int default_op, int ext_copy)
2169 STACK_OF(CONF_VALUE) *sk=NULL;
2172 CONF_VALUE *cv=NULL;
2173 NETSCAPE_SPKI *spki = NULL;
2176 EVP_PKEY *pktmp=NULL;
2178 X509_NAME_ENTRY *ne=NULL;
2184 * Load input file into a hash table. (This is just an easy
2185 * way to read and parse the file, then put it into a convenient
2188 parms=CONF_load(NULL,infile,&errline);
2191 BIO_printf(bio_err,"error on line %ld of %s\n",errline,infile);
2192 ERR_print_errors(bio_err);
2196 sk=CONF_get_section(parms, "default");
2197 if (sk_CONF_VALUE_num(sk) == 0)
2199 BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
2205 * Now create a dummy X509 request structure. We don't actually
2206 * have an X509 request, but we have many of the components
2207 * (a public key, various DN components). The idea is that we
2208 * put these components into the right X509 request structure
2209 * and we can use the same code as if you had a real X509 request.
2214 ERR_print_errors(bio_err);
2219 * Build up the subject name set.
2226 if (sk_CONF_VALUE_num(sk) <= i) break;
2228 cv=sk_CONF_VALUE_value(sk,i);
2230 /* Skip past any leading X. X: X, etc to allow for
2231 * multiple instances
2233 for (buf = cv->name; *buf ; buf++)
2234 if ((*buf == ':') || (*buf == ',') || (*buf == '.'))
2237 if (*buf) type = buf;
2242 if ((nid=OBJ_txt2nid(type)) == NID_undef)
2244 if (strcmp(type, "SPKAC") == 0)
2246 spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
2249 BIO_printf(bio_err,"unable to load Netscape SPKAC structure\n");
2250 ERR_print_errors(bio_err);
2258 if ((nid == NID_pkcs9_emailAddress) && (email_dn == 0))
2262 j=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
2263 if (fix_data(nid, &j) == 0)
2266 "invalid characters in string %s\n",buf);
2270 if ((ne=X509_NAME_ENTRY_create_by_NID(&ne,nid,j,
2271 (unsigned char *)buf,
2272 strlen(buf))) == NULL)
2275 if (!X509_NAME_add_entry(n,ne,-1, 0)) goto err;
2279 BIO_printf(bio_err,"Netscape SPKAC structure not found in %s\n",
2285 * Now extract the key from the SPKI structure.
2288 BIO_printf(bio_err,"Check that the SPKAC request matches the signature\n");
2290 if ((pktmp=NETSCAPE_SPKI_get_pubkey(spki)) == NULL)
2292 BIO_printf(bio_err,"error unpacking SPKAC public key\n");
2296 j = NETSCAPE_SPKI_verify(spki, pktmp);
2299 BIO_printf(bio_err,"signature verification failed on SPKAC public key\n");
2302 BIO_printf(bio_err,"Signature ok\n");
2304 X509_REQ_set_pubkey(req,pktmp);
2305 EVP_PKEY_free(pktmp);
2306 ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,email_dn,startdate,enddate,
2307 days,1,verbose,req,ext_sect,lconf, certopt, nameopt, default_op,
2310 if (req != NULL) X509_REQ_free(req);
2311 if (parms != NULL) CONF_free(parms);
2312 if (spki != NULL) NETSCAPE_SPKI_free(spki);
2313 if (ne != NULL) X509_NAME_ENTRY_free(ne);
2318 static int fix_data(int nid, int *type)
2320 if (nid == NID_pkcs9_emailAddress)
2321 *type=V_ASN1_IA5STRING;
2322 if ((nid == NID_commonName) && (*type == V_ASN1_IA5STRING))
2323 *type=V_ASN1_T61STRING;
2324 if ((nid == NID_pkcs9_challengePassword) && (*type == V_ASN1_IA5STRING))
2325 *type=V_ASN1_T61STRING;
2326 if ((nid == NID_pkcs9_unstructuredName) && (*type == V_ASN1_T61STRING))
2328 if (nid == NID_pkcs9_unstructuredName)
2329 *type=V_ASN1_IA5STRING;
2333 static int check_time_format(char *str)
2337 tm.data=(unsigned char *)str;
2338 tm.length=strlen(str);
2339 tm.type=V_ASN1_UTCTIME;
2340 return(ASN1_UTCTIME_check(&tm));
2343 static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
2345 ASN1_UTCTIME *tm=NULL;
2346 char *row[DB_NUMBER],**rrow,**irow;
2347 char *rev_str = NULL;
2351 for (i=0; i<DB_NUMBER; i++)
2353 row[DB_name]=X509_NAME_oneline(X509_get_subject_name(x509),NULL,0);
2354 bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509),NULL);
2356 row[DB_serial]=BUF_strdup("00");
2358 row[DB_serial]=BN_bn2hex(bn);
2360 if ((row[DB_name] == NULL) || (row[DB_serial] == NULL))
2362 BIO_printf(bio_err,"Memory allocation failure\n");
2365 /* We have to lookup by serial number because name lookup
2366 * skips revoked certs
2368 rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
2371 BIO_printf(bio_err,"Adding Entry with serial number %s to DB for %s\n", row[DB_serial], row[DB_name]);
2373 /* We now just add it to the database */
2374 row[DB_type]=(char *)OPENSSL_malloc(2);
2376 tm=X509_get_notAfter(x509);
2377 row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
2378 memcpy(row[DB_exp_date],tm->data,tm->length);
2379 row[DB_exp_date][tm->length]='\0';
2381 row[DB_rev_date]=NULL;
2383 /* row[DB_serial] done already */
2384 row[DB_file]=(char *)OPENSSL_malloc(8);
2386 /* row[DB_name] done already */
2388 if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2389 (row[DB_file] == NULL))
2391 BIO_printf(bio_err,"Memory allocation failure\n");
2394 BUF_strlcpy(row[DB_file],"unknown",8);
2395 row[DB_type][0]='V';
2396 row[DB_type][1]='\0';
2398 if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
2400 BIO_printf(bio_err,"Memory allocation failure\n");
2404 for (i=0; i<DB_NUMBER; i++)
2409 irow[DB_NUMBER]=NULL;
2411 if (!TXT_DB_insert(db->db,irow))
2413 BIO_printf(bio_err,"failed to update database\n");
2414 BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
2418 /* Revoke Certificate */
2419 ok = do_revoke(x509,db, type, value);
2424 else if (index_name_cmp((const char **)row,(const char **)rrow))
2426 BIO_printf(bio_err,"ERROR:name does not match %s\n",
2430 else if (rrow[DB_type][0]=='R')
2432 BIO_printf(bio_err,"ERROR:Already revoked, serial number %s\n",
2438 BIO_printf(bio_err,"Revoking Certificate %s.\n", rrow[DB_serial]);
2439 rev_str = make_revocation_str(type, value);
2442 BIO_printf(bio_err, "Error in revocation arguments\n");
2445 rrow[DB_type][0]='R';
2446 rrow[DB_type][1]='\0';
2447 rrow[DB_rev_date] = rev_str;
2451 for (i=0; i<DB_NUMBER; i++)
2454 OPENSSL_free(row[i]);
2459 static int get_certificate_status(const char *serial, CA_DB *db)
2461 char *row[DB_NUMBER],**rrow;
2464 /* Free Resources */
2465 for (i=0; i<DB_NUMBER; i++)
2468 /* Malloc needed char spaces */
2469 row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
2470 if (row[DB_serial] == NULL)
2472 BIO_printf(bio_err,"Malloc failure\n");
2476 if (strlen(serial) % 2)
2478 /* Set the first char to 0 */;
2479 row[DB_serial][0]='0';
2481 /* Copy String from serial to row[DB_serial] */
2482 memcpy(row[DB_serial]+1, serial, strlen(serial));
2483 row[DB_serial][strlen(serial)+1]='\0';
2487 /* Copy String from serial to row[DB_serial] */
2488 memcpy(row[DB_serial], serial, strlen(serial));
2489 row[DB_serial][strlen(serial)]='\0';
2492 /* Make it Upper Case */
2493 for (i=0; row[DB_serial][i] != '\0'; i++)
2494 row[DB_serial][i] = toupper(row[DB_serial][i]);
2499 /* Search for the certificate */
2500 rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
2503 BIO_printf(bio_err,"Serial %s not present in db.\n",
2508 else if (rrow[DB_type][0]=='V')
2510 BIO_printf(bio_err,"%s=Valid (%c)\n",
2511 row[DB_serial], rrow[DB_type][0]);
2514 else if (rrow[DB_type][0]=='R')
2516 BIO_printf(bio_err,"%s=Revoked (%c)\n",
2517 row[DB_serial], rrow[DB_type][0]);
2520 else if (rrow[DB_type][0]=='E')
2522 BIO_printf(bio_err,"%s=Expired (%c)\n",
2523 row[DB_serial], rrow[DB_type][0]);
2526 else if (rrow[DB_type][0]=='S')
2528 BIO_printf(bio_err,"%s=Suspended (%c)\n",
2529 row[DB_serial], rrow[DB_type][0]);
2534 BIO_printf(bio_err,"%s=Unknown (%c).\n",
2535 row[DB_serial], rrow[DB_type][0]);
2539 for (i=0; i<DB_NUMBER; i++)
2542 OPENSSL_free(row[i]);
2547 static int do_updatedb (CA_DB *db)
2549 ASN1_UTCTIME *a_tm = NULL;
2551 int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
2552 char **rrow, *a_tm_s;
2554 a_tm = ASN1_UTCTIME_new();
2556 /* get actual time and make a string */
2557 a_tm = X509_gmtime_adj(a_tm, 0);
2558 a_tm_s = (char *) OPENSSL_malloc(a_tm->length+1);
2565 memcpy(a_tm_s, a_tm->data, a_tm->length);
2566 a_tm_s[a_tm->length] = '\0';
2568 if (strncmp(a_tm_s, "49", 2) <= 0)
2573 for (i = 0; i < sk_num(db->db->data); i++)
2575 rrow = (char **) sk_value(db->db->data, i);
2577 if (rrow[DB_type][0] == 'V')
2579 /* ignore entries that are not valid */
2580 if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
2585 if (db_y2k == a_y2k)
2587 /* all on the same y2k side */
2588 if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0)
2590 rrow[DB_type][0] = 'E';
2591 rrow[DB_type][1] = '\0';
2594 BIO_printf(bio_err, "%s=Expired\n",
2598 else if (db_y2k < a_y2k)
2600 rrow[DB_type][0] = 'E';
2601 rrow[DB_type][1] = '\0';
2604 BIO_printf(bio_err, "%s=Expired\n",
2613 ASN1_UTCTIME_free(a_tm);
2614 OPENSSL_free(a_tm_s);
2619 static char *crl_reasons[] = {
2620 /* CRL reason strings */
2624 "affiliationChanged",
2626 "cessationOfOperation",
2629 /* Additional pseudo reasons */
2635 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2637 /* Given revocation information convert to a DB string.
2638 * The format of the string is:
2639 * revtime[,reason,extra]. Where 'revtime' is the
2640 * revocation time (the current time). 'reason' is the
2641 * optional CRL reason and 'extra' is any additional
2645 char *make_revocation_str(int rev_type, char *rev_arg)
2647 char *reason = NULL, *other = NULL, *str;
2649 ASN1_UTCTIME *revtm = NULL;
2656 case REV_CRL_REASON:
2657 for (i = 0; i < 8; i++)
2659 if (!strcasecmp(rev_arg, crl_reasons[i]))
2661 reason = crl_reasons[i];
2667 BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
2673 /* Argument is an OID */
2675 otmp = OBJ_txt2obj(rev_arg, 0);
2676 ASN1_OBJECT_free(otmp);
2680 BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
2684 reason = "holdInstruction";
2688 case REV_KEY_COMPROMISE:
2689 case REV_CA_COMPROMISE:
2691 /* Argument is the key compromise time */
2692 if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg))
2694 BIO_printf(bio_err, "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", rev_arg);
2698 if (rev_type == REV_KEY_COMPROMISE)
2701 reason = "CAkeyTime";
2707 revtm = X509_gmtime_adj(NULL, 0);
2709 i = revtm->length + 1;
2711 if (reason) i += strlen(reason) + 1;
2712 if (other) i += strlen(other) + 1;
2714 str = OPENSSL_malloc(i);
2716 if (!str) return NULL;
2718 BUF_strlcpy(str, (char *)revtm->data, i);
2721 BUF_strlcat(str, ",", i);
2722 BUF_strlcat(str, reason, i);
2726 BUF_strlcat(str, ",", i);
2727 BUF_strlcat(str, other, i);
2729 ASN1_UTCTIME_free(revtm);
2733 /* Convert revocation field to X509_REVOKED entry
2737 * 2 OK and some extensions added (i.e. V2 CRL)
2741 int make_revoked(X509_REVOKED *rev, char *str)
2744 int reason_code = -1;
2746 ASN1_OBJECT *hold = NULL;
2747 ASN1_GENERALIZEDTIME *comp_time = NULL;
2748 ASN1_ENUMERATED *rtmp = NULL;
2750 ASN1_TIME *revDate = NULL;
2752 i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
2757 if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
2760 if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS))
2762 rtmp = ASN1_ENUMERATED_new();
2763 if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
2765 if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
2769 if (rev && comp_time)
2771 if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0))
2776 if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0))
2780 if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
2786 if (tmp) OPENSSL_free(tmp);
2787 ASN1_OBJECT_free(hold);
2788 ASN1_GENERALIZEDTIME_free(comp_time);
2789 ASN1_ENUMERATED_free(rtmp);
2790 ASN1_TIME_free(revDate);
2796 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
2797 * where characters may be escaped by \
2799 X509_NAME *do_subject(char *subject, long chtype)
2801 size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
2802 char *buf = OPENSSL_malloc(buflen);
2803 size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
2804 char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
2805 char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
2807 char *sp = subject, *bp = buf;
2810 X509_NAME *n = NULL;
2813 if (!buf || !ne_types || !ne_values)
2815 BIO_printf(bio_err, "malloc error\n");
2819 if (*subject != '/')
2821 BIO_printf(bio_err, "Subject does not start with '/'.\n");
2824 sp++; /* skip leading / */
2829 ne_types[ne_num] = bp;
2832 if (*sp == '\\') /* is there anything to escape in the type...? */
2838 BIO_printf(bio_err, "escape character at end of string\n");
2842 else if (*sp == '=')
2853 BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
2856 ne_values[ne_num] = bp;
2865 BIO_printf(bio_err, "escape character at end of string\n");
2869 else if (*sp == '/')
2881 if (!(n = X509_NAME_new()))
2884 for (i = 0; i < ne_num; i++)
2886 if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
2888 BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
2894 BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
2898 if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,0))
2902 OPENSSL_free(ne_values);
2903 OPENSSL_free(ne_types);
2910 OPENSSL_free(ne_values);
2912 OPENSSL_free(ne_types);
2918 int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
2920 char buf[25],*pbuf, *p;
2922 j=i2a_ASN1_OBJECT(bp,obj);
2924 for (j=22-j; j>0; j--)
2930 if (str->type == V_ASN1_PRINTABLESTRING)
2931 BIO_printf(bp,"PRINTABLE:'");
2932 else if (str->type == V_ASN1_T61STRING)
2933 BIO_printf(bp,"T61STRING:'");
2934 else if (str->type == V_ASN1_IA5STRING)
2935 BIO_printf(bp,"IA5STRING:'");
2936 else if (str->type == V_ASN1_UNIVERSALSTRING)
2937 BIO_printf(bp,"UNIVERSALSTRING:'");
2939 BIO_printf(bp,"ASN.1 %2d:'",str->type);
2941 p=(char *)str->data;
2942 for (j=str->length; j>0; j--)
2944 if ((*p >= ' ') && (*p <= '~'))
2945 BIO_printf(bp,"%c",*p);
2947 BIO_printf(bp,"\\0x%02X",*p);
2948 else if ((unsigned char)*p == 0xf7)
2949 BIO_printf(bp,"^?");
2950 else BIO_printf(bp,"^%c",*p+'@');
2953 BIO_printf(bp,"'\n");
2957 int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str)
2960 char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
2961 int reason_code = -1;
2963 ASN1_OBJECT *hold = NULL;
2964 ASN1_GENERALIZEDTIME *comp_time = NULL;
2965 tmp = BUF_strdup(str);
2967 p = strchr(tmp, ',');
2986 *prevtm = ASN1_UTCTIME_new();
2987 if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
2989 BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
2995 for (i = 0; i < NUM_REASONS; i++)
2997 if(!strcasecmp(reason_str, crl_reasons[i]))
3003 if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
3005 BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
3009 if (reason_code == 7)
3010 reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
3011 else if (reason_code == 8) /* Hold instruction */
3015 BIO_printf(bio_err, "missing hold instruction\n");
3018 reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
3019 hold = OBJ_txt2obj(arg_str, 0);
3023 BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
3026 if (phold) *phold = hold;
3028 else if ((reason_code == 9) || (reason_code == 10))
3032 BIO_printf(bio_err, "missing compromised time\n");
3035 comp_time = ASN1_GENERALIZEDTIME_new();
3036 if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
3038 BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
3041 if (reason_code == 9)
3042 reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
3044 reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
3048 if (preason) *preason = reason_code;
3049 if (pinvtm) *pinvtm = comp_time;
3050 else ASN1_GENERALIZEDTIME_free(comp_time);
3056 if (tmp) OPENSSL_free(tmp);
3057 if (!phold) ASN1_OBJECT_free(hold);
3058 if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);