x509.c 43 KB

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