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