]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/x509.c
Make the DSA structure opaque
[thirdparty/openssl.git] / apps / x509.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "apps.h"
62 #include <openssl/bio.h>
63 #include <openssl/asn1.h>
64 #include <openssl/err.h>
65 #include <openssl/bn.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509.h>
68 #include <openssl/x509v3.h>
69 #include <openssl/objects.h>
70 #include <openssl/pem.h>
71 #ifndef OPENSSL_NO_RSA
72 # include <openssl/rsa.h>
73 #endif
74 #ifndef OPENSSL_NO_DSA
75 # include <openssl/dsa.h>
76 #endif
77
78 #undef POSTFIX
79 #define POSTFIX ".srl"
80 #define DEF_DAYS 30
81
82 static int callb(int ok, X509_STORE_CTX *ctx);
83 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
84 const EVP_MD *digest, CONF *conf, char *section);
85 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
86 X509 *x, X509 *xca, EVP_PKEY *pkey,
87 STACK_OF(OPENSSL_STRING) *sigopts, char *serial,
88 int create, int days, int clrext, CONF *conf,
89 char *section, ASN1_INTEGER *sno, int reqfile);
90 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
91
92 typedef enum OPTION_choice {
93 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
94 OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
95 OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
96 OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA,
97 OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY,
98 OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT,
99 OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
100 OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
101 OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
102 OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
103 OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
104 OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
105 OPT_SUBJECT_HASH_OLD,
106 OPT_ISSUER_HASH_OLD,
107 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT
108 } OPTION_CHOICE;
109
110 OPTIONS x509_options[] = {
111 {"help", OPT_HELP, '-', "Display this summary"},
112 {"inform", OPT_INFORM, 'f',
113 "Input format - default PEM (one of DER, NET or PEM)"},
114 {"in", OPT_IN, '<', "Input file - default stdin"},
115 {"outform", OPT_OUTFORM, 'f',
116 "Output format - default PEM (one of DER, NET or PEM)"},
117 {"out", OPT_OUT, '>', "Output file - default stdout"},
118 {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
119 {"passin", OPT_PASSIN, 's', "Private key password source"},
120 {"serial", OPT_SERIAL, '-', "Print serial number value"},
121 {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
122 {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
123 {"hash", OPT_HASH, '-', "Synonym for -subject_hash"},
124 {"subject", OPT_SUBJECT, '-', "Print subject DN"},
125 {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
126 {"email", OPT_EMAIL, '-', "Print email address(es)"},
127 {"startdate", OPT_STARTDATE, '-', "Set notBefore field"},
128 {"enddate", OPT_ENDDATE, '-', "Set notAfter field"},
129 {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
130 {"dates", OPT_DATES, '-', "Both Before and After dates"},
131 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
132 {"pubkey", OPT_PUBKEY, '-', "Output the public key"},
133 {"fingerprint", OPT_FINGERPRINT, '-',
134 "Print the certificate fingerprint"},
135 {"alias", OPT_ALIAS, '-', "Output certificate alias"},
136 {"noout", OPT_NOOUT, '-', "No output, just status"},
137 {"nocert", OPT_NOCERT, '-', "No certificate output"},
138 {"ocspid", OPT_OCSPID, '-',
139 "Print OCSP hash values for the subject name and public key"},
140 {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
141 {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"},
142 {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
143 {"clrext", OPT_CLREXT, '-', "Clear all rejected purposes"},
144 {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
145 {"addreject", OPT_ADDREJECT, 's',
146 "Reject certificate for a given purpose"},
147 {"setalias", OPT_SETALIAS, 's', "Set certificate alias"},
148 {"days", OPT_DAYS, 'n',
149 "How long till expiry of a signed certificate - def 30 days"},
150 {"checkend", OPT_CHECKEND, 'M',
151 "Check whether the cert expires in the next arg seconds"},
152 {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"},
153 {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"},
154 {"x509toreq", OPT_X509TOREQ, '-',
155 "Output a certification request object"},
156 {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"},
157 {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"},
158 {"CAkey", OPT_CAKEY, 's',
159 "The CA key, must be PEM format; if not in CAfile"},
160 {"CAcreateserial", OPT_CACREATESERIAL, '-',
161 "Create serial number file if it does not exist"},
162 {"CAserial", OPT_CASERIAL, 's', "Serial file"},
163 {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
164 {"text", OPT_TEXT, '-', "Print the certificate in text form"},
165 {"C", OPT_C, '-', "Print out C code forms"},
166 {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"},
167 {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"},
168 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
169 {"certopt", OPT_CERTOPT, 's', "Various certificate text options"},
170 {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
171 {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
172 {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
173 {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"},
174 {"CAkeyform", OPT_CAKEYFORM, 'F', "CA key format - default PEM"},
175 {"sigopt", OPT_SIGOPT, 's'},
176 {"force_pubkey", OPT_FORCE_PUBKEY, '<'},
177 {"next_serial", OPT_NEXT_SERIAL, '-'},
178 {"clrreject", OPT_CLRREJECT, '-'},
179 {"badsig", OPT_BADSIG, '-'},
180 {"", OPT_MD, '-', "Any supported digest"},
181 #ifndef OPENSSL_NO_MD5
182 {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
183 "Print old-style (MD5) issuer hash value"},
184 {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
185 "Print old-style (MD5) subject hash value"},
186 #endif
187 #ifndef OPENSSL_NO_ENGINE
188 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
189 #endif
190 {NULL}
191 };
192
193 int x509_main(int argc, char **argv)
194 {
195 ASN1_INTEGER *sno = NULL;
196 ASN1_OBJECT *objtmp;
197 BIO *out = NULL;
198 CONF *extconf = NULL;
199 EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;
200 STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
201 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
202 X509 *x = NULL, *xca = NULL;
203 X509_REQ *req = NULL, *rq = NULL;
204 X509_STORE *ctx = NULL;
205 const EVP_MD *digest = NULL;
206 char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;
207 char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
208 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
209 char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
210 char buf[256], *prog;
211 int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;
212 int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;
213 int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;
214 int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
215 int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
216 int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
217 int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
218 int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;
219 int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;
220 int enddate = 0;
221 time_t checkoffset = 0;
222 unsigned long nmflag = 0, certflag = 0;
223 char nmflag_set = 0;
224 OPTION_CHOICE o;
225 ENGINE *e = NULL;
226 #ifndef OPENSSL_NO_MD5
227 int subject_hash_old = 0, issuer_hash_old = 0;
228 #endif
229
230 ctx = X509_STORE_new();
231 if (ctx == NULL)
232 goto end;
233 X509_STORE_set_verify_cb(ctx, callb);
234
235 prog = opt_init(argc, argv, x509_options);
236 while ((o = opt_next()) != OPT_EOF) {
237 switch (o) {
238 case OPT_EOF:
239 case OPT_ERR:
240 opthelp:
241 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
242 goto end;
243 case OPT_HELP:
244 opt_help(x509_options);
245 ret = 0;
246 goto end;
247 case OPT_INFORM:
248 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
249 goto opthelp;
250 break;
251 case OPT_IN:
252 infile = opt_arg();
253 break;
254 case OPT_OUTFORM:
255 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
256 goto opthelp;
257 break;
258 case OPT_KEYFORM:
259 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
260 goto opthelp;
261 break;
262 case OPT_CAFORM:
263 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
264 goto opthelp;
265 break;
266 case OPT_CAKEYFORM:
267 if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
268 goto opthelp;
269 break;
270 case OPT_OUT:
271 outfile = opt_arg();
272 break;
273 case OPT_REQ:
274 reqfile = need_rand = 1;
275 break;
276
277 case OPT_SIGOPT:
278 if (!sigopts)
279 sigopts = sk_OPENSSL_STRING_new_null();
280 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
281 goto opthelp;
282 break;
283 case OPT_DAYS:
284 days = atoi(opt_arg());
285 break;
286 case OPT_PASSIN:
287 passinarg = opt_arg();
288 break;
289 case OPT_EXTFILE:
290 extfile = opt_arg();
291 break;
292 case OPT_EXTENSIONS:
293 extsect = opt_arg();
294 break;
295 case OPT_SIGNKEY:
296 keyfile = opt_arg();
297 sign_flag = ++num;
298 need_rand = 1;
299 break;
300 case OPT_CA:
301 CAfile = opt_arg();
302 CA_flag = ++num;
303 need_rand = 1;
304 break;
305 case OPT_CAKEY:
306 CAkeyfile = opt_arg();
307 break;
308 case OPT_CASERIAL:
309 CAserial = opt_arg();
310 break;
311 case OPT_SET_SERIAL:
312 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
313 goto opthelp;
314 break;
315 case OPT_FORCE_PUBKEY:
316 fkeyfile = opt_arg();
317 break;
318 case OPT_ADDTRUST:
319 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
320 BIO_printf(bio_err,
321 "%s: Invalid trust object value %s\n",
322 prog, opt_arg());
323 goto opthelp;
324 }
325 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
326 goto end;
327 sk_ASN1_OBJECT_push(trust, objtmp);
328 trustout = 1;
329 break;
330 case OPT_ADDREJECT:
331 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
332 BIO_printf(bio_err,
333 "%s: Invalid reject object value %s\n",
334 prog, opt_arg());
335 goto opthelp;
336 }
337 if (reject == NULL
338 && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
339 goto end;
340 sk_ASN1_OBJECT_push(reject, objtmp);
341 trustout = 1;
342 break;
343 case OPT_SETALIAS:
344 alias = opt_arg();
345 trustout = 1;
346 break;
347 case OPT_CERTOPT:
348 if (!set_cert_ex(&certflag, opt_arg()))
349 goto opthelp;
350 break;
351 case OPT_NAMEOPT:
352 nmflag_set = 1;
353 if (!set_name_ex(&nmflag, opt_arg()))
354 goto opthelp;
355 break;
356 case OPT_ENGINE:
357 e = setup_engine(opt_arg(), 0);
358 break;
359 case OPT_C:
360 C = ++num;
361 break;
362 case OPT_EMAIL:
363 email = ++num;
364 break;
365 case OPT_OCSP_URI:
366 ocsp_uri = ++num;
367 break;
368 case OPT_SERIAL:
369 serial = ++num;
370 break;
371 case OPT_NEXT_SERIAL:
372 next_serial = ++num;
373 break;
374 case OPT_MODULUS:
375 modulus = ++num;
376 break;
377 case OPT_PUBKEY:
378 pubkey = ++num;
379 break;
380 case OPT_X509TOREQ:
381 x509req = ++num;
382 break;
383 case OPT_TEXT:
384 text = ++num;
385 break;
386 case OPT_SUBJECT:
387 subject = ++num;
388 break;
389 case OPT_ISSUER:
390 issuer = ++num;
391 break;
392 case OPT_FINGERPRINT:
393 fingerprint = ++num;
394 break;
395 case OPT_HASH:
396 subject_hash = ++num;
397 break;
398 case OPT_ISSUER_HASH:
399 issuer_hash = ++num;
400 break;
401 case OPT_PURPOSE:
402 pprint = ++num;
403 break;
404 case OPT_STARTDATE:
405 startdate = ++num;
406 break;
407 case OPT_ENDDATE:
408 enddate = ++num;
409 break;
410 case OPT_NOOUT:
411 noout = ++num;
412 break;
413 case OPT_NOCERT:
414 nocert = 1;
415 break;
416 case OPT_TRUSTOUT:
417 trustout = 1;
418 break;
419 case OPT_CLRTRUST:
420 clrtrust = ++num;
421 break;
422 case OPT_CLRREJECT:
423 clrreject = ++num;
424 break;
425 case OPT_ALIAS:
426 aliasout = ++num;
427 break;
428 case OPT_CACREATESERIAL:
429 CA_createserial = ++num;
430 break;
431 case OPT_CLREXT:
432 clrext = 1;
433 break;
434 case OPT_OCSPID:
435 ocspid = ++num;
436 break;
437 case OPT_BADSIG:
438 badsig = 1;
439 break;
440 #ifndef OPENSSL_NO_MD5
441 case OPT_SUBJECT_HASH_OLD:
442 subject_hash_old = ++num;
443 break;
444 case OPT_ISSUER_HASH_OLD:
445 issuer_hash_old = ++num;
446 break;
447 #else
448 case OPT_SUBJECT_HASH_OLD:
449 case OPT_ISSUER_HASH_OLD:
450 break;
451 #endif
452 case OPT_DATES:
453 startdate = ++num;
454 enddate = ++num;
455 break;
456 case OPT_CHECKEND:
457 checkend = 1;
458 {
459 intmax_t temp = 0;
460 if (!opt_imax(opt_arg(), &temp))
461 goto opthelp;
462 checkoffset = (time_t)temp;
463 if ((intmax_t)checkoffset != temp) {
464 BIO_printf(bio_err, "%s: checkend time out of range %s\n",
465 prog, opt_arg());
466 goto opthelp;
467 }
468 }
469 break;
470 case OPT_CHECKHOST:
471 checkhost = opt_arg();
472 break;
473 case OPT_CHECKEMAIL:
474 checkemail = opt_arg();
475 break;
476 case OPT_CHECKIP:
477 checkip = opt_arg();
478 break;
479 case OPT_MD:
480 if (!opt_md(opt_unknown(), &digest))
481 goto opthelp;
482 }
483 }
484 argc = opt_num_rest();
485 argv = opt_rest();
486 if (argc != 0) {
487 BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
488 goto opthelp;
489 }
490
491 if (!nmflag_set)
492 nmflag = XN_FLAG_ONELINE;
493
494 out = bio_open_default(outfile, 'w', outformat);
495 if (out == NULL)
496 goto end;
497
498 if (need_rand)
499 app_RAND_load_file(NULL, 0);
500
501 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
502 BIO_printf(bio_err, "Error getting password\n");
503 goto end;
504 }
505
506 if (!X509_STORE_set_default_paths(ctx)) {
507 ERR_print_errors(bio_err);
508 goto end;
509 }
510
511 if (fkeyfile) {
512 fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
513 if (fkey == NULL)
514 goto end;
515 }
516
517 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
518 CAkeyfile = CAfile;
519 } else if ((CA_flag) && (CAkeyfile == NULL)) {
520 BIO_printf(bio_err,
521 "need to specify a CAkey if using the CA command\n");
522 goto end;
523 }
524
525 if (extfile) {
526 X509V3_CTX ctx2;
527 if ((extconf = app_load_config(extfile)) == NULL)
528 goto end;
529 if (!extsect) {
530 extsect = NCONF_get_string(extconf, "default", "extensions");
531 if (!extsect) {
532 ERR_clear_error();
533 extsect = "default";
534 }
535 }
536 X509V3_set_ctx_test(&ctx2);
537 X509V3_set_nconf(&ctx2, extconf);
538 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
539 BIO_printf(bio_err,
540 "Error Loading extension section %s\n", extsect);
541 ERR_print_errors(bio_err);
542 goto end;
543 }
544 }
545
546 if (reqfile) {
547 EVP_PKEY *pkey;
548 BIO *in;
549
550 if (!sign_flag && !CA_flag) {
551 BIO_printf(bio_err, "We need a private key to sign with\n");
552 goto end;
553 }
554 in = bio_open_default(infile, 'r', informat);
555 if (in == NULL)
556 goto end;
557 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
558 BIO_free(in);
559
560 if (req == NULL) {
561 ERR_print_errors(bio_err);
562 goto end;
563 }
564
565 if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
566 BIO_printf(bio_err, "error unpacking public key\n");
567 goto end;
568 }
569 i = X509_REQ_verify(req, pkey);
570 EVP_PKEY_free(pkey);
571 if (i < 0) {
572 BIO_printf(bio_err, "Signature verification error\n");
573 ERR_print_errors(bio_err);
574 goto end;
575 }
576 if (i == 0) {
577 BIO_printf(bio_err,
578 "Signature did not match the certificate request\n");
579 goto end;
580 } else
581 BIO_printf(bio_err, "Signature ok\n");
582
583 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
584 nmflag);
585
586 if ((x = X509_new()) == NULL)
587 goto end;
588
589 if (sno == NULL) {
590 sno = ASN1_INTEGER_new();
591 if (sno == NULL || !rand_serial(NULL, sno))
592 goto end;
593 if (!X509_set_serialNumber(x, sno))
594 goto end;
595 ASN1_INTEGER_free(sno);
596 sno = NULL;
597 } else if (!X509_set_serialNumber(x, sno))
598 goto end;
599
600 if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))
601 goto end;
602 if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))
603 goto end;
604
605 X509_gmtime_adj(X509_get_notBefore(x), 0);
606 X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
607 if (fkey)
608 X509_set_pubkey(x, fkey);
609 else {
610 pkey = X509_REQ_get_pubkey(req);
611 X509_set_pubkey(x, pkey);
612 EVP_PKEY_free(pkey);
613 }
614 } else
615 x = load_cert(infile, informat, "Certificate");
616
617 if (x == NULL)
618 goto end;
619 if (CA_flag) {
620 xca = load_cert(CAfile, CAformat, "CA Certificate");
621 if (xca == NULL)
622 goto end;
623 }
624
625 if (!noout || text || next_serial) {
626 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
627
628 }
629
630 if (alias)
631 X509_alias_set1(x, (unsigned char *)alias, -1);
632
633 if (clrtrust)
634 X509_trust_clear(x);
635 if (clrreject)
636 X509_reject_clear(x);
637
638 if (trust) {
639 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
640 objtmp = sk_ASN1_OBJECT_value(trust, i);
641 X509_add1_trust_object(x, objtmp);
642 }
643 }
644
645 if (reject) {
646 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
647 objtmp = sk_ASN1_OBJECT_value(reject, i);
648 X509_add1_reject_object(x, objtmp);
649 }
650 }
651
652 if (num) {
653 for (i = 1; i <= num; i++) {
654 if (issuer == i) {
655 print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);
656 } else if (subject == i) {
657 print_name(out, "subject= ",
658 X509_get_subject_name(x), nmflag);
659 } else if (serial == i) {
660 BIO_printf(out, "serial=");
661 i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));
662 BIO_printf(out, "\n");
663 } else if (next_serial == i) {
664 BIGNUM *bnser;
665 ASN1_INTEGER *ser;
666 ser = X509_get_serialNumber(x);
667 bnser = ASN1_INTEGER_to_BN(ser, NULL);
668 if (!bnser)
669 goto end;
670 if (!BN_add_word(bnser, 1))
671 goto end;
672 ser = BN_to_ASN1_INTEGER(bnser, NULL);
673 if (!ser)
674 goto end;
675 BN_free(bnser);
676 i2a_ASN1_INTEGER(out, ser);
677 ASN1_INTEGER_free(ser);
678 BIO_puts(out, "\n");
679 } else if ((email == i) || (ocsp_uri == i)) {
680 int j;
681 STACK_OF(OPENSSL_STRING) *emlst;
682 if (email == i)
683 emlst = X509_get1_email(x);
684 else
685 emlst = X509_get1_ocsp(x);
686 for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
687 BIO_printf(out, "%s\n",
688 sk_OPENSSL_STRING_value(emlst, j));
689 X509_email_free(emlst);
690 } else if (aliasout == i) {
691 unsigned char *alstr;
692 alstr = X509_alias_get0(x, NULL);
693 if (alstr)
694 BIO_printf(out, "%s\n", alstr);
695 else
696 BIO_puts(out, "<No Alias>\n");
697 } else if (subject_hash == i) {
698 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
699 }
700 #ifndef OPENSSL_NO_MD5
701 else if (subject_hash_old == i) {
702 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
703 }
704 #endif
705 else if (issuer_hash == i) {
706 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
707 }
708 #ifndef OPENSSL_NO_MD5
709 else if (issuer_hash_old == i) {
710 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
711 }
712 #endif
713 else if (pprint == i) {
714 X509_PURPOSE *ptmp;
715 int j;
716 BIO_printf(out, "Certificate purposes:\n");
717 for (j = 0; j < X509_PURPOSE_get_count(); j++) {
718 ptmp = X509_PURPOSE_get0(j);
719 purpose_print(out, x, ptmp);
720 }
721 } else if (modulus == i) {
722 EVP_PKEY *pkey;
723
724 pkey = X509_get0_pubkey(x);
725 if (pkey == NULL) {
726 BIO_printf(bio_err, "Modulus=unavailable\n");
727 ERR_print_errors(bio_err);
728 goto end;
729 }
730 BIO_printf(out, "Modulus=");
731 #ifndef OPENSSL_NO_RSA
732 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
733 BN_print(out, EVP_PKEY_get0_RSA(pkey)->n);
734 else
735 #endif
736 #ifndef OPENSSL_NO_DSA
737 if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA)
738 BN_print(out, DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey)));
739 else
740 #endif
741 BIO_printf(out, "Wrong Algorithm type");
742 BIO_printf(out, "\n");
743 } else if (pubkey == i) {
744 EVP_PKEY *pkey;
745
746 pkey = X509_get0_pubkey(x);
747 if (pkey == NULL) {
748 BIO_printf(bio_err, "Error getting public key\n");
749 ERR_print_errors(bio_err);
750 goto end;
751 }
752 PEM_write_bio_PUBKEY(out, pkey);
753 } else if (C == i) {
754 unsigned char *d;
755 char *m;
756 int len;
757
758 X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);
759 BIO_printf(out, "/*\n"
760 " * Subject: %s\n", buf);
761
762 m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);
763 BIO_printf(out, " * Issuer: %s\n"
764 " */\n", buf);
765
766 len = i2d_X509(x, NULL);
767 m = app_malloc(len, "x509 name buffer");
768 d = (unsigned char *)m;
769 len = i2d_X509_NAME(X509_get_subject_name(x), &d);
770 print_array(out, "the_subject_name", len, (unsigned char *)m);
771 d = (unsigned char *)m;
772 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
773 print_array(out, "the_public_key", len, (unsigned char *)m);
774 d = (unsigned char *)m;
775 len = i2d_X509(x, &d);
776 print_array(out, "the_certificate", len, (unsigned char *)m);
777 OPENSSL_free(m);
778 } else if (text == i) {
779 X509_print_ex(out, x, nmflag, certflag);
780 } else if (startdate == i) {
781 BIO_puts(out, "notBefore=");
782 ASN1_TIME_print(out, X509_get_notBefore(x));
783 BIO_puts(out, "\n");
784 } else if (enddate == i) {
785 BIO_puts(out, "notAfter=");
786 ASN1_TIME_print(out, X509_get_notAfter(x));
787 BIO_puts(out, "\n");
788 } else if (fingerprint == i) {
789 int j;
790 unsigned int n;
791 unsigned char md[EVP_MAX_MD_SIZE];
792 const EVP_MD *fdig = digest;
793
794 if (!fdig)
795 fdig = EVP_sha1();
796
797 if (!X509_digest(x, fdig, md, &n)) {
798 BIO_printf(bio_err, "out of memory\n");
799 goto end;
800 }
801 BIO_printf(out, "%s Fingerprint=",
802 OBJ_nid2sn(EVP_MD_type(fdig)));
803 for (j = 0; j < (int)n; j++) {
804 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
805 ? '\n' : ':');
806 }
807 }
808
809 /* should be in the library */
810 else if ((sign_flag == i) && (x509req == 0)) {
811 BIO_printf(bio_err, "Getting Private key\n");
812 if (Upkey == NULL) {
813 Upkey = load_key(keyfile, keyformat, 0,
814 passin, e, "Private key");
815 if (Upkey == NULL)
816 goto end;
817 }
818
819 assert(need_rand);
820 if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))
821 goto end;
822 } else if (CA_flag == i) {
823 BIO_printf(bio_err, "Getting CA Private Key\n");
824 if (CAkeyfile != NULL) {
825 CApkey = load_key(CAkeyfile, CAkeyformat,
826 0, passin, e, "CA Private Key");
827 if (CApkey == NULL)
828 goto end;
829 }
830
831 assert(need_rand);
832 if (!x509_certify(ctx, CAfile, digest, x, xca,
833 CApkey, sigopts,
834 CAserial, CA_createserial, days, clrext,
835 extconf, extsect, sno, reqfile))
836 goto end;
837 } else if (x509req == i) {
838 EVP_PKEY *pk;
839
840 BIO_printf(bio_err, "Getting request Private Key\n");
841 if (keyfile == NULL) {
842 BIO_printf(bio_err, "no request key file specified\n");
843 goto end;
844 } else {
845 pk = load_key(keyfile, keyformat, 0,
846 passin, e, "request key");
847 if (pk == NULL)
848 goto end;
849 }
850
851 BIO_printf(bio_err, "Generating certificate request\n");
852
853 rq = X509_to_X509_REQ(x, pk, digest);
854 EVP_PKEY_free(pk);
855 if (rq == NULL) {
856 ERR_print_errors(bio_err);
857 goto end;
858 }
859 if (!noout) {
860 X509_REQ_print(out, rq);
861 PEM_write_bio_X509_REQ(out, rq);
862 }
863 noout = 1;
864 } else if (ocspid == i) {
865 X509_ocspid_print(out, x);
866 }
867 }
868 }
869
870 if (checkend) {
871 time_t tcheck = time(NULL) + checkoffset;
872
873 if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
874 BIO_printf(out, "Certificate will expire\n");
875 ret = 1;
876 } else {
877 BIO_printf(out, "Certificate will not expire\n");
878 ret = 0;
879 }
880 goto end;
881 }
882
883 print_cert_checks(out, x, checkhost, checkemail, checkip);
884
885 if (noout || nocert) {
886 ret = 0;
887 goto end;
888 }
889
890 if (badsig) {
891 ASN1_BIT_STRING *signature;
892 unsigned char *s;
893 X509_get0_signature(&signature, NULL, x);
894 s = ASN1_STRING_data(signature);
895 s[ASN1_STRING_length(signature) - 1] ^= 0x1;
896 }
897
898 if (outformat == FORMAT_ASN1)
899 i = i2d_X509_bio(out, x);
900 else if (outformat == FORMAT_PEM) {
901 if (trustout)
902 i = PEM_write_bio_X509_AUX(out, x);
903 else
904 i = PEM_write_bio_X509(out, x);
905 } else {
906 BIO_printf(bio_err, "bad output format specified for outfile\n");
907 goto end;
908 }
909 if (!i) {
910 BIO_printf(bio_err, "unable to write certificate\n");
911 ERR_print_errors(bio_err);
912 goto end;
913 }
914 ret = 0;
915 end:
916 if (need_rand)
917 app_RAND_write_file(NULL);
918 OBJ_cleanup();
919 NCONF_free(extconf);
920 BIO_free_all(out);
921 X509_STORE_free(ctx);
922 X509_REQ_free(req);
923 X509_free(x);
924 X509_free(xca);
925 EVP_PKEY_free(Upkey);
926 EVP_PKEY_free(CApkey);
927 EVP_PKEY_free(fkey);
928 sk_OPENSSL_STRING_free(sigopts);
929 X509_REQ_free(rq);
930 ASN1_INTEGER_free(sno);
931 sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
932 sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
933 OPENSSL_free(passin);
934 return (ret);
935 }
936
937 static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
938 int create)
939 {
940 char *buf = NULL, *p;
941 ASN1_INTEGER *bs = NULL;
942 BIGNUM *serial = NULL;
943 size_t len;
944
945 len = ((serialfile == NULL)
946 ? (strlen(CAfile) + strlen(POSTFIX) + 1)
947 : (strlen(serialfile))) + 1;
948 buf = app_malloc(len, "serial# buffer");
949 if (serialfile == NULL) {
950 OPENSSL_strlcpy(buf, CAfile, len);
951 for (p = buf; *p; p++)
952 if (*p == '.') {
953 *p = '\0';
954 break;
955 }
956 OPENSSL_strlcat(buf, POSTFIX, len);
957 } else
958 OPENSSL_strlcpy(buf, serialfile, len);
959
960 serial = load_serial(buf, create, NULL);
961 if (serial == NULL)
962 goto end;
963
964 if (!BN_add_word(serial, 1)) {
965 BIO_printf(bio_err, "add_word failure\n");
966 goto end;
967 }
968
969 if (!save_serial(buf, NULL, serial, &bs))
970 goto end;
971
972 end:
973 OPENSSL_free(buf);
974 BN_free(serial);
975 return bs;
976 }
977
978 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
979 X509 *x, X509 *xca, EVP_PKEY *pkey,
980 STACK_OF(OPENSSL_STRING) *sigopts,
981 char *serialfile, int create,
982 int days, int clrext, CONF *conf, char *section,
983 ASN1_INTEGER *sno, int reqfile)
984 {
985 int ret = 0;
986 ASN1_INTEGER *bs = NULL;
987 X509_STORE_CTX xsc;
988 EVP_PKEY *upkey;
989
990 upkey = X509_get0_pubkey(xca);
991 EVP_PKEY_copy_parameters(upkey, pkey);
992
993 if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
994 BIO_printf(bio_err, "Error initialising X509 store\n");
995 goto end;
996 }
997 if (sno)
998 bs = sno;
999 else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
1000 goto end;
1001
1002 /*
1003 * NOTE: this certificate can/should be self signed, unless it was a
1004 * certificate request in which case it is not.
1005 */
1006 X509_STORE_CTX_set_cert(&xsc, x);
1007 X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
1008 if (!reqfile && X509_verify_cert(&xsc) <= 0)
1009 goto end;
1010
1011 if (!X509_check_private_key(xca, pkey)) {
1012 BIO_printf(bio_err,
1013 "CA certificate and CA private key do not match\n");
1014 goto end;
1015 }
1016
1017 if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
1018 goto end;
1019 if (!X509_set_serialNumber(x, bs))
1020 goto end;
1021
1022 if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
1023 goto end;
1024
1025 /* hardwired expired */
1026 if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
1027 goto end;
1028
1029 if (clrext) {
1030 while (X509_get_ext_count(x) > 0)
1031 X509_delete_ext(x, 0);
1032 }
1033
1034 if (conf) {
1035 X509V3_CTX ctx2;
1036 X509_set_version(x, 2); /* version 3 certificate */
1037 X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
1038 X509V3_set_nconf(&ctx2, conf);
1039 if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
1040 goto end;
1041 }
1042
1043 if (!do_X509_sign(x, pkey, digest, sigopts))
1044 goto end;
1045 ret = 1;
1046 end:
1047 X509_STORE_CTX_cleanup(&xsc);
1048 if (!ret)
1049 ERR_print_errors(bio_err);
1050 if (!sno)
1051 ASN1_INTEGER_free(bs);
1052 return ret;
1053 }
1054
1055 static int callb(int ok, X509_STORE_CTX *ctx)
1056 {
1057 int err;
1058 X509 *err_cert;
1059
1060 /*
1061 * it is ok to use a self signed certificate This case will catch both
1062 * the initial ok == 0 and the final ok == 1 calls to this function
1063 */
1064 err = X509_STORE_CTX_get_error(ctx);
1065 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1066 return 1;
1067
1068 /*
1069 * BAD we should have gotten an error. Normally if everything worked
1070 * X509_STORE_CTX_get_error(ctx) will still be set to
1071 * DEPTH_ZERO_SELF_....
1072 */
1073 if (ok) {
1074 BIO_printf(bio_err,
1075 "error with certificate to be certified - should be self signed\n");
1076 return 0;
1077 } else {
1078 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1079 print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
1080 BIO_printf(bio_err,
1081 "error with certificate - error %d at depth %d\n%s\n", err,
1082 X509_STORE_CTX_get_error_depth(ctx),
1083 X509_verify_cert_error_string(err));
1084 return 1;
1085 }
1086 }
1087
1088 /* self sign */
1089 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
1090 const EVP_MD *digest, CONF *conf, char *section)
1091 {
1092
1093 if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
1094 goto err;
1095 if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
1096 goto err;
1097
1098 if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
1099 goto err;
1100
1101 if (!X509_set_pubkey(x, pkey))
1102 goto err;
1103 if (clrext) {
1104 while (X509_get_ext_count(x) > 0)
1105 X509_delete_ext(x, 0);
1106 }
1107 if (conf) {
1108 X509V3_CTX ctx;
1109 X509_set_version(x, 2); /* version 3 certificate */
1110 X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
1111 X509V3_set_nconf(&ctx, conf);
1112 if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
1113 goto err;
1114 }
1115 if (!X509_sign(x, pkey, digest))
1116 goto err;
1117 return 1;
1118 err:
1119 ERR_print_errors(bio_err);
1120 return 0;
1121 }
1122
1123 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1124 {
1125 int id, i, idret;
1126 char *pname;
1127 id = X509_PURPOSE_get_id(pt);
1128 pname = X509_PURPOSE_get0_name(pt);
1129 for (i = 0; i < 2; i++) {
1130 idret = X509_check_purpose(cert, id, i);
1131 BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
1132 if (idret == 1)
1133 BIO_printf(bio, "Yes\n");
1134 else if (idret == 0)
1135 BIO_printf(bio, "No\n");
1136 else
1137 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
1138 }
1139 return 1;
1140 }