2
0

x509.c 34 KB

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