req.c 51 KB

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