req.c 54 KB

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