req.c 46 KB

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