req.c 54 KB

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