x509.c 32 KB

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