]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 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 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
d02b48c6 | 13 | #include "apps.h" |
dab2cd68 | 14 | #include "progs.h" |
ec577822 BM |
15 | #include <openssl/bio.h> |
16 | #include <openssl/asn1.h> | |
17 | #include <openssl/err.h> | |
18 | #include <openssl/bn.h> | |
19 | #include <openssl/evp.h> | |
20 | #include <openssl/x509.h> | |
21 | #include <openssl/x509v3.h> | |
22 | #include <openssl/objects.h> | |
23 | #include <openssl/pem.h> | |
3eeaab4b | 24 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 25 | # include <openssl/rsa.h> |
3eeaab4b NL |
26 | #endif |
27 | #ifndef OPENSSL_NO_DSA | |
0f113f3e | 28 | # include <openssl/dsa.h> |
3eeaab4b | 29 | #endif |
d02b48c6 | 30 | |
d02b48c6 | 31 | #undef POSTFIX |
0f113f3e MC |
32 | #define POSTFIX ".srl" |
33 | #define DEF_DAYS 30 | |
34 | ||
6d23cf97 | 35 | static int callb(int ok, X509_STORE_CTX *ctx); |
2292c8e1 RL |
36 | static int sign(X509 *x, EVP_PKEY *pkey, EVP_PKEY *fkey, |
37 | STACK_OF(OPENSSL_STRING) *sigopts, | |
38 | int days, int clrext, | |
4a60bb18 TS |
39 | const EVP_MD *digest, CONF *conf, const char *section, |
40 | int preserve_dates); | |
cc696296 | 41 | static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest, |
0f113f3e | 42 | X509 *x, X509 *xca, EVP_PKEY *pkey, |
cc696296 | 43 | STACK_OF(OPENSSL_STRING) *sigopts, const char *serialfile, |
0f113f3e | 44 | int create, int days, int clrext, CONF *conf, |
4a60bb18 TS |
45 | const char *section, ASN1_INTEGER *sno, int reqfile, |
46 | int preserve_dates); | |
d4cec6a1 | 47 | static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt); |
c2908538 | 48 | static int print_x509v3_exts(BIO *bio, X509 *x, const char *exts); |
7e1b7485 | 49 | |
7e1b7485 RS |
50 | typedef enum OPTION_choice { |
51 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
52 | OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM, | |
2292c8e1 | 53 | OPT_CAKEYFORM, OPT_VFYOPT, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE, |
52958608 DO |
54 | OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA, OPT_CAKEY, |
55 | OPT_CASERIAL, OPT_SET_SERIAL, OPT_NEW, OPT_FORCE_PUBKEY, OPT_SUBJ, | |
7e1b7485 RS |
56 | OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT, |
57 | OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL, | |
58 | OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH, | |
59 | OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES, | |
60 | OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST, | |
61 | OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST, | |
62 | OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID, | |
7e1b7485 RS |
63 | OPT_SUBJECT_HASH_OLD, |
64 | OPT_ISSUER_HASH_OLD, | |
3ee1eac2 | 65 | OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES, |
6bd4e3f2 | 66 | OPT_R_ENUM, OPT_PROV_ENUM, OPT_EXT |
7e1b7485 RS |
67 | } OPTION_CHOICE; |
68 | ||
44c83ebd | 69 | const OPTIONS x509_options[] = { |
5388f986 | 70 | OPT_SECTION("General"), |
7e1b7485 | 71 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
72 | #ifndef OPENSSL_NO_ENGINE |
73 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
74 | #endif | |
75 | ||
7e1b7485 | 76 | {"inform", OPT_INFORM, 'f', |
6d382c74 | 77 | "CSR input format (DER or PEM) - default PEM"}, |
7e1b7485 | 78 | {"in", OPT_IN, '<', "Input file - default stdin"}, |
2a33470b | 79 | {"passin", OPT_PASSIN, 's', "Private key and cert file pass-phrase source"}, |
7e1b7485 | 80 | {"outform", OPT_OUTFORM, 'f', |
6d382c74 | 81 | "Output format (DER or PEM) - default PEM"}, |
7e1b7485 | 82 | {"out", OPT_OUT, '>', "Output file - default stdout"}, |
6d382c74 | 83 | {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"}, |
5388f986 | 84 | {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"}, |
2292c8e1 | 85 | {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"}, |
5388f986 RS |
86 | |
87 | OPT_SECTION("Output"), | |
7e1b7485 RS |
88 | {"serial", OPT_SERIAL, '-', "Print serial number value"}, |
89 | {"subject_hash", OPT_HASH, '-', "Print subject hash value"}, | |
90 | {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"}, | |
7e1b7485 RS |
91 | {"hash", OPT_HASH, '-', "Synonym for -subject_hash"}, |
92 | {"subject", OPT_SUBJECT, '-', "Print subject DN"}, | |
93 | {"issuer", OPT_ISSUER, '-', "Print issuer DN"}, | |
94 | {"email", OPT_EMAIL, '-', "Print email address(es)"}, | |
7e1b7485 | 95 | {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"}, |
7e1b7485 RS |
96 | {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"}, |
97 | {"pubkey", OPT_PUBKEY, '-', "Output the public key"}, | |
5388f986 | 98 | {"fingerprint", OPT_FINGERPRINT, '-', "Print the certificate fingerprint"}, |
7e1b7485 RS |
99 | {"alias", OPT_ALIAS, '-', "Output certificate alias"}, |
100 | {"noout", OPT_NOOUT, '-', "No output, just status"}, | |
7e1b7485 RS |
101 | {"ocspid", OPT_OCSPID, '-', |
102 | "Print OCSP hash values for the subject name and public key"}, | |
103 | {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"}, | |
5388f986 | 104 | {"nocert", OPT_NOCERT, '-', "No certificate output"}, |
7e1b7485 | 105 | {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"}, |
5388f986 RS |
106 | {"x509toreq", OPT_X509TOREQ, '-', |
107 | "Output a certification request object"}, | |
108 | {"checkend", OPT_CHECKEND, 'M', | |
109 | "Check whether the cert expires in the next arg seconds"}, | |
110 | {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"}, | |
111 | {"text", OPT_TEXT, '-', "Print the certificate in text form"}, | |
112 | {"ext", OPT_EXT, 's', "Print various X509V3 extensions"}, | |
113 | {"C", OPT_C, '-', "Print out C code forms"}, | |
114 | #ifndef OPENSSL_NO_MD5 | |
115 | {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-', | |
5388f986 | 116 | "Print old-style (MD5) subject hash value"}, |
7e4f01d8 NJ |
117 | {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-', |
118 | "Print old-style (MD5) issuer hash value"}, | |
5388f986 | 119 | #endif |
2b264aee | 120 | {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"}, |
5388f986 RS |
121 | |
122 | OPT_SECTION("Certificate"), | |
123 | {"startdate", OPT_STARTDATE, '-', "Set notBefore field"}, | |
124 | {"enddate", OPT_ENDDATE, '-', "Set notAfter field"}, | |
125 | {"dates", OPT_DATES, '-', "Both Before and After dates"}, | |
7e1b7485 | 126 | {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"}, |
12d56b29 | 127 | {"clrext", OPT_CLREXT, '-', "Clear all certificate extensions"}, |
7e1b7485 RS |
128 | {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"}, |
129 | {"addreject", OPT_ADDREJECT, 's', | |
130 | "Reject certificate for a given purpose"}, | |
131 | {"setalias", OPT_SETALIAS, 's', "Set certificate alias"}, | |
132 | {"days", OPT_DAYS, 'n', | |
133 | "How long till expiry of a signed certificate - def 30 days"}, | |
ade08735 | 134 | {"signkey", OPT_SIGNKEY, 's', "Self-sign cert with arg"}, |
7e1b7485 | 135 | {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"}, |
7e1b7485 | 136 | {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"}, |
7e1b7485 RS |
137 | {"certopt", OPT_CERTOPT, 's', "Various certificate text options"}, |
138 | {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"}, | |
139 | {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"}, | |
140 | {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"}, | |
5388f986 RS |
141 | {"force_pubkey", OPT_FORCE_PUBKEY, '<', "Force the key to put inside certificate"}, |
142 | {"subj", OPT_SUBJ, 's', "Set or override certificate subject (and issuer)"}, | |
143 | ||
144 | OPT_SECTION("CA"), | |
145 | {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"}, | |
146 | {"CAkey", OPT_CAKEY, 's', | |
147 | "The CA key, must be PEM format; if not in CAfile"}, | |
148 | {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"}, | |
149 | OPT_R_OPTIONS, | |
6bd4e3f2 | 150 | OPT_PROV_OPTIONS, |
6d382c74 DDO |
151 | {"CAform", OPT_CAFORM, 'F', "CA cert format (PEM/DER/P12); has no effect"}, |
152 | {"CAkeyform", OPT_CAKEYFORM, 'E', "CA key format (ENGINE, other values ignored)"}, | |
16e1b281 | 153 | {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"}, |
5388f986 RS |
154 | {"CAcreateserial", OPT_CACREATESERIAL, '-', |
155 | "Create serial number file if it does not exist"}, | |
156 | {"CAserial", OPT_CASERIAL, 's', "Serial file"}, | |
52958608 | 157 | {"new", OPT_NEW, '-', "Generate a certificate from scratch"}, |
12d56b29 F |
158 | {"next_serial", OPT_NEXT_SERIAL, '-', "Increment current certificate serial number"}, |
159 | {"clrreject", OPT_CLRREJECT, '-', | |
160 | "Clears all the prohibited or rejected uses of the certificate"}, | |
16e1b281 | 161 | {"badsig", OPT_BADSIG, '-', "Corrupt last byte of certificate signature (for test)"}, |
7e1b7485 | 162 | {"", OPT_MD, '-', "Any supported digest"}, |
4a60bb18 | 163 | {"preserve_dates", OPT_PRESERVE_DATES, '-', "preserve existing dates when signing"}, |
7e1b7485 RS |
164 | {NULL} |
165 | }; | |
667ac4ec | 166 | |
7e1b7485 | 167 | int x509_main(int argc, char **argv) |
0f113f3e | 168 | { |
0f113f3e | 169 | ASN1_INTEGER *sno = NULL; |
f83b85fb | 170 | ASN1_OBJECT *objtmp = NULL; |
0f113f3e | 171 | BIO *out = NULL; |
7e1b7485 RS |
172 | CONF *extconf = NULL; |
173 | EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL; | |
52958608 DO |
174 | int newcert = 0; |
175 | char *subj = NULL; | |
176 | X509_NAME *fsubj = NULL; | |
177 | const unsigned long chtype = MBSTRING_ASC; | |
5a0991d0 | 178 | const int multirdn = 1; |
0f113f3e | 179 | STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL; |
2292c8e1 | 180 | STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL; |
7e1b7485 RS |
181 | X509 *x = NULL, *xca = NULL; |
182 | X509_REQ *req = NULL, *rq = NULL; | |
183 | X509_STORE *ctx = NULL; | |
184 | const EVP_MD *digest = NULL; | |
185 | char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL; | |
c2908538 | 186 | char *checkhost = NULL, *checkemail = NULL, *checkip = NULL, *exts = NULL; |
7e1b7485 | 187 | char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL; |
0f113f3e | 188 | char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL; |
b5c4209b | 189 | char *prog; |
333b070e RS |
190 | int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0; |
191 | int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM; | |
3ee1eac2 | 192 | int fingerprint = 0, reqfile = 0, checkend = 0; |
7e1b7485 RS |
193 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM; |
194 | int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0; | |
195 | int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0; | |
196 | int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0; | |
197 | int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0; | |
c2908538 | 198 | int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0, ext = 0; |
56087077 VD |
199 | int enddate = 0; |
200 | time_t checkoffset = 0; | |
b5c4209b | 201 | unsigned long certflag = 0; |
4a60bb18 | 202 | int preserve_dates = 0; |
7e1b7485 | 203 | OPTION_CHOICE o; |
7e1b7485 | 204 | ENGINE *e = NULL; |
7e1b7485 RS |
205 | #ifndef OPENSSL_NO_MD5 |
206 | int subject_hash_old = 0, issuer_hash_old = 0; | |
645749ef | 207 | #endif |
d02b48c6 | 208 | |
0f113f3e MC |
209 | ctx = X509_STORE_new(); |
210 | if (ctx == NULL) | |
211 | goto end; | |
212 | X509_STORE_set_verify_cb(ctx, callb); | |
213 | ||
7e1b7485 RS |
214 | prog = opt_init(argc, argv, x509_options); |
215 | while ((o = opt_next()) != OPT_EOF) { | |
216 | switch (o) { | |
217 | case OPT_EOF: | |
218 | case OPT_ERR: | |
219 | opthelp: | |
220 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
221 | goto end; | |
222 | case OPT_HELP: | |
223 | opt_help(x509_options); | |
224 | ret = 0; | |
225 | goto end; | |
226 | case OPT_INFORM: | |
6d382c74 | 227 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
7e1b7485 RS |
228 | goto opthelp; |
229 | break; | |
230 | case OPT_IN: | |
231 | infile = opt_arg(); | |
232 | break; | |
233 | case OPT_OUTFORM: | |
234 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
235 | goto opthelp; | |
236 | break; | |
237 | case OPT_KEYFORM: | |
6d382c74 | 238 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) |
7e1b7485 RS |
239 | goto opthelp; |
240 | break; | |
241 | case OPT_CAFORM: | |
6d382c74 | 242 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAformat)) |
7e1b7485 RS |
243 | goto opthelp; |
244 | break; | |
245 | case OPT_CAKEYFORM: | |
6d382c74 | 246 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat)) |
7e1b7485 RS |
247 | goto opthelp; |
248 | break; | |
249 | case OPT_OUT: | |
250 | outfile = opt_arg(); | |
251 | break; | |
252 | case OPT_REQ: | |
3ee1eac2 | 253 | reqfile = 1; |
7e1b7485 RS |
254 | break; |
255 | ||
256 | case OPT_SIGOPT: | |
0f113f3e MC |
257 | if (!sigopts) |
258 | sigopts = sk_OPENSSL_STRING_new_null(); | |
7e1b7485 RS |
259 | if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
260 | goto opthelp; | |
261 | break; | |
2292c8e1 RL |
262 | case OPT_VFYOPT: |
263 | if (!vfyopts) | |
264 | vfyopts = sk_OPENSSL_STRING_new_null(); | |
265 | if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg())) | |
266 | goto opthelp; | |
267 | break; | |
7e1b7485 | 268 | case OPT_DAYS: |
4a60bb18 TS |
269 | if (preserve_dates) |
270 | goto opthelp; | |
7e1b7485 RS |
271 | days = atoi(opt_arg()); |
272 | break; | |
273 | case OPT_PASSIN: | |
274 | passinarg = opt_arg(); | |
275 | break; | |
276 | case OPT_EXTFILE: | |
277 | extfile = opt_arg(); | |
278 | break; | |
3ee1eac2 RS |
279 | case OPT_R_CASES: |
280 | if (!opt_rand(o)) | |
281 | goto end; | |
282 | break; | |
6bd4e3f2 P |
283 | case OPT_PROV_CASES: |
284 | if (!opt_provider(o)) | |
285 | goto end; | |
286 | break; | |
7e1b7485 RS |
287 | case OPT_EXTENSIONS: |
288 | extsect = opt_arg(); | |
289 | break; | |
290 | case OPT_SIGNKEY: | |
291 | keyfile = opt_arg(); | |
0f113f3e | 292 | sign_flag = ++num; |
7e1b7485 RS |
293 | break; |
294 | case OPT_CA: | |
295 | CAfile = opt_arg(); | |
0f113f3e | 296 | CA_flag = ++num; |
7e1b7485 RS |
297 | break; |
298 | case OPT_CAKEY: | |
299 | CAkeyfile = opt_arg(); | |
300 | break; | |
301 | case OPT_CASERIAL: | |
302 | CAserial = opt_arg(); | |
303 | break; | |
304 | case OPT_SET_SERIAL: | |
08f6ae5b | 305 | if (sno != NULL) { |
efba7787 | 306 | BIO_printf(bio_err, "Serial number supplied twice\n"); |
08f6ae5b MC |
307 | goto opthelp; |
308 | } | |
7e1b7485 RS |
309 | if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) |
310 | goto opthelp; | |
311 | break; | |
52958608 DO |
312 | case OPT_NEW: |
313 | newcert = 1; | |
314 | break; | |
7e1b7485 RS |
315 | case OPT_FORCE_PUBKEY: |
316 | fkeyfile = opt_arg(); | |
317 | break; | |
52958608 DO |
318 | case OPT_SUBJ: |
319 | subj = opt_arg(); | |
320 | break; | |
7e1b7485 RS |
321 | case OPT_ADDTRUST: |
322 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
323 | BIO_printf(bio_err, | |
324 | "%s: Invalid trust object value %s\n", | |
325 | prog, opt_arg()); | |
326 | goto opthelp; | |
0f113f3e | 327 | } |
7e1b7485 RS |
328 | if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL) |
329 | goto end; | |
0f113f3e | 330 | sk_ASN1_OBJECT_push(trust, objtmp); |
f83b85fb | 331 | objtmp = NULL; |
0f113f3e | 332 | trustout = 1; |
7e1b7485 RS |
333 | break; |
334 | case OPT_ADDREJECT: | |
335 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
0f113f3e | 336 | BIO_printf(bio_err, |
7e1b7485 RS |
337 | "%s: Invalid reject object value %s\n", |
338 | prog, opt_arg()); | |
339 | goto opthelp; | |
0f113f3e | 340 | } |
7e1b7485 RS |
341 | if (reject == NULL |
342 | && (reject = sk_ASN1_OBJECT_new_null()) == NULL) | |
343 | goto end; | |
0f113f3e | 344 | sk_ASN1_OBJECT_push(reject, objtmp); |
f83b85fb | 345 | objtmp = NULL; |
0f113f3e | 346 | trustout = 1; |
7e1b7485 RS |
347 | break; |
348 | case OPT_SETALIAS: | |
349 | alias = opt_arg(); | |
0f113f3e | 350 | trustout = 1; |
7e1b7485 RS |
351 | break; |
352 | case OPT_CERTOPT: | |
353 | if (!set_cert_ex(&certflag, opt_arg())) | |
354 | goto opthelp; | |
355 | break; | |
356 | case OPT_NAMEOPT: | |
b5c4209b | 357 | if (!set_nameopt(opt_arg())) |
7e1b7485 RS |
358 | goto opthelp; |
359 | break; | |
360 | case OPT_ENGINE: | |
333b070e | 361 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
362 | break; |
363 | case OPT_C: | |
0f113f3e | 364 | C = ++num; |
7e1b7485 RS |
365 | break; |
366 | case OPT_EMAIL: | |
0f113f3e | 367 | email = ++num; |
7e1b7485 RS |
368 | break; |
369 | case OPT_OCSP_URI: | |
0f113f3e | 370 | ocsp_uri = ++num; |
7e1b7485 RS |
371 | break; |
372 | case OPT_SERIAL: | |
0f113f3e | 373 | serial = ++num; |
7e1b7485 RS |
374 | break; |
375 | case OPT_NEXT_SERIAL: | |
0f113f3e | 376 | next_serial = ++num; |
7e1b7485 RS |
377 | break; |
378 | case OPT_MODULUS: | |
0f113f3e | 379 | modulus = ++num; |
7e1b7485 RS |
380 | break; |
381 | case OPT_PUBKEY: | |
0f113f3e | 382 | pubkey = ++num; |
7e1b7485 RS |
383 | break; |
384 | case OPT_X509TOREQ: | |
0f113f3e | 385 | x509req = ++num; |
7e1b7485 RS |
386 | break; |
387 | case OPT_TEXT: | |
0f113f3e | 388 | text = ++num; |
7e1b7485 RS |
389 | break; |
390 | case OPT_SUBJECT: | |
0f113f3e | 391 | subject = ++num; |
7e1b7485 RS |
392 | break; |
393 | case OPT_ISSUER: | |
0f113f3e | 394 | issuer = ++num; |
7e1b7485 RS |
395 | break; |
396 | case OPT_FINGERPRINT: | |
0f113f3e | 397 | fingerprint = ++num; |
7e1b7485 RS |
398 | break; |
399 | case OPT_HASH: | |
400 | subject_hash = ++num; | |
401 | break; | |
402 | case OPT_ISSUER_HASH: | |
403 | issuer_hash = ++num; | |
404 | break; | |
405 | case OPT_PURPOSE: | |
0f113f3e | 406 | pprint = ++num; |
7e1b7485 RS |
407 | break; |
408 | case OPT_STARTDATE: | |
0f113f3e | 409 | startdate = ++num; |
7e1b7485 RS |
410 | break; |
411 | case OPT_ENDDATE: | |
0f113f3e | 412 | enddate = ++num; |
7e1b7485 RS |
413 | break; |
414 | case OPT_NOOUT: | |
0f113f3e | 415 | noout = ++num; |
7e1b7485 | 416 | break; |
c2908538 PY |
417 | case OPT_EXT: |
418 | ext = ++num; | |
419 | exts = opt_arg(); | |
420 | break; | |
7e1b7485 RS |
421 | case OPT_NOCERT: |
422 | nocert = 1; | |
423 | break; | |
424 | case OPT_TRUSTOUT: | |
0f113f3e | 425 | trustout = 1; |
7e1b7485 RS |
426 | break; |
427 | case OPT_CLRTRUST: | |
0f113f3e | 428 | clrtrust = ++num; |
7e1b7485 RS |
429 | break; |
430 | case OPT_CLRREJECT: | |
0f113f3e | 431 | clrreject = ++num; |
7e1b7485 RS |
432 | break; |
433 | case OPT_ALIAS: | |
0f113f3e | 434 | aliasout = ++num; |
7e1b7485 RS |
435 | break; |
436 | case OPT_CACREATESERIAL: | |
0f113f3e | 437 | CA_createserial = ++num; |
7e1b7485 RS |
438 | break; |
439 | case OPT_CLREXT: | |
0f113f3e | 440 | clrext = 1; |
7e1b7485 RS |
441 | break; |
442 | case OPT_OCSPID: | |
0f113f3e | 443 | ocspid = ++num; |
7e1b7485 RS |
444 | break; |
445 | case OPT_BADSIG: | |
0f113f3e | 446 | badsig = 1; |
0f113f3e | 447 | break; |
7e1b7485 RS |
448 | #ifndef OPENSSL_NO_MD5 |
449 | case OPT_SUBJECT_HASH_OLD: | |
450 | subject_hash_old = ++num; | |
451 | break; | |
452 | case OPT_ISSUER_HASH_OLD: | |
453 | issuer_hash_old = ++num; | |
454 | break; | |
9c3bcfa0 RS |
455 | #else |
456 | case OPT_SUBJECT_HASH_OLD: | |
457 | case OPT_ISSUER_HASH_OLD: | |
458 | break; | |
7e1b7485 RS |
459 | #endif |
460 | case OPT_DATES: | |
461 | startdate = ++num; | |
462 | enddate = ++num; | |
463 | break; | |
464 | case OPT_CHECKEND: | |
7e1b7485 | 465 | checkend = 1; |
33254e1c RL |
466 | { |
467 | intmax_t temp = 0; | |
468 | if (!opt_imax(opt_arg(), &temp)) | |
469 | goto opthelp; | |
470 | checkoffset = (time_t)temp; | |
471 | if ((intmax_t)checkoffset != temp) { | |
472 | BIO_printf(bio_err, "%s: checkend time out of range %s\n", | |
473 | prog, opt_arg()); | |
474 | goto opthelp; | |
475 | } | |
56087077 | 476 | } |
7e1b7485 RS |
477 | break; |
478 | case OPT_CHECKHOST: | |
479 | checkhost = opt_arg(); | |
480 | break; | |
481 | case OPT_CHECKEMAIL: | |
482 | checkemail = opt_arg(); | |
483 | break; | |
484 | case OPT_CHECKIP: | |
485 | checkip = opt_arg(); | |
486 | break; | |
4a60bb18 TS |
487 | case OPT_PRESERVE_DATES: |
488 | if (days != DEF_DAYS) | |
489 | goto opthelp; | |
490 | preserve_dates = 1; | |
491 | break; | |
7e1b7485 RS |
492 | case OPT_MD: |
493 | if (!opt_md(opt_unknown(), &digest)) | |
494 | goto opthelp; | |
0f113f3e | 495 | } |
7e1b7485 RS |
496 | } |
497 | argc = opt_num_rest(); | |
498 | argv = opt_rest(); | |
499 | if (argc != 0) { | |
500 | BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]); | |
501 | goto opthelp; | |
0f113f3e MC |
502 | } |
503 | ||
7e1b7485 | 504 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
505 | BIO_printf(bio_err, "Error getting password\n"); |
506 | goto end; | |
507 | } | |
508 | ||
d8652be0 MC |
509 | if (!X509_STORE_set_default_paths_ex(ctx, app_get0_libctx(), |
510 | app_get0_propq())) { | |
0f113f3e MC |
511 | ERR_print_errors(bio_err); |
512 | goto end; | |
513 | } | |
514 | ||
52958608 DO |
515 | if (newcert && infile != NULL) { |
516 | BIO_printf(bio_err, "The -in option must not be used since -new is set\n"); | |
517 | goto end; | |
518 | } | |
519 | if (newcert && fkeyfile == NULL) { | |
520 | BIO_printf(bio_err, | |
521 | "The -new option requires a public key to be set using -force_pubkey\n"); | |
522 | goto end; | |
523 | } | |
2234212c | 524 | if (fkeyfile != NULL) { |
50eb2a50 | 525 | fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "forced key"); |
0f113f3e MC |
526 | if (fkey == NULL) |
527 | goto end; | |
528 | } | |
529 | ||
52958608 DO |
530 | if (newcert && subj == NULL) { |
531 | BIO_printf(bio_err, | |
532 | "The -new option requires a subject to be set using -subj\n"); | |
533 | goto end; | |
534 | } | |
57c05c57 DDO |
535 | if (subj != NULL |
536 | && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL) | |
52958608 DO |
537 | goto end; |
538 | ||
539 | if (CAkeyfile == NULL && CA_flag && CAformat == FORMAT_PEM) { | |
0f113f3e | 540 | CAkeyfile = CAfile; |
52958608 | 541 | } else if (CA_flag && CAkeyfile == NULL) { |
0f113f3e MC |
542 | BIO_printf(bio_err, |
543 | "need to specify a CAkey if using the CA command\n"); | |
544 | goto end; | |
52958608 DO |
545 | } else if (!CA_flag && CAkeyfile != NULL) { |
546 | BIO_printf(bio_err, | |
547 | "ignoring -CAkey option since no -CA option is given\n"); | |
0f113f3e MC |
548 | } |
549 | ||
2234212c | 550 | if (extfile != NULL) { |
0f113f3e | 551 | X509V3_CTX ctx2; |
cc01d217 | 552 | if ((extconf = app_load_config(extfile)) == NULL) |
0f113f3e | 553 | goto end; |
2234212c | 554 | if (extsect == NULL) { |
0f113f3e | 555 | extsect = NCONF_get_string(extconf, "default", "extensions"); |
2234212c | 556 | if (extsect == NULL) { |
0f113f3e MC |
557 | ERR_clear_error(); |
558 | extsect = "default"; | |
559 | } | |
560 | } | |
561 | X509V3_set_ctx_test(&ctx2); | |
562 | X509V3_set_nconf(&ctx2, extconf); | |
563 | if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) { | |
564 | BIO_printf(bio_err, | |
565 | "Error Loading extension section %s\n", extsect); | |
566 | ERR_print_errors(bio_err); | |
567 | goto end; | |
568 | } | |
569 | } | |
570 | ||
571 | if (reqfile) { | |
572 | EVP_PKEY *pkey; | |
0f113f3e | 573 | |
9d5aca65 | 574 | req = load_csr(infile, informat, "certificate request input"); |
01c12100 | 575 | if (req == NULL) |
0f113f3e | 576 | goto end; |
0f113f3e | 577 | |
c5137473 | 578 | if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) { |
0f113f3e MC |
579 | BIO_printf(bio_err, "error unpacking public key\n"); |
580 | goto end; | |
581 | } | |
2292c8e1 | 582 | i = do_X509_REQ_verify(req, pkey, vfyopts); |
0f113f3e | 583 | if (i < 0) { |
52958608 | 584 | BIO_printf(bio_err, "Request self-signature verification error\n"); |
0f113f3e MC |
585 | ERR_print_errors(bio_err); |
586 | goto end; | |
587 | } | |
588 | if (i == 0) { | |
589 | BIO_printf(bio_err, | |
52958608 | 590 | "Request self-signature did not match the certificate request\n"); |
0f113f3e | 591 | goto end; |
2234212c | 592 | } else { |
52958608 | 593 | BIO_printf(bio_err, "Request self-signature ok\n"); |
2234212c | 594 | } |
0f113f3e MC |
595 | |
596 | print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), | |
b5c4209b | 597 | get_nameopt()); |
52958608 DO |
598 | } |
599 | ||
600 | if (reqfile || newcert) { | |
601 | X509_NAME *n; | |
0f113f3e | 602 | |
52958608 DO |
603 | if (!sign_flag && CAkeyfile == NULL) { |
604 | BIO_printf(bio_err, | |
605 | "We need a private key to sign with, use -signkey or -CAkey or -CA <file> with private key\n"); | |
606 | goto end; | |
607 | } | |
d8652be0 | 608 | if ((x = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL) |
0f113f3e MC |
609 | goto end; |
610 | ||
611 | if (sno == NULL) { | |
612 | sno = ASN1_INTEGER_new(); | |
96487cdd | 613 | if (sno == NULL || !rand_serial(NULL, sno)) |
0f113f3e MC |
614 | goto end; |
615 | if (!X509_set_serialNumber(x, sno)) | |
616 | goto end; | |
617 | ASN1_INTEGER_free(sno); | |
618 | sno = NULL; | |
2234212c | 619 | } else if (!X509_set_serialNumber(x, sno)) { |
0f113f3e | 620 | goto end; |
2234212c | 621 | } |
0f113f3e | 622 | |
52958608 DO |
623 | n = req == NULL ? fsubj : X509_REQ_get_subject_name(req); |
624 | if (!X509_set_issuer_name(x, n) || !X509_set_subject_name(x, n)) | |
0f113f3e | 625 | goto end; |
dc047d31 DSH |
626 | if (!set_cert_times(x, NULL, NULL, days)) |
627 | goto end; | |
0f113f3e | 628 | |
56a98c3e DO |
629 | if (!X509_set_pubkey(x, fkey != NULL ? fkey : X509_REQ_get0_pubkey(req))) |
630 | goto end; | |
2234212c | 631 | } else { |
50eb2a50 | 632 | x = load_cert_pass(infile, FORMAT_UNDEF, passin, "certificate"); |
56a98c3e DO |
633 | if (x == NULL) |
634 | goto end; | |
635 | if (fkey != NULL && !X509_set_pubkey(x, fkey)) | |
636 | goto end; | |
52958608 DO |
637 | if (fsubj != NULL && !X509_set_subject_name(x, fsubj)) |
638 | goto end; | |
2234212c | 639 | } |
0f113f3e | 640 | |
0f113f3e | 641 | if (CA_flag) { |
50eb2a50 | 642 | xca = load_cert_pass(CAfile, CAformat, passin, "CA certificate"); |
0f113f3e MC |
643 | if (xca == NULL) |
644 | goto end; | |
645 | } | |
646 | ||
2d1b5717 BE |
647 | out = bio_open_default(outfile, 'w', outformat); |
648 | if (out == NULL) | |
649 | goto end; | |
0f113f3e | 650 | |
2d1b5717 BE |
651 | if (!noout || text || next_serial) |
652 | OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3"); | |
0f113f3e MC |
653 | |
654 | if (alias) | |
655 | X509_alias_set1(x, (unsigned char *)alias, -1); | |
656 | ||
657 | if (clrtrust) | |
658 | X509_trust_clear(x); | |
659 | if (clrreject) | |
660 | X509_reject_clear(x); | |
661 | ||
2234212c | 662 | if (trust != NULL) { |
0f113f3e MC |
663 | for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) { |
664 | objtmp = sk_ASN1_OBJECT_value(trust, i); | |
665 | X509_add1_trust_object(x, objtmp); | |
666 | } | |
f83b85fb | 667 | objtmp = NULL; |
0f113f3e MC |
668 | } |
669 | ||
2234212c | 670 | if (reject != NULL) { |
0f113f3e MC |
671 | for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) { |
672 | objtmp = sk_ASN1_OBJECT_value(reject, i); | |
673 | X509_add1_reject_object(x, objtmp); | |
674 | } | |
f83b85fb | 675 | objtmp = NULL; |
0f113f3e MC |
676 | } |
677 | ||
0f022f5a | 678 | if (badsig) { |
8adc1cb8 DSH |
679 | const ASN1_BIT_STRING *signature; |
680 | ||
0f022f5a | 681 | X509_get0_signature(&signature, NULL, x); |
a0754084 | 682 | corrupt_signature(signature); |
0f022f5a DSH |
683 | } |
684 | ||
0f113f3e MC |
685 | if (num) { |
686 | for (i = 1; i <= num; i++) { | |
687 | if (issuer == i) { | |
b5c4209b | 688 | print_name(out, "issuer=", X509_get_issuer_name(x), get_nameopt()); |
0f113f3e | 689 | } else if (subject == i) { |
fb0303f3 | 690 | print_name(out, "subject=", |
b5c4209b | 691 | X509_get_subject_name(x), get_nameopt()); |
0f113f3e | 692 | } else if (serial == i) { |
7e1b7485 | 693 | BIO_printf(out, "serial="); |
1337a3a9 | 694 | i2a_ASN1_INTEGER(out, X509_get0_serialNumber(x)); |
7e1b7485 | 695 | BIO_printf(out, "\n"); |
0f113f3e | 696 | } else if (next_serial == i) { |
12d56b29 F |
697 | ASN1_INTEGER *ser = X509_get_serialNumber(x); |
698 | BIGNUM *bnser = ASN1_INTEGER_to_BN(ser, NULL); | |
699 | ||
0f113f3e MC |
700 | if (!bnser) |
701 | goto end; | |
702 | if (!BN_add_word(bnser, 1)) | |
703 | goto end; | |
704 | ser = BN_to_ASN1_INTEGER(bnser, NULL); | |
705 | if (!ser) | |
706 | goto end; | |
707 | BN_free(bnser); | |
708 | i2a_ASN1_INTEGER(out, ser); | |
709 | ASN1_INTEGER_free(ser); | |
710 | BIO_puts(out, "\n"); | |
52958608 | 711 | } else if (email == i || ocsp_uri == i) { |
0f113f3e MC |
712 | int j; |
713 | STACK_OF(OPENSSL_STRING) *emlst; | |
714 | if (email == i) | |
715 | emlst = X509_get1_email(x); | |
716 | else | |
717 | emlst = X509_get1_ocsp(x); | |
718 | for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++) | |
7e1b7485 | 719 | BIO_printf(out, "%s\n", |
0f113f3e MC |
720 | sk_OPENSSL_STRING_value(emlst, j)); |
721 | X509_email_free(emlst); | |
722 | } else if (aliasout == i) { | |
723 | unsigned char *alstr; | |
724 | alstr = X509_alias_get0(x, NULL); | |
725 | if (alstr) | |
7e1b7485 | 726 | BIO_printf(out, "%s\n", alstr); |
0f113f3e | 727 | else |
7e1b7485 | 728 | BIO_puts(out, "<No Alias>\n"); |
0f113f3e | 729 | } else if (subject_hash == i) { |
7e1b7485 | 730 | BIO_printf(out, "%08lx\n", X509_subject_name_hash(x)); |
0f113f3e | 731 | } |
0e0c6821 | 732 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 733 | else if (subject_hash_old == i) { |
7e1b7485 | 734 | BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x)); |
0f113f3e | 735 | } |
0e0c6821 | 736 | #endif |
0f113f3e | 737 | else if (issuer_hash == i) { |
7e1b7485 | 738 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x)); |
0f113f3e | 739 | } |
0e0c6821 | 740 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 741 | else if (issuer_hash_old == i) { |
7e1b7485 | 742 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x)); |
0f113f3e | 743 | } |
0e0c6821 | 744 | #endif |
0f113f3e MC |
745 | else if (pprint == i) { |
746 | X509_PURPOSE *ptmp; | |
747 | int j; | |
7e1b7485 | 748 | BIO_printf(out, "Certificate purposes:\n"); |
0f113f3e MC |
749 | for (j = 0; j < X509_PURPOSE_get_count(); j++) { |
750 | ptmp = X509_PURPOSE_get0(j); | |
7e1b7485 | 751 | purpose_print(out, x, ptmp); |
0f113f3e MC |
752 | } |
753 | } else if (modulus == i) { | |
754 | EVP_PKEY *pkey; | |
755 | ||
c01ff880 | 756 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
757 | if (pkey == NULL) { |
758 | BIO_printf(bio_err, "Modulus=unavailable\n"); | |
759 | ERR_print_errors(bio_err); | |
760 | goto end; | |
761 | } | |
7e1b7485 | 762 | BIO_printf(out, "Modulus="); |
cf1b7d96 | 763 | #ifndef OPENSSL_NO_RSA |
9862e9aa | 764 | if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
2ac6115d | 765 | const BIGNUM *n; |
9862e9aa RL |
766 | RSA_get0_key(EVP_PKEY_get0_RSA(pkey), &n, NULL, NULL); |
767 | BN_print(out, n); | |
768 | } else | |
7be304ac | 769 | #endif |
cf1b7d96 | 770 | #ifndef OPENSSL_NO_DSA |
6e9fa57c | 771 | if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA) { |
2ac6115d | 772 | const BIGNUM *dsapub = NULL; |
6e9fa57c MC |
773 | DSA_get0_key(EVP_PKEY_get0_DSA(pkey), &dsapub, NULL); |
774 | BN_print(out, dsapub); | |
775 | } else | |
7be304ac | 776 | #endif |
6e9fa57c | 777 | { |
7e1b7485 | 778 | BIO_printf(out, "Wrong Algorithm type"); |
6e9fa57c | 779 | } |
7e1b7485 | 780 | BIO_printf(out, "\n"); |
0f113f3e MC |
781 | } else if (pubkey == i) { |
782 | EVP_PKEY *pkey; | |
783 | ||
c01ff880 | 784 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
785 | if (pkey == NULL) { |
786 | BIO_printf(bio_err, "Error getting public key\n"); | |
787 | ERR_print_errors(bio_err); | |
788 | goto end; | |
789 | } | |
7e1b7485 | 790 | PEM_write_bio_PUBKEY(out, pkey); |
0f113f3e MC |
791 | } else if (C == i) { |
792 | unsigned char *d; | |
793 | char *m; | |
7e1b7485 | 794 | int len; |
0f113f3e | 795 | |
b5c4209b DB |
796 | print_name(out, "/*\n" |
797 | " * Subject: ", X509_get_subject_name(x), get_nameopt()); | |
798 | print_name(out, " * Issuer: ", X509_get_issuer_name(x), get_nameopt()); | |
799 | BIO_puts(out, " */\n"); | |
0f113f3e | 800 | |
7e1b7485 | 801 | len = i2d_X509(x, NULL); |
68dc6824 | 802 | m = app_malloc(len, "x509 name buffer"); |
0f113f3e | 803 | d = (unsigned char *)m; |
7e1b7485 RS |
804 | len = i2d_X509_NAME(X509_get_subject_name(x), &d); |
805 | print_array(out, "the_subject_name", len, (unsigned char *)m); | |
0f113f3e | 806 | d = (unsigned char *)m; |
7e1b7485 RS |
807 | len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d); |
808 | print_array(out, "the_public_key", len, (unsigned char *)m); | |
0f113f3e | 809 | d = (unsigned char *)m; |
7e1b7485 RS |
810 | len = i2d_X509(x, &d); |
811 | print_array(out, "the_certificate", len, (unsigned char *)m); | |
0f113f3e MC |
812 | OPENSSL_free(m); |
813 | } else if (text == i) { | |
b5c4209b | 814 | X509_print_ex(out, x, get_nameopt(), certflag); |
0f113f3e | 815 | } else if (startdate == i) { |
7e1b7485 | 816 | BIO_puts(out, "notBefore="); |
568ce3a5 | 817 | ASN1_TIME_print(out, X509_get0_notBefore(x)); |
7e1b7485 | 818 | BIO_puts(out, "\n"); |
0f113f3e | 819 | } else if (enddate == i) { |
7e1b7485 | 820 | BIO_puts(out, "notAfter="); |
568ce3a5 | 821 | ASN1_TIME_print(out, X509_get0_notAfter(x)); |
7e1b7485 | 822 | BIO_puts(out, "\n"); |
0f113f3e MC |
823 | } else if (fingerprint == i) { |
824 | int j; | |
825 | unsigned int n; | |
826 | unsigned char md[EVP_MAX_MD_SIZE]; | |
827 | const EVP_MD *fdig = digest; | |
828 | ||
2234212c | 829 | if (fdig == NULL) |
0f113f3e MC |
830 | fdig = EVP_sha1(); |
831 | ||
832 | if (!X509_digest(x, fdig, md, &n)) { | |
833 | BIO_printf(bio_err, "out of memory\n"); | |
834 | goto end; | |
835 | } | |
7e1b7485 | 836 | BIO_printf(out, "%s Fingerprint=", |
0f113f3e MC |
837 | OBJ_nid2sn(EVP_MD_type(fdig))); |
838 | for (j = 0; j < (int)n; j++) { | |
7e1b7485 | 839 | BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) |
0f113f3e MC |
840 | ? '\n' : ':'); |
841 | } | |
842 | } | |
843 | ||
844 | /* should be in the library */ | |
52958608 | 845 | else if (sign_flag == i && x509req == 0) { |
0f113f3e MC |
846 | BIO_printf(bio_err, "Getting Private key\n"); |
847 | if (Upkey == NULL) { | |
7e1b7485 | 848 | Upkey = load_key(keyfile, keyformat, 0, |
50eb2a50 | 849 | passin, e, "private key"); |
0f113f3e MC |
850 | if (Upkey == NULL) |
851 | goto end; | |
852 | } | |
853 | ||
2292c8e1 RL |
854 | if (!sign(x, Upkey, fkey, sigopts, days, clrext, digest, |
855 | extconf, extsect, preserve_dates)) | |
0f113f3e MC |
856 | goto end; |
857 | } else if (CA_flag == i) { | |
858 | BIO_printf(bio_err, "Getting CA Private Key\n"); | |
859 | if (CAkeyfile != NULL) { | |
7e1b7485 | 860 | CApkey = load_key(CAkeyfile, CAkeyformat, |
50eb2a50 | 861 | 0, passin, e, "CA private key"); |
0f113f3e MC |
862 | if (CApkey == NULL) |
863 | goto end; | |
864 | } | |
865 | ||
0f113f3e MC |
866 | if (!x509_certify(ctx, CAfile, digest, x, xca, |
867 | CApkey, sigopts, | |
868 | CAserial, CA_createserial, days, clrext, | |
4a60bb18 | 869 | extconf, extsect, sno, reqfile, preserve_dates)) |
0f113f3e MC |
870 | goto end; |
871 | } else if (x509req == i) { | |
872 | EVP_PKEY *pk; | |
873 | ||
874 | BIO_printf(bio_err, "Getting request Private Key\n"); | |
875 | if (keyfile == NULL) { | |
876 | BIO_printf(bio_err, "no request key file specified\n"); | |
877 | goto end; | |
878 | } else { | |
7e1b7485 | 879 | pk = load_key(keyfile, keyformat, 0, |
0f113f3e MC |
880 | passin, e, "request key"); |
881 | if (pk == NULL) | |
882 | goto end; | |
883 | } | |
884 | ||
885 | BIO_printf(bio_err, "Generating certificate request\n"); | |
886 | ||
887 | rq = X509_to_X509_REQ(x, pk, digest); | |
888 | EVP_PKEY_free(pk); | |
889 | if (rq == NULL) { | |
890 | ERR_print_errors(bio_err); | |
891 | goto end; | |
892 | } | |
893 | if (!noout) { | |
b5c4209b | 894 | X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT); |
0f113f3e MC |
895 | PEM_write_bio_X509_REQ(out, rq); |
896 | } | |
897 | noout = 1; | |
898 | } else if (ocspid == i) { | |
899 | X509_ocspid_print(out, x); | |
c2908538 PY |
900 | } else if (ext == i) { |
901 | print_x509v3_exts(out, x, exts); | |
0f113f3e MC |
902 | } |
903 | } | |
904 | } | |
905 | ||
906 | if (checkend) { | |
907 | time_t tcheck = time(NULL) + checkoffset; | |
908 | ||
568ce3a5 | 909 | if (X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0) { |
0f113f3e MC |
910 | BIO_printf(out, "Certificate will expire\n"); |
911 | ret = 1; | |
912 | } else { | |
913 | BIO_printf(out, "Certificate will not expire\n"); | |
914 | ret = 0; | |
915 | } | |
916 | goto end; | |
917 | } | |
918 | ||
7e1b7485 | 919 | print_cert_checks(out, x, checkhost, checkemail, checkip); |
0f113f3e | 920 | |
7e1b7485 | 921 | if (noout || nocert) { |
0f113f3e MC |
922 | ret = 0; |
923 | goto end; | |
924 | } | |
925 | ||
2234212c | 926 | if (outformat == FORMAT_ASN1) { |
0f113f3e | 927 | i = i2d_X509_bio(out, x); |
2234212c | 928 | } else if (outformat == FORMAT_PEM) { |
0f113f3e MC |
929 | if (trustout) |
930 | i = PEM_write_bio_X509_AUX(out, x); | |
931 | else | |
932 | i = PEM_write_bio_X509(out, x); | |
0f113f3e MC |
933 | } else { |
934 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
935 | goto end; | |
936 | } | |
937 | if (!i) { | |
938 | BIO_printf(bio_err, "unable to write certificate\n"); | |
939 | ERR_print_errors(bio_err); | |
940 | goto end; | |
941 | } | |
942 | ret = 0; | |
943 | end: | |
0f113f3e MC |
944 | NCONF_free(extconf); |
945 | BIO_free_all(out); | |
0f113f3e | 946 | X509_STORE_free(ctx); |
52958608 | 947 | X509_NAME_free(fsubj); |
0f113f3e MC |
948 | X509_REQ_free(req); |
949 | X509_free(x); | |
950 | X509_free(xca); | |
951 | EVP_PKEY_free(Upkey); | |
952 | EVP_PKEY_free(CApkey); | |
953 | EVP_PKEY_free(fkey); | |
25aaa98a | 954 | sk_OPENSSL_STRING_free(sigopts); |
2292c8e1 | 955 | sk_OPENSSL_STRING_free(vfyopts); |
0f113f3e MC |
956 | X509_REQ_free(rq); |
957 | ASN1_INTEGER_free(sno); | |
958 | sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free); | |
959 | sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free); | |
f83b85fb | 960 | ASN1_OBJECT_free(objtmp); |
dd1abd44 | 961 | release_engine(e); |
2a33470b | 962 | clear_free(passin); |
eee95522 | 963 | return ret; |
0f113f3e MC |
964 | } |
965 | ||
eee95522 P |
966 | static ASN1_INTEGER *x509_load_serial(const char *CAfile, |
967 | const char *serialfile, int create) | |
0f113f3e | 968 | { |
eee95522 | 969 | char *buf = NULL; |
0f113f3e MC |
970 | ASN1_INTEGER *bs = NULL; |
971 | BIGNUM *serial = NULL; | |
0f113f3e | 972 | |
0f113f3e | 973 | if (serialfile == NULL) { |
f223e8fd | 974 | const char *p = strrchr(CAfile, '.'); |
eee95522 P |
975 | size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile); |
976 | ||
977 | buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer"); | |
978 | memcpy(buf, CAfile, len); | |
979 | memcpy(buf + len, POSTFIX, sizeof(POSTFIX)); | |
980 | serialfile = buf; | |
2234212c | 981 | } |
0f113f3e | 982 | |
eee95522 | 983 | serial = load_serial(serialfile, create, NULL); |
0f113f3e MC |
984 | if (serial == NULL) |
985 | goto end; | |
986 | ||
987 | if (!BN_add_word(serial, 1)) { | |
988 | BIO_printf(bio_err, "add_word failure\n"); | |
989 | goto end; | |
990 | } | |
991 | ||
eee95522 | 992 | if (!save_serial(serialfile, NULL, serial, &bs)) |
0f113f3e | 993 | goto end; |
acba75c5 | 994 | |
f85b68cd | 995 | end: |
b548a1f1 | 996 | OPENSSL_free(buf); |
0f113f3e MC |
997 | BN_free(serial); |
998 | return bs; | |
999 | } | |
acba75c5 | 1000 | |
cc696296 | 1001 | static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest, |
0f113f3e MC |
1002 | X509 *x, X509 *xca, EVP_PKEY *pkey, |
1003 | STACK_OF(OPENSSL_STRING) *sigopts, | |
cc696296 F |
1004 | const char *serialfile, int create, |
1005 | int days, int clrext, CONF *conf, const char *section, | |
4a60bb18 | 1006 | ASN1_INTEGER *sno, int reqfile, int preserve_dates) |
0f113f3e MC |
1007 | { |
1008 | int ret = 0; | |
1009 | ASN1_INTEGER *bs = NULL; | |
f0e0fd51 | 1010 | X509_STORE_CTX *xsc = NULL; |
0f113f3e MC |
1011 | EVP_PKEY *upkey; |
1012 | ||
c01ff880 | 1013 | upkey = X509_get0_pubkey(xca); |
c223c4a9 MC |
1014 | if (upkey == NULL) { |
1015 | BIO_printf(bio_err, "Error obtaining CA X509 public key\n"); | |
1016 | goto end; | |
1017 | } | |
0f113f3e | 1018 | EVP_PKEY_copy_parameters(upkey, pkey); |
0f113f3e | 1019 | |
f0e0fd51 RS |
1020 | xsc = X509_STORE_CTX_new(); |
1021 | if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) { | |
0f113f3e MC |
1022 | BIO_printf(bio_err, "Error initialising X509 store\n"); |
1023 | goto end; | |
1024 | } | |
1025 | if (sno) | |
1026 | bs = sno; | |
75ebbd9a | 1027 | else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL) |
0f113f3e MC |
1028 | goto end; |
1029 | ||
0f113f3e | 1030 | /* |
ade08735 | 1031 | * NOTE: this certificate can/should be self-signed, unless it was a |
0f113f3e MC |
1032 | * certificate request in which case it is not. |
1033 | */ | |
f0e0fd51 RS |
1034 | X509_STORE_CTX_set_cert(xsc, x); |
1035 | X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE); | |
1036 | if (!reqfile && X509_verify_cert(xsc) <= 0) | |
0f113f3e MC |
1037 | goto end; |
1038 | ||
1039 | if (!X509_check_private_key(xca, pkey)) { | |
1040 | BIO_printf(bio_err, | |
1041 | "CA certificate and CA private key do not match\n"); | |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | if (!X509_set_issuer_name(x, X509_get_subject_name(xca))) | |
1046 | goto end; | |
1047 | if (!X509_set_serialNumber(x, bs)) | |
1048 | goto end; | |
1049 | ||
4a60bb18 | 1050 | if (!preserve_dates && !set_cert_times(x, NULL, NULL, days)) |
0f113f3e MC |
1051 | goto end; |
1052 | ||
1053 | if (clrext) { | |
1054 | while (X509_get_ext_count(x) > 0) | |
1055 | X509_delete_ext(x, 0); | |
1056 | } | |
1057 | ||
2234212c | 1058 | if (conf != NULL) { |
0f113f3e | 1059 | X509V3_CTX ctx2; |
0f113f3e | 1060 | X509_set_version(x, 2); /* version 3 certificate */ |
0f113f3e MC |
1061 | X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0); |
1062 | X509V3_set_nconf(&ctx2, conf); | |
1063 | if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) | |
1064 | goto end; | |
1065 | } | |
1066 | ||
7e1b7485 | 1067 | if (!do_X509_sign(x, pkey, digest, sigopts)) |
0f113f3e MC |
1068 | goto end; |
1069 | ret = 1; | |
1070 | end: | |
f0e0fd51 | 1071 | X509_STORE_CTX_free(xsc); |
0f113f3e MC |
1072 | if (!ret) |
1073 | ERR_print_errors(bio_err); | |
1074 | if (!sno) | |
1075 | ASN1_INTEGER_free(bs); | |
1076 | return ret; | |
1077 | } | |
d02b48c6 | 1078 | |
6d23cf97 | 1079 | static int callb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
1080 | { |
1081 | int err; | |
1082 | X509 *err_cert; | |
1083 | ||
1084 | /* | |
ade08735 DDO |
1085 | * It is ok to use a self-signed certificate. This case will catch both |
1086 | * the initial ok == 0 and the final ok == 1 calls to this function. | |
0f113f3e MC |
1087 | */ |
1088 | err = X509_STORE_CTX_get_error(ctx); | |
1089 | if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) | |
1090 | return 1; | |
1091 | ||
1092 | /* | |
1093 | * BAD we should have gotten an error. Normally if everything worked | |
1094 | * X509_STORE_CTX_get_error(ctx) will still be set to | |
1095 | * DEPTH_ZERO_SELF_.... | |
1096 | */ | |
1097 | if (ok) { | |
1098 | BIO_printf(bio_err, | |
ade08735 | 1099 | "error with certificate to be certified - should be self-signed\n"); |
0f113f3e MC |
1100 | return 0; |
1101 | } else { | |
1102 | err_cert = X509_STORE_CTX_get_current_cert(ctx); | |
1103 | print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0); | |
1104 | BIO_printf(bio_err, | |
1105 | "error with certificate - error %d at depth %d\n%s\n", err, | |
1106 | X509_STORE_CTX_get_error_depth(ctx), | |
1107 | X509_verify_cert_error_string(err)); | |
1108 | return 1; | |
1109 | } | |
1110 | } | |
d02b48c6 | 1111 | |
56a98c3e | 1112 | /* self-issue; self-sign unless a forced public key (fkey) is given */ |
2292c8e1 RL |
1113 | static int sign(X509 *x, EVP_PKEY *pkey, EVP_PKEY *fkey, |
1114 | STACK_OF(OPENSSL_STRING) *sigopts, | |
1115 | int days, int clrext, | |
4a60bb18 TS |
1116 | const EVP_MD *digest, CONF *conf, const char *section, |
1117 | int preserve_dates) | |
0f113f3e | 1118 | { |
0f113f3e MC |
1119 | if (!X509_set_issuer_name(x, X509_get_subject_name(x))) |
1120 | goto err; | |
4a60bb18 | 1121 | if (!preserve_dates && !set_cert_times(x, NULL, NULL, days)) |
0f113f3e | 1122 | goto err; |
56a98c3e | 1123 | if (fkey == NULL && !X509_set_pubkey(x, pkey)) |
0f113f3e MC |
1124 | goto err; |
1125 | if (clrext) { | |
1126 | while (X509_get_ext_count(x) > 0) | |
1127 | X509_delete_ext(x, 0); | |
1128 | } | |
2234212c | 1129 | if (conf != NULL) { |
0f113f3e | 1130 | X509V3_CTX ctx; |
0f113f3e | 1131 | X509_set_version(x, 2); /* version 3 certificate */ |
0f113f3e MC |
1132 | X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0); |
1133 | X509V3_set_nconf(&ctx, conf); | |
1134 | if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) | |
1135 | goto err; | |
1136 | } | |
2292c8e1 | 1137 | if (!do_X509_sign(x, pkey, digest, sigopts)) |
0f113f3e MC |
1138 | goto err; |
1139 | return 1; | |
1140 | err: | |
1141 | ERR_print_errors(bio_err); | |
1142 | return 0; | |
1143 | } | |
673b102c | 1144 | |
d4cec6a1 | 1145 | static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt) |
673b102c | 1146 | { |
0f113f3e | 1147 | int id, i, idret; |
82643254 | 1148 | const char *pname; |
0f113f3e MC |
1149 | id = X509_PURPOSE_get_id(pt); |
1150 | pname = X509_PURPOSE_get0_name(pt); | |
1151 | for (i = 0; i < 2; i++) { | |
1152 | idret = X509_check_purpose(cert, id, i); | |
1153 | BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); | |
1154 | if (idret == 1) | |
1155 | BIO_printf(bio, "Yes\n"); | |
1156 | else if (idret == 0) | |
1157 | BIO_printf(bio, "No\n"); | |
1158 | else | |
1159 | BIO_printf(bio, "Yes (WARNING code=%d)\n", idret); | |
1160 | } | |
1161 | return 1; | |
673b102c | 1162 | } |
c2908538 PY |
1163 | |
1164 | static int parse_ext_names(char *names, const char **result) | |
1165 | { | |
1166 | char *p, *q; | |
1167 | int cnt = 0, len = 0; | |
1168 | ||
1169 | p = q = names; | |
1170 | len = strlen(names); | |
1171 | ||
1172 | while (q - names <= len) { | |
1173 | if (*q != ',' && *q != '\0') { | |
1174 | q++; | |
1175 | continue; | |
1176 | } | |
1177 | if (p != q) { | |
1178 | /* found */ | |
1179 | if (result != NULL) { | |
1180 | result[cnt] = p; | |
1181 | *q = '\0'; | |
1182 | } | |
1183 | cnt++; | |
1184 | } | |
1185 | p = ++q; | |
1186 | } | |
1187 | ||
1188 | return cnt; | |
1189 | } | |
1190 | ||
1191 | static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names) | |
1192 | { | |
1193 | const STACK_OF(X509_EXTENSION) *exts = NULL; | |
1194 | STACK_OF(X509_EXTENSION) *exts2 = NULL; | |
1195 | X509_EXTENSION *ext = NULL; | |
1196 | ASN1_OBJECT *obj; | |
1197 | int i, j, ret = 0, num, nn = 0; | |
1198 | const char *sn, **names = NULL; | |
1199 | char *tmp_ext_names = NULL; | |
1200 | ||
1201 | exts = X509_get0_extensions(x); | |
1202 | if ((num = sk_X509_EXTENSION_num(exts)) <= 0) { | |
1203 | BIO_printf(bio, "No extensions in certificate\n"); | |
1204 | ret = 1; | |
1205 | goto end; | |
1206 | } | |
1207 | ||
1208 | /* parse comma separated ext name string */ | |
1209 | if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL) | |
1210 | goto end; | |
1211 | if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) { | |
1212 | BIO_printf(bio, "Invalid extension names: %s\n", ext_names); | |
1213 | goto end; | |
1214 | } | |
1215 | if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL) | |
1216 | goto end; | |
1217 | parse_ext_names(tmp_ext_names, names); | |
1218 | ||
1219 | for (i = 0; i < num; i++) { | |
1220 | ext = sk_X509_EXTENSION_value(exts, i); | |
1221 | ||
1222 | /* check if this ext is what we want */ | |
1223 | obj = X509_EXTENSION_get_object(ext); | |
1224 | sn = OBJ_nid2sn(OBJ_obj2nid(obj)); | |
1225 | if (sn == NULL || strcmp(sn, "UNDEF") == 0) | |
1226 | continue; | |
1227 | ||
1228 | for (j = 0; j < nn; j++) { | |
1229 | if (strcmp(sn, names[j]) == 0) { | |
1230 | /* push the extension into a new stack */ | |
1231 | if (exts2 == NULL | |
1232 | && (exts2 = sk_X509_EXTENSION_new_null()) == NULL) | |
1233 | goto end; | |
1234 | if (!sk_X509_EXTENSION_push(exts2, ext)) | |
1235 | goto end; | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | ||
1240 | if (!sk_X509_EXTENSION_num(exts2)) { | |
1241 | BIO_printf(bio, "No extensions matched with %s\n", ext_names); | |
1242 | ret = 1; | |
1243 | goto end; | |
1244 | } | |
1245 | ||
1246 | ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0); | |
1247 | end: | |
1248 | sk_X509_EXTENSION_free(exts2); | |
1249 | OPENSSL_free(names); | |
1250 | OPENSSL_free(tmp_ext_names); | |
1251 | return ret; | |
1252 | } |