1ebfedea0SLionel Sambuc /* apps/ca.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc #include <stdio.h>
62ebfedea0SLionel Sambuc #include <stdlib.h>
63ebfedea0SLionel Sambuc #include <string.h>
64ebfedea0SLionel Sambuc #include <ctype.h>
65ebfedea0SLionel Sambuc #include <sys/types.h>
66ebfedea0SLionel Sambuc #include <openssl/conf.h>
67ebfedea0SLionel Sambuc #include <openssl/bio.h>
68ebfedea0SLionel Sambuc #include <openssl/err.h>
69ebfedea0SLionel Sambuc #include <openssl/bn.h>
70ebfedea0SLionel Sambuc #include <openssl/txt_db.h>
71ebfedea0SLionel Sambuc #include <openssl/evp.h>
72ebfedea0SLionel Sambuc #include <openssl/x509.h>
73ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
74ebfedea0SLionel Sambuc #include <openssl/objects.h>
75ebfedea0SLionel Sambuc #include <openssl/ocsp.h>
76ebfedea0SLionel Sambuc #include <openssl/pem.h>
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc #ifndef W_OK
79ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
80ebfedea0SLionel Sambuc # if defined(__DECC)
81ebfedea0SLionel Sambuc # include <unistd.h>
82ebfedea0SLionel Sambuc # else
83ebfedea0SLionel Sambuc # include <unixlib.h>
84ebfedea0SLionel Sambuc # endif
85ebfedea0SLionel Sambuc # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE)
86ebfedea0SLionel Sambuc # include <sys/file.h>
87ebfedea0SLionel Sambuc # endif
88ebfedea0SLionel Sambuc #endif
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc #include "apps.h"
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc #ifndef W_OK
93ebfedea0SLionel Sambuc # define F_OK 0
94ebfedea0SLionel Sambuc # define X_OK 1
95ebfedea0SLionel Sambuc # define W_OK 2
96ebfedea0SLionel Sambuc # define R_OK 4
97ebfedea0SLionel Sambuc #endif
98ebfedea0SLionel Sambuc
99ebfedea0SLionel Sambuc #undef PROG
100ebfedea0SLionel Sambuc #define PROG ca_main
101ebfedea0SLionel Sambuc
102ebfedea0SLionel Sambuc #define BASE_SECTION "ca"
103ebfedea0SLionel Sambuc #define CONFIG_FILE "openssl.cnf"
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc #define ENV_DEFAULT_CA "default_ca"
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc #define STRING_MASK "string_mask"
108ebfedea0SLionel Sambuc #define UTF8_IN "utf8"
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc #define ENV_DIR "dir"
111ebfedea0SLionel Sambuc #define ENV_CERTS "certs"
112ebfedea0SLionel Sambuc #define ENV_CRL_DIR "crl_dir"
113ebfedea0SLionel Sambuc #define ENV_CA_DB "CA_DB"
114ebfedea0SLionel Sambuc #define ENV_NEW_CERTS_DIR "new_certs_dir"
115ebfedea0SLionel Sambuc #define ENV_CERTIFICATE "certificate"
116ebfedea0SLionel Sambuc #define ENV_SERIAL "serial"
117ebfedea0SLionel Sambuc #define ENV_CRLNUMBER "crlnumber"
118ebfedea0SLionel Sambuc #define ENV_CRL "crl"
119ebfedea0SLionel Sambuc #define ENV_PRIVATE_KEY "private_key"
120ebfedea0SLionel Sambuc #define ENV_RANDFILE "RANDFILE"
121ebfedea0SLionel Sambuc #define ENV_DEFAULT_DAYS "default_days"
122ebfedea0SLionel Sambuc #define ENV_DEFAULT_STARTDATE "default_startdate"
123ebfedea0SLionel Sambuc #define ENV_DEFAULT_ENDDATE "default_enddate"
124ebfedea0SLionel Sambuc #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
125ebfedea0SLionel Sambuc #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
126ebfedea0SLionel Sambuc #define ENV_DEFAULT_MD "default_md"
127ebfedea0SLionel Sambuc #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
128ebfedea0SLionel Sambuc #define ENV_PRESERVE "preserve"
129ebfedea0SLionel Sambuc #define ENV_POLICY "policy"
130ebfedea0SLionel Sambuc #define ENV_EXTENSIONS "x509_extensions"
131ebfedea0SLionel Sambuc #define ENV_CRLEXT "crl_extensions"
132ebfedea0SLionel Sambuc #define ENV_MSIE_HACK "msie_hack"
133ebfedea0SLionel Sambuc #define ENV_NAMEOPT "name_opt"
134ebfedea0SLionel Sambuc #define ENV_CERTOPT "cert_opt"
135ebfedea0SLionel Sambuc #define ENV_EXTCOPY "copy_extensions"
136ebfedea0SLionel Sambuc #define ENV_UNIQUE_SUBJECT "unique_subject"
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc #define ENV_DATABASE "database"
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc /* Additional revocation information types */
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc #define REV_NONE 0 /* No addditional information */
143ebfedea0SLionel Sambuc #define REV_CRL_REASON 1 /* Value is CRL reason code */
144ebfedea0SLionel Sambuc #define REV_HOLD 2 /* Value is hold instruction */
145ebfedea0SLionel Sambuc #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
146ebfedea0SLionel Sambuc #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc static const char *ca_usage[] = {
149ebfedea0SLionel Sambuc "usage: ca args\n",
150ebfedea0SLionel Sambuc "\n",
151ebfedea0SLionel Sambuc " -verbose - Talk alot while doing things\n",
152ebfedea0SLionel Sambuc " -config file - A config file\n",
153ebfedea0SLionel Sambuc " -name arg - The particular CA definition to use\n",
154ebfedea0SLionel Sambuc " -gencrl - Generate a new CRL\n",
155ebfedea0SLionel Sambuc " -crldays days - Days is when the next CRL is due\n",
156ebfedea0SLionel Sambuc " -crlhours hours - Hours is when the next CRL is due\n",
157ebfedea0SLionel Sambuc " -startdate YYMMDDHHMMSSZ - certificate validity notBefore\n",
158ebfedea0SLionel Sambuc " -enddate YYMMDDHHMMSSZ - certificate validity notAfter (overrides -days)\n",
159ebfedea0SLionel Sambuc " -days arg - number of days to certify the certificate for\n",
160ebfedea0SLionel Sambuc " -md arg - md to use, one of md2, md5, sha or sha1\n",
161ebfedea0SLionel Sambuc " -policy arg - The CA 'policy' to support\n",
162ebfedea0SLionel Sambuc " -keyfile arg - private key file\n",
163ebfedea0SLionel Sambuc " -keyform arg - private key file format (PEM or ENGINE)\n",
164ebfedea0SLionel Sambuc " -key arg - key to decode the private key if it is encrypted\n",
165ebfedea0SLionel Sambuc " -cert file - The CA certificate\n",
166ebfedea0SLionel Sambuc " -selfsign - sign a certificate with the key associated with it\n",
167ebfedea0SLionel Sambuc " -in file - The input PEM encoded certificate request(s)\n",
168ebfedea0SLionel Sambuc " -out file - Where to put the output file(s)\n",
169ebfedea0SLionel Sambuc " -outdir dir - Where to put output certificates\n",
170ebfedea0SLionel Sambuc " -infiles .... - The last argument, requests to process\n",
171ebfedea0SLionel Sambuc " -spkac file - File contains DN and signed public key and challenge\n",
172ebfedea0SLionel Sambuc " -ss_cert file - File contains a self signed cert to sign\n",
173ebfedea0SLionel Sambuc " -preserveDN - Don't re-order the DN\n",
174ebfedea0SLionel Sambuc " -noemailDN - Don't add the EMAIL field into certificate' subject\n",
175ebfedea0SLionel Sambuc " -batch - Don't ask questions\n",
176ebfedea0SLionel Sambuc " -msie_hack - msie modifications to handle all those universal strings\n",
177ebfedea0SLionel Sambuc " -revoke file - Revoke a certificate (given in file)\n",
178ebfedea0SLionel Sambuc " -subj arg - Use arg instead of request's subject\n",
179ebfedea0SLionel Sambuc " -utf8 - input characters are UTF8 (default ASCII)\n",
180ebfedea0SLionel Sambuc " -multivalue-rdn - enable support for multivalued RDNs\n",
181ebfedea0SLionel Sambuc " -extensions .. - Extension section (override value in config file)\n",
182ebfedea0SLionel Sambuc " -extfile file - Configuration file with X509v3 extentions to add\n",
183ebfedea0SLionel Sambuc " -crlexts .. - CRL extension section (override value in config file)\n",
184ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
185ebfedea0SLionel Sambuc " -engine e - use engine e, possibly a hardware device.\n",
186ebfedea0SLionel Sambuc #endif
187ebfedea0SLionel Sambuc " -status serial - Shows certificate status given the serial number\n",
188ebfedea0SLionel Sambuc " -updatedb - Updates db for expired certificates\n",
189ebfedea0SLionel Sambuc NULL
190ebfedea0SLionel Sambuc };
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc #ifdef EFENCE
193ebfedea0SLionel Sambuc extern int EF_PROTECT_FREE;
194ebfedea0SLionel Sambuc extern int EF_PROTECT_BELOW;
195ebfedea0SLionel Sambuc extern int EF_ALIGNMENT;
196ebfedea0SLionel Sambuc #endif
197ebfedea0SLionel Sambuc
198ebfedea0SLionel Sambuc static void lookup_fail(const char *name, const char *tag);
199ebfedea0SLionel Sambuc static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
200ebfedea0SLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
201ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
202*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
203*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate, char *enddate,
204*0a6a1f1dSLionel Sambuc long days, int batch, char *ext_sect, CONF *conf,
205ebfedea0SLionel Sambuc int verbose, unsigned long certopt, unsigned long nameopt,
206ebfedea0SLionel Sambuc int default_op, int ext_copy, int selfsign);
207ebfedea0SLionel Sambuc static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
208ebfedea0SLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
209*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
210*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
211*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate,
212*0a6a1f1dSLionel Sambuc char *enddate, long days, int batch, char *ext_sect,
213*0a6a1f1dSLionel Sambuc CONF *conf, int verbose, unsigned long certopt,
214ebfedea0SLionel Sambuc unsigned long nameopt, int default_op, int ext_copy,
215ebfedea0SLionel Sambuc ENGINE *e);
216*0a6a1f1dSLionel Sambuc static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
217*0a6a1f1dSLionel Sambuc X509 *x509, const EVP_MD *dgst,
218ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts,
219*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
220*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
221*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate,
222*0a6a1f1dSLionel Sambuc char *enddate, long days, char *ext_sect, CONF *conf,
223*0a6a1f1dSLionel Sambuc int verbose, unsigned long certopt,
224*0a6a1f1dSLionel Sambuc unsigned long nameopt, int default_op, int ext_copy);
225*0a6a1f1dSLionel Sambuc static void write_new_certificate(BIO *bp, X509 *x, int output_der,
226*0a6a1f1dSLionel Sambuc int notext);
227*0a6a1f1dSLionel Sambuc static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
228*0a6a1f1dSLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
229*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
230*0a6a1f1dSLionel Sambuc char *subj, unsigned long chtype, int multirdn,
231*0a6a1f1dSLionel Sambuc int email_dn, char *startdate, char *enddate, long days,
232*0a6a1f1dSLionel Sambuc int batch, int verbose, X509_REQ *req, char *ext_sect,
233*0a6a1f1dSLionel Sambuc CONF *conf, unsigned long certopt, unsigned long nameopt,
234*0a6a1f1dSLionel Sambuc int default_op, int ext_copy, int selfsign);
235ebfedea0SLionel Sambuc static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
236ebfedea0SLionel Sambuc static int get_certificate_status(const char *ser_status, CA_DB *db);
237ebfedea0SLionel Sambuc static int do_updatedb(CA_DB *db);
238ebfedea0SLionel Sambuc static int check_time_format(const char *str);
239ebfedea0SLionel Sambuc char *make_revocation_str(int rev_type, char *rev_arg);
240ebfedea0SLionel Sambuc int make_revoked(X509_REVOKED *rev, const char *str);
241ebfedea0SLionel Sambuc int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
242ebfedea0SLionel Sambuc static CONF *conf = NULL;
243ebfedea0SLionel Sambuc static CONF *extconf = NULL;
244ebfedea0SLionel Sambuc static char *section = NULL;
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc static int preserve = 0;
247ebfedea0SLionel Sambuc static int msie_hack = 0;
248ebfedea0SLionel Sambuc
249ebfedea0SLionel Sambuc int MAIN(int, char **);
250ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)251ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
252ebfedea0SLionel Sambuc {
253ebfedea0SLionel Sambuc ENGINE *e = NULL;
254ebfedea0SLionel Sambuc char *key = NULL, *passargin = NULL;
255ebfedea0SLionel Sambuc int create_ser = 0;
256ebfedea0SLionel Sambuc int free_key = 0;
257ebfedea0SLionel Sambuc int total = 0;
258ebfedea0SLionel Sambuc int total_done = 0;
259ebfedea0SLionel Sambuc int badops = 0;
260ebfedea0SLionel Sambuc int ret = 1;
261ebfedea0SLionel Sambuc int email_dn = 1;
262ebfedea0SLionel Sambuc int req = 0;
263ebfedea0SLionel Sambuc int verbose = 0;
264ebfedea0SLionel Sambuc int gencrl = 0;
265ebfedea0SLionel Sambuc int dorevoke = 0;
266ebfedea0SLionel Sambuc int doupdatedb = 0;
267ebfedea0SLionel Sambuc long crldays = 0;
268ebfedea0SLionel Sambuc long crlhours = 0;
269ebfedea0SLionel Sambuc long crlsec = 0;
270ebfedea0SLionel Sambuc long errorline = -1;
271ebfedea0SLionel Sambuc char *configfile = NULL;
272ebfedea0SLionel Sambuc char *md = NULL;
273ebfedea0SLionel Sambuc char *policy = NULL;
274ebfedea0SLionel Sambuc char *keyfile = NULL;
275ebfedea0SLionel Sambuc char *certfile = NULL;
276ebfedea0SLionel Sambuc int keyform = FORMAT_PEM;
277ebfedea0SLionel Sambuc char *infile = NULL;
278ebfedea0SLionel Sambuc char *spkac_file = NULL;
279ebfedea0SLionel Sambuc char *ss_cert_file = NULL;
280ebfedea0SLionel Sambuc char *ser_status = NULL;
281ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
282ebfedea0SLionel Sambuc int output_der = 0;
283ebfedea0SLionel Sambuc char *outfile = NULL;
284ebfedea0SLionel Sambuc char *outdir = NULL;
285ebfedea0SLionel Sambuc char *serialfile = NULL;
286ebfedea0SLionel Sambuc char *crlnumberfile = NULL;
287ebfedea0SLionel Sambuc char *extensions = NULL;
288ebfedea0SLionel Sambuc char *extfile = NULL;
289ebfedea0SLionel Sambuc char *subj = NULL;
290ebfedea0SLionel Sambuc unsigned long chtype = MBSTRING_ASC;
291ebfedea0SLionel Sambuc int multirdn = 0;
292ebfedea0SLionel Sambuc char *tmp_email_dn = NULL;
293ebfedea0SLionel Sambuc char *crl_ext = NULL;
294ebfedea0SLionel Sambuc int rev_type = REV_NONE;
295ebfedea0SLionel Sambuc char *rev_arg = NULL;
296ebfedea0SLionel Sambuc BIGNUM *serial = NULL;
297ebfedea0SLionel Sambuc BIGNUM *crlnumber = NULL;
298ebfedea0SLionel Sambuc char *startdate = NULL;
299ebfedea0SLionel Sambuc char *enddate = NULL;
300ebfedea0SLionel Sambuc long days = 0;
301ebfedea0SLionel Sambuc int batch = 0;
302ebfedea0SLionel Sambuc int notext = 0;
303ebfedea0SLionel Sambuc unsigned long nameopt = 0, certopt = 0;
304ebfedea0SLionel Sambuc int default_op = 1;
305ebfedea0SLionel Sambuc int ext_copy = EXT_COPY_NONE;
306ebfedea0SLionel Sambuc int selfsign = 0;
307ebfedea0SLionel Sambuc X509 *x509 = NULL, *x509p = NULL;
308ebfedea0SLionel Sambuc X509 *x = NULL;
309ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL;
310ebfedea0SLionel Sambuc char *dbfile = NULL;
311ebfedea0SLionel Sambuc CA_DB *db = NULL;
312ebfedea0SLionel Sambuc X509_CRL *crl = NULL;
313ebfedea0SLionel Sambuc X509_REVOKED *r = NULL;
314ebfedea0SLionel Sambuc ASN1_TIME *tmptm;
315ebfedea0SLionel Sambuc ASN1_INTEGER *tmpser;
316ebfedea0SLionel Sambuc char *f;
317ebfedea0SLionel Sambuc const char *p;
318ebfedea0SLionel Sambuc char *const *pp;
319ebfedea0SLionel Sambuc int i, j;
320ebfedea0SLionel Sambuc const EVP_MD *dgst = NULL;
321ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attribs = NULL;
322ebfedea0SLionel Sambuc STACK_OF(X509) *cert_sk = NULL;
323ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts = NULL;
324ebfedea0SLionel Sambuc #undef BSIZE
325ebfedea0SLionel Sambuc #define BSIZE 256
326ebfedea0SLionel Sambuc MS_STATIC char buf[3][BSIZE];
327ebfedea0SLionel Sambuc char *randfile = NULL;
328ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
329ebfedea0SLionel Sambuc char *engine = NULL;
330ebfedea0SLionel Sambuc #endif
331ebfedea0SLionel Sambuc char *tofree = NULL;
332ebfedea0SLionel Sambuc DB_ATTR db_attr;
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc #ifdef EFENCE
335ebfedea0SLionel Sambuc EF_PROTECT_FREE = 1;
336ebfedea0SLionel Sambuc EF_PROTECT_BELOW = 1;
337ebfedea0SLionel Sambuc EF_ALIGNMENT = 0;
338ebfedea0SLionel Sambuc #endif
339ebfedea0SLionel Sambuc
340ebfedea0SLionel Sambuc apps_startup();
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc conf = NULL;
343ebfedea0SLionel Sambuc key = NULL;
344ebfedea0SLionel Sambuc section = NULL;
345ebfedea0SLionel Sambuc
346ebfedea0SLionel Sambuc preserve = 0;
347ebfedea0SLionel Sambuc msie_hack = 0;
348ebfedea0SLionel Sambuc if (bio_err == NULL)
349ebfedea0SLionel Sambuc if ((bio_err = BIO_new(BIO_s_file())) != NULL)
350ebfedea0SLionel Sambuc BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
351ebfedea0SLionel Sambuc
352ebfedea0SLionel Sambuc argc--;
353ebfedea0SLionel Sambuc argv++;
354*0a6a1f1dSLionel Sambuc while (argc >= 1) {
355ebfedea0SLionel Sambuc if (strcmp(*argv, "-verbose") == 0)
356ebfedea0SLionel Sambuc verbose = 1;
357*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-config") == 0) {
358*0a6a1f1dSLionel Sambuc if (--argc < 1)
359*0a6a1f1dSLionel Sambuc goto bad;
360ebfedea0SLionel Sambuc configfile = *(++argv);
361*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-name") == 0) {
362*0a6a1f1dSLionel Sambuc if (--argc < 1)
363*0a6a1f1dSLionel Sambuc goto bad;
364ebfedea0SLionel Sambuc section = *(++argv);
365*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-subj") == 0) {
366*0a6a1f1dSLionel Sambuc if (--argc < 1)
367*0a6a1f1dSLionel Sambuc goto bad;
368ebfedea0SLionel Sambuc subj = *(++argv);
369ebfedea0SLionel Sambuc /* preserve=1; */
370*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-utf8") == 0)
371ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8;
372ebfedea0SLionel Sambuc else if (strcmp(*argv, "-create_serial") == 0)
373ebfedea0SLionel Sambuc create_ser = 1;
374ebfedea0SLionel Sambuc else if (strcmp(*argv, "-multivalue-rdn") == 0)
375ebfedea0SLionel Sambuc multirdn = 1;
376*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-startdate") == 0) {
377*0a6a1f1dSLionel Sambuc if (--argc < 1)
378*0a6a1f1dSLionel Sambuc goto bad;
379ebfedea0SLionel Sambuc startdate = *(++argv);
380*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-enddate") == 0) {
381*0a6a1f1dSLionel Sambuc if (--argc < 1)
382*0a6a1f1dSLionel Sambuc goto bad;
383ebfedea0SLionel Sambuc enddate = *(++argv);
384*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-days") == 0) {
385*0a6a1f1dSLionel Sambuc if (--argc < 1)
386*0a6a1f1dSLionel Sambuc goto bad;
387ebfedea0SLionel Sambuc days = atoi(*(++argv));
388*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-md") == 0) {
389*0a6a1f1dSLionel Sambuc if (--argc < 1)
390*0a6a1f1dSLionel Sambuc goto bad;
391ebfedea0SLionel Sambuc md = *(++argv);
392*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-policy") == 0) {
393*0a6a1f1dSLionel Sambuc if (--argc < 1)
394*0a6a1f1dSLionel Sambuc goto bad;
395ebfedea0SLionel Sambuc policy = *(++argv);
396*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-keyfile") == 0) {
397*0a6a1f1dSLionel Sambuc if (--argc < 1)
398*0a6a1f1dSLionel Sambuc goto bad;
399ebfedea0SLionel Sambuc keyfile = *(++argv);
400*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-keyform") == 0) {
401*0a6a1f1dSLionel Sambuc if (--argc < 1)
402*0a6a1f1dSLionel Sambuc goto bad;
403ebfedea0SLionel Sambuc keyform = str2fmt(*(++argv));
404*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-passin") == 0) {
405*0a6a1f1dSLionel Sambuc if (--argc < 1)
406*0a6a1f1dSLionel Sambuc goto bad;
407ebfedea0SLionel Sambuc passargin = *(++argv);
408*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-key") == 0) {
409*0a6a1f1dSLionel Sambuc if (--argc < 1)
410*0a6a1f1dSLionel Sambuc goto bad;
411ebfedea0SLionel Sambuc key = *(++argv);
412*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-cert") == 0) {
413*0a6a1f1dSLionel Sambuc if (--argc < 1)
414*0a6a1f1dSLionel Sambuc goto bad;
415ebfedea0SLionel Sambuc certfile = *(++argv);
416*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-selfsign") == 0)
417ebfedea0SLionel Sambuc selfsign = 1;
418*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-in") == 0) {
419*0a6a1f1dSLionel Sambuc if (--argc < 1)
420*0a6a1f1dSLionel Sambuc goto bad;
421ebfedea0SLionel Sambuc infile = *(++argv);
422ebfedea0SLionel Sambuc req = 1;
423*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-out") == 0) {
424*0a6a1f1dSLionel Sambuc if (--argc < 1)
425*0a6a1f1dSLionel Sambuc goto bad;
426ebfedea0SLionel Sambuc outfile = *(++argv);
427*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-outdir") == 0) {
428*0a6a1f1dSLionel Sambuc if (--argc < 1)
429*0a6a1f1dSLionel Sambuc goto bad;
430ebfedea0SLionel Sambuc outdir = *(++argv);
431*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-sigopt") == 0) {
432ebfedea0SLionel Sambuc if (--argc < 1)
433ebfedea0SLionel Sambuc goto bad;
434ebfedea0SLionel Sambuc if (!sigopts)
435ebfedea0SLionel Sambuc sigopts = sk_OPENSSL_STRING_new_null();
436ebfedea0SLionel Sambuc if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
437ebfedea0SLionel Sambuc goto bad;
438*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-notext") == 0)
439ebfedea0SLionel Sambuc notext = 1;
440ebfedea0SLionel Sambuc else if (strcmp(*argv, "-batch") == 0)
441ebfedea0SLionel Sambuc batch = 1;
442ebfedea0SLionel Sambuc else if (strcmp(*argv, "-preserveDN") == 0)
443ebfedea0SLionel Sambuc preserve = 1;
444ebfedea0SLionel Sambuc else if (strcmp(*argv, "-noemailDN") == 0)
445ebfedea0SLionel Sambuc email_dn = 0;
446ebfedea0SLionel Sambuc else if (strcmp(*argv, "-gencrl") == 0)
447ebfedea0SLionel Sambuc gencrl = 1;
448ebfedea0SLionel Sambuc else if (strcmp(*argv, "-msie_hack") == 0)
449ebfedea0SLionel Sambuc msie_hack = 1;
450*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-crldays") == 0) {
451*0a6a1f1dSLionel Sambuc if (--argc < 1)
452*0a6a1f1dSLionel Sambuc goto bad;
453ebfedea0SLionel Sambuc crldays = atol(*(++argv));
454*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crlhours") == 0) {
455*0a6a1f1dSLionel Sambuc if (--argc < 1)
456*0a6a1f1dSLionel Sambuc goto bad;
457ebfedea0SLionel Sambuc crlhours = atol(*(++argv));
458*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crlsec") == 0) {
459*0a6a1f1dSLionel Sambuc if (--argc < 1)
460*0a6a1f1dSLionel Sambuc goto bad;
461ebfedea0SLionel Sambuc crlsec = atol(*(++argv));
462*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-infiles") == 0) {
463ebfedea0SLionel Sambuc argc--;
464ebfedea0SLionel Sambuc argv++;
465ebfedea0SLionel Sambuc req = 1;
466ebfedea0SLionel Sambuc break;
467*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-ss_cert") == 0) {
468*0a6a1f1dSLionel Sambuc if (--argc < 1)
469*0a6a1f1dSLionel Sambuc goto bad;
470ebfedea0SLionel Sambuc ss_cert_file = *(++argv);
471ebfedea0SLionel Sambuc req = 1;
472*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-spkac") == 0) {
473*0a6a1f1dSLionel Sambuc if (--argc < 1)
474*0a6a1f1dSLionel Sambuc goto bad;
475ebfedea0SLionel Sambuc spkac_file = *(++argv);
476ebfedea0SLionel Sambuc req = 1;
477*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-revoke") == 0) {
478*0a6a1f1dSLionel Sambuc if (--argc < 1)
479*0a6a1f1dSLionel Sambuc goto bad;
480ebfedea0SLionel Sambuc infile = *(++argv);
481ebfedea0SLionel Sambuc dorevoke = 1;
482*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-extensions") == 0) {
483*0a6a1f1dSLionel Sambuc if (--argc < 1)
484*0a6a1f1dSLionel Sambuc goto bad;
485ebfedea0SLionel Sambuc extensions = *(++argv);
486*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-extfile") == 0) {
487*0a6a1f1dSLionel Sambuc if (--argc < 1)
488*0a6a1f1dSLionel Sambuc goto bad;
489ebfedea0SLionel Sambuc extfile = *(++argv);
490*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-status") == 0) {
491*0a6a1f1dSLionel Sambuc if (--argc < 1)
492*0a6a1f1dSLionel Sambuc goto bad;
493ebfedea0SLionel Sambuc ser_status = *(++argv);
494*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-updatedb") == 0) {
495ebfedea0SLionel Sambuc doupdatedb = 1;
496*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crlexts") == 0) {
497*0a6a1f1dSLionel Sambuc if (--argc < 1)
498*0a6a1f1dSLionel Sambuc goto bad;
499ebfedea0SLionel Sambuc crl_ext = *(++argv);
500*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crl_reason") == 0) {
501*0a6a1f1dSLionel Sambuc if (--argc < 1)
502*0a6a1f1dSLionel Sambuc goto bad;
503ebfedea0SLionel Sambuc rev_arg = *(++argv);
504ebfedea0SLionel Sambuc rev_type = REV_CRL_REASON;
505*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crl_hold") == 0) {
506*0a6a1f1dSLionel Sambuc if (--argc < 1)
507*0a6a1f1dSLionel Sambuc goto bad;
508ebfedea0SLionel Sambuc rev_arg = *(++argv);
509ebfedea0SLionel Sambuc rev_type = REV_HOLD;
510*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crl_compromise") == 0) {
511*0a6a1f1dSLionel Sambuc if (--argc < 1)
512*0a6a1f1dSLionel Sambuc goto bad;
513ebfedea0SLionel Sambuc rev_arg = *(++argv);
514ebfedea0SLionel Sambuc rev_type = REV_KEY_COMPROMISE;
515*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-crl_CA_compromise") == 0) {
516*0a6a1f1dSLionel Sambuc if (--argc < 1)
517*0a6a1f1dSLionel Sambuc goto bad;
518ebfedea0SLionel Sambuc rev_arg = *(++argv);
519ebfedea0SLionel Sambuc rev_type = REV_CA_COMPROMISE;
520ebfedea0SLionel Sambuc }
521ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
522*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-engine") == 0) {
523*0a6a1f1dSLionel Sambuc if (--argc < 1)
524*0a6a1f1dSLionel Sambuc goto bad;
525ebfedea0SLionel Sambuc engine = *(++argv);
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc #endif
528*0a6a1f1dSLionel Sambuc else {
529ebfedea0SLionel Sambuc bad:
530ebfedea0SLionel Sambuc BIO_printf(bio_err, "unknown option %s\n", *argv);
531ebfedea0SLionel Sambuc badops = 1;
532ebfedea0SLionel Sambuc break;
533ebfedea0SLionel Sambuc }
534ebfedea0SLionel Sambuc argc--;
535ebfedea0SLionel Sambuc argv++;
536ebfedea0SLionel Sambuc }
537ebfedea0SLionel Sambuc
538*0a6a1f1dSLionel Sambuc if (badops) {
539ebfedea0SLionel Sambuc const char **pp2;
540ebfedea0SLionel Sambuc
541ebfedea0SLionel Sambuc for (pp2 = ca_usage; (*pp2 != NULL); pp2++)
542ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s", *pp2);
543ebfedea0SLionel Sambuc goto err;
544ebfedea0SLionel Sambuc }
545ebfedea0SLionel Sambuc
546ebfedea0SLionel Sambuc ERR_load_crypto_strings();
547ebfedea0SLionel Sambuc
548ebfedea0SLionel Sambuc /*****************************************************************/
549ebfedea0SLionel Sambuc tofree = NULL;
550ebfedea0SLionel Sambuc if (configfile == NULL)
551*0a6a1f1dSLionel Sambuc configfile = getenv("OPENSSL_CONF");
552*0a6a1f1dSLionel Sambuc if (configfile == NULL)
553*0a6a1f1dSLionel Sambuc configfile = getenv("SSLEAY_CONF");
554*0a6a1f1dSLionel Sambuc if (configfile == NULL) {
555ebfedea0SLionel Sambuc const char *s = X509_get_default_cert_area();
556ebfedea0SLionel Sambuc size_t len;
557ebfedea0SLionel Sambuc
558ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
559ebfedea0SLionel Sambuc len = strlen(s) + sizeof(CONFIG_FILE);
560ebfedea0SLionel Sambuc tofree = OPENSSL_malloc(len);
561*0a6a1f1dSLionel Sambuc if (!tofree) {
562*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Out of memory\n");
563*0a6a1f1dSLionel Sambuc goto err;
564*0a6a1f1dSLionel Sambuc }
565ebfedea0SLionel Sambuc strcpy(tofree, s);
566ebfedea0SLionel Sambuc #else
567ebfedea0SLionel Sambuc len = strlen(s) + sizeof(CONFIG_FILE) + 1;
568ebfedea0SLionel Sambuc tofree = OPENSSL_malloc(len);
569*0a6a1f1dSLionel Sambuc if (!tofree) {
570*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Out of memory\n");
571*0a6a1f1dSLionel Sambuc goto err;
572*0a6a1f1dSLionel Sambuc }
573ebfedea0SLionel Sambuc BUF_strlcpy(tofree, s, len);
574ebfedea0SLionel Sambuc BUF_strlcat(tofree, "/", len);
575ebfedea0SLionel Sambuc #endif
576ebfedea0SLionel Sambuc BUF_strlcat(tofree, CONFIG_FILE, len);
577ebfedea0SLionel Sambuc configfile = tofree;
578ebfedea0SLionel Sambuc }
579ebfedea0SLionel Sambuc
580ebfedea0SLionel Sambuc BIO_printf(bio_err, "Using configuration from %s\n", configfile);
581ebfedea0SLionel Sambuc conf = NCONF_new(NULL);
582*0a6a1f1dSLionel Sambuc if (NCONF_load(conf, configfile, &errorline) <= 0) {
583ebfedea0SLionel Sambuc if (errorline <= 0)
584ebfedea0SLionel Sambuc BIO_printf(bio_err, "error loading the config file '%s'\n",
585ebfedea0SLionel Sambuc configfile);
586ebfedea0SLionel Sambuc else
587*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
588*0a6a1f1dSLionel Sambuc errorline, configfile);
589ebfedea0SLionel Sambuc goto err;
590ebfedea0SLionel Sambuc }
591*0a6a1f1dSLionel Sambuc if (tofree) {
592ebfedea0SLionel Sambuc OPENSSL_free(tofree);
593ebfedea0SLionel Sambuc tofree = NULL;
594ebfedea0SLionel Sambuc }
595ebfedea0SLionel Sambuc
596ebfedea0SLionel Sambuc if (!load_config(bio_err, conf))
597ebfedea0SLionel Sambuc goto err;
598ebfedea0SLionel Sambuc
599ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
600ebfedea0SLionel Sambuc e = setup_engine(bio_err, engine, 0);
601ebfedea0SLionel Sambuc #endif
602ebfedea0SLionel Sambuc
603ebfedea0SLionel Sambuc /* Lets get the config section we are using */
604*0a6a1f1dSLionel Sambuc if (section == NULL) {
605ebfedea0SLionel Sambuc section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_CA);
606*0a6a1f1dSLionel Sambuc if (section == NULL) {
607ebfedea0SLionel Sambuc lookup_fail(BASE_SECTION, ENV_DEFAULT_CA);
608ebfedea0SLionel Sambuc goto err;
609ebfedea0SLionel Sambuc }
610ebfedea0SLionel Sambuc }
611ebfedea0SLionel Sambuc
612*0a6a1f1dSLionel Sambuc if (conf != NULL) {
613ebfedea0SLionel Sambuc p = NCONF_get_string(conf, NULL, "oid_file");
614ebfedea0SLionel Sambuc if (p == NULL)
615ebfedea0SLionel Sambuc ERR_clear_error();
616*0a6a1f1dSLionel Sambuc if (p != NULL) {
617ebfedea0SLionel Sambuc BIO *oid_bio;
618ebfedea0SLionel Sambuc
619ebfedea0SLionel Sambuc oid_bio = BIO_new_file(p, "r");
620*0a6a1f1dSLionel Sambuc if (oid_bio == NULL) {
621*0a6a1f1dSLionel Sambuc /*-
622ebfedea0SLionel Sambuc BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
623ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
624ebfedea0SLionel Sambuc */
625ebfedea0SLionel Sambuc ERR_clear_error();
626*0a6a1f1dSLionel Sambuc } else {
627ebfedea0SLionel Sambuc OBJ_create_objects(oid_bio);
628ebfedea0SLionel Sambuc BIO_free(oid_bio);
629ebfedea0SLionel Sambuc }
630ebfedea0SLionel Sambuc }
631*0a6a1f1dSLionel Sambuc if (!add_oid_section(bio_err, conf)) {
632ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
633ebfedea0SLionel Sambuc goto err;
634ebfedea0SLionel Sambuc }
635ebfedea0SLionel Sambuc }
636ebfedea0SLionel Sambuc
637ebfedea0SLionel Sambuc randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
638ebfedea0SLionel Sambuc if (randfile == NULL)
639ebfedea0SLionel Sambuc ERR_clear_error();
640ebfedea0SLionel Sambuc app_RAND_load_file(randfile, bio_err, 0);
641ebfedea0SLionel Sambuc
642ebfedea0SLionel Sambuc f = NCONF_get_string(conf, section, STRING_MASK);
643ebfedea0SLionel Sambuc if (!f)
644ebfedea0SLionel Sambuc ERR_clear_error();
645ebfedea0SLionel Sambuc
646ebfedea0SLionel Sambuc if (f && !ASN1_STRING_set_default_mask_asc(f)) {
647ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
648ebfedea0SLionel Sambuc goto err;
649ebfedea0SLionel Sambuc }
650ebfedea0SLionel Sambuc
651ebfedea0SLionel Sambuc if (chtype != MBSTRING_UTF8) {
652ebfedea0SLionel Sambuc f = NCONF_get_string(conf, section, UTF8_IN);
653ebfedea0SLionel Sambuc if (!f)
654ebfedea0SLionel Sambuc ERR_clear_error();
655ebfedea0SLionel Sambuc else if (!strcmp(f, "yes"))
656ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8;
657ebfedea0SLionel Sambuc }
658ebfedea0SLionel Sambuc
659ebfedea0SLionel Sambuc db_attr.unique_subject = 1;
660ebfedea0SLionel Sambuc p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
661*0a6a1f1dSLionel Sambuc if (p) {
662ebfedea0SLionel Sambuc #ifdef RL_DEBUG
663ebfedea0SLionel Sambuc BIO_printf(bio_err, "DEBUG: unique_subject = \"%s\"\n", p);
664ebfedea0SLionel Sambuc #endif
665ebfedea0SLionel Sambuc db_attr.unique_subject = parse_yesno(p, 1);
666*0a6a1f1dSLionel Sambuc } else
667ebfedea0SLionel Sambuc ERR_clear_error();
668ebfedea0SLionel Sambuc #ifdef RL_DEBUG
669ebfedea0SLionel Sambuc if (!p)
670*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "DEBUG: unique_subject undefined\n");
671ebfedea0SLionel Sambuc #endif
672ebfedea0SLionel Sambuc #ifdef RL_DEBUG
673ebfedea0SLionel Sambuc BIO_printf(bio_err, "DEBUG: configured unique_subject is %d\n",
674ebfedea0SLionel Sambuc db_attr.unique_subject);
675ebfedea0SLionel Sambuc #endif
676ebfedea0SLionel Sambuc
677ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file());
678ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
679ebfedea0SLionel Sambuc Sout = BIO_new(BIO_s_file());
680ebfedea0SLionel Sambuc Cout = BIO_new(BIO_s_file());
681*0a6a1f1dSLionel Sambuc if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL)) {
682ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
683ebfedea0SLionel Sambuc goto err;
684ebfedea0SLionel Sambuc }
685ebfedea0SLionel Sambuc
686ebfedea0SLionel Sambuc /*****************************************************************/
687ebfedea0SLionel Sambuc /* report status of cert with serial number given on command line */
688*0a6a1f1dSLionel Sambuc if (ser_status) {
689*0a6a1f1dSLionel Sambuc if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
690ebfedea0SLionel Sambuc lookup_fail(section, ENV_DATABASE);
691ebfedea0SLionel Sambuc goto err;
692ebfedea0SLionel Sambuc }
693ebfedea0SLionel Sambuc db = load_index(dbfile, &db_attr);
694*0a6a1f1dSLionel Sambuc if (db == NULL)
695*0a6a1f1dSLionel Sambuc goto err;
696ebfedea0SLionel Sambuc
697*0a6a1f1dSLionel Sambuc if (!index_index(db))
698*0a6a1f1dSLionel Sambuc goto err;
699ebfedea0SLionel Sambuc
700ebfedea0SLionel Sambuc if (get_certificate_status(ser_status, db) != 1)
701*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
702ebfedea0SLionel Sambuc goto err;
703ebfedea0SLionel Sambuc }
704ebfedea0SLionel Sambuc
705ebfedea0SLionel Sambuc /*****************************************************************/
706ebfedea0SLionel Sambuc /* we definitely need a private key, so let's get it */
707ebfedea0SLionel Sambuc
708ebfedea0SLionel Sambuc if ((keyfile == NULL) && ((keyfile = NCONF_get_string(conf,
709*0a6a1f1dSLionel Sambuc section,
710*0a6a1f1dSLionel Sambuc ENV_PRIVATE_KEY)) ==
711*0a6a1f1dSLionel Sambuc NULL)) {
712ebfedea0SLionel Sambuc lookup_fail(section, ENV_PRIVATE_KEY);
713ebfedea0SLionel Sambuc goto err;
714ebfedea0SLionel Sambuc }
715*0a6a1f1dSLionel Sambuc if (!key) {
716ebfedea0SLionel Sambuc free_key = 1;
717*0a6a1f1dSLionel Sambuc if (!app_passwd(bio_err, passargin, NULL, &key, NULL)) {
718ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error getting password\n");
719ebfedea0SLionel Sambuc goto err;
720ebfedea0SLionel Sambuc }
721ebfedea0SLionel Sambuc }
722*0a6a1f1dSLionel Sambuc pkey = load_key(bio_err, keyfile, keyform, 0, key, e, "CA private key");
723*0a6a1f1dSLionel Sambuc if (key)
724*0a6a1f1dSLionel Sambuc OPENSSL_cleanse(key, strlen(key));
725*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
726ebfedea0SLionel Sambuc /* load_key() has already printed an appropriate message */
727ebfedea0SLionel Sambuc goto err;
728ebfedea0SLionel Sambuc }
729ebfedea0SLionel Sambuc
730ebfedea0SLionel Sambuc /*****************************************************************/
731ebfedea0SLionel Sambuc /* we need a certificate */
732*0a6a1f1dSLionel Sambuc if (!selfsign || spkac_file || ss_cert_file || gencrl) {
733ebfedea0SLionel Sambuc if ((certfile == NULL)
734ebfedea0SLionel Sambuc && ((certfile = NCONF_get_string(conf,
735*0a6a1f1dSLionel Sambuc section,
736*0a6a1f1dSLionel Sambuc ENV_CERTIFICATE)) == NULL)) {
737ebfedea0SLionel Sambuc lookup_fail(section, ENV_CERTIFICATE);
738ebfedea0SLionel Sambuc goto err;
739ebfedea0SLionel Sambuc }
740ebfedea0SLionel Sambuc x509 = load_cert(bio_err, certfile, FORMAT_PEM, NULL, e,
741ebfedea0SLionel Sambuc "CA certificate");
742ebfedea0SLionel Sambuc if (x509 == NULL)
743ebfedea0SLionel Sambuc goto err;
744ebfedea0SLionel Sambuc
745*0a6a1f1dSLionel Sambuc if (!X509_check_private_key(x509, pkey)) {
746*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
747*0a6a1f1dSLionel Sambuc "CA certificate and CA private key do not match\n");
748ebfedea0SLionel Sambuc goto err;
749ebfedea0SLionel Sambuc }
750ebfedea0SLionel Sambuc }
751*0a6a1f1dSLionel Sambuc if (!selfsign)
752*0a6a1f1dSLionel Sambuc x509p = x509;
753ebfedea0SLionel Sambuc
754ebfedea0SLionel Sambuc f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
755ebfedea0SLionel Sambuc if (f == NULL)
756ebfedea0SLionel Sambuc ERR_clear_error();
757ebfedea0SLionel Sambuc if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
758ebfedea0SLionel Sambuc preserve = 1;
759ebfedea0SLionel Sambuc f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
760ebfedea0SLionel Sambuc if (f == NULL)
761ebfedea0SLionel Sambuc ERR_clear_error();
762ebfedea0SLionel Sambuc if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
763ebfedea0SLionel Sambuc msie_hack = 1;
764ebfedea0SLionel Sambuc
765ebfedea0SLionel Sambuc f = NCONF_get_string(conf, section, ENV_NAMEOPT);
766ebfedea0SLionel Sambuc
767*0a6a1f1dSLionel Sambuc if (f) {
768*0a6a1f1dSLionel Sambuc if (!set_name_ex(&nameopt, f)) {
769ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
770ebfedea0SLionel Sambuc goto err;
771ebfedea0SLionel Sambuc }
772ebfedea0SLionel Sambuc default_op = 0;
773*0a6a1f1dSLionel Sambuc } else
774ebfedea0SLionel Sambuc ERR_clear_error();
775ebfedea0SLionel Sambuc
776ebfedea0SLionel Sambuc f = NCONF_get_string(conf, section, ENV_CERTOPT);
777ebfedea0SLionel Sambuc
778*0a6a1f1dSLionel Sambuc if (f) {
779*0a6a1f1dSLionel Sambuc if (!set_cert_ex(&certopt, f)) {
780ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
781ebfedea0SLionel Sambuc goto err;
782ebfedea0SLionel Sambuc }
783ebfedea0SLionel Sambuc default_op = 0;
784*0a6a1f1dSLionel Sambuc } else
785ebfedea0SLionel Sambuc ERR_clear_error();
786ebfedea0SLionel Sambuc
787ebfedea0SLionel Sambuc f = NCONF_get_string(conf, section, ENV_EXTCOPY);
788ebfedea0SLionel Sambuc
789*0a6a1f1dSLionel Sambuc if (f) {
790*0a6a1f1dSLionel Sambuc if (!set_ext_copy(&ext_copy, f)) {
791ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
792ebfedea0SLionel Sambuc goto err;
793ebfedea0SLionel Sambuc }
794*0a6a1f1dSLionel Sambuc } else
795ebfedea0SLionel Sambuc ERR_clear_error();
796ebfedea0SLionel Sambuc
797ebfedea0SLionel Sambuc /*****************************************************************/
798ebfedea0SLionel Sambuc /* lookup where to write new certificates */
799*0a6a1f1dSLionel Sambuc if ((outdir == NULL) && (req)) {
800ebfedea0SLionel Sambuc
801ebfedea0SLionel Sambuc if ((outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR))
802*0a6a1f1dSLionel Sambuc == NULL) {
803*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
804*0a6a1f1dSLionel Sambuc "there needs to be defined a directory for new certificate to be placed in\n");
805ebfedea0SLionel Sambuc goto err;
806ebfedea0SLionel Sambuc }
807ebfedea0SLionel Sambuc #ifndef OPENSSL_SYS_VMS
808*0a6a1f1dSLionel Sambuc /*
809*0a6a1f1dSLionel Sambuc * outdir is a directory spec, but access() for VMS demands a
810*0a6a1f1dSLionel Sambuc * filename. In any case, stat(), below, will catch the problem if
811*0a6a1f1dSLionel Sambuc * outdir is not a directory spec, and the fopen() or open() will
812*0a6a1f1dSLionel Sambuc * catch an error if there is no write access.
813*0a6a1f1dSLionel Sambuc *
814*0a6a1f1dSLionel Sambuc * Presumably, this problem could also be solved by using the DEC C
815*0a6a1f1dSLionel Sambuc * routines to convert the directory syntax to Unixly, and give that
816*0a6a1f1dSLionel Sambuc * to access(). However, time's too short to do that just now.
817ebfedea0SLionel Sambuc */
818ebfedea0SLionel Sambuc # ifndef _WIN32
819ebfedea0SLionel Sambuc if (access(outdir, R_OK | W_OK | X_OK) != 0)
820ebfedea0SLionel Sambuc # else
821ebfedea0SLionel Sambuc if (_access(outdir, R_OK | W_OK | X_OK) != 0)
822ebfedea0SLionel Sambuc # endif
823ebfedea0SLionel Sambuc {
824*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "I am unable to access the %s directory\n",
825*0a6a1f1dSLionel Sambuc outdir);
826ebfedea0SLionel Sambuc perror(outdir);
827ebfedea0SLionel Sambuc goto err;
828ebfedea0SLionel Sambuc }
829ebfedea0SLionel Sambuc
830*0a6a1f1dSLionel Sambuc if (app_isdir(outdir) <= 0) {
831ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s need to be a directory\n", outdir);
832ebfedea0SLionel Sambuc perror(outdir);
833ebfedea0SLionel Sambuc goto err;
834ebfedea0SLionel Sambuc }
835ebfedea0SLionel Sambuc #endif
836ebfedea0SLionel Sambuc }
837ebfedea0SLionel Sambuc
838ebfedea0SLionel Sambuc /*****************************************************************/
839ebfedea0SLionel Sambuc /* we need to load the database file */
840*0a6a1f1dSLionel Sambuc if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
841ebfedea0SLionel Sambuc lookup_fail(section, ENV_DATABASE);
842ebfedea0SLionel Sambuc goto err;
843ebfedea0SLionel Sambuc }
844ebfedea0SLionel Sambuc db = load_index(dbfile, &db_attr);
845*0a6a1f1dSLionel Sambuc if (db == NULL)
846*0a6a1f1dSLionel Sambuc goto err;
847ebfedea0SLionel Sambuc
848ebfedea0SLionel Sambuc /* Lets check some fields */
849*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
850ebfedea0SLionel Sambuc pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
851*0a6a1f1dSLionel Sambuc if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
852*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
853*0a6a1f1dSLionel Sambuc "entry %d: not revoked yet, but has a revocation date\n",
854*0a6a1f1dSLionel Sambuc i + 1);
855ebfedea0SLionel Sambuc goto err;
856ebfedea0SLionel Sambuc }
857ebfedea0SLionel Sambuc if ((pp[DB_type][0] == DB_TYPE_REV) &&
858*0a6a1f1dSLionel Sambuc !make_revoked(NULL, pp[DB_rev_date])) {
859ebfedea0SLionel Sambuc BIO_printf(bio_err, " in entry %d\n", i + 1);
860ebfedea0SLionel Sambuc goto err;
861ebfedea0SLionel Sambuc }
862*0a6a1f1dSLionel Sambuc if (!check_time_format((char *)pp[DB_exp_date])) {
863ebfedea0SLionel Sambuc BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
864ebfedea0SLionel Sambuc goto err;
865ebfedea0SLionel Sambuc }
866ebfedea0SLionel Sambuc p = pp[DB_serial];
867ebfedea0SLionel Sambuc j = strlen(p);
868*0a6a1f1dSLionel Sambuc if (*p == '-') {
869ebfedea0SLionel Sambuc p++;
870ebfedea0SLionel Sambuc j--;
871ebfedea0SLionel Sambuc }
872*0a6a1f1dSLionel Sambuc if ((j & 1) || (j < 2)) {
873*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
874*0a6a1f1dSLionel Sambuc i + 1, j);
875ebfedea0SLionel Sambuc goto err;
876ebfedea0SLionel Sambuc }
877*0a6a1f1dSLionel Sambuc while (*p) {
878ebfedea0SLionel Sambuc if (!(((*p >= '0') && (*p <= '9')) ||
879ebfedea0SLionel Sambuc ((*p >= 'A') && (*p <= 'F')) ||
880*0a6a1f1dSLionel Sambuc ((*p >= 'a') && (*p <= 'f')))) {
881*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
882*0a6a1f1dSLionel Sambuc "entry %d: bad serial number characters, char pos %ld, char is '%c'\n",
883*0a6a1f1dSLionel Sambuc i + 1, (long)(p - pp[DB_serial]), *p);
884ebfedea0SLionel Sambuc goto err;
885ebfedea0SLionel Sambuc }
886ebfedea0SLionel Sambuc p++;
887ebfedea0SLionel Sambuc }
888ebfedea0SLionel Sambuc }
889*0a6a1f1dSLionel Sambuc if (verbose) {
890ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT); /* cannot fail */
891ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
892ebfedea0SLionel Sambuc {
893ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
894ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
895ebfedea0SLionel Sambuc }
896ebfedea0SLionel Sambuc #endif
897ebfedea0SLionel Sambuc TXT_DB_write(out, db->db);
898ebfedea0SLionel Sambuc BIO_printf(bio_err, "%d entries loaded from the database\n",
899ebfedea0SLionel Sambuc sk_OPENSSL_PSTRING_num(db->db->data));
900ebfedea0SLionel Sambuc BIO_printf(bio_err, "generating index\n");
901ebfedea0SLionel Sambuc }
902ebfedea0SLionel Sambuc
903*0a6a1f1dSLionel Sambuc if (!index_index(db))
904*0a6a1f1dSLionel Sambuc goto err;
905ebfedea0SLionel Sambuc
906ebfedea0SLionel Sambuc /*****************************************************************/
907ebfedea0SLionel Sambuc /* Update the db file for expired certificates */
908*0a6a1f1dSLionel Sambuc if (doupdatedb) {
909ebfedea0SLionel Sambuc if (verbose)
910*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Updating %s ...\n", dbfile);
911ebfedea0SLionel Sambuc
912ebfedea0SLionel Sambuc i = do_updatedb(db);
913*0a6a1f1dSLionel Sambuc if (i == -1) {
914ebfedea0SLionel Sambuc BIO_printf(bio_err, "Malloc failure\n");
915ebfedea0SLionel Sambuc goto err;
916*0a6a1f1dSLionel Sambuc } else if (i == 0) {
917*0a6a1f1dSLionel Sambuc if (verbose)
918*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "No entries found to mark expired\n");
919*0a6a1f1dSLionel Sambuc } else {
920*0a6a1f1dSLionel Sambuc if (!save_index(dbfile, "new", db))
921*0a6a1f1dSLionel Sambuc goto err;
922ebfedea0SLionel Sambuc
923*0a6a1f1dSLionel Sambuc if (!rotate_index(dbfile, "new", "old"))
924*0a6a1f1dSLionel Sambuc goto err;
925ebfedea0SLionel Sambuc
926*0a6a1f1dSLionel Sambuc if (verbose)
927*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
928ebfedea0SLionel Sambuc "Done. %d entries marked as expired\n", i);
929ebfedea0SLionel Sambuc }
930ebfedea0SLionel Sambuc }
931ebfedea0SLionel Sambuc
932ebfedea0SLionel Sambuc /*****************************************************************/
933ebfedea0SLionel Sambuc /* Read extentions config file */
934*0a6a1f1dSLionel Sambuc if (extfile) {
935ebfedea0SLionel Sambuc extconf = NCONF_new(NULL);
936*0a6a1f1dSLionel Sambuc if (NCONF_load(extconf, extfile, &errorline) <= 0) {
937ebfedea0SLionel Sambuc if (errorline <= 0)
938ebfedea0SLionel Sambuc BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
939ebfedea0SLionel Sambuc extfile);
940ebfedea0SLionel Sambuc else
941*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
942*0a6a1f1dSLionel Sambuc "ERROR: on line %ld of config file '%s'\n",
943ebfedea0SLionel Sambuc errorline, extfile);
944ebfedea0SLionel Sambuc ret = 1;
945ebfedea0SLionel Sambuc goto err;
946ebfedea0SLionel Sambuc }
947ebfedea0SLionel Sambuc
948ebfedea0SLionel Sambuc if (verbose)
949*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
950*0a6a1f1dSLionel Sambuc extfile);
951ebfedea0SLionel Sambuc
952ebfedea0SLionel Sambuc /* We can have sections in the ext file */
953*0a6a1f1dSLionel Sambuc if (!extensions
954*0a6a1f1dSLionel Sambuc && !(extensions =
955*0a6a1f1dSLionel Sambuc NCONF_get_string(extconf, "default", "extensions")))
956ebfedea0SLionel Sambuc extensions = "default";
957ebfedea0SLionel Sambuc }
958ebfedea0SLionel Sambuc
959ebfedea0SLionel Sambuc /*****************************************************************/
960*0a6a1f1dSLionel Sambuc if (req || gencrl) {
961*0a6a1f1dSLionel Sambuc if (outfile != NULL) {
962*0a6a1f1dSLionel Sambuc if (BIO_write_filename(Sout, outfile) <= 0) {
963ebfedea0SLionel Sambuc perror(outfile);
964ebfedea0SLionel Sambuc goto err;
965ebfedea0SLionel Sambuc }
966*0a6a1f1dSLionel Sambuc } else {
967ebfedea0SLionel Sambuc BIO_set_fp(Sout, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
968ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
969ebfedea0SLionel Sambuc {
970ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
971ebfedea0SLionel Sambuc Sout = BIO_push(tmpbio, Sout);
972ebfedea0SLionel Sambuc }
973ebfedea0SLionel Sambuc #endif
974ebfedea0SLionel Sambuc }
975ebfedea0SLionel Sambuc }
976ebfedea0SLionel Sambuc
977ebfedea0SLionel Sambuc if ((md == NULL) && ((md = NCONF_get_string(conf,
978*0a6a1f1dSLionel Sambuc section,
979*0a6a1f1dSLionel Sambuc ENV_DEFAULT_MD)) == NULL)) {
980ebfedea0SLionel Sambuc lookup_fail(section, ENV_DEFAULT_MD);
981ebfedea0SLionel Sambuc goto err;
982ebfedea0SLionel Sambuc }
983ebfedea0SLionel Sambuc
984*0a6a1f1dSLionel Sambuc if (!strcmp(md, "default")) {
985ebfedea0SLionel Sambuc int def_nid;
986*0a6a1f1dSLionel Sambuc if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {
987ebfedea0SLionel Sambuc BIO_puts(bio_err, "no default digest\n");
988ebfedea0SLionel Sambuc goto err;
989ebfedea0SLionel Sambuc }
990ebfedea0SLionel Sambuc md = (char *)OBJ_nid2sn(def_nid);
991ebfedea0SLionel Sambuc }
992ebfedea0SLionel Sambuc
993*0a6a1f1dSLionel Sambuc if ((dgst = EVP_get_digestbyname(md)) == NULL) {
994ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s is an unsupported message digest type\n", md);
995ebfedea0SLionel Sambuc goto err;
996ebfedea0SLionel Sambuc }
997ebfedea0SLionel Sambuc
998*0a6a1f1dSLionel Sambuc if (req) {
999ebfedea0SLionel Sambuc if ((email_dn == 1) && ((tmp_email_dn = NCONF_get_string(conf,
1000*0a6a1f1dSLionel Sambuc section,
1001*0a6a1f1dSLionel Sambuc ENV_DEFAULT_EMAIL_DN))
1002*0a6a1f1dSLionel Sambuc != NULL)) {
1003ebfedea0SLionel Sambuc if (strcmp(tmp_email_dn, "no") == 0)
1004ebfedea0SLionel Sambuc email_dn = 0;
1005ebfedea0SLionel Sambuc }
1006ebfedea0SLionel Sambuc if (verbose)
1007ebfedea0SLionel Sambuc BIO_printf(bio_err, "message digest is %s\n",
1008ebfedea0SLionel Sambuc OBJ_nid2ln(dgst->type));
1009ebfedea0SLionel Sambuc if ((policy == NULL) && ((policy = NCONF_get_string(conf,
1010*0a6a1f1dSLionel Sambuc section,
1011*0a6a1f1dSLionel Sambuc ENV_POLICY)) ==
1012*0a6a1f1dSLionel Sambuc NULL)) {
1013ebfedea0SLionel Sambuc lookup_fail(section, ENV_POLICY);
1014ebfedea0SLionel Sambuc goto err;
1015ebfedea0SLionel Sambuc }
1016ebfedea0SLionel Sambuc if (verbose)
1017ebfedea0SLionel Sambuc BIO_printf(bio_err, "policy is %s\n", policy);
1018ebfedea0SLionel Sambuc
1019ebfedea0SLionel Sambuc if ((serialfile = NCONF_get_string(conf, section, ENV_SERIAL))
1020*0a6a1f1dSLionel Sambuc == NULL) {
1021ebfedea0SLionel Sambuc lookup_fail(section, ENV_SERIAL);
1022ebfedea0SLionel Sambuc goto err;
1023ebfedea0SLionel Sambuc }
1024ebfedea0SLionel Sambuc
1025*0a6a1f1dSLionel Sambuc if (!extconf) {
1026*0a6a1f1dSLionel Sambuc /*
1027*0a6a1f1dSLionel Sambuc * no '-extfile' option, so we look for extensions in the main
1028*0a6a1f1dSLionel Sambuc * configuration file
1029*0a6a1f1dSLionel Sambuc */
1030*0a6a1f1dSLionel Sambuc if (!extensions) {
1031*0a6a1f1dSLionel Sambuc extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
1032ebfedea0SLionel Sambuc if (!extensions)
1033ebfedea0SLionel Sambuc ERR_clear_error();
1034ebfedea0SLionel Sambuc }
1035*0a6a1f1dSLionel Sambuc if (extensions) {
1036ebfedea0SLionel Sambuc /* Check syntax of file */
1037ebfedea0SLionel Sambuc X509V3_CTX ctx;
1038ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx);
1039ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, conf);
1040*0a6a1f1dSLionel Sambuc if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
1041ebfedea0SLionel Sambuc BIO_printf(bio_err,
1042ebfedea0SLionel Sambuc "Error Loading extension section %s\n",
1043ebfedea0SLionel Sambuc extensions);
1044ebfedea0SLionel Sambuc ret = 1;
1045ebfedea0SLionel Sambuc goto err;
1046ebfedea0SLionel Sambuc }
1047ebfedea0SLionel Sambuc }
1048ebfedea0SLionel Sambuc }
1049ebfedea0SLionel Sambuc
1050*0a6a1f1dSLionel Sambuc if (startdate == NULL) {
1051ebfedea0SLionel Sambuc startdate = NCONF_get_string(conf, section,
1052ebfedea0SLionel Sambuc ENV_DEFAULT_STARTDATE);
1053ebfedea0SLionel Sambuc if (startdate == NULL)
1054ebfedea0SLionel Sambuc ERR_clear_error();
1055ebfedea0SLionel Sambuc }
1056*0a6a1f1dSLionel Sambuc if (startdate && !ASN1_TIME_set_string(NULL, startdate)) {
1057*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1058*0a6a1f1dSLionel Sambuc "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
1059ebfedea0SLionel Sambuc goto err;
1060ebfedea0SLionel Sambuc }
1061*0a6a1f1dSLionel Sambuc if (startdate == NULL)
1062*0a6a1f1dSLionel Sambuc startdate = "today";
1063ebfedea0SLionel Sambuc
1064*0a6a1f1dSLionel Sambuc if (enddate == NULL) {
1065*0a6a1f1dSLionel Sambuc enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
1066ebfedea0SLionel Sambuc if (enddate == NULL)
1067ebfedea0SLionel Sambuc ERR_clear_error();
1068ebfedea0SLionel Sambuc }
1069*0a6a1f1dSLionel Sambuc if (enddate && !ASN1_TIME_set_string(NULL, enddate)) {
1070*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1071*0a6a1f1dSLionel Sambuc "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
1072ebfedea0SLionel Sambuc goto err;
1073ebfedea0SLionel Sambuc }
1074ebfedea0SLionel Sambuc
1075*0a6a1f1dSLionel Sambuc if (days == 0) {
1076ebfedea0SLionel Sambuc if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
1077ebfedea0SLionel Sambuc days = 0;
1078ebfedea0SLionel Sambuc }
1079*0a6a1f1dSLionel Sambuc if (!enddate && (days == 0)) {
1080*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1081*0a6a1f1dSLionel Sambuc "cannot lookup how many days to certify for\n");
1082ebfedea0SLionel Sambuc goto err;
1083ebfedea0SLionel Sambuc }
1084ebfedea0SLionel Sambuc
1085*0a6a1f1dSLionel Sambuc if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
1086ebfedea0SLionel Sambuc BIO_printf(bio_err, "error while loading serial number\n");
1087ebfedea0SLionel Sambuc goto err;
1088ebfedea0SLionel Sambuc }
1089*0a6a1f1dSLionel Sambuc if (verbose) {
1090ebfedea0SLionel Sambuc if (BN_is_zero(serial))
1091ebfedea0SLionel Sambuc BIO_printf(bio_err, "next serial number is 00\n");
1092*0a6a1f1dSLionel Sambuc else {
1093*0a6a1f1dSLionel Sambuc if ((f = BN_bn2hex(serial)) == NULL)
1094*0a6a1f1dSLionel Sambuc goto err;
1095ebfedea0SLionel Sambuc BIO_printf(bio_err, "next serial number is %s\n", f);
1096ebfedea0SLionel Sambuc OPENSSL_free(f);
1097ebfedea0SLionel Sambuc }
1098ebfedea0SLionel Sambuc }
1099ebfedea0SLionel Sambuc
1100*0a6a1f1dSLionel Sambuc if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
1101ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
1102ebfedea0SLionel Sambuc goto err;
1103ebfedea0SLionel Sambuc }
1104ebfedea0SLionel Sambuc
1105*0a6a1f1dSLionel Sambuc if ((cert_sk = sk_X509_new_null()) == NULL) {
1106ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1107ebfedea0SLionel Sambuc goto err;
1108ebfedea0SLionel Sambuc }
1109*0a6a1f1dSLionel Sambuc if (spkac_file != NULL) {
1110ebfedea0SLionel Sambuc total++;
1111ebfedea0SLionel Sambuc j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
1112ebfedea0SLionel Sambuc attribs, db, serial, subj, chtype, multirdn,
1113ebfedea0SLionel Sambuc email_dn, startdate, enddate, days, extensions,
1114*0a6a1f1dSLionel Sambuc conf, verbose, certopt, nameopt, default_op,
1115*0a6a1f1dSLionel Sambuc ext_copy);
1116*0a6a1f1dSLionel Sambuc if (j < 0)
1117*0a6a1f1dSLionel Sambuc goto err;
1118*0a6a1f1dSLionel Sambuc if (j > 0) {
1119ebfedea0SLionel Sambuc total_done++;
1120ebfedea0SLionel Sambuc BIO_printf(bio_err, "\n");
1121*0a6a1f1dSLionel Sambuc if (!BN_add_word(serial, 1))
1122*0a6a1f1dSLionel Sambuc goto err;
1123*0a6a1f1dSLionel Sambuc if (!sk_X509_push(cert_sk, x)) {
1124ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1125ebfedea0SLionel Sambuc goto err;
1126ebfedea0SLionel Sambuc }
1127*0a6a1f1dSLionel Sambuc if (outfile) {
1128ebfedea0SLionel Sambuc output_der = 1;
1129ebfedea0SLionel Sambuc batch = 1;
1130ebfedea0SLionel Sambuc }
1131ebfedea0SLionel Sambuc }
1132ebfedea0SLionel Sambuc }
1133*0a6a1f1dSLionel Sambuc if (ss_cert_file != NULL) {
1134ebfedea0SLionel Sambuc total++;
1135ebfedea0SLionel Sambuc j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,
1136ebfedea0SLionel Sambuc attribs,
1137*0a6a1f1dSLionel Sambuc db, serial, subj, chtype, multirdn, email_dn,
1138*0a6a1f1dSLionel Sambuc startdate, enddate, days, batch, extensions,
1139*0a6a1f1dSLionel Sambuc conf, verbose, certopt, nameopt, default_op,
1140*0a6a1f1dSLionel Sambuc ext_copy, e);
1141*0a6a1f1dSLionel Sambuc if (j < 0)
1142*0a6a1f1dSLionel Sambuc goto err;
1143*0a6a1f1dSLionel Sambuc if (j > 0) {
1144ebfedea0SLionel Sambuc total_done++;
1145ebfedea0SLionel Sambuc BIO_printf(bio_err, "\n");
1146*0a6a1f1dSLionel Sambuc if (!BN_add_word(serial, 1))
1147*0a6a1f1dSLionel Sambuc goto err;
1148*0a6a1f1dSLionel Sambuc if (!sk_X509_push(cert_sk, x)) {
1149ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1150ebfedea0SLionel Sambuc goto err;
1151ebfedea0SLionel Sambuc }
1152ebfedea0SLionel Sambuc }
1153ebfedea0SLionel Sambuc }
1154*0a6a1f1dSLionel Sambuc if (infile != NULL) {
1155ebfedea0SLionel Sambuc total++;
1156ebfedea0SLionel Sambuc j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,
1157*0a6a1f1dSLionel Sambuc serial, subj, chtype, multirdn, email_dn, startdate,
1158*0a6a1f1dSLionel Sambuc enddate, days, batch, extensions, conf, verbose,
1159*0a6a1f1dSLionel Sambuc certopt, nameopt, default_op, ext_copy, selfsign);
1160*0a6a1f1dSLionel Sambuc if (j < 0)
1161*0a6a1f1dSLionel Sambuc goto err;
1162*0a6a1f1dSLionel Sambuc if (j > 0) {
1163ebfedea0SLionel Sambuc total_done++;
1164ebfedea0SLionel Sambuc BIO_printf(bio_err, "\n");
1165*0a6a1f1dSLionel Sambuc if (!BN_add_word(serial, 1))
1166*0a6a1f1dSLionel Sambuc goto err;
1167*0a6a1f1dSLionel Sambuc if (!sk_X509_push(cert_sk, x)) {
1168ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1169ebfedea0SLionel Sambuc goto err;
1170ebfedea0SLionel Sambuc }
1171ebfedea0SLionel Sambuc }
1172ebfedea0SLionel Sambuc }
1173*0a6a1f1dSLionel Sambuc for (i = 0; i < argc; i++) {
1174ebfedea0SLionel Sambuc total++;
1175ebfedea0SLionel Sambuc j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,
1176*0a6a1f1dSLionel Sambuc serial, subj, chtype, multirdn, email_dn, startdate,
1177*0a6a1f1dSLionel Sambuc enddate, days, batch, extensions, conf, verbose,
1178*0a6a1f1dSLionel Sambuc certopt, nameopt, default_op, ext_copy, selfsign);
1179*0a6a1f1dSLionel Sambuc if (j < 0)
1180*0a6a1f1dSLionel Sambuc goto err;
1181*0a6a1f1dSLionel Sambuc if (j > 0) {
1182ebfedea0SLionel Sambuc total_done++;
1183ebfedea0SLionel Sambuc BIO_printf(bio_err, "\n");
1184*0a6a1f1dSLionel Sambuc if (!BN_add_word(serial, 1))
1185*0a6a1f1dSLionel Sambuc goto err;
1186*0a6a1f1dSLionel Sambuc if (!sk_X509_push(cert_sk, x)) {
1187ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1188ebfedea0SLionel Sambuc goto err;
1189ebfedea0SLionel Sambuc }
1190ebfedea0SLionel Sambuc }
1191ebfedea0SLionel Sambuc }
1192*0a6a1f1dSLionel Sambuc /*
1193*0a6a1f1dSLionel Sambuc * we have a stack of newly certified certificates and a data base
1194*0a6a1f1dSLionel Sambuc * and serial number that need updating
1195*0a6a1f1dSLionel Sambuc */
1196ebfedea0SLionel Sambuc
1197*0a6a1f1dSLionel Sambuc if (sk_X509_num(cert_sk) > 0) {
1198*0a6a1f1dSLionel Sambuc if (!batch) {
1199*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1200*0a6a1f1dSLionel Sambuc "\n%d out of %d certificate requests certified, commit? [y/n]",
1201*0a6a1f1dSLionel Sambuc total_done, total);
1202ebfedea0SLionel Sambuc (void)BIO_flush(bio_err);
1203ebfedea0SLionel Sambuc buf[0][0] = '\0';
1204*0a6a1f1dSLionel Sambuc if (!fgets(buf[0], 10, stdin)) {
1205*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1206*0a6a1f1dSLionel Sambuc "CERTIFICATION CANCELED: I/O error\n");
1207ebfedea0SLionel Sambuc ret = 0;
1208ebfedea0SLionel Sambuc goto err;
1209ebfedea0SLionel Sambuc }
1210*0a6a1f1dSLionel Sambuc if ((buf[0][0] != 'y') && (buf[0][0] != 'Y')) {
1211ebfedea0SLionel Sambuc BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
1212ebfedea0SLionel Sambuc ret = 0;
1213ebfedea0SLionel Sambuc goto err;
1214ebfedea0SLionel Sambuc }
1215ebfedea0SLionel Sambuc }
1216ebfedea0SLionel Sambuc
1217*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Write out database with %d new entries\n",
1218*0a6a1f1dSLionel Sambuc sk_X509_num(cert_sk));
1219ebfedea0SLionel Sambuc
1220*0a6a1f1dSLionel Sambuc if (!save_serial(serialfile, "new", serial, NULL))
1221*0a6a1f1dSLionel Sambuc goto err;
1222ebfedea0SLionel Sambuc
1223*0a6a1f1dSLionel Sambuc if (!save_index(dbfile, "new", db))
1224*0a6a1f1dSLionel Sambuc goto err;
1225ebfedea0SLionel Sambuc }
1226ebfedea0SLionel Sambuc
1227ebfedea0SLionel Sambuc if (verbose)
1228ebfedea0SLionel Sambuc BIO_printf(bio_err, "writing new certificates\n");
1229*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_num(cert_sk); i++) {
1230ebfedea0SLionel Sambuc int k;
1231ebfedea0SLionel Sambuc char *n;
1232ebfedea0SLionel Sambuc
1233ebfedea0SLionel Sambuc x = sk_X509_value(cert_sk, i);
1234ebfedea0SLionel Sambuc
1235ebfedea0SLionel Sambuc j = x->cert_info->serialNumber->length;
1236ebfedea0SLionel Sambuc p = (const char *)x->cert_info->serialNumber->data;
1237ebfedea0SLionel Sambuc
1238*0a6a1f1dSLionel Sambuc if (strlen(outdir) >= (size_t)(j ? BSIZE - j * 2 - 6 : BSIZE - 8)) {
1239ebfedea0SLionel Sambuc BIO_printf(bio_err, "certificate file name too long\n");
1240ebfedea0SLionel Sambuc goto err;
1241ebfedea0SLionel Sambuc }
1242ebfedea0SLionel Sambuc
1243ebfedea0SLionel Sambuc strlcpy(buf[2], outdir, sizeof(buf[2]));
1244ebfedea0SLionel Sambuc
1245ebfedea0SLionel Sambuc #ifndef OPENSSL_SYS_VMS
1246ebfedea0SLionel Sambuc BUF_strlcat(buf[2], "/", sizeof(buf[2]));
1247ebfedea0SLionel Sambuc #endif
1248ebfedea0SLionel Sambuc
1249ebfedea0SLionel Sambuc n = (char *)&(buf[2][strlen(buf[2])]);
1250*0a6a1f1dSLionel Sambuc if (j > 0) {
1251*0a6a1f1dSLionel Sambuc for (k = 0; k < j; k++) {
1252ebfedea0SLionel Sambuc if (n >= &(buf[2][sizeof(buf[2])]))
1253ebfedea0SLionel Sambuc break;
1254ebfedea0SLionel Sambuc BIO_snprintf(n,
1255ebfedea0SLionel Sambuc &buf[2][0] + sizeof(buf[2]) - n,
1256ebfedea0SLionel Sambuc "%02X", (unsigned char)*(p++));
1257ebfedea0SLionel Sambuc n += 2;
1258ebfedea0SLionel Sambuc }
1259*0a6a1f1dSLionel Sambuc } else {
1260ebfedea0SLionel Sambuc *(n++) = '0';
1261ebfedea0SLionel Sambuc *(n++) = '0';
1262ebfedea0SLionel Sambuc }
1263*0a6a1f1dSLionel Sambuc *(n++) = '.';
1264*0a6a1f1dSLionel Sambuc *(n++) = 'p';
1265*0a6a1f1dSLionel Sambuc *(n++) = 'e';
1266*0a6a1f1dSLionel Sambuc *(n++) = 'm';
1267ebfedea0SLionel Sambuc *n = '\0';
1268ebfedea0SLionel Sambuc if (verbose)
1269ebfedea0SLionel Sambuc BIO_printf(bio_err, "writing %s\n", buf[2]);
1270ebfedea0SLionel Sambuc
1271*0a6a1f1dSLionel Sambuc if (BIO_write_filename(Cout, buf[2]) <= 0) {
1272ebfedea0SLionel Sambuc perror(buf[2]);
1273ebfedea0SLionel Sambuc goto err;
1274ebfedea0SLionel Sambuc }
1275ebfedea0SLionel Sambuc write_new_certificate(Cout, x, 0, notext);
1276ebfedea0SLionel Sambuc write_new_certificate(Sout, x, output_der, notext);
1277ebfedea0SLionel Sambuc }
1278ebfedea0SLionel Sambuc
1279*0a6a1f1dSLionel Sambuc if (sk_X509_num(cert_sk)) {
1280ebfedea0SLionel Sambuc /* Rename the database and the serial file */
1281*0a6a1f1dSLionel Sambuc if (!rotate_serial(serialfile, "new", "old"))
1282*0a6a1f1dSLionel Sambuc goto err;
1283ebfedea0SLionel Sambuc
1284*0a6a1f1dSLionel Sambuc if (!rotate_index(dbfile, "new", "old"))
1285*0a6a1f1dSLionel Sambuc goto err;
1286ebfedea0SLionel Sambuc
1287ebfedea0SLionel Sambuc BIO_printf(bio_err, "Data Base Updated\n");
1288ebfedea0SLionel Sambuc }
1289ebfedea0SLionel Sambuc }
1290ebfedea0SLionel Sambuc
1291ebfedea0SLionel Sambuc /*****************************************************************/
1292*0a6a1f1dSLionel Sambuc if (gencrl) {
1293ebfedea0SLionel Sambuc int crl_v2 = 0;
1294*0a6a1f1dSLionel Sambuc if (!crl_ext) {
1295ebfedea0SLionel Sambuc crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
1296ebfedea0SLionel Sambuc if (!crl_ext)
1297ebfedea0SLionel Sambuc ERR_clear_error();
1298ebfedea0SLionel Sambuc }
1299*0a6a1f1dSLionel Sambuc if (crl_ext) {
1300ebfedea0SLionel Sambuc /* Check syntax of file */
1301ebfedea0SLionel Sambuc X509V3_CTX ctx;
1302ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx);
1303ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, conf);
1304*0a6a1f1dSLionel Sambuc if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
1305ebfedea0SLionel Sambuc BIO_printf(bio_err,
1306ebfedea0SLionel Sambuc "Error Loading CRL extension section %s\n",
1307ebfedea0SLionel Sambuc crl_ext);
1308ebfedea0SLionel Sambuc ret = 1;
1309ebfedea0SLionel Sambuc goto err;
1310ebfedea0SLionel Sambuc }
1311ebfedea0SLionel Sambuc }
1312ebfedea0SLionel Sambuc
1313ebfedea0SLionel Sambuc if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
1314ebfedea0SLionel Sambuc != NULL)
1315*0a6a1f1dSLionel Sambuc if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
1316ebfedea0SLionel Sambuc BIO_printf(bio_err, "error while loading CRL number\n");
1317ebfedea0SLionel Sambuc goto err;
1318ebfedea0SLionel Sambuc }
1319ebfedea0SLionel Sambuc
1320*0a6a1f1dSLionel Sambuc if (!crldays && !crlhours && !crlsec) {
1321ebfedea0SLionel Sambuc if (!NCONF_get_number(conf, section,
1322ebfedea0SLionel Sambuc ENV_DEFAULT_CRL_DAYS, &crldays))
1323ebfedea0SLionel Sambuc crldays = 0;
1324ebfedea0SLionel Sambuc if (!NCONF_get_number(conf, section,
1325ebfedea0SLionel Sambuc ENV_DEFAULT_CRL_HOURS, &crlhours))
1326ebfedea0SLionel Sambuc crlhours = 0;
1327ebfedea0SLionel Sambuc ERR_clear_error();
1328ebfedea0SLionel Sambuc }
1329*0a6a1f1dSLionel Sambuc if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
1330*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1331*0a6a1f1dSLionel Sambuc "cannot lookup how long until the next CRL is issued\n");
1332ebfedea0SLionel Sambuc goto err;
1333ebfedea0SLionel Sambuc }
1334ebfedea0SLionel Sambuc
1335*0a6a1f1dSLionel Sambuc if (verbose)
1336*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "making CRL\n");
1337*0a6a1f1dSLionel Sambuc if ((crl = X509_CRL_new()) == NULL)
1338*0a6a1f1dSLionel Sambuc goto err;
1339*0a6a1f1dSLionel Sambuc if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
1340*0a6a1f1dSLionel Sambuc goto err;
1341ebfedea0SLionel Sambuc
1342ebfedea0SLionel Sambuc tmptm = ASN1_TIME_new();
1343*0a6a1f1dSLionel Sambuc if (!tmptm)
1344*0a6a1f1dSLionel Sambuc goto err;
1345ebfedea0SLionel Sambuc X509_gmtime_adj(tmptm, 0);
1346ebfedea0SLionel Sambuc X509_CRL_set_lastUpdate(crl, tmptm);
1347ebfedea0SLionel Sambuc if (!X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,
1348*0a6a1f1dSLionel Sambuc NULL)) {
1349ebfedea0SLionel Sambuc BIO_puts(bio_err, "error setting CRL nextUpdate\n");
1350ebfedea0SLionel Sambuc goto err;
1351ebfedea0SLionel Sambuc }
1352ebfedea0SLionel Sambuc X509_CRL_set_nextUpdate(crl, tmptm);
1353ebfedea0SLionel Sambuc
1354ebfedea0SLionel Sambuc ASN1_TIME_free(tmptm);
1355ebfedea0SLionel Sambuc
1356*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
1357ebfedea0SLionel Sambuc pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
1358*0a6a1f1dSLionel Sambuc if (pp[DB_type][0] == DB_TYPE_REV) {
1359*0a6a1f1dSLionel Sambuc if ((r = X509_REVOKED_new()) == NULL)
1360*0a6a1f1dSLionel Sambuc goto err;
1361ebfedea0SLionel Sambuc j = make_revoked(r, pp[DB_rev_date]);
1362*0a6a1f1dSLionel Sambuc if (!j)
1363*0a6a1f1dSLionel Sambuc goto err;
1364*0a6a1f1dSLionel Sambuc if (j == 2)
1365*0a6a1f1dSLionel Sambuc crl_v2 = 1;
1366ebfedea0SLionel Sambuc if (!BN_hex2bn(&serial, pp[DB_serial]))
1367ebfedea0SLionel Sambuc goto err;
1368ebfedea0SLionel Sambuc tmpser = BN_to_ASN1_INTEGER(serial, NULL);
1369ebfedea0SLionel Sambuc BN_free(serial);
1370ebfedea0SLionel Sambuc serial = NULL;
1371ebfedea0SLionel Sambuc if (!tmpser)
1372ebfedea0SLionel Sambuc goto err;
1373ebfedea0SLionel Sambuc X509_REVOKED_set_serialNumber(r, tmpser);
1374ebfedea0SLionel Sambuc ASN1_INTEGER_free(tmpser);
1375ebfedea0SLionel Sambuc X509_CRL_add0_revoked(crl, r);
1376ebfedea0SLionel Sambuc }
1377ebfedea0SLionel Sambuc }
1378ebfedea0SLionel Sambuc
1379*0a6a1f1dSLionel Sambuc /*
1380*0a6a1f1dSLionel Sambuc * sort the data so it will be written in serial number order
1381*0a6a1f1dSLionel Sambuc */
1382ebfedea0SLionel Sambuc X509_CRL_sort(crl);
1383ebfedea0SLionel Sambuc
1384ebfedea0SLionel Sambuc /* we now have a CRL */
1385*0a6a1f1dSLionel Sambuc if (verbose)
1386*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "signing CRL\n");
1387ebfedea0SLionel Sambuc
1388ebfedea0SLionel Sambuc /* Add any extensions asked for */
1389ebfedea0SLionel Sambuc
1390*0a6a1f1dSLionel Sambuc if (crl_ext || crlnumberfile != NULL) {
1391ebfedea0SLionel Sambuc X509V3_CTX crlctx;
1392ebfedea0SLionel Sambuc X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
1393ebfedea0SLionel Sambuc X509V3_set_nconf(&crlctx, conf);
1394ebfedea0SLionel Sambuc
1395ebfedea0SLionel Sambuc if (crl_ext)
1396*0a6a1f1dSLionel Sambuc if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
1397*0a6a1f1dSLionel Sambuc goto err;
1398*0a6a1f1dSLionel Sambuc if (crlnumberfile != NULL) {
1399ebfedea0SLionel Sambuc tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
1400*0a6a1f1dSLionel Sambuc if (!tmpser)
1401*0a6a1f1dSLionel Sambuc goto err;
1402ebfedea0SLionel Sambuc X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
1403ebfedea0SLionel Sambuc ASN1_INTEGER_free(tmpser);
1404ebfedea0SLionel Sambuc crl_v2 = 1;
1405*0a6a1f1dSLionel Sambuc if (!BN_add_word(crlnumber, 1))
1406*0a6a1f1dSLionel Sambuc goto err;
1407ebfedea0SLionel Sambuc }
1408ebfedea0SLionel Sambuc }
1409*0a6a1f1dSLionel Sambuc if (crl_ext || crl_v2) {
1410ebfedea0SLionel Sambuc if (!X509_CRL_set_version(crl, 1))
1411ebfedea0SLionel Sambuc goto err; /* version 2 CRL */
1412ebfedea0SLionel Sambuc }
1413ebfedea0SLionel Sambuc
1414*0a6a1f1dSLionel Sambuc /* we have a CRL number that need updating */
1415*0a6a1f1dSLionel Sambuc if (crlnumberfile != NULL)
1416*0a6a1f1dSLionel Sambuc if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
1417*0a6a1f1dSLionel Sambuc goto err;
1418ebfedea0SLionel Sambuc
1419*0a6a1f1dSLionel Sambuc if (crlnumber) {
1420ebfedea0SLionel Sambuc BN_free(crlnumber);
1421ebfedea0SLionel Sambuc crlnumber = NULL;
1422ebfedea0SLionel Sambuc }
1423ebfedea0SLionel Sambuc
1424*0a6a1f1dSLionel Sambuc if (!do_X509_CRL_sign(bio_err, crl, pkey, dgst, sigopts))
1425*0a6a1f1dSLionel Sambuc goto err;
1426ebfedea0SLionel Sambuc
1427ebfedea0SLionel Sambuc PEM_write_bio_X509_CRL(Sout, crl);
1428ebfedea0SLionel Sambuc
1429ebfedea0SLionel Sambuc if (crlnumberfile != NULL) /* Rename the crlnumber file */
1430*0a6a1f1dSLionel Sambuc if (!rotate_serial(crlnumberfile, "new", "old"))
1431*0a6a1f1dSLionel Sambuc goto err;
1432ebfedea0SLionel Sambuc
1433ebfedea0SLionel Sambuc }
1434ebfedea0SLionel Sambuc /*****************************************************************/
1435*0a6a1f1dSLionel Sambuc if (dorevoke) {
1436*0a6a1f1dSLionel Sambuc if (infile == NULL) {
1437ebfedea0SLionel Sambuc BIO_printf(bio_err, "no input files\n");
1438ebfedea0SLionel Sambuc goto err;
1439*0a6a1f1dSLionel Sambuc } else {
1440ebfedea0SLionel Sambuc X509 *revcert;
1441*0a6a1f1dSLionel Sambuc revcert = load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile);
1442ebfedea0SLionel Sambuc if (revcert == NULL)
1443ebfedea0SLionel Sambuc goto err;
1444ebfedea0SLionel Sambuc j = do_revoke(revcert, db, rev_type, rev_arg);
1445*0a6a1f1dSLionel Sambuc if (j <= 0)
1446*0a6a1f1dSLionel Sambuc goto err;
1447ebfedea0SLionel Sambuc X509_free(revcert);
1448ebfedea0SLionel Sambuc
1449*0a6a1f1dSLionel Sambuc if (!save_index(dbfile, "new", db))
1450*0a6a1f1dSLionel Sambuc goto err;
1451ebfedea0SLionel Sambuc
1452*0a6a1f1dSLionel Sambuc if (!rotate_index(dbfile, "new", "old"))
1453*0a6a1f1dSLionel Sambuc goto err;
1454ebfedea0SLionel Sambuc
1455ebfedea0SLionel Sambuc BIO_printf(bio_err, "Data Base Updated\n");
1456ebfedea0SLionel Sambuc }
1457ebfedea0SLionel Sambuc }
1458ebfedea0SLionel Sambuc /*****************************************************************/
1459ebfedea0SLionel Sambuc ret = 0;
1460ebfedea0SLionel Sambuc err:
1461ebfedea0SLionel Sambuc if (tofree)
1462ebfedea0SLionel Sambuc OPENSSL_free(tofree);
1463ebfedea0SLionel Sambuc BIO_free_all(Cout);
1464ebfedea0SLionel Sambuc BIO_free_all(Sout);
1465ebfedea0SLionel Sambuc BIO_free_all(out);
1466ebfedea0SLionel Sambuc BIO_free_all(in);
1467ebfedea0SLionel Sambuc
1468ebfedea0SLionel Sambuc if (cert_sk)
1469ebfedea0SLionel Sambuc sk_X509_pop_free(cert_sk, X509_free);
1470ebfedea0SLionel Sambuc
1471*0a6a1f1dSLionel Sambuc if (ret)
1472*0a6a1f1dSLionel Sambuc ERR_print_errors(bio_err);
1473ebfedea0SLionel Sambuc app_RAND_write_file(randfile, bio_err);
1474ebfedea0SLionel Sambuc if (free_key && key)
1475ebfedea0SLionel Sambuc OPENSSL_free(key);
1476ebfedea0SLionel Sambuc BN_free(serial);
1477ebfedea0SLionel Sambuc BN_free(crlnumber);
1478ebfedea0SLionel Sambuc free_index(db);
1479ebfedea0SLionel Sambuc if (sigopts)
1480ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(sigopts);
1481ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
1482*0a6a1f1dSLionel Sambuc if (x509)
1483*0a6a1f1dSLionel Sambuc X509_free(x509);
1484ebfedea0SLionel Sambuc X509_CRL_free(crl);
1485ebfedea0SLionel Sambuc NCONF_free(conf);
1486ebfedea0SLionel Sambuc NCONF_free(extconf);
1487ebfedea0SLionel Sambuc OBJ_cleanup();
1488ebfedea0SLionel Sambuc apps_shutdown();
1489ebfedea0SLionel Sambuc OPENSSL_EXIT(ret);
1490ebfedea0SLionel Sambuc }
1491ebfedea0SLionel Sambuc
lookup_fail(const char * name,const char * tag)1492ebfedea0SLionel Sambuc static void lookup_fail(const char *name, const char *tag)
1493ebfedea0SLionel Sambuc {
1494ebfedea0SLionel Sambuc BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
1495ebfedea0SLionel Sambuc }
1496ebfedea0SLionel Sambuc
certify(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)1497ebfedea0SLionel Sambuc static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1498ebfedea0SLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1499ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1500*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
1501*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate, char *enddate,
1502*0a6a1f1dSLionel Sambuc long days, int batch, char *ext_sect, CONF *lconf,
1503*0a6a1f1dSLionel Sambuc int verbose, unsigned long certopt, unsigned long nameopt,
1504*0a6a1f1dSLionel Sambuc int default_op, int ext_copy, int selfsign)
1505ebfedea0SLionel Sambuc {
1506ebfedea0SLionel Sambuc X509_REQ *req = NULL;
1507ebfedea0SLionel Sambuc BIO *in = NULL;
1508ebfedea0SLionel Sambuc EVP_PKEY *pktmp = NULL;
1509ebfedea0SLionel Sambuc int ok = -1, i;
1510ebfedea0SLionel Sambuc
1511ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file());
1512ebfedea0SLionel Sambuc
1513*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, infile) <= 0) {
1514ebfedea0SLionel Sambuc perror(infile);
1515ebfedea0SLionel Sambuc goto err;
1516ebfedea0SLionel Sambuc }
1517*0a6a1f1dSLionel Sambuc if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
1518ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading certificate request in %s\n",
1519ebfedea0SLionel Sambuc infile);
1520ebfedea0SLionel Sambuc goto err;
1521ebfedea0SLionel Sambuc }
1522ebfedea0SLionel Sambuc if (verbose)
1523ebfedea0SLionel Sambuc X509_REQ_print(bio_err, req);
1524ebfedea0SLionel Sambuc
1525ebfedea0SLionel Sambuc BIO_printf(bio_err, "Check that the request matches the signature\n");
1526ebfedea0SLionel Sambuc
1527*0a6a1f1dSLionel Sambuc if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
1528*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1529*0a6a1f1dSLionel Sambuc "Certificate request and CA private key do not match\n");
1530ebfedea0SLionel Sambuc ok = 0;
1531ebfedea0SLionel Sambuc goto err;
1532ebfedea0SLionel Sambuc }
1533*0a6a1f1dSLionel Sambuc if ((pktmp = X509_REQ_get_pubkey(req)) == NULL) {
1534ebfedea0SLionel Sambuc BIO_printf(bio_err, "error unpacking public key\n");
1535ebfedea0SLionel Sambuc goto err;
1536ebfedea0SLionel Sambuc }
1537ebfedea0SLionel Sambuc i = X509_REQ_verify(req, pktmp);
1538ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
1539*0a6a1f1dSLionel Sambuc if (i < 0) {
1540ebfedea0SLionel Sambuc ok = 0;
1541ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature verification problems....\n");
1542*0a6a1f1dSLionel Sambuc ERR_print_errors(bio_err);
1543ebfedea0SLionel Sambuc goto err;
1544ebfedea0SLionel Sambuc }
1545*0a6a1f1dSLionel Sambuc if (i == 0) {
1546ebfedea0SLionel Sambuc ok = 0;
1547*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1548*0a6a1f1dSLionel Sambuc "Signature did not match the certificate request\n");
1549*0a6a1f1dSLionel Sambuc ERR_print_errors(bio_err);
1550ebfedea0SLionel Sambuc goto err;
1551*0a6a1f1dSLionel Sambuc } else
1552ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature ok\n");
1553ebfedea0SLionel Sambuc
1554*0a6a1f1dSLionel Sambuc ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1555*0a6a1f1dSLionel Sambuc chtype, multirdn, email_dn, startdate, enddate, days, batch,
1556*0a6a1f1dSLionel Sambuc verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
1557*0a6a1f1dSLionel Sambuc ext_copy, selfsign);
1558ebfedea0SLionel Sambuc
1559ebfedea0SLionel Sambuc err:
1560*0a6a1f1dSLionel Sambuc if (req != NULL)
1561*0a6a1f1dSLionel Sambuc X509_REQ_free(req);
1562*0a6a1f1dSLionel Sambuc if (in != NULL)
1563*0a6a1f1dSLionel Sambuc BIO_free(in);
1564ebfedea0SLionel Sambuc return (ok);
1565ebfedea0SLionel Sambuc }
1566ebfedea0SLionel Sambuc
certify_cert(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,ENGINE * e)1567ebfedea0SLionel Sambuc static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1568ebfedea0SLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1569ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1570*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
1571*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate,
1572*0a6a1f1dSLionel Sambuc char *enddate, long days, int batch, char *ext_sect,
1573*0a6a1f1dSLionel Sambuc CONF *lconf, int verbose, unsigned long certopt,
1574*0a6a1f1dSLionel Sambuc unsigned long nameopt, int default_op, int ext_copy,
1575*0a6a1f1dSLionel Sambuc ENGINE *e)
1576ebfedea0SLionel Sambuc {
1577ebfedea0SLionel Sambuc X509 *req = NULL;
1578ebfedea0SLionel Sambuc X509_REQ *rreq = NULL;
1579ebfedea0SLionel Sambuc EVP_PKEY *pktmp = NULL;
1580ebfedea0SLionel Sambuc int ok = -1, i;
1581ebfedea0SLionel Sambuc
1582*0a6a1f1dSLionel Sambuc if ((req =
1583*0a6a1f1dSLionel Sambuc load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile)) == NULL)
1584ebfedea0SLionel Sambuc goto err;
1585ebfedea0SLionel Sambuc if (verbose)
1586ebfedea0SLionel Sambuc X509_print(bio_err, req);
1587ebfedea0SLionel Sambuc
1588ebfedea0SLionel Sambuc BIO_printf(bio_err, "Check that the request matches the signature\n");
1589ebfedea0SLionel Sambuc
1590*0a6a1f1dSLionel Sambuc if ((pktmp = X509_get_pubkey(req)) == NULL) {
1591ebfedea0SLionel Sambuc BIO_printf(bio_err, "error unpacking public key\n");
1592ebfedea0SLionel Sambuc goto err;
1593ebfedea0SLionel Sambuc }
1594ebfedea0SLionel Sambuc i = X509_verify(req, pktmp);
1595ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
1596*0a6a1f1dSLionel Sambuc if (i < 0) {
1597ebfedea0SLionel Sambuc ok = 0;
1598ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature verification problems....\n");
1599ebfedea0SLionel Sambuc goto err;
1600ebfedea0SLionel Sambuc }
1601*0a6a1f1dSLionel Sambuc if (i == 0) {
1602ebfedea0SLionel Sambuc ok = 0;
1603ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature did not match the certificate\n");
1604ebfedea0SLionel Sambuc goto err;
1605*0a6a1f1dSLionel Sambuc } else
1606ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature ok\n");
1607ebfedea0SLionel Sambuc
1608ebfedea0SLionel Sambuc if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL)
1609ebfedea0SLionel Sambuc goto err;
1610ebfedea0SLionel Sambuc
1611*0a6a1f1dSLionel Sambuc ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1612*0a6a1f1dSLionel Sambuc chtype, multirdn, email_dn, startdate, enddate, days, batch,
1613*0a6a1f1dSLionel Sambuc verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
1614ebfedea0SLionel Sambuc ext_copy, 0);
1615ebfedea0SLionel Sambuc
1616ebfedea0SLionel Sambuc err:
1617*0a6a1f1dSLionel Sambuc if (rreq != NULL)
1618*0a6a1f1dSLionel Sambuc X509_REQ_free(rreq);
1619*0a6a1f1dSLionel Sambuc if (req != NULL)
1620*0a6a1f1dSLionel Sambuc X509_free(req);
1621ebfedea0SLionel Sambuc return (ok);
1622ebfedea0SLionel Sambuc }
1623ebfedea0SLionel Sambuc
do_body(X509 ** xret,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,int verbose,X509_REQ * req,char * ext_sect,CONF * lconf,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)1624*0a6a1f1dSLionel Sambuc static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
1625*0a6a1f1dSLionel Sambuc const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1626*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
1627*0a6a1f1dSLionel Sambuc char *subj, unsigned long chtype, int multirdn,
1628*0a6a1f1dSLionel Sambuc int email_dn, char *startdate, char *enddate, long days,
1629*0a6a1f1dSLionel Sambuc int batch, int verbose, X509_REQ *req, char *ext_sect,
1630*0a6a1f1dSLionel Sambuc CONF *lconf, unsigned long certopt, unsigned long nameopt,
1631*0a6a1f1dSLionel Sambuc int default_op, int ext_copy, int selfsign)
1632ebfedea0SLionel Sambuc {
1633*0a6a1f1dSLionel Sambuc X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject =
1634*0a6a1f1dSLionel Sambuc NULL;
1635ebfedea0SLionel Sambuc ASN1_UTCTIME *tm, *tmptm;
1636ebfedea0SLionel Sambuc ASN1_STRING *str, *str2;
1637ebfedea0SLionel Sambuc ASN1_OBJECT *obj;
1638ebfedea0SLionel Sambuc X509 *ret = NULL;
1639ebfedea0SLionel Sambuc X509_CINF *ci;
1640ebfedea0SLionel Sambuc X509_NAME_ENTRY *ne;
1641ebfedea0SLionel Sambuc X509_NAME_ENTRY *tne, *push;
1642ebfedea0SLionel Sambuc EVP_PKEY *pktmp;
1643ebfedea0SLionel Sambuc int ok = -1, i, j, last, nid;
1644ebfedea0SLionel Sambuc const char *p;
1645ebfedea0SLionel Sambuc CONF_VALUE *cv;
1646ebfedea0SLionel Sambuc OPENSSL_STRING row[DB_NUMBER];
1647ebfedea0SLionel Sambuc OPENSSL_STRING *irow = NULL;
1648ebfedea0SLionel Sambuc OPENSSL_STRING *rrow = NULL;
1649ebfedea0SLionel Sambuc char buf[25];
1650ebfedea0SLionel Sambuc
1651ebfedea0SLionel Sambuc tmptm = ASN1_UTCTIME_new();
1652*0a6a1f1dSLionel Sambuc if (tmptm == NULL) {
1653ebfedea0SLionel Sambuc BIO_printf(bio_err, "malloc error\n");
1654ebfedea0SLionel Sambuc return (0);
1655ebfedea0SLionel Sambuc }
1656ebfedea0SLionel Sambuc
1657ebfedea0SLionel Sambuc for (i = 0; i < DB_NUMBER; i++)
1658ebfedea0SLionel Sambuc row[i] = NULL;
1659ebfedea0SLionel Sambuc
1660*0a6a1f1dSLionel Sambuc if (subj) {
1661ebfedea0SLionel Sambuc X509_NAME *n = parse_name(subj, chtype, multirdn);
1662ebfedea0SLionel Sambuc
1663*0a6a1f1dSLionel Sambuc if (!n) {
1664ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1665ebfedea0SLionel Sambuc goto err;
1666ebfedea0SLionel Sambuc }
1667ebfedea0SLionel Sambuc X509_REQ_set_subject_name(req, n);
1668ebfedea0SLionel Sambuc req->req_info->enc.modified = 1;
1669ebfedea0SLionel Sambuc X509_NAME_free(n);
1670ebfedea0SLionel Sambuc }
1671ebfedea0SLionel Sambuc
1672ebfedea0SLionel Sambuc if (default_op)
1673*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1674*0a6a1f1dSLionel Sambuc "The Subject's Distinguished Name is as follows\n");
1675ebfedea0SLionel Sambuc
1676ebfedea0SLionel Sambuc name = X509_REQ_get_subject_name(req);
1677*0a6a1f1dSLionel Sambuc for (i = 0; i < X509_NAME_entry_count(name); i++) {
1678ebfedea0SLionel Sambuc ne = X509_NAME_get_entry(name, i);
1679ebfedea0SLionel Sambuc str = X509_NAME_ENTRY_get_data(ne);
1680ebfedea0SLionel Sambuc obj = X509_NAME_ENTRY_get_object(ne);
1681ebfedea0SLionel Sambuc
1682*0a6a1f1dSLionel Sambuc if (msie_hack) {
1683ebfedea0SLionel Sambuc /* assume all type should be strings */
1684ebfedea0SLionel Sambuc nid = OBJ_obj2nid(ne->object);
1685ebfedea0SLionel Sambuc
1686ebfedea0SLionel Sambuc if (str->type == V_ASN1_UNIVERSALSTRING)
1687ebfedea0SLionel Sambuc ASN1_UNIVERSALSTRING_to_string(str);
1688ebfedea0SLionel Sambuc
1689ebfedea0SLionel Sambuc if ((str->type == V_ASN1_IA5STRING) &&
1690ebfedea0SLionel Sambuc (nid != NID_pkcs9_emailAddress))
1691ebfedea0SLionel Sambuc str->type = V_ASN1_T61STRING;
1692ebfedea0SLionel Sambuc
1693ebfedea0SLionel Sambuc if ((nid == NID_pkcs9_emailAddress) &&
1694ebfedea0SLionel Sambuc (str->type == V_ASN1_PRINTABLESTRING))
1695ebfedea0SLionel Sambuc str->type = V_ASN1_IA5STRING;
1696ebfedea0SLionel Sambuc }
1697ebfedea0SLionel Sambuc
1698ebfedea0SLionel Sambuc /* If no EMAIL is wanted in the subject */
1699ebfedea0SLionel Sambuc if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
1700ebfedea0SLionel Sambuc continue;
1701ebfedea0SLionel Sambuc
1702ebfedea0SLionel Sambuc /* check some things */
1703ebfedea0SLionel Sambuc if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
1704*0a6a1f1dSLionel Sambuc (str->type != V_ASN1_IA5STRING)) {
1705*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1706*0a6a1f1dSLionel Sambuc "\nemailAddress type needs to be of type IA5STRING\n");
1707ebfedea0SLionel Sambuc goto err;
1708ebfedea0SLionel Sambuc }
1709*0a6a1f1dSLionel Sambuc if ((str->type != V_ASN1_BMPSTRING)
1710*0a6a1f1dSLionel Sambuc && (str->type != V_ASN1_UTF8STRING)) {
1711ebfedea0SLionel Sambuc j = ASN1_PRINTABLE_type(str->data, str->length);
1712ebfedea0SLionel Sambuc if (((j == V_ASN1_T61STRING) &&
1713ebfedea0SLionel Sambuc (str->type != V_ASN1_T61STRING)) ||
1714ebfedea0SLionel Sambuc ((j == V_ASN1_IA5STRING) &&
1715*0a6a1f1dSLionel Sambuc (str->type == V_ASN1_PRINTABLESTRING))) {
1716*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1717*0a6a1f1dSLionel Sambuc "\nThe string contains characters that are illegal for the ASN.1 type\n");
1718ebfedea0SLionel Sambuc goto err;
1719ebfedea0SLionel Sambuc }
1720ebfedea0SLionel Sambuc }
1721ebfedea0SLionel Sambuc
1722ebfedea0SLionel Sambuc if (default_op)
1723ebfedea0SLionel Sambuc old_entry_print(bio_err, obj, str);
1724ebfedea0SLionel Sambuc }
1725ebfedea0SLionel Sambuc
1726ebfedea0SLionel Sambuc /* Ok, now we check the 'policy' stuff. */
1727*0a6a1f1dSLionel Sambuc if ((subject = X509_NAME_new()) == NULL) {
1728ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1729ebfedea0SLionel Sambuc goto err;
1730ebfedea0SLionel Sambuc }
1731ebfedea0SLionel Sambuc
1732ebfedea0SLionel Sambuc /* take a copy of the issuer name before we mess with it. */
1733ebfedea0SLionel Sambuc if (selfsign)
1734ebfedea0SLionel Sambuc CAname = X509_NAME_dup(name);
1735ebfedea0SLionel Sambuc else
1736ebfedea0SLionel Sambuc CAname = X509_NAME_dup(x509->cert_info->subject);
1737*0a6a1f1dSLionel Sambuc if (CAname == NULL)
1738*0a6a1f1dSLionel Sambuc goto err;
1739ebfedea0SLionel Sambuc str = str2 = NULL;
1740ebfedea0SLionel Sambuc
1741*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
1742ebfedea0SLionel Sambuc cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
1743*0a6a1f1dSLionel Sambuc if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
1744*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1745*0a6a1f1dSLionel Sambuc "%s:unknown object type in 'policy' configuration\n",
1746*0a6a1f1dSLionel Sambuc cv->name);
1747ebfedea0SLionel Sambuc goto err;
1748ebfedea0SLionel Sambuc }
1749ebfedea0SLionel Sambuc obj = OBJ_nid2obj(j);
1750ebfedea0SLionel Sambuc
1751ebfedea0SLionel Sambuc last = -1;
1752*0a6a1f1dSLionel Sambuc for (;;) {
1753ebfedea0SLionel Sambuc /* lookup the object in the supplied name list */
1754ebfedea0SLionel Sambuc j = X509_NAME_get_index_by_OBJ(name, obj, last);
1755*0a6a1f1dSLionel Sambuc if (j < 0) {
1756*0a6a1f1dSLionel Sambuc if (last != -1)
1757*0a6a1f1dSLionel Sambuc break;
1758ebfedea0SLionel Sambuc tne = NULL;
1759*0a6a1f1dSLionel Sambuc } else {
1760ebfedea0SLionel Sambuc tne = X509_NAME_get_entry(name, j);
1761ebfedea0SLionel Sambuc }
1762ebfedea0SLionel Sambuc last = j;
1763ebfedea0SLionel Sambuc
1764ebfedea0SLionel Sambuc /* depending on the 'policy', decide what to do. */
1765ebfedea0SLionel Sambuc push = NULL;
1766*0a6a1f1dSLionel Sambuc if (strcmp(cv->value, "optional") == 0) {
1767ebfedea0SLionel Sambuc if (tne != NULL)
1768ebfedea0SLionel Sambuc push = tne;
1769*0a6a1f1dSLionel Sambuc } else if (strcmp(cv->value, "supplied") == 0) {
1770*0a6a1f1dSLionel Sambuc if (tne == NULL) {
1771*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1772*0a6a1f1dSLionel Sambuc "The %s field needed to be supplied and was missing\n",
1773*0a6a1f1dSLionel Sambuc cv->name);
1774ebfedea0SLionel Sambuc goto err;
1775*0a6a1f1dSLionel Sambuc } else
1776ebfedea0SLionel Sambuc push = tne;
1777*0a6a1f1dSLionel Sambuc } else if (strcmp(cv->value, "match") == 0) {
1778ebfedea0SLionel Sambuc int last2;
1779ebfedea0SLionel Sambuc
1780*0a6a1f1dSLionel Sambuc if (tne == NULL) {
1781*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1782*0a6a1f1dSLionel Sambuc "The mandatory %s field was missing\n",
1783*0a6a1f1dSLionel Sambuc cv->name);
1784ebfedea0SLionel Sambuc goto err;
1785ebfedea0SLionel Sambuc }
1786ebfedea0SLionel Sambuc
1787ebfedea0SLionel Sambuc last2 = -1;
1788ebfedea0SLionel Sambuc
1789ebfedea0SLionel Sambuc again2:
1790ebfedea0SLionel Sambuc j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
1791*0a6a1f1dSLionel Sambuc if ((j < 0) && (last2 == -1)) {
1792*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1793*0a6a1f1dSLionel Sambuc "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",
1794*0a6a1f1dSLionel Sambuc cv->name);
1795ebfedea0SLionel Sambuc goto err;
1796ebfedea0SLionel Sambuc }
1797*0a6a1f1dSLionel Sambuc if (j >= 0) {
1798ebfedea0SLionel Sambuc push = X509_NAME_get_entry(CAname, j);
1799ebfedea0SLionel Sambuc str = X509_NAME_ENTRY_get_data(tne);
1800ebfedea0SLionel Sambuc str2 = X509_NAME_ENTRY_get_data(push);
1801ebfedea0SLionel Sambuc last2 = j;
1802ebfedea0SLionel Sambuc if (ASN1_STRING_cmp(str, str2) != 0)
1803ebfedea0SLionel Sambuc goto again2;
1804ebfedea0SLionel Sambuc }
1805*0a6a1f1dSLionel Sambuc if (j < 0) {
1806*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1807*0a6a1f1dSLionel Sambuc "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",
1808*0a6a1f1dSLionel Sambuc cv->name,
1809*0a6a1f1dSLionel Sambuc ((str2 == NULL) ? "NULL" : (char *)str2->data),
1810*0a6a1f1dSLionel Sambuc ((str == NULL) ? "NULL" : (char *)str->data));
1811ebfedea0SLionel Sambuc goto err;
1812ebfedea0SLionel Sambuc }
1813*0a6a1f1dSLionel Sambuc } else {
1814*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1815*0a6a1f1dSLionel Sambuc "%s:invalid type in 'policy' configuration\n",
1816*0a6a1f1dSLionel Sambuc cv->value);
1817ebfedea0SLionel Sambuc goto err;
1818ebfedea0SLionel Sambuc }
1819ebfedea0SLionel Sambuc
1820*0a6a1f1dSLionel Sambuc if (push != NULL) {
1821*0a6a1f1dSLionel Sambuc if (!X509_NAME_add_entry(subject, push, -1, 0)) {
1822ebfedea0SLionel Sambuc if (push != NULL)
1823ebfedea0SLionel Sambuc X509_NAME_ENTRY_free(push);
1824ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1825ebfedea0SLionel Sambuc goto err;
1826ebfedea0SLionel Sambuc }
1827ebfedea0SLionel Sambuc }
1828*0a6a1f1dSLionel Sambuc if (j < 0)
1829*0a6a1f1dSLionel Sambuc break;
1830ebfedea0SLionel Sambuc }
1831ebfedea0SLionel Sambuc }
1832ebfedea0SLionel Sambuc
1833*0a6a1f1dSLionel Sambuc if (preserve) {
1834ebfedea0SLionel Sambuc X509_NAME_free(subject);
1835ebfedea0SLionel Sambuc /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1836ebfedea0SLionel Sambuc subject = X509_NAME_dup(name);
1837*0a6a1f1dSLionel Sambuc if (subject == NULL)
1838*0a6a1f1dSLionel Sambuc goto err;
1839ebfedea0SLionel Sambuc }
1840ebfedea0SLionel Sambuc
1841ebfedea0SLionel Sambuc if (verbose)
1842*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1843*0a6a1f1dSLionel Sambuc "The subject name appears to be ok, checking data base for clashes\n");
1844ebfedea0SLionel Sambuc
1845ebfedea0SLionel Sambuc /* Build the correct Subject if no e-mail is wanted in the subject */
1846*0a6a1f1dSLionel Sambuc /*
1847*0a6a1f1dSLionel Sambuc * and add it later on because of the method extensions are added
1848*0a6a1f1dSLionel Sambuc * (altName)
1849*0a6a1f1dSLionel Sambuc */
1850ebfedea0SLionel Sambuc
1851ebfedea0SLionel Sambuc if (email_dn)
1852ebfedea0SLionel Sambuc dn_subject = subject;
1853*0a6a1f1dSLionel Sambuc else {
1854ebfedea0SLionel Sambuc X509_NAME_ENTRY *tmpne;
1855*0a6a1f1dSLionel Sambuc /*
1856*0a6a1f1dSLionel Sambuc * Its best to dup the subject DN and then delete any email addresses
1857*0a6a1f1dSLionel Sambuc * because this retains its structure.
1858ebfedea0SLionel Sambuc */
1859*0a6a1f1dSLionel Sambuc if (!(dn_subject = X509_NAME_dup(subject))) {
1860ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1861ebfedea0SLionel Sambuc goto err;
1862ebfedea0SLionel Sambuc }
1863ebfedea0SLionel Sambuc while ((i = X509_NAME_get_index_by_NID(dn_subject,
1864*0a6a1f1dSLionel Sambuc NID_pkcs9_emailAddress,
1865*0a6a1f1dSLionel Sambuc -1)) >= 0) {
1866ebfedea0SLionel Sambuc tmpne = X509_NAME_get_entry(dn_subject, i);
1867ebfedea0SLionel Sambuc X509_NAME_delete_entry(dn_subject, i);
1868ebfedea0SLionel Sambuc X509_NAME_ENTRY_free(tmpne);
1869ebfedea0SLionel Sambuc }
1870ebfedea0SLionel Sambuc }
1871ebfedea0SLionel Sambuc
1872ebfedea0SLionel Sambuc if (BN_is_zero(serial))
1873ebfedea0SLionel Sambuc row[DB_serial] = BUF_strdup("00");
1874ebfedea0SLionel Sambuc else
1875ebfedea0SLionel Sambuc row[DB_serial] = BN_bn2hex(serial);
1876*0a6a1f1dSLionel Sambuc if (row[DB_serial] == NULL) {
1877ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
1878ebfedea0SLionel Sambuc goto err;
1879ebfedea0SLionel Sambuc }
1880ebfedea0SLionel Sambuc
1881*0a6a1f1dSLionel Sambuc if (db->attributes.unique_subject) {
1882ebfedea0SLionel Sambuc OPENSSL_STRING *crow = row;
1883ebfedea0SLionel Sambuc
1884ebfedea0SLionel Sambuc rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
1885*0a6a1f1dSLionel Sambuc if (rrow != NULL) {
1886ebfedea0SLionel Sambuc BIO_printf(bio_err,
1887ebfedea0SLionel Sambuc "ERROR:There is already a certificate for %s\n",
1888ebfedea0SLionel Sambuc row[DB_name]);
1889ebfedea0SLionel Sambuc }
1890ebfedea0SLionel Sambuc }
1891*0a6a1f1dSLionel Sambuc if (rrow == NULL) {
1892ebfedea0SLionel Sambuc rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1893*0a6a1f1dSLionel Sambuc if (rrow != NULL) {
1894*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1895*0a6a1f1dSLionel Sambuc "ERROR:Serial number %s has already been issued,\n",
1896ebfedea0SLionel Sambuc row[DB_serial]);
1897*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1898*0a6a1f1dSLionel Sambuc " check the database/serial_file for corruption\n");
1899ebfedea0SLionel Sambuc }
1900ebfedea0SLionel Sambuc }
1901ebfedea0SLionel Sambuc
1902*0a6a1f1dSLionel Sambuc if (rrow != NULL) {
1903*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "The matching entry has the following details\n");
1904ebfedea0SLionel Sambuc if (rrow[DB_type][0] == 'E')
1905ebfedea0SLionel Sambuc p = "Expired";
1906ebfedea0SLionel Sambuc else if (rrow[DB_type][0] == 'R')
1907ebfedea0SLionel Sambuc p = "Revoked";
1908ebfedea0SLionel Sambuc else if (rrow[DB_type][0] == 'V')
1909ebfedea0SLionel Sambuc p = "Valid";
1910ebfedea0SLionel Sambuc else
1911ebfedea0SLionel Sambuc p = "\ninvalid type, Data base error\n";
1912ebfedea0SLionel Sambuc BIO_printf(bio_err, "Type :%s\n", p);;
1913*0a6a1f1dSLionel Sambuc if (rrow[DB_type][0] == 'R') {
1914*0a6a1f1dSLionel Sambuc p = rrow[DB_exp_date];
1915*0a6a1f1dSLionel Sambuc if (p == NULL)
1916*0a6a1f1dSLionel Sambuc p = "undef";
1917ebfedea0SLionel Sambuc BIO_printf(bio_err, "Was revoked on:%s\n", p);
1918ebfedea0SLionel Sambuc }
1919*0a6a1f1dSLionel Sambuc p = rrow[DB_exp_date];
1920*0a6a1f1dSLionel Sambuc if (p == NULL)
1921*0a6a1f1dSLionel Sambuc p = "undef";
1922ebfedea0SLionel Sambuc BIO_printf(bio_err, "Expires on :%s\n", p);
1923*0a6a1f1dSLionel Sambuc p = rrow[DB_serial];
1924*0a6a1f1dSLionel Sambuc if (p == NULL)
1925*0a6a1f1dSLionel Sambuc p = "undef";
1926ebfedea0SLionel Sambuc BIO_printf(bio_err, "Serial Number :%s\n", p);
1927*0a6a1f1dSLionel Sambuc p = rrow[DB_file];
1928*0a6a1f1dSLionel Sambuc if (p == NULL)
1929*0a6a1f1dSLionel Sambuc p = "undef";
1930ebfedea0SLionel Sambuc BIO_printf(bio_err, "File name :%s\n", p);
1931*0a6a1f1dSLionel Sambuc p = rrow[DB_name];
1932*0a6a1f1dSLionel Sambuc if (p == NULL)
1933*0a6a1f1dSLionel Sambuc p = "undef";
1934ebfedea0SLionel Sambuc BIO_printf(bio_err, "Subject Name :%s\n", p);
1935ebfedea0SLionel Sambuc ok = -1; /* This is now a 'bad' error. */
1936ebfedea0SLionel Sambuc goto err;
1937ebfedea0SLionel Sambuc }
1938ebfedea0SLionel Sambuc
1939ebfedea0SLionel Sambuc /* We are now totally happy, lets make and sign the certificate */
1940ebfedea0SLionel Sambuc if (verbose)
1941*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1942*0a6a1f1dSLionel Sambuc "Everything appears to be ok, creating and signing the certificate\n");
1943ebfedea0SLionel Sambuc
1944*0a6a1f1dSLionel Sambuc if ((ret = X509_new()) == NULL)
1945*0a6a1f1dSLionel Sambuc goto err;
1946ebfedea0SLionel Sambuc ci = ret->cert_info;
1947ebfedea0SLionel Sambuc
1948ebfedea0SLionel Sambuc #ifdef X509_V3
1949ebfedea0SLionel Sambuc /* Make it an X509 v3 certificate. */
1950*0a6a1f1dSLionel Sambuc if (!X509_set_version(ret, 2))
1951*0a6a1f1dSLionel Sambuc goto err;
1952ebfedea0SLionel Sambuc #endif
1953ebfedea0SLionel Sambuc
1954ebfedea0SLionel Sambuc if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL)
1955ebfedea0SLionel Sambuc goto err;
1956*0a6a1f1dSLionel Sambuc if (selfsign) {
1957ebfedea0SLionel Sambuc if (!X509_set_issuer_name(ret, subject))
1958ebfedea0SLionel Sambuc goto err;
1959*0a6a1f1dSLionel Sambuc } else {
1960ebfedea0SLionel Sambuc if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
1961ebfedea0SLionel Sambuc goto err;
1962ebfedea0SLionel Sambuc }
1963ebfedea0SLionel Sambuc
1964ebfedea0SLionel Sambuc if (strcmp(startdate, "today") == 0)
1965ebfedea0SLionel Sambuc X509_gmtime_adj(X509_get_notBefore(ret), 0);
1966*0a6a1f1dSLionel Sambuc else
1967*0a6a1f1dSLionel Sambuc ASN1_TIME_set_string(X509_get_notBefore(ret), startdate);
1968ebfedea0SLionel Sambuc
1969ebfedea0SLionel Sambuc if (enddate == NULL)
1970ebfedea0SLionel Sambuc X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL);
1971*0a6a1f1dSLionel Sambuc else
1972*0a6a1f1dSLionel Sambuc ASN1_TIME_set_string(X509_get_notAfter(ret), enddate);
1973ebfedea0SLionel Sambuc
1974*0a6a1f1dSLionel Sambuc if (!X509_set_subject_name(ret, subject))
1975*0a6a1f1dSLionel Sambuc goto err;
1976ebfedea0SLionel Sambuc
1977ebfedea0SLionel Sambuc pktmp = X509_REQ_get_pubkey(req);
1978ebfedea0SLionel Sambuc i = X509_set_pubkey(ret, pktmp);
1979ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
1980*0a6a1f1dSLionel Sambuc if (!i)
1981*0a6a1f1dSLionel Sambuc goto err;
1982ebfedea0SLionel Sambuc
1983ebfedea0SLionel Sambuc /* Lets add the extensions, if there are any */
1984*0a6a1f1dSLionel Sambuc if (ext_sect) {
1985ebfedea0SLionel Sambuc X509V3_CTX ctx;
1986ebfedea0SLionel Sambuc if (ci->version == NULL)
1987ebfedea0SLionel Sambuc if ((ci->version = ASN1_INTEGER_new()) == NULL)
1988ebfedea0SLionel Sambuc goto err;
1989ebfedea0SLionel Sambuc ASN1_INTEGER_set(ci->version, 2); /* version 3 certificate */
1990ebfedea0SLionel Sambuc
1991*0a6a1f1dSLionel Sambuc /*
1992*0a6a1f1dSLionel Sambuc * Free the current entries if any, there should not be any I believe
1993*0a6a1f1dSLionel Sambuc */
1994ebfedea0SLionel Sambuc if (ci->extensions != NULL)
1995*0a6a1f1dSLionel Sambuc sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free);
1996ebfedea0SLionel Sambuc
1997ebfedea0SLionel Sambuc ci->extensions = NULL;
1998ebfedea0SLionel Sambuc
1999ebfedea0SLionel Sambuc /* Initialize the context structure */
2000ebfedea0SLionel Sambuc if (selfsign)
2001ebfedea0SLionel Sambuc X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
2002ebfedea0SLionel Sambuc else
2003ebfedea0SLionel Sambuc X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
2004ebfedea0SLionel Sambuc
2005*0a6a1f1dSLionel Sambuc if (extconf) {
2006ebfedea0SLionel Sambuc if (verbose)
2007ebfedea0SLionel Sambuc BIO_printf(bio_err, "Extra configuration file found\n");
2008ebfedea0SLionel Sambuc
2009ebfedea0SLionel Sambuc /* Use the extconf configuration db LHASH */
2010ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, extconf);
2011ebfedea0SLionel Sambuc
2012ebfedea0SLionel Sambuc /* Test the structure (needed?) */
2013ebfedea0SLionel Sambuc /* X509V3_set_ctx_test(&ctx); */
2014ebfedea0SLionel Sambuc
2015ebfedea0SLionel Sambuc /* Adds exts contained in the configuration file */
2016*0a6a1f1dSLionel Sambuc if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
2017ebfedea0SLionel Sambuc BIO_printf(bio_err,
2018ebfedea0SLionel Sambuc "ERROR: adding extensions in section %s\n",
2019ebfedea0SLionel Sambuc ext_sect);
2020ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2021ebfedea0SLionel Sambuc goto err;
2022ebfedea0SLionel Sambuc }
2023ebfedea0SLionel Sambuc if (verbose)
2024*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2025*0a6a1f1dSLionel Sambuc "Successfully added extensions from file.\n");
2026*0a6a1f1dSLionel Sambuc } else if (ext_sect) {
2027ebfedea0SLionel Sambuc /* We found extensions to be set from config file */
2028ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, lconf);
2029ebfedea0SLionel Sambuc
2030*0a6a1f1dSLionel Sambuc if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
2031*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2032*0a6a1f1dSLionel Sambuc "ERROR: adding extensions in section %s\n",
2033*0a6a1f1dSLionel Sambuc ext_sect);
2034ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2035ebfedea0SLionel Sambuc goto err;
2036ebfedea0SLionel Sambuc }
2037ebfedea0SLionel Sambuc
2038ebfedea0SLionel Sambuc if (verbose)
2039*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2040*0a6a1f1dSLionel Sambuc "Successfully added extensions from config\n");
2041ebfedea0SLionel Sambuc }
2042ebfedea0SLionel Sambuc }
2043ebfedea0SLionel Sambuc
2044ebfedea0SLionel Sambuc /* Copy extensions from request (if any) */
2045ebfedea0SLionel Sambuc
2046*0a6a1f1dSLionel Sambuc if (!copy_extensions(ret, req, ext_copy)) {
2047ebfedea0SLionel Sambuc BIO_printf(bio_err, "ERROR: adding extensions from request\n");
2048ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2049ebfedea0SLionel Sambuc goto err;
2050ebfedea0SLionel Sambuc }
2051ebfedea0SLionel Sambuc
2052ebfedea0SLionel Sambuc /* Set the right value for the noemailDN option */
2053*0a6a1f1dSLionel Sambuc if (email_dn == 0) {
2054*0a6a1f1dSLionel Sambuc if (!X509_set_subject_name(ret, dn_subject))
2055*0a6a1f1dSLionel Sambuc goto err;
2056ebfedea0SLionel Sambuc }
2057ebfedea0SLionel Sambuc
2058*0a6a1f1dSLionel Sambuc if (!default_op) {
2059ebfedea0SLionel Sambuc BIO_printf(bio_err, "Certificate Details:\n");
2060*0a6a1f1dSLionel Sambuc /*
2061*0a6a1f1dSLionel Sambuc * Never print signature details because signature not present
2062*0a6a1f1dSLionel Sambuc */
2063ebfedea0SLionel Sambuc certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
2064ebfedea0SLionel Sambuc X509_print_ex(bio_err, ret, nameopt, certopt);
2065ebfedea0SLionel Sambuc }
2066ebfedea0SLionel Sambuc
2067ebfedea0SLionel Sambuc BIO_printf(bio_err, "Certificate is to be certified until ");
2068ebfedea0SLionel Sambuc ASN1_TIME_print(bio_err, X509_get_notAfter(ret));
2069*0a6a1f1dSLionel Sambuc if (days)
2070*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, " (%ld days)", days);
2071ebfedea0SLionel Sambuc BIO_printf(bio_err, "\n");
2072ebfedea0SLionel Sambuc
2073*0a6a1f1dSLionel Sambuc if (!batch) {
2074ebfedea0SLionel Sambuc
2075ebfedea0SLionel Sambuc BIO_printf(bio_err, "Sign the certificate? [y/n]:");
2076ebfedea0SLionel Sambuc (void)BIO_flush(bio_err);
2077ebfedea0SLionel Sambuc buf[0] = '\0';
2078*0a6a1f1dSLionel Sambuc if (!fgets(buf, sizeof(buf) - 1, stdin)) {
2079*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2080*0a6a1f1dSLionel Sambuc "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
2081ebfedea0SLionel Sambuc ok = 0;
2082ebfedea0SLionel Sambuc goto err;
2083ebfedea0SLionel Sambuc }
2084*0a6a1f1dSLionel Sambuc if (!((buf[0] == 'y') || (buf[0] == 'Y'))) {
2085ebfedea0SLionel Sambuc BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
2086ebfedea0SLionel Sambuc ok = 0;
2087ebfedea0SLionel Sambuc goto err;
2088ebfedea0SLionel Sambuc }
2089ebfedea0SLionel Sambuc }
2090ebfedea0SLionel Sambuc
2091ebfedea0SLionel Sambuc pktmp = X509_get_pubkey(ret);
2092ebfedea0SLionel Sambuc if (EVP_PKEY_missing_parameters(pktmp) &&
2093ebfedea0SLionel Sambuc !EVP_PKEY_missing_parameters(pkey))
2094ebfedea0SLionel Sambuc EVP_PKEY_copy_parameters(pktmp, pkey);
2095ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
2096ebfedea0SLionel Sambuc
2097ebfedea0SLionel Sambuc if (!do_X509_sign(bio_err, ret, pkey, dgst, sigopts))
2098ebfedea0SLionel Sambuc goto err;
2099ebfedea0SLionel Sambuc
2100ebfedea0SLionel Sambuc /* We now just add it to the database */
2101ebfedea0SLionel Sambuc row[DB_type] = (char *)OPENSSL_malloc(2);
2102ebfedea0SLionel Sambuc
2103ebfedea0SLionel Sambuc tm = X509_get_notAfter(ret);
2104ebfedea0SLionel Sambuc row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
2105ebfedea0SLionel Sambuc memcpy(row[DB_exp_date], tm->data, tm->length);
2106ebfedea0SLionel Sambuc row[DB_exp_date][tm->length] = '\0';
2107ebfedea0SLionel Sambuc
2108ebfedea0SLionel Sambuc row[DB_rev_date] = NULL;
2109ebfedea0SLionel Sambuc
2110ebfedea0SLionel Sambuc /* row[DB_serial] done already */
2111ebfedea0SLionel Sambuc row[DB_file] = (char *)OPENSSL_malloc(8);
2112ebfedea0SLionel Sambuc row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
2113ebfedea0SLionel Sambuc
2114ebfedea0SLionel Sambuc if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2115*0a6a1f1dSLionel Sambuc (row[DB_file] == NULL) || (row[DB_name] == NULL)) {
2116ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
2117ebfedea0SLionel Sambuc goto err;
2118ebfedea0SLionel Sambuc }
2119ebfedea0SLionel Sambuc BUF_strlcpy(row[DB_file], "unknown", 8);
2120ebfedea0SLionel Sambuc row[DB_type][0] = 'V';
2121ebfedea0SLionel Sambuc row[DB_type][1] = '\0';
2122ebfedea0SLionel Sambuc
2123*0a6a1f1dSLionel Sambuc if ((irow =
2124*0a6a1f1dSLionel Sambuc (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {
2125ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
2126ebfedea0SLionel Sambuc goto err;
2127ebfedea0SLionel Sambuc }
2128ebfedea0SLionel Sambuc
2129*0a6a1f1dSLionel Sambuc for (i = 0; i < DB_NUMBER; i++) {
2130ebfedea0SLionel Sambuc irow[i] = row[i];
2131ebfedea0SLionel Sambuc row[i] = NULL;
2132ebfedea0SLionel Sambuc }
2133ebfedea0SLionel Sambuc irow[DB_NUMBER] = NULL;
2134ebfedea0SLionel Sambuc
2135*0a6a1f1dSLionel Sambuc if (!TXT_DB_insert(db->db, irow)) {
2136ebfedea0SLionel Sambuc BIO_printf(bio_err, "failed to update database\n");
2137ebfedea0SLionel Sambuc BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2138ebfedea0SLionel Sambuc goto err;
2139ebfedea0SLionel Sambuc }
2140ebfedea0SLionel Sambuc ok = 1;
2141ebfedea0SLionel Sambuc err:
2142ebfedea0SLionel Sambuc for (i = 0; i < DB_NUMBER; i++)
2143*0a6a1f1dSLionel Sambuc if (row[i] != NULL)
2144*0a6a1f1dSLionel Sambuc OPENSSL_free(row[i]);
2145ebfedea0SLionel Sambuc
2146ebfedea0SLionel Sambuc if (CAname != NULL)
2147ebfedea0SLionel Sambuc X509_NAME_free(CAname);
2148ebfedea0SLionel Sambuc if (subject != NULL)
2149ebfedea0SLionel Sambuc X509_NAME_free(subject);
2150ebfedea0SLionel Sambuc if ((dn_subject != NULL) && !email_dn)
2151ebfedea0SLionel Sambuc X509_NAME_free(dn_subject);
2152ebfedea0SLionel Sambuc if (tmptm != NULL)
2153ebfedea0SLionel Sambuc ASN1_UTCTIME_free(tmptm);
2154*0a6a1f1dSLionel Sambuc if (ok <= 0) {
2155*0a6a1f1dSLionel Sambuc if (ret != NULL)
2156*0a6a1f1dSLionel Sambuc X509_free(ret);
2157ebfedea0SLionel Sambuc ret = NULL;
2158*0a6a1f1dSLionel Sambuc } else
2159ebfedea0SLionel Sambuc *xret = ret;
2160ebfedea0SLionel Sambuc return (ok);
2161ebfedea0SLionel Sambuc }
2162ebfedea0SLionel Sambuc
write_new_certificate(BIO * bp,X509 * x,int output_der,int notext)2163*0a6a1f1dSLionel Sambuc static void write_new_certificate(BIO *bp, X509 *x, int output_der,
2164*0a6a1f1dSLionel Sambuc int notext)
2165ebfedea0SLionel Sambuc {
2166ebfedea0SLionel Sambuc
2167*0a6a1f1dSLionel Sambuc if (output_der) {
2168ebfedea0SLionel Sambuc (void)i2d_X509_bio(bp, x);
2169ebfedea0SLionel Sambuc return;
2170ebfedea0SLionel Sambuc }
2171ebfedea0SLionel Sambuc #if 0
2172ebfedea0SLionel Sambuc /* ??? Not needed since X509_print prints all this stuff anyway */
2173ebfedea0SLionel Sambuc f = X509_NAME_oneline(X509_get_issuer_name(x), buf, 256);
2174ebfedea0SLionel Sambuc BIO_printf(bp, "issuer :%s\n", f);
2175ebfedea0SLionel Sambuc
2176ebfedea0SLionel Sambuc f = X509_NAME_oneline(X509_get_subject_name(x), buf, 256);
2177ebfedea0SLionel Sambuc BIO_printf(bp, "subject:%s\n", f);
2178ebfedea0SLionel Sambuc
2179ebfedea0SLionel Sambuc BIO_puts(bp, "serial :");
2180ebfedea0SLionel Sambuc i2a_ASN1_INTEGER(bp, x->cert_info->serialNumber);
2181ebfedea0SLionel Sambuc BIO_puts(bp, "\n\n");
2182ebfedea0SLionel Sambuc #endif
2183*0a6a1f1dSLionel Sambuc if (!notext)
2184*0a6a1f1dSLionel Sambuc X509_print(bp, x);
2185ebfedea0SLionel Sambuc PEM_write_bio_X509(bp, x);
2186ebfedea0SLionel Sambuc }
2187ebfedea0SLionel Sambuc
certify_spkac(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy)2188*0a6a1f1dSLionel Sambuc static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
2189*0a6a1f1dSLionel Sambuc X509 *x509, const EVP_MD *dgst,
2190*0a6a1f1dSLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts,
2191ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *policy, CA_DB *db,
2192*0a6a1f1dSLionel Sambuc BIGNUM *serial, char *subj, unsigned long chtype,
2193*0a6a1f1dSLionel Sambuc int multirdn, int email_dn, char *startdate,
2194*0a6a1f1dSLionel Sambuc char *enddate, long days, char *ext_sect,
2195*0a6a1f1dSLionel Sambuc CONF *lconf, int verbose, unsigned long certopt,
2196ebfedea0SLionel Sambuc unsigned long nameopt, int default_op, int ext_copy)
2197ebfedea0SLionel Sambuc {
2198ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *sk = NULL;
2199ebfedea0SLionel Sambuc LHASH_OF(CONF_VALUE) *parms = NULL;
2200ebfedea0SLionel Sambuc X509_REQ *req = NULL;
2201ebfedea0SLionel Sambuc CONF_VALUE *cv = NULL;
2202ebfedea0SLionel Sambuc NETSCAPE_SPKI *spki = NULL;
2203ebfedea0SLionel Sambuc X509_REQ_INFO *ri;
2204ebfedea0SLionel Sambuc char *type, *buf;
2205ebfedea0SLionel Sambuc EVP_PKEY *pktmp = NULL;
2206ebfedea0SLionel Sambuc X509_NAME *n = NULL;
2207ebfedea0SLionel Sambuc X509_NAME_ENTRY *ne = NULL;
2208ebfedea0SLionel Sambuc int ok = -1, i, j;
2209ebfedea0SLionel Sambuc long errline;
2210ebfedea0SLionel Sambuc int nid;
2211ebfedea0SLionel Sambuc
2212ebfedea0SLionel Sambuc /*
2213ebfedea0SLionel Sambuc * Load input file into a hash table. (This is just an easy
2214ebfedea0SLionel Sambuc * way to read and parse the file, then put it into a convenient
2215ebfedea0SLionel Sambuc * STACK format).
2216ebfedea0SLionel Sambuc */
2217ebfedea0SLionel Sambuc parms = CONF_load(NULL, infile, &errline);
2218*0a6a1f1dSLionel Sambuc if (parms == NULL) {
2219ebfedea0SLionel Sambuc BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
2220ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2221ebfedea0SLionel Sambuc goto err;
2222ebfedea0SLionel Sambuc }
2223ebfedea0SLionel Sambuc
2224ebfedea0SLionel Sambuc sk = CONF_get_section(parms, "default");
2225*0a6a1f1dSLionel Sambuc if (sk_CONF_VALUE_num(sk) == 0) {
2226ebfedea0SLionel Sambuc BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
2227ebfedea0SLionel Sambuc CONF_free(parms);
2228ebfedea0SLionel Sambuc goto err;
2229ebfedea0SLionel Sambuc }
2230ebfedea0SLionel Sambuc
2231ebfedea0SLionel Sambuc /*
2232ebfedea0SLionel Sambuc * Now create a dummy X509 request structure. We don't actually
2233ebfedea0SLionel Sambuc * have an X509 request, but we have many of the components
2234ebfedea0SLionel Sambuc * (a public key, various DN components). The idea is that we
2235ebfedea0SLionel Sambuc * put these components into the right X509 request structure
2236ebfedea0SLionel Sambuc * and we can use the same code as if you had a real X509 request.
2237ebfedea0SLionel Sambuc */
2238ebfedea0SLionel Sambuc req = X509_REQ_new();
2239*0a6a1f1dSLionel Sambuc if (req == NULL) {
2240ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2241ebfedea0SLionel Sambuc goto err;
2242ebfedea0SLionel Sambuc }
2243ebfedea0SLionel Sambuc
2244ebfedea0SLionel Sambuc /*
2245ebfedea0SLionel Sambuc * Build up the subject name set.
2246ebfedea0SLionel Sambuc */
2247ebfedea0SLionel Sambuc ri = req->req_info;
2248ebfedea0SLionel Sambuc n = ri->subject;
2249ebfedea0SLionel Sambuc
2250*0a6a1f1dSLionel Sambuc for (i = 0;; i++) {
2251*0a6a1f1dSLionel Sambuc if (sk_CONF_VALUE_num(sk) <= i)
2252*0a6a1f1dSLionel Sambuc break;
2253ebfedea0SLionel Sambuc
2254ebfedea0SLionel Sambuc cv = sk_CONF_VALUE_value(sk, i);
2255ebfedea0SLionel Sambuc type = cv->name;
2256*0a6a1f1dSLionel Sambuc /*
2257*0a6a1f1dSLionel Sambuc * Skip past any leading X. X: X, etc to allow for multiple instances
2258ebfedea0SLionel Sambuc */
2259ebfedea0SLionel Sambuc for (buf = cv->name; *buf; buf++)
2260*0a6a1f1dSLionel Sambuc if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
2261ebfedea0SLionel Sambuc buf++;
2262*0a6a1f1dSLionel Sambuc if (*buf)
2263*0a6a1f1dSLionel Sambuc type = buf;
2264ebfedea0SLionel Sambuc break;
2265ebfedea0SLionel Sambuc }
2266ebfedea0SLionel Sambuc
2267ebfedea0SLionel Sambuc buf = cv->value;
2268*0a6a1f1dSLionel Sambuc if ((nid = OBJ_txt2nid(type)) == NID_undef) {
2269*0a6a1f1dSLionel Sambuc if (strcmp(type, "SPKAC") == 0) {
2270ebfedea0SLionel Sambuc spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
2271*0a6a1f1dSLionel Sambuc if (spki == NULL) {
2272*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2273*0a6a1f1dSLionel Sambuc "unable to load Netscape SPKAC structure\n");
2274ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
2275ebfedea0SLionel Sambuc goto err;
2276ebfedea0SLionel Sambuc }
2277ebfedea0SLionel Sambuc }
2278ebfedea0SLionel Sambuc continue;
2279ebfedea0SLionel Sambuc }
2280ebfedea0SLionel Sambuc
2281ebfedea0SLionel Sambuc if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
2282ebfedea0SLionel Sambuc (unsigned char *)buf, -1, -1, 0))
2283ebfedea0SLionel Sambuc goto err;
2284ebfedea0SLionel Sambuc }
2285*0a6a1f1dSLionel Sambuc if (spki == NULL) {
2286ebfedea0SLionel Sambuc BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
2287ebfedea0SLionel Sambuc infile);
2288ebfedea0SLionel Sambuc goto err;
2289ebfedea0SLionel Sambuc }
2290ebfedea0SLionel Sambuc
2291ebfedea0SLionel Sambuc /*
2292ebfedea0SLionel Sambuc * Now extract the key from the SPKI structure.
2293ebfedea0SLionel Sambuc */
2294ebfedea0SLionel Sambuc
2295*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2296*0a6a1f1dSLionel Sambuc "Check that the SPKAC request matches the signature\n");
2297ebfedea0SLionel Sambuc
2298*0a6a1f1dSLionel Sambuc if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
2299ebfedea0SLionel Sambuc BIO_printf(bio_err, "error unpacking SPKAC public key\n");
2300ebfedea0SLionel Sambuc goto err;
2301ebfedea0SLionel Sambuc }
2302ebfedea0SLionel Sambuc
2303ebfedea0SLionel Sambuc j = NETSCAPE_SPKI_verify(spki, pktmp);
2304*0a6a1f1dSLionel Sambuc if (j <= 0) {
2305*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2306*0a6a1f1dSLionel Sambuc "signature verification failed on SPKAC public key\n");
2307ebfedea0SLionel Sambuc goto err;
2308ebfedea0SLionel Sambuc }
2309ebfedea0SLionel Sambuc BIO_printf(bio_err, "Signature ok\n");
2310ebfedea0SLionel Sambuc
2311ebfedea0SLionel Sambuc X509_REQ_set_pubkey(req, pktmp);
2312ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
2313*0a6a1f1dSLionel Sambuc ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
2314*0a6a1f1dSLionel Sambuc chtype, multirdn, email_dn, startdate, enddate, days, 1,
2315*0a6a1f1dSLionel Sambuc verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
2316*0a6a1f1dSLionel Sambuc ext_copy, 0);
2317ebfedea0SLionel Sambuc err:
2318*0a6a1f1dSLionel Sambuc if (req != NULL)
2319*0a6a1f1dSLionel Sambuc X509_REQ_free(req);
2320*0a6a1f1dSLionel Sambuc if (parms != NULL)
2321*0a6a1f1dSLionel Sambuc CONF_free(parms);
2322*0a6a1f1dSLionel Sambuc if (spki != NULL)
2323*0a6a1f1dSLionel Sambuc NETSCAPE_SPKI_free(spki);
2324*0a6a1f1dSLionel Sambuc if (ne != NULL)
2325*0a6a1f1dSLionel Sambuc X509_NAME_ENTRY_free(ne);
2326ebfedea0SLionel Sambuc
2327ebfedea0SLionel Sambuc return (ok);
2328ebfedea0SLionel Sambuc }
2329ebfedea0SLionel Sambuc
check_time_format(const char * str)2330ebfedea0SLionel Sambuc static int check_time_format(const char *str)
2331ebfedea0SLionel Sambuc {
2332ebfedea0SLionel Sambuc return ASN1_TIME_set_string(NULL, str);
2333ebfedea0SLionel Sambuc }
2334ebfedea0SLionel Sambuc
do_revoke(X509 * x509,CA_DB * db,int type,char * value)2335ebfedea0SLionel Sambuc static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
2336ebfedea0SLionel Sambuc {
2337ebfedea0SLionel Sambuc ASN1_UTCTIME *tm = NULL;
2338ebfedea0SLionel Sambuc char *row[DB_NUMBER], **rrow, **irow;
2339ebfedea0SLionel Sambuc char *rev_str = NULL;
2340ebfedea0SLionel Sambuc BIGNUM *bn = NULL;
2341ebfedea0SLionel Sambuc int ok = -1, i;
2342ebfedea0SLionel Sambuc
2343ebfedea0SLionel Sambuc for (i = 0; i < DB_NUMBER; i++)
2344ebfedea0SLionel Sambuc row[i] = NULL;
2345ebfedea0SLionel Sambuc row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
2346ebfedea0SLionel Sambuc bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
2347ebfedea0SLionel Sambuc if (!bn)
2348ebfedea0SLionel Sambuc goto err;
2349ebfedea0SLionel Sambuc if (BN_is_zero(bn))
2350ebfedea0SLionel Sambuc row[DB_serial] = BUF_strdup("00");
2351ebfedea0SLionel Sambuc else
2352ebfedea0SLionel Sambuc row[DB_serial] = BN_bn2hex(bn);
2353ebfedea0SLionel Sambuc BN_free(bn);
2354*0a6a1f1dSLionel Sambuc if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
2355ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
2356ebfedea0SLionel Sambuc goto err;
2357ebfedea0SLionel Sambuc }
2358*0a6a1f1dSLionel Sambuc /*
2359*0a6a1f1dSLionel Sambuc * We have to lookup by serial number because name lookup skips revoked
2360*0a6a1f1dSLionel Sambuc * certs
2361ebfedea0SLionel Sambuc */
2362ebfedea0SLionel Sambuc rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2363*0a6a1f1dSLionel Sambuc if (rrow == NULL) {
2364*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2365*0a6a1f1dSLionel Sambuc "Adding Entry with serial number %s to DB for %s\n",
2366*0a6a1f1dSLionel Sambuc row[DB_serial], row[DB_name]);
2367ebfedea0SLionel Sambuc
2368ebfedea0SLionel Sambuc /* We now just add it to the database */
2369ebfedea0SLionel Sambuc row[DB_type] = (char *)OPENSSL_malloc(2);
2370ebfedea0SLionel Sambuc
2371ebfedea0SLionel Sambuc tm = X509_get_notAfter(x509);
2372ebfedea0SLionel Sambuc row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
2373ebfedea0SLionel Sambuc memcpy(row[DB_exp_date], tm->data, tm->length);
2374ebfedea0SLionel Sambuc row[DB_exp_date][tm->length] = '\0';
2375ebfedea0SLionel Sambuc
2376ebfedea0SLionel Sambuc row[DB_rev_date] = NULL;
2377ebfedea0SLionel Sambuc
2378ebfedea0SLionel Sambuc /* row[DB_serial] done already */
2379ebfedea0SLionel Sambuc row[DB_file] = (char *)OPENSSL_malloc(8);
2380ebfedea0SLionel Sambuc
2381ebfedea0SLionel Sambuc /* row[DB_name] done already */
2382ebfedea0SLionel Sambuc
2383ebfedea0SLionel Sambuc if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2384*0a6a1f1dSLionel Sambuc (row[DB_file] == NULL)) {
2385ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
2386ebfedea0SLionel Sambuc goto err;
2387ebfedea0SLionel Sambuc }
2388ebfedea0SLionel Sambuc BUF_strlcpy(row[DB_file], "unknown", 8);
2389ebfedea0SLionel Sambuc row[DB_type][0] = 'V';
2390ebfedea0SLionel Sambuc row[DB_type][1] = '\0';
2391ebfedea0SLionel Sambuc
2392*0a6a1f1dSLionel Sambuc if ((irow =
2393*0a6a1f1dSLionel Sambuc (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) ==
2394*0a6a1f1dSLionel Sambuc NULL) {
2395ebfedea0SLionel Sambuc BIO_printf(bio_err, "Memory allocation failure\n");
2396ebfedea0SLionel Sambuc goto err;
2397ebfedea0SLionel Sambuc }
2398ebfedea0SLionel Sambuc
2399*0a6a1f1dSLionel Sambuc for (i = 0; i < DB_NUMBER; i++) {
2400ebfedea0SLionel Sambuc irow[i] = row[i];
2401ebfedea0SLionel Sambuc row[i] = NULL;
2402ebfedea0SLionel Sambuc }
2403ebfedea0SLionel Sambuc irow[DB_NUMBER] = NULL;
2404ebfedea0SLionel Sambuc
2405*0a6a1f1dSLionel Sambuc if (!TXT_DB_insert(db->db, irow)) {
2406ebfedea0SLionel Sambuc BIO_printf(bio_err, "failed to update database\n");
2407ebfedea0SLionel Sambuc BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2408ebfedea0SLionel Sambuc goto err;
2409ebfedea0SLionel Sambuc }
2410ebfedea0SLionel Sambuc
2411ebfedea0SLionel Sambuc /* Revoke Certificate */
2412ebfedea0SLionel Sambuc ok = do_revoke(x509, db, type, value);
2413ebfedea0SLionel Sambuc
2414ebfedea0SLionel Sambuc goto err;
2415ebfedea0SLionel Sambuc
2416*0a6a1f1dSLionel Sambuc } else if (index_name_cmp_noconst(row, rrow)) {
2417*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
2418ebfedea0SLionel Sambuc goto err;
2419*0a6a1f1dSLionel Sambuc } else if (rrow[DB_type][0] == 'R') {
2420ebfedea0SLionel Sambuc BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
2421ebfedea0SLionel Sambuc row[DB_serial]);
2422ebfedea0SLionel Sambuc goto err;
2423*0a6a1f1dSLionel Sambuc } else {
2424ebfedea0SLionel Sambuc BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
2425ebfedea0SLionel Sambuc rev_str = make_revocation_str(type, value);
2426*0a6a1f1dSLionel Sambuc if (!rev_str) {
2427ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error in revocation arguments\n");
2428ebfedea0SLionel Sambuc goto err;
2429ebfedea0SLionel Sambuc }
2430ebfedea0SLionel Sambuc rrow[DB_type][0] = 'R';
2431ebfedea0SLionel Sambuc rrow[DB_type][1] = '\0';
2432ebfedea0SLionel Sambuc rrow[DB_rev_date] = rev_str;
2433ebfedea0SLionel Sambuc }
2434ebfedea0SLionel Sambuc ok = 1;
2435ebfedea0SLionel Sambuc err:
2436*0a6a1f1dSLionel Sambuc for (i = 0; i < DB_NUMBER; i++) {
2437ebfedea0SLionel Sambuc if (row[i] != NULL)
2438ebfedea0SLionel Sambuc OPENSSL_free(row[i]);
2439ebfedea0SLionel Sambuc }
2440ebfedea0SLionel Sambuc return (ok);
2441ebfedea0SLionel Sambuc }
2442ebfedea0SLionel Sambuc
get_certificate_status(const char * serial,CA_DB * db)2443ebfedea0SLionel Sambuc static int get_certificate_status(const char *serial, CA_DB *db)
2444ebfedea0SLionel Sambuc {
2445ebfedea0SLionel Sambuc char *row[DB_NUMBER], **rrow;
2446ebfedea0SLionel Sambuc int ok = -1, i;
2447ebfedea0SLionel Sambuc
2448ebfedea0SLionel Sambuc /* Free Resources */
2449ebfedea0SLionel Sambuc for (i = 0; i < DB_NUMBER; i++)
2450ebfedea0SLionel Sambuc row[i] = NULL;
2451ebfedea0SLionel Sambuc
2452ebfedea0SLionel Sambuc /* Malloc needed char spaces */
2453ebfedea0SLionel Sambuc row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
2454*0a6a1f1dSLionel Sambuc if (row[DB_serial] == NULL) {
2455ebfedea0SLionel Sambuc BIO_printf(bio_err, "Malloc failure\n");
2456ebfedea0SLionel Sambuc goto err;
2457ebfedea0SLionel Sambuc }
2458ebfedea0SLionel Sambuc
2459*0a6a1f1dSLionel Sambuc if (strlen(serial) % 2) {
2460*0a6a1f1dSLionel Sambuc /*
2461*0a6a1f1dSLionel Sambuc * Set the first char to 0
2462*0a6a1f1dSLionel Sambuc */ ;
2463ebfedea0SLionel Sambuc row[DB_serial][0] = '0';
2464ebfedea0SLionel Sambuc
2465ebfedea0SLionel Sambuc /* Copy String from serial to row[DB_serial] */
2466ebfedea0SLionel Sambuc memcpy(row[DB_serial] + 1, serial, strlen(serial));
2467ebfedea0SLionel Sambuc row[DB_serial][strlen(serial) + 1] = '\0';
2468*0a6a1f1dSLionel Sambuc } else {
2469ebfedea0SLionel Sambuc /* Copy String from serial to row[DB_serial] */
2470ebfedea0SLionel Sambuc memcpy(row[DB_serial], serial, strlen(serial));
2471ebfedea0SLionel Sambuc row[DB_serial][strlen(serial)] = '\0';
2472ebfedea0SLionel Sambuc }
2473ebfedea0SLionel Sambuc
2474ebfedea0SLionel Sambuc /* Make it Upper Case */
2475ebfedea0SLionel Sambuc for (i = 0; row[DB_serial][i] != '\0'; i++)
2476ebfedea0SLionel Sambuc row[DB_serial][i] = toupper((unsigned char)row[DB_serial][i]);
2477ebfedea0SLionel Sambuc
2478ebfedea0SLionel Sambuc ok = 1;
2479ebfedea0SLionel Sambuc
2480ebfedea0SLionel Sambuc /* Search for the certificate */
2481ebfedea0SLionel Sambuc rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2482*0a6a1f1dSLionel Sambuc if (rrow == NULL) {
2483*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
2484ebfedea0SLionel Sambuc ok = -1;
2485ebfedea0SLionel Sambuc goto err;
2486*0a6a1f1dSLionel Sambuc } else if (rrow[DB_type][0] == 'V') {
2487ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s=Valid (%c)\n",
2488ebfedea0SLionel Sambuc row[DB_serial], rrow[DB_type][0]);
2489ebfedea0SLionel Sambuc goto err;
2490*0a6a1f1dSLionel Sambuc } else if (rrow[DB_type][0] == 'R') {
2491ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s=Revoked (%c)\n",
2492ebfedea0SLionel Sambuc row[DB_serial], rrow[DB_type][0]);
2493ebfedea0SLionel Sambuc goto err;
2494*0a6a1f1dSLionel Sambuc } else if (rrow[DB_type][0] == 'E') {
2495ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s=Expired (%c)\n",
2496ebfedea0SLionel Sambuc row[DB_serial], rrow[DB_type][0]);
2497ebfedea0SLionel Sambuc goto err;
2498*0a6a1f1dSLionel Sambuc } else if (rrow[DB_type][0] == 'S') {
2499ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s=Suspended (%c)\n",
2500ebfedea0SLionel Sambuc row[DB_serial], rrow[DB_type][0]);
2501ebfedea0SLionel Sambuc goto err;
2502*0a6a1f1dSLionel Sambuc } else {
2503ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s=Unknown (%c).\n",
2504ebfedea0SLionel Sambuc row[DB_serial], rrow[DB_type][0]);
2505ebfedea0SLionel Sambuc ok = -1;
2506ebfedea0SLionel Sambuc }
2507ebfedea0SLionel Sambuc err:
2508*0a6a1f1dSLionel Sambuc for (i = 0; i < DB_NUMBER; i++) {
2509ebfedea0SLionel Sambuc if (row[i] != NULL)
2510ebfedea0SLionel Sambuc OPENSSL_free(row[i]);
2511ebfedea0SLionel Sambuc }
2512ebfedea0SLionel Sambuc return (ok);
2513ebfedea0SLionel Sambuc }
2514ebfedea0SLionel Sambuc
do_updatedb(CA_DB * db)2515ebfedea0SLionel Sambuc static int do_updatedb(CA_DB *db)
2516ebfedea0SLionel Sambuc {
2517ebfedea0SLionel Sambuc ASN1_UTCTIME *a_tm = NULL;
2518ebfedea0SLionel Sambuc int i, cnt = 0;
2519ebfedea0SLionel Sambuc int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
2520ebfedea0SLionel Sambuc char **rrow, *a_tm_s;
2521ebfedea0SLionel Sambuc
2522ebfedea0SLionel Sambuc a_tm = ASN1_UTCTIME_new();
2523ebfedea0SLionel Sambuc
2524ebfedea0SLionel Sambuc /* get actual time and make a string */
2525ebfedea0SLionel Sambuc a_tm = X509_gmtime_adj(a_tm, 0);
2526ebfedea0SLionel Sambuc a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1);
2527*0a6a1f1dSLionel Sambuc if (a_tm_s == NULL) {
2528ebfedea0SLionel Sambuc cnt = -1;
2529ebfedea0SLionel Sambuc goto err;
2530ebfedea0SLionel Sambuc }
2531ebfedea0SLionel Sambuc
2532ebfedea0SLionel Sambuc memcpy(a_tm_s, a_tm->data, a_tm->length);
2533ebfedea0SLionel Sambuc a_tm_s[a_tm->length] = '\0';
2534ebfedea0SLionel Sambuc
2535ebfedea0SLionel Sambuc if (strncmp(a_tm_s, "49", 2) <= 0)
2536ebfedea0SLionel Sambuc a_y2k = 1;
2537ebfedea0SLionel Sambuc else
2538ebfedea0SLionel Sambuc a_y2k = 0;
2539ebfedea0SLionel Sambuc
2540*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
2541ebfedea0SLionel Sambuc rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
2542ebfedea0SLionel Sambuc
2543*0a6a1f1dSLionel Sambuc if (rrow[DB_type][0] == 'V') {
2544ebfedea0SLionel Sambuc /* ignore entries that are not valid */
2545ebfedea0SLionel Sambuc if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
2546ebfedea0SLionel Sambuc db_y2k = 1;
2547ebfedea0SLionel Sambuc else
2548ebfedea0SLionel Sambuc db_y2k = 0;
2549ebfedea0SLionel Sambuc
2550*0a6a1f1dSLionel Sambuc if (db_y2k == a_y2k) {
2551ebfedea0SLionel Sambuc /* all on the same y2k side */
2552*0a6a1f1dSLionel Sambuc if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
2553ebfedea0SLionel Sambuc rrow[DB_type][0] = 'E';
2554ebfedea0SLionel Sambuc rrow[DB_type][1] = '\0';
2555ebfedea0SLionel Sambuc cnt++;
2556ebfedea0SLionel Sambuc
2557*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2558ebfedea0SLionel Sambuc }
2559*0a6a1f1dSLionel Sambuc } else if (db_y2k < a_y2k) {
2560ebfedea0SLionel Sambuc rrow[DB_type][0] = 'E';
2561ebfedea0SLionel Sambuc rrow[DB_type][1] = '\0';
2562ebfedea0SLionel Sambuc cnt++;
2563ebfedea0SLionel Sambuc
2564*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2565ebfedea0SLionel Sambuc }
2566ebfedea0SLionel Sambuc
2567ebfedea0SLionel Sambuc }
2568ebfedea0SLionel Sambuc }
2569ebfedea0SLionel Sambuc
2570ebfedea0SLionel Sambuc err:
2571ebfedea0SLionel Sambuc
2572ebfedea0SLionel Sambuc ASN1_UTCTIME_free(a_tm);
2573ebfedea0SLionel Sambuc OPENSSL_free(a_tm_s);
2574ebfedea0SLionel Sambuc
2575ebfedea0SLionel Sambuc return (cnt);
2576ebfedea0SLionel Sambuc }
2577ebfedea0SLionel Sambuc
2578ebfedea0SLionel Sambuc static const char *crl_reasons[] = {
2579ebfedea0SLionel Sambuc /* CRL reason strings */
2580ebfedea0SLionel Sambuc "unspecified",
2581ebfedea0SLionel Sambuc "keyCompromise",
2582ebfedea0SLionel Sambuc "CACompromise",
2583ebfedea0SLionel Sambuc "affiliationChanged",
2584ebfedea0SLionel Sambuc "superseded",
2585ebfedea0SLionel Sambuc "cessationOfOperation",
2586ebfedea0SLionel Sambuc "certificateHold",
2587ebfedea0SLionel Sambuc "removeFromCRL",
2588ebfedea0SLionel Sambuc /* Additional pseudo reasons */
2589ebfedea0SLionel Sambuc "holdInstruction",
2590ebfedea0SLionel Sambuc "keyTime",
2591ebfedea0SLionel Sambuc "CAkeyTime"
2592ebfedea0SLionel Sambuc };
2593ebfedea0SLionel Sambuc
2594ebfedea0SLionel Sambuc #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2595ebfedea0SLionel Sambuc
2596*0a6a1f1dSLionel Sambuc /*
2597*0a6a1f1dSLionel Sambuc * Given revocation information convert to a DB string. The format of the
2598*0a6a1f1dSLionel Sambuc * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2599*0a6a1f1dSLionel Sambuc * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2600*0a6a1f1dSLionel Sambuc * additional argument
2601ebfedea0SLionel Sambuc */
2602ebfedea0SLionel Sambuc
make_revocation_str(int rev_type,char * rev_arg)2603ebfedea0SLionel Sambuc char *make_revocation_str(int rev_type, char *rev_arg)
2604ebfedea0SLionel Sambuc {
2605ebfedea0SLionel Sambuc char *other = NULL, *str;
2606ebfedea0SLionel Sambuc const char *reason = NULL;
2607ebfedea0SLionel Sambuc ASN1_OBJECT *otmp;
2608ebfedea0SLionel Sambuc ASN1_UTCTIME *revtm = NULL;
2609ebfedea0SLionel Sambuc int i;
2610*0a6a1f1dSLionel Sambuc switch (rev_type) {
2611ebfedea0SLionel Sambuc case REV_NONE:
2612ebfedea0SLionel Sambuc break;
2613ebfedea0SLionel Sambuc
2614ebfedea0SLionel Sambuc case REV_CRL_REASON:
2615*0a6a1f1dSLionel Sambuc for (i = 0; i < 8; i++) {
2616*0a6a1f1dSLionel Sambuc if (!strcasecmp(rev_arg, crl_reasons[i])) {
2617ebfedea0SLionel Sambuc reason = crl_reasons[i];
2618ebfedea0SLionel Sambuc break;
2619ebfedea0SLionel Sambuc }
2620ebfedea0SLionel Sambuc }
2621*0a6a1f1dSLionel Sambuc if (reason == NULL) {
2622ebfedea0SLionel Sambuc BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
2623ebfedea0SLionel Sambuc return NULL;
2624ebfedea0SLionel Sambuc }
2625ebfedea0SLionel Sambuc break;
2626ebfedea0SLionel Sambuc
2627ebfedea0SLionel Sambuc case REV_HOLD:
2628ebfedea0SLionel Sambuc /* Argument is an OID */
2629ebfedea0SLionel Sambuc
2630ebfedea0SLionel Sambuc otmp = OBJ_txt2obj(rev_arg, 0);
2631ebfedea0SLionel Sambuc ASN1_OBJECT_free(otmp);
2632ebfedea0SLionel Sambuc
2633*0a6a1f1dSLionel Sambuc if (otmp == NULL) {
2634ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
2635ebfedea0SLionel Sambuc return NULL;
2636ebfedea0SLionel Sambuc }
2637ebfedea0SLionel Sambuc
2638ebfedea0SLionel Sambuc reason = "holdInstruction";
2639ebfedea0SLionel Sambuc other = rev_arg;
2640ebfedea0SLionel Sambuc break;
2641ebfedea0SLionel Sambuc
2642ebfedea0SLionel Sambuc case REV_KEY_COMPROMISE:
2643ebfedea0SLionel Sambuc case REV_CA_COMPROMISE:
2644ebfedea0SLionel Sambuc
2645ebfedea0SLionel Sambuc /* Argument is the key compromise time */
2646*0a6a1f1dSLionel Sambuc if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
2647*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
2648*0a6a1f1dSLionel Sambuc "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2649*0a6a1f1dSLionel Sambuc rev_arg);
2650ebfedea0SLionel Sambuc return NULL;
2651ebfedea0SLionel Sambuc }
2652ebfedea0SLionel Sambuc other = rev_arg;
2653ebfedea0SLionel Sambuc if (rev_type == REV_KEY_COMPROMISE)
2654ebfedea0SLionel Sambuc reason = "keyTime";
2655ebfedea0SLionel Sambuc else
2656ebfedea0SLionel Sambuc reason = "CAkeyTime";
2657ebfedea0SLionel Sambuc
2658ebfedea0SLionel Sambuc break;
2659ebfedea0SLionel Sambuc
2660ebfedea0SLionel Sambuc }
2661ebfedea0SLionel Sambuc
2662ebfedea0SLionel Sambuc revtm = X509_gmtime_adj(NULL, 0);
2663ebfedea0SLionel Sambuc
2664*0a6a1f1dSLionel Sambuc if (!revtm)
2665*0a6a1f1dSLionel Sambuc return NULL;
2666*0a6a1f1dSLionel Sambuc
2667ebfedea0SLionel Sambuc i = revtm->length + 1;
2668ebfedea0SLionel Sambuc
2669*0a6a1f1dSLionel Sambuc if (reason)
2670*0a6a1f1dSLionel Sambuc i += strlen(reason) + 1;
2671*0a6a1f1dSLionel Sambuc if (other)
2672*0a6a1f1dSLionel Sambuc i += strlen(other) + 1;
2673ebfedea0SLionel Sambuc
2674ebfedea0SLionel Sambuc str = OPENSSL_malloc(i);
2675ebfedea0SLionel Sambuc
2676*0a6a1f1dSLionel Sambuc if (!str)
2677*0a6a1f1dSLionel Sambuc return NULL;
2678ebfedea0SLionel Sambuc
2679ebfedea0SLionel Sambuc BUF_strlcpy(str, (char *)revtm->data, i);
2680*0a6a1f1dSLionel Sambuc if (reason) {
2681ebfedea0SLionel Sambuc BUF_strlcat(str, ",", i);
2682ebfedea0SLionel Sambuc BUF_strlcat(str, reason, i);
2683ebfedea0SLionel Sambuc }
2684*0a6a1f1dSLionel Sambuc if (other) {
2685ebfedea0SLionel Sambuc BUF_strlcat(str, ",", i);
2686ebfedea0SLionel Sambuc BUF_strlcat(str, other, i);
2687ebfedea0SLionel Sambuc }
2688ebfedea0SLionel Sambuc ASN1_UTCTIME_free(revtm);
2689ebfedea0SLionel Sambuc return str;
2690ebfedea0SLionel Sambuc }
2691ebfedea0SLionel Sambuc
2692*0a6a1f1dSLionel Sambuc /*-
2693*0a6a1f1dSLionel Sambuc * Convert revocation field to X509_REVOKED entry
2694ebfedea0SLionel Sambuc * return code:
2695ebfedea0SLionel Sambuc * 0 error
2696ebfedea0SLionel Sambuc * 1 OK
2697ebfedea0SLionel Sambuc * 2 OK and some extensions added (i.e. V2 CRL)
2698ebfedea0SLionel Sambuc */
2699ebfedea0SLionel Sambuc
make_revoked(X509_REVOKED * rev,const char * str)2700ebfedea0SLionel Sambuc int make_revoked(X509_REVOKED *rev, const char *str)
2701ebfedea0SLionel Sambuc {
2702ebfedea0SLionel Sambuc char *tmp = NULL;
2703ebfedea0SLionel Sambuc int reason_code = -1;
2704ebfedea0SLionel Sambuc int i, ret = 0;
2705ebfedea0SLionel Sambuc ASN1_OBJECT *hold = NULL;
2706ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME *comp_time = NULL;
2707ebfedea0SLionel Sambuc ASN1_ENUMERATED *rtmp = NULL;
2708ebfedea0SLionel Sambuc
2709ebfedea0SLionel Sambuc ASN1_TIME *revDate = NULL;
2710ebfedea0SLionel Sambuc
2711ebfedea0SLionel Sambuc i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
2712ebfedea0SLionel Sambuc
2713ebfedea0SLionel Sambuc if (i == 0)
2714ebfedea0SLionel Sambuc goto err;
2715ebfedea0SLionel Sambuc
2716ebfedea0SLionel Sambuc if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
2717ebfedea0SLionel Sambuc goto err;
2718ebfedea0SLionel Sambuc
2719*0a6a1f1dSLionel Sambuc if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
2720ebfedea0SLionel Sambuc rtmp = ASN1_ENUMERATED_new();
2721ebfedea0SLionel Sambuc if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
2722ebfedea0SLionel Sambuc goto err;
2723ebfedea0SLionel Sambuc if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
2724ebfedea0SLionel Sambuc goto err;
2725ebfedea0SLionel Sambuc }
2726ebfedea0SLionel Sambuc
2727*0a6a1f1dSLionel Sambuc if (rev && comp_time) {
2728*0a6a1f1dSLionel Sambuc if (!X509_REVOKED_add1_ext_i2d
2729*0a6a1f1dSLionel Sambuc (rev, NID_invalidity_date, comp_time, 0, 0))
2730ebfedea0SLionel Sambuc goto err;
2731ebfedea0SLionel Sambuc }
2732*0a6a1f1dSLionel Sambuc if (rev && hold) {
2733*0a6a1f1dSLionel Sambuc if (!X509_REVOKED_add1_ext_i2d
2734*0a6a1f1dSLionel Sambuc (rev, NID_hold_instruction_code, hold, 0, 0))
2735ebfedea0SLionel Sambuc goto err;
2736ebfedea0SLionel Sambuc }
2737ebfedea0SLionel Sambuc
2738ebfedea0SLionel Sambuc if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
2739ebfedea0SLionel Sambuc ret = 2;
2740*0a6a1f1dSLionel Sambuc else
2741*0a6a1f1dSLionel Sambuc ret = 1;
2742ebfedea0SLionel Sambuc
2743ebfedea0SLionel Sambuc err:
2744ebfedea0SLionel Sambuc
2745*0a6a1f1dSLionel Sambuc if (tmp)
2746*0a6a1f1dSLionel Sambuc OPENSSL_free(tmp);
2747ebfedea0SLionel Sambuc ASN1_OBJECT_free(hold);
2748ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME_free(comp_time);
2749ebfedea0SLionel Sambuc ASN1_ENUMERATED_free(rtmp);
2750ebfedea0SLionel Sambuc ASN1_TIME_free(revDate);
2751ebfedea0SLionel Sambuc
2752ebfedea0SLionel Sambuc return ret;
2753ebfedea0SLionel Sambuc }
2754ebfedea0SLionel Sambuc
old_entry_print(BIO * bp,ASN1_OBJECT * obj,ASN1_STRING * str)2755ebfedea0SLionel Sambuc int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
2756ebfedea0SLionel Sambuc {
2757ebfedea0SLionel Sambuc char buf[25], *pbuf, *p;
2758ebfedea0SLionel Sambuc int j;
2759ebfedea0SLionel Sambuc j = i2a_ASN1_OBJECT(bp, obj);
2760ebfedea0SLionel Sambuc pbuf = buf;
2761ebfedea0SLionel Sambuc for (j = 22 - j; j > 0; j--)
2762ebfedea0SLionel Sambuc *(pbuf++) = ' ';
2763ebfedea0SLionel Sambuc *(pbuf++) = ':';
2764ebfedea0SLionel Sambuc *(pbuf++) = '\0';
2765ebfedea0SLionel Sambuc BIO_puts(bp, buf);
2766ebfedea0SLionel Sambuc
2767ebfedea0SLionel Sambuc if (str->type == V_ASN1_PRINTABLESTRING)
2768ebfedea0SLionel Sambuc BIO_printf(bp, "PRINTABLE:'");
2769ebfedea0SLionel Sambuc else if (str->type == V_ASN1_T61STRING)
2770ebfedea0SLionel Sambuc BIO_printf(bp, "T61STRING:'");
2771ebfedea0SLionel Sambuc else if (str->type == V_ASN1_IA5STRING)
2772ebfedea0SLionel Sambuc BIO_printf(bp, "IA5STRING:'");
2773ebfedea0SLionel Sambuc else if (str->type == V_ASN1_UNIVERSALSTRING)
2774ebfedea0SLionel Sambuc BIO_printf(bp, "UNIVERSALSTRING:'");
2775ebfedea0SLionel Sambuc else
2776ebfedea0SLionel Sambuc BIO_printf(bp, "ASN.1 %2d:'", str->type);
2777ebfedea0SLionel Sambuc
2778ebfedea0SLionel Sambuc p = (char *)str->data;
2779*0a6a1f1dSLionel Sambuc for (j = str->length; j > 0; j--) {
2780ebfedea0SLionel Sambuc if ((*p >= ' ') && (*p <= '~'))
2781ebfedea0SLionel Sambuc BIO_printf(bp, "%c", *p);
2782ebfedea0SLionel Sambuc else if (*p & 0x80)
2783ebfedea0SLionel Sambuc BIO_printf(bp, "\\0x%02X", *p);
2784ebfedea0SLionel Sambuc else if ((unsigned char)*p == 0xf7)
2785ebfedea0SLionel Sambuc BIO_printf(bp, "^?");
2786*0a6a1f1dSLionel Sambuc else
2787*0a6a1f1dSLionel Sambuc BIO_printf(bp, "^%c", *p + '@');
2788ebfedea0SLionel Sambuc p++;
2789ebfedea0SLionel Sambuc }
2790ebfedea0SLionel Sambuc BIO_printf(bp, "'\n");
2791ebfedea0SLionel Sambuc return 1;
2792ebfedea0SLionel Sambuc }
2793ebfedea0SLionel Sambuc
unpack_revinfo(ASN1_TIME ** prevtm,int * preason,ASN1_OBJECT ** phold,ASN1_GENERALIZEDTIME ** pinvtm,const char * str)2794*0a6a1f1dSLionel Sambuc int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
2795*0a6a1f1dSLionel Sambuc ASN1_GENERALIZEDTIME **pinvtm, const char *str)
2796ebfedea0SLionel Sambuc {
2797ebfedea0SLionel Sambuc char *tmp = NULL;
2798ebfedea0SLionel Sambuc char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
2799ebfedea0SLionel Sambuc int reason_code = -1;
2800ebfedea0SLionel Sambuc int ret = 0;
2801ebfedea0SLionel Sambuc unsigned int i;
2802ebfedea0SLionel Sambuc ASN1_OBJECT *hold = NULL;
2803ebfedea0SLionel Sambuc ASN1_GENERALIZEDTIME *comp_time = NULL;
2804ebfedea0SLionel Sambuc tmp = BUF_strdup(str);
2805ebfedea0SLionel Sambuc
2806*0a6a1f1dSLionel Sambuc if (!tmp) {
2807*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "memory allocation failure\n");
2808*0a6a1f1dSLionel Sambuc goto err;
2809*0a6a1f1dSLionel Sambuc }
2810*0a6a1f1dSLionel Sambuc
2811ebfedea0SLionel Sambuc p = strchr(tmp, ',');
2812ebfedea0SLionel Sambuc
2813ebfedea0SLionel Sambuc rtime_str = tmp;
2814ebfedea0SLionel Sambuc
2815*0a6a1f1dSLionel Sambuc if (p) {
2816ebfedea0SLionel Sambuc *p = '\0';
2817ebfedea0SLionel Sambuc p++;
2818ebfedea0SLionel Sambuc reason_str = p;
2819ebfedea0SLionel Sambuc p = strchr(p, ',');
2820*0a6a1f1dSLionel Sambuc if (p) {
2821ebfedea0SLionel Sambuc *p = '\0';
2822ebfedea0SLionel Sambuc arg_str = p + 1;
2823ebfedea0SLionel Sambuc }
2824ebfedea0SLionel Sambuc }
2825ebfedea0SLionel Sambuc
2826*0a6a1f1dSLionel Sambuc if (prevtm) {
2827ebfedea0SLionel Sambuc *prevtm = ASN1_UTCTIME_new();
2828*0a6a1f1dSLionel Sambuc if (!*prevtm) {
2829*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "memory allocation failure\n");
2830*0a6a1f1dSLionel Sambuc goto err;
2831*0a6a1f1dSLionel Sambuc }
2832*0a6a1f1dSLionel Sambuc if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
2833ebfedea0SLionel Sambuc BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
2834ebfedea0SLionel Sambuc goto err;
2835ebfedea0SLionel Sambuc }
2836ebfedea0SLionel Sambuc }
2837*0a6a1f1dSLionel Sambuc if (reason_str) {
2838*0a6a1f1dSLionel Sambuc for (i = 0; i < NUM_REASONS; i++) {
2839*0a6a1f1dSLionel Sambuc if (!strcasecmp(reason_str, crl_reasons[i])) {
2840ebfedea0SLionel Sambuc reason_code = i;
2841ebfedea0SLionel Sambuc break;
2842ebfedea0SLionel Sambuc }
2843ebfedea0SLionel Sambuc }
2844*0a6a1f1dSLionel Sambuc if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
2845ebfedea0SLionel Sambuc BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
2846ebfedea0SLionel Sambuc goto err;
2847ebfedea0SLionel Sambuc }
2848ebfedea0SLionel Sambuc
2849ebfedea0SLionel Sambuc if (reason_code == 7)
2850ebfedea0SLionel Sambuc reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
2851*0a6a1f1dSLionel Sambuc else if (reason_code == 8) { /* Hold instruction */
2852*0a6a1f1dSLionel Sambuc if (!arg_str) {
2853ebfedea0SLionel Sambuc BIO_printf(bio_err, "missing hold instruction\n");
2854ebfedea0SLionel Sambuc goto err;
2855ebfedea0SLionel Sambuc }
2856ebfedea0SLionel Sambuc reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
2857ebfedea0SLionel Sambuc hold = OBJ_txt2obj(arg_str, 0);
2858ebfedea0SLionel Sambuc
2859*0a6a1f1dSLionel Sambuc if (!hold) {
2860*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "invalid object identifier %s\n",
2861*0a6a1f1dSLionel Sambuc arg_str);
2862ebfedea0SLionel Sambuc goto err;
2863ebfedea0SLionel Sambuc }
2864*0a6a1f1dSLionel Sambuc if (phold)
2865*0a6a1f1dSLionel Sambuc *phold = hold;
2866*0a6a1f1dSLionel Sambuc } else if ((reason_code == 9) || (reason_code == 10)) {
2867*0a6a1f1dSLionel Sambuc if (!arg_str) {
2868ebfedea0SLionel Sambuc BIO_printf(bio_err, "missing compromised time\n");
2869ebfedea0SLionel Sambuc goto err;
2870ebfedea0SLionel Sambuc }
2871ebfedea0SLionel Sambuc comp_time = ASN1_GENERALIZEDTIME_new();
2872*0a6a1f1dSLionel Sambuc if (!comp_time) {
2873*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "memory allocation failure\n");
2874*0a6a1f1dSLionel Sambuc goto err;
2875*0a6a1f1dSLionel Sambuc }
2876*0a6a1f1dSLionel Sambuc if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
2877ebfedea0SLionel Sambuc BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
2878ebfedea0SLionel Sambuc goto err;
2879ebfedea0SLionel Sambuc }
2880ebfedea0SLionel Sambuc if (reason_code == 9)
2881ebfedea0SLionel Sambuc reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
2882ebfedea0SLionel Sambuc else
2883ebfedea0SLionel Sambuc reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
2884ebfedea0SLionel Sambuc }
2885ebfedea0SLionel Sambuc }
2886ebfedea0SLionel Sambuc
2887*0a6a1f1dSLionel Sambuc if (preason)
2888*0a6a1f1dSLionel Sambuc *preason = reason_code;
2889*0a6a1f1dSLionel Sambuc if (pinvtm)
2890*0a6a1f1dSLionel Sambuc *pinvtm = comp_time;
2891*0a6a1f1dSLionel Sambuc else
2892*0a6a1f1dSLionel Sambuc ASN1_GENERALIZEDTIME_free(comp_time);
2893ebfedea0SLionel Sambuc
2894ebfedea0SLionel Sambuc ret = 1;
2895ebfedea0SLionel Sambuc
2896ebfedea0SLionel Sambuc err:
2897ebfedea0SLionel Sambuc
2898*0a6a1f1dSLionel Sambuc if (tmp)
2899*0a6a1f1dSLionel Sambuc OPENSSL_free(tmp);
2900*0a6a1f1dSLionel Sambuc if (!phold)
2901*0a6a1f1dSLionel Sambuc ASN1_OBJECT_free(hold);
2902*0a6a1f1dSLionel Sambuc if (!pinvtm)
2903*0a6a1f1dSLionel Sambuc ASN1_GENERALIZEDTIME_free(comp_time);
2904ebfedea0SLionel Sambuc
2905ebfedea0SLionel Sambuc return ret;
2906ebfedea0SLionel Sambuc }
2907