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 (pkey == NULL) goto end;
  752. if (keyout == NULL)
  753. {
  754. keyout=NCONF_get_string(req_conf,SECTION,KEYFILE);
  755. if (keyout == NULL)
  756. ERR_clear_error();
  757. }
  758. if (keyout == NULL)
  759. {
  760. BIO_printf(bio_err,"writing new private key to stdout\n");
  761. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  762. #ifdef OPENSSL_SYS_VMS
  763. {
  764. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  765. out = BIO_push(tmpbio, out);
  766. }
  767. #endif
  768. }
  769. else
  770. {
  771. BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
  772. if (BIO_write_filename(out,keyout) <= 0)
  773. {
  774. perror(keyout);
  775. goto end;
  776. }
  777. }
  778. p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
  779. if (p == NULL)
  780. {
  781. ERR_clear_error();
  782. p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
  783. if (p == NULL)
  784. ERR_clear_error();
  785. }
  786. if ((p != NULL) && (strcmp(p,"no") == 0))
  787. cipher=NULL;
  788. if (nodes) cipher=NULL;
  789. i=0;
  790. loop:
  791. if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
  792. NULL,0,NULL,passout))
  793. {
  794. if ((ERR_GET_REASON(ERR_peek_error()) ==
  795. PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
  796. {
  797. ERR_clear_error();
  798. i++;
  799. goto loop;
  800. }
  801. goto end;
  802. }
  803. BIO_printf(bio_err,"-----\n");
  804. }
  805. if (!newreq)
  806. {
  807. /* Since we are using a pre-existing certificate
  808. * request, the kludge 'format' info should not be
  809. * changed. */
  810. kludge= -1;
  811. if (infile == NULL)
  812. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  813. else
  814. {
  815. if (BIO_read_filename(in,infile) <= 0)
  816. {
  817. perror(infile);
  818. goto end;
  819. }
  820. }
  821. if (informat == FORMAT_ASN1)
  822. req=d2i_X509_REQ_bio(in,NULL);
  823. else if (informat == FORMAT_PEM)
  824. req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
  825. else
  826. {
  827. BIO_printf(bio_err,"bad input format specified for X509 request\n");
  828. goto end;
  829. }
  830. if (req == NULL)
  831. {
  832. BIO_printf(bio_err,"unable to load X509 request\n");
  833. goto end;
  834. }
  835. }
  836. if (newreq || x509)
  837. {
  838. if (pkey == NULL)
  839. {
  840. BIO_printf(bio_err,"you need to specify a private key\n");
  841. goto end;
  842. }
  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. if (req == NULL)
  852. {
  853. req=X509_REQ_new();
  854. if (req == NULL)
  855. {
  856. goto end;
  857. }
  858. i=make_REQ(req,pkey,subj,multirdn,!x509, chtype);
  859. subj=NULL; /* done processing '-subj' option */
  860. if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
  861. {
  862. sk_X509_ATTRIBUTE_free(req->req_info->attributes);
  863. req->req_info->attributes = NULL;
  864. }
  865. if (!i)
  866. {
  867. BIO_printf(bio_err,"problems making Certificate Request\n");
  868. goto end;
  869. }
  870. }
  871. if (x509)
  872. {
  873. EVP_PKEY *tmppkey;
  874. X509V3_CTX ext_ctx;
  875. if ((x509ss=X509_new()) == NULL) goto end;
  876. /* Set version to V3 */
  877. if(extensions && !X509_set_version(x509ss, 2)) goto end;
  878. if (serial)
  879. {
  880. if (!X509_set_serialNumber(x509ss, serial)) goto end;
  881. }
  882. else
  883. {
  884. if (!rand_serial(NULL,
  885. X509_get_serialNumber(x509ss)))
  886. goto end;
  887. }
  888. if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
  889. if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
  890. if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
  891. if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
  892. tmppkey = X509_REQ_get_pubkey(req);
  893. if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
  894. EVP_PKEY_free(tmppkey);
  895. /* Set up V3 context struct */
  896. X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
  897. X509V3_set_nconf(&ext_ctx, req_conf);
  898. /* Add extensions */
  899. if(extensions && !X509V3_EXT_add_nconf(req_conf,
  900. &ext_ctx, extensions, x509ss))
  901. {
  902. BIO_printf(bio_err,
  903. "Error Loading extension section %s\n",
  904. extensions);
  905. goto end;
  906. }
  907. if (!(i=X509_sign(x509ss,pkey,digest)))
  908. goto end;
  909. }
  910. else
  911. {
  912. X509V3_CTX ext_ctx;
  913. /* Set up V3 context struct */
  914. X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
  915. X509V3_set_nconf(&ext_ctx, req_conf);
  916. /* Add extensions */
  917. if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
  918. &ext_ctx, req_exts, req))
  919. {
  920. BIO_printf(bio_err,
  921. "Error Loading extension section %s\n",
  922. req_exts);
  923. goto end;
  924. }
  925. if (!(i=X509_REQ_sign(req,pkey,digest)))
  926. goto end;
  927. }
  928. }
  929. if (subj && x509)
  930. {
  931. BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
  932. goto end;
  933. }
  934. if (subj && !x509)
  935. {
  936. if (verbose)
  937. {
  938. BIO_printf(bio_err, "Modifying Request's Subject\n");
  939. print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
  940. }
  941. if (build_subject(req, subj, chtype, multirdn) == 0)
  942. {
  943. BIO_printf(bio_err, "ERROR: cannot modify subject\n");
  944. ex=1;
  945. goto end;
  946. }
  947. req->req_info->enc.modified = 1;
  948. if (verbose)
  949. {
  950. print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
  951. }
  952. }
  953. if (verify && !x509)
  954. {
  955. int tmp=0;
  956. if (pkey == NULL)
  957. {
  958. pkey=X509_REQ_get_pubkey(req);
  959. tmp=1;
  960. if (pkey == NULL) goto end;
  961. }
  962. i=X509_REQ_verify(req,pkey);
  963. if (tmp) {
  964. EVP_PKEY_free(pkey);
  965. pkey=NULL;
  966. }
  967. if (i < 0)
  968. {
  969. goto end;
  970. }
  971. else if (i == 0)
  972. {
  973. BIO_printf(bio_err,"verify failure\n");
  974. ERR_print_errors(bio_err);
  975. }
  976. else /* if (i > 0) */
  977. BIO_printf(bio_err,"verify OK\n");
  978. }
  979. if (noout && !text && !modulus && !subject && !pubkey)
  980. {
  981. ex=0;
  982. goto end;
  983. }
  984. if (outfile == NULL)
  985. {
  986. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  987. #ifdef OPENSSL_SYS_VMS
  988. {
  989. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  990. out = BIO_push(tmpbio, out);
  991. }
  992. #endif
  993. }
  994. else
  995. {
  996. if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
  997. i=(int)BIO_append_filename(out,outfile);
  998. else
  999. i=(int)BIO_write_filename(out,outfile);
  1000. if (!i)
  1001. {
  1002. perror(outfile);
  1003. goto end;
  1004. }
  1005. }
  1006. if (pubkey)
  1007. {
  1008. EVP_PKEY *tpubkey;
  1009. tpubkey=X509_REQ_get_pubkey(req);
  1010. if (tpubkey == NULL)
  1011. {
  1012. BIO_printf(bio_err,"Error getting public key\n");
  1013. ERR_print_errors(bio_err);
  1014. goto end;
  1015. }
  1016. PEM_write_bio_PUBKEY(out, tpubkey);
  1017. EVP_PKEY_free(tpubkey);
  1018. }
  1019. if (text)
  1020. {
  1021. if (x509)
  1022. X509_print_ex(out, x509ss, nmflag, reqflag);
  1023. else
  1024. X509_REQ_print_ex(out, req, nmflag, reqflag);
  1025. }
  1026. if(subject)
  1027. {
  1028. if(x509)
  1029. print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
  1030. else
  1031. print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
  1032. }
  1033. if (modulus)
  1034. {
  1035. EVP_PKEY *tpubkey;
  1036. if (x509)
  1037. tpubkey=X509_get_pubkey(x509ss);
  1038. else
  1039. tpubkey=X509_REQ_get_pubkey(req);
  1040. if (tpubkey == NULL)
  1041. {
  1042. fprintf(stdout,"Modulus=unavailable\n");
  1043. goto end;
  1044. }
  1045. fprintf(stdout,"Modulus=");
  1046. #ifndef OPENSSL_NO_RSA
  1047. if (tpubkey->type == EVP_PKEY_RSA)
  1048. BN_print(out,tpubkey->pkey.rsa->n);
  1049. else
  1050. #endif
  1051. fprintf(stdout,"Wrong Algorithm type");
  1052. EVP_PKEY_free(tpubkey);
  1053. fprintf(stdout,"\n");
  1054. }
  1055. if (!noout && !x509)
  1056. {
  1057. if (outformat == FORMAT_ASN1)
  1058. i=i2d_X509_REQ_bio(out,req);
  1059. else if (outformat == FORMAT_PEM) {
  1060. if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
  1061. else i=PEM_write_bio_X509_REQ(out,req);
  1062. } else {
  1063. BIO_printf(bio_err,"bad output format specified for outfile\n");
  1064. goto end;
  1065. }
  1066. if (!i)
  1067. {
  1068. BIO_printf(bio_err,"unable to write X509 request\n");
  1069. goto end;
  1070. }
  1071. }
  1072. if (!noout && x509 && (x509ss != NULL))
  1073. {
  1074. if (outformat == FORMAT_ASN1)
  1075. i=i2d_X509_bio(out,x509ss);
  1076. else if (outformat == FORMAT_PEM)
  1077. i=PEM_write_bio_X509(out,x509ss);
  1078. else {
  1079. BIO_printf(bio_err,"bad output format specified for outfile\n");
  1080. goto end;
  1081. }
  1082. if (!i)
  1083. {
  1084. BIO_printf(bio_err,"unable to write X509 certificate\n");
  1085. goto end;
  1086. }
  1087. }
  1088. ex=0;
  1089. end:
  1090. #ifndef MONOLITH
  1091. if(to_free)
  1092. OPENSSL_free(to_free);
  1093. #endif
  1094. if (ex)
  1095. {
  1096. ERR_print_errors(bio_err);
  1097. }
  1098. if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf);
  1099. BIO_free(in);
  1100. BIO_free_all(out);
  1101. EVP_PKEY_free(pkey);
  1102. X509_REQ_free(req);
  1103. X509_free(x509ss);
  1104. ASN1_INTEGER_free(serial);
  1105. if(passargin && passin) OPENSSL_free(passin);
  1106. if(passargout && passout) OPENSSL_free(passout);
  1107. OBJ_cleanup();
  1108. #ifndef OPENSSL_NO_DSA
  1109. if (dsa_params != NULL) DSA_free(dsa_params);
  1110. #endif
  1111. #ifndef OPENSSL_NO_ECDSA
  1112. if (ec_params != NULL) EC_KEY_free(ec_params);
  1113. #endif
  1114. apps_shutdown();
  1115. OPENSSL_EXIT(ex);
  1116. }
  1117. static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
  1118. int attribs, unsigned long chtype)
  1119. {
  1120. int ret=0,i;
  1121. char no_prompt = 0;
  1122. STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
  1123. char *tmp, *dn_sect,*attr_sect;
  1124. tmp=NCONF_get_string(req_conf,SECTION,PROMPT);
  1125. if (tmp == NULL)
  1126. ERR_clear_error();
  1127. if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
  1128. dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
  1129. if (dn_sect == NULL)
  1130. {
  1131. BIO_printf(bio_err,"unable to find '%s' in config\n",
  1132. DISTINGUISHED_NAME);
  1133. goto err;
  1134. }
  1135. dn_sk=NCONF_get_section(req_conf,dn_sect);
  1136. if (dn_sk == NULL)
  1137. {
  1138. BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect);
  1139. goto err;
  1140. }
  1141. attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES);
  1142. if (attr_sect == NULL)
  1143. {
  1144. ERR_clear_error();
  1145. attr_sk=NULL;
  1146. }
  1147. else
  1148. {
  1149. attr_sk=NCONF_get_section(req_conf,attr_sect);
  1150. if (attr_sk == NULL)
  1151. {
  1152. BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect);
  1153. goto err;
  1154. }
  1155. }
  1156. /* setup version number */
  1157. if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */
  1158. if (no_prompt)
  1159. i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
  1160. else
  1161. {
  1162. if (subj)
  1163. i = build_subject(req, subj, chtype, multirdn);
  1164. else
  1165. i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
  1166. }
  1167. if(!i) goto err;
  1168. if (!X509_REQ_set_pubkey(req,pkey)) goto err;
  1169. ret=1;
  1170. err:
  1171. return(ret);
  1172. }
  1173. /*
  1174. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1175. * where characters may be escaped by \
  1176. */
  1177. static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn)
  1178. {
  1179. X509_NAME *n;
  1180. if (!(n = parse_name(subject, chtype, multirdn)))
  1181. return 0;
  1182. if (!X509_REQ_set_subject_name(req, n))
  1183. {
  1184. X509_NAME_free(n);
  1185. return 0;
  1186. }
  1187. X509_NAME_free(n);
  1188. return 1;
  1189. }
  1190. static int prompt_info(X509_REQ *req,
  1191. STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
  1192. STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
  1193. unsigned long chtype)
  1194. {
  1195. int i;
  1196. char *p,*q;
  1197. char buf[100];
  1198. int nid, mval;
  1199. long n_min,n_max;
  1200. char *type, *value;
  1201. const char *def;
  1202. CONF_VALUE *v;
  1203. X509_NAME *subj;
  1204. subj = X509_REQ_get_subject_name(req);
  1205. if(!batch)
  1206. {
  1207. BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
  1208. BIO_printf(bio_err,"into your certificate request.\n");
  1209. BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
  1210. BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
  1211. BIO_printf(bio_err,"For some fields there will be a default value,\n");
  1212. BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
  1213. BIO_printf(bio_err,"-----\n");
  1214. }
  1215. if (sk_CONF_VALUE_num(dn_sk))
  1216. {
  1217. i= -1;
  1218. start: for (;;)
  1219. {
  1220. i++;
  1221. if (sk_CONF_VALUE_num(dn_sk) <= i) break;
  1222. v=sk_CONF_VALUE_value(dn_sk,i);
  1223. p=q=NULL;
  1224. type=v->name;
  1225. if(!check_end(type,"_min") || !check_end(type,"_max") ||
  1226. !check_end(type,"_default") ||
  1227. !check_end(type,"_value")) continue;
  1228. /* Skip past any leading X. X: X, etc to allow for
  1229. * multiple instances
  1230. */
  1231. for(p = v->name; *p ; p++)
  1232. if ((*p == ':') || (*p == ',') ||
  1233. (*p == '.')) {
  1234. p++;
  1235. if(*p) type = p;
  1236. break;
  1237. }
  1238. if (*type == '+')
  1239. {
  1240. mval = -1;
  1241. type++;
  1242. }
  1243. else
  1244. mval = 0;
  1245. /* If OBJ not recognised ignore it */
  1246. if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
  1247. if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name)
  1248. >= (int)sizeof(buf))
  1249. {
  1250. BIO_printf(bio_err,"Name '%s' too long\n",v->name);
  1251. return 0;
  1252. }
  1253. if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
  1254. {
  1255. ERR_clear_error();
  1256. def="";
  1257. }
  1258. BIO_snprintf(buf,sizeof buf,"%s_value",v->name);
  1259. if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
  1260. {
  1261. ERR_clear_error();
  1262. value=NULL;
  1263. }
  1264. BIO_snprintf(buf,sizeof buf,"%s_min",v->name);
  1265. if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min))
  1266. {
  1267. ERR_clear_error();
  1268. n_min = -1;
  1269. }
  1270. BIO_snprintf(buf,sizeof buf,"%s_max",v->name);
  1271. if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max))
  1272. {
  1273. ERR_clear_error();
  1274. n_max = -1;
  1275. }
  1276. if (!add_DN_object(subj,v->value,def,value,nid,
  1277. n_min,n_max, chtype, mval))
  1278. return 0;
  1279. }
  1280. if (X509_NAME_entry_count(subj) == 0)
  1281. {
  1282. BIO_printf(bio_err,"error, no objects specified in config file\n");
  1283. return 0;
  1284. }
  1285. if (attribs)
  1286. {
  1287. if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch))
  1288. {
  1289. BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
  1290. BIO_printf(bio_err,"to be sent with your certificate request\n");
  1291. }
  1292. i= -1;
  1293. start2: for (;;)
  1294. {
  1295. i++;
  1296. if ((attr_sk == NULL) ||
  1297. (sk_CONF_VALUE_num(attr_sk) <= i))
  1298. break;
  1299. v=sk_CONF_VALUE_value(attr_sk,i);
  1300. type=v->name;
  1301. if ((nid=OBJ_txt2nid(type)) == NID_undef)
  1302. goto start2;
  1303. if (BIO_snprintf(buf,sizeof buf,"%s_default",type)
  1304. >= (int)sizeof(buf))
  1305. {
  1306. BIO_printf(bio_err,"Name '%s' too long\n",v->name);
  1307. return 0;
  1308. }
  1309. if ((def=NCONF_get_string(req_conf,attr_sect,buf))
  1310. == NULL)
  1311. {
  1312. ERR_clear_error();
  1313. def="";
  1314. }
  1315. BIO_snprintf(buf,sizeof buf,"%s_value",type);
  1316. if ((value=NCONF_get_string(req_conf,attr_sect,buf))
  1317. == NULL)
  1318. {
  1319. ERR_clear_error();
  1320. value=NULL;
  1321. }
  1322. BIO_snprintf(buf,sizeof buf,"%s_min",type);
  1323. if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min))
  1324. n_min = -1;
  1325. BIO_snprintf(buf,sizeof buf,"%s_max",type);
  1326. if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max))
  1327. n_max = -1;
  1328. if (!add_attribute_object(req,
  1329. v->value,def,value,nid,n_min,n_max, chtype))
  1330. return 0;
  1331. }
  1332. }
  1333. }
  1334. else
  1335. {
  1336. BIO_printf(bio_err,"No template, please set one up.\n");
  1337. return 0;
  1338. }
  1339. return 1;
  1340. }
  1341. static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
  1342. STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype)
  1343. {
  1344. int i;
  1345. char *p,*q;
  1346. char *type;
  1347. CONF_VALUE *v;
  1348. X509_NAME *subj;
  1349. subj = X509_REQ_get_subject_name(req);
  1350. for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
  1351. {
  1352. int mval;
  1353. v=sk_CONF_VALUE_value(dn_sk,i);
  1354. p=q=NULL;
  1355. type=v->name;
  1356. /* Skip past any leading X. X: X, etc to allow for
  1357. * multiple instances
  1358. */
  1359. for(p = v->name; *p ; p++)
  1360. #ifndef CHARSET_EBCDIC
  1361. if ((*p == ':') || (*p == ',') || (*p == '.')) {
  1362. #else
  1363. if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
  1364. #endif
  1365. p++;
  1366. if(*p) type = p;
  1367. break;
  1368. }
  1369. #ifndef CHARSET_EBCDIC
  1370. if (*p == '+')
  1371. #else
  1372. if (*p == os_toascii['+'])
  1373. #endif
  1374. {
  1375. p++;
  1376. mval = -1;
  1377. }
  1378. else
  1379. mval = 0;
  1380. if (!X509_NAME_add_entry_by_txt(subj,type, chtype,
  1381. (unsigned char *) v->value,-1,-1,mval)) return 0;
  1382. }
  1383. if (!X509_NAME_entry_count(subj))
  1384. {
  1385. BIO_printf(bio_err,"error, no objects specified in config file\n");
  1386. return 0;
  1387. }
  1388. if (attribs)
  1389. {
  1390. for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
  1391. {
  1392. v=sk_CONF_VALUE_value(attr_sk,i);
  1393. if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
  1394. (unsigned char *)v->value, -1)) return 0;
  1395. }
  1396. }
  1397. return 1;
  1398. }
  1399. static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
  1400. int nid, int n_min, int n_max, unsigned long chtype, int mval)
  1401. {
  1402. int i,ret=0;
  1403. MS_STATIC char buf[1024];
  1404. start:
  1405. if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
  1406. (void)BIO_flush(bio_err);
  1407. if(value != NULL)
  1408. {
  1409. BUF_strlcpy(buf,value,sizeof buf);
  1410. BUF_strlcat(buf,"\n",sizeof buf);
  1411. BIO_printf(bio_err,"%s\n",value);
  1412. }
  1413. else
  1414. {
  1415. buf[0]='\0';
  1416. if (!batch)
  1417. {
  1418. fgets(buf,sizeof buf,stdin);
  1419. }
  1420. else
  1421. {
  1422. buf[0] = '\n';
  1423. buf[1] = '\0';
  1424. }
  1425. }
  1426. if (buf[0] == '\0') return(0);
  1427. else if (buf[0] == '\n')
  1428. {
  1429. if ((def == NULL) || (def[0] == '\0'))
  1430. return(1);
  1431. BUF_strlcpy(buf,def,sizeof buf);
  1432. BUF_strlcat(buf,"\n",sizeof buf);
  1433. }
  1434. else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
  1435. i=strlen(buf);
  1436. if (buf[i-1] != '\n')
  1437. {
  1438. BIO_printf(bio_err,"weird input :-(\n");
  1439. return(0);
  1440. }
  1441. buf[--i]='\0';
  1442. #ifdef CHARSET_EBCDIC
  1443. ebcdic2ascii(buf, buf, i);
  1444. #endif
  1445. if(!req_check_len(i, n_min, n_max)) goto start;
  1446. if (!X509_NAME_add_entry_by_NID(n,nid, chtype,
  1447. (unsigned char *) buf, -1,-1,mval)) goto err;
  1448. ret=1;
  1449. err:
  1450. return(ret);
  1451. }
  1452. static int add_attribute_object(X509_REQ *req, char *text, const char *def,
  1453. char *value, int nid, int n_min,
  1454. int n_max, unsigned long chtype)
  1455. {
  1456. int i;
  1457. static char buf[1024];
  1458. start:
  1459. if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
  1460. (void)BIO_flush(bio_err);
  1461. if (value != NULL)
  1462. {
  1463. BUF_strlcpy(buf,value,sizeof buf);
  1464. BUF_strlcat(buf,"\n",sizeof buf);
  1465. BIO_printf(bio_err,"%s\n",value);
  1466. }
  1467. else
  1468. {
  1469. buf[0]='\0';
  1470. if (!batch)
  1471. {
  1472. fgets(buf,sizeof buf,stdin);
  1473. }
  1474. else
  1475. {
  1476. buf[0] = '\n';
  1477. buf[1] = '\0';
  1478. }
  1479. }
  1480. if (buf[0] == '\0') return(0);
  1481. else if (buf[0] == '\n')
  1482. {
  1483. if ((def == NULL) || (def[0] == '\0'))
  1484. return(1);
  1485. BUF_strlcpy(buf,def,sizeof buf);
  1486. BUF_strlcat(buf,"\n",sizeof buf);
  1487. }
  1488. else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
  1489. i=strlen(buf);
  1490. if (buf[i-1] != '\n')
  1491. {
  1492. BIO_printf(bio_err,"weird input :-(\n");
  1493. return(0);
  1494. }
  1495. buf[--i]='\0';
  1496. #ifdef CHARSET_EBCDIC
  1497. ebcdic2ascii(buf, buf, i);
  1498. #endif
  1499. if(!req_check_len(i, n_min, n_max)) goto start;
  1500. if(!X509_REQ_add1_attr_by_NID(req, nid, chtype,
  1501. (unsigned char *)buf, -1)) {
  1502. BIO_printf(bio_err, "Error adding attribute\n");
  1503. ERR_print_errors(bio_err);
  1504. goto err;
  1505. }
  1506. return(1);
  1507. err:
  1508. return(0);
  1509. }
  1510. #ifndef OPENSSL_NO_RSA
  1511. static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb)
  1512. {
  1513. char c='*';
  1514. if (p == 0) c='.';
  1515. if (p == 1) c='+';
  1516. if (p == 2) c='*';
  1517. if (p == 3) c='\n';
  1518. BIO_write(cb->arg,&c,1);
  1519. (void)BIO_flush(cb->arg);
  1520. #ifdef LINT
  1521. p=n;
  1522. #endif
  1523. return 1;
  1524. }
  1525. #endif
  1526. static int req_check_len(int len, int n_min, int n_max)
  1527. {
  1528. if ((n_min > 0) && (len < n_min))
  1529. {
  1530. BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min);
  1531. return(0);
  1532. }
  1533. if ((n_max >= 0) && (len > n_max))
  1534. {
  1535. BIO_printf(bio_err,"string is too long, it needs to be less than %d bytes long\n",n_max);
  1536. return(0);
  1537. }
  1538. return(1);
  1539. }
  1540. /* Check if the end of a string matches 'end' */
  1541. static int check_end(const char *str, const char *end)
  1542. {
  1543. int elen, slen;
  1544. const char *tmp;
  1545. elen = strlen(end);
  1546. slen = strlen(str);
  1547. if(elen > slen) return 1;
  1548. tmp = str + slen - elen;
  1549. return strcmp(tmp, end);
  1550. }