req.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <time.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/bio.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/conf.h>
  65. #include <openssl/err.h>
  66. #include <openssl/asn1.h>
  67. #include <openssl/x509.h>
  68. #include <openssl/x509v3.h>
  69. #include <openssl/objects.h>
  70. #include <openssl/pem.h>
  71. #include <openssl/bn.h>
  72. #ifndef OPENSSL_NO_RSA
  73. # include <openssl/rsa.h>
  74. #endif
  75. #ifndef OPENSSL_NO_DSA
  76. # include <openssl/dsa.h>
  77. #endif
  78. #define SECTION "req"
  79. #define BITS "default_bits"
  80. #define KEYFILE "default_keyfile"
  81. #define PROMPT "prompt"
  82. #define DISTINGUISHED_NAME "distinguished_name"
  83. #define ATTRIBUTES "attributes"
  84. #define V3_EXTENSIONS "x509_extensions"
  85. #define REQ_EXTENSIONS "req_extensions"
  86. #define STRING_MASK "string_mask"
  87. #define UTF8_IN "utf8"
  88. #define DEFAULT_KEY_LENGTH 512
  89. #define MIN_KEY_LENGTH 384
  90. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
  91. int attribs, unsigned long chtype);
  92. static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
  93. int multirdn);
  94. static int prompt_info(X509_REQ *req,
  95. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  96. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  97. int attribs, unsigned long chtype);
  98. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
  99. STACK_OF(CONF_VALUE) *attr, int attribs,
  100. unsigned long chtype);
  101. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  102. char *value, int nid, int n_min, int n_max,
  103. unsigned long chtype);
  104. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  105. char *value, int nid, int n_min, int n_max,
  106. unsigned long chtype, int mval);
  107. static int genpkey_cb(EVP_PKEY_CTX *ctx);
  108. static int req_check_len(int len, int n_min, int n_max);
  109. static int check_end(const char *str, const char *end);
  110. static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
  111. int *pkey_type, long *pkeylen,
  112. char **palgnam, ENGINE *keygen_engine);
  113. static CONF *req_conf = NULL;
  114. static int batch = 0;
  115. typedef enum OPTION_choice {
  116. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  117. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_KEYGEN_ENGINE, OPT_KEY,
  118. OPT_PUBKEY, OPT_NEW, OPT_CONFIG, OPT_KEYFORM, OPT_IN, OPT_OUT,
  119. OPT_KEYOUT, OPT_PASSIN, OPT_PASSOUT, OPT_RAND, OPT_NEWKEY,
  120. OPT_PKEYOPT, OPT_SIGOPT, OPT_BATCH, OPT_NEWHDR, OPT_MODULUS,
  121. OPT_VERIFY, OPT_NODES, OPT_NOOUT, OPT_VERBOSE, OPT_UTF8,
  122. OPT_NAMEOPT, OPT_REQOPT, OPT_SUBJECT, OPT_TEXT, OPT_X509,
  123. OPT_ASN1_KLUDGE, OPT_NO_ASN1_KLUDGE, OPT_MULTIVALUE_RDN,
  124. OPT_DAYS, OPT_SET_SERIAL, OPT_EXTENSIONS, OPT_REQEXTS, OPT_MD
  125. } OPTION_CHOICE;
  126. OPTIONS req_options[] = {
  127. {"help", OPT_HELP, '-', "Display this summary"},
  128. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  129. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  130. {"in", OPT_IN, '<', "Input file"},
  131. {"out", OPT_OUT, '>', "Output file"},
  132. {"key", OPT_KEY, '<', "Use the private key contained in file"},
  133. {"keyform", OPT_KEYFORM, 'F', "Key file format"},
  134. {"pubkey", OPT_PUBKEY, '-', "Output public key"},
  135. {"new", OPT_NEW, '-', "New request"},
  136. {"config", OPT_CONFIG, '<', "Request template file"},
  137. {"keyout", OPT_KEYOUT, '>', "File to send the key to"},
  138. {"passin", OPT_PASSIN, 's', "Private key password source"},
  139. {"passout", OPT_PASSOUT, 's'},
  140. {"rand", OPT_RAND, 's',
  141. "Load the file(s) into the random number generator"},
  142. {"newkey", OPT_NEWKEY, 's', "Specify as type:bits"},
  143. {"pkeyopt", OPT_PKEYOPT, 's'},
  144. {"sigopt", OPT_SIGOPT, 's'},
  145. {"batch", OPT_BATCH, '-',
  146. "Do not ask anything during request generation"},
  147. {"newhdr", OPT_NEWHDR, '-', "Output \"NEW\" in the header lines"},
  148. {"modulus", OPT_MODULUS, '-', "RSA modulus"},
  149. {"verify", OPT_VERIFY, '-', "Verify signature on REQ"},
  150. {"nodes", OPT_NODES, '-', "Don't encrypt the output key"},
  151. {"noout", OPT_NOOUT, '-', "Do not output REQ"},
  152. {"verbose", OPT_VERBOSE, '-'},
  153. {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"},
  154. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  155. {"reqopt", OPT_REQOPT, 's', "Various request text options"},
  156. {"text", OPT_TEXT, '-', "Text form of request"},
  157. {"x509", OPT_X509, '-',
  158. "Output a x509 structure instead of a cert request"},
  159. {"asn1-kludge", OPT_ASN1_KLUDGE, '-',
  160. "Output the request in a format that is wrong"},
  161. {OPT_MORE_STR, 1, 1, "(Required by some CA's)"},
  162. {"no-asn1-kludge", OPT_NO_ASN1_KLUDGE, '-'},
  163. {"subject", OPT_SUBJECT, 's', "Output the request's subject"},
  164. {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
  165. "Enable support for multivalued RDNs"},
  166. {"days", OPT_DAYS, 'p', "Number of days cert is valid for"},
  167. {"set-serial", OPT_SET_SERIAL, 'p', "Serial number to use"},
  168. {"extensions", OPT_EXTENSIONS, 's',
  169. "Cert extension section (override value in config file)"},
  170. {"reqexts", OPT_REQEXTS, 's',
  171. "Request extension section (override value in config file)"},
  172. #ifndef OPENSSL_NO_ENGINE
  173. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  174. {"keygen_engine", OPT_KEYGEN_ENGINE, 's'},
  175. #endif
  176. {"", OPT_MD, '-', "Any supported digest"},
  177. {NULL}
  178. };
  179. int req_main(int argc, char **argv)
  180. {
  181. ASN1_INTEGER *serial = NULL;
  182. BIO *in = NULL, *out = NULL;
  183. ENGINE *e = NULL, *gen_eng = NULL;
  184. EVP_PKEY *pkey = NULL;
  185. EVP_PKEY_CTX *genctx = NULL;
  186. STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL;
  187. X509 *x509ss = NULL;
  188. X509_REQ *req = NULL;
  189. const EVP_CIPHER *cipher = NULL;
  190. const EVP_MD *md_alg = NULL, *digest = NULL;
  191. char *extensions = NULL, *infile = NULL;
  192. char *outfile = NULL, *keyfile = NULL, *inrand = NULL;
  193. char *keyalgstr = NULL, *p, *prog, *passargin = NULL, *passargout = NULL;
  194. char *passin = NULL, *passout = NULL, *req_exts = NULL, *subj = NULL;
  195. char *template = NULL, *keyout = NULL;
  196. const char *keyalg = NULL;
  197. OPTION_CHOICE o;
  198. int ret = 1, x509 = 0, days = 30, i = 0, newreq = 0, verbose =
  199. 0, pkey_type = -1;
  200. int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyform = FORMAT_PEM;
  201. int modulus = 0, multirdn = 0, verify = 0, noout = 0, text = 0;
  202. int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
  203. long newkey = -1;
  204. unsigned long chtype = MBSTRING_ASC, nmflag = 0, reqflag = 0;
  205. #ifndef OPENSSL_NO_DES
  206. cipher = EVP_des_ede3_cbc();
  207. #endif
  208. prog = opt_init(argc, argv, req_options);
  209. while ((o = opt_next()) != OPT_EOF) {
  210. switch (o) {
  211. case OPT_EOF:
  212. case OPT_ERR:
  213. opthelp:
  214. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  215. goto end;
  216. case OPT_HELP:
  217. opt_help(req_options);
  218. ret = 0;
  219. goto end;
  220. case OPT_INFORM:
  221. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  222. goto opthelp;
  223. break;
  224. case OPT_OUTFORM:
  225. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  226. goto opthelp;
  227. break;
  228. case OPT_ENGINE:
  229. (void)setup_engine(opt_arg(), 0);
  230. break;
  231. case OPT_KEYGEN_ENGINE:
  232. #ifndef OPENSSL_NO_ENGINE
  233. gen_eng = ENGINE_by_id(opt_arg());
  234. if (gen_eng == NULL) {
  235. BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
  236. goto opthelp;
  237. }
  238. #endif
  239. break;
  240. case OPT_KEY:
  241. keyfile = opt_arg();
  242. break;
  243. case OPT_PUBKEY:
  244. pubkey = 1;
  245. break;
  246. case OPT_NEW:
  247. newreq = 1;
  248. break;
  249. case OPT_CONFIG:
  250. template = opt_arg();
  251. break;
  252. case OPT_KEYFORM:
  253. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyform))
  254. goto opthelp;
  255. break;
  256. case OPT_IN:
  257. infile = opt_arg();
  258. break;
  259. case OPT_OUT:
  260. outfile = opt_arg();
  261. break;
  262. case OPT_KEYOUT:
  263. keyout = opt_arg();
  264. break;
  265. case OPT_PASSIN:
  266. passargin = opt_arg();
  267. break;
  268. case OPT_PASSOUT:
  269. passargout = opt_arg();
  270. break;
  271. case OPT_RAND:
  272. inrand = opt_arg();
  273. break;
  274. case OPT_NEWKEY:
  275. keyalg = opt_arg();
  276. newreq = 1;
  277. break;
  278. case OPT_PKEYOPT:
  279. if (!pkeyopts)
  280. pkeyopts = sk_OPENSSL_STRING_new_null();
  281. if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, opt_arg()))
  282. goto opthelp;
  283. break;
  284. case OPT_SIGOPT:
  285. if (!sigopts)
  286. sigopts = sk_OPENSSL_STRING_new_null();
  287. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  288. goto opthelp;
  289. break;
  290. case OPT_BATCH:
  291. batch = 1;
  292. break;
  293. case OPT_NEWHDR:
  294. newhdr = 1;
  295. break;
  296. case OPT_MODULUS:
  297. modulus = 1;
  298. break;
  299. case OPT_VERIFY:
  300. verify = 1;
  301. break;
  302. case OPT_NODES:
  303. nodes = 1;
  304. break;
  305. case OPT_NOOUT:
  306. noout = 1;
  307. break;
  308. case OPT_VERBOSE:
  309. verbose = 1;
  310. break;
  311. case OPT_UTF8:
  312. chtype = MBSTRING_UTF8;
  313. break;
  314. case OPT_NAMEOPT:
  315. if (!set_name_ex(&nmflag, opt_arg()))
  316. goto opthelp;
  317. break;
  318. case OPT_REQOPT:
  319. if (!set_cert_ex(&reqflag, opt_arg()))
  320. goto opthelp;
  321. break;
  322. case OPT_TEXT:
  323. text = 1;
  324. break;
  325. case OPT_X509:
  326. x509 = 1;
  327. break;
  328. case OPT_ASN1_KLUDGE:
  329. kludge = 1;
  330. break;
  331. case OPT_NO_ASN1_KLUDGE:
  332. kludge = 0;
  333. break;
  334. multirdn = 1;
  335. case OPT_DAYS:
  336. days = atoi(opt_arg());
  337. break;
  338. case OPT_SET_SERIAL:
  339. serial = s2i_ASN1_INTEGER(NULL, opt_arg());
  340. if (serial == NULL)
  341. goto opthelp;
  342. break;
  343. case OPT_SUBJECT:
  344. subj = opt_arg();
  345. break;
  346. case OPT_MULTIVALUE_RDN:
  347. multirdn = 1;
  348. break;
  349. case OPT_EXTENSIONS:
  350. extensions = opt_arg();
  351. break;
  352. case OPT_REQEXTS:
  353. req_exts = opt_arg();
  354. break;
  355. case OPT_MD:
  356. if (!opt_md(opt_unknown(), &md_alg))
  357. goto opthelp;
  358. digest = md_alg;
  359. break;
  360. }
  361. }
  362. argc = opt_num_rest();
  363. argv = opt_rest();
  364. if (!app_passwd(passargin, passargout, &passin, &passout)) {
  365. BIO_printf(bio_err, "Error getting passwords\n");
  366. goto end;
  367. }
  368. if (template != NULL) {
  369. long errline = -1;
  370. if (verbose)
  371. BIO_printf(bio_err, "Using configuration from %s\n", template);
  372. req_conf = NCONF_new(NULL);
  373. i = NCONF_load(req_conf, template, &errline);
  374. if (i == 0) {
  375. BIO_printf(bio_err, "error on line %ld of %s\n", errline,
  376. template);
  377. goto end;
  378. }
  379. } else {
  380. req_conf = config;
  381. if (req_conf == NULL) {
  382. BIO_printf(bio_err, "Unable to load config info from %s\n",
  383. default_config_file);
  384. if (newreq)
  385. goto end;
  386. } else if (verbose)
  387. BIO_printf(bio_err, "Using configuration from %s\n",
  388. default_config_file);
  389. }
  390. if (req_conf != NULL) {
  391. p = NCONF_get_string(req_conf, NULL, "oid_file");
  392. if (p == NULL)
  393. ERR_clear_error();
  394. if (p != NULL) {
  395. BIO *oid_bio;
  396. oid_bio = BIO_new_file(p, "r");
  397. if (oid_bio == NULL) {
  398. /*-
  399. BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
  400. ERR_print_errors(bio_err);
  401. */
  402. } else {
  403. OBJ_create_objects(oid_bio);
  404. BIO_free(oid_bio);
  405. }
  406. }
  407. }
  408. if (!add_oid_section(req_conf))
  409. goto end;
  410. if (md_alg == NULL) {
  411. p = NCONF_get_string(req_conf, SECTION, "default_md");
  412. if (p == NULL)
  413. ERR_clear_error();
  414. else {
  415. if (!opt_md(p, &md_alg))
  416. goto opthelp;
  417. digest = md_alg;
  418. }
  419. }
  420. if (!extensions) {
  421. extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
  422. if (!extensions)
  423. ERR_clear_error();
  424. }
  425. if (extensions) {
  426. /* Check syntax of file */
  427. X509V3_CTX ctx;
  428. X509V3_set_ctx_test(&ctx);
  429. X509V3_set_nconf(&ctx, req_conf);
  430. if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
  431. BIO_printf(bio_err,
  432. "Error Loading extension section %s\n", extensions);
  433. goto end;
  434. }
  435. }
  436. if (!passin) {
  437. passin = NCONF_get_string(req_conf, SECTION, "input_password");
  438. if (!passin)
  439. ERR_clear_error();
  440. }
  441. if (!passout) {
  442. passout = NCONF_get_string(req_conf, SECTION, "output_password");
  443. if (!passout)
  444. ERR_clear_error();
  445. }
  446. p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
  447. if (!p)
  448. ERR_clear_error();
  449. if (p && !ASN1_STRING_set_default_mask_asc(p)) {
  450. BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
  451. goto end;
  452. }
  453. if (chtype != MBSTRING_UTF8) {
  454. p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
  455. if (!p)
  456. ERR_clear_error();
  457. else if (strcmp(p, "yes") == 0)
  458. chtype = MBSTRING_UTF8;
  459. }
  460. if (!req_exts) {
  461. req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
  462. if (!req_exts)
  463. ERR_clear_error();
  464. }
  465. if (req_exts) {
  466. /* Check syntax of file */
  467. X509V3_CTX ctx;
  468. X509V3_set_ctx_test(&ctx);
  469. X509V3_set_nconf(&ctx, req_conf);
  470. if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
  471. BIO_printf(bio_err,
  472. "Error Loading request extension section %s\n",
  473. req_exts);
  474. goto end;
  475. }
  476. }
  477. if (keyfile != NULL) {
  478. pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
  479. if (!pkey) {
  480. /* load_key() has already printed an appropriate message */
  481. goto end;
  482. } else {
  483. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  484. if (randfile == NULL)
  485. ERR_clear_error();
  486. app_RAND_load_file(randfile, 0);
  487. }
  488. }
  489. if (newreq && (pkey == NULL)) {
  490. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  491. if (randfile == NULL)
  492. ERR_clear_error();
  493. app_RAND_load_file(randfile, 0);
  494. if (inrand)
  495. app_RAND_load_files(inrand);
  496. if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
  497. newkey = DEFAULT_KEY_LENGTH;
  498. }
  499. if (keyalg) {
  500. genctx = set_keygen_ctx(keyalg, &pkey_type, &newkey,
  501. &keyalgstr, gen_eng);
  502. if (!genctx)
  503. goto end;
  504. }
  505. if (newkey < MIN_KEY_LENGTH
  506. && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
  507. BIO_printf(bio_err, "private key length is too short,\n");
  508. BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
  509. MIN_KEY_LENGTH, newkey);
  510. goto end;
  511. }
  512. if (!genctx) {
  513. genctx = set_keygen_ctx(NULL, &pkey_type, &newkey,
  514. &keyalgstr, gen_eng);
  515. if (!genctx)
  516. goto end;
  517. }
  518. if (pkeyopts) {
  519. char *genopt;
  520. for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
  521. genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
  522. if (pkey_ctrl_string(genctx, genopt) <= 0) {
  523. BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
  524. ERR_print_errors(bio_err);
  525. goto end;
  526. }
  527. }
  528. }
  529. BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
  530. newkey, keyalgstr);
  531. EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
  532. EVP_PKEY_CTX_set_app_data(genctx, bio_err);
  533. if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
  534. BIO_puts(bio_err, "Error Generating Key\n");
  535. goto end;
  536. }
  537. EVP_PKEY_CTX_free(genctx);
  538. genctx = NULL;
  539. app_RAND_write_file(randfile);
  540. if (keyout == NULL) {
  541. keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
  542. if (keyout == NULL)
  543. ERR_clear_error();
  544. }
  545. if (keyout == NULL)
  546. BIO_printf(bio_err, "writing new private key to stdout\n");
  547. else
  548. BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
  549. out = bio_open_default(keyout, "w");
  550. if (out == NULL)
  551. goto end;
  552. p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
  553. if (p == NULL) {
  554. ERR_clear_error();
  555. p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
  556. if (p == NULL)
  557. ERR_clear_error();
  558. }
  559. if ((p != NULL) && (strcmp(p, "no") == 0))
  560. cipher = NULL;
  561. if (nodes)
  562. cipher = NULL;
  563. i = 0;
  564. loop:
  565. if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
  566. NULL, 0, NULL, passout)) {
  567. if ((ERR_GET_REASON(ERR_peek_error()) ==
  568. PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
  569. ERR_clear_error();
  570. i++;
  571. goto loop;
  572. }
  573. goto end;
  574. }
  575. BIO_printf(bio_err, "-----\n");
  576. }
  577. if (!newreq) {
  578. /*
  579. * Since we are using a pre-existing certificate request, the kludge
  580. * 'format' info should not be changed.
  581. */
  582. kludge = -1;
  583. in = bio_open_default(infile, RB(informat));
  584. if (in == NULL)
  585. goto end;
  586. if (informat == FORMAT_ASN1)
  587. req = d2i_X509_REQ_bio(in, NULL);
  588. else
  589. req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
  590. if (req == NULL) {
  591. BIO_printf(bio_err, "unable to load X509 request\n");
  592. goto end;
  593. }
  594. }
  595. if (newreq || x509) {
  596. if (pkey == NULL) {
  597. BIO_printf(bio_err, "you need to specify a private key\n");
  598. goto end;
  599. }
  600. if (req == NULL) {
  601. req = X509_REQ_new();
  602. if (req == NULL) {
  603. goto end;
  604. }
  605. i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
  606. subj = NULL; /* done processing '-subj' option */
  607. if ((kludge > 0)
  608. && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) {
  609. sk_X509_ATTRIBUTE_free(req->req_info->attributes);
  610. req->req_info->attributes = NULL;
  611. }
  612. if (!i) {
  613. BIO_printf(bio_err, "problems making Certificate Request\n");
  614. goto end;
  615. }
  616. }
  617. if (x509) {
  618. EVP_PKEY *tmppkey;
  619. X509V3_CTX ext_ctx;
  620. if ((x509ss = X509_new()) == NULL)
  621. goto end;
  622. /* Set version to V3 */
  623. if (extensions && !X509_set_version(x509ss, 2))
  624. goto end;
  625. if (serial) {
  626. if (!X509_set_serialNumber(x509ss, serial))
  627. goto end;
  628. } else {
  629. if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
  630. goto end;
  631. }
  632. if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
  633. goto end;
  634. if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
  635. goto end;
  636. if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
  637. goto end;
  638. if (!X509_set_subject_name
  639. (x509ss, X509_REQ_get_subject_name(req)))
  640. goto end;
  641. tmppkey = X509_REQ_get_pubkey(req);
  642. if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
  643. goto end;
  644. EVP_PKEY_free(tmppkey);
  645. /* Set up V3 context struct */
  646. X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
  647. X509V3_set_nconf(&ext_ctx, req_conf);
  648. /* Add extensions */
  649. if (extensions && !X509V3_EXT_add_nconf(req_conf,
  650. &ext_ctx, extensions,
  651. x509ss)) {
  652. BIO_printf(bio_err, "Error Loading extension section %s\n",
  653. extensions);
  654. goto end;
  655. }
  656. i = do_X509_sign(x509ss, pkey, digest, sigopts);
  657. if (!i) {
  658. ERR_print_errors(bio_err);
  659. goto end;
  660. }
  661. } else {
  662. X509V3_CTX ext_ctx;
  663. /* Set up V3 context struct */
  664. X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
  665. X509V3_set_nconf(&ext_ctx, req_conf);
  666. /* Add extensions */
  667. if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
  668. &ext_ctx, req_exts,
  669. req)) {
  670. BIO_printf(bio_err, "Error Loading extension section %s\n",
  671. req_exts);
  672. goto end;
  673. }
  674. i = do_X509_REQ_sign(req, pkey, digest, sigopts);
  675. if (!i) {
  676. ERR_print_errors(bio_err);
  677. goto end;
  678. }
  679. }
  680. }
  681. if (subj && x509) {
  682. BIO_printf(bio_err, "Cannot modify certificate subject\n");
  683. goto end;
  684. }
  685. if (subj && !x509) {
  686. if (verbose) {
  687. BIO_printf(bio_err, "Modifying Request's Subject\n");
  688. print_name(bio_err, "old subject=",
  689. X509_REQ_get_subject_name(req), nmflag);
  690. }
  691. if (build_subject(req, subj, chtype, multirdn) == 0) {
  692. BIO_printf(bio_err, "ERROR: cannot modify subject\n");
  693. ret = 1;
  694. goto end;
  695. }
  696. req->req_info->enc.modified = 1;
  697. if (verbose) {
  698. print_name(bio_err, "new subject=",
  699. X509_REQ_get_subject_name(req), nmflag);
  700. }
  701. }
  702. if (verify && !x509) {
  703. int tmp = 0;
  704. if (pkey == NULL) {
  705. pkey = X509_REQ_get_pubkey(req);
  706. tmp = 1;
  707. if (pkey == NULL)
  708. goto end;
  709. }
  710. i = X509_REQ_verify(req, pkey);
  711. if (tmp) {
  712. EVP_PKEY_free(pkey);
  713. pkey = NULL;
  714. }
  715. if (i < 0) {
  716. goto end;
  717. } else if (i == 0) {
  718. BIO_printf(bio_err, "verify failure\n");
  719. ERR_print_errors(bio_err);
  720. } else /* if (i > 0) */
  721. BIO_printf(bio_err, "verify OK\n");
  722. }
  723. if (noout && !text && !modulus && !subject && !pubkey) {
  724. ret = 0;
  725. goto end;
  726. }
  727. out = bio_open_default(outfile,
  728. keyout != NULL && outfile != NULL &&
  729. strcmp(keyout, outfile) == 0 ? "a" : "w");
  730. if (out == NULL)
  731. goto end;
  732. if (pubkey) {
  733. EVP_PKEY *tpubkey;
  734. tpubkey = X509_REQ_get_pubkey(req);
  735. if (tpubkey == NULL) {
  736. BIO_printf(bio_err, "Error getting public key\n");
  737. ERR_print_errors(bio_err);
  738. goto end;
  739. }
  740. PEM_write_bio_PUBKEY(out, tpubkey);
  741. EVP_PKEY_free(tpubkey);
  742. }
  743. if (text) {
  744. if (x509)
  745. X509_print_ex(out, x509ss, nmflag, reqflag);
  746. else
  747. X509_REQ_print_ex(out, req, nmflag, reqflag);
  748. }
  749. if (subject) {
  750. if (x509)
  751. print_name(out, "subject=", X509_get_subject_name(x509ss),
  752. nmflag);
  753. else
  754. print_name(out, "subject=", X509_REQ_get_subject_name(req),
  755. nmflag);
  756. }
  757. if (modulus) {
  758. EVP_PKEY *tpubkey;
  759. if (x509)
  760. tpubkey = X509_get_pubkey(x509ss);
  761. else
  762. tpubkey = X509_REQ_get_pubkey(req);
  763. if (tpubkey == NULL) {
  764. fprintf(stdout, "Modulus=unavailable\n");
  765. goto end;
  766. }
  767. fprintf(stdout, "Modulus=");
  768. #ifndef OPENSSL_NO_RSA
  769. if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA)
  770. BN_print(out, tpubkey->pkey.rsa->n);
  771. else
  772. #endif
  773. fprintf(stdout, "Wrong Algorithm type");
  774. EVP_PKEY_free(tpubkey);
  775. fprintf(stdout, "\n");
  776. }
  777. if (!noout && !x509) {
  778. if (outformat == FORMAT_ASN1)
  779. i = i2d_X509_REQ_bio(out, req);
  780. else if (newhdr)
  781. i = PEM_write_bio_X509_REQ_NEW(out, req);
  782. else
  783. i = PEM_write_bio_X509_REQ(out, req);
  784. if (!i) {
  785. BIO_printf(bio_err, "unable to write X509 request\n");
  786. goto end;
  787. }
  788. }
  789. if (!noout && x509 && (x509ss != NULL)) {
  790. if (outformat == FORMAT_ASN1)
  791. i = i2d_X509_bio(out, x509ss);
  792. else
  793. i = PEM_write_bio_X509(out, x509ss);
  794. if (!i) {
  795. BIO_printf(bio_err, "unable to write X509 certificate\n");
  796. goto end;
  797. }
  798. }
  799. ret = 0;
  800. end:
  801. if (ret) {
  802. ERR_print_errors(bio_err);
  803. }
  804. if (req_conf != config)
  805. NCONF_free(req_conf);
  806. BIO_free(in);
  807. BIO_free_all(out);
  808. EVP_PKEY_free(pkey);
  809. EVP_PKEY_CTX_free(genctx);
  810. sk_OPENSSL_STRING_free(pkeyopts);
  811. sk_OPENSSL_STRING_free(sigopts);
  812. #ifndef OPENSSL_NO_ENGINE
  813. ENGINE_free(gen_eng);
  814. #endif
  815. OPENSSL_free(keyalgstr);
  816. X509_REQ_free(req);
  817. X509_free(x509ss);
  818. ASN1_INTEGER_free(serial);
  819. OPENSSL_free(passin);
  820. OPENSSL_free(passout);
  821. OBJ_cleanup();
  822. return (ret);
  823. }
  824. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
  825. int attribs, unsigned long chtype)
  826. {
  827. int ret = 0, i;
  828. char no_prompt = 0;
  829. STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
  830. char *tmp, *dn_sect, *attr_sect;
  831. tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
  832. if (tmp == NULL)
  833. ERR_clear_error();
  834. if ((tmp != NULL) && strcmp(tmp, "no") == 0)
  835. no_prompt = 1;
  836. dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
  837. if (dn_sect == NULL) {
  838. BIO_printf(bio_err, "unable to find '%s' in config\n",
  839. DISTINGUISHED_NAME);
  840. goto err;
  841. }
  842. dn_sk = NCONF_get_section(req_conf, dn_sect);
  843. if (dn_sk == NULL) {
  844. BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
  845. goto err;
  846. }
  847. attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
  848. if (attr_sect == NULL) {
  849. ERR_clear_error();
  850. attr_sk = NULL;
  851. } else {
  852. attr_sk = NCONF_get_section(req_conf, attr_sect);
  853. if (attr_sk == NULL) {
  854. BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
  855. goto err;
  856. }
  857. }
  858. /* setup version number */
  859. if (!X509_REQ_set_version(req, 0L))
  860. goto err; /* version 1 */
  861. if (subj)
  862. i = build_subject(req, subj, chtype, multirdn);
  863. else if (no_prompt)
  864. i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
  865. else
  866. i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
  867. chtype);
  868. if (!i)
  869. goto err;
  870. if (!X509_REQ_set_pubkey(req, pkey))
  871. goto err;
  872. ret = 1;
  873. err:
  874. return (ret);
  875. }
  876. /*
  877. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  878. * where characters may be escaped by \
  879. */
  880. static int build_subject(X509_REQ *req, char *subject, unsigned long chtype,
  881. int multirdn)
  882. {
  883. X509_NAME *n;
  884. if (!(n = parse_name(subject, chtype, multirdn)))
  885. return 0;
  886. if (!X509_REQ_set_subject_name(req, n)) {
  887. X509_NAME_free(n);
  888. return 0;
  889. }
  890. X509_NAME_free(n);
  891. return 1;
  892. }
  893. static int prompt_info(X509_REQ *req,
  894. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  895. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  896. int attribs, unsigned long chtype)
  897. {
  898. int i;
  899. char *p, *q;
  900. char buf[100];
  901. int nid, mval;
  902. long n_min, n_max;
  903. char *type, *value;
  904. const char *def;
  905. CONF_VALUE *v;
  906. X509_NAME *subj;
  907. subj = X509_REQ_get_subject_name(req);
  908. if (!batch) {
  909. BIO_printf(bio_err,
  910. "You are about to be asked to enter information that will be incorporated\n");
  911. BIO_printf(bio_err, "into your certificate request.\n");
  912. BIO_printf(bio_err,
  913. "What you are about to enter is what is called a Distinguished Name or a DN.\n");
  914. BIO_printf(bio_err,
  915. "There are quite a few fields but you can leave some blank\n");
  916. BIO_printf(bio_err,
  917. "For some fields there will be a default value,\n");
  918. BIO_printf(bio_err,
  919. "If you enter '.', the field will be left blank.\n");
  920. BIO_printf(bio_err, "-----\n");
  921. }
  922. if (sk_CONF_VALUE_num(dn_sk)) {
  923. i = -1;
  924. start:for (;;) {
  925. i++;
  926. if (sk_CONF_VALUE_num(dn_sk) <= i)
  927. break;
  928. v = sk_CONF_VALUE_value(dn_sk, i);
  929. p = q = NULL;
  930. type = v->name;
  931. if (!check_end(type, "_min") || !check_end(type, "_max") ||
  932. !check_end(type, "_default") || !check_end(type, "_value"))
  933. continue;
  934. /*
  935. * Skip past any leading X. X: X, etc to allow for multiple
  936. * instances
  937. */
  938. for (p = v->name; *p; p++)
  939. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  940. p++;
  941. if (*p)
  942. type = p;
  943. break;
  944. }
  945. if (*type == '+') {
  946. mval = -1;
  947. type++;
  948. } else
  949. mval = 0;
  950. /* If OBJ not recognised ignore it */
  951. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  952. goto start;
  953. if (BIO_snprintf(buf, sizeof buf, "%s_default", v->name)
  954. >= (int)sizeof(buf)) {
  955. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  956. return 0;
  957. }
  958. if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  959. ERR_clear_error();
  960. def = "";
  961. }
  962. BIO_snprintf(buf, sizeof buf, "%s_value", v->name);
  963. if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  964. ERR_clear_error();
  965. value = NULL;
  966. }
  967. BIO_snprintf(buf, sizeof buf, "%s_min", v->name);
  968. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
  969. ERR_clear_error();
  970. n_min = -1;
  971. }
  972. BIO_snprintf(buf, sizeof buf, "%s_max", v->name);
  973. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
  974. ERR_clear_error();
  975. n_max = -1;
  976. }
  977. if (!add_DN_object(subj, v->value, def, value, nid,
  978. n_min, n_max, chtype, mval))
  979. return 0;
  980. }
  981. if (X509_NAME_entry_count(subj) == 0) {
  982. BIO_printf(bio_err,
  983. "error, no objects specified in config file\n");
  984. return 0;
  985. }
  986. if (attribs) {
  987. if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
  988. && (!batch)) {
  989. BIO_printf(bio_err,
  990. "\nPlease enter the following 'extra' attributes\n");
  991. BIO_printf(bio_err,
  992. "to be sent with your certificate request\n");
  993. }
  994. i = -1;
  995. start2: for (;;) {
  996. i++;
  997. if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
  998. break;
  999. v = sk_CONF_VALUE_value(attr_sk, i);
  1000. type = v->name;
  1001. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  1002. goto start2;
  1003. if (BIO_snprintf(buf, sizeof buf, "%s_default", type)
  1004. >= (int)sizeof(buf)) {
  1005. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  1006. return 0;
  1007. }
  1008. if ((def = NCONF_get_string(req_conf, attr_sect, buf))
  1009. == NULL) {
  1010. ERR_clear_error();
  1011. def = "";
  1012. }
  1013. BIO_snprintf(buf, sizeof buf, "%s_value", type);
  1014. if ((value = NCONF_get_string(req_conf, attr_sect, buf))
  1015. == NULL) {
  1016. ERR_clear_error();
  1017. value = NULL;
  1018. }
  1019. BIO_snprintf(buf, sizeof buf, "%s_min", type);
  1020. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
  1021. ERR_clear_error();
  1022. n_min = -1;
  1023. }
  1024. BIO_snprintf(buf, sizeof buf, "%s_max", type);
  1025. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
  1026. ERR_clear_error();
  1027. n_max = -1;
  1028. }
  1029. if (!add_attribute_object(req,
  1030. v->value, def, value, nid, n_min,
  1031. n_max, chtype))
  1032. return 0;
  1033. }
  1034. }
  1035. } else {
  1036. BIO_printf(bio_err, "No template, please set one up.\n");
  1037. return 0;
  1038. }
  1039. return 1;
  1040. }
  1041. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
  1042. STACK_OF(CONF_VALUE) *attr_sk, int attribs,
  1043. unsigned long chtype)
  1044. {
  1045. int i;
  1046. char *p, *q;
  1047. char *type;
  1048. CONF_VALUE *v;
  1049. X509_NAME *subj;
  1050. subj = X509_REQ_get_subject_name(req);
  1051. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1052. int mval;
  1053. v = sk_CONF_VALUE_value(dn_sk, i);
  1054. p = q = NULL;
  1055. type = v->name;
  1056. /*
  1057. * Skip past any leading X. X: X, etc to allow for multiple instances
  1058. */
  1059. for (p = v->name; *p; p++)
  1060. #ifndef CHARSET_EBCDIC
  1061. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1062. #else
  1063. if ((*p == os_toascii[':']) || (*p == os_toascii[','])
  1064. || (*p == os_toascii['.'])) {
  1065. #endif
  1066. p++;
  1067. if (*p)
  1068. type = p;
  1069. break;
  1070. }
  1071. #ifndef CHARSET_EBCDIC
  1072. if (*p == '+')
  1073. #else
  1074. if (*p == os_toascii['+'])
  1075. #endif
  1076. {
  1077. p++;
  1078. mval = -1;
  1079. } else
  1080. mval = 0;
  1081. if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
  1082. (unsigned char *)v->value, -1, -1,
  1083. mval))
  1084. return 0;
  1085. }
  1086. if (!X509_NAME_entry_count(subj)) {
  1087. BIO_printf(bio_err, "error, no objects specified in config file\n");
  1088. return 0;
  1089. }
  1090. if (attribs) {
  1091. for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
  1092. v = sk_CONF_VALUE_value(attr_sk, i);
  1093. if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
  1094. (unsigned char *)v->value, -1))
  1095. return 0;
  1096. }
  1097. }
  1098. return 1;
  1099. }
  1100. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  1101. char *value, int nid, int n_min, int n_max,
  1102. unsigned long chtype, int mval)
  1103. {
  1104. int i, ret = 0;
  1105. char buf[1024];
  1106. start:
  1107. if (!batch)
  1108. BIO_printf(bio_err, "%s [%s]:", text, def);
  1109. (void)BIO_flush(bio_err);
  1110. if (value != NULL) {
  1111. BUF_strlcpy(buf, value, sizeof buf);
  1112. BUF_strlcat(buf, "\n", sizeof buf);
  1113. BIO_printf(bio_err, "%s\n", value);
  1114. } else {
  1115. buf[0] = '\0';
  1116. if (!batch) {
  1117. if (!fgets(buf, sizeof buf, stdin))
  1118. return 0;
  1119. } else {
  1120. buf[0] = '\n';
  1121. buf[1] = '\0';
  1122. }
  1123. }
  1124. if (buf[0] == '\0')
  1125. return (0);
  1126. else if (buf[0] == '\n') {
  1127. if ((def == NULL) || (def[0] == '\0'))
  1128. return (1);
  1129. BUF_strlcpy(buf, def, sizeof buf);
  1130. BUF_strlcat(buf, "\n", sizeof buf);
  1131. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1132. return (1);
  1133. i = strlen(buf);
  1134. if (buf[i - 1] != '\n') {
  1135. BIO_printf(bio_err, "weird input :-(\n");
  1136. return (0);
  1137. }
  1138. buf[--i] = '\0';
  1139. #ifdef CHARSET_EBCDIC
  1140. ebcdic2ascii(buf, buf, i);
  1141. #endif
  1142. if (!req_check_len(i, n_min, n_max)) {
  1143. if (batch || value)
  1144. return 0;
  1145. goto start;
  1146. }
  1147. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1148. (unsigned char *)buf, -1, -1, mval))
  1149. goto err;
  1150. ret = 1;
  1151. err:
  1152. return (ret);
  1153. }
  1154. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  1155. char *value, int nid, int n_min,
  1156. int n_max, unsigned long chtype)
  1157. {
  1158. int i;
  1159. static char buf[1024];
  1160. start:
  1161. if (!batch)
  1162. BIO_printf(bio_err, "%s [%s]:", text, def);
  1163. (void)BIO_flush(bio_err);
  1164. if (value != NULL) {
  1165. BUF_strlcpy(buf, value, sizeof buf);
  1166. BUF_strlcat(buf, "\n", sizeof buf);
  1167. BIO_printf(bio_err, "%s\n", value);
  1168. } else {
  1169. buf[0] = '\0';
  1170. if (!batch) {
  1171. if (!fgets(buf, sizeof buf, stdin))
  1172. return 0;
  1173. } else {
  1174. buf[0] = '\n';
  1175. buf[1] = '\0';
  1176. }
  1177. }
  1178. if (buf[0] == '\0')
  1179. return (0);
  1180. else if (buf[0] == '\n') {
  1181. if ((def == NULL) || (def[0] == '\0'))
  1182. return (1);
  1183. BUF_strlcpy(buf, def, sizeof buf);
  1184. BUF_strlcat(buf, "\n", sizeof buf);
  1185. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1186. return (1);
  1187. i = strlen(buf);
  1188. if (buf[i - 1] != '\n') {
  1189. BIO_printf(bio_err, "weird input :-(\n");
  1190. return (0);
  1191. }
  1192. buf[--i] = '\0';
  1193. #ifdef CHARSET_EBCDIC
  1194. ebcdic2ascii(buf, buf, i);
  1195. #endif
  1196. if (!req_check_len(i, n_min, n_max)) {
  1197. if (batch || value)
  1198. return 0;
  1199. goto start;
  1200. }
  1201. if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
  1202. (unsigned char *)buf, -1)) {
  1203. BIO_printf(bio_err, "Error adding attribute\n");
  1204. ERR_print_errors(bio_err);
  1205. goto err;
  1206. }
  1207. return (1);
  1208. err:
  1209. return (0);
  1210. }
  1211. static int req_check_len(int len, int n_min, int n_max)
  1212. {
  1213. if ((n_min > 0) && (len < n_min)) {
  1214. BIO_printf(bio_err,
  1215. "string is too short, it needs to be at least %d bytes long\n",
  1216. n_min);
  1217. return (0);
  1218. }
  1219. if ((n_max >= 0) && (len > n_max)) {
  1220. BIO_printf(bio_err,
  1221. "string is too long, it needs to be less than %d bytes long\n",
  1222. n_max);
  1223. return (0);
  1224. }
  1225. return (1);
  1226. }
  1227. /* Check if the end of a string matches 'end' */
  1228. static int check_end(const char *str, const char *end)
  1229. {
  1230. int elen, slen;
  1231. const char *tmp;
  1232. elen = strlen(end);
  1233. slen = strlen(str);
  1234. if (elen > slen)
  1235. return 1;
  1236. tmp = str + slen - elen;
  1237. return strcmp(tmp, end);
  1238. }
  1239. static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
  1240. int *pkey_type, long *pkeylen,
  1241. char **palgnam, ENGINE *keygen_engine)
  1242. {
  1243. EVP_PKEY_CTX *gctx = NULL;
  1244. EVP_PKEY *param = NULL;
  1245. long keylen = -1;
  1246. BIO *pbio = NULL;
  1247. const char *paramfile = NULL;
  1248. if (gstr == NULL) {
  1249. *pkey_type = EVP_PKEY_RSA;
  1250. keylen = *pkeylen;
  1251. } else if (gstr[0] >= '0' && gstr[0] <= '9') {
  1252. *pkey_type = EVP_PKEY_RSA;
  1253. keylen = atol(gstr);
  1254. *pkeylen = keylen;
  1255. } else if (strncmp(gstr, "param:", 6) == 0)
  1256. paramfile = gstr + 6;
  1257. else {
  1258. const char *p = strchr(gstr, ':');
  1259. int len;
  1260. ENGINE *tmpeng;
  1261. const EVP_PKEY_ASN1_METHOD *ameth;
  1262. if (p)
  1263. len = p - gstr;
  1264. else
  1265. len = strlen(gstr);
  1266. /*
  1267. * The lookup of a the string will cover all engines so keep a note
  1268. * of the implementation.
  1269. */
  1270. ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
  1271. if (!ameth) {
  1272. BIO_printf(bio_err, "Unknown algorithm %.*s\n", len, gstr);
  1273. return NULL;
  1274. }
  1275. EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
  1276. #ifndef OPENSSL_NO_ENGINE
  1277. if (tmpeng)
  1278. ENGINE_finish(tmpeng);
  1279. #endif
  1280. if (*pkey_type == EVP_PKEY_RSA) {
  1281. if (p) {
  1282. keylen = atol(p + 1);
  1283. *pkeylen = keylen;
  1284. } else
  1285. keylen = *pkeylen;
  1286. } else if (p)
  1287. paramfile = p + 1;
  1288. }
  1289. if (paramfile) {
  1290. pbio = BIO_new_file(paramfile, "r");
  1291. if (!pbio) {
  1292. BIO_printf(bio_err, "Can't open parameter file %s\n", paramfile);
  1293. return NULL;
  1294. }
  1295. param = PEM_read_bio_Parameters(pbio, NULL);
  1296. if (!param) {
  1297. X509 *x;
  1298. (void)BIO_reset(pbio);
  1299. x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
  1300. if (x) {
  1301. param = X509_get_pubkey(x);
  1302. X509_free(x);
  1303. }
  1304. }
  1305. BIO_free(pbio);
  1306. if (!param) {
  1307. BIO_printf(bio_err, "Error reading parameter file %s\n", paramfile);
  1308. return NULL;
  1309. }
  1310. if (*pkey_type == -1)
  1311. *pkey_type = EVP_PKEY_id(param);
  1312. else if (*pkey_type != EVP_PKEY_base_id(param)) {
  1313. BIO_printf(bio_err, "Key Type does not match parameters\n");
  1314. EVP_PKEY_free(param);
  1315. return NULL;
  1316. }
  1317. }
  1318. if (palgnam) {
  1319. const EVP_PKEY_ASN1_METHOD *ameth;
  1320. ENGINE *tmpeng;
  1321. const char *anam;
  1322. ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
  1323. if (!ameth) {
  1324. BIO_puts(bio_err, "Internal error: can't find key algorithm\n");
  1325. return NULL;
  1326. }
  1327. EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
  1328. *palgnam = BUF_strdup(anam);
  1329. #ifndef OPENSSL_NO_ENGINE
  1330. if (tmpeng)
  1331. ENGINE_finish(tmpeng);
  1332. #endif
  1333. }
  1334. if (param) {
  1335. gctx = EVP_PKEY_CTX_new(param, keygen_engine);
  1336. *pkeylen = EVP_PKEY_bits(param);
  1337. EVP_PKEY_free(param);
  1338. } else
  1339. gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
  1340. if (!gctx) {
  1341. BIO_puts(bio_err, "Error allocating keygen context\n");
  1342. ERR_print_errors(bio_err);
  1343. return NULL;
  1344. }
  1345. if (EVP_PKEY_keygen_init(gctx) <= 0) {
  1346. BIO_puts(bio_err, "Error initializing keygen context\n");
  1347. ERR_print_errors(bio_err);
  1348. return NULL;
  1349. }
  1350. #ifndef OPENSSL_NO_RSA
  1351. if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
  1352. if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
  1353. BIO_puts(bio_err, "Error setting RSA keysize\n");
  1354. ERR_print_errors(bio_err);
  1355. EVP_PKEY_CTX_free(gctx);
  1356. return NULL;
  1357. }
  1358. }
  1359. #endif
  1360. return gctx;
  1361. }
  1362. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  1363. {
  1364. char c = '*';
  1365. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  1366. int p;
  1367. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  1368. if (p == 0)
  1369. c = '.';
  1370. if (p == 1)
  1371. c = '+';
  1372. if (p == 2)
  1373. c = '*';
  1374. if (p == 3)
  1375. c = '\n';
  1376. BIO_write(b, &c, 1);
  1377. (void)BIO_flush(b);
  1378. return 1;
  1379. }
  1380. static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
  1381. const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts)
  1382. {
  1383. EVP_PKEY_CTX *pkctx = NULL;
  1384. int i;
  1385. EVP_MD_CTX_init(ctx);
  1386. if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
  1387. return 0;
  1388. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  1389. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  1390. if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
  1391. BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
  1392. ERR_print_errors(bio_err);
  1393. return 0;
  1394. }
  1395. }
  1396. return 1;
  1397. }
  1398. int do_X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md,
  1399. STACK_OF(OPENSSL_STRING) *sigopts)
  1400. {
  1401. int rv;
  1402. EVP_MD_CTX mctx;
  1403. EVP_MD_CTX_init(&mctx);
  1404. rv = do_sign_init(&mctx, pkey, md, sigopts);
  1405. if (rv > 0)
  1406. rv = X509_sign_ctx(x, &mctx);
  1407. EVP_MD_CTX_cleanup(&mctx);
  1408. return rv > 0 ? 1 : 0;
  1409. }
  1410. int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md,
  1411. STACK_OF(OPENSSL_STRING) *sigopts)
  1412. {
  1413. int rv;
  1414. EVP_MD_CTX mctx;
  1415. EVP_MD_CTX_init(&mctx);
  1416. rv = do_sign_init(&mctx, pkey, md, sigopts);
  1417. if (rv > 0)
  1418. rv = X509_REQ_sign_ctx(x, &mctx);
  1419. EVP_MD_CTX_cleanup(&mctx);
  1420. return rv > 0 ? 1 : 0;
  1421. }
  1422. int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md,
  1423. STACK_OF(OPENSSL_STRING) *sigopts)
  1424. {
  1425. int rv;
  1426. EVP_MD_CTX mctx;
  1427. EVP_MD_CTX_init(&mctx);
  1428. rv = do_sign_init(&mctx, pkey, md, sigopts);
  1429. if (rv > 0)
  1430. rv = X509_CRL_sign_ctx(x, &mctx);
  1431. EVP_MD_CTX_cleanup(&mctx);
  1432. return rv > 0 ? 1 : 0;
  1433. }