req.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /* apps/req.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /*
  59. * Until the key-gen callbacks are modified to use newer prototypes, we allow
  60. * deprecated functions for openssl-internal code
  61. */
  62. #ifdef OPENSSL_NO_DEPRECATED
  63. # undef OPENSSL_NO_DEPRECATED
  64. #endif
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <time.h>
  68. #include <string.h>
  69. #ifdef OPENSSL_NO_STDIO
  70. # define APPS_WIN16
  71. #endif
  72. #include "apps.h"
  73. #include <openssl/bio.h>
  74. #include <openssl/evp.h>
  75. #include <openssl/conf.h>
  76. #include <openssl/err.h>
  77. #include <openssl/asn1.h>
  78. #include <openssl/x509.h>
  79. #include <openssl/x509v3.h>
  80. #include <openssl/objects.h>
  81. #include <openssl/pem.h>
  82. #include <openssl/bn.h>
  83. #ifndef OPENSSL_NO_RSA
  84. # include <openssl/rsa.h>
  85. #endif
  86. #ifndef OPENSSL_NO_DSA
  87. # include <openssl/dsa.h>
  88. #endif
  89. #define SECTION "req"
  90. #define BITS "default_bits"
  91. #define KEYFILE "default_keyfile"
  92. #define PROMPT "prompt"
  93. #define DISTINGUISHED_NAME "distinguished_name"
  94. #define ATTRIBUTES "attributes"
  95. #define V3_EXTENSIONS "x509_extensions"
  96. #define REQ_EXTENSIONS "req_extensions"
  97. #define STRING_MASK "string_mask"
  98. #define UTF8_IN "utf8"
  99. #define DEFAULT_KEY_LENGTH 2048
  100. #define MIN_KEY_LENGTH 512
  101. #undef PROG
  102. #define PROG req_main
  103. /*-
  104. * -inform arg - input format - default PEM (DER or PEM)
  105. * -outform arg - output format - default PEM
  106. * -in arg - input file - default stdin
  107. * -out arg - output file - default stdout
  108. * -verify - check request signature
  109. * -noout - don't print stuff out.
  110. * -text - print out human readable text.
  111. * -nodes - no des encryption
  112. * -config file - Load configuration file.
  113. * -key file - make a request using key in file (or use it for verification).
  114. * -keyform arg - key file format.
  115. * -rand file(s) - load the file(s) into the PRNG.
  116. * -newkey - make a key and a request.
  117. * -modulus - print RSA modulus.
  118. * -pubkey - output Public Key.
  119. * -x509 - output a self signed X509 structure instead.
  120. * -asn1-kludge - output new certificate request in a format that some CA's
  121. * require. This format is wrong
  122. */
  123. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
  124. int attribs, unsigned long chtype);
  125. static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
  126. int multirdn);
  127. static int prompt_info(X509_REQ *req,
  128. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  129. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  130. int attribs, unsigned long chtype);
  131. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
  132. STACK_OF(CONF_VALUE) *attr, int attribs,
  133. unsigned long chtype);
  134. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  135. char *value, int nid, int n_min, int n_max,
  136. unsigned long chtype);
  137. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  138. char *value, int nid, int n_min, int n_max,
  139. unsigned long chtype, int mval);
  140. static int genpkey_cb(EVP_PKEY_CTX *ctx);
  141. static int req_check_len(int len, int n_min, int n_max);
  142. static int check_end(const char *str, const char *end);
  143. static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
  144. int *pkey_type, long *pkeylen,
  145. char **palgnam, ENGINE *keygen_engine);
  146. #ifndef MONOLITH
  147. static char *default_config_file = NULL;
  148. #endif
  149. static CONF *req_conf = NULL;
  150. static int batch = 0;
  151. int MAIN(int, char **);
  152. int MAIN(int argc, char **argv)
  153. {
  154. ENGINE *e = NULL, *gen_eng = NULL;
  155. unsigned long nmflag = 0, reqflag = 0;
  156. int ex = 1, x509 = 0, days = 30;
  157. X509 *x509ss = NULL;
  158. X509_REQ *req = NULL;
  159. EVP_PKEY_CTX *genctx = NULL;
  160. const char *keyalg = NULL;
  161. char *keyalgstr = NULL;
  162. STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL;
  163. EVP_PKEY *pkey = NULL;
  164. int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = -1;
  165. long newkey = -1;
  166. BIO *in = NULL, *out = NULL;
  167. int informat, outformat, verify = 0, noout = 0, text = 0, keyform =
  168. FORMAT_PEM;
  169. int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
  170. char *infile, *outfile, *prog, *keyfile = NULL, *template =
  171. NULL, *keyout = NULL;
  172. char *engine = NULL;
  173. char *extensions = NULL;
  174. char *req_exts = NULL;
  175. const EVP_CIPHER *cipher = NULL;
  176. ASN1_INTEGER *serial = NULL;
  177. int modulus = 0;
  178. char *inrand = NULL;
  179. char *passargin = NULL, *passargout = NULL;
  180. char *passin = NULL, *passout = NULL;
  181. char *p;
  182. char *subj = NULL;
  183. int multirdn = 0;
  184. const EVP_MD *md_alg = NULL, *digest = NULL;
  185. unsigned long chtype = MBSTRING_ASC;
  186. #ifndef MONOLITH
  187. char *to_free;
  188. long errline;
  189. #endif
  190. req_conf = NULL;
  191. #ifndef OPENSSL_NO_DES
  192. cipher = EVP_des_ede3_cbc();
  193. #endif
  194. apps_startup();
  195. if (bio_err == NULL)
  196. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  197. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  198. infile = NULL;
  199. outfile = NULL;
  200. informat = FORMAT_PEM;
  201. outformat = FORMAT_PEM;
  202. prog = argv[0];
  203. argc--;
  204. argv++;
  205. while (argc >= 1) {
  206. if (strcmp(*argv, "-inform") == 0) {
  207. if (--argc < 1)
  208. goto bad;
  209. informat = str2fmt(*(++argv));
  210. } else if (strcmp(*argv, "-outform") == 0) {
  211. if (--argc < 1)
  212. goto bad;
  213. outformat = str2fmt(*(++argv));
  214. }
  215. #ifndef OPENSSL_NO_ENGINE
  216. else if (strcmp(*argv, "-engine") == 0) {
  217. if (--argc < 1)
  218. goto bad;
  219. engine = *(++argv);
  220. } else if (strcmp(*argv, "-keygen_engine") == 0) {
  221. if (--argc < 1)
  222. goto bad;
  223. gen_eng = ENGINE_by_id(*(++argv));
  224. if (gen_eng == NULL) {
  225. BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
  226. goto end;
  227. }
  228. }
  229. #endif
  230. else if (strcmp(*argv, "-key") == 0) {
  231. if (--argc < 1)
  232. goto bad;
  233. keyfile = *(++argv);
  234. } else if (strcmp(*argv, "-pubkey") == 0) {
  235. pubkey = 1;
  236. } else if (strcmp(*argv, "-new") == 0) {
  237. newreq = 1;
  238. } else if (strcmp(*argv, "-config") == 0) {
  239. if (--argc < 1)
  240. goto bad;
  241. template = *(++argv);
  242. } else if (strcmp(*argv, "-keyform") == 0) {
  243. if (--argc < 1)
  244. goto bad;
  245. keyform = str2fmt(*(++argv));
  246. } else if (strcmp(*argv, "-in") == 0) {
  247. if (--argc < 1)
  248. goto bad;
  249. infile = *(++argv);
  250. } else if (strcmp(*argv, "-out") == 0) {
  251. if (--argc < 1)
  252. goto bad;
  253. outfile = *(++argv);
  254. } else if (strcmp(*argv, "-keyout") == 0) {
  255. if (--argc < 1)
  256. goto bad;
  257. keyout = *(++argv);
  258. } else if (strcmp(*argv, "-passin") == 0) {
  259. if (--argc < 1)
  260. goto bad;
  261. passargin = *(++argv);
  262. } else if (strcmp(*argv, "-passout") == 0) {
  263. if (--argc < 1)
  264. goto bad;
  265. passargout = *(++argv);
  266. } else if (strcmp(*argv, "-rand") == 0) {
  267. if (--argc < 1)
  268. goto bad;
  269. inrand = *(++argv);
  270. } else if (strcmp(*argv, "-newkey") == 0) {
  271. if (--argc < 1)
  272. goto bad;
  273. keyalg = *(++argv);
  274. newreq = 1;
  275. } else if (strcmp(*argv, "-pkeyopt") == 0) {
  276. if (--argc < 1)
  277. goto bad;
  278. if (!pkeyopts)
  279. pkeyopts = sk_OPENSSL_STRING_new_null();
  280. if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv)))
  281. goto bad;
  282. } else if (strcmp(*argv, "-sigopt") == 0) {
  283. if (--argc < 1)
  284. goto bad;
  285. if (!sigopts)
  286. sigopts = sk_OPENSSL_STRING_new_null();
  287. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
  288. goto bad;
  289. } else if (strcmp(*argv, "-batch") == 0)
  290. batch = 1;
  291. else if (strcmp(*argv, "-newhdr") == 0)
  292. newhdr = 1;
  293. else if (strcmp(*argv, "-modulus") == 0)
  294. modulus = 1;
  295. else if (strcmp(*argv, "-verify") == 0)
  296. verify = 1;
  297. else if (strcmp(*argv, "-nodes") == 0)
  298. nodes = 1;
  299. else if (strcmp(*argv, "-noout") == 0)
  300. noout = 1;
  301. else if (strcmp(*argv, "-verbose") == 0)
  302. verbose = 1;
  303. else if (strcmp(*argv, "-utf8") == 0)
  304. chtype = MBSTRING_UTF8;
  305. else if (strcmp(*argv, "-nameopt") == 0) {
  306. if (--argc < 1)
  307. goto bad;
  308. if (!set_name_ex(&nmflag, *(++argv)))
  309. goto bad;
  310. } else if (strcmp(*argv, "-reqopt") == 0) {
  311. if (--argc < 1)
  312. goto bad;
  313. if (!set_cert_ex(&reqflag, *(++argv)))
  314. goto bad;
  315. } else if (strcmp(*argv, "-subject") == 0)
  316. subject = 1;
  317. else if (strcmp(*argv, "-text") == 0)
  318. text = 1;
  319. else if (strcmp(*argv, "-x509") == 0) {
  320. x509 = 1;
  321. } else if (strcmp(*argv, "-asn1-kludge") == 0)
  322. kludge = 1;
  323. else if (strcmp(*argv, "-no-asn1-kludge") == 0)
  324. kludge = 0;
  325. else if (strcmp(*argv, "-subj") == 0) {
  326. if (--argc < 1)
  327. goto bad;
  328. subj = *(++argv);
  329. } else if (strcmp(*argv, "-multivalue-rdn") == 0)
  330. multirdn = 1;
  331. else if (strcmp(*argv, "-days") == 0) {
  332. if (--argc < 1)
  333. goto bad;
  334. days = atoi(*(++argv));
  335. if (days == 0)
  336. days = 30;
  337. } else if (strcmp(*argv, "-set_serial") == 0) {
  338. if (--argc < 1)
  339. goto bad;
  340. serial = s2i_ASN1_INTEGER(NULL, *(++argv));
  341. if (!serial)
  342. goto bad;
  343. } else if (strcmp(*argv, "-extensions") == 0) {
  344. if (--argc < 1)
  345. goto bad;
  346. extensions = *(++argv);
  347. } else if (strcmp(*argv, "-reqexts") == 0) {
  348. if (--argc < 1)
  349. goto bad;
  350. req_exts = *(++argv);
  351. } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) {
  352. /* ok */
  353. digest = md_alg;
  354. } else {
  355. BIO_printf(bio_err, "unknown option %s\n", *argv);
  356. badops = 1;
  357. break;
  358. }
  359. argc--;
  360. argv++;
  361. }
  362. if (badops) {
  363. bad:
  364. BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
  365. BIO_printf(bio_err, "where options are\n");
  366. BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
  367. BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
  368. BIO_printf(bio_err, " -in arg input file\n");
  369. BIO_printf(bio_err, " -out arg output file\n");
  370. BIO_printf(bio_err, " -text text form of request\n");
  371. BIO_printf(bio_err, " -pubkey output public key\n");
  372. BIO_printf(bio_err, " -noout do not output REQ\n");
  373. BIO_printf(bio_err, " -verify verify signature on REQ\n");
  374. BIO_printf(bio_err, " -modulus RSA modulus\n");
  375. BIO_printf(bio_err, " -nodes don't encrypt the output key\n");
  376. #ifndef OPENSSL_NO_ENGINE
  377. BIO_printf(bio_err,
  378. " -engine e use engine e, possibly a hardware device\n");
  379. #endif
  380. BIO_printf(bio_err, " -subject output the request's subject\n");
  381. BIO_printf(bio_err, " -passin private key password source\n");
  382. BIO_printf(bio_err,
  383. " -key file use the private key contained in file\n");
  384. BIO_printf(bio_err, " -keyform arg key file format\n");
  385. BIO_printf(bio_err, " -keyout arg file to send the key to\n");
  386. BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  387. LIST_SEPARATOR_CHAR);
  388. BIO_printf(bio_err,
  389. " load the file (or the files in the directory) into\n");
  390. BIO_printf(bio_err, " the random number generator\n");
  391. BIO_printf(bio_err,
  392. " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
  393. BIO_printf(bio_err,
  394. " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
  395. #ifndef OPENSSL_NO_ECDSA
  396. BIO_printf(bio_err,
  397. " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
  398. #endif
  399. BIO_printf(bio_err,
  400. " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
  401. BIO_printf(bio_err, " -config file request template file.\n");
  402. BIO_printf(bio_err,
  403. " -subj arg set or modify request subject\n");
  404. BIO_printf(bio_err,
  405. " -multivalue-rdn enable support for multivalued RDNs\n");
  406. BIO_printf(bio_err, " -new new request.\n");
  407. BIO_printf(bio_err,
  408. " -batch do not ask anything during request generation\n");
  409. BIO_printf(bio_err,
  410. " -x509 output a x509 structure instead of a cert. req.\n");
  411. BIO_printf(bio_err,
  412. " -days number of days a certificate generated by -x509 is valid for.\n");
  413. BIO_printf(bio_err,
  414. " -set_serial serial number to use for a certificate generated by -x509.\n");
  415. BIO_printf(bio_err,
  416. " -newhdr output \"NEW\" in the header lines\n");
  417. BIO_printf(bio_err,
  418. " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
  419. BIO_printf(bio_err,
  420. " have been reported as requiring\n");
  421. BIO_printf(bio_err,
  422. " -extensions .. specify certificate extension section (override value in config file)\n");
  423. BIO_printf(bio_err,
  424. " -reqexts .. specify request extension section (override value in config file)\n");
  425. BIO_printf(bio_err,
  426. " -utf8 input characters are UTF8 (default ASCII)\n");
  427. BIO_printf(bio_err,
  428. " -nameopt arg - various certificate name options\n");
  429. BIO_printf(bio_err,
  430. " -reqopt arg - various request text options\n\n");
  431. goto end;
  432. }
  433. if (x509 && infile == NULL)
  434. newreq = 1;
  435. ERR_load_crypto_strings();
  436. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  437. BIO_printf(bio_err, "Error getting passwords\n");
  438. goto end;
  439. }
  440. #ifndef MONOLITH /* else this has happened in openssl.c
  441. * (global `config') */
  442. /* Lets load up our environment a little */
  443. p = getenv("OPENSSL_CONF");
  444. if (p == NULL)
  445. p = getenv("SSLEAY_CONF");
  446. if (p == NULL)
  447. p = to_free = make_config_name();
  448. default_config_file = p;
  449. config = NCONF_new(NULL);
  450. i = NCONF_load(config, p, &errline);
  451. #endif
  452. if (template != NULL) {
  453. long errline = -1;
  454. if (verbose)
  455. BIO_printf(bio_err, "Using configuration from %s\n", template);
  456. req_conf = NCONF_new(NULL);
  457. i = NCONF_load(req_conf, template, &errline);
  458. if (i == 0) {
  459. BIO_printf(bio_err, "error on line %ld of %s\n", errline,
  460. template);
  461. goto end;
  462. }
  463. } else {
  464. req_conf = config;
  465. if (req_conf == NULL) {
  466. BIO_printf(bio_err, "Unable to load config info from %s\n",
  467. default_config_file);
  468. if (newreq)
  469. goto end;
  470. } else if (verbose)
  471. BIO_printf(bio_err, "Using configuration from %s\n",
  472. default_config_file);
  473. }
  474. if (req_conf != NULL) {
  475. if (!load_config(bio_err, req_conf))
  476. goto end;
  477. p = NCONF_get_string(req_conf, NULL, "oid_file");
  478. if (p == NULL)
  479. ERR_clear_error();
  480. if (p != NULL) {
  481. BIO *oid_bio;
  482. oid_bio = BIO_new_file(p, "r");
  483. if (oid_bio == NULL) {
  484. /*-
  485. BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
  486. ERR_print_errors(bio_err);
  487. */
  488. } else {
  489. OBJ_create_objects(oid_bio);
  490. BIO_free(oid_bio);
  491. }
  492. }
  493. }
  494. if (!add_oid_section(bio_err, req_conf))
  495. goto end;
  496. if (md_alg == NULL) {
  497. p = NCONF_get_string(req_conf, SECTION, "default_md");
  498. if (p == NULL)
  499. ERR_clear_error();
  500. if (p != NULL) {
  501. if ((md_alg = EVP_get_digestbyname(p)) != NULL)
  502. digest = md_alg;
  503. }
  504. }
  505. if (!extensions) {
  506. extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
  507. if (!extensions)
  508. ERR_clear_error();
  509. }
  510. if (extensions) {
  511. /* Check syntax of file */
  512. X509V3_CTX ctx;
  513. X509V3_set_ctx_test(&ctx);
  514. X509V3_set_nconf(&ctx, req_conf);
  515. if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
  516. BIO_printf(bio_err,
  517. "Error Loading extension section %s\n", extensions);
  518. goto end;
  519. }
  520. }
  521. if (!passin) {
  522. passin = NCONF_get_string(req_conf, SECTION, "input_password");
  523. if (!passin)
  524. ERR_clear_error();
  525. }
  526. if (!passout) {
  527. passout = NCONF_get_string(req_conf, SECTION, "output_password");
  528. if (!passout)
  529. ERR_clear_error();
  530. }
  531. p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
  532. if (!p)
  533. ERR_clear_error();
  534. if (p && !ASN1_STRING_set_default_mask_asc(p)) {
  535. BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
  536. goto end;
  537. }
  538. if (chtype != MBSTRING_UTF8) {
  539. p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
  540. if (!p)
  541. ERR_clear_error();
  542. else if (!strcmp(p, "yes"))
  543. chtype = MBSTRING_UTF8;
  544. }
  545. if (!req_exts) {
  546. req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
  547. if (!req_exts)
  548. ERR_clear_error();
  549. }
  550. if (req_exts) {
  551. /* Check syntax of file */
  552. X509V3_CTX ctx;
  553. X509V3_set_ctx_test(&ctx);
  554. X509V3_set_nconf(&ctx, req_conf);
  555. if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
  556. BIO_printf(bio_err,
  557. "Error Loading request extension section %s\n",
  558. req_exts);
  559. goto end;
  560. }
  561. }
  562. in = BIO_new(BIO_s_file());
  563. out = BIO_new(BIO_s_file());
  564. if ((in == NULL) || (out == NULL))
  565. goto end;
  566. e = setup_engine(bio_err, engine, 0);
  567. if (keyfile != NULL) {
  568. pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
  569. "Private Key");
  570. if (!pkey) {
  571. /*
  572. * load_key() has already printed an appropriate message
  573. */
  574. goto end;
  575. } else {
  576. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  577. if (randfile == NULL)
  578. ERR_clear_error();
  579. app_RAND_load_file(randfile, bio_err, 0);
  580. }
  581. }
  582. if (newreq && (pkey == NULL)) {
  583. char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
  584. if (randfile == NULL)
  585. ERR_clear_error();
  586. app_RAND_load_file(randfile, bio_err, 0);
  587. if (inrand)
  588. app_RAND_load_files(inrand);
  589. if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
  590. newkey = DEFAULT_KEY_LENGTH;
  591. }
  592. if (keyalg) {
  593. genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey,
  594. &keyalgstr, gen_eng);
  595. if (!genctx)
  596. goto end;
  597. }
  598. if (newkey < MIN_KEY_LENGTH
  599. && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
  600. BIO_printf(bio_err, "private key length is too short,\n");
  601. BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
  602. MIN_KEY_LENGTH, newkey);
  603. goto end;
  604. }
  605. if (!genctx) {
  606. genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey,
  607. &keyalgstr, gen_eng);
  608. if (!genctx)
  609. goto end;
  610. }
  611. if (pkeyopts) {
  612. char *genopt;
  613. for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
  614. genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
  615. if (pkey_ctrl_string(genctx, genopt) <= 0) {
  616. BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
  617. ERR_print_errors(bio_err);
  618. goto end;
  619. }
  620. }
  621. }
  622. BIO_printf(bio_err, "Generating a %s private key\n", keyalgstr);
  623. EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
  624. EVP_PKEY_CTX_set_app_data(genctx, bio_err);
  625. if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
  626. BIO_puts(bio_err, "Error Generating Key\n");
  627. goto end;
  628. }
  629. EVP_PKEY_CTX_free(genctx);
  630. genctx = NULL;
  631. app_RAND_write_file(randfile, bio_err);
  632. if (keyout == NULL) {
  633. keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
  634. if (keyout == NULL)
  635. ERR_clear_error();
  636. }
  637. if (keyout == NULL) {
  638. BIO_printf(bio_err, "writing new private key to stdout\n");
  639. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  640. #ifdef OPENSSL_SYS_VMS
  641. {
  642. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  643. out = BIO_push(tmpbio, out);
  644. }
  645. #endif
  646. } else {
  647. BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
  648. if (BIO_write_filename(out, keyout) <= 0) {
  649. perror(keyout);
  650. goto end;
  651. }
  652. }
  653. p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
  654. if (p == NULL) {
  655. ERR_clear_error();
  656. p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
  657. if (p == NULL)
  658. ERR_clear_error();
  659. }
  660. if ((p != NULL) && (strcmp(p, "no") == 0))
  661. cipher = NULL;
  662. if (nodes)
  663. cipher = NULL;
  664. i = 0;
  665. loop:
  666. if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
  667. NULL, 0, NULL, passout)) {
  668. if ((ERR_GET_REASON(ERR_peek_error()) ==
  669. PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
  670. ERR_clear_error();
  671. i++;
  672. goto loop;
  673. }
  674. goto end;
  675. }
  676. BIO_printf(bio_err, "-----\n");
  677. }
  678. if (!newreq) {
  679. /*
  680. * Since we are using a pre-existing certificate request, the kludge
  681. * 'format' info should not be changed.
  682. */
  683. kludge = -1;
  684. if (infile == NULL)
  685. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  686. else {
  687. if (BIO_read_filename(in, infile) <= 0) {
  688. perror(infile);
  689. goto end;
  690. }
  691. }
  692. if (informat == FORMAT_ASN1)
  693. req = d2i_X509_REQ_bio(in, NULL);
  694. else if (informat == FORMAT_PEM)
  695. req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
  696. else {
  697. BIO_printf(bio_err,
  698. "bad input format specified for X509 request\n");
  699. goto end;
  700. }
  701. if (req == NULL) {
  702. BIO_printf(bio_err, "unable to load X509 request\n");
  703. goto end;
  704. }
  705. }
  706. if (newreq || x509) {
  707. if (pkey == NULL) {
  708. BIO_printf(bio_err, "you need to specify a private key\n");
  709. goto end;
  710. }
  711. if (req == NULL) {
  712. req = X509_REQ_new();
  713. if (req == NULL) {
  714. goto end;
  715. }
  716. i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
  717. subj = NULL; /* done processing '-subj' option */
  718. if ((kludge > 0)
  719. && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) {
  720. sk_X509_ATTRIBUTE_free(req->req_info->attributes);
  721. req->req_info->attributes = NULL;
  722. }
  723. if (!i) {
  724. BIO_printf(bio_err, "problems making Certificate Request\n");
  725. goto end;
  726. }
  727. }
  728. if (x509) {
  729. EVP_PKEY *tmppkey;
  730. X509V3_CTX ext_ctx;
  731. if ((x509ss = X509_new()) == NULL)
  732. goto end;
  733. /* Set version to V3 */
  734. if (extensions && !X509_set_version(x509ss, 2))
  735. goto end;
  736. if (serial) {
  737. if (!X509_set_serialNumber(x509ss, serial))
  738. goto end;
  739. } else {
  740. if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
  741. goto end;
  742. }
  743. if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
  744. goto end;
  745. if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
  746. goto end;
  747. if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
  748. goto end;
  749. if (!X509_set_subject_name
  750. (x509ss, X509_REQ_get_subject_name(req)))
  751. goto end;
  752. tmppkey = X509_REQ_get_pubkey(req);
  753. if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
  754. goto end;
  755. EVP_PKEY_free(tmppkey);
  756. /* Set up V3 context struct */
  757. X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
  758. X509V3_set_nconf(&ext_ctx, req_conf);
  759. /* Add extensions */
  760. if (extensions && !X509V3_EXT_add_nconf(req_conf,
  761. &ext_ctx, extensions,
  762. x509ss)) {
  763. BIO_printf(bio_err, "Error Loading extension section %s\n",
  764. extensions);
  765. goto end;
  766. }
  767. i = do_X509_sign(bio_err, x509ss, pkey, digest, sigopts);
  768. if (!i) {
  769. ERR_print_errors(bio_err);
  770. goto end;
  771. }
  772. } else {
  773. X509V3_CTX ext_ctx;
  774. /* Set up V3 context struct */
  775. X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
  776. X509V3_set_nconf(&ext_ctx, req_conf);
  777. /* Add extensions */
  778. if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
  779. &ext_ctx, req_exts,
  780. req)) {
  781. BIO_printf(bio_err, "Error Loading extension section %s\n",
  782. req_exts);
  783. goto end;
  784. }
  785. i = do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts);
  786. if (!i) {
  787. ERR_print_errors(bio_err);
  788. goto end;
  789. }
  790. }
  791. }
  792. if (subj && x509) {
  793. BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
  794. goto end;
  795. }
  796. if (subj && !x509) {
  797. if (verbose) {
  798. BIO_printf(bio_err, "Modifying Request's Subject\n");
  799. print_name(bio_err, "old subject=",
  800. X509_REQ_get_subject_name(req), nmflag);
  801. }
  802. if (build_subject(req, subj, chtype, multirdn) == 0) {
  803. BIO_printf(bio_err, "ERROR: cannot modify subject\n");
  804. ex = 1;
  805. goto end;
  806. }
  807. req->req_info->enc.modified = 1;
  808. if (verbose) {
  809. print_name(bio_err, "new subject=",
  810. X509_REQ_get_subject_name(req), nmflag);
  811. }
  812. }
  813. if (verify && !x509) {
  814. int tmp = 0;
  815. if (pkey == NULL) {
  816. pkey = X509_REQ_get_pubkey(req);
  817. tmp = 1;
  818. if (pkey == NULL)
  819. goto end;
  820. }
  821. i = X509_REQ_verify(req, pkey);
  822. if (tmp) {
  823. EVP_PKEY_free(pkey);
  824. pkey = NULL;
  825. }
  826. if (i < 0) {
  827. goto end;
  828. } else if (i == 0) {
  829. BIO_printf(bio_err, "verify failure\n");
  830. ERR_print_errors(bio_err);
  831. } else /* if (i > 0) */
  832. BIO_printf(bio_err, "verify OK\n");
  833. }
  834. if (noout && !text && !modulus && !subject && !pubkey) {
  835. ex = 0;
  836. goto end;
  837. }
  838. if (outfile == NULL) {
  839. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  840. #ifdef OPENSSL_SYS_VMS
  841. {
  842. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  843. out = BIO_push(tmpbio, out);
  844. }
  845. #endif
  846. } else {
  847. if ((keyout != NULL) && (strcmp(outfile, keyout) == 0))
  848. i = (int)BIO_append_filename(out, outfile);
  849. else
  850. i = (int)BIO_write_filename(out, outfile);
  851. if (!i) {
  852. perror(outfile);
  853. goto end;
  854. }
  855. }
  856. if (pubkey) {
  857. EVP_PKEY *tpubkey;
  858. tpubkey = X509_REQ_get_pubkey(req);
  859. if (tpubkey == NULL) {
  860. BIO_printf(bio_err, "Error getting public key\n");
  861. ERR_print_errors(bio_err);
  862. goto end;
  863. }
  864. PEM_write_bio_PUBKEY(out, tpubkey);
  865. EVP_PKEY_free(tpubkey);
  866. }
  867. if (text) {
  868. if (x509)
  869. X509_print_ex(out, x509ss, nmflag, reqflag);
  870. else
  871. X509_REQ_print_ex(out, req, nmflag, reqflag);
  872. }
  873. if (subject) {
  874. if (x509)
  875. print_name(out, "subject=", X509_get_subject_name(x509ss),
  876. nmflag);
  877. else
  878. print_name(out, "subject=", X509_REQ_get_subject_name(req),
  879. nmflag);
  880. }
  881. if (modulus) {
  882. EVP_PKEY *tpubkey;
  883. if (x509)
  884. tpubkey = X509_get_pubkey(x509ss);
  885. else
  886. tpubkey = X509_REQ_get_pubkey(req);
  887. if (tpubkey == NULL) {
  888. fprintf(stdout, "Modulus=unavailable\n");
  889. goto end;
  890. }
  891. fprintf(stdout, "Modulus=");
  892. #ifndef OPENSSL_NO_RSA
  893. if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA)
  894. BN_print(out, tpubkey->pkey.rsa->n);
  895. else
  896. #endif
  897. fprintf(stdout, "Wrong Algorithm type");
  898. EVP_PKEY_free(tpubkey);
  899. fprintf(stdout, "\n");
  900. }
  901. if (!noout && !x509) {
  902. if (outformat == FORMAT_ASN1)
  903. i = i2d_X509_REQ_bio(out, req);
  904. else if (outformat == FORMAT_PEM) {
  905. if (newhdr)
  906. i = PEM_write_bio_X509_REQ_NEW(out, req);
  907. else
  908. i = PEM_write_bio_X509_REQ(out, req);
  909. } else {
  910. BIO_printf(bio_err, "bad output format specified for outfile\n");
  911. goto end;
  912. }
  913. if (!i) {
  914. BIO_printf(bio_err, "unable to write X509 request\n");
  915. goto end;
  916. }
  917. }
  918. if (!noout && x509 && (x509ss != NULL)) {
  919. if (outformat == FORMAT_ASN1)
  920. i = i2d_X509_bio(out, x509ss);
  921. else if (outformat == FORMAT_PEM)
  922. i = PEM_write_bio_X509(out, x509ss);
  923. else {
  924. BIO_printf(bio_err, "bad output format specified for outfile\n");
  925. goto end;
  926. }
  927. if (!i) {
  928. BIO_printf(bio_err, "unable to write X509 certificate\n");
  929. goto end;
  930. }
  931. }
  932. ex = 0;
  933. end:
  934. #ifndef MONOLITH
  935. if (to_free)
  936. OPENSSL_free(to_free);
  937. #endif
  938. if (ex) {
  939. ERR_print_errors(bio_err);
  940. }
  941. if ((req_conf != NULL) && (req_conf != config))
  942. NCONF_free(req_conf);
  943. BIO_free(in);
  944. BIO_free_all(out);
  945. EVP_PKEY_free(pkey);
  946. if (genctx)
  947. EVP_PKEY_CTX_free(genctx);
  948. if (pkeyopts)
  949. sk_OPENSSL_STRING_free(pkeyopts);
  950. if (sigopts)
  951. sk_OPENSSL_STRING_free(sigopts);
  952. #ifndef OPENSSL_NO_ENGINE
  953. if (gen_eng)
  954. ENGINE_free(gen_eng);
  955. #endif
  956. if (keyalgstr)
  957. OPENSSL_free(keyalgstr);
  958. X509_REQ_free(req);
  959. X509_free(x509ss);
  960. ASN1_INTEGER_free(serial);
  961. release_engine(e);
  962. if (passargin && passin)
  963. OPENSSL_free(passin);
  964. if (passargout && passout)
  965. OPENSSL_free(passout);
  966. OBJ_cleanup();
  967. apps_shutdown();
  968. OPENSSL_EXIT(ex);
  969. }
  970. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
  971. int attribs, unsigned long chtype)
  972. {
  973. int ret = 0, i;
  974. char no_prompt = 0;
  975. STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
  976. char *tmp, *dn_sect, *attr_sect;
  977. tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
  978. if (tmp == NULL)
  979. ERR_clear_error();
  980. if ((tmp != NULL) && !strcmp(tmp, "no"))
  981. no_prompt = 1;
  982. dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
  983. if (dn_sect == NULL) {
  984. BIO_printf(bio_err, "unable to find '%s' in config\n",
  985. DISTINGUISHED_NAME);
  986. goto err;
  987. }
  988. dn_sk = NCONF_get_section(req_conf, dn_sect);
  989. if (dn_sk == NULL) {
  990. BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
  991. goto err;
  992. }
  993. attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
  994. if (attr_sect == NULL) {
  995. ERR_clear_error();
  996. attr_sk = NULL;
  997. } else {
  998. attr_sk = NCONF_get_section(req_conf, attr_sect);
  999. if (attr_sk == NULL) {
  1000. BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
  1001. goto err;
  1002. }
  1003. }
  1004. /* setup version number */
  1005. if (!X509_REQ_set_version(req, 0L))
  1006. goto err; /* version 1 */
  1007. if (no_prompt)
  1008. i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
  1009. else {
  1010. if (subj)
  1011. i = build_subject(req, subj, chtype, multirdn);
  1012. else
  1013. i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
  1014. chtype);
  1015. }
  1016. if (!i)
  1017. goto err;
  1018. if (!X509_REQ_set_pubkey(req, pkey))
  1019. goto err;
  1020. ret = 1;
  1021. err:
  1022. return (ret);
  1023. }
  1024. /*
  1025. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1026. * where characters may be escaped by \
  1027. */
  1028. static int build_subject(X509_REQ *req, char *subject, unsigned long chtype,
  1029. int multirdn)
  1030. {
  1031. X509_NAME *n;
  1032. if (!(n = parse_name(subject, chtype, multirdn)))
  1033. return 0;
  1034. if (!X509_REQ_set_subject_name(req, n)) {
  1035. X509_NAME_free(n);
  1036. return 0;
  1037. }
  1038. X509_NAME_free(n);
  1039. return 1;
  1040. }
  1041. static int prompt_info(X509_REQ *req,
  1042. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  1043. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
  1044. int attribs, unsigned long chtype)
  1045. {
  1046. int i;
  1047. char *p, *q;
  1048. char buf[100];
  1049. int nid, mval;
  1050. long n_min, n_max;
  1051. char *type, *value;
  1052. const char *def;
  1053. CONF_VALUE *v;
  1054. X509_NAME *subj;
  1055. subj = X509_REQ_get_subject_name(req);
  1056. if (!batch) {
  1057. BIO_printf(bio_err,
  1058. "You are about to be asked to enter information that will be incorporated\n");
  1059. BIO_printf(bio_err, "into your certificate request.\n");
  1060. BIO_printf(bio_err,
  1061. "What you are about to enter is what is called a Distinguished Name or a DN.\n");
  1062. BIO_printf(bio_err,
  1063. "There are quite a few fields but you can leave some blank\n");
  1064. BIO_printf(bio_err,
  1065. "For some fields there will be a default value,\n");
  1066. BIO_printf(bio_err,
  1067. "If you enter '.', the field will be left blank.\n");
  1068. BIO_printf(bio_err, "-----\n");
  1069. }
  1070. if (sk_CONF_VALUE_num(dn_sk)) {
  1071. i = -1;
  1072. start:for (;;) {
  1073. i++;
  1074. if (sk_CONF_VALUE_num(dn_sk) <= i)
  1075. break;
  1076. v = sk_CONF_VALUE_value(dn_sk, i);
  1077. p = q = NULL;
  1078. type = v->name;
  1079. if (!check_end(type, "_min") || !check_end(type, "_max") ||
  1080. !check_end(type, "_default") || !check_end(type, "_value"))
  1081. continue;
  1082. /*
  1083. * Skip past any leading X. X: X, etc to allow for multiple
  1084. * instances
  1085. */
  1086. for (p = v->name; *p; p++)
  1087. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1088. p++;
  1089. if (*p)
  1090. type = p;
  1091. break;
  1092. }
  1093. if (*type == '+') {
  1094. mval = -1;
  1095. type++;
  1096. } else
  1097. mval = 0;
  1098. /* If OBJ not recognised ignore it */
  1099. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  1100. goto start;
  1101. if (BIO_snprintf(buf, sizeof(buf), "%s_default", v->name)
  1102. >= (int)sizeof(buf)) {
  1103. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  1104. return 0;
  1105. }
  1106. if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  1107. ERR_clear_error();
  1108. def = "";
  1109. }
  1110. BIO_snprintf(buf, sizeof(buf), "%s_value", v->name);
  1111. if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
  1112. ERR_clear_error();
  1113. value = NULL;
  1114. }
  1115. BIO_snprintf(buf, sizeof(buf), "%s_min", v->name);
  1116. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
  1117. ERR_clear_error();
  1118. n_min = -1;
  1119. }
  1120. BIO_snprintf(buf, sizeof(buf), "%s_max", v->name);
  1121. if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
  1122. ERR_clear_error();
  1123. n_max = -1;
  1124. }
  1125. if (!add_DN_object(subj, v->value, def, value, nid,
  1126. n_min, n_max, chtype, mval))
  1127. return 0;
  1128. }
  1129. if (X509_NAME_entry_count(subj) == 0) {
  1130. BIO_printf(bio_err,
  1131. "error, no objects specified in config file\n");
  1132. return 0;
  1133. }
  1134. if (attribs) {
  1135. if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
  1136. && (!batch)) {
  1137. BIO_printf(bio_err,
  1138. "\nPlease enter the following 'extra' attributes\n");
  1139. BIO_printf(bio_err,
  1140. "to be sent with your certificate request\n");
  1141. }
  1142. i = -1;
  1143. start2: for (;;) {
  1144. i++;
  1145. if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
  1146. break;
  1147. v = sk_CONF_VALUE_value(attr_sk, i);
  1148. type = v->name;
  1149. if ((nid = OBJ_txt2nid(type)) == NID_undef)
  1150. goto start2;
  1151. if (BIO_snprintf(buf, sizeof(buf), "%s_default", type)
  1152. >= (int)sizeof(buf)) {
  1153. BIO_printf(bio_err, "Name '%s' too long\n", v->name);
  1154. return 0;
  1155. }
  1156. if ((def = NCONF_get_string(req_conf, attr_sect, buf))
  1157. == NULL) {
  1158. ERR_clear_error();
  1159. def = "";
  1160. }
  1161. BIO_snprintf(buf, sizeof(buf), "%s_value", type);
  1162. if ((value = NCONF_get_string(req_conf, attr_sect, buf))
  1163. == NULL) {
  1164. ERR_clear_error();
  1165. value = NULL;
  1166. }
  1167. BIO_snprintf(buf, sizeof(buf), "%s_min", type);
  1168. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
  1169. ERR_clear_error();
  1170. n_min = -1;
  1171. }
  1172. BIO_snprintf(buf, sizeof(buf), "%s_max", type);
  1173. if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
  1174. ERR_clear_error();
  1175. n_max = -1;
  1176. }
  1177. if (!add_attribute_object(req,
  1178. v->value, def, value, nid, n_min,
  1179. n_max, chtype))
  1180. return 0;
  1181. }
  1182. }
  1183. } else {
  1184. BIO_printf(bio_err, "No template, please set one up.\n");
  1185. return 0;
  1186. }
  1187. return 1;
  1188. }
  1189. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
  1190. STACK_OF(CONF_VALUE) *attr_sk, int attribs,
  1191. unsigned long chtype)
  1192. {
  1193. int i;
  1194. char *p, *q;
  1195. char *type;
  1196. CONF_VALUE *v;
  1197. X509_NAME *subj;
  1198. subj = X509_REQ_get_subject_name(req);
  1199. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
  1200. int mval;
  1201. v = sk_CONF_VALUE_value(dn_sk, i);
  1202. p = q = NULL;
  1203. type = v->name;
  1204. /*
  1205. * Skip past any leading X. X: X, etc to allow for multiple instances
  1206. */
  1207. for (p = v->name; *p; p++)
  1208. #ifndef CHARSET_EBCDIC
  1209. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1210. #else
  1211. if ((*p == os_toascii[':']) || (*p == os_toascii[','])
  1212. || (*p == os_toascii['.'])) {
  1213. #endif
  1214. p++;
  1215. if (*p)
  1216. type = p;
  1217. break;
  1218. }
  1219. #ifndef CHARSET_EBCDIC
  1220. if (*type == '+') {
  1221. #else
  1222. if (*type == os_toascii['+']) {
  1223. #endif
  1224. type++;
  1225. mval = -1;
  1226. } else
  1227. mval = 0;
  1228. if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
  1229. (unsigned char *)v->value, -1, -1,
  1230. mval))
  1231. return 0;
  1232. }
  1233. if (!X509_NAME_entry_count(subj)) {
  1234. BIO_printf(bio_err, "error, no objects specified in config file\n");
  1235. return 0;
  1236. }
  1237. if (attribs) {
  1238. for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
  1239. v = sk_CONF_VALUE_value(attr_sk, i);
  1240. if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
  1241. (unsigned char *)v->value, -1))
  1242. return 0;
  1243. }
  1244. }
  1245. return 1;
  1246. }
  1247. static int add_DN_object(X509_NAME *n, char *text, const char *def,
  1248. char *value, int nid, int n_min, int n_max,
  1249. unsigned long chtype, int mval)
  1250. {
  1251. int i, ret = 0;
  1252. MS_STATIC char buf[1024];
  1253. start:
  1254. if (!batch)
  1255. BIO_printf(bio_err, "%s [%s]:", text, def);
  1256. (void)BIO_flush(bio_err);
  1257. if (value != NULL) {
  1258. BUF_strlcpy(buf, value, sizeof(buf));
  1259. BUF_strlcat(buf, "\n", sizeof(buf));
  1260. BIO_printf(bio_err, "%s\n", value);
  1261. } else {
  1262. buf[0] = '\0';
  1263. if (!batch) {
  1264. if (!fgets(buf, sizeof(buf), stdin))
  1265. return 0;
  1266. } else {
  1267. buf[0] = '\n';
  1268. buf[1] = '\0';
  1269. }
  1270. }
  1271. if (buf[0] == '\0')
  1272. return (0);
  1273. else if (buf[0] == '\n') {
  1274. if ((def == NULL) || (def[0] == '\0'))
  1275. return (1);
  1276. BUF_strlcpy(buf, def, sizeof(buf));
  1277. BUF_strlcat(buf, "\n", sizeof(buf));
  1278. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1279. return (1);
  1280. i = strlen(buf);
  1281. if (buf[i - 1] != '\n') {
  1282. BIO_printf(bio_err, "weird input :-(\n");
  1283. return (0);
  1284. }
  1285. buf[--i] = '\0';
  1286. #ifdef CHARSET_EBCDIC
  1287. ebcdic2ascii(buf, buf, i);
  1288. #endif
  1289. if (!req_check_len(i, n_min, n_max)) {
  1290. if (batch || value)
  1291. return 0;
  1292. goto start;
  1293. }
  1294. if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
  1295. (unsigned char *)buf, -1, -1, mval))
  1296. goto err;
  1297. ret = 1;
  1298. err:
  1299. return (ret);
  1300. }
  1301. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  1302. char *value, int nid, int n_min,
  1303. int n_max, unsigned long chtype)
  1304. {
  1305. int i;
  1306. static char buf[1024];
  1307. start:
  1308. if (!batch)
  1309. BIO_printf(bio_err, "%s [%s]:", text, def);
  1310. (void)BIO_flush(bio_err);
  1311. if (value != NULL) {
  1312. BUF_strlcpy(buf, value, sizeof(buf));
  1313. BUF_strlcat(buf, "\n", sizeof(buf));
  1314. BIO_printf(bio_err, "%s\n", value);
  1315. } else {
  1316. buf[0] = '\0';
  1317. if (!batch) {
  1318. if (!fgets(buf, sizeof(buf), stdin))
  1319. return 0;
  1320. } else {
  1321. buf[0] = '\n';
  1322. buf[1] = '\0';
  1323. }
  1324. }
  1325. if (buf[0] == '\0')
  1326. return (0);
  1327. else if (buf[0] == '\n') {
  1328. if ((def == NULL) || (def[0] == '\0'))
  1329. return (1);
  1330. BUF_strlcpy(buf, def, sizeof(buf));
  1331. BUF_strlcat(buf, "\n", sizeof(buf));
  1332. } else if ((buf[0] == '.') && (buf[1] == '\n'))
  1333. return (1);
  1334. i = strlen(buf);
  1335. if (buf[i - 1] != '\n') {
  1336. BIO_printf(bio_err, "weird input :-(\n");
  1337. return (0);
  1338. }
  1339. buf[--i] = '\0';
  1340. #ifdef CHARSET_EBCDIC
  1341. ebcdic2ascii(buf, buf, i);
  1342. #endif
  1343. if (!req_check_len(i, n_min, n_max)) {
  1344. if (batch || value)
  1345. return 0;
  1346. goto start;
  1347. }
  1348. if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
  1349. (unsigned char *)buf, -1)) {
  1350. BIO_printf(bio_err, "Error adding attribute\n");
  1351. ERR_print_errors(bio_err);
  1352. goto err;
  1353. }
  1354. return (1);
  1355. err:
  1356. return (0);
  1357. }
  1358. static int req_check_len(int len, int n_min, int n_max)
  1359. {
  1360. if ((n_min > 0) && (len < n_min)) {
  1361. BIO_printf(bio_err,
  1362. "string is too short, it needs to be at least %d bytes long\n",
  1363. n_min);
  1364. return (0);
  1365. }
  1366. if ((n_max >= 0) && (len > n_max)) {
  1367. BIO_printf(bio_err,
  1368. "string is too long, it needs to be less than %d bytes long\n",
  1369. n_max);
  1370. return (0);
  1371. }
  1372. return (1);
  1373. }
  1374. /* Check if the end of a string matches 'end' */
  1375. static int check_end(const char *str, const char *end)
  1376. {
  1377. int elen, slen;
  1378. const char *tmp;
  1379. elen = strlen(end);
  1380. slen = strlen(str);
  1381. if (elen > slen)
  1382. return 1;
  1383. tmp = str + slen - elen;
  1384. return strcmp(tmp, end);
  1385. }
  1386. static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
  1387. int *pkey_type, long *pkeylen,
  1388. char **palgnam, ENGINE *keygen_engine)
  1389. {
  1390. EVP_PKEY_CTX *gctx = NULL;
  1391. EVP_PKEY *param = NULL;
  1392. long keylen = -1;
  1393. BIO *pbio = NULL;
  1394. const char *paramfile = NULL;
  1395. if (gstr == NULL) {
  1396. *pkey_type = EVP_PKEY_RSA;
  1397. keylen = *pkeylen;
  1398. } else if (gstr[0] >= '0' && gstr[0] <= '9') {
  1399. *pkey_type = EVP_PKEY_RSA;
  1400. keylen = atol(gstr);
  1401. *pkeylen = keylen;
  1402. } else if (!strncmp(gstr, "param:", 6))
  1403. paramfile = gstr + 6;
  1404. else {
  1405. const char *p = strchr(gstr, ':');
  1406. int len;
  1407. ENGINE *tmpeng;
  1408. const EVP_PKEY_ASN1_METHOD *ameth;
  1409. if (p)
  1410. len = p - gstr;
  1411. else
  1412. len = strlen(gstr);
  1413. /*
  1414. * The lookup of a the string will cover all engines so keep a note
  1415. * of the implementation.
  1416. */
  1417. ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
  1418. if (!ameth) {
  1419. BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr);
  1420. return NULL;
  1421. }
  1422. EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
  1423. #ifndef OPENSSL_NO_ENGINE
  1424. if (tmpeng)
  1425. ENGINE_finish(tmpeng);
  1426. #endif
  1427. if (*pkey_type == EVP_PKEY_RSA) {
  1428. if (p) {
  1429. keylen = atol(p + 1);
  1430. *pkeylen = keylen;
  1431. } else
  1432. keylen = *pkeylen;
  1433. } else if (p)
  1434. paramfile = p + 1;
  1435. }
  1436. if (paramfile) {
  1437. pbio = BIO_new_file(paramfile, "r");
  1438. if (!pbio) {
  1439. BIO_printf(err, "Can't open parameter file %s\n", paramfile);
  1440. return NULL;
  1441. }
  1442. param = PEM_read_bio_Parameters(pbio, NULL);
  1443. if (!param) {
  1444. X509 *x;
  1445. (void)BIO_reset(pbio);
  1446. x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
  1447. if (x) {
  1448. param = X509_get_pubkey(x);
  1449. X509_free(x);
  1450. }
  1451. }
  1452. BIO_free(pbio);
  1453. if (!param) {
  1454. BIO_printf(err, "Error reading parameter file %s\n", paramfile);
  1455. return NULL;
  1456. }
  1457. if (*pkey_type == -1)
  1458. *pkey_type = EVP_PKEY_id(param);
  1459. else if (*pkey_type != EVP_PKEY_base_id(param)) {
  1460. BIO_printf(err, "Key Type does not match parameters\n");
  1461. EVP_PKEY_free(param);
  1462. return NULL;
  1463. }
  1464. }
  1465. if (palgnam) {
  1466. const EVP_PKEY_ASN1_METHOD *ameth;
  1467. ENGINE *tmpeng;
  1468. const char *anam;
  1469. ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
  1470. if (!ameth) {
  1471. BIO_puts(err, "Internal error: can't find key algorithm\n");
  1472. return NULL;
  1473. }
  1474. EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
  1475. *palgnam = BUF_strdup(anam);
  1476. #ifndef OPENSSL_NO_ENGINE
  1477. if (tmpeng)
  1478. ENGINE_finish(tmpeng);
  1479. #endif
  1480. }
  1481. if (param) {
  1482. gctx = EVP_PKEY_CTX_new(param, keygen_engine);
  1483. *pkeylen = EVP_PKEY_bits(param);
  1484. EVP_PKEY_free(param);
  1485. } else
  1486. gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
  1487. if (!gctx) {
  1488. BIO_puts(err, "Error allocating keygen context\n");
  1489. ERR_print_errors(err);
  1490. return NULL;
  1491. }
  1492. if (EVP_PKEY_keygen_init(gctx) <= 0) {
  1493. BIO_puts(err, "Error initializing keygen context\n");
  1494. ERR_print_errors(err);
  1495. return NULL;
  1496. }
  1497. #ifndef OPENSSL_NO_RSA
  1498. if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
  1499. if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
  1500. BIO_puts(err, "Error setting RSA keysize\n");
  1501. ERR_print_errors(err);
  1502. EVP_PKEY_CTX_free(gctx);
  1503. return NULL;
  1504. }
  1505. }
  1506. #endif
  1507. return gctx;
  1508. }
  1509. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  1510. {
  1511. char c = '*';
  1512. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  1513. int p;
  1514. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  1515. if (p == 0)
  1516. c = '.';
  1517. if (p == 1)
  1518. c = '+';
  1519. if (p == 2)
  1520. c = '*';
  1521. if (p == 3)
  1522. c = '\n';
  1523. BIO_write(b, &c, 1);
  1524. (void)BIO_flush(b);
  1525. #ifdef LINT
  1526. p = n;
  1527. #endif
  1528. return 1;
  1529. }
  1530. static int do_sign_init(BIO *err, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
  1531. const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts)
  1532. {
  1533. EVP_PKEY_CTX *pkctx = NULL;
  1534. int i;
  1535. EVP_MD_CTX_init(ctx);
  1536. if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
  1537. return 0;
  1538. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  1539. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  1540. if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
  1541. BIO_printf(err, "parameter error \"%s\"\n", sigopt);
  1542. ERR_print_errors(bio_err);
  1543. return 0;
  1544. }
  1545. }
  1546. return 1;
  1547. }
  1548. int do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md,
  1549. STACK_OF(OPENSSL_STRING) *sigopts)
  1550. {
  1551. int rv;
  1552. EVP_MD_CTX mctx;
  1553. EVP_MD_CTX_init(&mctx);
  1554. rv = do_sign_init(err, &mctx, pkey, md, sigopts);
  1555. if (rv > 0)
  1556. rv = X509_sign_ctx(x, &mctx);
  1557. EVP_MD_CTX_cleanup(&mctx);
  1558. return rv > 0 ? 1 : 0;
  1559. }
  1560. int do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md,
  1561. STACK_OF(OPENSSL_STRING) *sigopts)
  1562. {
  1563. int rv;
  1564. EVP_MD_CTX mctx;
  1565. EVP_MD_CTX_init(&mctx);
  1566. rv = do_sign_init(err, &mctx, pkey, md, sigopts);
  1567. if (rv > 0)
  1568. rv = X509_REQ_sign_ctx(x, &mctx);
  1569. EVP_MD_CTX_cleanup(&mctx);
  1570. return rv > 0 ? 1 : 0;
  1571. }
  1572. int do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md,
  1573. STACK_OF(OPENSSL_STRING) *sigopts)
  1574. {
  1575. int rv;
  1576. EVP_MD_CTX mctx;
  1577. EVP_MD_CTX_init(&mctx);
  1578. rv = do_sign_init(err, &mctx, pkey, md, sigopts);
  1579. if (rv > 0)
  1580. rv = X509_CRL_sign_ctx(x, &mctx);
  1581. EVP_MD_CTX_cleanup(&mctx);
  1582. return rv > 0 ? 1 : 0;
  1583. }