x509.c 33 KB

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