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