x509.c 43 KB

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