req.c 41 KB

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