]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
065121ff | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 | 8 | */ |
d02b48c6 RE |
9 | #include <stdio.h> |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
c67cdb50 | 12 | #include <ctype.h> |
d02b48c6 | 13 | #include <sys/types.h> |
ec577822 BM |
14 | #include <openssl/conf.h> |
15 | #include <openssl/bio.h> | |
16 | #include <openssl/err.h> | |
17 | #include <openssl/bn.h> | |
18 | #include <openssl/txt_db.h> | |
19 | #include <openssl/evp.h> | |
20 | #include <openssl/x509.h> | |
21 | #include <openssl/x509v3.h> | |
22 | #include <openssl/objects.h> | |
a6b7ffdd | 23 | #include <openssl/ocsp.h> |
ec577822 | 24 | #include <openssl/pem.h> |
d02b48c6 RE |
25 | |
26 | #ifndef W_OK | |
0f113f3e | 27 | # ifdef OPENSSL_SYS_VMS |
e627a13c | 28 | # include <unistd.h> |
cdb5129e | 29 | # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_TANDEM) |
0f113f3e MC |
30 | # include <sys/file.h> |
31 | # endif | |
7d7d2cbc UM |
32 | #endif |
33 | ||
d610d27f | 34 | #include "apps.h" |
dab2cd68 | 35 | #include "progs.h" |
d610d27f | 36 | |
7d7d2cbc | 37 | #ifndef W_OK |
0f113f3e | 38 | # define F_OK 0 |
0f113f3e MC |
39 | # define W_OK 2 |
40 | # define R_OK 4 | |
d02b48c6 RE |
41 | #endif |
42 | ||
dfc3ffe5 F |
43 | #ifndef PATH_MAX |
44 | # define PATH_MAX 4096 | |
45 | #endif | |
d02b48c6 | 46 | |
070c2332 | 47 | #define BASE_SECTION "ca" |
d02b48c6 | 48 | |
0f113f3e MC |
49 | #define ENV_DEFAULT_CA "default_ca" |
50 | ||
070c2332 | 51 | #define STRING_MASK "string_mask" |
0f113f3e MC |
52 | #define UTF8_IN "utf8" |
53 | ||
0f113f3e | 54 | #define ENV_NEW_CERTS_DIR "new_certs_dir" |
070c2332 | 55 | #define ENV_CERTIFICATE "certificate" |
0f113f3e | 56 | #define ENV_SERIAL "serial" |
ffb46830 | 57 | #define ENV_RAND_SERIAL "rand_serial" |
0f113f3e | 58 | #define ENV_CRLNUMBER "crlnumber" |
0f113f3e | 59 | #define ENV_PRIVATE_KEY "private_key" |
0f113f3e MC |
60 | #define ENV_DEFAULT_DAYS "default_days" |
61 | #define ENV_DEFAULT_STARTDATE "default_startdate" | |
62 | #define ENV_DEFAULT_ENDDATE "default_enddate" | |
63 | #define ENV_DEFAULT_CRL_DAYS "default_crl_days" | |
64 | #define ENV_DEFAULT_CRL_HOURS "default_crl_hours" | |
65 | #define ENV_DEFAULT_MD "default_md" | |
66 | #define ENV_DEFAULT_EMAIL_DN "email_in_dn" | |
67 | #define ENV_PRESERVE "preserve" | |
68 | #define ENV_POLICY "policy" | |
69 | #define ENV_EXTENSIONS "x509_extensions" | |
70 | #define ENV_CRLEXT "crl_extensions" | |
71 | #define ENV_MSIE_HACK "msie_hack" | |
72 | #define ENV_NAMEOPT "name_opt" | |
73 | #define ENV_CERTOPT "cert_opt" | |
74 | #define ENV_EXTCOPY "copy_extensions" | |
75 | #define ENV_UNIQUE_SUBJECT "unique_subject" | |
76 | ||
77 | #define ENV_DATABASE "database" | |
d02b48c6 | 78 | |
a6b7ffdd | 79 | /* Additional revocation information types */ |
06a79af2 F |
80 | typedef enum { |
81 | REV_VALID = -1, /* Valid (not-revoked) status */ | |
82 | REV_NONE = 0, /* No additional information */ | |
83 | REV_CRL_REASON = 1, /* Value is CRL reason code */ | |
84 | REV_HOLD = 2, /* Value is hold instruction */ | |
85 | REV_KEY_COMPROMISE = 3, /* Value is cert key compromise time */ | |
86 | REV_CA_COMPROMISE = 4 /* Value is CA key compromise time */ | |
87 | } REVINFO_TYPE; | |
0f113f3e | 88 | |
edbff8da | 89 | static char *lookup_conf(const CONF *conf, const char *group, const char *tag); |
cc696296 | 90 | |
9d5aca65 DO |
91 | static int certify(X509 **xret, const char *infile, int informat, |
92 | EVP_PKEY *pkey, X509 *x509, | |
91034b68 | 93 | const char *dgst, |
2292c8e1 RL |
94 | STACK_OF(OPENSSL_STRING) *sigopts, |
95 | STACK_OF(OPENSSL_STRING) *vfyopts, | |
0f113f3e | 96 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, |
cc696296 F |
97 | BIGNUM *serial, const char *subj, unsigned long chtype, |
98 | int multirdn, int email_dn, const char *startdate, | |
99 | const char *enddate, | |
100 | long days, int batch, const char *ext_sect, CONF *conf, | |
0f113f3e | 101 | int verbose, unsigned long certopt, unsigned long nameopt, |
8c5bff22 | 102 | int default_op, int ext_copy, int selfsign, unsigned long dateopt); |
c1097eec | 103 | static int certify_cert(X509 **xret, const char *infile, int certformat, |
2a33470b | 104 | const char *passin, EVP_PKEY *pkey, X509 *x509, |
91034b68 | 105 | const char *dgst, |
2292c8e1 RL |
106 | STACK_OF(OPENSSL_STRING) *sigopts, |
107 | STACK_OF(OPENSSL_STRING) *vfyopts, | |
0f113f3e | 108 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, |
cc696296 F |
109 | BIGNUM *serial, const char *subj, unsigned long chtype, |
110 | int multirdn, int email_dn, const char *startdate, | |
111 | const char *enddate, long days, int batch, const char *ext_sect, | |
0f113f3e | 112 | CONF *conf, int verbose, unsigned long certopt, |
8c5bff22 | 113 | unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt); |
cc696296 | 114 | static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey, |
91034b68 | 115 | X509 *x509, const char *dgst, |
0f113f3e MC |
116 | STACK_OF(OPENSSL_STRING) *sigopts, |
117 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
118 | BIGNUM *serial, const char *subj, unsigned long chtype, |
119 | int multirdn, int email_dn, const char *startdate, | |
120 | const char *enddate, long days, const char *ext_sect, CONF *conf, | |
0f113f3e | 121 | int verbose, unsigned long certopt, |
8c5bff22 | 122 | unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt); |
0f113f3e | 123 | static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, |
91034b68 | 124 | const char *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
0f113f3e | 125 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, |
cc696296 F |
126 | const char *subj, unsigned long chtype, int multirdn, |
127 | int email_dn, const char *startdate, const char *enddate, long days, | |
128 | int batch, int verbose, X509_REQ *req, const char *ext_sect, | |
0f113f3e | 129 | CONF *conf, unsigned long certopt, unsigned long nameopt, |
8c5bff22 | 130 | int default_op, int ext_copy, int selfsign, unsigned long dateopt); |
f85b68cd | 131 | static int get_certificate_status(const char *ser_status, CA_DB *db); |
7c337e00 | 132 | static int check_time_format(const char *str); |
06a79af2 F |
133 | static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, |
134 | const char *extval); | |
135 | static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg); | |
136 | static int make_revoked(X509_REVOKED *rev, const char *str); | |
69b017f6 | 137 | static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str); |
06a79af2 | 138 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext); |
7e1b7485 | 139 | |
1a683b80 | 140 | static CONF *extfile_conf = NULL; |
0f113f3e MC |
141 | static int preserve = 0; |
142 | static int msie_hack = 0; | |
35a99b63 | 143 | |
7e1b7485 | 144 | typedef enum OPTION_choice { |
b0f96018 | 145 | OPT_COMMON, |
7e1b7485 RS |
146 | OPT_ENGINE, OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SUBJ, OPT_UTF8, |
147 | OPT_CREATE_SERIAL, OPT_MULTIVALUE_RDN, OPT_STARTDATE, OPT_ENDDATE, | |
148 | OPT_DAYS, OPT_MD, OPT_POLICY, OPT_KEYFILE, OPT_KEYFORM, OPT_PASSIN, | |
9d5aca65 | 149 | OPT_KEY, OPT_CERT, OPT_CERTFORM, OPT_SELFSIGN, |
8c5bff22 | 150 | OPT_IN, OPT_INFORM, OPT_OUT, OPT_DATEOPT, OPT_OUTDIR, OPT_VFYOPT, |
7e1b7485 | 151 | OPT_SIGOPT, OPT_NOTEXT, OPT_BATCH, OPT_PRESERVEDN, OPT_NOEMAILDN, |
64713cb1 CN |
152 | OPT_GENCRL, OPT_MSIE_HACK, OPT_CRL_LASTUPDATE, OPT_CRL_NEXTUPDATE, |
153 | OPT_CRLDAYS, OPT_CRLHOURS, OPT_CRLSEC, | |
7e1b7485 RS |
154 | OPT_INFILES, OPT_SS_CERT, OPT_SPKAC, OPT_REVOKE, OPT_VALID, |
155 | OPT_EXTENSIONS, OPT_EXTFILE, OPT_STATUS, OPT_UPDATEDB, OPT_CRLEXTS, | |
a414fd67 | 156 | OPT_RAND_SERIAL, OPT_QUIET, |
2292c8e1 | 157 | OPT_R_ENUM, OPT_PROV_ENUM, |
06a79af2 F |
158 | /* Do not change the order here; see related case statements below */ |
159 | OPT_CRL_REASON, OPT_CRL_HOLD, OPT_CRL_COMPROMISE, OPT_CRL_CA_COMPROMISE | |
7e1b7485 RS |
160 | } OPTION_CHOICE; |
161 | ||
44c83ebd | 162 | const OPTIONS ca_options[] = { |
92de469f RS |
163 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [certreq...]\n"}, |
164 | ||
5388f986 | 165 | OPT_SECTION("General"), |
7e1b7485 RS |
166 | {"help", OPT_HELP, '-', "Display this summary"}, |
167 | {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"}, | |
a414fd67 | 168 | {"quiet", OPT_QUIET, '-', "Terse output during processing"}, |
5388f986 | 169 | {"outdir", OPT_OUTDIR, '/', "Where to put output cert"}, |
9d5aca65 | 170 | {"in", OPT_IN, '<', "The input cert request(s)"}, |
200d8447 DDO |
171 | {"inform", OPT_INFORM, 'F', |
172 | "CSR input format to use (PEM or DER; by default try PEM first)"}, | |
5388f986 RS |
173 | {"infiles", OPT_INFILES, '-', "The last argument, requests to process"}, |
174 | {"out", OPT_OUT, '>', "Where to put the output file(s)"}, | |
8c5bff22 | 175 | {"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."}, |
5388f986 RS |
176 | {"notext", OPT_NOTEXT, '-', "Do not print the generated certificate"}, |
177 | {"batch", OPT_BATCH, '-', "Don't ask questions"}, | |
178 | {"msie_hack", OPT_MSIE_HACK, '-', | |
179 | "msie modifications to handle all Universal Strings"}, | |
180 | {"ss_cert", OPT_SS_CERT, '<', "File contains a self signed cert to sign"}, | |
181 | {"spkac", OPT_SPKAC, '<', | |
182 | "File contains DN and signed public key and challenge"}, | |
183 | #ifndef OPENSSL_NO_ENGINE | |
184 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
185 | #endif | |
186 | ||
187 | OPT_SECTION("Configuration"), | |
7e1b7485 RS |
188 | {"config", OPT_CONFIG, 's', "A config file"}, |
189 | {"name", OPT_NAME, 's', "The particular CA definition to use"}, | |
d462b5ff | 190 | {"section", OPT_NAME, 's', "An alias for -name"}, |
5388f986 RS |
191 | {"policy", OPT_POLICY, 's', "The CA 'policy' to support"}, |
192 | ||
193 | OPT_SECTION("Certificate"), | |
7e1b7485 | 194 | {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"}, |
9d5aca65 | 195 | {"utf8", OPT_UTF8, '-', "Input characters are UTF8; default ASCII"}, |
6755ff11 | 196 | {"create_serial", OPT_CREATE_SERIAL, '-', |
d6073e27 | 197 | "If reading serial fails, create a new random serial"}, |
ffb46830 RS |
198 | {"rand_serial", OPT_RAND_SERIAL, '-', |
199 | "Always create a random serial; do not store it"}, | |
7e1b7485 | 200 | {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-', |
5a0991d0 | 201 | "Deprecated; multi-valued RDNs support is always on."}, |
7e1b7485 RS |
202 | {"startdate", OPT_STARTDATE, 's', "Cert notBefore, YYMMDDHHMMSSZ"}, |
203 | {"enddate", OPT_ENDDATE, 's', | |
204 | "YYMMDDHHMMSSZ cert notAfter (overrides -days)"}, | |
205 | {"days", OPT_DAYS, 'p', "Number of days to certify the cert for"}, | |
5388f986 RS |
206 | {"extensions", OPT_EXTENSIONS, 's', |
207 | "Extension section (override value in config file)"}, | |
208 | {"extfile", OPT_EXTFILE, '<', | |
209 | "Configuration file with X509v3 extensions to add"}, | |
5388f986 RS |
210 | {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"}, |
211 | {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"}, | |
212 | ||
213 | OPT_SECTION("Signing"), | |
51e5df0e | 214 | {"md", OPT_MD, 's', "Digest to use, such as sha256"}, |
b3c5aadf | 215 | {"keyfile", OPT_KEYFILE, 's', "The CA private key"}, |
c1097eec DO |
216 | {"keyform", OPT_KEYFORM, 'f', |
217 | "Private key file format (ENGINE, other values ignored)"}, | |
2a33470b | 218 | {"passin", OPT_PASSIN, 's', "Key and cert input file pass phrase source"}, |
c1097eec DO |
219 | {"key", OPT_KEY, 's', |
220 | "Key to decrypt the private key or cert files if encrypted. Better use -passin"}, | |
7e1b7485 | 221 | {"cert", OPT_CERT, '<', "The CA cert"}, |
9d5aca65 | 222 | {"certform", OPT_CERTFORM, 'F', |
2a33470b | 223 | "Certificate input format (DER/PEM/P12); has no effect"}, |
7e1b7485 RS |
224 | {"selfsign", OPT_SELFSIGN, '-', |
225 | "Sign a cert with the key associated with it"}, | |
16e1b281 | 226 | {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"}, |
2a33470b | 227 | {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"}, |
5388f986 RS |
228 | |
229 | OPT_SECTION("Revocation"), | |
7e1b7485 | 230 | {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"}, |
b4b42ce6 F |
231 | {"valid", OPT_VALID, 's', |
232 | "Add a Valid(not-revoked) DB entry about a cert (given in file)"}, | |
7e1b7485 RS |
233 | {"status", OPT_STATUS, 's', "Shows cert status given the serial number"}, |
234 | {"updatedb", OPT_UPDATEDB, '-', "Updates db for expired cert"}, | |
235 | {"crlexts", OPT_CRLEXTS, 's', | |
236 | "CRL extension section (override value in config file)"}, | |
6755ff11 MR |
237 | {"crl_reason", OPT_CRL_REASON, 's', "revocation reason"}, |
238 | {"crl_hold", OPT_CRL_HOLD, 's', | |
239 | "the hold instruction, an OID. Sets revocation reason to certificateHold"}, | |
240 | {"crl_compromise", OPT_CRL_COMPROMISE, 's', | |
241 | "sets compromise time to val and the revocation reason to keyCompromise"}, | |
242 | {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE, 's', | |
243 | "sets compromise time to val and the revocation reason to CACompromise"}, | |
64713cb1 CN |
244 | {"crl_lastupdate", OPT_CRL_LASTUPDATE, 's', |
245 | "Sets the CRL lastUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"}, | |
246 | {"crl_nextupdate", OPT_CRL_NEXTUPDATE, 's', | |
247 | "Sets the CRL nextUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"}, | |
5388f986 RS |
248 | {"crldays", OPT_CRLDAYS, 'p', "Days until the next CRL is due"}, |
249 | {"crlhours", OPT_CRLHOURS, 'p', "Hours until the next CRL is due"}, | |
250 | {"crlsec", OPT_CRLSEC, 'p', "Seconds until the next CRL is due"}, | |
251 | {"revoke", OPT_REVOKE, '<', "Revoke a cert (given in file)"}, | |
252 | ||
3ee1eac2 | 253 | OPT_R_OPTIONS, |
6bd4e3f2 | 254 | OPT_PROV_OPTIONS, |
92de469f RS |
255 | |
256 | OPT_PARAMETERS(), | |
257 | {"certreq", 0, 0, "Certificate requests to be signed (optional)"}, | |
7e1b7485 RS |
258 | {NULL} |
259 | }; | |
667ac4ec | 260 | |
7e1b7485 | 261 | int ca_main(int argc, char **argv) |
0f113f3e | 262 | { |
fba13663 | 263 | CONF *conf = NULL; |
0f113f3e | 264 | ENGINE *e = NULL; |
7e1b7485 | 265 | BIGNUM *crlnumber = NULL, *serial = NULL; |
0f113f3e | 266 | EVP_PKEY *pkey = NULL; |
2d87ee68 | 267 | BIO *in = NULL, *out = NULL, *Sout = NULL; |
0f113f3e | 268 | ASN1_INTEGER *tmpser; |
7e1b7485 RS |
269 | CA_DB *db = NULL; |
270 | DB_ATTR db_attr; | |
0f113f3e | 271 | STACK_OF(CONF_VALUE) *attribs = NULL; |
2292c8e1 | 272 | STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL; |
7e1b7485 RS |
273 | STACK_OF(X509) *cert_sk = NULL; |
274 | X509_CRL *crl = NULL; | |
fba13663 | 275 | char *configfile = default_config_file, *section = NULL; |
91034b68 PG |
276 | char def_dgst[80] = ""; |
277 | char *dgst = NULL, *policy = NULL, *keyfile = NULL; | |
2a33470b | 278 | char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL; |
d382e796 | 279 | int certformat = FORMAT_UNDEF, informat = FORMAT_UNDEF; |
8c5bff22 | 280 | unsigned long dateopt = ASN1_DTFLGS_RFC822; |
cc696296 F |
281 | const char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL; |
282 | const char *extensions = NULL, *extfile = NULL, *passinarg = NULL; | |
2a33470b | 283 | char *passin = NULL; |
7e1b7485 | 284 | char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL; |
cc696296 F |
285 | const char *serialfile = NULL, *subj = NULL; |
286 | char *prog, *startdate = NULL, *enddate = NULL; | |
3ee1eac2 | 287 | char *dbfile = NULL, *f; |
b5895815 | 288 | char new_cert[PATH_MAX]; |
3a19b22a | 289 | char tmp[10 + 1] = "\0"; |
333b070e | 290 | char *const *pp; |
7e1b7485 | 291 | const char *p; |
b5895815 | 292 | size_t outdirlen = 0; |
2a33470b | 293 | int create_ser = 0, free_passin = 0, total = 0, total_done = 0; |
7e1b7485 | 294 | int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE; |
d382e796 | 295 | int keyformat = FORMAT_UNDEF, multirdn = 1, notext = 0, output_der = 0; |
7e1b7485 | 296 | int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0; |
91034b68 | 297 | int rand_ser = 0, i, j, selfsign = 0, def_ret; |
64713cb1 | 298 | char *crl_lastupdate = NULL, *crl_nextupdate = NULL; |
cc01d217 | 299 | long crldays = 0, crlhours = 0, crlsec = 0, days = 0; |
b5c4209b | 300 | unsigned long chtype = MBSTRING_ASC, certopt = 0; |
7e1b7485 | 301 | X509 *x509 = NULL, *x509p = NULL, *x = NULL; |
06a79af2 | 302 | REVINFO_TYPE rev_type = REV_NONE; |
7e1b7485 RS |
303 | X509_REVOKED *r = NULL; |
304 | OPTION_CHOICE o; | |
d02b48c6 | 305 | |
7e1b7485 RS |
306 | prog = opt_init(argc, argv, ca_options); |
307 | while ((o = opt_next()) != OPT_EOF) { | |
308 | switch (o) { | |
8bf78043 F |
309 | case OPT_EOF: |
310 | case OPT_ERR: | |
7e1b7485 | 311 | opthelp: |
8bf78043 F |
312 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
313 | goto end; | |
314 | case OPT_HELP: | |
315 | opt_help(ca_options); | |
316 | ret = 0; | |
317 | goto end; | |
318 | case OPT_IN: | |
319 | req = 1; | |
320 | infile = opt_arg(); | |
321 | break; | |
9d5aca65 DO |
322 | case OPT_INFORM: |
323 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
324 | goto opthelp; | |
325 | break; | |
8bf78043 F |
326 | case OPT_OUT: |
327 | outfile = opt_arg(); | |
328 | break; | |
8c5bff22 WE |
329 | case OPT_DATEOPT: |
330 | if (!set_dateopt(&dateopt, opt_arg())) | |
331 | goto opthelp; | |
332 | break; | |
8bf78043 F |
333 | case OPT_VERBOSE: |
334 | verbose = 1; | |
335 | break; | |
a414fd67 PP |
336 | case OPT_QUIET: |
337 | verbose = 0; | |
338 | break; | |
8bf78043 F |
339 | case OPT_CONFIG: |
340 | configfile = opt_arg(); | |
341 | break; | |
342 | case OPT_NAME: | |
343 | section = opt_arg(); | |
344 | break; | |
345 | case OPT_SUBJ: | |
346 | subj = opt_arg(); | |
347 | /* preserve=1; */ | |
348 | break; | |
349 | case OPT_UTF8: | |
350 | chtype = MBSTRING_UTF8; | |
351 | break; | |
ffb46830 RS |
352 | case OPT_RAND_SERIAL: |
353 | rand_ser = 1; | |
354 | break; | |
7e1b7485 | 355 | case OPT_CREATE_SERIAL: |
0f113f3e | 356 | create_ser = 1; |
7e1b7485 RS |
357 | break; |
358 | case OPT_MULTIVALUE_RDN: | |
5a0991d0 | 359 | /* obsolete */ |
7e1b7485 RS |
360 | break; |
361 | case OPT_STARTDATE: | |
362 | startdate = opt_arg(); | |
363 | break; | |
364 | case OPT_ENDDATE: | |
365 | enddate = opt_arg(); | |
366 | break; | |
367 | case OPT_DAYS: | |
368 | days = atoi(opt_arg()); | |
369 | break; | |
370 | case OPT_MD: | |
91034b68 | 371 | dgst = opt_arg(); |
7e1b7485 RS |
372 | break; |
373 | case OPT_POLICY: | |
374 | policy = opt_arg(); | |
375 | break; | |
376 | case OPT_KEYFILE: | |
377 | keyfile = opt_arg(); | |
378 | break; | |
379 | case OPT_KEYFORM: | |
380 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) | |
381 | goto opthelp; | |
382 | break; | |
383 | case OPT_PASSIN: | |
384 | passinarg = opt_arg(); | |
385 | break; | |
3ee1eac2 RS |
386 | case OPT_R_CASES: |
387 | if (!opt_rand(o)) | |
388 | goto end; | |
389 | break; | |
6bd4e3f2 P |
390 | case OPT_PROV_CASES: |
391 | if (!opt_provider(o)) | |
392 | goto end; | |
393 | break; | |
7e1b7485 | 394 | case OPT_KEY: |
2a33470b | 395 | passin = opt_arg(); |
7e1b7485 RS |
396 | break; |
397 | case OPT_CERT: | |
398 | certfile = opt_arg(); | |
399 | break; | |
9d5aca65 | 400 | case OPT_CERTFORM: |
6d382c74 | 401 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &certformat)) |
9d5aca65 DO |
402 | goto opthelp; |
403 | break; | |
7e1b7485 | 404 | case OPT_SELFSIGN: |
0f113f3e | 405 | selfsign = 1; |
7e1b7485 RS |
406 | break; |
407 | case OPT_OUTDIR: | |
408 | outdir = opt_arg(); | |
409 | break; | |
410 | case OPT_SIGOPT: | |
411 | if (sigopts == NULL) | |
0f113f3e | 412 | sigopts = sk_OPENSSL_STRING_new_null(); |
b5895815 | 413 | if (sigopts == NULL || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
7e1b7485 RS |
414 | goto end; |
415 | break; | |
2292c8e1 RL |
416 | case OPT_VFYOPT: |
417 | if (vfyopts == NULL) | |
418 | vfyopts = sk_OPENSSL_STRING_new_null(); | |
419 | if (vfyopts == NULL || !sk_OPENSSL_STRING_push(vfyopts, opt_arg())) | |
420 | goto end; | |
421 | break; | |
7e1b7485 | 422 | case OPT_NOTEXT: |
0f113f3e | 423 | notext = 1; |
7e1b7485 RS |
424 | break; |
425 | case OPT_BATCH: | |
0f113f3e | 426 | batch = 1; |
7e1b7485 RS |
427 | break; |
428 | case OPT_PRESERVEDN: | |
0f113f3e | 429 | preserve = 1; |
7e1b7485 RS |
430 | break; |
431 | case OPT_NOEMAILDN: | |
0f113f3e | 432 | email_dn = 0; |
7e1b7485 RS |
433 | break; |
434 | case OPT_GENCRL: | |
0f113f3e | 435 | gencrl = 1; |
7e1b7485 RS |
436 | break; |
437 | case OPT_MSIE_HACK: | |
0f113f3e | 438 | msie_hack = 1; |
0f113f3e | 439 | break; |
64713cb1 CN |
440 | case OPT_CRL_LASTUPDATE: |
441 | crl_lastupdate = opt_arg(); | |
442 | break; | |
443 | case OPT_CRL_NEXTUPDATE: | |
444 | crl_nextupdate = opt_arg(); | |
445 | break; | |
7e1b7485 RS |
446 | case OPT_CRLDAYS: |
447 | crldays = atol(opt_arg()); | |
448 | break; | |
449 | case OPT_CRLHOURS: | |
450 | crlhours = atol(opt_arg()); | |
451 | break; | |
452 | case OPT_CRLSEC: | |
453 | crlsec = atol(opt_arg()); | |
454 | break; | |
455 | case OPT_INFILES: | |
0f113f3e | 456 | req = 1; |
7e1b7485 RS |
457 | goto end_of_options; |
458 | case OPT_SS_CERT: | |
459 | ss_cert_file = opt_arg(); | |
0f113f3e | 460 | req = 1; |
7e1b7485 RS |
461 | break; |
462 | case OPT_SPKAC: | |
463 | spkac_file = opt_arg(); | |
464 | req = 1; | |
465 | break; | |
466 | case OPT_REVOKE: | |
467 | infile = opt_arg(); | |
0f113f3e | 468 | dorevoke = 1; |
7e1b7485 RS |
469 | break; |
470 | case OPT_VALID: | |
471 | infile = opt_arg(); | |
0f113f3e | 472 | dorevoke = 2; |
7e1b7485 RS |
473 | break; |
474 | case OPT_EXTENSIONS: | |
475 | extensions = opt_arg(); | |
476 | break; | |
477 | case OPT_EXTFILE: | |
478 | extfile = opt_arg(); | |
479 | break; | |
480 | case OPT_STATUS: | |
481 | ser_status = opt_arg(); | |
482 | break; | |
483 | case OPT_UPDATEDB: | |
0f113f3e | 484 | doupdatedb = 1; |
7e1b7485 RS |
485 | break; |
486 | case OPT_CRLEXTS: | |
487 | crl_ext = opt_arg(); | |
488 | break; | |
b5895815 | 489 | case OPT_CRL_REASON: /* := REV_CRL_REASON */ |
7e1b7485 | 490 | case OPT_CRL_HOLD: |
7e1b7485 | 491 | case OPT_CRL_COMPROMISE: |
7e1b7485 RS |
492 | case OPT_CRL_CA_COMPROMISE: |
493 | rev_arg = opt_arg(); | |
06a79af2 | 494 | rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON; |
7e1b7485 RS |
495 | break; |
496 | case OPT_ENGINE: | |
333b070e | 497 | e = setup_engine(opt_arg(), 0); |
0f113f3e MC |
498 | break; |
499 | } | |
0f113f3e | 500 | } |
021410ea | 501 | |
7e1b7485 | 502 | end_of_options: |
021410ea | 503 | /* Remaining args are files to certify. */ |
7e1b7485 RS |
504 | argc = opt_num_rest(); |
505 | argv = opt_rest(); | |
0f113f3e | 506 | |
15795943 | 507 | if ((conf = app_load_config_verbose(configfile, 1)) == NULL) |
2e66d3d6 | 508 | goto end; |
c821defc | 509 | if (configfile != default_config_file && !app_load_modules(conf)) |
2e66d3d6 | 510 | goto end; |
b5a93e22 | 511 | |
0f113f3e | 512 | /* Lets get the config section we are using */ |
edbff8da F |
513 | if (section == NULL |
514 | && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL) | |
515 | goto end; | |
0f113f3e | 516 | |
da7f81d3 | 517 | p = app_conf_try_string(conf, NULL, "oid_file"); |
df364297 F |
518 | if (p != NULL) { |
519 | BIO *oid_bio = BIO_new_file(p, "r"); | |
0f113f3e | 520 | |
df364297 F |
521 | if (oid_bio == NULL) { |
522 | ERR_clear_error(); | |
523 | } else { | |
524 | OBJ_create_objects(oid_bio); | |
525 | BIO_free(oid_bio); | |
0f113f3e MC |
526 | } |
527 | } | |
9c73e48a | 528 | if (!add_oid_section(conf)) |
df364297 | 529 | goto end; |
0f113f3e | 530 | |
3ee1eac2 | 531 | app_RAND_load_conf(conf, BASE_SECTION); |
3ad60309 DDO |
532 | if (!app_RAND_load()) |
533 | goto end; | |
0f113f3e | 534 | |
da7f81d3 | 535 | f = app_conf_try_string(conf, section, STRING_MASK); |
2234212c | 536 | if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) { |
0f113f3e | 537 | BIO_printf(bio_err, "Invalid global string mask setting %s\n", f); |
7e1b7485 | 538 | goto end; |
0f113f3e MC |
539 | } |
540 | ||
541 | if (chtype != MBSTRING_UTF8) { | |
da7f81d3 DDO |
542 | f = app_conf_try_string(conf, section, UTF8_IN); |
543 | if (f != NULL && strcmp(f, "yes") == 0) | |
0f113f3e MC |
544 | chtype = MBSTRING_UTF8; |
545 | } | |
546 | ||
547 | db_attr.unique_subject = 1; | |
da7f81d3 | 548 | p = app_conf_try_string(conf, section, ENV_UNIQUE_SUBJECT); |
2234212c | 549 | if (p != NULL) |
0f113f3e | 550 | db_attr.unique_subject = parse_yesno(p, 1); |
0f113f3e | 551 | |
8bf78043 | 552 | /*****************************************************************/ |
0f113f3e MC |
553 | /* report status of cert with serial number given on command line */ |
554 | if (ser_status) { | |
edbff8da | 555 | dbfile = lookup_conf(conf, section, ENV_DATABASE); |
d6073e27 | 556 | if (dbfile == NULL) |
7e1b7485 | 557 | goto end; |
edbff8da | 558 | |
0f113f3e | 559 | db = load_index(dbfile, &db_attr); |
ca29cc14 FM |
560 | if (db == NULL) { |
561 | BIO_printf(bio_err, "Problem with index file: %s (could not load/parse file)\n", dbfile); | |
7e1b7485 | 562 | goto end; |
ca29cc14 | 563 | } |
0f113f3e | 564 | |
a4107d73 | 565 | if (index_index(db) <= 0) |
7e1b7485 | 566 | goto end; |
0f113f3e MC |
567 | |
568 | if (get_certificate_status(ser_status, db) != 1) | |
569 | BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status); | |
7e1b7485 | 570 | goto end; |
0f113f3e MC |
571 | } |
572 | ||
8bf78043 | 573 | /*****************************************************************/ |
0f113f3e MC |
574 | /* we definitely need a private key, so let's get it */ |
575 | ||
edbff8da F |
576 | if (keyfile == NULL |
577 | && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL) | |
7e1b7485 | 578 | goto end; |
edbff8da | 579 | |
2a33470b DDO |
580 | if (passin == NULL) { |
581 | free_passin = 1; | |
582 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { | |
0f113f3e | 583 | BIO_printf(bio_err, "Error getting password\n"); |
7e1b7485 | 584 | goto end; |
0f113f3e MC |
585 | } |
586 | } | |
2a33470b | 587 | pkey = load_key(keyfile, keyformat, 0, passin, e, "CA private key"); |
c1097eec | 588 | cleanse(passin); |
2234212c | 589 | if (pkey == NULL) |
0f113f3e | 590 | /* load_key() has already printed an appropriate message */ |
7e1b7485 | 591 | goto end; |
0f113f3e | 592 | |
8bf78043 | 593 | /*****************************************************************/ |
0f113f3e MC |
594 | /* we need a certificate */ |
595 | if (!selfsign || spkac_file || ss_cert_file || gencrl) { | |
edbff8da F |
596 | if (certfile == NULL |
597 | && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL) | |
7e1b7485 | 598 | goto end; |
edbff8da | 599 | |
d382e796 | 600 | x509 = load_cert_pass(certfile, certformat, 1, passin, "CA certificate"); |
0f113f3e | 601 | if (x509 == NULL) |
7e1b7485 | 602 | goto end; |
0f113f3e MC |
603 | |
604 | if (!X509_check_private_key(x509, pkey)) { | |
605 | BIO_printf(bio_err, | |
606 | "CA certificate and CA private key do not match\n"); | |
7e1b7485 | 607 | goto end; |
0f113f3e MC |
608 | } |
609 | } | |
610 | if (!selfsign) | |
611 | x509p = x509; | |
612 | ||
da7f81d3 DDO |
613 | f = app_conf_try_string(conf, BASE_SECTION, ENV_PRESERVE); |
614 | if (f != NULL && (*f == 'y' || *f == 'Y')) | |
0f113f3e | 615 | preserve = 1; |
da7f81d3 DDO |
616 | f = app_conf_try_string(conf, BASE_SECTION, ENV_MSIE_HACK); |
617 | if (f != NULL && (*f == 'y' || *f == 'Y')) | |
0f113f3e MC |
618 | msie_hack = 1; |
619 | ||
da7f81d3 | 620 | f = app_conf_try_string(conf, section, ENV_NAMEOPT); |
2234212c | 621 | if (f != NULL) { |
b5c4209b | 622 | if (!set_nameopt(f)) { |
0f113f3e | 623 | BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f); |
7e1b7485 | 624 | goto end; |
0f113f3e MC |
625 | } |
626 | default_op = 0; | |
f1cece55 | 627 | } |
0f113f3e | 628 | |
da7f81d3 | 629 | f = app_conf_try_string(conf, section, ENV_CERTOPT); |
2234212c | 630 | if (f != NULL) { |
0f113f3e MC |
631 | if (!set_cert_ex(&certopt, f)) { |
632 | BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f); | |
7e1b7485 | 633 | goto end; |
0f113f3e MC |
634 | } |
635 | default_op = 0; | |
2234212c | 636 | } |
0f113f3e | 637 | |
da7f81d3 | 638 | f = app_conf_try_string(conf, section, ENV_EXTCOPY); |
2234212c | 639 | if (f != NULL) { |
0f113f3e MC |
640 | if (!set_ext_copy(&ext_copy, f)) { |
641 | BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f); | |
7e1b7485 | 642 | goto end; |
0f113f3e | 643 | } |
2234212c | 644 | } |
0f113f3e | 645 | |
8bf78043 | 646 | /*****************************************************************/ |
0f113f3e MC |
647 | /* lookup where to write new certificates */ |
648 | if ((outdir == NULL) && (req)) { | |
649 | ||
edbff8da F |
650 | outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR); |
651 | if (outdir == NULL) { | |
0f113f3e MC |
652 | BIO_printf(bio_err, |
653 | "there needs to be defined a directory for new certificate to be placed in\n"); | |
7e1b7485 | 654 | goto end; |
0f113f3e | 655 | } |
bc36ee62 | 656 | #ifndef OPENSSL_SYS_VMS |
0f113f3e MC |
657 | /* |
658 | * outdir is a directory spec, but access() for VMS demands a | |
b6d3cb54 | 659 | * filename. We could use the DEC C routine to convert the |
46d08509 | 660 | * directory syntax to Unix, and give that to app_isdir, |
b6d3cb54 RS |
661 | * but for now the fopen will catch the error if it's not a |
662 | * directory | |
0f113f3e | 663 | */ |
0f113f3e | 664 | if (app_isdir(outdir) <= 0) { |
b6d3cb54 | 665 | BIO_printf(bio_err, "%s: %s is not a directory\n", prog, outdir); |
0f113f3e | 666 | perror(outdir); |
7e1b7485 | 667 | goto end; |
0f113f3e | 668 | } |
79875776 | 669 | #endif |
0f113f3e MC |
670 | } |
671 | ||
8bf78043 | 672 | /*****************************************************************/ |
0f113f3e | 673 | /* we need to load the database file */ |
edbff8da F |
674 | dbfile = lookup_conf(conf, section, ENV_DATABASE); |
675 | if (dbfile == NULL) | |
7e1b7485 | 676 | goto end; |
edbff8da | 677 | |
0f113f3e | 678 | db = load_index(dbfile, &db_attr); |
ca29cc14 FM |
679 | if (db == NULL) { |
680 | BIO_printf(bio_err, "Problem with index file: %s (could not load/parse file)\n", dbfile); | |
7e1b7485 | 681 | goto end; |
ca29cc14 | 682 | } |
0f113f3e MC |
683 | |
684 | /* Lets check some fields */ | |
685 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
686 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
687 | if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) { | |
688 | BIO_printf(bio_err, | |
689 | "entry %d: not revoked yet, but has a revocation date\n", | |
690 | i + 1); | |
7e1b7485 | 691 | goto end; |
0f113f3e MC |
692 | } |
693 | if ((pp[DB_type][0] == DB_TYPE_REV) && | |
694 | !make_revoked(NULL, pp[DB_rev_date])) { | |
695 | BIO_printf(bio_err, " in entry %d\n", i + 1); | |
7e1b7485 | 696 | goto end; |
0f113f3e MC |
697 | } |
698 | if (!check_time_format((char *)pp[DB_exp_date])) { | |
699 | BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1); | |
7e1b7485 | 700 | goto end; |
0f113f3e MC |
701 | } |
702 | p = pp[DB_serial]; | |
703 | j = strlen(p); | |
704 | if (*p == '-') { | |
705 | p++; | |
706 | j--; | |
707 | } | |
708 | if ((j & 1) || (j < 2)) { | |
709 | BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n", | |
710 | i + 1, j); | |
7e1b7485 | 711 | goto end; |
0f113f3e | 712 | } |
2fa45e6e | 713 | for ( ; *p; p++) { |
18295f0c | 714 | if (!isxdigit(_UC(*p))) { |
0f113f3e | 715 | BIO_printf(bio_err, |
2fa45e6e RS |
716 | "entry %d: bad char 0%o '%c' in serial number\n", |
717 | i + 1, *p, *p); | |
7e1b7485 | 718 | goto end; |
0f113f3e | 719 | } |
0f113f3e MC |
720 | } |
721 | } | |
722 | if (verbose) { | |
7e1b7485 | 723 | TXT_DB_write(bio_out, db->db); |
0f113f3e MC |
724 | BIO_printf(bio_err, "%d entries loaded from the database\n", |
725 | sk_OPENSSL_PSTRING_num(db->db->data)); | |
726 | BIO_printf(bio_err, "generating index\n"); | |
727 | } | |
728 | ||
a4107d73 | 729 | if (index_index(db) <= 0) |
7e1b7485 | 730 | goto end; |
0f113f3e | 731 | |
8bf78043 | 732 | /*****************************************************************/ |
0f113f3e MC |
733 | /* Update the db file for expired certificates */ |
734 | if (doupdatedb) { | |
735 | if (verbose) | |
736 | BIO_printf(bio_err, "Updating %s ...\n", dbfile); | |
737 | ||
065121ff | 738 | i = do_updatedb(db, NULL); |
0f113f3e MC |
739 | if (i == -1) { |
740 | BIO_printf(bio_err, "Malloc failure\n"); | |
7e1b7485 | 741 | goto end; |
0f113f3e MC |
742 | } else if (i == 0) { |
743 | if (verbose) | |
744 | BIO_printf(bio_err, "No entries found to mark expired\n"); | |
745 | } else { | |
746 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 747 | goto end; |
0f113f3e MC |
748 | |
749 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 750 | goto end; |
0f113f3e MC |
751 | |
752 | if (verbose) | |
b5895815 | 753 | BIO_printf(bio_err, "Done. %d entries marked as expired\n", i); |
0f113f3e MC |
754 | } |
755 | } | |
756 | ||
cc01d217 | 757 | /*****************************************************************/ |
0f113f3e MC |
758 | /* Read extensions config file */ |
759 | if (extfile) { | |
1a683b80 | 760 | if ((extfile_conf = app_load_config(extfile)) == NULL) { |
0f113f3e | 761 | ret = 1; |
7e1b7485 | 762 | goto end; |
0f113f3e MC |
763 | } |
764 | ||
765 | if (verbose) | |
766 | BIO_printf(bio_err, "Successfully loaded extensions file %s\n", | |
767 | extfile); | |
768 | ||
769 | /* We can have sections in the ext file */ | |
edbff8da | 770 | if (extensions == NULL) { |
da7f81d3 DDO |
771 | extensions = |
772 | app_conf_try_string(extfile_conf, "default", "extensions"); | |
773 | if (extensions == NULL) | |
edbff8da F |
774 | extensions = "default"; |
775 | } | |
0f113f3e MC |
776 | } |
777 | ||
d303b9d8 | 778 | /*****************************************************************/ |
0f113f3e | 779 | if (req || gencrl) { |
69f6b3ce | 780 | if (spkac_file != NULL && outfile != NULL) { |
25642ad2 RL |
781 | output_der = 1; |
782 | batch = 1; | |
783 | } | |
0f113f3e MC |
784 | } |
785 | ||
91034b68 | 786 | def_ret = EVP_PKEY_get_default_digest_name(pkey, def_dgst, sizeof(def_dgst)); |
aabbc24e | 787 | /* |
91034b68 | 788 | * EVP_PKEY_get_default_digest_name() returns 2 if the digest is |
aabbc24e MC |
789 | * mandatory for this algorithm. |
790 | */ | |
91034b68 | 791 | if (def_ret == 2 && strcmp(def_dgst, "UNDEF") == 0) { |
aabbc24e | 792 | /* The signing algorithm requires there to be no digest */ |
91034b68 PG |
793 | dgst = NULL; |
794 | } else if (dgst == NULL | |
795 | && (dgst = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL) { | |
aabbc24e | 796 | goto end; |
f3021aca | 797 | } else { |
91034b68 | 798 | if (strcmp(dgst, "default") == 0) { |
aabbc24e | 799 | if (def_ret <= 0) { |
f3021aca MC |
800 | BIO_puts(bio_err, "no default digest\n"); |
801 | goto end; | |
802 | } | |
91034b68 | 803 | dgst = def_dgst; |
0f113f3e | 804 | } |
0f113f3e MC |
805 | } |
806 | ||
807 | if (req) { | |
cdd202f2 F |
808 | if (email_dn == 1) { |
809 | char *tmp_email_dn = NULL; | |
810 | ||
da7f81d3 DDO |
811 | tmp_email_dn = |
812 | app_conf_try_string(conf, section, ENV_DEFAULT_EMAIL_DN); | |
cdd202f2 | 813 | if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0) |
0f113f3e MC |
814 | email_dn = 0; |
815 | } | |
816 | if (verbose) | |
91034b68 | 817 | BIO_printf(bio_err, "message digest is %s\n", dgst); |
edbff8da F |
818 | if (policy == NULL |
819 | && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL) | |
7e1b7485 | 820 | goto end; |
edbff8da | 821 | |
0f113f3e MC |
822 | if (verbose) |
823 | BIO_printf(bio_err, "policy is %s\n", policy); | |
824 | ||
da7f81d3 | 825 | if (app_conf_try_string(conf, section, ENV_RAND_SERIAL) != NULL) { |
ffb46830 RS |
826 | rand_ser = 1; |
827 | } else { | |
828 | serialfile = lookup_conf(conf, section, ENV_SERIAL); | |
829 | if (serialfile == NULL) | |
830 | goto end; | |
831 | } | |
0f113f3e | 832 | |
1a683b80 DDO |
833 | if (extfile_conf != NULL) { |
834 | /* Check syntax of extfile */ | |
835 | X509V3_CTX ctx; | |
836 | ||
837 | X509V3_set_ctx_test(&ctx); | |
838 | X509V3_set_nconf(&ctx, extfile_conf); | |
839 | if (!X509V3_EXT_add_nconf(extfile_conf, &ctx, extensions, NULL)) { | |
840 | BIO_printf(bio_err, | |
841 | "Error checking certificate extensions from extfile section %s\n", | |
842 | extensions); | |
843 | ret = 1; | |
844 | goto end; | |
845 | } | |
846 | } else { | |
0f113f3e MC |
847 | /* |
848 | * no '-extfile' option, so we look for extensions in the main | |
849 | * configuration file | |
850 | */ | |
da7f81d3 DDO |
851 | if (extensions == NULL) |
852 | extensions = app_conf_try_string(conf, section, ENV_EXTENSIONS); | |
2234212c | 853 | if (extensions != NULL) { |
1a683b80 | 854 | /* Check syntax of config file section */ |
0f113f3e | 855 | X509V3_CTX ctx; |
d44a8a16 | 856 | |
0f113f3e MC |
857 | X509V3_set_ctx_test(&ctx); |
858 | X509V3_set_nconf(&ctx, conf); | |
859 | if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) { | |
860 | BIO_printf(bio_err, | |
1a683b80 | 861 | "Error checking certificate extension config section %s\n", |
0f113f3e MC |
862 | extensions); |
863 | ret = 1; | |
7e1b7485 | 864 | goto end; |
0f113f3e MC |
865 | } |
866 | } | |
867 | } | |
868 | ||
da7f81d3 DDO |
869 | if (startdate == NULL) |
870 | startdate = | |
871 | app_conf_try_string(conf, section, ENV_DEFAULT_STARTDATE); | |
2234212c | 872 | if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) { |
0f113f3e MC |
873 | BIO_printf(bio_err, |
874 | "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 875 | goto end; |
0f113f3e MC |
876 | } |
877 | if (startdate == NULL) | |
878 | startdate = "today"; | |
879 | ||
da7f81d3 DDO |
880 | if (enddate == NULL) |
881 | enddate = app_conf_try_string(conf, section, ENV_DEFAULT_ENDDATE); | |
2234212c | 882 | if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) { |
0f113f3e MC |
883 | BIO_printf(bio_err, |
884 | "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 885 | goto end; |
0f113f3e MC |
886 | } |
887 | ||
888 | if (days == 0) { | |
b7782687 | 889 | if (!app_conf_try_number(conf, section, ENV_DEFAULT_DAYS, &days)) |
0f113f3e MC |
890 | days = 0; |
891 | } | |
b5895815 F |
892 | if (enddate == NULL && days == 0) { |
893 | BIO_printf(bio_err, "cannot lookup how many days to certify for\n"); | |
7e1b7485 | 894 | goto end; |
0f113f3e MC |
895 | } |
896 | ||
ffb46830 RS |
897 | if (rand_ser) { |
898 | if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) { | |
899 | BIO_printf(bio_err, "error generating serial number\n"); | |
900 | goto end; | |
901 | } | |
902 | } else { | |
ec8a3409 DDO |
903 | serial = load_serial(serialfile, NULL, create_ser, NULL); |
904 | if (serial == NULL) { | |
ffb46830 RS |
905 | BIO_printf(bio_err, "error while loading serial number\n"); |
906 | goto end; | |
907 | } | |
908 | if (verbose) { | |
909 | if (BN_is_zero(serial)) { | |
910 | BIO_printf(bio_err, "next serial number is 00\n"); | |
911 | } else { | |
912 | if ((f = BN_bn2hex(serial)) == NULL) | |
913 | goto end; | |
914 | BIO_printf(bio_err, "next serial number is %s\n", f); | |
915 | OPENSSL_free(f); | |
916 | } | |
0f113f3e MC |
917 | } |
918 | } | |
919 | ||
920 | if ((attribs = NCONF_get_section(conf, policy)) == NULL) { | |
921 | BIO_printf(bio_err, "unable to find 'section' for %s\n", policy); | |
7e1b7485 | 922 | goto end; |
0f113f3e MC |
923 | } |
924 | ||
925 | if ((cert_sk = sk_X509_new_null()) == NULL) { | |
926 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 927 | goto end; |
0f113f3e MC |
928 | } |
929 | if (spkac_file != NULL) { | |
930 | total++; | |
931 | j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts, | |
932 | attribs, db, serial, subj, chtype, multirdn, | |
933 | email_dn, startdate, enddate, days, extensions, | |
b5c4209b | 934 | conf, verbose, certopt, get_nameopt(), default_op, |
8c5bff22 | 935 | ext_copy, dateopt); |
0f113f3e | 936 | if (j < 0) |
7e1b7485 | 937 | goto end; |
0f113f3e MC |
938 | if (j > 0) { |
939 | total_done++; | |
940 | BIO_printf(bio_err, "\n"); | |
941 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 942 | goto end; |
0f113f3e MC |
943 | if (!sk_X509_push(cert_sk, x)) { |
944 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 945 | goto end; |
0f113f3e | 946 | } |
0f113f3e MC |
947 | } |
948 | } | |
949 | if (ss_cert_file != NULL) { | |
950 | total++; | |
2a33470b DDO |
951 | j = certify_cert(&x, ss_cert_file, certformat, passin, pkey, |
952 | x509, dgst, sigopts, vfyopts, attribs, | |
0f113f3e MC |
953 | db, serial, subj, chtype, multirdn, email_dn, |
954 | startdate, enddate, days, batch, extensions, | |
b5c4209b | 955 | conf, verbose, certopt, get_nameopt(), default_op, |
8c5bff22 | 956 | ext_copy, dateopt); |
0f113f3e | 957 | if (j < 0) |
7e1b7485 | 958 | goto end; |
0f113f3e MC |
959 | if (j > 0) { |
960 | total_done++; | |
961 | BIO_printf(bio_err, "\n"); | |
962 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 963 | goto end; |
0f113f3e MC |
964 | if (!sk_X509_push(cert_sk, x)) { |
965 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 966 | goto end; |
0f113f3e MC |
967 | } |
968 | } | |
969 | } | |
970 | if (infile != NULL) { | |
971 | total++; | |
9d5aca65 DO |
972 | j = certify(&x, infile, informat, pkey, x509p, dgst, |
973 | sigopts, vfyopts, attribs, db, | |
0f113f3e MC |
974 | serial, subj, chtype, multirdn, email_dn, startdate, |
975 | enddate, days, batch, extensions, conf, verbose, | |
8c5bff22 | 976 | certopt, get_nameopt(), default_op, ext_copy, selfsign, dateopt); |
0f113f3e | 977 | if (j < 0) |
7e1b7485 | 978 | goto end; |
0f113f3e MC |
979 | if (j > 0) { |
980 | total_done++; | |
981 | BIO_printf(bio_err, "\n"); | |
982 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 983 | goto end; |
0f113f3e MC |
984 | if (!sk_X509_push(cert_sk, x)) { |
985 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 986 | goto end; |
0f113f3e MC |
987 | } |
988 | } | |
989 | } | |
990 | for (i = 0; i < argc; i++) { | |
991 | total++; | |
9d5aca65 DO |
992 | j = certify(&x, argv[i], informat, pkey, x509p, dgst, |
993 | sigopts, vfyopts, | |
2292c8e1 | 994 | attribs, db, |
0f113f3e MC |
995 | serial, subj, chtype, multirdn, email_dn, startdate, |
996 | enddate, days, batch, extensions, conf, verbose, | |
8c5bff22 | 997 | certopt, get_nameopt(), default_op, ext_copy, selfsign, dateopt); |
0f113f3e | 998 | if (j < 0) |
7e1b7485 | 999 | goto end; |
0f113f3e MC |
1000 | if (j > 0) { |
1001 | total_done++; | |
1002 | BIO_printf(bio_err, "\n"); | |
aebd0e5c PK |
1003 | if (!BN_add_word(serial, 1)) { |
1004 | X509_free(x); | |
7e1b7485 | 1005 | goto end; |
aebd0e5c | 1006 | } |
0f113f3e MC |
1007 | if (!sk_X509_push(cert_sk, x)) { |
1008 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
aebd0e5c | 1009 | X509_free(x); |
7e1b7485 | 1010 | goto end; |
0f113f3e MC |
1011 | } |
1012 | } | |
1013 | } | |
1014 | /* | |
1015 | * we have a stack of newly certified certificates and a data base | |
1016 | * and serial number that need updating | |
1017 | */ | |
1018 | ||
1019 | if (sk_X509_num(cert_sk) > 0) { | |
1020 | if (!batch) { | |
1021 | BIO_printf(bio_err, | |
1022 | "\n%d out of %d certificate requests certified, commit? [y/n]", | |
1023 | total_done, total); | |
1024 | (void)BIO_flush(bio_err); | |
3a19b22a F |
1025 | tmp[0] = '\0'; |
1026 | if (fgets(tmp, sizeof(tmp), stdin) == NULL) { | |
b5895815 | 1027 | BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n"); |
0f113f3e | 1028 | ret = 0; |
7e1b7485 | 1029 | goto end; |
0f113f3e | 1030 | } |
3a19b22a | 1031 | if (tmp[0] != 'y' && tmp[0] != 'Y') { |
0f113f3e MC |
1032 | BIO_printf(bio_err, "CERTIFICATION CANCELED\n"); |
1033 | ret = 0; | |
7e1b7485 | 1034 | goto end; |
0f113f3e MC |
1035 | } |
1036 | } | |
1037 | ||
1038 | BIO_printf(bio_err, "Write out database with %d new entries\n", | |
1039 | sk_X509_num(cert_sk)); | |
1040 | ||
aeec793b | 1041 | if (serialfile != NULL |
ffb46830 | 1042 | && !save_serial(serialfile, "new", serial, NULL)) |
7e1b7485 | 1043 | goto end; |
0f113f3e MC |
1044 | |
1045 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1046 | goto end; |
0f113f3e MC |
1047 | } |
1048 | ||
b5895815 F |
1049 | outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert)); |
1050 | #ifndef OPENSSL_SYS_VMS | |
1051 | outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert)); | |
1052 | #endif | |
1053 | ||
0f113f3e MC |
1054 | if (verbose) |
1055 | BIO_printf(bio_err, "writing new certificates\n"); | |
b5895815 | 1056 | |
0f113f3e | 1057 | for (i = 0; i < sk_X509_num(cert_sk); i++) { |
2d87ee68 | 1058 | BIO *Cout = NULL; |
0db1fb3f | 1059 | X509 *xi = sk_X509_value(cert_sk, i); |
1337a3a9 | 1060 | const ASN1_INTEGER *serialNumber = X509_get0_serialNumber(xi); |
b5895815 F |
1061 | const unsigned char *psn = ASN1_STRING_get0_data(serialNumber); |
1062 | const int snl = ASN1_STRING_length(serialNumber); | |
1063 | const int filen_len = 2 * (snl > 0 ? snl : 1) + sizeof(".pem"); | |
1064 | char *n = new_cert + outdirlen; | |
0f113f3e | 1065 | |
b5895815 | 1066 | if (outdirlen + filen_len > PATH_MAX) { |
0f113f3e | 1067 | BIO_printf(bio_err, "certificate file name too long\n"); |
7e1b7485 | 1068 | goto end; |
0f113f3e MC |
1069 | } |
1070 | ||
b5895815 F |
1071 | if (snl > 0) { |
1072 | static const char HEX_DIGITS[] = "0123456789ABCDEF"; | |
7d7d2cbc | 1073 | |
b5895815 F |
1074 | for (j = 0; j < snl; j++, psn++) { |
1075 | *n++ = HEX_DIGITS[*psn >> 4]; | |
1076 | *n++ = HEX_DIGITS[*psn & 0x0F]; | |
0f113f3e MC |
1077 | } |
1078 | } else { | |
1079 | *(n++) = '0'; | |
1080 | *(n++) = '0'; | |
1081 | } | |
1082 | *(n++) = '.'; | |
1083 | *(n++) = 'p'; | |
1084 | *(n++) = 'e'; | |
1085 | *(n++) = 'm'; | |
b5895815 | 1086 | *n = '\0'; /* closing new_cert */ |
0f113f3e | 1087 | if (verbose) |
3a19b22a | 1088 | BIO_printf(bio_err, "writing %s\n", new_cert); |
0f113f3e | 1089 | |
63871d9f RL |
1090 | Sout = bio_open_default(outfile, 'w', |
1091 | output_der ? FORMAT_ASN1 : FORMAT_TEXT); | |
1092 | if (Sout == NULL) | |
1093 | goto end; | |
1094 | ||
3a19b22a | 1095 | Cout = BIO_new_file(new_cert, "w"); |
7e1b7485 | 1096 | if (Cout == NULL) { |
3a19b22a | 1097 | perror(new_cert); |
7e1b7485 | 1098 | goto end; |
0f113f3e | 1099 | } |
0db1fb3f F |
1100 | write_new_certificate(Cout, xi, 0, notext); |
1101 | write_new_certificate(Sout, xi, output_der, notext); | |
2d87ee68 | 1102 | BIO_free_all(Cout); |
63871d9f RL |
1103 | BIO_free_all(Sout); |
1104 | Sout = NULL; | |
0f113f3e MC |
1105 | } |
1106 | ||
1107 | if (sk_X509_num(cert_sk)) { | |
1108 | /* Rename the database and the serial file */ | |
aeec793b DMSP |
1109 | if (serialfile != NULL |
1110 | && !rotate_serial(serialfile, "new", "old")) | |
7e1b7485 | 1111 | goto end; |
0f113f3e MC |
1112 | |
1113 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1114 | goto end; |
0f113f3e MC |
1115 | |
1116 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1117 | } | |
1118 | } | |
1119 | ||
8bf78043 | 1120 | /*****************************************************************/ |
0f113f3e MC |
1121 | if (gencrl) { |
1122 | int crl_v2 = 0; | |
da7f81d3 DDO |
1123 | |
1124 | if (crl_ext == NULL) | |
1125 | crl_ext = app_conf_try_string(conf, section, ENV_CRLEXT); | |
2234212c | 1126 | if (crl_ext != NULL) { |
0f113f3e MC |
1127 | /* Check syntax of file */ |
1128 | X509V3_CTX ctx; | |
d44a8a16 | 1129 | |
0f113f3e MC |
1130 | X509V3_set_ctx_test(&ctx); |
1131 | X509V3_set_nconf(&ctx, conf); | |
1132 | if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) { | |
1133 | BIO_printf(bio_err, | |
1a683b80 | 1134 | "Error checking CRL extension section %s\n", crl_ext); |
0f113f3e | 1135 | ret = 1; |
7e1b7485 | 1136 | goto end; |
0f113f3e MC |
1137 | } |
1138 | } | |
1139 | ||
da7f81d3 | 1140 | crlnumberfile = app_conf_try_string(conf, section, ENV_CRLNUMBER); |
c8aec163 | 1141 | if (crlnumberfile != NULL) { |
ec8a3409 DDO |
1142 | if ((crlnumber = load_serial(crlnumberfile, NULL, 0, NULL)) |
1143 | == NULL) { | |
0f113f3e | 1144 | BIO_printf(bio_err, "error while loading CRL number\n"); |
7e1b7485 | 1145 | goto end; |
0f113f3e | 1146 | } |
c8aec163 | 1147 | } |
0f113f3e MC |
1148 | |
1149 | if (!crldays && !crlhours && !crlsec) { | |
b7782687 DDO |
1150 | if (!app_conf_try_number(conf, section, |
1151 | ENV_DEFAULT_CRL_DAYS, &crldays)) | |
0f113f3e | 1152 | crldays = 0; |
b7782687 DDO |
1153 | if (!app_conf_try_number(conf, section, |
1154 | ENV_DEFAULT_CRL_HOURS, &crlhours)) | |
0f113f3e | 1155 | crlhours = 0; |
0f113f3e | 1156 | } |
64713cb1 CN |
1157 | if ((crl_nextupdate == NULL) && |
1158 | (crldays == 0) && (crlhours == 0) && (crlsec == 0)) { | |
0f113f3e MC |
1159 | BIO_printf(bio_err, |
1160 | "cannot lookup how long until the next CRL is issued\n"); | |
7e1b7485 | 1161 | goto end; |
0f113f3e MC |
1162 | } |
1163 | ||
1164 | if (verbose) | |
1165 | BIO_printf(bio_err, "making CRL\n"); | |
e6c2f964 | 1166 | if ((crl = X509_CRL_new_ex(app_get0_libctx(), app_get0_propq())) == NULL) |
7e1b7485 | 1167 | goto end; |
0f113f3e | 1168 | if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) |
7e1b7485 | 1169 | goto end; |
0f113f3e | 1170 | |
64713cb1 CN |
1171 | if (!set_crl_lastupdate(crl, crl_lastupdate)) { |
1172 | BIO_puts(bio_err, "error setting CRL lastUpdate\n"); | |
1173 | ret = 1; | |
7e1b7485 | 1174 | goto end; |
0f113f3e | 1175 | } |
0f113f3e | 1176 | |
64713cb1 CN |
1177 | if (!set_crl_nextupdate(crl, crl_nextupdate, |
1178 | crldays, crlhours, crlsec)) { | |
1179 | BIO_puts(bio_err, "error setting CRL nextUpdate\n"); | |
1180 | ret = 1; | |
1181 | goto end; | |
1182 | } | |
0f113f3e MC |
1183 | |
1184 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
1185 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
1186 | if (pp[DB_type][0] == DB_TYPE_REV) { | |
1187 | if ((r = X509_REVOKED_new()) == NULL) | |
7e1b7485 | 1188 | goto end; |
0f113f3e MC |
1189 | j = make_revoked(r, pp[DB_rev_date]); |
1190 | if (!j) | |
7e1b7485 | 1191 | goto end; |
0f113f3e MC |
1192 | if (j == 2) |
1193 | crl_v2 = 1; | |
1194 | if (!BN_hex2bn(&serial, pp[DB_serial])) | |
7e1b7485 | 1195 | goto end; |
0f113f3e MC |
1196 | tmpser = BN_to_ASN1_INTEGER(serial, NULL); |
1197 | BN_free(serial); | |
1198 | serial = NULL; | |
1199 | if (!tmpser) | |
7e1b7485 | 1200 | goto end; |
0f113f3e MC |
1201 | X509_REVOKED_set_serialNumber(r, tmpser); |
1202 | ASN1_INTEGER_free(tmpser); | |
1203 | X509_CRL_add0_revoked(crl, r); | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | /* | |
1208 | * sort the data so it will be written in serial number order | |
1209 | */ | |
1210 | X509_CRL_sort(crl); | |
1211 | ||
1212 | /* we now have a CRL */ | |
1213 | if (verbose) | |
1214 | BIO_printf(bio_err, "signing CRL\n"); | |
1215 | ||
1216 | /* Add any extensions asked for */ | |
1217 | ||
2234212c | 1218 | if (crl_ext != NULL || crlnumberfile != NULL) { |
0f113f3e | 1219 | X509V3_CTX crlctx; |
d44a8a16 | 1220 | |
0f113f3e MC |
1221 | X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0); |
1222 | X509V3_set_nconf(&crlctx, conf); | |
1223 | ||
2234212c | 1224 | if (crl_ext != NULL) |
1a683b80 DDO |
1225 | if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl)) { |
1226 | BIO_printf(bio_err, | |
1227 | "Error adding CRL extensions from section %s\n", crl_ext); | |
7e1b7485 | 1228 | goto end; |
1a683b80 | 1229 | } |
0f113f3e MC |
1230 | if (crlnumberfile != NULL) { |
1231 | tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL); | |
1232 | if (!tmpser) | |
7e1b7485 | 1233 | goto end; |
0f113f3e MC |
1234 | X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0); |
1235 | ASN1_INTEGER_free(tmpser); | |
1236 | crl_v2 = 1; | |
1237 | if (!BN_add_word(crlnumber, 1)) | |
7e1b7485 | 1238 | goto end; |
0f113f3e MC |
1239 | } |
1240 | } | |
2234212c | 1241 | if (crl_ext != NULL || crl_v2) { |
cdf63a37 DB |
1242 | if (!X509_CRL_set_version(crl, X509_CRL_VERSION_2)) |
1243 | goto end; | |
0f113f3e MC |
1244 | } |
1245 | ||
1246 | /* we have a CRL number that need updating */ | |
aeec793b DMSP |
1247 | if (crlnumberfile != NULL |
1248 | && !save_serial(crlnumberfile, "new", crlnumber, NULL)) | |
1249 | goto end; | |
0f113f3e | 1250 | |
23a1d5e9 RS |
1251 | BN_free(crlnumber); |
1252 | crlnumber = NULL; | |
0f113f3e | 1253 | |
7e1b7485 RS |
1254 | if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts)) |
1255 | goto end; | |
0f113f3e | 1256 | |
63871d9f RL |
1257 | Sout = bio_open_default(outfile, 'w', |
1258 | output_der ? FORMAT_ASN1 : FORMAT_TEXT); | |
1259 | if (Sout == NULL) | |
1260 | goto end; | |
1261 | ||
0f113f3e MC |
1262 | PEM_write_bio_X509_CRL(Sout, crl); |
1263 | ||
aeec793b DMSP |
1264 | /* Rename the crlnumber file */ |
1265 | if (crlnumberfile != NULL | |
1266 | && !rotate_serial(crlnumberfile, "new", "old")) | |
1267 | goto end; | |
0f113f3e MC |
1268 | |
1269 | } | |
8bf78043 | 1270 | /*****************************************************************/ |
0f113f3e MC |
1271 | if (dorevoke) { |
1272 | if (infile == NULL) { | |
1273 | BIO_printf(bio_err, "no input files\n"); | |
7e1b7485 | 1274 | goto end; |
0f113f3e MC |
1275 | } else { |
1276 | X509 *revcert; | |
2a33470b | 1277 | |
d382e796 | 1278 | revcert = load_cert_pass(infile, informat, 1, passin, |
50eb2a50 | 1279 | "certificate to be revoked"); |
0f113f3e | 1280 | if (revcert == NULL) |
7e1b7485 | 1281 | goto end; |
0f113f3e | 1282 | if (dorevoke == 2) |
06a79af2 | 1283 | rev_type = REV_VALID; |
0f113f3e MC |
1284 | j = do_revoke(revcert, db, rev_type, rev_arg); |
1285 | if (j <= 0) | |
7e1b7485 | 1286 | goto end; |
0f113f3e MC |
1287 | X509_free(revcert); |
1288 | ||
1289 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1290 | goto end; |
0f113f3e MC |
1291 | |
1292 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1293 | goto end; |
0f113f3e MC |
1294 | |
1295 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1296 | } | |
1297 | } | |
0f113f3e | 1298 | ret = 0; |
ffb46830 | 1299 | |
7e1b7485 | 1300 | end: |
ffb46830 RS |
1301 | if (ret) |
1302 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1303 | BIO_free_all(Sout); |
1304 | BIO_free_all(out); | |
1305 | BIO_free_all(in); | |
79b2a2f2 | 1306 | OSSL_STACK_OF_X509_free(cert_sk); |
0f113f3e | 1307 | |
2a33470b DDO |
1308 | cleanse(passin); |
1309 | if (free_passin) | |
1310 | OPENSSL_free(passin); | |
0f113f3e MC |
1311 | BN_free(serial); |
1312 | BN_free(crlnumber); | |
1313 | free_index(db); | |
25aaa98a | 1314 | sk_OPENSSL_STRING_free(sigopts); |
2292c8e1 | 1315 | sk_OPENSSL_STRING_free(vfyopts); |
0f113f3e | 1316 | EVP_PKEY_free(pkey); |
222561fe | 1317 | X509_free(x509); |
0f113f3e MC |
1318 | X509_CRL_free(crl); |
1319 | NCONF_free(conf); | |
1a683b80 | 1320 | NCONF_free(extfile_conf); |
dd1abd44 | 1321 | release_engine(e); |
26a7d938 | 1322 | return ret; |
0f113f3e | 1323 | } |
d02b48c6 | 1324 | |
edbff8da | 1325 | static char *lookup_conf(const CONF *conf, const char *section, const char *tag) |
0f113f3e | 1326 | { |
edbff8da F |
1327 | char *entry = NCONF_get_string(conf, section, tag); |
1328 | if (entry == NULL) | |
1329 | BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag); | |
1330 | return entry; | |
0f113f3e | 1331 | } |
d02b48c6 | 1332 | |
9d5aca65 DO |
1333 | static int certify(X509 **xret, const char *infile, int informat, |
1334 | EVP_PKEY *pkey, X509 *x509, | |
91034b68 | 1335 | const char *dgst, |
2292c8e1 RL |
1336 | STACK_OF(OPENSSL_STRING) *sigopts, |
1337 | STACK_OF(OPENSSL_STRING) *vfyopts, | |
0f113f3e | 1338 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, |
cc696296 F |
1339 | BIGNUM *serial, const char *subj, unsigned long chtype, |
1340 | int multirdn, int email_dn, const char *startdate, | |
1341 | const char *enddate, | |
1342 | long days, int batch, const char *ext_sect, CONF *lconf, | |
0f113f3e | 1343 | int verbose, unsigned long certopt, unsigned long nameopt, |
8c5bff22 | 1344 | int default_op, int ext_copy, int selfsign, unsigned long dateopt) |
0f113f3e MC |
1345 | { |
1346 | X509_REQ *req = NULL; | |
0f113f3e MC |
1347 | EVP_PKEY *pktmp = NULL; |
1348 | int ok = -1, i; | |
1349 | ||
a75f707f | 1350 | req = load_csr_autofmt(infile, informat, vfyopts, "certificate request"); |
9d5aca65 | 1351 | if (req == NULL) |
7e1b7485 | 1352 | goto end; |
9c73e48a DO |
1353 | if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) { |
1354 | BIO_printf(bio_err, "Error unpacking public key\n"); | |
1355 | goto end; | |
1356 | } | |
0f113f3e | 1357 | if (verbose) |
b5c4209b | 1358 | X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT); |
0f113f3e MC |
1359 | |
1360 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
9c73e48a | 1361 | ok = 0; |
0f113f3e MC |
1362 | |
1363 | if (selfsign && !X509_REQ_check_private_key(req, pkey)) { | |
1364 | BIO_printf(bio_err, | |
1365 | "Certificate request and CA private key do not match\n"); | |
7e1b7485 | 1366 | goto end; |
0f113f3e | 1367 | } |
2292c8e1 | 1368 | i = do_X509_REQ_verify(req, pktmp, vfyopts); |
0f113f3e | 1369 | if (i < 0) { |
9c73e48a | 1370 | BIO_printf(bio_err, "Signature verification problems...\n"); |
7e1b7485 | 1371 | goto end; |
0f113f3e MC |
1372 | } |
1373 | if (i == 0) { | |
0f113f3e MC |
1374 | BIO_printf(bio_err, |
1375 | "Signature did not match the certificate request\n"); | |
7e1b7485 | 1376 | goto end; |
2234212c | 1377 | } |
9c73e48a | 1378 | BIO_printf(bio_err, "Signature ok\n"); |
0f113f3e MC |
1379 | |
1380 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
1381 | chtype, multirdn, email_dn, startdate, enddate, days, batch, | |
1382 | verbose, req, ext_sect, lconf, certopt, nameopt, default_op, | |
8c5bff22 | 1383 | ext_copy, selfsign, dateopt); |
0f113f3e | 1384 | |
7e1b7485 | 1385 | end: |
9c73e48a | 1386 | ERR_print_errors(bio_err); |
222561fe | 1387 | X509_REQ_free(req); |
26a7d938 | 1388 | return ok; |
0f113f3e | 1389 | } |
d02b48c6 | 1390 | |
9d5aca65 | 1391 | static int certify_cert(X509 **xret, const char *infile, int certformat, |
2a33470b | 1392 | const char *passin, EVP_PKEY *pkey, X509 *x509, |
91034b68 | 1393 | const char *dgst, |
2292c8e1 RL |
1394 | STACK_OF(OPENSSL_STRING) *sigopts, |
1395 | STACK_OF(OPENSSL_STRING) *vfyopts, | |
0f113f3e | 1396 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, |
cc696296 F |
1397 | BIGNUM *serial, const char *subj, unsigned long chtype, |
1398 | int multirdn, int email_dn, const char *startdate, | |
1399 | const char *enddate, long days, int batch, const char *ext_sect, | |
0f113f3e | 1400 | CONF *lconf, int verbose, unsigned long certopt, |
8c5bff22 | 1401 | unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt) |
0f113f3e | 1402 | { |
1061baf6 | 1403 | X509 *template_cert = NULL; |
0f113f3e MC |
1404 | X509_REQ *rreq = NULL; |
1405 | EVP_PKEY *pktmp = NULL; | |
1406 | int ok = -1, i; | |
1407 | ||
d382e796 | 1408 | if ((template_cert = load_cert_pass(infile, certformat, 1, passin, |
22dddfb9 | 1409 | "template certificate")) == NULL) |
7e1b7485 | 1410 | goto end; |
0f113f3e | 1411 | if (verbose) |
1061baf6 | 1412 | X509_print(bio_err, template_cert); |
0f113f3e MC |
1413 | |
1414 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
1415 | ||
1061baf6 | 1416 | if ((pktmp = X509_get0_pubkey(template_cert)) == NULL) { |
0f113f3e | 1417 | BIO_printf(bio_err, "error unpacking public key\n"); |
7e1b7485 | 1418 | goto end; |
0f113f3e | 1419 | } |
1061baf6 | 1420 | i = do_X509_verify(template_cert, pktmp, vfyopts); |
0f113f3e MC |
1421 | if (i < 0) { |
1422 | ok = 0; | |
1423 | BIO_printf(bio_err, "Signature verification problems....\n"); | |
7e1b7485 | 1424 | goto end; |
0f113f3e MC |
1425 | } |
1426 | if (i == 0) { | |
1427 | ok = 0; | |
1428 | BIO_printf(bio_err, "Signature did not match the certificate\n"); | |
7e1b7485 | 1429 | goto end; |
2234212c | 1430 | } else { |
0f113f3e | 1431 | BIO_printf(bio_err, "Signature ok\n"); |
2234212c | 1432 | } |
0f113f3e | 1433 | |
1061baf6 | 1434 | if ((rreq = X509_to_X509_REQ(template_cert, NULL, NULL)) == NULL) |
7e1b7485 | 1435 | goto end; |
0f113f3e MC |
1436 | |
1437 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
1438 | chtype, multirdn, email_dn, startdate, enddate, days, batch, | |
1439 | verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op, | |
8c5bff22 | 1440 | ext_copy, 0, dateopt); |
0f113f3e | 1441 | |
7e1b7485 | 1442 | end: |
222561fe | 1443 | X509_REQ_free(rreq); |
1061baf6 | 1444 | X509_free(template_cert); |
26a7d938 | 1445 | return ok; |
0f113f3e MC |
1446 | } |
1447 | ||
1448 | static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, | |
91034b68 | 1449 | const char *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
0f113f3e | 1450 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, |
cc696296 F |
1451 | const char *subj, unsigned long chtype, int multirdn, |
1452 | int email_dn, const char *startdate, const char *enddate, long days, | |
1453 | int batch, int verbose, X509_REQ *req, const char *ext_sect, | |
0f113f3e | 1454 | CONF *lconf, unsigned long certopt, unsigned long nameopt, |
8c5bff22 | 1455 | int default_op, int ext_copy, int selfsign, unsigned long dateopt) |
0f113f3e | 1456 | { |
8cc86b81 DDO |
1457 | const X509_NAME *name = NULL; |
1458 | X509_NAME *CAname = NULL, *subject = NULL; | |
568ce3a5 | 1459 | const ASN1_TIME *tm; |
0f113f3e MC |
1460 | ASN1_STRING *str, *str2; |
1461 | ASN1_OBJECT *obj; | |
1462 | X509 *ret = NULL; | |
07a38fd2 | 1463 | X509_NAME_ENTRY *ne, *tne; |
0f113f3e MC |
1464 | EVP_PKEY *pktmp; |
1465 | int ok = -1, i, j, last, nid; | |
1466 | const char *p; | |
1467 | CONF_VALUE *cv; | |
1468 | OPENSSL_STRING row[DB_NUMBER]; | |
1469 | OPENSSL_STRING *irow = NULL; | |
1470 | OPENSSL_STRING *rrow = NULL; | |
1471 | char buf[25]; | |
ec2bfb7d | 1472 | X509V3_CTX ext_ctx; |
0f113f3e | 1473 | |
0f113f3e MC |
1474 | for (i = 0; i < DB_NUMBER; i++) |
1475 | row[i] = NULL; | |
1476 | ||
1477 | if (subj) { | |
57c05c57 | 1478 | X509_NAME *n = parse_name(subj, chtype, multirdn, "subject"); |
0f113f3e | 1479 | |
9c73e48a | 1480 | if (!n) |
7e1b7485 | 1481 | goto end; |
0f113f3e | 1482 | X509_REQ_set_subject_name(req, n); |
0f113f3e MC |
1483 | X509_NAME_free(n); |
1484 | } | |
1485 | ||
1486 | if (default_op) | |
b5895815 | 1487 | BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n"); |
0f113f3e MC |
1488 | |
1489 | name = X509_REQ_get_subject_name(req); | |
1490 | for (i = 0; i < X509_NAME_entry_count(name); i++) { | |
1491 | ne = X509_NAME_get_entry(name, i); | |
1492 | str = X509_NAME_ENTRY_get_data(ne); | |
1493 | obj = X509_NAME_ENTRY_get_object(ne); | |
07a38fd2 | 1494 | nid = OBJ_obj2nid(obj); |
0f113f3e MC |
1495 | |
1496 | if (msie_hack) { | |
1497 | /* assume all type should be strings */ | |
0f113f3e MC |
1498 | |
1499 | if (str->type == V_ASN1_UNIVERSALSTRING) | |
1500 | ASN1_UNIVERSALSTRING_to_string(str); | |
1501 | ||
07a38fd2 | 1502 | if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress) |
0f113f3e MC |
1503 | str->type = V_ASN1_T61STRING; |
1504 | ||
07a38fd2 F |
1505 | if (nid == NID_pkcs9_emailAddress |
1506 | && str->type == V_ASN1_PRINTABLESTRING) | |
0f113f3e MC |
1507 | str->type = V_ASN1_IA5STRING; |
1508 | } | |
1509 | ||
1510 | /* If no EMAIL is wanted in the subject */ | |
07a38fd2 | 1511 | if (nid == NID_pkcs9_emailAddress && !email_dn) |
0f113f3e MC |
1512 | continue; |
1513 | ||
1514 | /* check some things */ | |
07a38fd2 | 1515 | if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) { |
0f113f3e MC |
1516 | BIO_printf(bio_err, |
1517 | "\nemailAddress type needs to be of type IA5STRING\n"); | |
7e1b7485 | 1518 | goto end; |
0f113f3e | 1519 | } |
07a38fd2 | 1520 | if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) { |
0f113f3e | 1521 | j = ASN1_PRINTABLE_type(str->data, str->length); |
07a38fd2 F |
1522 | if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) || |
1523 | (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING)) | |
1524 | { | |
0f113f3e MC |
1525 | BIO_printf(bio_err, |
1526 | "\nThe string contains characters that are illegal for the ASN.1 type\n"); | |
7e1b7485 | 1527 | goto end; |
0f113f3e MC |
1528 | } |
1529 | } | |
1530 | ||
1531 | if (default_op) | |
ecf3a1fb | 1532 | old_entry_print(obj, str); |
0f113f3e MC |
1533 | } |
1534 | ||
1535 | /* Ok, now we check the 'policy' stuff. */ | |
1536 | if ((subject = X509_NAME_new()) == NULL) { | |
1537 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1538 | goto end; |
0f113f3e MC |
1539 | } |
1540 | ||
1541 | /* take a copy of the issuer name before we mess with it. */ | |
1542 | if (selfsign) | |
1543 | CAname = X509_NAME_dup(name); | |
1544 | else | |
a8d8e06b | 1545 | CAname = X509_NAME_dup(X509_get_subject_name(x509)); |
0f113f3e | 1546 | if (CAname == NULL) |
7e1b7485 | 1547 | goto end; |
0f113f3e MC |
1548 | str = str2 = NULL; |
1549 | ||
1550 | for (i = 0; i < sk_CONF_VALUE_num(policy); i++) { | |
1551 | cv = sk_CONF_VALUE_value(policy, i); /* get the object id */ | |
1552 | if ((j = OBJ_txt2nid(cv->name)) == NID_undef) { | |
1553 | BIO_printf(bio_err, | |
1554 | "%s:unknown object type in 'policy' configuration\n", | |
1555 | cv->name); | |
7e1b7485 | 1556 | goto end; |
0f113f3e MC |
1557 | } |
1558 | obj = OBJ_nid2obj(j); | |
1559 | ||
1560 | last = -1; | |
1561 | for (;;) { | |
07a38fd2 F |
1562 | X509_NAME_ENTRY *push = NULL; |
1563 | ||
0f113f3e MC |
1564 | /* lookup the object in the supplied name list */ |
1565 | j = X509_NAME_get_index_by_OBJ(name, obj, last); | |
1566 | if (j < 0) { | |
1567 | if (last != -1) | |
1568 | break; | |
1569 | tne = NULL; | |
1570 | } else { | |
1571 | tne = X509_NAME_get_entry(name, j); | |
1572 | } | |
1573 | last = j; | |
1574 | ||
1575 | /* depending on the 'policy', decide what to do. */ | |
0f113f3e MC |
1576 | if (strcmp(cv->value, "optional") == 0) { |
1577 | if (tne != NULL) | |
1578 | push = tne; | |
1579 | } else if (strcmp(cv->value, "supplied") == 0) { | |
1580 | if (tne == NULL) { | |
1581 | BIO_printf(bio_err, | |
1582 | "The %s field needed to be supplied and was missing\n", | |
1583 | cv->name); | |
7e1b7485 | 1584 | goto end; |
2234212c | 1585 | } else { |
0f113f3e | 1586 | push = tne; |
2234212c | 1587 | } |
0f113f3e MC |
1588 | } else if (strcmp(cv->value, "match") == 0) { |
1589 | int last2; | |
1590 | ||
1591 | if (tne == NULL) { | |
1592 | BIO_printf(bio_err, | |
1593 | "The mandatory %s field was missing\n", | |
1594 | cv->name); | |
7e1b7485 | 1595 | goto end; |
0f113f3e MC |
1596 | } |
1597 | ||
1598 | last2 = -1; | |
1599 | ||
1600 | again2: | |
1601 | j = X509_NAME_get_index_by_OBJ(CAname, obj, last2); | |
1602 | if ((j < 0) && (last2 == -1)) { | |
1603 | BIO_printf(bio_err, | |
fefa4d55 | 1604 | "The %s field does not exist in the CA certificate,\n" |
b5895815 | 1605 | "the 'policy' is misconfigured\n", cv->name); |
7e1b7485 | 1606 | goto end; |
0f113f3e MC |
1607 | } |
1608 | if (j >= 0) { | |
1609 | push = X509_NAME_get_entry(CAname, j); | |
1610 | str = X509_NAME_ENTRY_get_data(tne); | |
1611 | str2 = X509_NAME_ENTRY_get_data(push); | |
1612 | last2 = j; | |
1613 | if (ASN1_STRING_cmp(str, str2) != 0) | |
1614 | goto again2; | |
1615 | } | |
1616 | if (j < 0) { | |
1617 | BIO_printf(bio_err, | |
fefa4d55 F |
1618 | "The %s field is different between\n" |
1619 | "CA certificate (%s) and the request (%s)\n", | |
0f113f3e MC |
1620 | cv->name, |
1621 | ((str2 == NULL) ? "NULL" : (char *)str2->data), | |
1622 | ((str == NULL) ? "NULL" : (char *)str->data)); | |
7e1b7485 | 1623 | goto end; |
0f113f3e MC |
1624 | } |
1625 | } else { | |
1626 | BIO_printf(bio_err, | |
1627 | "%s:invalid type in 'policy' configuration\n", | |
1628 | cv->value); | |
7e1b7485 | 1629 | goto end; |
0f113f3e MC |
1630 | } |
1631 | ||
1632 | if (push != NULL) { | |
1633 | if (!X509_NAME_add_entry(subject, push, -1, 0)) { | |
0f113f3e | 1634 | BIO_printf(bio_err, "Memory allocation failure\n"); |
7e1b7485 | 1635 | goto end; |
0f113f3e MC |
1636 | } |
1637 | } | |
1638 | if (j < 0) | |
1639 | break; | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | if (preserve) { | |
1644 | X509_NAME_free(subject); | |
1645 | /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */ | |
1646 | subject = X509_NAME_dup(name); | |
1647 | if (subject == NULL) | |
7e1b7485 | 1648 | goto end; |
0f113f3e MC |
1649 | } |
1650 | ||
0f113f3e MC |
1651 | /* We are now totally happy, lets make and sign the certificate */ |
1652 | if (verbose) | |
1653 | BIO_printf(bio_err, | |
1654 | "Everything appears to be ok, creating and signing the certificate\n"); | |
1655 | ||
d8652be0 | 1656 | if ((ret = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL) |
7e1b7485 | 1657 | goto end; |
d02b48c6 | 1658 | |
22293ea1 | 1659 | if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL) |
7e1b7485 | 1660 | goto end; |
0f113f3e MC |
1661 | if (selfsign) { |
1662 | if (!X509_set_issuer_name(ret, subject)) | |
7e1b7485 | 1663 | goto end; |
0f113f3e MC |
1664 | } else { |
1665 | if (!X509_set_issuer_name(ret, X509_get_subject_name(x509))) | |
7e1b7485 | 1666 | goto end; |
0f113f3e MC |
1667 | } |
1668 | ||
dc047d31 DSH |
1669 | if (!set_cert_times(ret, startdate, enddate, days)) |
1670 | goto end; | |
0f113f3e | 1671 | |
dc047d31 | 1672 | if (enddate != NULL) { |
0f113f3e | 1673 | int tdays; |
0d502c35 MC |
1674 | |
1675 | if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret))) | |
1676 | goto end; | |
0f113f3e MC |
1677 | days = tdays; |
1678 | } | |
1679 | ||
1680 | if (!X509_set_subject_name(ret, subject)) | |
7e1b7485 | 1681 | goto end; |
0f113f3e | 1682 | |
6e863f07 | 1683 | pktmp = X509_REQ_get0_pubkey(req); |
0f113f3e | 1684 | i = X509_set_pubkey(ret, pktmp); |
0f113f3e | 1685 | if (!i) |
7e1b7485 | 1686 | goto end; |
0f113f3e | 1687 | |
d44a8a16 DDO |
1688 | /* Initialize the context structure */ |
1689 | X509V3_set_ctx(&ext_ctx, selfsign ? ret : x509, | |
adbd77f6 DDO |
1690 | ret, NULL /* no need to give req, needed info is in ret */, |
1691 | NULL, X509V3_CTX_REPLACE); | |
1692 | /* prepare fallback for AKID, but only if issuer cert equals subject cert */ | |
1693 | if (selfsign) { | |
1694 | if (!X509V3_set_issuer_pkey(&ext_ctx, pkey)) | |
1695 | goto end; | |
1696 | if (!cert_matches_key(ret, pkey)) | |
1697 | BIO_printf(bio_err, | |
1698 | "Warning: Signature key and public key of cert do not match\n"); | |
1699 | } | |
d44a8a16 | 1700 | |
0f113f3e MC |
1701 | /* Lets add the extensions, if there are any */ |
1702 | if (ext_sect) { | |
1a683b80 | 1703 | if (extfile_conf != NULL) { |
0f113f3e MC |
1704 | if (verbose) |
1705 | BIO_printf(bio_err, "Extra configuration file found\n"); | |
1706 | ||
1a683b80 DDO |
1707 | /* Use the extfile_conf configuration db LHASH */ |
1708 | X509V3_set_nconf(&ext_ctx, extfile_conf); | |
0f113f3e MC |
1709 | |
1710 | /* Adds exts contained in the configuration file */ | |
1a683b80 | 1711 | if (!X509V3_EXT_add_nconf(extfile_conf, &ext_ctx, ext_sect, ret)) { |
0f113f3e | 1712 | BIO_printf(bio_err, |
1a683b80 | 1713 | "Error adding certificate extensions from extfile section %s\n", |
0f113f3e | 1714 | ext_sect); |
7e1b7485 | 1715 | goto end; |
0f113f3e MC |
1716 | } |
1717 | if (verbose) | |
1718 | BIO_printf(bio_err, | |
1719 | "Successfully added extensions from file.\n"); | |
1720 | } else if (ext_sect) { | |
1721 | /* We found extensions to be set from config file */ | |
1a683b80 | 1722 | X509V3_set_nconf(&ext_ctx, lconf); |
0f113f3e | 1723 | |
1a683b80 | 1724 | if (!X509V3_EXT_add_nconf(lconf, &ext_ctx, ext_sect, ret)) { |
0f113f3e | 1725 | BIO_printf(bio_err, |
1a683b80 | 1726 | "Error adding certificate extensions from config section %s\n", |
0f113f3e | 1727 | ext_sect); |
7e1b7485 | 1728 | goto end; |
0f113f3e MC |
1729 | } |
1730 | ||
1731 | if (verbose) | |
1732 | BIO_printf(bio_err, | |
1733 | "Successfully added extensions from config\n"); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | /* Copy extensions from request (if any) */ | |
1738 | ||
1739 | if (!copy_extensions(ret, req, ext_copy)) { | |
1740 | BIO_printf(bio_err, "ERROR: adding extensions from request\n"); | |
7e1b7485 | 1741 | goto end; |
0f113f3e MC |
1742 | } |
1743 | ||
2cedf794 MC |
1744 | if (verbose) |
1745 | BIO_printf(bio_err, | |
1746 | "The subject name appears to be ok, checking data base for clashes\n"); | |
1747 | ||
1748 | /* Build the correct Subject if no e-mail is wanted in the subject. */ | |
1749 | if (!email_dn) { | |
1750 | X509_NAME_ENTRY *tmpne; | |
1751 | X509_NAME *dn_subject; | |
1752 | ||
1753 | /* | |
1754 | * Its best to dup the subject DN and then delete any email addresses | |
1755 | * because this retains its structure. | |
1756 | */ | |
1757 | if ((dn_subject = X509_NAME_dup(subject)) == NULL) { | |
1758 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1759 | goto end; | |
1760 | } | |
991f0355 | 1761 | i = -1; |
2cedf794 MC |
1762 | while ((i = X509_NAME_get_index_by_NID(dn_subject, |
1763 | NID_pkcs9_emailAddress, | |
991f0355 F |
1764 | i)) >= 0) { |
1765 | tmpne = X509_NAME_delete_entry(dn_subject, i--); | |
2cedf794 MC |
1766 | X509_NAME_ENTRY_free(tmpne); |
1767 | } | |
1768 | ||
1769 | if (!X509_set_subject_name(ret, dn_subject)) { | |
1770 | X509_NAME_free(dn_subject); | |
7e1b7485 | 1771 | goto end; |
2cedf794 MC |
1772 | } |
1773 | X509_NAME_free(dn_subject); | |
1774 | } | |
1775 | ||
1776 | row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0); | |
1777 | if (row[DB_name] == NULL) { | |
1778 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1779 | goto end; | |
1780 | } | |
1781 | ||
1782 | if (BN_is_zero(serial)) | |
1783 | row[DB_serial] = OPENSSL_strdup("00"); | |
1784 | else | |
1785 | row[DB_serial] = BN_bn2hex(serial); | |
1786 | if (row[DB_serial] == NULL) { | |
1787 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1788 | goto end; | |
1789 | } | |
1790 | ||
5af88441 MC |
1791 | if (row[DB_name][0] == '\0') { |
1792 | /* | |
1793 | * An empty subject! We'll use the serial number instead. If | |
1794 | * unique_subject is in use then we don't want different entries with | |
1795 | * empty subjects matching each other. | |
1796 | */ | |
1797 | OPENSSL_free(row[DB_name]); | |
1798 | row[DB_name] = OPENSSL_strdup(row[DB_serial]); | |
1799 | if (row[DB_name] == NULL) { | |
1800 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1801 | goto end; | |
1802 | } | |
1803 | } | |
1804 | ||
2cedf794 MC |
1805 | if (db->attributes.unique_subject) { |
1806 | OPENSSL_STRING *crow = row; | |
1807 | ||
1808 | rrow = TXT_DB_get_by_index(db->db, DB_name, crow); | |
1809 | if (rrow != NULL) { | |
1810 | BIO_printf(bio_err, | |
1811 | "ERROR:There is already a certificate for %s\n", | |
1812 | row[DB_name]); | |
1813 | } | |
1814 | } | |
1815 | if (rrow == NULL) { | |
1816 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
1817 | if (rrow != NULL) { | |
1818 | BIO_printf(bio_err, | |
1819 | "ERROR:Serial number %s has already been issued,\n", | |
1820 | row[DB_serial]); | |
1821 | BIO_printf(bio_err, | |
1822 | " check the database/serial_file for corruption\n"); | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | if (rrow != NULL) { | |
1827 | BIO_printf(bio_err, "The matching entry has the following details\n"); | |
1828 | if (rrow[DB_type][0] == DB_TYPE_EXP) | |
1829 | p = "Expired"; | |
1830 | else if (rrow[DB_type][0] == DB_TYPE_REV) | |
1831 | p = "Revoked"; | |
1832 | else if (rrow[DB_type][0] == DB_TYPE_VAL) | |
1833 | p = "Valid"; | |
1834 | else | |
1835 | p = "\ninvalid type, Data base error\n"; | |
e52698f9 | 1836 | BIO_printf(bio_err, "Type :%s\n", p); |
2cedf794 MC |
1837 | if (rrow[DB_type][0] == DB_TYPE_REV) { |
1838 | p = rrow[DB_exp_date]; | |
1839 | if (p == NULL) | |
1840 | p = "undef"; | |
1841 | BIO_printf(bio_err, "Was revoked on:%s\n", p); | |
1842 | } | |
1843 | p = rrow[DB_exp_date]; | |
1844 | if (p == NULL) | |
1845 | p = "undef"; | |
1846 | BIO_printf(bio_err, "Expires on :%s\n", p); | |
1847 | p = rrow[DB_serial]; | |
1848 | if (p == NULL) | |
1849 | p = "undef"; | |
1850 | BIO_printf(bio_err, "Serial Number :%s\n", p); | |
1851 | p = rrow[DB_file]; | |
1852 | if (p == NULL) | |
1853 | p = "undef"; | |
1854 | BIO_printf(bio_err, "File name :%s\n", p); | |
1855 | p = rrow[DB_name]; | |
1856 | if (p == NULL) | |
1857 | p = "undef"; | |
1858 | BIO_printf(bio_err, "Subject Name :%s\n", p); | |
1859 | ok = -1; /* This is now a 'bad' error. */ | |
1860 | goto end; | |
0f113f3e MC |
1861 | } |
1862 | ||
1863 | if (!default_op) { | |
1864 | BIO_printf(bio_err, "Certificate Details:\n"); | |
1865 | /* | |
1866 | * Never print signature details because signature not present | |
1867 | */ | |
1868 | certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME; | |
1869 | X509_print_ex(bio_err, ret, nameopt, certopt); | |
1870 | } | |
1871 | ||
1872 | BIO_printf(bio_err, "Certificate is to be certified until "); | |
8c5bff22 | 1873 | ASN1_TIME_print_ex(bio_err, X509_get0_notAfter(ret), dateopt); |
0f113f3e MC |
1874 | if (days) |
1875 | BIO_printf(bio_err, " (%ld days)", days); | |
1876 | BIO_printf(bio_err, "\n"); | |
1877 | ||
1878 | if (!batch) { | |
1879 | ||
1880 | BIO_printf(bio_err, "Sign the certificate? [y/n]:"); | |
1881 | (void)BIO_flush(bio_err); | |
1882 | buf[0] = '\0'; | |
3a19b22a | 1883 | if (fgets(buf, sizeof(buf), stdin) == NULL) { |
0f113f3e MC |
1884 | BIO_printf(bio_err, |
1885 | "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n"); | |
1886 | ok = 0; | |
7e1b7485 | 1887 | goto end; |
0f113f3e | 1888 | } |
3a19b22a | 1889 | if (!(buf[0] == 'y' || buf[0] == 'Y')) { |
0f113f3e MC |
1890 | BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n"); |
1891 | ok = 0; | |
7e1b7485 | 1892 | goto end; |
0f113f3e MC |
1893 | } |
1894 | } | |
1895 | ||
c01ff880 | 1896 | pktmp = X509_get0_pubkey(ret); |
0f113f3e MC |
1897 | if (EVP_PKEY_missing_parameters(pktmp) && |
1898 | !EVP_PKEY_missing_parameters(pkey)) | |
1899 | EVP_PKEY_copy_parameters(pktmp, pkey); | |
0f113f3e | 1900 | |
342e3652 | 1901 | if (!do_X509_sign(ret, 0, pkey, dgst, sigopts, &ext_ctx)) |
7e1b7485 | 1902 | goto end; |
0f113f3e | 1903 | |
06a79af2 | 1904 | /* We now just add it to the database as DB_TYPE_VAL('V') */ |
7644a9ae | 1905 | row[DB_type] = OPENSSL_strdup("V"); |
568ce3a5 | 1906 | tm = X509_get0_notAfter(ret); |
68dc6824 | 1907 | row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate"); |
0f113f3e MC |
1908 | memcpy(row[DB_exp_date], tm->data, tm->length); |
1909 | row[DB_exp_date][tm->length] = '\0'; | |
0f113f3e | 1910 | row[DB_rev_date] = NULL; |
7644a9ae | 1911 | row[DB_file] = OPENSSL_strdup("unknown"); |
6a13c9c9 | 1912 | if ((row[DB_type] == NULL) || (row[DB_file] == NULL) |
1913 | || (row[DB_name] == NULL)) { | |
0f113f3e | 1914 | BIO_printf(bio_err, "Memory allocation failure\n"); |
7e1b7485 | 1915 | goto end; |
0f113f3e | 1916 | } |
0f113f3e | 1917 | |
b4faea50 | 1918 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space"); |
9ad52c56 | 1919 | for (i = 0; i < DB_NUMBER; i++) |
0f113f3e | 1920 | irow[i] = row[i]; |
0f113f3e MC |
1921 | irow[DB_NUMBER] = NULL; |
1922 | ||
1923 | if (!TXT_DB_insert(db->db, irow)) { | |
1924 | BIO_printf(bio_err, "failed to update database\n"); | |
1925 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
7e1b7485 | 1926 | goto end; |
0f113f3e | 1927 | } |
9ad52c56 | 1928 | irow = NULL; |
0f113f3e | 1929 | ok = 1; |
7e1b7485 | 1930 | end: |
01985122 | 1931 | if (ok != 1) { |
9ad52c56 BE |
1932 | for (i = 0; i < DB_NUMBER; i++) |
1933 | OPENSSL_free(row[i]); | |
9ad52c56 | 1934 | } |
01985122 | 1935 | OPENSSL_free(irow); |
0f113f3e | 1936 | |
222561fe RS |
1937 | X509_NAME_free(CAname); |
1938 | X509_NAME_free(subject); | |
222561fe RS |
1939 | if (ok <= 0) |
1940 | X509_free(ret); | |
1941 | else | |
0f113f3e | 1942 | *xret = ret; |
26a7d938 | 1943 | return ok; |
0f113f3e MC |
1944 | } |
1945 | ||
b5895815 | 1946 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext) |
0f113f3e MC |
1947 | { |
1948 | ||
1949 | if (output_der) { | |
1950 | (void)i2d_X509_bio(bp, x); | |
1951 | return; | |
1952 | } | |
0f113f3e MC |
1953 | if (!notext) |
1954 | X509_print(bp, x); | |
1955 | PEM_write_bio_X509(bp, x); | |
1956 | } | |
1957 | ||
cc696296 | 1958 | static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey, |
91034b68 | 1959 | X509 *x509, const char *dgst, |
0f113f3e MC |
1960 | STACK_OF(OPENSSL_STRING) *sigopts, |
1961 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
1962 | BIGNUM *serial, const char *subj, unsigned long chtype, |
1963 | int multirdn, int email_dn, const char *startdate, | |
1964 | const char *enddate, long days, const char *ext_sect, | |
0f113f3e | 1965 | CONF *lconf, int verbose, unsigned long certopt, |
8c5bff22 | 1966 | unsigned long nameopt, int default_op, int ext_copy, unsigned long dateopt) |
0f113f3e MC |
1967 | { |
1968 | STACK_OF(CONF_VALUE) *sk = NULL; | |
1969 | LHASH_OF(CONF_VALUE) *parms = NULL; | |
1970 | X509_REQ *req = NULL; | |
1971 | CONF_VALUE *cv = NULL; | |
1972 | NETSCAPE_SPKI *spki = NULL; | |
0f113f3e MC |
1973 | char *type, *buf; |
1974 | EVP_PKEY *pktmp = NULL; | |
1975 | X509_NAME *n = NULL; | |
1976 | X509_NAME_ENTRY *ne = NULL; | |
1977 | int ok = -1, i, j; | |
1978 | long errline; | |
1979 | int nid; | |
1980 | ||
1981 | /* | |
1982 | * Load input file into a hash table. (This is just an easy | |
1983 | * way to read and parse the file, then put it into a convenient | |
1984 | * STACK format). | |
1985 | */ | |
1986 | parms = CONF_load(NULL, infile, &errline); | |
1987 | if (parms == NULL) { | |
1988 | BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile); | |
7e1b7485 | 1989 | goto end; |
0f113f3e MC |
1990 | } |
1991 | ||
1992 | sk = CONF_get_section(parms, "default"); | |
1993 | if (sk_CONF_VALUE_num(sk) == 0) { | |
1994 | BIO_printf(bio_err, "no name/value pairs found in %s\n", infile); | |
7e1b7485 | 1995 | goto end; |
0f113f3e MC |
1996 | } |
1997 | ||
1998 | /* | |
1999 | * Now create a dummy X509 request structure. We don't actually | |
2000 | * have an X509 request, but we have many of the components | |
2001 | * (a public key, various DN components). The idea is that we | |
2002 | * put these components into the right X509 request structure | |
2003 | * and we can use the same code as if you had a real X509 request. | |
2004 | */ | |
2005 | req = X509_REQ_new(); | |
9c73e48a | 2006 | if (req == NULL) |
7e1b7485 | 2007 | goto end; |
0f113f3e MC |
2008 | |
2009 | /* | |
2010 | * Build up the subject name set. | |
2011 | */ | |
124055a9 | 2012 | n = X509_REQ_get_subject_name(req); |
0f113f3e MC |
2013 | |
2014 | for (i = 0;; i++) { | |
2015 | if (sk_CONF_VALUE_num(sk) <= i) | |
2016 | break; | |
2017 | ||
2018 | cv = sk_CONF_VALUE_value(sk, i); | |
2019 | type = cv->name; | |
2020 | /* | |
2021 | * Skip past any leading X. X: X, etc to allow for multiple instances | |
2022 | */ | |
2023 | for (buf = cv->name; *buf; buf++) | |
2024 | if ((*buf == ':') || (*buf == ',') || (*buf == '.')) { | |
2025 | buf++; | |
2026 | if (*buf) | |
2027 | type = buf; | |
2028 | break; | |
2029 | } | |
2030 | ||
2031 | buf = cv->value; | |
2032 | if ((nid = OBJ_txt2nid(type)) == NID_undef) { | |
2033 | if (strcmp(type, "SPKAC") == 0) { | |
2034 | spki = NETSCAPE_SPKI_b64_decode(cv->value, -1); | |
2035 | if (spki == NULL) { | |
2036 | BIO_printf(bio_err, | |
2037 | "unable to load Netscape SPKAC structure\n"); | |
7e1b7485 | 2038 | goto end; |
0f113f3e MC |
2039 | } |
2040 | } | |
2041 | continue; | |
2042 | } | |
2043 | ||
2044 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, | |
2045 | (unsigned char *)buf, -1, -1, 0)) | |
7e1b7485 | 2046 | goto end; |
0f113f3e MC |
2047 | } |
2048 | if (spki == NULL) { | |
2049 | BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n", | |
2050 | infile); | |
7e1b7485 | 2051 | goto end; |
0f113f3e MC |
2052 | } |
2053 | ||
2054 | /* | |
2055 | * Now extract the key from the SPKI structure. | |
2056 | */ | |
2057 | ||
b5895815 | 2058 | BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n"); |
0f113f3e MC |
2059 | |
2060 | if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) { | |
2061 | BIO_printf(bio_err, "error unpacking SPKAC public key\n"); | |
7e1b7485 | 2062 | goto end; |
0f113f3e MC |
2063 | } |
2064 | ||
2065 | j = NETSCAPE_SPKI_verify(spki, pktmp); | |
2066 | if (j <= 0) { | |
f6c006ea | 2067 | EVP_PKEY_free(pktmp); |
0f113f3e MC |
2068 | BIO_printf(bio_err, |
2069 | "signature verification failed on SPKAC public key\n"); | |
7e1b7485 | 2070 | goto end; |
0f113f3e MC |
2071 | } |
2072 | BIO_printf(bio_err, "Signature ok\n"); | |
2073 | ||
2074 | X509_REQ_set_pubkey(req, pktmp); | |
2075 | EVP_PKEY_free(pktmp); | |
2076 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
2077 | chtype, multirdn, email_dn, startdate, enddate, days, 1, | |
2078 | verbose, req, ext_sect, lconf, certopt, nameopt, default_op, | |
8c5bff22 | 2079 | ext_copy, 0, dateopt); |
7e1b7485 | 2080 | end: |
222561fe | 2081 | X509_REQ_free(req); |
25aaa98a RS |
2082 | CONF_free(parms); |
2083 | NETSCAPE_SPKI_free(spki); | |
222561fe | 2084 | X509_NAME_ENTRY_free(ne); |
0f113f3e | 2085 | |
26a7d938 | 2086 | return ok; |
0f113f3e | 2087 | } |
d02b48c6 | 2088 | |
7c337e00 | 2089 | static int check_time_format(const char *str) |
0f113f3e MC |
2090 | { |
2091 | return ASN1_TIME_set_string(NULL, str); | |
2092 | } | |
d02b48c6 | 2093 | |
06a79af2 F |
2094 | static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, |
2095 | const char *value) | |
0f113f3e | 2096 | { |
568ce3a5 | 2097 | const ASN1_TIME *tm = NULL; |
0f113f3e MC |
2098 | char *row[DB_NUMBER], **rrow, **irow; |
2099 | char *rev_str = NULL; | |
2100 | BIGNUM *bn = NULL; | |
2101 | int ok = -1, i; | |
2102 | ||
2103 | for (i = 0; i < DB_NUMBER; i++) | |
2104 | row[i] = NULL; | |
2105 | row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0); | |
1337a3a9 | 2106 | bn = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x509), NULL); |
0f113f3e | 2107 | if (!bn) |
7e1b7485 | 2108 | goto end; |
0f113f3e | 2109 | if (BN_is_zero(bn)) |
7644a9ae | 2110 | row[DB_serial] = OPENSSL_strdup("00"); |
0f113f3e MC |
2111 | else |
2112 | row[DB_serial] = BN_bn2hex(bn); | |
2113 | BN_free(bn); | |
5af88441 MC |
2114 | if (row[DB_name] != NULL && row[DB_name][0] == '\0') { |
2115 | /* Entries with empty Subjects actually use the serial number instead */ | |
2116 | OPENSSL_free(row[DB_name]); | |
2117 | row[DB_name] = OPENSSL_strdup(row[DB_serial]); | |
2118 | } | |
0f113f3e MC |
2119 | if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { |
2120 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2121 | goto end; |
0f113f3e MC |
2122 | } |
2123 | /* | |
2124 | * We have to lookup by serial number because name lookup skips revoked | |
2125 | * certs | |
2126 | */ | |
2127 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2128 | if (rrow == NULL) { | |
2129 | BIO_printf(bio_err, | |
2130 | "Adding Entry with serial number %s to DB for %s\n", | |
2131 | row[DB_serial], row[DB_name]); | |
2132 | ||
06a79af2 | 2133 | /* We now just add it to the database as DB_TYPE_REV('V') */ |
7644a9ae | 2134 | row[DB_type] = OPENSSL_strdup("V"); |
568ce3a5 | 2135 | tm = X509_get0_notAfter(x509); |
68dc6824 | 2136 | row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data"); |
0f113f3e MC |
2137 | memcpy(row[DB_exp_date], tm->data, tm->length); |
2138 | row[DB_exp_date][tm->length] = '\0'; | |
0f113f3e | 2139 | row[DB_rev_date] = NULL; |
7644a9ae | 2140 | row[DB_file] = OPENSSL_strdup("unknown"); |
0f113f3e | 2141 | |
0fbaef9e BE |
2142 | if (row[DB_type] == NULL || row[DB_file] == NULL) { |
2143 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
2144 | goto end; | |
2145 | } | |
2146 | ||
b4faea50 | 2147 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr"); |
0fbaef9e | 2148 | for (i = 0; i < DB_NUMBER; i++) |
0f113f3e | 2149 | irow[i] = row[i]; |
0f113f3e MC |
2150 | irow[DB_NUMBER] = NULL; |
2151 | ||
2152 | if (!TXT_DB_insert(db->db, irow)) { | |
2153 | BIO_printf(bio_err, "failed to update database\n"); | |
2154 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
0fbaef9e | 2155 | OPENSSL_free(irow); |
7e1b7485 | 2156 | goto end; |
0f113f3e MC |
2157 | } |
2158 | ||
0fbaef9e BE |
2159 | for (i = 0; i < DB_NUMBER; i++) |
2160 | row[i] = NULL; | |
2161 | ||
0f113f3e | 2162 | /* Revoke Certificate */ |
06a79af2 | 2163 | if (rev_type == REV_VALID) |
0f113f3e MC |
2164 | ok = 1; |
2165 | else | |
06a79af2 F |
2166 | /* Retry revocation after DB insertion */ |
2167 | ok = do_revoke(x509, db, rev_type, value); | |
0f113f3e | 2168 | |
7e1b7485 | 2169 | goto end; |
0f113f3e MC |
2170 | |
2171 | } else if (index_name_cmp_noconst(row, rrow)) { | |
2172 | BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]); | |
7e1b7485 | 2173 | goto end; |
06a79af2 | 2174 | } else if (rev_type == REV_VALID) { |
0f113f3e MC |
2175 | BIO_printf(bio_err, "ERROR:Already present, serial number %s\n", |
2176 | row[DB_serial]); | |
7e1b7485 | 2177 | goto end; |
06a79af2 | 2178 | } else if (rrow[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
2179 | BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n", |
2180 | row[DB_serial]); | |
7e1b7485 | 2181 | goto end; |
0f113f3e MC |
2182 | } else { |
2183 | BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]); | |
06a79af2 | 2184 | rev_str = make_revocation_str(rev_type, value); |
0f113f3e MC |
2185 | if (!rev_str) { |
2186 | BIO_printf(bio_err, "Error in revocation arguments\n"); | |
7e1b7485 | 2187 | goto end; |
0f113f3e | 2188 | } |
06a79af2 | 2189 | rrow[DB_type][0] = DB_TYPE_REV; |
0f113f3e MC |
2190 | rrow[DB_type][1] = '\0'; |
2191 | rrow[DB_rev_date] = rev_str; | |
2192 | } | |
2193 | ok = 1; | |
7e1b7485 | 2194 | end: |
0fbaef9e | 2195 | for (i = 0; i < DB_NUMBER; i++) |
b548a1f1 | 2196 | OPENSSL_free(row[i]); |
26a7d938 | 2197 | return ok; |
0f113f3e | 2198 | } |
c67cdb50 | 2199 | |
f85b68cd | 2200 | static int get_certificate_status(const char *serial, CA_DB *db) |
0f113f3e MC |
2201 | { |
2202 | char *row[DB_NUMBER], **rrow; | |
2203 | int ok = -1, i; | |
f6c460e8 | 2204 | size_t serial_len = strlen(serial); |
0f113f3e MC |
2205 | |
2206 | /* Free Resources */ | |
2207 | for (i = 0; i < DB_NUMBER; i++) | |
2208 | row[i] = NULL; | |
2209 | ||
2210 | /* Malloc needed char spaces */ | |
f6c460e8 | 2211 | row[DB_serial] = app_malloc(serial_len + 2, "row serial#"); |
0f113f3e | 2212 | |
f6c460e8 | 2213 | if (serial_len % 2) { |
0f113f3e MC |
2214 | /* |
2215 | * Set the first char to 0 | |
9fa36f5f | 2216 | */ |
0f113f3e MC |
2217 | row[DB_serial][0] = '0'; |
2218 | ||
2219 | /* Copy String from serial to row[DB_serial] */ | |
f6c460e8 F |
2220 | memcpy(row[DB_serial] + 1, serial, serial_len); |
2221 | row[DB_serial][serial_len + 1] = '\0'; | |
0f113f3e MC |
2222 | } else { |
2223 | /* Copy String from serial to row[DB_serial] */ | |
f6c460e8 F |
2224 | memcpy(row[DB_serial], serial, serial_len); |
2225 | row[DB_serial][serial_len] = '\0'; | |
0f113f3e MC |
2226 | } |
2227 | ||
2228 | /* Make it Upper Case */ | |
20967afb | 2229 | make_uppercase(row[DB_serial]); |
0f113f3e MC |
2230 | |
2231 | ok = 1; | |
2232 | ||
2233 | /* Search for the certificate */ | |
2234 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2235 | if (rrow == NULL) { | |
2236 | BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]); | |
2237 | ok = -1; | |
7e1b7485 | 2238 | goto end; |
06a79af2 | 2239 | } else if (rrow[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e MC |
2240 | BIO_printf(bio_err, "%s=Valid (%c)\n", |
2241 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2242 | goto end; |
06a79af2 | 2243 | } else if (rrow[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
2244 | BIO_printf(bio_err, "%s=Revoked (%c)\n", |
2245 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2246 | goto end; |
06a79af2 | 2247 | } else if (rrow[DB_type][0] == DB_TYPE_EXP) { |
0f113f3e MC |
2248 | BIO_printf(bio_err, "%s=Expired (%c)\n", |
2249 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2250 | goto end; |
06a79af2 | 2251 | } else if (rrow[DB_type][0] == DB_TYPE_SUSP) { |
0f113f3e MC |
2252 | BIO_printf(bio_err, "%s=Suspended (%c)\n", |
2253 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2254 | goto end; |
0f113f3e MC |
2255 | } else { |
2256 | BIO_printf(bio_err, "%s=Unknown (%c).\n", | |
2257 | row[DB_serial], rrow[DB_type][0]); | |
2258 | ok = -1; | |
2259 | } | |
7e1b7485 | 2260 | end: |
0f113f3e | 2261 | for (i = 0; i < DB_NUMBER; i++) { |
b548a1f1 | 2262 | OPENSSL_free(row[i]); |
0f113f3e | 2263 | } |
26a7d938 | 2264 | return ok; |
0f113f3e MC |
2265 | } |
2266 | ||
065121ff | 2267 | int do_updatedb(CA_DB *db, time_t *now) |
0f113f3e | 2268 | { |
963a65bf | 2269 | ASN1_TIME *a_tm = NULL; |
0f113f3e | 2270 | int i, cnt = 0; |
963a65bf | 2271 | char **rrow; |
0f113f3e | 2272 | |
963a65bf | 2273 | a_tm = ASN1_TIME_new(); |
96487cdd MC |
2274 | if (a_tm == NULL) |
2275 | return -1; | |
0f113f3e | 2276 | |
963a65bf | 2277 | /* get actual time */ |
065121ff | 2278 | if (X509_time_adj(a_tm, 0, now) == NULL) { |
963a65bf | 2279 | ASN1_TIME_free(a_tm); |
bc2a0dd2 MC |
2280 | return -1; |
2281 | } | |
0f113f3e MC |
2282 | |
2283 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
2284 | rrow = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
2285 | ||
06a79af2 | 2286 | if (rrow[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e | 2287 | /* ignore entries that are not valid */ |
963a65bf AF |
2288 | ASN1_TIME *exp_date = NULL; |
2289 | ||
2290 | exp_date = ASN1_TIME_new(); | |
2291 | if (exp_date == NULL) { | |
2292 | ASN1_TIME_free(a_tm); | |
2293 | return -1; | |
2294 | } | |
2295 | ||
2296 | if (!ASN1_TIME_set_string(exp_date, rrow[DB_exp_date])) { | |
2297 | ASN1_TIME_free(a_tm); | |
2298 | ASN1_TIME_free(exp_date); | |
2299 | return -1; | |
2300 | } | |
2301 | ||
2302 | if (ASN1_TIME_compare(exp_date, a_tm) <= 0) { | |
06a79af2 | 2303 | rrow[DB_type][0] = DB_TYPE_EXP; |
0f113f3e MC |
2304 | rrow[DB_type][1] = '\0'; |
2305 | cnt++; | |
2306 | ||
2307 | BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]); | |
2308 | } | |
963a65bf | 2309 | ASN1_TIME_free(exp_date); |
0f113f3e MC |
2310 | } |
2311 | } | |
2312 | ||
963a65bf | 2313 | ASN1_TIME_free(a_tm); |
26a7d938 | 2314 | return cnt; |
0f113f3e | 2315 | } |
a6b7ffdd | 2316 | |
7d727231 | 2317 | static const char *crl_reasons[] = { |
0f113f3e MC |
2318 | /* CRL reason strings */ |
2319 | "unspecified", | |
2320 | "keyCompromise", | |
2321 | "CACompromise", | |
2322 | "affiliationChanged", | |
2323 | "superseded", | |
2324 | "cessationOfOperation", | |
2325 | "certificateHold", | |
2326 | "removeFromCRL", | |
2327 | /* Additional pseudo reasons */ | |
2328 | "holdInstruction", | |
2329 | "keyTime", | |
2330 | "CAkeyTime" | |
a6b7ffdd DSH |
2331 | }; |
2332 | ||
bdcb1a2c | 2333 | #define NUM_REASONS OSSL_NELEM(crl_reasons) |
a6b7ffdd | 2334 | |
0f113f3e MC |
2335 | /* |
2336 | * Given revocation information convert to a DB string. The format of the | |
2337 | * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time | |
2338 | * (the current time). 'reason' is the optional CRL reason and 'extra' is any | |
2339 | * additional argument | |
a6b7ffdd DSH |
2340 | */ |
2341 | ||
06a79af2 | 2342 | static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg) |
0f113f3e | 2343 | { |
cc696296 | 2344 | char *str; |
06a79af2 | 2345 | const char *reason = NULL, *other = NULL; |
0f113f3e MC |
2346 | ASN1_OBJECT *otmp; |
2347 | ASN1_UTCTIME *revtm = NULL; | |
2348 | int i; | |
06a79af2 | 2349 | |
0f113f3e MC |
2350 | switch (rev_type) { |
2351 | case REV_NONE: | |
06a79af2 | 2352 | case REV_VALID: |
0f113f3e MC |
2353 | break; |
2354 | ||
2355 | case REV_CRL_REASON: | |
2356 | for (i = 0; i < 8; i++) { | |
fba140c7 | 2357 | if (OPENSSL_strcasecmp(rev_arg, crl_reasons[i]) == 0) { |
0f113f3e MC |
2358 | reason = crl_reasons[i]; |
2359 | break; | |
2360 | } | |
2361 | } | |
2362 | if (reason == NULL) { | |
2363 | BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg); | |
2364 | return NULL; | |
2365 | } | |
2366 | break; | |
2367 | ||
2368 | case REV_HOLD: | |
2369 | /* Argument is an OID */ | |
0f113f3e MC |
2370 | otmp = OBJ_txt2obj(rev_arg, 0); |
2371 | ASN1_OBJECT_free(otmp); | |
2372 | ||
2373 | if (otmp == NULL) { | |
2374 | BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg); | |
2375 | return NULL; | |
2376 | } | |
2377 | ||
2378 | reason = "holdInstruction"; | |
2379 | other = rev_arg; | |
2380 | break; | |
2381 | ||
2382 | case REV_KEY_COMPROMISE: | |
2383 | case REV_CA_COMPROMISE: | |
0f113f3e MC |
2384 | /* Argument is the key compromise time */ |
2385 | if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) { | |
2386 | BIO_printf(bio_err, | |
2387 | "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", | |
2388 | rev_arg); | |
2389 | return NULL; | |
2390 | } | |
2391 | other = rev_arg; | |
2392 | if (rev_type == REV_KEY_COMPROMISE) | |
2393 | reason = "keyTime"; | |
2394 | else | |
2395 | reason = "CAkeyTime"; | |
2396 | ||
2397 | break; | |
0f113f3e MC |
2398 | } |
2399 | ||
2400 | revtm = X509_gmtime_adj(NULL, 0); | |
2401 | ||
2402 | if (!revtm) | |
2403 | return NULL; | |
2404 | ||
2405 | i = revtm->length + 1; | |
2406 | ||
2407 | if (reason) | |
2408 | i += strlen(reason) + 1; | |
2409 | if (other) | |
2410 | i += strlen(other) + 1; | |
2411 | ||
68dc6824 | 2412 | str = app_malloc(i, "revocation reason"); |
7644a9ae | 2413 | OPENSSL_strlcpy(str, (char *)revtm->data, i); |
0f113f3e | 2414 | if (reason) { |
7644a9ae RS |
2415 | OPENSSL_strlcat(str, ",", i); |
2416 | OPENSSL_strlcat(str, reason, i); | |
0f113f3e MC |
2417 | } |
2418 | if (other) { | |
7644a9ae RS |
2419 | OPENSSL_strlcat(str, ",", i); |
2420 | OPENSSL_strlcat(str, other, i); | |
0f113f3e MC |
2421 | } |
2422 | ASN1_UTCTIME_free(revtm); | |
2423 | return str; | |
2424 | } | |
a6b7ffdd | 2425 | |
1d97c843 | 2426 | /*- |
0f113f3e | 2427 | * Convert revocation field to X509_REVOKED entry |
a6b7ffdd DSH |
2428 | * return code: |
2429 | * 0 error | |
2430 | * 1 OK | |
2431 | * 2 OK and some extensions added (i.e. V2 CRL) | |
2432 | */ | |
2433 | ||
06a79af2 | 2434 | static int make_revoked(X509_REVOKED *rev, const char *str) |
0f113f3e MC |
2435 | { |
2436 | char *tmp = NULL; | |
2437 | int reason_code = -1; | |
2438 | int i, ret = 0; | |
2439 | ASN1_OBJECT *hold = NULL; | |
2440 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
2441 | ASN1_ENUMERATED *rtmp = NULL; | |
2442 | ||
2443 | ASN1_TIME *revDate = NULL; | |
2444 | ||
2445 | i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str); | |
2446 | ||
2447 | if (i == 0) | |
7e1b7485 | 2448 | goto end; |
0f113f3e MC |
2449 | |
2450 | if (rev && !X509_REVOKED_set_revocationDate(rev, revDate)) | |
7e1b7485 | 2451 | goto end; |
0f113f3e MC |
2452 | |
2453 | if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { | |
2454 | rtmp = ASN1_ENUMERATED_new(); | |
96487cdd | 2455 | if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code)) |
7e1b7485 | 2456 | goto end; |
c540a827 | 2457 | if (X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0) <= 0) |
7e1b7485 | 2458 | goto end; |
0f113f3e MC |
2459 | } |
2460 | ||
2461 | if (rev && comp_time) { | |
c540a827 PH |
2462 | if (X509_REVOKED_add1_ext_i2d |
2463 | (rev, NID_invalidity_date, comp_time, 0, 0) <= 0) | |
7e1b7485 | 2464 | goto end; |
0f113f3e MC |
2465 | } |
2466 | if (rev && hold) { | |
c540a827 PH |
2467 | if (X509_REVOKED_add1_ext_i2d |
2468 | (rev, NID_hold_instruction_code, hold, 0, 0) <= 0) | |
7e1b7485 | 2469 | goto end; |
0f113f3e MC |
2470 | } |
2471 | ||
2472 | if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS) | |
2473 | ret = 2; | |
2474 | else | |
2475 | ret = 1; | |
2476 | ||
7e1b7485 | 2477 | end: |
0f113f3e | 2478 | |
b548a1f1 | 2479 | OPENSSL_free(tmp); |
0f113f3e MC |
2480 | ASN1_OBJECT_free(hold); |
2481 | ASN1_GENERALIZEDTIME_free(comp_time); | |
2482 | ASN1_ENUMERATED_free(rtmp); | |
2483 | ASN1_TIME_free(revDate); | |
2484 | ||
2485 | return ret; | |
2486 | } | |
bad40585 | 2487 | |
69b017f6 | 2488 | static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str) |
0f113f3e | 2489 | { |
cc696296 F |
2490 | char buf[25], *pbuf; |
2491 | const char *p; | |
0f113f3e | 2492 | int j; |
cc696296 | 2493 | |
ecf3a1fb | 2494 | j = i2a_ASN1_OBJECT(bio_err, obj); |
0f113f3e MC |
2495 | pbuf = buf; |
2496 | for (j = 22 - j; j > 0; j--) | |
2497 | *(pbuf++) = ' '; | |
2498 | *(pbuf++) = ':'; | |
2499 | *(pbuf++) = '\0'; | |
ecf3a1fb | 2500 | BIO_puts(bio_err, buf); |
0f113f3e MC |
2501 | |
2502 | if (str->type == V_ASN1_PRINTABLESTRING) | |
ecf3a1fb | 2503 | BIO_printf(bio_err, "PRINTABLE:'"); |
0f113f3e | 2504 | else if (str->type == V_ASN1_T61STRING) |
ecf3a1fb | 2505 | BIO_printf(bio_err, "T61STRING:'"); |
0f113f3e | 2506 | else if (str->type == V_ASN1_IA5STRING) |
ecf3a1fb | 2507 | BIO_printf(bio_err, "IA5STRING:'"); |
0f113f3e | 2508 | else if (str->type == V_ASN1_UNIVERSALSTRING) |
ecf3a1fb | 2509 | BIO_printf(bio_err, "UNIVERSALSTRING:'"); |
0f113f3e | 2510 | else |
ecf3a1fb | 2511 | BIO_printf(bio_err, "ASN.1 %2d:'", str->type); |
0f113f3e | 2512 | |
cc696296 | 2513 | p = (const char *)str->data; |
0f113f3e MC |
2514 | for (j = str->length; j > 0; j--) { |
2515 | if ((*p >= ' ') && (*p <= '~')) | |
ecf3a1fb | 2516 | BIO_printf(bio_err, "%c", *p); |
0f113f3e | 2517 | else if (*p & 0x80) |
ecf3a1fb | 2518 | BIO_printf(bio_err, "\\0x%02X", *p); |
0f113f3e | 2519 | else if ((unsigned char)*p == 0xf7) |
ecf3a1fb | 2520 | BIO_printf(bio_err, "^?"); |
0f113f3e | 2521 | else |
ecf3a1fb | 2522 | BIO_printf(bio_err, "^%c", *p + '@'); |
0f113f3e MC |
2523 | p++; |
2524 | } | |
ecf3a1fb | 2525 | BIO_printf(bio_err, "'\n"); |
0f113f3e MC |
2526 | return 1; |
2527 | } | |
2528 | ||
2529 | int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, | |
2530 | ASN1_GENERALIZEDTIME **pinvtm, const char *str) | |
2531 | { | |
7e1b7485 | 2532 | char *tmp; |
0f113f3e MC |
2533 | char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; |
2534 | int reason_code = -1; | |
2535 | int ret = 0; | |
2536 | unsigned int i; | |
2537 | ASN1_OBJECT *hold = NULL; | |
2538 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
0f113f3e | 2539 | |
7644a9ae | 2540 | tmp = OPENSSL_strdup(str); |
61986d32 | 2541 | if (!tmp) { |
a561bfe9 | 2542 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2543 | goto end; |
a561bfe9 MC |
2544 | } |
2545 | ||
0f113f3e MC |
2546 | p = strchr(tmp, ','); |
2547 | ||
2548 | rtime_str = tmp; | |
2549 | ||
2550 | if (p) { | |
2551 | *p = '\0'; | |
2552 | p++; | |
2553 | reason_str = p; | |
2554 | p = strchr(p, ','); | |
2555 | if (p) { | |
2556 | *p = '\0'; | |
2557 | arg_str = p + 1; | |
2558 | } | |
2559 | } | |
2560 | ||
2561 | if (prevtm) { | |
2562 | *prevtm = ASN1_UTCTIME_new(); | |
96487cdd | 2563 | if (*prevtm == NULL) { |
a561bfe9 | 2564 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2565 | goto end; |
a561bfe9 | 2566 | } |
0f113f3e MC |
2567 | if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) { |
2568 | BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); | |
7e1b7485 | 2569 | goto end; |
0f113f3e MC |
2570 | } |
2571 | } | |
2572 | if (reason_str) { | |
2573 | for (i = 0; i < NUM_REASONS; i++) { | |
fba140c7 | 2574 | if (OPENSSL_strcasecmp(reason_str, crl_reasons[i]) == 0) { |
0f113f3e MC |
2575 | reason_code = i; |
2576 | break; | |
2577 | } | |
2578 | } | |
2579 | if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { | |
2580 | BIO_printf(bio_err, "invalid reason code %s\n", reason_str); | |
7e1b7485 | 2581 | goto end; |
0f113f3e MC |
2582 | } |
2583 | ||
2234212c | 2584 | if (reason_code == 7) { |
0f113f3e | 2585 | reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; |
2234212c | 2586 | } else if (reason_code == 8) { /* Hold instruction */ |
0f113f3e MC |
2587 | if (!arg_str) { |
2588 | BIO_printf(bio_err, "missing hold instruction\n"); | |
7e1b7485 | 2589 | goto end; |
0f113f3e MC |
2590 | } |
2591 | reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; | |
2592 | hold = OBJ_txt2obj(arg_str, 0); | |
2593 | ||
2594 | if (!hold) { | |
b5895815 | 2595 | BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); |
7e1b7485 | 2596 | goto end; |
0f113f3e MC |
2597 | } |
2598 | if (phold) | |
2599 | *phold = hold; | |
2ace7450 RS |
2600 | else |
2601 | ASN1_OBJECT_free(hold); | |
0f113f3e MC |
2602 | } else if ((reason_code == 9) || (reason_code == 10)) { |
2603 | if (!arg_str) { | |
2604 | BIO_printf(bio_err, "missing compromised time\n"); | |
7e1b7485 | 2605 | goto end; |
0f113f3e MC |
2606 | } |
2607 | comp_time = ASN1_GENERALIZEDTIME_new(); | |
96487cdd | 2608 | if (comp_time == NULL) { |
a561bfe9 | 2609 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2610 | goto end; |
a561bfe9 | 2611 | } |
0f113f3e MC |
2612 | if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { |
2613 | BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); | |
7e1b7485 | 2614 | goto end; |
0f113f3e MC |
2615 | } |
2616 | if (reason_code == 9) | |
2617 | reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; | |
2618 | else | |
2619 | reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | if (preason) | |
2624 | *preason = reason_code; | |
2ace7450 | 2625 | if (pinvtm) { |
0f113f3e | 2626 | *pinvtm = comp_time; |
2ace7450 RS |
2627 | comp_time = NULL; |
2628 | } | |
0f113f3e MC |
2629 | |
2630 | ret = 1; | |
2631 | ||
7e1b7485 | 2632 | end: |
0f113f3e | 2633 | |
b548a1f1 | 2634 | OPENSSL_free(tmp); |
2ace7450 | 2635 | ASN1_GENERALIZEDTIME_free(comp_time); |
0f113f3e MC |
2636 | |
2637 | return ret; | |
2638 | } |