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