req.c 51 KB

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