req.c 48 KB

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