x509.c 32 KB

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