x509.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* apps/x509.c */
  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. #include <assert.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #ifdef OPENSSL_NO_STDIO
  63. # define APPS_WIN16
  64. #endif
  65. #include "apps.h"
  66. #include <openssl/bio.h>
  67. #include <openssl/asn1.h>
  68. #include <openssl/err.h>
  69. #include <openssl/bn.h>
  70. #include <openssl/evp.h>
  71. #include <openssl/x509.h>
  72. #include <openssl/x509v3.h>
  73. #include <openssl/objects.h>
  74. #include <openssl/pem.h>
  75. #ifndef OPENSSL_NO_RSA
  76. # include <openssl/rsa.h>
  77. #endif
  78. #ifndef OPENSSL_NO_DSA
  79. # include <openssl/dsa.h>
  80. #endif
  81. #undef PROG
  82. #define PROG x509_main
  83. #undef POSTFIX
  84. #define POSTFIX ".srl"
  85. #define DEF_DAYS 30
  86. static const char *x509_usage[] = {
  87. "usage: x509 args\n",
  88. " -inform arg - input format - default PEM (one of DER, NET or PEM)\n",
  89. " -outform arg - output format - default PEM (one of DER, NET or PEM)\n",
  90. " -keyform arg - private key format - default PEM\n",
  91. " -CAform arg - CA format - default PEM\n",
  92. " -CAkeyform arg - CA key format - default PEM\n",
  93. " -in arg - input file - default stdin\n",
  94. " -out arg - output file - default stdout\n",
  95. " -passin arg - private key password source\n",
  96. " -serial - print serial number value\n",
  97. " -subject_hash - print subject hash value\n",
  98. " -issuer_hash - print issuer hash value\n",
  99. " -hash - synonym for -subject_hash\n",
  100. " -subject - print subject DN\n",
  101. " -issuer - print issuer DN\n",
  102. " -email - print email address(es)\n",
  103. " -startdate - notBefore field\n",
  104. " -enddate - notAfter field\n",
  105. " -purpose - print out certificate purposes\n",
  106. " -dates - both Before and After dates\n",
  107. " -modulus - print the RSA key modulus\n",
  108. " -pubkey - output the public key\n",
  109. " -fingerprint - print the certificate fingerprint\n",
  110. " -alias - output certificate alias\n",
  111. " -noout - no certificate output\n",
  112. " -ocspid - print OCSP hash values for the subject name and public key\n",
  113. " -ocsp_uri - print OCSP Responder URL(s)\n",
  114. " -trustout - output a \"trusted\" certificate\n",
  115. " -clrtrust - clear all trusted purposes\n",
  116. " -clrreject - clear all rejected purposes\n",
  117. " -addtrust arg - trust certificate for a given purpose\n",
  118. " -addreject arg - reject certificate for a given purpose\n",
  119. " -setalias arg - set certificate alias\n",
  120. " -days arg - How long till expiry of a signed certificate - def 30 days\n",
  121. " -checkend arg - check whether the cert expires in the next arg seconds\n",
  122. " exit 1 if so, 0 if not\n",
  123. " -signkey arg - self sign cert with arg\n",
  124. " -x509toreq - output a certification request object\n",
  125. " -req - input is a certificate request, sign and output.\n",
  126. " -CA arg - set the CA certificate, must be PEM format.\n",
  127. " -CAkey arg - set the CA key, must be PEM format\n",
  128. " missing, it is assumed to be in the CA file.\n",
  129. " -CAcreateserial - create serial number file if it does not exist\n",
  130. " -CAserial arg - serial file\n",
  131. " -set_serial - serial number to use\n",
  132. " -text - print the certificate in text form\n",
  133. " -C - print out C code forms\n",
  134. " -md2/-md5/-sha1/-mdc2 - digest to use\n",
  135. " -extfile - configuration file with X509V3 extensions to add\n",
  136. " -extensions - section from config file with X509V3 extensions to add\n",
  137. " -clrext - delete extensions before signing and input certificate\n",
  138. " -nameopt arg - various certificate name options\n",
  139. #ifndef OPENSSL_NO_ENGINE
  140. " -engine e - use engine e, possibly a hardware device.\n",
  141. #endif
  142. " -certopt arg - various certificate text options\n",
  143. NULL
  144. };
  145. static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx);
  146. static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
  147. const EVP_MD *digest, CONF *conf, char *section);
  148. static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
  149. X509 *x, X509 *xca, EVP_PKEY *pkey, char *serial,
  150. int create, int days, int clrext, CONF *conf,
  151. char *section, ASN1_INTEGER *sno);
  152. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
  153. static int reqfile = 0;
  154. int MAIN(int, char **);
  155. int MAIN(int argc, char **argv)
  156. {
  157. ENGINE *e = NULL;
  158. int ret = 1;
  159. X509_REQ *req = NULL;
  160. X509 *x = NULL, *xca = NULL;
  161. ASN1_OBJECT *objtmp;
  162. EVP_PKEY *Upkey = NULL, *CApkey = NULL;
  163. ASN1_INTEGER *sno = NULL;
  164. int i, num, badops = 0;
  165. BIO *out = NULL;
  166. BIO *STDout = NULL;
  167. STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
  168. int informat, outformat, keyformat, CAformat, CAkeyformat;
  169. char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
  170. char *CAkeyfile = NULL, *CAserial = NULL;
  171. char *alias = NULL;
  172. int text = 0, serial = 0, subject = 0, issuer = 0, startdate =
  173. 0, enddate = 0;
  174. int next_serial = 0;
  175. int subject_hash = 0, issuer_hash = 0, ocspid = 0;
  176. int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
  177. int ocsp_uri = 0;
  178. int trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0, clrext = 0;
  179. int C = 0;
  180. int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0;
  181. int pprint = 0;
  182. const char **pp;
  183. X509_STORE *ctx = NULL;
  184. X509_REQ *rq = NULL;
  185. int fingerprint = 0;
  186. char buf[256];
  187. const EVP_MD *md_alg, *digest = EVP_sha1();
  188. CONF *extconf = NULL;
  189. char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
  190. int need_rand = 0;
  191. int checkend = 0, checkoffset = 0;
  192. unsigned long nmflag = 0, certflag = 0;
  193. #ifndef OPENSSL_NO_ENGINE
  194. char *engine = NULL;
  195. #endif
  196. reqfile = 0;
  197. apps_startup();
  198. if (bio_err == NULL)
  199. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  200. if (!load_config(bio_err, NULL))
  201. goto end;
  202. STDout = BIO_new_fp(stdout, BIO_NOCLOSE);
  203. #ifdef OPENSSL_SYS_VMS
  204. {
  205. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  206. STDout = BIO_push(tmpbio, STDout);
  207. }
  208. #endif
  209. informat = FORMAT_PEM;
  210. outformat = FORMAT_PEM;
  211. keyformat = FORMAT_PEM;
  212. CAformat = FORMAT_PEM;
  213. CAkeyformat = FORMAT_PEM;
  214. ctx = X509_STORE_new();
  215. if (ctx == NULL)
  216. goto end;
  217. X509_STORE_set_verify_cb_func(ctx, callb);
  218. argc--;
  219. argv++;
  220. num = 0;
  221. while (argc >= 1) {
  222. if (strcmp(*argv, "-inform") == 0) {
  223. if (--argc < 1)
  224. goto bad;
  225. informat = str2fmt(*(++argv));
  226. } else if (strcmp(*argv, "-outform") == 0) {
  227. if (--argc < 1)
  228. goto bad;
  229. outformat = str2fmt(*(++argv));
  230. } else if (strcmp(*argv, "-keyform") == 0) {
  231. if (--argc < 1)
  232. goto bad;
  233. keyformat = str2fmt(*(++argv));
  234. } else if (strcmp(*argv, "-req") == 0) {
  235. reqfile = 1;
  236. need_rand = 1;
  237. } else if (strcmp(*argv, "-CAform") == 0) {
  238. if (--argc < 1)
  239. goto bad;
  240. CAformat = str2fmt(*(++argv));
  241. } else if (strcmp(*argv, "-CAkeyform") == 0) {
  242. if (--argc < 1)
  243. goto bad;
  244. CAkeyformat = str2fmt(*(++argv));
  245. } else if (strcmp(*argv, "-days") == 0) {
  246. if (--argc < 1)
  247. goto bad;
  248. days = atoi(*(++argv));
  249. if (days == 0) {
  250. BIO_printf(STDout, "bad number of days\n");
  251. goto bad;
  252. }
  253. } else if (strcmp(*argv, "-passin") == 0) {
  254. if (--argc < 1)
  255. goto bad;
  256. passargin = *(++argv);
  257. } else if (strcmp(*argv, "-extfile") == 0) {
  258. if (--argc < 1)
  259. goto bad;
  260. extfile = *(++argv);
  261. } else if (strcmp(*argv, "-extensions") == 0) {
  262. if (--argc < 1)
  263. goto bad;
  264. extsect = *(++argv);
  265. } else if (strcmp(*argv, "-in") == 0) {
  266. if (--argc < 1)
  267. goto bad;
  268. infile = *(++argv);
  269. } else if (strcmp(*argv, "-out") == 0) {
  270. if (--argc < 1)
  271. goto bad;
  272. outfile = *(++argv);
  273. } else if (strcmp(*argv, "-signkey") == 0) {
  274. if (--argc < 1)
  275. goto bad;
  276. keyfile = *(++argv);
  277. sign_flag = ++num;
  278. need_rand = 1;
  279. } else if (strcmp(*argv, "-CA") == 0) {
  280. if (--argc < 1)
  281. goto bad;
  282. CAfile = *(++argv);
  283. CA_flag = ++num;
  284. need_rand = 1;
  285. } else if (strcmp(*argv, "-CAkey") == 0) {
  286. if (--argc < 1)
  287. goto bad;
  288. CAkeyfile = *(++argv);
  289. } else if (strcmp(*argv, "-CAserial") == 0) {
  290. if (--argc < 1)
  291. goto bad;
  292. CAserial = *(++argv);
  293. } else if (strcmp(*argv, "-set_serial") == 0) {
  294. if (--argc < 1)
  295. goto bad;
  296. if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv))))
  297. goto bad;
  298. } else if (strcmp(*argv, "-addtrust") == 0) {
  299. if (--argc < 1)
  300. goto bad;
  301. if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
  302. BIO_printf(bio_err, "Invalid trust object value %s\n", *argv);
  303. goto bad;
  304. }
  305. if (!trust)
  306. trust = sk_ASN1_OBJECT_new_null();
  307. sk_ASN1_OBJECT_push(trust, objtmp);
  308. trustout = 1;
  309. } else if (strcmp(*argv, "-addreject") == 0) {
  310. if (--argc < 1)
  311. goto bad;
  312. if (!(objtmp = OBJ_txt2obj(*(++argv), 0))) {
  313. BIO_printf(bio_err,
  314. "Invalid reject object value %s\n", *argv);
  315. goto bad;
  316. }
  317. if (!reject)
  318. reject = sk_ASN1_OBJECT_new_null();
  319. sk_ASN1_OBJECT_push(reject, objtmp);
  320. trustout = 1;
  321. } else if (strcmp(*argv, "-setalias") == 0) {
  322. if (--argc < 1)
  323. goto bad;
  324. alias = *(++argv);
  325. trustout = 1;
  326. } else if (strcmp(*argv, "-certopt") == 0) {
  327. if (--argc < 1)
  328. goto bad;
  329. if (!set_cert_ex(&certflag, *(++argv)))
  330. goto bad;
  331. } else if (strcmp(*argv, "-nameopt") == 0) {
  332. if (--argc < 1)
  333. goto bad;
  334. if (!set_name_ex(&nmflag, *(++argv)))
  335. goto bad;
  336. }
  337. #ifndef OPENSSL_NO_ENGINE
  338. else if (strcmp(*argv, "-engine") == 0) {
  339. if (--argc < 1)
  340. goto bad;
  341. engine = *(++argv);
  342. }
  343. #endif
  344. else if (strcmp(*argv, "-C") == 0)
  345. C = ++num;
  346. else if (strcmp(*argv, "-email") == 0)
  347. email = ++num;
  348. else if (strcmp(*argv, "-ocsp_uri") == 0)
  349. ocsp_uri = ++num;
  350. else if (strcmp(*argv, "-serial") == 0)
  351. serial = ++num;
  352. else if (strcmp(*argv, "-next_serial") == 0)
  353. next_serial = ++num;
  354. else if (strcmp(*argv, "-modulus") == 0)
  355. modulus = ++num;
  356. else if (strcmp(*argv, "-pubkey") == 0)
  357. pubkey = ++num;
  358. else if (strcmp(*argv, "-x509toreq") == 0)
  359. x509req = ++num;
  360. else if (strcmp(*argv, "-text") == 0)
  361. text = ++num;
  362. else if (strcmp(*argv, "-hash") == 0
  363. || strcmp(*argv, "-subject_hash") == 0)
  364. subject_hash = ++num;
  365. else if (strcmp(*argv, "-issuer_hash") == 0)
  366. issuer_hash = ++num;
  367. else if (strcmp(*argv, "-subject") == 0)
  368. subject = ++num;
  369. else if (strcmp(*argv, "-issuer") == 0)
  370. issuer = ++num;
  371. else if (strcmp(*argv, "-fingerprint") == 0)
  372. fingerprint = ++num;
  373. else if (strcmp(*argv, "-dates") == 0) {
  374. startdate = ++num;
  375. enddate = ++num;
  376. } else if (strcmp(*argv, "-purpose") == 0)
  377. pprint = ++num;
  378. else if (strcmp(*argv, "-startdate") == 0)
  379. startdate = ++num;
  380. else if (strcmp(*argv, "-enddate") == 0)
  381. enddate = ++num;
  382. else if (strcmp(*argv, "-checkend") == 0) {
  383. if (--argc < 1)
  384. goto bad;
  385. checkoffset = atoi(*(++argv));
  386. checkend = 1;
  387. } else if (strcmp(*argv, "-noout") == 0)
  388. noout = ++num;
  389. else if (strcmp(*argv, "-trustout") == 0)
  390. trustout = 1;
  391. else if (strcmp(*argv, "-clrtrust") == 0)
  392. clrtrust = ++num;
  393. else if (strcmp(*argv, "-clrreject") == 0)
  394. clrreject = ++num;
  395. else if (strcmp(*argv, "-alias") == 0)
  396. aliasout = ++num;
  397. else if (strcmp(*argv, "-CAcreateserial") == 0)
  398. CA_createserial = ++num;
  399. else if (strcmp(*argv, "-clrext") == 0)
  400. clrext = 1;
  401. #if 1 /* stay backwards-compatible with 0.9.5; this
  402. * should go away soon */
  403. else if (strcmp(*argv, "-crlext") == 0) {
  404. BIO_printf(bio_err, "use -clrext instead of -crlext\n");
  405. clrext = 1;
  406. }
  407. #endif
  408. else if (strcmp(*argv, "-ocspid") == 0)
  409. ocspid = ++num;
  410. else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
  411. /* ok */
  412. digest = md_alg;
  413. } else {
  414. BIO_printf(bio_err, "unknown option %s\n", *argv);
  415. badops = 1;
  416. break;
  417. }
  418. argc--;
  419. argv++;
  420. }
  421. if (badops) {
  422. bad:
  423. for (pp = x509_usage; (*pp != NULL); pp++)
  424. BIO_printf(bio_err, "%s", *pp);
  425. goto end;
  426. }
  427. #ifndef OPENSSL_NO_ENGINE
  428. e = setup_engine(bio_err, engine, 0);
  429. #endif
  430. if (need_rand)
  431. app_RAND_load_file(NULL, bio_err, 0);
  432. ERR_load_crypto_strings();
  433. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  434. BIO_printf(bio_err, "Error getting password\n");
  435. goto end;
  436. }
  437. if (!X509_STORE_set_default_paths(ctx)) {
  438. ERR_print_errors(bio_err);
  439. goto end;
  440. }
  441. if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
  442. CAkeyfile = CAfile;
  443. } else if ((CA_flag) && (CAkeyfile == NULL)) {
  444. BIO_printf(bio_err,
  445. "need to specify a CAkey if using the CA command\n");
  446. goto end;
  447. }
  448. if (extfile) {
  449. long errorline = -1;
  450. X509V3_CTX ctx2;
  451. extconf = NCONF_new(NULL);
  452. if (!NCONF_load(extconf, extfile, &errorline)) {
  453. if (errorline <= 0)
  454. BIO_printf(bio_err,
  455. "error loading the config file '%s'\n", extfile);
  456. else
  457. BIO_printf(bio_err,
  458. "error on line %ld of config file '%s'\n",
  459. errorline, extfile);
  460. goto end;
  461. }
  462. if (!extsect) {
  463. extsect = NCONF_get_string(extconf, "default", "extensions");
  464. if (!extsect) {
  465. ERR_clear_error();
  466. extsect = "default";
  467. }
  468. }
  469. X509V3_set_ctx_test(&ctx2);
  470. X509V3_set_nconf(&ctx2, extconf);
  471. if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
  472. BIO_printf(bio_err,
  473. "Error Loading extension section %s\n", extsect);
  474. ERR_print_errors(bio_err);
  475. goto end;
  476. }
  477. }
  478. if (reqfile) {
  479. EVP_PKEY *pkey;
  480. BIO *in;
  481. if (!sign_flag && !CA_flag) {
  482. BIO_printf(bio_err, "We need a private key to sign with\n");
  483. goto end;
  484. }
  485. in = BIO_new(BIO_s_file());
  486. if (in == NULL) {
  487. ERR_print_errors(bio_err);
  488. goto end;
  489. }
  490. if (infile == NULL)
  491. BIO_set_fp(in, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
  492. else {
  493. if (BIO_read_filename(in, infile) <= 0) {
  494. perror(infile);
  495. BIO_free(in);
  496. goto end;
  497. }
  498. }
  499. req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
  500. BIO_free(in);
  501. if (req == NULL) {
  502. ERR_print_errors(bio_err);
  503. goto end;
  504. }
  505. if ((req->req_info == NULL) ||
  506. (req->req_info->pubkey == NULL) ||
  507. (req->req_info->pubkey->public_key == NULL) ||
  508. (req->req_info->pubkey->public_key->data == NULL)) {
  509. BIO_printf(bio_err,
  510. "The certificate request appears to corrupted\n");
  511. BIO_printf(bio_err, "It does not contain a public key\n");
  512. goto end;
  513. }
  514. if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
  515. BIO_printf(bio_err, "error unpacking public key\n");
  516. goto end;
  517. }
  518. i = X509_REQ_verify(req, pkey);
  519. EVP_PKEY_free(pkey);
  520. if (i < 0) {
  521. BIO_printf(bio_err, "Signature verification error\n");
  522. ERR_print_errors(bio_err);
  523. goto end;
  524. }
  525. if (i == 0) {
  526. BIO_printf(bio_err,
  527. "Signature did not match the certificate request\n");
  528. goto end;
  529. } else
  530. BIO_printf(bio_err, "Signature ok\n");
  531. print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
  532. nmflag);
  533. if ((x = X509_new()) == NULL)
  534. goto end;
  535. if (sno == NULL) {
  536. sno = ASN1_INTEGER_new();
  537. if (!sno || !rand_serial(NULL, sno))
  538. goto end;
  539. if (!X509_set_serialNumber(x, sno))
  540. goto end;
  541. ASN1_INTEGER_free(sno);
  542. sno = NULL;
  543. } else if (!X509_set_serialNumber(x, sno))
  544. goto end;
  545. if (!X509_set_issuer_name(x, req->req_info->subject))
  546. goto end;
  547. if (!X509_set_subject_name(x, req->req_info->subject))
  548. goto end;
  549. X509_gmtime_adj(X509_get_notBefore(x), 0);
  550. X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days);
  551. pkey = X509_REQ_get_pubkey(req);
  552. X509_set_pubkey(x, pkey);
  553. EVP_PKEY_free(pkey);
  554. } else
  555. x = load_cert(bio_err, infile, informat, NULL, e, "Certificate");
  556. if (x == NULL)
  557. goto end;
  558. if (CA_flag) {
  559. xca = load_cert(bio_err, CAfile, CAformat, NULL, e, "CA Certificate");
  560. if (xca == NULL)
  561. goto end;
  562. }
  563. if (!noout || text || next_serial) {
  564. OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
  565. out = BIO_new(BIO_s_file());
  566. if (out == NULL) {
  567. ERR_print_errors(bio_err);
  568. goto end;
  569. }
  570. if (outfile == NULL) {
  571. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  572. #ifdef OPENSSL_SYS_VMS
  573. {
  574. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  575. out = BIO_push(tmpbio, out);
  576. }
  577. #endif
  578. } else {
  579. if (BIO_write_filename(out, outfile) <= 0) {
  580. perror(outfile);
  581. goto end;
  582. }
  583. }
  584. }
  585. if (alias)
  586. X509_alias_set1(x, (unsigned char *)alias, -1);
  587. if (clrtrust)
  588. X509_trust_clear(x);
  589. if (clrreject)
  590. X509_reject_clear(x);
  591. if (trust) {
  592. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  593. objtmp = sk_ASN1_OBJECT_value(trust, i);
  594. X509_add1_trust_object(x, objtmp);
  595. }
  596. }
  597. if (reject) {
  598. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  599. objtmp = sk_ASN1_OBJECT_value(reject, i);
  600. X509_add1_reject_object(x, objtmp);
  601. }
  602. }
  603. if (num) {
  604. for (i = 1; i <= num; i++) {
  605. if (issuer == i) {
  606. print_name(STDout, "issuer= ",
  607. X509_get_issuer_name(x), nmflag);
  608. } else if (subject == i) {
  609. print_name(STDout, "subject= ",
  610. X509_get_subject_name(x), nmflag);
  611. } else if (serial == i) {
  612. BIO_printf(STDout, "serial=");
  613. i2a_ASN1_INTEGER(STDout, X509_get_serialNumber(x));
  614. BIO_printf(STDout, "\n");
  615. } else if (next_serial == i) {
  616. BIGNUM *bnser;
  617. ASN1_INTEGER *ser;
  618. ser = X509_get_serialNumber(x);
  619. bnser = ASN1_INTEGER_to_BN(ser, NULL);
  620. if (!bnser)
  621. goto end;
  622. if (!BN_add_word(bnser, 1))
  623. goto end;
  624. ser = BN_to_ASN1_INTEGER(bnser, NULL);
  625. if (!ser)
  626. goto end;
  627. BN_free(bnser);
  628. i2a_ASN1_INTEGER(out, ser);
  629. ASN1_INTEGER_free(ser);
  630. BIO_puts(out, "\n");
  631. } else if ((email == i) || (ocsp_uri == i)) {
  632. int j;
  633. STACK *emlst;
  634. if (email == i)
  635. emlst = X509_get1_email(x);
  636. else
  637. emlst = X509_get1_ocsp(x);
  638. for (j = 0; j < sk_num(emlst); j++)
  639. BIO_printf(STDout, "%s\n", sk_value(emlst, j));
  640. X509_email_free(emlst);
  641. } else if (aliasout == i) {
  642. unsigned char *alstr;
  643. alstr = X509_alias_get0(x, NULL);
  644. if (alstr)
  645. BIO_printf(STDout, "%s\n", alstr);
  646. else
  647. BIO_puts(STDout, "<No Alias>\n");
  648. } else if (subject_hash == i) {
  649. BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
  650. } else if (issuer_hash == i) {
  651. BIO_printf(STDout, "%08lx\n", X509_issuer_name_hash(x));
  652. } else if (pprint == i) {
  653. X509_PURPOSE *ptmp;
  654. int j;
  655. BIO_printf(STDout, "Certificate purposes:\n");
  656. for (j = 0; j < X509_PURPOSE_get_count(); j++) {
  657. ptmp = X509_PURPOSE_get0(j);
  658. purpose_print(STDout, x, ptmp);
  659. }
  660. } else if (modulus == i) {
  661. EVP_PKEY *pkey;
  662. pkey = X509_get_pubkey(x);
  663. if (pkey == NULL) {
  664. BIO_printf(bio_err, "Modulus=unavailable\n");
  665. ERR_print_errors(bio_err);
  666. goto end;
  667. }
  668. BIO_printf(STDout, "Modulus=");
  669. #ifndef OPENSSL_NO_RSA
  670. if (pkey->type == EVP_PKEY_RSA)
  671. BN_print(STDout, pkey->pkey.rsa->n);
  672. else
  673. #endif
  674. #ifndef OPENSSL_NO_DSA
  675. if (pkey->type == EVP_PKEY_DSA)
  676. BN_print(STDout, pkey->pkey.dsa->pub_key);
  677. else
  678. #endif
  679. BIO_printf(STDout, "Wrong Algorithm type");
  680. BIO_printf(STDout, "\n");
  681. EVP_PKEY_free(pkey);
  682. } else if (pubkey == i) {
  683. EVP_PKEY *pkey;
  684. pkey = X509_get_pubkey(x);
  685. if (pkey == NULL) {
  686. BIO_printf(bio_err, "Error getting public key\n");
  687. ERR_print_errors(bio_err);
  688. goto end;
  689. }
  690. PEM_write_bio_PUBKEY(STDout, pkey);
  691. EVP_PKEY_free(pkey);
  692. } else if (C == i) {
  693. unsigned char *d;
  694. char *m;
  695. int y, z;
  696. X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);
  697. BIO_printf(STDout, "/* subject:%s */\n", buf);
  698. m = X509_NAME_oneline(X509_get_issuer_name(x), buf,
  699. sizeof buf);
  700. BIO_printf(STDout, "/* issuer :%s */\n", buf);
  701. z = i2d_X509(x, NULL);
  702. m = OPENSSL_malloc(z);
  703. d = (unsigned char *)m;
  704. z = i2d_X509_NAME(X509_get_subject_name(x), &d);
  705. BIO_printf(STDout, "unsigned char XXX_subject_name[%d]={\n",
  706. z);
  707. d = (unsigned char *)m;
  708. for (y = 0; y < z; y++) {
  709. BIO_printf(STDout, "0x%02X,", d[y]);
  710. if ((y & 0x0f) == 0x0f)
  711. BIO_printf(STDout, "\n");
  712. }
  713. if (y % 16 != 0)
  714. BIO_printf(STDout, "\n");
  715. BIO_printf(STDout, "};\n");
  716. z = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
  717. BIO_printf(STDout, "unsigned char XXX_public_key[%d]={\n", z);
  718. d = (unsigned char *)m;
  719. for (y = 0; y < z; y++) {
  720. BIO_printf(STDout, "0x%02X,", d[y]);
  721. if ((y & 0x0f) == 0x0f)
  722. BIO_printf(STDout, "\n");
  723. }
  724. if (y % 16 != 0)
  725. BIO_printf(STDout, "\n");
  726. BIO_printf(STDout, "};\n");
  727. z = i2d_X509(x, &d);
  728. BIO_printf(STDout, "unsigned char XXX_certificate[%d]={\n",
  729. z);
  730. d = (unsigned char *)m;
  731. for (y = 0; y < z; y++) {
  732. BIO_printf(STDout, "0x%02X,", d[y]);
  733. if ((y & 0x0f) == 0x0f)
  734. BIO_printf(STDout, "\n");
  735. }
  736. if (y % 16 != 0)
  737. BIO_printf(STDout, "\n");
  738. BIO_printf(STDout, "};\n");
  739. OPENSSL_free(m);
  740. } else if (text == i) {
  741. X509_print_ex(out, x, nmflag, certflag);
  742. } else if (startdate == i) {
  743. BIO_puts(STDout, "notBefore=");
  744. ASN1_TIME_print(STDout, X509_get_notBefore(x));
  745. BIO_puts(STDout, "\n");
  746. } else if (enddate == i) {
  747. BIO_puts(STDout, "notAfter=");
  748. ASN1_TIME_print(STDout, X509_get_notAfter(x));
  749. BIO_puts(STDout, "\n");
  750. } else if (fingerprint == i) {
  751. int j;
  752. unsigned int n;
  753. unsigned char md[EVP_MAX_MD_SIZE];
  754. if (!X509_digest(x, digest, md, &n)) {
  755. BIO_printf(bio_err, "out of memory\n");
  756. goto end;
  757. }
  758. BIO_printf(STDout, "%s Fingerprint=",
  759. OBJ_nid2sn(EVP_MD_type(digest)));
  760. for (j = 0; j < (int)n; j++) {
  761. BIO_printf(STDout, "%02X%c", md[j], (j + 1 == (int)n)
  762. ? '\n' : ':');
  763. }
  764. }
  765. /* should be in the library */
  766. else if ((sign_flag == i) && (x509req == 0)) {
  767. BIO_printf(bio_err, "Getting Private key\n");
  768. if (Upkey == NULL) {
  769. Upkey = load_key(bio_err,
  770. keyfile, keyformat, 0,
  771. passin, e, "Private key");
  772. if (Upkey == NULL)
  773. goto end;
  774. }
  775. #ifndef OPENSSL_NO_DSA
  776. if (Upkey->type == EVP_PKEY_DSA)
  777. digest = EVP_dss1();
  778. #endif
  779. #ifndef OPENSSL_NO_ECDSA
  780. if (Upkey->type == EVP_PKEY_EC)
  781. digest = EVP_ecdsa();
  782. #endif
  783. assert(need_rand);
  784. if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))
  785. goto end;
  786. } else if (CA_flag == i) {
  787. BIO_printf(bio_err, "Getting CA Private Key\n");
  788. if (CAkeyfile != NULL) {
  789. CApkey = load_key(bio_err,
  790. CAkeyfile, CAkeyformat,
  791. 0, passin, e, "CA Private Key");
  792. if (CApkey == NULL)
  793. goto end;
  794. }
  795. #ifndef OPENSSL_NO_DSA
  796. if (CApkey->type == EVP_PKEY_DSA)
  797. digest = EVP_dss1();
  798. #endif
  799. #ifndef OPENSSL_NO_ECDSA
  800. if (CApkey->type == EVP_PKEY_EC)
  801. digest = EVP_ecdsa();
  802. #endif
  803. assert(need_rand);
  804. if (!x509_certify(ctx, CAfile, digest, x, xca,
  805. CApkey, CAserial, CA_createserial, days,
  806. clrext, extconf, extsect, sno))
  807. goto end;
  808. } else if (x509req == i) {
  809. EVP_PKEY *pk;
  810. BIO_printf(bio_err, "Getting request Private Key\n");
  811. if (keyfile == NULL) {
  812. BIO_printf(bio_err, "no request key file specified\n");
  813. goto end;
  814. } else {
  815. pk = load_key(bio_err,
  816. keyfile, keyformat, 0,
  817. passin, e, "request key");
  818. if (pk == NULL)
  819. goto end;
  820. }
  821. BIO_printf(bio_err, "Generating certificate request\n");
  822. #ifndef OPENSSL_NO_DSA
  823. if (pk->type == EVP_PKEY_DSA)
  824. digest = EVP_dss1();
  825. #endif
  826. #ifndef OPENSSL_NO_ECDSA
  827. if (pk->type == EVP_PKEY_EC)
  828. digest = EVP_ecdsa();
  829. #endif
  830. rq = X509_to_X509_REQ(x, pk, digest);
  831. EVP_PKEY_free(pk);
  832. if (rq == NULL) {
  833. ERR_print_errors(bio_err);
  834. goto end;
  835. }
  836. if (!noout) {
  837. X509_REQ_print(out, rq);
  838. PEM_write_bio_X509_REQ(out, rq);
  839. }
  840. noout = 1;
  841. } else if (ocspid == i) {
  842. X509_ocspid_print(out, x);
  843. }
  844. }
  845. }
  846. if (checkend) {
  847. time_t tcheck = time(NULL) + checkoffset;
  848. if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
  849. BIO_printf(out, "Certificate will expire\n");
  850. ret = 1;
  851. } else {
  852. BIO_printf(out, "Certificate will not expire\n");
  853. ret = 0;
  854. }
  855. goto end;
  856. }
  857. if (noout) {
  858. ret = 0;
  859. goto end;
  860. }
  861. if (outformat == FORMAT_ASN1)
  862. i = i2d_X509_bio(out, x);
  863. else if (outformat == FORMAT_PEM) {
  864. if (trustout)
  865. i = PEM_write_bio_X509_AUX(out, x);
  866. else
  867. i = PEM_write_bio_X509(out, x);
  868. } else if (outformat == FORMAT_NETSCAPE) {
  869. ASN1_HEADER ah;
  870. ASN1_OCTET_STRING os;
  871. os.data = (unsigned char *)NETSCAPE_CERT_HDR;
  872. os.length = strlen(NETSCAPE_CERT_HDR);
  873. ah.header = &os;
  874. ah.data = (char *)x;
  875. ah.meth = X509_asn1_meth();
  876. i = ASN1_i2d_bio_of(ASN1_HEADER, i2d_ASN1_HEADER, out, &ah);
  877. } else {
  878. BIO_printf(bio_err, "bad output format specified for outfile\n");
  879. goto end;
  880. }
  881. if (!i) {
  882. BIO_printf(bio_err, "unable to write certificate\n");
  883. ERR_print_errors(bio_err);
  884. goto end;
  885. }
  886. ret = 0;
  887. end:
  888. if (need_rand)
  889. app_RAND_write_file(NULL, bio_err);
  890. OBJ_cleanup();
  891. NCONF_free(extconf);
  892. BIO_free_all(out);
  893. BIO_free_all(STDout);
  894. X509_STORE_free(ctx);
  895. X509_REQ_free(req);
  896. X509_free(x);
  897. X509_free(xca);
  898. EVP_PKEY_free(Upkey);
  899. EVP_PKEY_free(CApkey);
  900. X509_REQ_free(rq);
  901. ASN1_INTEGER_free(sno);
  902. sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
  903. sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
  904. if (passin)
  905. OPENSSL_free(passin);
  906. apps_shutdown();
  907. OPENSSL_EXIT(ret);
  908. }
  909. static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
  910. int create)
  911. {
  912. char *buf = NULL, *p;
  913. ASN1_INTEGER *bs = NULL;
  914. BIGNUM *serial = NULL;
  915. size_t len;
  916. len = ((serialfile == NULL)
  917. ? (strlen(CAfile) + strlen(POSTFIX) + 1)
  918. : (strlen(serialfile))) + 1;
  919. buf = OPENSSL_malloc(len);
  920. if (buf == NULL) {
  921. BIO_printf(bio_err, "out of mem\n");
  922. goto end;
  923. }
  924. if (serialfile == NULL) {
  925. BUF_strlcpy(buf, CAfile, len);
  926. for (p = buf; *p; p++)
  927. if (*p == '.') {
  928. *p = '\0';
  929. break;
  930. }
  931. BUF_strlcat(buf, POSTFIX, len);
  932. } else
  933. BUF_strlcpy(buf, serialfile, len);
  934. serial = load_serial(buf, create, NULL);
  935. if (serial == NULL)
  936. goto end;
  937. if (!BN_add_word(serial, 1)) {
  938. BIO_printf(bio_err, "add_word failure\n");
  939. goto end;
  940. }
  941. if (!save_serial(buf, NULL, serial, &bs))
  942. goto end;
  943. end:
  944. if (buf)
  945. OPENSSL_free(buf);
  946. BN_free(serial);
  947. return bs;
  948. }
  949. static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
  950. X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile,
  951. int create, int days, int clrext, CONF *conf,
  952. char *section, ASN1_INTEGER *sno)
  953. {
  954. int ret = 0;
  955. ASN1_INTEGER *bs = NULL;
  956. X509_STORE_CTX xsc;
  957. EVP_PKEY *upkey;
  958. upkey = X509_get_pubkey(xca);
  959. EVP_PKEY_copy_parameters(upkey, pkey);
  960. EVP_PKEY_free(upkey);
  961. if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
  962. BIO_printf(bio_err, "Error initialising X509 store\n");
  963. goto end;
  964. }
  965. if (sno)
  966. bs = sno;
  967. else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
  968. goto end;
  969. /* if (!X509_STORE_add_cert(ctx,x)) goto end;*/
  970. /*
  971. * NOTE: this certificate can/should be self signed, unless it was a
  972. * certificate request in which case it is not.
  973. */
  974. X509_STORE_CTX_set_cert(&xsc, x);
  975. X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
  976. if (!reqfile && X509_verify_cert(&xsc) <= 0)
  977. goto end;
  978. if (!X509_check_private_key(xca, pkey)) {
  979. BIO_printf(bio_err,
  980. "CA certificate and CA private key do not match\n");
  981. goto end;
  982. }
  983. if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
  984. goto end;
  985. if (!X509_set_serialNumber(x, bs))
  986. goto end;
  987. if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
  988. goto end;
  989. /* hardwired expired */
  990. if (X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days) ==
  991. NULL)
  992. goto end;
  993. if (clrext) {
  994. while (X509_get_ext_count(x) > 0)
  995. X509_delete_ext(x, 0);
  996. }
  997. if (conf) {
  998. X509V3_CTX ctx2;
  999. X509_set_version(x, 2); /* version 3 certificate */
  1000. X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
  1001. X509V3_set_nconf(&ctx2, conf);
  1002. if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
  1003. goto end;
  1004. }
  1005. if (!X509_sign(x, pkey, digest))
  1006. goto end;
  1007. ret = 1;
  1008. end:
  1009. X509_STORE_CTX_cleanup(&xsc);
  1010. if (!ret)
  1011. ERR_print_errors(bio_err);
  1012. if (!sno)
  1013. ASN1_INTEGER_free(bs);
  1014. return ret;
  1015. }
  1016. static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx)
  1017. {
  1018. int err;
  1019. X509 *err_cert;
  1020. /*
  1021. * it is ok to use a self signed certificate This case will catch both
  1022. * the initial ok == 0 and the final ok == 1 calls to this function
  1023. */
  1024. err = X509_STORE_CTX_get_error(ctx);
  1025. if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
  1026. return 1;
  1027. /*
  1028. * BAD we should have gotten an error. Normally if everything worked
  1029. * X509_STORE_CTX_get_error(ctx) will still be set to
  1030. * DEPTH_ZERO_SELF_....
  1031. */
  1032. if (ok) {
  1033. BIO_printf(bio_err,
  1034. "error with certificate to be certified - should be self signed\n");
  1035. return 0;
  1036. } else {
  1037. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  1038. print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
  1039. BIO_printf(bio_err,
  1040. "error with certificate - error %d at depth %d\n%s\n", err,
  1041. X509_STORE_CTX_get_error_depth(ctx),
  1042. X509_verify_cert_error_string(err));
  1043. return 1;
  1044. }
  1045. }
  1046. /* self sign */
  1047. static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
  1048. const EVP_MD *digest, CONF *conf, char *section)
  1049. {
  1050. EVP_PKEY *pktmp;
  1051. pktmp = X509_get_pubkey(x);
  1052. EVP_PKEY_copy_parameters(pktmp, pkey);
  1053. EVP_PKEY_save_parameters(pktmp, 1);
  1054. EVP_PKEY_free(pktmp);
  1055. if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
  1056. goto err;
  1057. if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
  1058. goto err;
  1059. /* Lets just make it 12:00am GMT, Jan 1 1970 */
  1060. /* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */
  1061. /* 28 days to be certified */
  1062. if (X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days) ==
  1063. NULL)
  1064. goto err;
  1065. if (!X509_set_pubkey(x, pkey))
  1066. goto err;
  1067. if (clrext) {
  1068. while (X509_get_ext_count(x) > 0)
  1069. X509_delete_ext(x, 0);
  1070. }
  1071. if (conf) {
  1072. X509V3_CTX ctx;
  1073. X509_set_version(x, 2); /* version 3 certificate */
  1074. X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
  1075. X509V3_set_nconf(&ctx, conf);
  1076. if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
  1077. goto err;
  1078. }
  1079. if (!X509_sign(x, pkey, digest))
  1080. goto err;
  1081. return 1;
  1082. err:
  1083. ERR_print_errors(bio_err);
  1084. return 0;
  1085. }
  1086. static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
  1087. {
  1088. int id, i, idret;
  1089. char *pname;
  1090. id = X509_PURPOSE_get_id(pt);
  1091. pname = X509_PURPOSE_get0_name(pt);
  1092. for (i = 0; i < 2; i++) {
  1093. idret = X509_check_purpose(cert, id, i);
  1094. BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
  1095. if (idret == 1)
  1096. BIO_printf(bio, "Yes\n");
  1097. else if (idret == 0)
  1098. BIO_printf(bio, "No\n");
  1099. else
  1100. BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
  1101. }
  1102. return 1;
  1103. }