req.c 51 KB

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