2
0

req.c 49 KB

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