req.c 41 KB

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