req.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /* apps/req.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. /*
  59. * Until the key-gen callbacks are modified to use newer prototypes, we allow
  60. * deprecated functions for openssl-internal code
  61. */
  62. #ifdef OPENSSL_NO_DEPRECATED
  63. # undef OPENSSL_NO_DEPRECATED
  64. #endif
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <time.h>
  68. #include <string.h>
  69. #ifdef OPENSSL_NO_STDIO
  70. # define APPS_WIN16
  71. #endif
  72. #include "apps.h"
  73. #include <openssl/bio.h>
  74. #include <openssl/evp.h>
  75. #include <openssl/conf.h>
  76. #include <openssl/err.h>
  77. #include <openssl/asn1.h>
  78. #include <openssl/x509.h>
  79. #include <openssl/x509v3.h>
  80. #include <openssl/objects.h>
  81. #include <openssl/pem.h>
  82. #include <openssl/bn.h>
  83. #ifndef OPENSSL_NO_RSA
  84. # include <openssl/rsa.h>
  85. #endif
  86. #ifndef OPENSSL_NO_DSA
  87. # include <openssl/dsa.h>
  88. #endif
  89. #define SECTION "req"
  90. #define BITS "default_bits"
  91. #define KEYFILE "default_keyfile"
  92. #define PROMPT "prompt"
  93. #define DISTINGUISHED_NAME "distinguished_name"
  94. #define ATTRIBUTES "attributes"
  95. #define V3_EXTENSIONS "x509_extensions"
  96. #define REQ_EXTENSIONS "req_extensions"
  97. #define STRING_MASK "string_mask"
  98. #define UTF8_IN "utf8"
  99. #define DEFAULT_KEY_LENGTH 512
  100. #define MIN_KEY_LENGTH 384
  101. #undef PROG
  102. #define PROG req_main
  103. /*-
  104. * -inform arg - input format - default PEM (DER or PEM)
  105. * -outform arg - output format - default PEM
  106. * -in arg - input file - default stdin
  107. * -out arg - output file - default stdout
  108. * -verify - check request signature
  109. * -noout - don't print stuff out.
  110. * -text - print out human readable text.
  111. * -nodes - no des encryption
  112. * -config file - Load configuration file.
  113. * -key file - make a request using key in file (or use it for verification).
  114. * -keyform arg - key file format.
  115. * -rand file(s) - load the file(s) into the PRNG.
  116. * -newkey - make a key and a request.
  117. * -modulus - print RSA modulus.
  118. * -pubkey - output Public Key.
  119. * -x509 - output a self signed X509 structure instead.
  120. * -asn1-kludge - output new certificate request in a format that some CA's
  121. * require. This format is wrong
  122. */
  123. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
  124. int attribs, unsigned long chtype);
  125. static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
  126. int multirdn);
  127. static int prompt_info(X509_REQ *req,
  128. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  129. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  130. int attribs, unsigned long chtype);
  131. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
  132. STACK_OF(CONF_VALUE) *attr, int attribs,
  133. unsigned long chtype);
  134. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  135. char *value, int nid, int n_min, int n_max,
  136. unsigned long chtype);
  137. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  138. char *value, int nid, int n_min, int n_max,
  139. unsigned long chtype, int mval);
  140. #ifndef OPENSSL_NO_RSA
  141. static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb);
  142. #endif
  143. static int req_check_len(int len, int n_min, int n_max);
  144. static int check_end(const char *str, const char *end);
  145. #ifndef MONOLITH
  146. static char *default_config_file = NULL;
  147. #endif
  148. static CONF *req_conf = NULL;
  149. static int batch = 0;
  150. #define TYPE_RSA 1
  151. #define TYPE_DSA 2
  152. #define TYPE_DH 3
  153. #define TYPE_EC 4
  154. int MAIN(int, char **);
  155. int MAIN(int argc, char **argv)
  156. {
  157. ENGINE *e = NULL;
  158. #ifndef OPENSSL_NO_DSA
  159. DSA *dsa_params = NULL;
  160. #endif
  161. #ifndef OPENSSL_NO_ECDSA
  162. EC_KEY *ec_params = NULL;
  163. #endif
  164. unsigned long nmflag = 0, reqflag = 0;
  165. int ex = 1, x509 = 0, days = 30;
  166. X509 *x509ss = NULL;
  167. X509_REQ *req = NULL;
  168. EVP_PKEY *pkey = NULL;
  169. int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = TYPE_RSA;
  170. long newkey = -1;
  171. BIO *in = NULL, *out = NULL;
  172. int informat, outformat, verify = 0, noout = 0, text = 0, keyform =
  173. FORMAT_PEM;
  174. int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
  175. char *infile, *outfile, *prog, *keyfile = NULL, *template =
  176. NULL, *keyout = NULL;
  177. #ifndef OPENSSL_NO_ENGINE
  178. char *engine = NULL;
  179. #endif
  180. char *extensions = NULL;
  181. char *req_exts = NULL;
  182. const EVP_CIPHER *cipher = NULL;
  183. ASN1_INTEGER *serial = NULL;
  184. int modulus = 0;
  185. char *inrand = NULL;
  186. char *passargin = NULL, *passargout = NULL;
  187. char *passin = NULL, *passout = NULL;
  188. char *p;
  189. char *subj = NULL;
  190. int multirdn = 0;
  191. const EVP_MD *md_alg = NULL, *digest = EVP_sha1();
  192. unsigned long chtype = MBSTRING_ASC;
  193. #ifndef MONOLITH
  194. char *to_free;
  195. long errline;
  196. #endif
  197. req_conf = NULL;
  198. #ifndef OPENSSL_NO_DES
  199. cipher = EVP_des_ede3_cbc();
  200. #endif
  201. apps_startup();
  202. if (bio_err == NULL)
  203. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  204. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  205. infile = NULL;
  206. outfile = NULL;
  207. informat = FORMAT_PEM;
  208. outformat = FORMAT_PEM;
  209. prog = argv[0];
  210. argc--;
  211. argv++;
  212. while (argc >= 1) {
  213. if (strcmp(*argv, "-inform") == 0) {
  214. if (--argc < 1)
  215. goto bad;
  216. informat = str2fmt(*(++argv));
  217. } else if (strcmp(*argv, "-outform") == 0) {
  218. if (--argc < 1)
  219. goto bad;
  220. outformat = str2fmt(*(++argv));
  221. }
  222. #ifndef OPENSSL_NO_ENGINE
  223. else if (strcmp(*argv, "-engine") == 0) {
  224. if (--argc < 1)
  225. goto bad;
  226. engine = *(++argv);
  227. }
  228. #endif
  229. else if (strcmp(*argv, "-key") == 0) {
  230. if (--argc < 1)
  231. goto bad;
  232. keyfile = *(++argv);
  233. } else if (strcmp(*argv, "-pubkey") == 0) {
  234. pubkey = 1;
  235. } else if (strcmp(*argv, "-new") == 0) {
  236. newreq = 1;
  237. } else if (strcmp(*argv, "-config") == 0) {
  238. if (--argc < 1)
  239. goto bad;
  240. template = *(++argv);
  241. } else if (strcmp(*argv, "-keyform") == 0) {
  242. if (--argc < 1)
  243. goto bad;
  244. keyform = str2fmt(*(++argv));
  245. } else if (strcmp(*argv, "-in") == 0) {
  246. if (--argc < 1)
  247. goto bad;
  248. infile = *(++argv);
  249. } else if (strcmp(*argv, "-out") == 0) {
  250. if (--argc < 1)
  251. goto bad;
  252. outfile = *(++argv);
  253. } else if (strcmp(*argv, "-keyout") == 0) {
  254. if (--argc < 1)
  255. goto bad;
  256. keyout = *(++argv);
  257. } else if (strcmp(*argv, "-passin") == 0) {
  258. if (--argc < 1)
  259. goto bad;
  260. passargin = *(++argv);
  261. } else if (strcmp(*argv, "-passout") == 0) {
  262. if (--argc < 1)
  263. goto bad;
  264. passargout = *(++argv);
  265. } else if (strcmp(*argv, "-rand") == 0) {
  266. if (--argc < 1)
  267. goto bad;
  268. inrand = *(++argv);
  269. } else if (strcmp(*argv, "-newkey") == 0) {
  270. int is_numeric;
  271. if (--argc < 1)
  272. goto bad;
  273. p = *(++argv);
  274. is_numeric = p[0] >= '0' && p[0] <= '9';
  275. if (strncmp("rsa:", p, 4) == 0 || is_numeric) {
  276. pkey_type = TYPE_RSA;
  277. if (!is_numeric)
  278. p += 4;
  279. newkey = atoi(p);
  280. } else
  281. #ifndef OPENSSL_NO_DSA
  282. if (strncmp("dsa:", p, 4) == 0) {
  283. X509 *xtmp = NULL;
  284. EVP_PKEY *dtmp;
  285. pkey_type = TYPE_DSA;
  286. p += 4;
  287. if ((in = BIO_new_file(p, "r")) == NULL) {
  288. perror(p);
  289. goto end;
  290. }
  291. if ((dsa_params =
  292. PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
  293. ERR_clear_error();
  294. (void)BIO_reset(in);
  295. if ((xtmp =
  296. PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL) {
  297. BIO_printf(bio_err,
  298. "unable to load DSA parameters from file\n");
  299. goto end;
  300. }
  301. if ((dtmp = X509_get_pubkey(xtmp)) == NULL)
  302. goto end;
  303. if (dtmp->type == EVP_PKEY_DSA)
  304. dsa_params = DSAparams_dup(dtmp->pkey.dsa);
  305. EVP_PKEY_free(dtmp);
  306. X509_free(xtmp);
  307. if (dsa_params == NULL) {
  308. BIO_printf(bio_err,
  309. "Certificate does not contain DSA parameters\n");
  310. goto end;
  311. }
  312. }
  313. BIO_free(in);
  314. in = NULL;
  315. newkey = BN_num_bits(dsa_params->p);
  316. } else
  317. #endif
  318. #ifndef OPENSSL_NO_ECDSA
  319. if (strncmp("ec:", p, 3) == 0) {
  320. X509 *xtmp = NULL;
  321. EVP_PKEY *dtmp;
  322. EC_GROUP *group;
  323. pkey_type = TYPE_EC;
  324. p += 3;
  325. if ((in = BIO_new_file(p, "r")) == NULL) {
  326. perror(p);
  327. goto end;
  328. }
  329. if ((ec_params = EC_KEY_new()) == NULL)
  330. goto end;
  331. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  332. if (group == NULL) {
  333. EC_KEY_free(ec_params);
  334. ERR_clear_error();
  335. (void)BIO_reset(in);
  336. if ((xtmp =
  337. PEM_read_bio_X509(in, NULL, NULL, NULL)) == NULL) {
  338. BIO_printf(bio_err,
  339. "unable to load EC parameters from file\n");
  340. goto end;
  341. }
  342. if ((dtmp = X509_get_pubkey(xtmp)) == NULL)
  343. goto end;
  344. if (dtmp->type == EVP_PKEY_EC)
  345. ec_params = EC_KEY_dup(dtmp->pkey.ec);
  346. EVP_PKEY_free(dtmp);
  347. X509_free(xtmp);
  348. if (ec_params == NULL) {
  349. BIO_printf(bio_err,
  350. "Certificate does not contain EC parameters\n");
  351. goto end;
  352. }
  353. } else {
  354. if (EC_KEY_set_group(ec_params, group) == 0)
  355. goto end;
  356. EC_GROUP_free(group);
  357. }
  358. BIO_free(in);
  359. in = NULL;
  360. newkey = EC_GROUP_get_degree(EC_KEY_get0_group(ec_params));
  361. } else
  362. #endif
  363. #ifndef OPENSSL_NO_DH
  364. if (strncmp("dh:", p, 4) == 0) {
  365. pkey_type = TYPE_DH;
  366. p += 3;
  367. } else
  368. #endif
  369. {
  370. goto bad;
  371. }
  372. newreq = 1;
  373. } else if (strcmp(*argv, "-batch") == 0)
  374. batch = 1;
  375. else if (strcmp(*argv, "-newhdr") == 0)
  376. newhdr = 1;
  377. else if (strcmp(*argv, "-modulus") == 0)
  378. modulus = 1;
  379. else if (strcmp(*argv, "-verify") == 0)
  380. verify = 1;
  381. else if (strcmp(*argv, "-nodes") == 0)
  382. nodes = 1;
  383. else if (strcmp(*argv, "-noout") == 0)
  384. noout = 1;
  385. else if (strcmp(*argv, "-verbose") == 0)
  386. verbose = 1;
  387. else if (strcmp(*argv, "-utf8") == 0)
  388. chtype = MBSTRING_UTF8;
  389. else if (strcmp(*argv, "-nameopt") == 0) {
  390. if (--argc < 1)
  391. goto bad;
  392. if (!set_name_ex(&nmflag, *(++argv)))
  393. goto bad;
  394. } else if (strcmp(*argv, "-reqopt") == 0) {
  395. if (--argc < 1)
  396. goto bad;
  397. if (!set_cert_ex(&reqflag, *(++argv)))
  398. goto bad;
  399. } else if (strcmp(*argv, "-subject") == 0)
  400. subject = 1;
  401. else if (strcmp(*argv, "-text") == 0)
  402. text = 1;
  403. else if (strcmp(*argv, "-x509") == 0)
  404. x509 = 1;
  405. else if (strcmp(*argv, "-asn1-kludge") == 0)
  406. kludge = 1;
  407. else if (strcmp(*argv, "-no-asn1-kludge") == 0)
  408. kludge = 0;
  409. else if (strcmp(*argv, "-subj") == 0) {
  410. if (--argc < 1)
  411. goto bad;
  412. subj = *(++argv);
  413. } else if (strcmp(*argv, "-multivalue-rdn") == 0)
  414. multirdn = 1;
  415. else if (strcmp(*argv, "-days") == 0) {
  416. if (--argc < 1)
  417. goto bad;
  418. days = atoi(*(++argv));
  419. if (days == 0)
  420. days = 30;
  421. } else if (strcmp(*argv, "-set_serial") == 0) {
  422. if (--argc < 1)
  423. goto bad;
  424. serial = s2i_ASN1_INTEGER(NULL, *(++argv));
  425. if (!serial)
  426. goto bad;
  427. } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) {
  428. /* ok */
  429. digest = md_alg;
  430. } else if (strcmp(*argv, "-extensions") == 0) {
  431. if (--argc < 1)
  432. goto bad;
  433. extensions = *(++argv);
  434. } else if (strcmp(*argv, "-reqexts") == 0) {
  435. if (--argc < 1)
  436. goto bad;
  437. req_exts = *(++argv);
  438. } else {
  439. BIO_printf(bio_err, "unknown option %s\n", *argv);
  440. badops = 1;
  441. break;
  442. }
  443. argc--;
  444. argv++;
  445. }
  446. if (badops) {
  447. bad:
  448. BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
  449. BIO_printf(bio_err, "where options are\n");
  450. BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
  451. BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
  452. BIO_printf(bio_err, " -in arg input file\n");
  453. BIO_printf(bio_err, " -out arg output file\n");
  454. BIO_printf(bio_err, " -text text form of request\n");
  455. BIO_printf(bio_err, " -pubkey output public key\n");
  456. BIO_printf(bio_err, " -noout do not output REQ\n");
  457. BIO_printf(bio_err, " -verify verify signature on REQ\n");
  458. BIO_printf(bio_err, " -modulus RSA modulus\n");
  459. BIO_printf(bio_err, " -nodes don't encrypt the output key\n");
  460. #ifndef OPENSSL_NO_ENGINE
  461. BIO_printf(bio_err,
  462. " -engine e use engine e, possibly a hardware device\n");
  463. #endif
  464. BIO_printf(bio_err, " -subject output the request's subject\n");
  465. BIO_printf(bio_err, " -passin private key password source\n");
  466. BIO_printf(bio_err,
  467. " -key file use the private key contained in file\n");
  468. BIO_printf(bio_err, " -keyform arg key file format\n");
  469. BIO_printf(bio_err, " -keyout arg file to send the key to\n");
  470. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  471. LIST_SEPARATOR_CHAR);
  472. BIO_printf(bio_err,
  473. " load the file (or the files in the directory) into\n");
  474. BIO_printf(bio_err, " the random number generator\n");
  475. BIO_printf(bio_err,
  476. " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
  477. BIO_printf(bio_err,
  478. " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
  479. #ifndef OPENSSL_NO_ECDSA
  480. BIO_printf(bio_err,
  481. " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
  482. #endif
  483. BIO_printf(bio_err,
  484. " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
  485. BIO_printf(bio_err, " -config file request template file.\n");
  486. BIO_printf(bio_err,
  487. " -subj arg set or modify request subject\n");
  488. BIO_printf(bio_err,
  489. " -multivalue-rdn enable support for multivalued RDNs\n");
  490. BIO_printf(bio_err, " -new new request.\n");
  491. BIO_printf(bio_err,
  492. " -batch do not ask anything during request generation\n");
  493. BIO_printf(bio_err,
  494. " -x509 output a x509 structure instead of a cert. req.\n");
  495. BIO_printf(bio_err,
  496. " -days number of days a certificate generated by -x509 is valid for.\n");
  497. BIO_printf(bio_err,
  498. " -set_serial serial number to use for a certificate generated by -x509.\n");
  499. BIO_printf(bio_err,
  500. " -newhdr output \"NEW\" in the header lines\n");
  501. BIO_printf(bio_err,
  502. " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
  503. BIO_printf(bio_err,
  504. " have been reported as requiring\n");
  505. BIO_printf(bio_err,
  506. " -extensions .. specify certificate extension section (override value in config file)\n");
  507. BIO_printf(bio_err,
  508. " -reqexts .. specify request extension section (override value in config file)\n");
  509. BIO_printf(bio_err,
  510. " -utf8 input characters are UTF8 (default ASCII)\n");
  511. BIO_printf(bio_err,
  512. " -nameopt arg - various certificate name options\n");
  513. BIO_printf(bio_err,
  514. " -reqopt arg - various request text options\n\n");
  515. goto end;
  516. }
  517. ERR_load_crypto_strings();
  518. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  519. BIO_printf(bio_err, "Error getting passwords\n");
  520. goto end;
  521. }
  522. #ifndef MONOLITH /* else this has happened in openssl.c
  523. * (global `config') */
  524. /* Lets load up our environment a little */
  525. p = getenv("OPENSSL_CONF");
  526. if (p == NULL)
  527. p = getenv("SSLEAY_CONF");
  528. if (p == NULL)
  529. p = to_free = make_config_name();
  530. default_config_file = p;
  531. config = NCONF_new(NULL);
  532. i = NCONF_load(config, p, &errline);
  533. #endif
  534. if (template != NULL) {
  535. long errline = -1;
  536. if (verbose)
  537. BIO_printf(bio_err, "Using configuration from %s\n", template);
  538. req_conf = NCONF_new(NULL);
  539. i = NCONF_load(req_conf, template, &errline);
  540. if (i == 0) {
  541. BIO_printf(bio_err, "error on line %ld of %s\n", errline,
  542. template);
  543. goto end;
  544. }
  545. } else {
  546. req_conf = config;
  547. if (req_conf == NULL) {
  548. BIO_printf(bio_err, "Unable to load config info from %s\n",
  549. default_config_file);
  550. if (newreq)
  551. goto end;
  552. } else if (verbose)
  553. BIO_printf(bio_err, "Using configuration from %s\n",
  554. default_config_file);
  555. }
  556. if (req_conf != NULL) {
  557. if (!load_config(bio_err, req_conf))
  558. goto end;
  559. p = NCONF_get_string(req_conf, NULL, "oid_file");
  560. if (p == NULL)
  561. ERR_clear_error();
  562. if (p != NULL) {
  563. BIO *oid_bio;
  564. oid_bio = BIO_new_file(p, "r");
  565. if (oid_bio == NULL) {
  566. /*-
  567. BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
  568. ERR_print_errors(bio_err);
  569. */
  570. } else {
  571. OBJ_create_objects(oid_bio);
  572. BIO_free(oid_bio);
  573. }
  574. }
  575. }
  576. if (!add_oid_section(bio_err, req_conf))
  577. goto end;
  578. if (md_alg == NULL) {
  579. p = NCONF_get_string(req_conf, SECTION, "default_md");
  580. if (p == NULL)
  581. ERR_clear_error();
  582. if (p != NULL) {
  583. if ((md_alg = EVP_get_digestbyname(p)) != NULL)
  584. digest = md_alg;
  585. }
  586. }
  587. if (!extensions) {
  588. extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
  589. if (!extensions)
  590. ERR_clear_error();
  591. }
  592. if (extensions) {
  593. /* Check syntax of file */
  594. X509V3_CTX ctx;
  595. X509V3_set_ctx_test(&ctx);
  596. X509V3_set_nconf(&ctx, req_conf);
  597. if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
  598. BIO_printf(bio_err,
  599. "Error Loading extension section %s\n", extensions);
  600. goto end;
  601. }
  602. }
  603. if (!passin) {
  604. passin = NCONF_get_string(req_conf, SECTION, "input_password");
  605. if (!passin)
  606. ERR_clear_error();
  607. }
  608. if (!passout) {
  609. passout = NCONF_get_string(req_conf, SECTION, "output_password");
  610. if (!passout)
  611. ERR_clear_error();
  612. }
  613. p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
  614. if (!p)
  615. ERR_clear_error();
  616. if (p && !ASN1_STRING_set_default_mask_asc(p)) {
  617. BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
  618. goto end;
  619. }
  620. if (chtype != MBSTRING_UTF8) {
  621. p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
  622. if (!p)
  623. ERR_clear_error();
  624. else if (!strcmp(p, "yes"))
  625. chtype = MBSTRING_UTF8;
  626. }
  627. if (!req_exts) {
  628. req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
  629. if (!req_exts)
  630. ERR_clear_error();
  631. }
  632. if (req_exts) {
  633. /* Check syntax of file */
  634. X509V3_CTX ctx;
  635. X509V3_set_ctx_test(&ctx);
  636. X509V3_set_nconf(&ctx, req_conf);
  637. if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
  638. BIO_printf(bio_err,
  639. "Error Loading request extension section %s\n",
  640. req_exts);
  641. goto end;
  642. }
  643. }
  644. in = BIO_new(BIO_s_file());
  645. out = BIO_new(BIO_s_file());
  646. if ((in == NULL) || (out == NULL))
  647. goto end;
  648. #ifndef OPENSSL_NO_ENGINE
  649. e = setup_engine(bio_err, engine, 0);
  650. #endif
  651. if (keyfile != NULL) {
  652. pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
  653. "Private Key");
  654. if (!pkey) {
  655. /*
  656. * load_key() has already printed an appropriate message
  657. */
  658. goto end;
  659. } else {
  660. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  661. if (randfile == NULL)
  662. ERR_clear_error();
  663. app_RAND_load_file(randfile, bio_err, 0);
  664. }
  665. }
  666. if (newreq && (pkey == NULL)) {
  667. #ifndef OPENSSL_NO_RSA
  668. BN_GENCB cb;
  669. #endif
  670. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  671. if (randfile == NULL)
  672. ERR_clear_error();
  673. app_RAND_load_file(randfile, bio_err, 0);
  674. if (inrand)
  675. app_RAND_load_files(inrand);
  676. if (newkey <= 0) {
  677. if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey))
  678. newkey = DEFAULT_KEY_LENGTH;
  679. }
  680. if (newkey < MIN_KEY_LENGTH
  681. && (pkey_type == TYPE_RSA || pkey_type == TYPE_DSA)) {
  682. BIO_printf(bio_err, "private key length is too short,\n");
  683. BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
  684. MIN_KEY_LENGTH, newkey);
  685. goto end;
  686. }
  687. BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
  688. newkey, (pkey_type == TYPE_RSA) ? "RSA" :
  689. (pkey_type == TYPE_DSA) ? "DSA" : "EC");
  690. if ((pkey = EVP_PKEY_new()) == NULL)
  691. goto end;
  692. #ifndef OPENSSL_NO_RSA
  693. BN_GENCB_set(&cb, req_cb, bio_err);
  694. if (pkey_type == TYPE_RSA) {
  695. RSA *rsa = RSA_new();
  696. BIGNUM *bn = BN_new();
  697. if (!bn || !rsa || !BN_set_word(bn, 0x10001) ||
  698. !RSA_generate_key_ex(rsa, newkey, bn, &cb) ||
  699. !EVP_PKEY_assign_RSA(pkey, rsa)) {
  700. if (bn)
  701. BN_free(bn);
  702. if (rsa)
  703. RSA_free(rsa);
  704. goto end;
  705. }
  706. BN_free(bn);
  707. } else
  708. #endif
  709. #ifndef OPENSSL_NO_DSA
  710. if (pkey_type == TYPE_DSA) {
  711. if (!DSA_generate_key(dsa_params))
  712. goto end;
  713. if (!EVP_PKEY_assign_DSA(pkey, dsa_params))
  714. goto end;
  715. dsa_params = NULL;
  716. }
  717. #endif
  718. #ifndef OPENSSL_NO_ECDSA
  719. if (pkey_type == TYPE_EC) {
  720. if (!EC_KEY_generate_key(ec_params))
  721. goto end;
  722. if (!EVP_PKEY_assign_EC_KEY(pkey, ec_params))
  723. goto end;
  724. ec_params = NULL;
  725. }
  726. #endif
  727. app_RAND_write_file(randfile, bio_err);
  728. if (pkey == NULL)
  729. goto end;
  730. if (keyout == NULL) {
  731. keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
  732. if (keyout == NULL)
  733. ERR_clear_error();
  734. }
  735. if (keyout == NULL) {
  736. BIO_printf(bio_err, "writing new private key to stdout\n");
  737. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  738. #ifdef OPENSSL_SYS_VMS
  739. {
  740. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  741. out = BIO_push(tmpbio, out);
  742. }
  743. #endif
  744. } else {
  745. BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
  746. if (BIO_write_filename(out, keyout) <= 0) {
  747. perror(keyout);
  748. goto end;
  749. }
  750. }
  751. p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
  752. if (p == NULL) {
  753. ERR_clear_error();
  754. p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
  755. if (p == NULL)
  756. ERR_clear_error();
  757. }
  758. if ((p != NULL) && (strcmp(p, "no") == 0))
  759. cipher = NULL;
  760. if (nodes)
  761. cipher = NULL;
  762. i = 0;
  763. loop:
  764. if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
  765. NULL, 0, NULL, passout)) {
  766. if ((ERR_GET_REASON(ERR_peek_error()) ==
  767. PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
  768. ERR_clear_error();
  769. i++;
  770. goto loop;
  771. }
  772. goto end;
  773. }
  774. BIO_printf(bio_err, "-----\n");
  775. }
  776. if (!newreq) {
  777. /*
  778. * Since we are using a pre-existing certificate request, the kludge
  779. * 'format' info should not be changed.
  780. */
  781. kludge = -1;
  782. if (infile == NULL)
  783. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  784. else {
  785. if (BIO_read_filename(in, infile) <= 0) {
  786. perror(infile);
  787. goto end;
  788. }
  789. }
  790. if (informat == FORMAT_ASN1)
  791. req = d2i_X509_REQ_bio(in, NULL);
  792. else if (informat == FORMAT_PEM)
  793. req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
  794. else {
  795. BIO_printf(bio_err,
  796. "bad input format specified for X509 request\n");
  797. goto end;
  798. }
  799. if (req == NULL) {
  800. BIO_printf(bio_err, "unable to load X509 request\n");
  801. goto end;
  802. }
  803. }
  804. if (newreq || x509) {
  805. if (pkey == NULL) {
  806. BIO_printf(bio_err, "you need to specify a private key\n");
  807. goto end;
  808. }
  809. #ifndef OPENSSL_NO_DSA
  810. if (pkey->type == EVP_PKEY_DSA)
  811. digest = EVP_dss1();
  812. #endif
  813. #ifndef OPENSSL_NO_ECDSA
  814. if (pkey->type == EVP_PKEY_EC)
  815. digest = EVP_ecdsa();
  816. #endif
  817. if (req == NULL) {
  818. req = X509_REQ_new();
  819. if (req == NULL) {
  820. goto end;
  821. }
  822. i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
  823. subj = NULL; /* done processing '-subj' option */
  824. if ((kludge > 0)
  825. && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) {
  826. sk_X509_ATTRIBUTE_free(req->req_info->attributes);
  827. req->req_info->attributes = NULL;
  828. }
  829. if (!i) {
  830. BIO_printf(bio_err, "problems making Certificate Request\n");
  831. goto end;
  832. }
  833. }
  834. if (x509) {
  835. EVP_PKEY *tmppkey;
  836. X509V3_CTX ext_ctx;
  837. if ((x509ss = X509_new()) == NULL)
  838. goto end;
  839. /* Set version to V3 */
  840. if (extensions && !X509_set_version(x509ss, 2))
  841. goto end;
  842. if (serial) {
  843. if (!X509_set_serialNumber(x509ss, serial))
  844. goto end;
  845. } else {
  846. if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
  847. goto end;
  848. }
  849. if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
  850. goto end;
  851. if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
  852. goto end;
  853. if (!X509_gmtime_adj
  854. (X509_get_notAfter(x509ss), (long)60 * 60 * 24 * days))
  855. goto end;
  856. if (!X509_set_subject_name
  857. (x509ss, X509_REQ_get_subject_name(req)))
  858. goto end;
  859. tmppkey = X509_REQ_get_pubkey(req);
  860. if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
  861. goto end;
  862. EVP_PKEY_free(tmppkey);
  863. /* Set up V3 context struct */
  864. X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
  865. X509V3_set_nconf(&ext_ctx, req_conf);
  866. /* Add extensions */
  867. if (extensions && !X509V3_EXT_add_nconf(req_conf,
  868. &ext_ctx, extensions,
  869. x509ss)) {
  870. BIO_printf(bio_err, "Error Loading extension section %s\n",
  871. extensions);
  872. goto end;
  873. }
  874. if (!(i = X509_sign(x509ss, pkey, digest)))
  875. goto end;
  876. } else {
  877. X509V3_CTX ext_ctx;
  878. /* Set up V3 context struct */
  879. X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
  880. X509V3_set_nconf(&ext_ctx, req_conf);
  881. /* Add extensions */
  882. if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
  883. &ext_ctx, req_exts,
  884. req)) {
  885. BIO_printf(bio_err, "Error Loading extension section %s\n",
  886. req_exts);
  887. goto end;
  888. }
  889. if (!(i = X509_REQ_sign(req, pkey, digest)))
  890. goto end;
  891. }
  892. }
  893. if (subj && x509) {
  894. BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
  895. goto end;
  896. }
  897. if (subj && !x509) {
  898. if (verbose) {
  899. BIO_printf(bio_err, "Modifying Request's Subject\n");
  900. print_name(bio_err, "old subject=",
  901. X509_REQ_get_subject_name(req), nmflag);
  902. }
  903. if (build_subject(req, subj, chtype, multirdn) == 0) {
  904. BIO_printf(bio_err, "ERROR: cannot modify subject\n");
  905. ex = 1;
  906. goto end;
  907. }
  908. req->req_info->enc.modified = 1;
  909. if (verbose) {
  910. print_name(bio_err, "new subject=",
  911. X509_REQ_get_subject_name(req), nmflag);
  912. }
  913. }
  914. if (verify && !x509) {
  915. int tmp = 0;
  916. if (pkey == NULL) {
  917. pkey = X509_REQ_get_pubkey(req);
  918. tmp = 1;
  919. if (pkey == NULL)
  920. goto end;
  921. }
  922. i = X509_REQ_verify(req, pkey);
  923. if (tmp) {
  924. EVP_PKEY_free(pkey);
  925. pkey = NULL;
  926. }
  927. if (i < 0) {
  928. goto end;
  929. } else if (i == 0) {
  930. BIO_printf(bio_err, "verify failure\n");
  931. ERR_print_errors(bio_err);
  932. } else /* if (i > 0) */
  933. BIO_printf(bio_err, "verify OK\n");
  934. }
  935. if (noout && !text && !modulus && !subject && !pubkey) {
  936. ex = 0;
  937. goto end;
  938. }
  939. if (outfile == NULL) {
  940. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  941. #ifdef OPENSSL_SYS_VMS
  942. {
  943. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  944. out = BIO_push(tmpbio, out);
  945. }
  946. #endif
  947. } else {
  948. if ((keyout != NULL) && (strcmp(outfile, keyout) == 0))
  949. i = (int)BIO_append_filename(out, outfile);
  950. else
  951. i = (int)BIO_write_filename(out, outfile);
  952. if (!i) {
  953. perror(outfile);
  954. goto end;
  955. }
  956. }
  957. if (pubkey) {
  958. EVP_PKEY *tpubkey;
  959. tpubkey = X509_REQ_get_pubkey(req);
  960. if (tpubkey == NULL) {
  961. BIO_printf(bio_err, "Error getting public key\n");
  962. ERR_print_errors(bio_err);
  963. goto end;
  964. }
  965. PEM_write_bio_PUBKEY(out, tpubkey);
  966. EVP_PKEY_free(tpubkey);
  967. }
  968. if (text) {
  969. if (x509)
  970. X509_print_ex(out, x509ss, nmflag, reqflag);
  971. else
  972. X509_REQ_print_ex(out, req, nmflag, reqflag);
  973. }
  974. if (subject) {
  975. if (x509)
  976. print_name(out, "subject=", X509_get_subject_name(x509ss),
  977. nmflag);
  978. else
  979. print_name(out, "subject=", X509_REQ_get_subject_name(req),
  980. nmflag);
  981. }
  982. if (modulus) {
  983. EVP_PKEY *tpubkey;
  984. if (x509)
  985. tpubkey = X509_get_pubkey(x509ss);
  986. else
  987. tpubkey = X509_REQ_get_pubkey(req);
  988. if (tpubkey == NULL) {
  989. fprintf(stdout, "Modulus=unavailable\n");
  990. goto end;
  991. }
  992. fprintf(stdout, "Modulus=");
  993. #ifndef OPENSSL_NO_RSA
  994. if (tpubkey->type == EVP_PKEY_RSA)
  995. BN_print(out, tpubkey->pkey.rsa->n);
  996. else
  997. #endif
  998. fprintf(stdout, "Wrong Algorithm type");
  999. EVP_PKEY_free(tpubkey);
  1000. fprintf(stdout, "\n");
  1001. }
  1002. if (!noout && !x509) {
  1003. if (outformat == FORMAT_ASN1)
  1004. i = i2d_X509_REQ_bio(out, req);
  1005. else if (outformat == FORMAT_PEM) {
  1006. if (newhdr)
  1007. i = PEM_write_bio_X509_REQ_NEW(out, req);
  1008. else
  1009. i = PEM_write_bio_X509_REQ(out, req);
  1010. } else {
  1011. BIO_printf(bio_err, "bad output format specified for outfile\n");
  1012. goto end;
  1013. }
  1014. if (!i) {
  1015. BIO_printf(bio_err, "unable to write X509 request\n");
  1016. goto end;
  1017. }
  1018. }
  1019. if (!noout && x509 && (x509ss != NULL)) {
  1020. if (outformat == FORMAT_ASN1)
  1021. i = i2d_X509_bio(out, x509ss);
  1022. else if (outformat == FORMAT_PEM)
  1023. i = PEM_write_bio_X509(out, x509ss);
  1024. else {
  1025. BIO_printf(bio_err, "bad output format specified for outfile\n");
  1026. goto end;
  1027. }
  1028. if (!i) {
  1029. BIO_printf(bio_err, "unable to write X509 certificate\n");
  1030. goto end;
  1031. }
  1032. }
  1033. ex = 0;
  1034. end:
  1035. #ifndef MONOLITH
  1036. if (to_free)
  1037. OPENSSL_free(to_free);
  1038. #endif
  1039. if (ex) {
  1040. ERR_print_errors(bio_err);
  1041. }
  1042. if ((req_conf != NULL) && (req_conf != config))
  1043. NCONF_free(req_conf);
  1044. BIO_free(in);
  1045. BIO_free_all(out);
  1046. EVP_PKEY_free(pkey);
  1047. X509_REQ_free(req);
  1048. X509_free(x509ss);
  1049. ASN1_INTEGER_free(serial);
  1050. if (passargin && passin)
  1051. OPENSSL_free(passin);
  1052. if (passargout && passout)
  1053. OPENSSL_free(passout);
  1054. OBJ_cleanup();
  1055. #ifndef OPENSSL_NO_DSA
  1056. if (dsa_params != NULL)
  1057. DSA_free(dsa_params);
  1058. #endif
  1059. #ifndef OPENSSL_NO_ECDSA
  1060. if (ec_params != NULL)
  1061. EC_KEY_free(ec_params);
  1062. #endif
  1063. apps_shutdown();
  1064. OPENSSL_EXIT(ex);
  1065. }
  1066. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
  1067. int attribs, unsigned long chtype)
  1068. {
  1069. int ret = 0, i;
  1070. char no_prompt = 0;
  1071. STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
  1072. char *tmp, *dn_sect, *attr_sect;
  1073. tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
  1074. if (tmp == NULL)
  1075. ERR_clear_error();
  1076. if ((tmp != NULL) && !strcmp(tmp, "no"))
  1077. no_prompt = 1;
  1078. dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
  1079. if (dn_sect == NULL) {
  1080. BIO_printf(bio_err, "unable to find '%s' in config\n",
  1081. DISTINGUISHED_NAME);
  1082. goto err;
  1083. }
  1084. dn_sk = NCONF_get_section(req_conf, dn_sect);
  1085. if (dn_sk == NULL) {
  1086. BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
  1087. goto err;
  1088. }
  1089. attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
  1090. if (attr_sect == NULL) {
  1091. ERR_clear_error();
  1092. attr_sk = NULL;
  1093. } else {
  1094. attr_sk = NCONF_get_section(req_conf, attr_sect);
  1095. if (attr_sk == NULL) {
  1096. BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
  1097. goto err;
  1098. }
  1099. }
  1100. /* setup version number */
  1101. if (!X509_REQ_set_version(req, 0L))
  1102. goto err; /* version 1 */
  1103. if (no_prompt)
  1104. i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
  1105. else {
  1106. if (subj)
  1107. i = build_subject(req, subj, chtype, multirdn);
  1108. else
  1109. i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
  1110. chtype);
  1111. }
  1112. if (!i)
  1113. goto err;
  1114. if (!X509_REQ_set_pubkey(req, pkey))
  1115. goto err;
  1116. ret = 1;
  1117. err:
  1118. return (ret);
  1119. }
  1120. /*
  1121. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1122. * where characters may be escaped by \
  1123. */
  1124. static int build_subject(X509_REQ *req, char *subject, unsigned long chtype,
  1125. int multirdn)
  1126. {
  1127. X509_NAME *n;
  1128. if (!(n = parse_name(subject, chtype, multirdn)))
  1129. return 0;
  1130. if (!X509_REQ_set_subject_name(req, n)) {
  1131. X509_NAME_free(n);
  1132. return 0;
  1133. }
  1134. X509_NAME_free(n);
  1135. return 1;
  1136. }
  1137. static int prompt_info(X509_REQ *req,
  1138. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  1139. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  1140. int attribs, unsigned long chtype)
  1141. {
  1142. int i;
  1143. char *p, *q;
  1144. char buf[100];
  1145. int nid, mval;
  1146. long n_min, n_max;
  1147. char *type, *value;
  1148. const char *def;
  1149. CONF_VALUE *v;
  1150. X509_NAME *subj;
  1151. subj = X509_REQ_get_subject_name(req);
  1152. if (!batch) {
  1153. BIO_printf(bio_err,
  1154. "You are about to be asked to enter information that will be incorporated\n");
  1155. BIO_printf(bio_err, "into your certificate request.\n");
  1156. BIO_printf(bio_err,
  1157. "What you are about to enter is what is called a Distinguished Name or a DN.\n");
  1158. BIO_printf(bio_err,
  1159. "There are quite a few fields but you can leave some blank\n");
  1160. BIO_printf(bio_err,
  1161. "For some fields there will be a default value,\n");
  1162. BIO_printf(bio_err,
  1163. "If you enter '.', the field will be left blank.\n");
  1164. BIO_printf(bio_err, "-----\n");
  1165. }
  1166. if (sk_CONF_VALUE_num(dn_sk)) {
  1167. i = -1;
  1168. start:for (;;) {
  1169. i++;
  1170. if (sk_CONF_VALUE_num(dn_sk) <= i)
  1171. break;
  1172. v = sk_CONF_VALUE_value(dn_sk, i);
  1173. p = q = NULL;
  1174. type = v->name;
  1175. if (!check_end(type, "_min") || !check_end(type, "_max") ||
  1176. !check_end(type, "_default") || !check_end(type, "_value"))
  1177. continue;
  1178. /*
  1179. * Skip past any leading X. X: X, etc to allow for multiple
  1180. * instances
  1181. */
  1182. for (p = v->name; *p; p++)
  1183. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1184. p++;
  1185. if (*p)
  1186. type = p;
  1187. break;
  1188. }
  1189. if (*type == '+') {
  1190. mval = -1;
  1191. type++;
  1192. } else
  1193. mval = 0;
  1194. /* If OBJ not recognised ignore it */
  1195. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  1196. goto start;
  1197. if (BIO_snprintf(buf, sizeof buf, "%s_default", v->name)
  1198. >= (int)sizeof(buf)) {
  1199. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  1200. return 0;
  1201. }
  1202. if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  1203. ERR_clear_error();
  1204. def = "";
  1205. }
  1206. BIO_snprintf(buf, sizeof buf, "%s_value", v->name);
  1207. if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  1208. ERR_clear_error();
  1209. value = NULL;
  1210. }
  1211. BIO_snprintf(buf, sizeof buf, "%s_min", v->name);
  1212. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
  1213. ERR_clear_error();
  1214. n_min = -1;
  1215. }
  1216. BIO_snprintf(buf, sizeof buf, "%s_max", v->name);
  1217. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
  1218. ERR_clear_error();
  1219. n_max = -1;
  1220. }
  1221. if (!add_DN_object(subj, v->value, def, value, nid,
  1222. n_min, n_max, chtype, mval))
  1223. return 0;
  1224. }
  1225. if (X509_NAME_entry_count(subj) == 0) {
  1226. BIO_printf(bio_err,
  1227. "error, no objects specified in config file\n");
  1228. return 0;
  1229. }
  1230. if (attribs) {
  1231. if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
  1232. && (!batch)) {
  1233. BIO_printf(bio_err,
  1234. "\nPlease enter the following 'extra' attributes\n");
  1235. BIO_printf(bio_err,
  1236. "to be sent with your certificate request\n");
  1237. }
  1238. i = -1;
  1239. start2: for (;;) {
  1240. i++;
  1241. if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
  1242. break;
  1243. v = sk_CONF_VALUE_value(attr_sk, i);
  1244. type = v->name;
  1245. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  1246. goto start2;
  1247. if (BIO_snprintf(buf, sizeof buf, "%s_default", type)
  1248. >= (int)sizeof(buf)) {
  1249. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  1250. return 0;
  1251. }
  1252. if ((def = NCONF_get_string(req_conf, attr_sect, buf))
  1253. == NULL) {
  1254. ERR_clear_error();
  1255. def = "";
  1256. }
  1257. BIO_snprintf(buf, sizeof buf, "%s_value", type);
  1258. if ((value = NCONF_get_string(req_conf, attr_sect, buf))
  1259. == NULL) {
  1260. ERR_clear_error();
  1261. value = NULL;
  1262. }
  1263. BIO_snprintf(buf, sizeof buf, "%s_min", type);
  1264. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
  1265. ERR_clear_error();
  1266. n_min = -1;
  1267. }
  1268. BIO_snprintf(buf, sizeof buf, "%s_max", type);
  1269. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
  1270. ERR_clear_error();
  1271. n_max = -1;
  1272. }
  1273. if (!add_attribute_object(req,
  1274. v->value, def, value, nid, n_min,
  1275. n_max, chtype))
  1276. return 0;
  1277. }
  1278. }
  1279. } else {
  1280. BIO_printf(bio_err, "No template, please set one up.\n");
  1281. return 0;
  1282. }
  1283. return 1;
  1284. }
  1285. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
  1286. STACK_OF(CONF_VALUE) *attr_sk, int attribs,
  1287. unsigned long chtype)
  1288. {
  1289. int i;
  1290. char *p, *q;
  1291. char *type;
  1292. CONF_VALUE *v;
  1293. X509_NAME *subj;
  1294. subj = X509_REQ_get_subject_name(req);
  1295. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1296. int mval;
  1297. v = sk_CONF_VALUE_value(dn_sk, i);
  1298. p = q = NULL;
  1299. type = v->name;
  1300. /*
  1301. * Skip past any leading X. X: X, etc to allow for multiple instances
  1302. */
  1303. for (p = v->name; *p; p++)
  1304. #ifndef CHARSET_EBCDIC
  1305. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1306. #else
  1307. if ((*p == os_toascii[':']) || (*p == os_toascii[','])
  1308. || (*p == os_toascii['.'])) {
  1309. #endif
  1310. p++;
  1311. if (*p)
  1312. type = p;
  1313. break;
  1314. }
  1315. #ifndef CHARSET_EBCDIC
  1316. if (*p == '+')
  1317. #else
  1318. if (*p == os_toascii['+'])
  1319. #endif
  1320. {
  1321. p++;
  1322. mval = -1;
  1323. } else
  1324. mval = 0;
  1325. if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
  1326. (unsigned char *)v->value, -1, -1,
  1327. mval))
  1328. return 0;
  1329. }
  1330. if (!X509_NAME_entry_count(subj)) {
  1331. BIO_printf(bio_err, "error, no objects specified in config file\n");
  1332. return 0;
  1333. }
  1334. if (attribs) {
  1335. for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
  1336. v = sk_CONF_VALUE_value(attr_sk, i);
  1337. if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
  1338. (unsigned char *)v->value, -1))
  1339. return 0;
  1340. }
  1341. }
  1342. return 1;
  1343. }
  1344. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  1345. char *value, int nid, int n_min, int n_max,
  1346. unsigned long chtype, int mval)
  1347. {
  1348. int i, ret = 0;
  1349. MS_STATIC char buf[1024];
  1350. start:
  1351. if (!batch)
  1352. BIO_printf(bio_err, "%s [%s]:", text, def);
  1353. (void)BIO_flush(bio_err);
  1354. if (value != NULL) {
  1355. BUF_strlcpy(buf, value, sizeof buf);
  1356. BUF_strlcat(buf, "\n", sizeof buf);
  1357. BIO_printf(bio_err, "%s\n", value);
  1358. } else {
  1359. buf[0] = '\0';
  1360. if (!batch) {
  1361. if (!fgets(buf, sizeof buf, stdin))
  1362. return 0;
  1363. } else {
  1364. buf[0] = '\n';
  1365. buf[1] = '\0';
  1366. }
  1367. }
  1368. if (buf[0] == '\0')
  1369. return (0);
  1370. else if (buf[0] == '\n') {
  1371. if ((def == NULL) || (def[0] == '\0'))
  1372. return (1);
  1373. BUF_strlcpy(buf, def, sizeof buf);
  1374. BUF_strlcat(buf, "\n", sizeof buf);
  1375. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1376. return (1);
  1377. i = strlen(buf);
  1378. if (buf[i - 1] != '\n') {
  1379. BIO_printf(bio_err, "weird input :-(\n");
  1380. return (0);
  1381. }
  1382. buf[--i] = '\0';
  1383. #ifdef CHARSET_EBCDIC
  1384. ebcdic2ascii(buf, buf, i);
  1385. #endif
  1386. if (!req_check_len(i, n_min, n_max)) {
  1387. if (batch || value)
  1388. return 0;
  1389. goto start;
  1390. }
  1391. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1392. (unsigned char *)buf, -1, -1, mval))
  1393. goto err;
  1394. ret = 1;
  1395. err:
  1396. return (ret);
  1397. }
  1398. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  1399. char *value, int nid, int n_min,
  1400. int n_max, unsigned long chtype)
  1401. {
  1402. int i;
  1403. static char buf[1024];
  1404. start:
  1405. if (!batch)
  1406. BIO_printf(bio_err, "%s [%s]:", text, def);
  1407. (void)BIO_flush(bio_err);
  1408. if (value != NULL) {
  1409. BUF_strlcpy(buf, value, sizeof buf);
  1410. BUF_strlcat(buf, "\n", sizeof buf);
  1411. BIO_printf(bio_err, "%s\n", value);
  1412. } else {
  1413. buf[0] = '\0';
  1414. if (!batch) {
  1415. if (!fgets(buf, sizeof buf, stdin))
  1416. return 0;
  1417. } else {
  1418. buf[0] = '\n';
  1419. buf[1] = '\0';
  1420. }
  1421. }
  1422. if (buf[0] == '\0')
  1423. return (0);
  1424. else if (buf[0] == '\n') {
  1425. if ((def == NULL) || (def[0] == '\0'))
  1426. return (1);
  1427. BUF_strlcpy(buf, def, sizeof buf);
  1428. BUF_strlcat(buf, "\n", sizeof buf);
  1429. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1430. return (1);
  1431. i = strlen(buf);
  1432. if (buf[i - 1] != '\n') {
  1433. BIO_printf(bio_err, "weird input :-(\n");
  1434. return (0);
  1435. }
  1436. buf[--i] = '\0';
  1437. #ifdef CHARSET_EBCDIC
  1438. ebcdic2ascii(buf, buf, i);
  1439. #endif
  1440. if (!req_check_len(i, n_min, n_max)) {
  1441. if (batch || value)
  1442. return 0;
  1443. goto start;
  1444. }
  1445. if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
  1446. (unsigned char *)buf, -1)) {
  1447. BIO_printf(bio_err, "Error adding attribute\n");
  1448. ERR_print_errors(bio_err);
  1449. goto err;
  1450. }
  1451. return (1);
  1452. err:
  1453. return (0);
  1454. }
  1455. #ifndef OPENSSL_NO_RSA
  1456. static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb)
  1457. {
  1458. char c = '*';
  1459. if (p == 0)
  1460. c = '.';
  1461. if (p == 1)
  1462. c = '+';
  1463. if (p == 2)
  1464. c = '*';
  1465. if (p == 3)
  1466. c = '\n';
  1467. BIO_write(cb->arg, &c, 1);
  1468. (void)BIO_flush(cb->arg);
  1469. # ifdef LINT
  1470. p = n;
  1471. # endif
  1472. return 1;
  1473. }
  1474. #endif
  1475. static int req_check_len(int len, int n_min, int n_max)
  1476. {
  1477. if ((n_min > 0) && (len < n_min)) {
  1478. BIO_printf(bio_err,
  1479. "string is too short, it needs to be at least %d bytes long\n",
  1480. n_min);
  1481. return (0);
  1482. }
  1483. if ((n_max >= 0) && (len > n_max)) {
  1484. BIO_printf(bio_err,
  1485. "string is too long, it needs to be less than %d bytes long\n",
  1486. n_max);
  1487. return (0);
  1488. }
  1489. return (1);
  1490. }
  1491. /* Check if the end of a string matches 'end' */
  1492. static int check_end(const char *str, const char *end)
  1493. {
  1494. int elen, slen;
  1495. const char *tmp;
  1496. elen = strlen(end);
  1497. slen = strlen(str);
  1498. if (elen > slen)
  1499. return 1;
  1500. tmp = str + slen - elen;
  1501. return strcmp(tmp, end);
  1502. }