apps.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  1. /* apps/apps.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. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
  112. /*
  113. * On VMS, you need to define this to get the declaration of fileno(). The
  114. * value 2 is to make sure no function defined in POSIX-2 is left undefined.
  115. */
  116. # define _POSIX_C_SOURCE 2
  117. #endif
  118. #include <stdio.h>
  119. #include <stdlib.h>
  120. #include <string.h>
  121. #if !defined(OPENSSL_SYSNAME_WIN32) && !defined(NETWARE_CLIB)
  122. # include <strings.h>
  123. #endif
  124. #include <sys/types.h>
  125. #include <ctype.h>
  126. #include <errno.h>
  127. #include <assert.h>
  128. #include <openssl/err.h>
  129. #include <openssl/x509.h>
  130. #include <openssl/x509v3.h>
  131. #include <openssl/pem.h>
  132. #include <openssl/pkcs12.h>
  133. #include <openssl/ui.h>
  134. #include <openssl/safestack.h>
  135. #ifndef OPENSSL_NO_ENGINE
  136. # include <openssl/engine.h>
  137. #endif
  138. #ifndef OPENSSL_NO_RSA
  139. # include <openssl/rsa.h>
  140. #endif
  141. #include <openssl/bn.h>
  142. #ifndef OPENSSL_NO_JPAKE
  143. # include <openssl/jpake.h>
  144. #endif
  145. #define NON_MAIN
  146. #include "apps.h"
  147. #undef NON_MAIN
  148. #ifdef _WIN32
  149. static int WIN32_rename(const char *from, const char *to);
  150. # define rename(from,to) WIN32_rename((from),(to))
  151. #endif
  152. typedef struct {
  153. const char *name;
  154. unsigned long flag;
  155. unsigned long mask;
  156. } NAME_EX_TBL;
  157. static UI_METHOD *ui_method = NULL;
  158. static int set_table_opts(unsigned long *flags, const char *arg,
  159. const NAME_EX_TBL * in_tbl);
  160. static int set_multi_opts(unsigned long *flags, const char *arg,
  161. const NAME_EX_TBL * in_tbl);
  162. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  163. /* Looks like this stuff is worth moving into separate function */
  164. static EVP_PKEY *load_netscape_key(BIO *err, BIO *key, const char *file,
  165. const char *key_descrip, int format);
  166. #endif
  167. int app_init(long mesgwin);
  168. #ifdef undef /* never finished - probably never will be
  169. * :-) */
  170. int args_from_file(char *file, int *argc, char **argv[])
  171. {
  172. FILE *fp;
  173. int num, i;
  174. unsigned int len;
  175. static char *buf = NULL;
  176. static char **arg = NULL;
  177. char *p;
  178. fp = fopen(file, "r");
  179. if (fp == NULL)
  180. return (0);
  181. if (fseek(fp, 0, SEEK_END) == 0)
  182. len = ftell(fp), rewind(fp);
  183. else
  184. len = -1;
  185. if (len <= 0) {
  186. fclose(fp);
  187. return (0);
  188. }
  189. *argc = 0;
  190. *argv = NULL;
  191. if (buf != NULL)
  192. OPENSSL_free(buf);
  193. buf = (char *)OPENSSL_malloc(len + 1);
  194. if (buf == NULL)
  195. return (0);
  196. len = fread(buf, 1, len, fp);
  197. if (len <= 1)
  198. return (0);
  199. buf[len] = '\0';
  200. i = 0;
  201. for (p = buf; *p; p++)
  202. if (*p == '\n')
  203. i++;
  204. if (arg != NULL)
  205. OPENSSL_free(arg);
  206. arg = (char **)OPENSSL_malloc(sizeof(char *) * (i * 2));
  207. *argv = arg;
  208. num = 0;
  209. p = buf;
  210. for (;;) {
  211. if (!*p)
  212. break;
  213. if (*p == '#') { /* comment line */
  214. while (*p && (*p != '\n'))
  215. p++;
  216. continue;
  217. }
  218. /* else we have a line */
  219. *(arg++) = p;
  220. num++;
  221. while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
  222. p++;
  223. if (!*p)
  224. break;
  225. if (*p == '\n') {
  226. *(p++) = '\0';
  227. continue;
  228. }
  229. /* else it is a tab or space */
  230. p++;
  231. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  232. p++;
  233. if (!*p)
  234. break;
  235. if (*p == '\n') {
  236. p++;
  237. continue;
  238. }
  239. *(arg++) = p++;
  240. num++;
  241. while (*p && (*p != '\n'))
  242. p++;
  243. if (!*p)
  244. break;
  245. /* else *p == '\n' */
  246. *(p++) = '\0';
  247. }
  248. *argc = num;
  249. return (1);
  250. }
  251. #endif
  252. int str2fmt(char *s)
  253. {
  254. if (s == NULL)
  255. return FORMAT_UNDEF;
  256. if ((*s == 'D') || (*s == 'd'))
  257. return (FORMAT_ASN1);
  258. else if ((*s == 'T') || (*s == 't'))
  259. return (FORMAT_TEXT);
  260. else if ((*s == 'N') || (*s == 'n'))
  261. return (FORMAT_NETSCAPE);
  262. else if ((*s == 'S') || (*s == 's'))
  263. return (FORMAT_SMIME);
  264. else if ((*s == 'M') || (*s == 'm'))
  265. return (FORMAT_MSBLOB);
  266. else if ((*s == '1')
  267. || (strcmp(s, "PKCS12") == 0) || (strcmp(s, "pkcs12") == 0)
  268. || (strcmp(s, "P12") == 0) || (strcmp(s, "p12") == 0))
  269. return (FORMAT_PKCS12);
  270. else if ((*s == 'E') || (*s == 'e'))
  271. return (FORMAT_ENGINE);
  272. else if ((*s == 'P') || (*s == 'p')) {
  273. if (s[1] == 'V' || s[1] == 'v')
  274. return FORMAT_PVK;
  275. else
  276. return (FORMAT_PEM);
  277. } else
  278. return (FORMAT_UNDEF);
  279. }
  280. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
  281. void program_name(char *in, char *out, int size)
  282. {
  283. int i, n;
  284. char *p = NULL;
  285. n = strlen(in);
  286. /* find the last '/', '\' or ':' */
  287. for (i = n - 1; i > 0; i--) {
  288. if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':')) {
  289. p = &(in[i + 1]);
  290. break;
  291. }
  292. }
  293. if (p == NULL)
  294. p = in;
  295. n = strlen(p);
  296. # if defined(OPENSSL_SYS_NETWARE)
  297. /* strip off trailing .nlm if present. */
  298. if ((n > 4) && (p[n - 4] == '.') &&
  299. ((p[n - 3] == 'n') || (p[n - 3] == 'N')) &&
  300. ((p[n - 2] == 'l') || (p[n - 2] == 'L')) &&
  301. ((p[n - 1] == 'm') || (p[n - 1] == 'M')))
  302. n -= 4;
  303. # else
  304. /* strip off trailing .exe if present. */
  305. if ((n > 4) && (p[n - 4] == '.') &&
  306. ((p[n - 3] == 'e') || (p[n - 3] == 'E')) &&
  307. ((p[n - 2] == 'x') || (p[n - 2] == 'X')) &&
  308. ((p[n - 1] == 'e') || (p[n - 1] == 'E')))
  309. n -= 4;
  310. # endif
  311. if (n > size - 1)
  312. n = size - 1;
  313. for (i = 0; i < n; i++) {
  314. if ((p[i] >= 'A') && (p[i] <= 'Z'))
  315. out[i] = p[i] - 'A' + 'a';
  316. else
  317. out[i] = p[i];
  318. }
  319. out[n] = '\0';
  320. }
  321. #else
  322. # ifdef OPENSSL_SYS_VMS
  323. void program_name(char *in, char *out, int size)
  324. {
  325. char *p = in, *q;
  326. char *chars = ":]>";
  327. while (*chars != '\0') {
  328. q = strrchr(p, *chars);
  329. if (q > p)
  330. p = q + 1;
  331. chars++;
  332. }
  333. q = strrchr(p, '.');
  334. if (q == NULL)
  335. q = p + strlen(p);
  336. strncpy(out, p, size - 1);
  337. if (q - p >= size) {
  338. out[size - 1] = '\0';
  339. } else {
  340. out[q - p] = '\0';
  341. }
  342. }
  343. # else
  344. void program_name(char *in, char *out, int size)
  345. {
  346. char *p;
  347. p = strrchr(in, '/');
  348. if (p != NULL)
  349. p++;
  350. else
  351. p = in;
  352. BUF_strlcpy(out, p, size);
  353. }
  354. # endif
  355. #endif
  356. int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
  357. {
  358. int num, i;
  359. char *p;
  360. *argc = 0;
  361. *argv = NULL;
  362. i = 0;
  363. if (arg->count == 0) {
  364. arg->count = 20;
  365. arg->data = (char **)OPENSSL_malloc(sizeof(char *) * arg->count);
  366. if (arg->data == NULL)
  367. return 0;
  368. }
  369. for (i = 0; i < arg->count; i++)
  370. arg->data[i] = NULL;
  371. num = 0;
  372. p = buf;
  373. for (;;) {
  374. /* first scan over white space */
  375. if (!*p)
  376. break;
  377. while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
  378. p++;
  379. if (!*p)
  380. break;
  381. /* The start of something good :-) */
  382. if (num >= arg->count) {
  383. char **tmp_p;
  384. int tlen = arg->count + 20;
  385. tmp_p = (char **)OPENSSL_realloc(arg->data,
  386. sizeof(char *) * tlen);
  387. if (tmp_p == NULL)
  388. return 0;
  389. arg->data = tmp_p;
  390. arg->count = tlen;
  391. /* initialize newly allocated data */
  392. for (i = num; i < arg->count; i++)
  393. arg->data[i] = NULL;
  394. }
  395. arg->data[num++] = p;
  396. /* now look for the end of this */
  397. if ((*p == '\'') || (*p == '\"')) { /* scan for closing quote */
  398. i = *(p++);
  399. arg->data[num - 1]++; /* jump over quote */
  400. while (*p && (*p != i))
  401. p++;
  402. *p = '\0';
  403. } else {
  404. while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
  405. p++;
  406. if (*p == '\0')
  407. p--;
  408. else
  409. *p = '\0';
  410. }
  411. p++;
  412. }
  413. *argc = num;
  414. *argv = arg->data;
  415. return (1);
  416. }
  417. #ifndef APP_INIT
  418. int app_init(long mesgwin)
  419. {
  420. return (1);
  421. }
  422. #endif
  423. int dump_cert_text(BIO *out, X509 *x)
  424. {
  425. char *p;
  426. p = X509_NAME_oneline(X509_get_subject_name(x), NULL, 0);
  427. BIO_puts(out, "subject=");
  428. BIO_puts(out, p);
  429. OPENSSL_free(p);
  430. p = X509_NAME_oneline(X509_get_issuer_name(x), NULL, 0);
  431. BIO_puts(out, "\nissuer=");
  432. BIO_puts(out, p);
  433. BIO_puts(out, "\n");
  434. OPENSSL_free(p);
  435. return 0;
  436. }
  437. static int ui_open(UI *ui)
  438. {
  439. return UI_method_get_opener(UI_OpenSSL())(ui);
  440. }
  441. static int ui_read(UI *ui, UI_STRING *uis)
  442. {
  443. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  444. && UI_get0_user_data(ui)) {
  445. switch (UI_get_string_type(uis)) {
  446. case UIT_PROMPT:
  447. case UIT_VERIFY:
  448. {
  449. const char *password =
  450. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  451. if (password && password[0] != '\0') {
  452. UI_set_result(ui, uis, password);
  453. return 1;
  454. }
  455. }
  456. default:
  457. break;
  458. }
  459. }
  460. return UI_method_get_reader(UI_OpenSSL())(ui, uis);
  461. }
  462. static int ui_write(UI *ui, UI_STRING *uis)
  463. {
  464. if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
  465. && UI_get0_user_data(ui)) {
  466. switch (UI_get_string_type(uis)) {
  467. case UIT_PROMPT:
  468. case UIT_VERIFY:
  469. {
  470. const char *password =
  471. ((PW_CB_DATA *)UI_get0_user_data(ui))->password;
  472. if (password && password[0] != '\0')
  473. return 1;
  474. }
  475. default:
  476. break;
  477. }
  478. }
  479. return UI_method_get_writer(UI_OpenSSL())(ui, uis);
  480. }
  481. static int ui_close(UI *ui)
  482. {
  483. return UI_method_get_closer(UI_OpenSSL())(ui);
  484. }
  485. int setup_ui_method(void)
  486. {
  487. ui_method = UI_create_method("OpenSSL application user interface");
  488. UI_method_set_opener(ui_method, ui_open);
  489. UI_method_set_reader(ui_method, ui_read);
  490. UI_method_set_writer(ui_method, ui_write);
  491. UI_method_set_closer(ui_method, ui_close);
  492. return 0;
  493. }
  494. void destroy_ui_method(void)
  495. {
  496. if (ui_method) {
  497. UI_destroy_method(ui_method);
  498. ui_method = NULL;
  499. }
  500. }
  501. int password_callback(char *buf, int bufsiz, int verify, PW_CB_DATA *cb_tmp)
  502. {
  503. UI *ui = NULL;
  504. int res = 0;
  505. const char *prompt_info = NULL;
  506. const char *password = NULL;
  507. PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
  508. if (cb_data) {
  509. if (cb_data->password)
  510. password = cb_data->password;
  511. if (cb_data->prompt_info)
  512. prompt_info = cb_data->prompt_info;
  513. }
  514. if (password) {
  515. res = strlen(password);
  516. if (res > bufsiz)
  517. res = bufsiz;
  518. memcpy(buf, password, res);
  519. return res;
  520. }
  521. ui = UI_new_method(ui_method);
  522. if (ui) {
  523. int ok = 0;
  524. char *buff = NULL;
  525. int ui_flags = 0;
  526. char *prompt = NULL;
  527. prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
  528. ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
  529. UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
  530. if (ok >= 0)
  531. ok = UI_add_input_string(ui, prompt, ui_flags, buf,
  532. PW_MIN_LENGTH, bufsiz - 1);
  533. if (ok >= 0 && verify) {
  534. buff = (char *)OPENSSL_malloc(bufsiz);
  535. ok = UI_add_verify_string(ui, prompt, ui_flags, buff,
  536. PW_MIN_LENGTH, bufsiz - 1, buf);
  537. }
  538. if (ok >= 0)
  539. do {
  540. ok = UI_process(ui);
  541. }
  542. while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
  543. if (buff) {
  544. OPENSSL_cleanse(buff, (unsigned int)bufsiz);
  545. OPENSSL_free(buff);
  546. }
  547. if (ok >= 0)
  548. res = strlen(buf);
  549. if (ok == -1) {
  550. BIO_printf(bio_err, "User interface error\n");
  551. ERR_print_errors(bio_err);
  552. OPENSSL_cleanse(buf, (unsigned int)bufsiz);
  553. res = 0;
  554. }
  555. if (ok == -2) {
  556. BIO_printf(bio_err, "aborted!\n");
  557. OPENSSL_cleanse(buf, (unsigned int)bufsiz);
  558. res = 0;
  559. }
  560. UI_free(ui);
  561. OPENSSL_free(prompt);
  562. }
  563. return res;
  564. }
  565. static char *app_get_pass(BIO *err, char *arg, int keepbio);
  566. int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
  567. {
  568. int same;
  569. if (!arg2 || !arg1 || strcmp(arg1, arg2))
  570. same = 0;
  571. else
  572. same = 1;
  573. if (arg1) {
  574. *pass1 = app_get_pass(err, arg1, same);
  575. if (!*pass1)
  576. return 0;
  577. } else if (pass1)
  578. *pass1 = NULL;
  579. if (arg2) {
  580. *pass2 = app_get_pass(err, arg2, same ? 2 : 0);
  581. if (!*pass2)
  582. return 0;
  583. } else if (pass2)
  584. *pass2 = NULL;
  585. return 1;
  586. }
  587. static char *app_get_pass(BIO *err, char *arg, int keepbio)
  588. {
  589. char *tmp, tpass[APP_PASS_LEN];
  590. static BIO *pwdbio = NULL;
  591. int i;
  592. if (!strncmp(arg, "pass:", 5))
  593. return BUF_strdup(arg + 5);
  594. if (!strncmp(arg, "env:", 4)) {
  595. tmp = getenv(arg + 4);
  596. if (!tmp) {
  597. BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
  598. return NULL;
  599. }
  600. return BUF_strdup(tmp);
  601. }
  602. if (!keepbio || !pwdbio) {
  603. if (!strncmp(arg, "file:", 5)) {
  604. pwdbio = BIO_new_file(arg + 5, "r");
  605. if (!pwdbio) {
  606. BIO_printf(err, "Can't open file %s\n", arg + 5);
  607. return NULL;
  608. }
  609. #if !defined(_WIN32)
  610. /*
  611. * Under _WIN32, which covers even Win64 and CE, file
  612. * descriptors referenced by BIO_s_fd are not inherited
  613. * by child process and therefore below is not an option.
  614. * It could have been an option if bss_fd.c was operating
  615. * on real Windows descriptors, such as those obtained
  616. * with CreateFile.
  617. */
  618. } else if (!strncmp(arg, "fd:", 3)) {
  619. BIO *btmp;
  620. i = atoi(arg + 3);
  621. if (i >= 0)
  622. pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
  623. if ((i < 0) || !pwdbio) {
  624. BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
  625. return NULL;
  626. }
  627. /*
  628. * Can't do BIO_gets on an fd BIO so add a buffering BIO
  629. */
  630. btmp = BIO_new(BIO_f_buffer());
  631. pwdbio = BIO_push(btmp, pwdbio);
  632. #endif
  633. } else if (!strcmp(arg, "stdin")) {
  634. pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
  635. if (!pwdbio) {
  636. BIO_printf(err, "Can't open BIO for stdin\n");
  637. return NULL;
  638. }
  639. } else {
  640. BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
  641. return NULL;
  642. }
  643. }
  644. i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
  645. if (keepbio != 1) {
  646. BIO_free_all(pwdbio);
  647. pwdbio = NULL;
  648. }
  649. if (i <= 0) {
  650. BIO_printf(err, "Error reading password from BIO\n");
  651. return NULL;
  652. }
  653. tmp = strchr(tpass, '\n');
  654. if (tmp)
  655. *tmp = 0;
  656. return BUF_strdup(tpass);
  657. }
  658. int add_oid_section(BIO *err, CONF *conf)
  659. {
  660. char *p;
  661. STACK_OF(CONF_VALUE) *sktmp;
  662. CONF_VALUE *cnf;
  663. int i;
  664. if (!(p = NCONF_get_string(conf, NULL, "oid_section"))) {
  665. ERR_clear_error();
  666. return 1;
  667. }
  668. if (!(sktmp = NCONF_get_section(conf, p))) {
  669. BIO_printf(err, "problem loading oid section %s\n", p);
  670. return 0;
  671. }
  672. for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  673. cnf = sk_CONF_VALUE_value(sktmp, i);
  674. if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  675. BIO_printf(err, "problem creating object %s=%s\n",
  676. cnf->name, cnf->value);
  677. return 0;
  678. }
  679. }
  680. return 1;
  681. }
  682. static int load_pkcs12(BIO *err, BIO *in, const char *desc,
  683. pem_password_cb *pem_cb, void *cb_data,
  684. EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
  685. {
  686. const char *pass;
  687. char tpass[PEM_BUFSIZE];
  688. int len, ret = 0;
  689. PKCS12 *p12;
  690. p12 = d2i_PKCS12_bio(in, NULL);
  691. if (p12 == NULL) {
  692. BIO_printf(err, "Error loading PKCS12 file for %s\n", desc);
  693. goto die;
  694. }
  695. /* See if an empty password will do */
  696. if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0))
  697. pass = "";
  698. else {
  699. if (!pem_cb)
  700. pem_cb = (pem_password_cb *)password_callback;
  701. len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data);
  702. if (len < 0) {
  703. BIO_printf(err, "Passpharse callback error for %s\n", desc);
  704. goto die;
  705. }
  706. if (len < PEM_BUFSIZE)
  707. tpass[len] = 0;
  708. if (!PKCS12_verify_mac(p12, tpass, len)) {
  709. BIO_printf(err,
  710. "Mac verify error (wrong password?) in PKCS12 file for %s\n",
  711. desc);
  712. goto die;
  713. }
  714. pass = tpass;
  715. }
  716. ret = PKCS12_parse(p12, pass, pkey, cert, ca);
  717. die:
  718. if (p12)
  719. PKCS12_free(p12);
  720. return ret;
  721. }
  722. X509 *load_cert(BIO *err, const char *file, int format,
  723. const char *pass, ENGINE *e, const char *cert_descrip)
  724. {
  725. X509 *x = NULL;
  726. BIO *cert;
  727. if ((cert = BIO_new(BIO_s_file())) == NULL) {
  728. ERR_print_errors(err);
  729. goto end;
  730. }
  731. if (file == NULL) {
  732. #ifdef _IONBF
  733. # ifndef OPENSSL_NO_SETVBUF_IONBF
  734. setvbuf(stdin, NULL, _IONBF, 0);
  735. # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
  736. #endif
  737. BIO_set_fp(cert, stdin, BIO_NOCLOSE);
  738. } else {
  739. if (BIO_read_filename(cert, file) <= 0) {
  740. BIO_printf(err, "Error opening %s %s\n", cert_descrip, file);
  741. ERR_print_errors(err);
  742. goto end;
  743. }
  744. }
  745. if (format == FORMAT_ASN1)
  746. x = d2i_X509_bio(cert, NULL);
  747. else if (format == FORMAT_NETSCAPE) {
  748. NETSCAPE_X509 *nx;
  749. nx = ASN1_item_d2i_bio(ASN1_ITEM_rptr(NETSCAPE_X509), cert, NULL);
  750. if (nx == NULL)
  751. goto end;
  752. if ((strncmp(NETSCAPE_CERT_HDR, (char *)nx->header->data,
  753. nx->header->length) != 0)) {
  754. NETSCAPE_X509_free(nx);
  755. BIO_printf(err, "Error reading header on certificate\n");
  756. goto end;
  757. }
  758. x = nx->cert;
  759. nx->cert = NULL;
  760. NETSCAPE_X509_free(nx);
  761. } else if (format == FORMAT_PEM)
  762. x = PEM_read_bio_X509_AUX(cert, NULL,
  763. (pem_password_cb *)password_callback, NULL);
  764. else if (format == FORMAT_PKCS12) {
  765. if (!load_pkcs12(err, cert, cert_descrip, NULL, NULL, NULL, &x, NULL))
  766. goto end;
  767. } else {
  768. BIO_printf(err, "bad input format specified for %s\n", cert_descrip);
  769. goto end;
  770. }
  771. end:
  772. if (x == NULL) {
  773. BIO_printf(err, "unable to load certificate\n");
  774. ERR_print_errors(err);
  775. }
  776. if (cert != NULL)
  777. BIO_free(cert);
  778. return (x);
  779. }
  780. EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
  781. const char *pass, ENGINE *e, const char *key_descrip)
  782. {
  783. BIO *key = NULL;
  784. EVP_PKEY *pkey = NULL;
  785. PW_CB_DATA cb_data;
  786. cb_data.password = pass;
  787. cb_data.prompt_info = file;
  788. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) {
  789. BIO_printf(err, "no keyfile specified\n");
  790. goto end;
  791. }
  792. #ifndef OPENSSL_NO_ENGINE
  793. if (format == FORMAT_ENGINE) {
  794. if (!e)
  795. BIO_printf(err, "no engine specified\n");
  796. else {
  797. pkey = ENGINE_load_private_key(e, file, ui_method, &cb_data);
  798. if (!pkey) {
  799. BIO_printf(err, "cannot load %s from engine\n", key_descrip);
  800. ERR_print_errors(err);
  801. }
  802. }
  803. goto end;
  804. }
  805. #endif
  806. key = BIO_new(BIO_s_file());
  807. if (key == NULL) {
  808. ERR_print_errors(err);
  809. goto end;
  810. }
  811. if (file == NULL && maybe_stdin) {
  812. #ifdef _IONBF
  813. # ifndef OPENSSL_NO_SETVBUF_IONBF
  814. setvbuf(stdin, NULL, _IONBF, 0);
  815. # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
  816. #endif
  817. BIO_set_fp(key, stdin, BIO_NOCLOSE);
  818. } else if (BIO_read_filename(key, file) <= 0) {
  819. BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
  820. ERR_print_errors(err);
  821. goto end;
  822. }
  823. if (format == FORMAT_ASN1) {
  824. pkey = d2i_PrivateKey_bio(key, NULL);
  825. } else if (format == FORMAT_PEM) {
  826. pkey = PEM_read_bio_PrivateKey(key, NULL,
  827. (pem_password_cb *)password_callback,
  828. &cb_data);
  829. }
  830. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  831. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  832. pkey = load_netscape_key(err, key, file, key_descrip, format);
  833. #endif
  834. else if (format == FORMAT_PKCS12) {
  835. if (!load_pkcs12(err, key, key_descrip,
  836. (pem_password_cb *)password_callback, &cb_data,
  837. &pkey, NULL, NULL))
  838. goto end;
  839. }
  840. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4)
  841. else if (format == FORMAT_MSBLOB)
  842. pkey = b2i_PrivateKey_bio(key);
  843. else if (format == FORMAT_PVK)
  844. pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback,
  845. &cb_data);
  846. #endif
  847. else {
  848. BIO_printf(err, "bad input format specified for key file\n");
  849. goto end;
  850. }
  851. end:
  852. if (key != NULL)
  853. BIO_free(key);
  854. if (pkey == NULL) {
  855. BIO_printf(err, "unable to load %s\n", key_descrip);
  856. ERR_print_errors(err);
  857. }
  858. return (pkey);
  859. }
  860. EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
  861. const char *pass, ENGINE *e, const char *key_descrip)
  862. {
  863. BIO *key = NULL;
  864. EVP_PKEY *pkey = NULL;
  865. PW_CB_DATA cb_data;
  866. cb_data.password = pass;
  867. cb_data.prompt_info = file;
  868. if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) {
  869. BIO_printf(err, "no keyfile specified\n");
  870. goto end;
  871. }
  872. #ifndef OPENSSL_NO_ENGINE
  873. if (format == FORMAT_ENGINE) {
  874. if (!e)
  875. BIO_printf(bio_err, "no engine specified\n");
  876. else
  877. pkey = ENGINE_load_public_key(e, file, ui_method, &cb_data);
  878. goto end;
  879. }
  880. #endif
  881. key = BIO_new(BIO_s_file());
  882. if (key == NULL) {
  883. ERR_print_errors(err);
  884. goto end;
  885. }
  886. if (file == NULL && maybe_stdin) {
  887. #ifdef _IONBF
  888. # ifndef OPENSSL_NO_SETVBUF_IONBF
  889. setvbuf(stdin, NULL, _IONBF, 0);
  890. # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
  891. #endif
  892. BIO_set_fp(key, stdin, BIO_NOCLOSE);
  893. } else if (BIO_read_filename(key, file) <= 0) {
  894. BIO_printf(err, "Error opening %s %s\n", key_descrip, file);
  895. ERR_print_errors(err);
  896. goto end;
  897. }
  898. if (format == FORMAT_ASN1) {
  899. pkey = d2i_PUBKEY_bio(key, NULL);
  900. }
  901. #ifndef OPENSSL_NO_RSA
  902. else if (format == FORMAT_ASN1RSA) {
  903. RSA *rsa;
  904. rsa = d2i_RSAPublicKey_bio(key, NULL);
  905. if (rsa) {
  906. pkey = EVP_PKEY_new();
  907. if (pkey)
  908. EVP_PKEY_set1_RSA(pkey, rsa);
  909. RSA_free(rsa);
  910. } else
  911. pkey = NULL;
  912. } else if (format == FORMAT_PEMRSA) {
  913. RSA *rsa;
  914. rsa = PEM_read_bio_RSAPublicKey(key, NULL,
  915. (pem_password_cb *)password_callback,
  916. &cb_data);
  917. if (rsa) {
  918. pkey = EVP_PKEY_new();
  919. if (pkey)
  920. EVP_PKEY_set1_RSA(pkey, rsa);
  921. RSA_free(rsa);
  922. } else
  923. pkey = NULL;
  924. }
  925. #endif
  926. else if (format == FORMAT_PEM) {
  927. pkey = PEM_read_bio_PUBKEY(key, NULL,
  928. (pem_password_cb *)password_callback,
  929. &cb_data);
  930. }
  931. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  932. else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
  933. pkey = load_netscape_key(err, key, file, key_descrip, format);
  934. #endif
  935. #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
  936. else if (format == FORMAT_MSBLOB)
  937. pkey = b2i_PublicKey_bio(key);
  938. #endif
  939. else {
  940. BIO_printf(err, "bad input format specified for key file\n");
  941. goto end;
  942. }
  943. end:
  944. if (key != NULL)
  945. BIO_free(key);
  946. if (pkey == NULL)
  947. BIO_printf(err, "unable to load %s\n", key_descrip);
  948. return (pkey);
  949. }
  950. #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
  951. static EVP_PKEY *load_netscape_key(BIO *err, BIO *key, const char *file,
  952. const char *key_descrip, int format)
  953. {
  954. EVP_PKEY *pkey;
  955. BUF_MEM *buf;
  956. RSA *rsa;
  957. const unsigned char *p;
  958. int size, i;
  959. buf = BUF_MEM_new();
  960. pkey = EVP_PKEY_new();
  961. size = 0;
  962. if (buf == NULL || pkey == NULL)
  963. goto error;
  964. for (;;) {
  965. if (!BUF_MEM_grow_clean(buf, size + 1024 * 10))
  966. goto error;
  967. i = BIO_read(key, &(buf->data[size]), 1024 * 10);
  968. size += i;
  969. if (i == 0)
  970. break;
  971. if (i < 0) {
  972. BIO_printf(err, "Error reading %s %s", key_descrip, file);
  973. goto error;
  974. }
  975. }
  976. p = (unsigned char *)buf->data;
  977. rsa = d2i_RSA_NET(NULL, &p, (long)size, NULL,
  978. (format == FORMAT_IISSGC ? 1 : 0));
  979. if (rsa == NULL)
  980. goto error;
  981. BUF_MEM_free(buf);
  982. EVP_PKEY_set1_RSA(pkey, rsa);
  983. return pkey;
  984. error:
  985. BUF_MEM_free(buf);
  986. EVP_PKEY_free(pkey);
  987. return NULL;
  988. }
  989. #endif /* ndef OPENSSL_NO_RC4 */
  990. static int load_certs_crls(BIO *err, const char *file, int format,
  991. const char *pass, ENGINE *e, const char *desc,
  992. STACK_OF(X509) **pcerts,
  993. STACK_OF(X509_CRL) **pcrls)
  994. {
  995. int i;
  996. BIO *bio;
  997. STACK_OF(X509_INFO) *xis = NULL;
  998. X509_INFO *xi;
  999. PW_CB_DATA cb_data;
  1000. int rv = 0;
  1001. cb_data.password = pass;
  1002. cb_data.prompt_info = file;
  1003. if (format != FORMAT_PEM) {
  1004. BIO_printf(err, "bad input format specified for %s\n", desc);
  1005. return 0;
  1006. }
  1007. if (file == NULL)
  1008. bio = BIO_new_fp(stdin, BIO_NOCLOSE);
  1009. else
  1010. bio = BIO_new_file(file, "r");
  1011. if (bio == NULL) {
  1012. BIO_printf(err, "Error opening %s %s\n", desc, file ? file : "stdin");
  1013. ERR_print_errors(err);
  1014. return 0;
  1015. }
  1016. xis = PEM_X509_INFO_read_bio(bio, NULL,
  1017. (pem_password_cb *)password_callback,
  1018. &cb_data);
  1019. BIO_free(bio);
  1020. if (pcerts) {
  1021. *pcerts = sk_X509_new_null();
  1022. if (!*pcerts)
  1023. goto end;
  1024. }
  1025. if (pcrls) {
  1026. *pcrls = sk_X509_CRL_new_null();
  1027. if (!*pcrls)
  1028. goto end;
  1029. }
  1030. for (i = 0; i < sk_X509_INFO_num(xis); i++) {
  1031. xi = sk_X509_INFO_value(xis, i);
  1032. if (xi->x509 && pcerts) {
  1033. if (!sk_X509_push(*pcerts, xi->x509))
  1034. goto end;
  1035. xi->x509 = NULL;
  1036. }
  1037. if (xi->crl && pcrls) {
  1038. if (!sk_X509_CRL_push(*pcrls, xi->crl))
  1039. goto end;
  1040. xi->crl = NULL;
  1041. }
  1042. }
  1043. if (pcerts && sk_X509_num(*pcerts) > 0)
  1044. rv = 1;
  1045. if (pcrls && sk_X509_CRL_num(*pcrls) > 0)
  1046. rv = 1;
  1047. end:
  1048. if (xis)
  1049. sk_X509_INFO_pop_free(xis, X509_INFO_free);
  1050. if (rv == 0) {
  1051. if (pcerts) {
  1052. sk_X509_pop_free(*pcerts, X509_free);
  1053. *pcerts = NULL;
  1054. }
  1055. if (pcrls) {
  1056. sk_X509_CRL_pop_free(*pcrls, X509_CRL_free);
  1057. *pcrls = NULL;
  1058. }
  1059. BIO_printf(err, "unable to load %s\n",
  1060. pcerts ? "certificates" : "CRLs");
  1061. ERR_print_errors(err);
  1062. }
  1063. return rv;
  1064. }
  1065. STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
  1066. const char *pass, ENGINE *e, const char *desc)
  1067. {
  1068. STACK_OF(X509) *certs;
  1069. if (!load_certs_crls(err, file, format, pass, e, desc, &certs, NULL))
  1070. return NULL;
  1071. return certs;
  1072. }
  1073. STACK_OF(X509_CRL) *load_crls(BIO *err, const char *file, int format,
  1074. const char *pass, ENGINE *e, const char *desc)
  1075. {
  1076. STACK_OF(X509_CRL) *crls;
  1077. if (!load_certs_crls(err, file, format, pass, e, desc, NULL, &crls))
  1078. return NULL;
  1079. return crls;
  1080. }
  1081. #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
  1082. /* Return error for unknown extensions */
  1083. #define X509V3_EXT_DEFAULT 0
  1084. /* Print error for unknown extensions */
  1085. #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
  1086. /* ASN1 parse unknown extensions */
  1087. #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
  1088. /* BIO_dump unknown extensions */
  1089. #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
  1090. #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
  1091. X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
  1092. int set_cert_ex(unsigned long *flags, const char *arg)
  1093. {
  1094. static const NAME_EX_TBL cert_tbl[] = {
  1095. {"compatible", X509_FLAG_COMPAT, 0xffffffffl},
  1096. {"ca_default", X509_FLAG_CA, 0xffffffffl},
  1097. {"no_header", X509_FLAG_NO_HEADER, 0},
  1098. {"no_version", X509_FLAG_NO_VERSION, 0},
  1099. {"no_serial", X509_FLAG_NO_SERIAL, 0},
  1100. {"no_signame", X509_FLAG_NO_SIGNAME, 0},
  1101. {"no_validity", X509_FLAG_NO_VALIDITY, 0},
  1102. {"no_subject", X509_FLAG_NO_SUBJECT, 0},
  1103. {"no_issuer", X509_FLAG_NO_ISSUER, 0},
  1104. {"no_pubkey", X509_FLAG_NO_PUBKEY, 0},
  1105. {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
  1106. {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
  1107. {"no_aux", X509_FLAG_NO_AUX, 0},
  1108. {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
  1109. {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
  1110. {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1111. {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1112. {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
  1113. {NULL, 0, 0}
  1114. };
  1115. return set_multi_opts(flags, arg, cert_tbl);
  1116. }
  1117. int set_name_ex(unsigned long *flags, const char *arg)
  1118. {
  1119. static const NAME_EX_TBL ex_tbl[] = {
  1120. {"esc_2253", ASN1_STRFLGS_ESC_2253, 0},
  1121. {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
  1122. {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
  1123. {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
  1124. {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
  1125. {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
  1126. {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
  1127. {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
  1128. {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
  1129. {"dump_der", ASN1_STRFLGS_DUMP_DER, 0},
  1130. {"compat", XN_FLAG_COMPAT, 0xffffffffL},
  1131. {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
  1132. {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
  1133. {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
  1134. {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
  1135. {"dn_rev", XN_FLAG_DN_REV, 0},
  1136. {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
  1137. {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
  1138. {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
  1139. {"align", XN_FLAG_FN_ALIGN, 0},
  1140. {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
  1141. {"space_eq", XN_FLAG_SPC_EQ, 0},
  1142. {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
  1143. {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
  1144. {"oneline", XN_FLAG_ONELINE, 0xffffffffL},
  1145. {"multiline", XN_FLAG_MULTILINE, 0xffffffffL},
  1146. {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
  1147. {NULL, 0, 0}
  1148. };
  1149. return set_multi_opts(flags, arg, ex_tbl);
  1150. }
  1151. int set_ext_copy(int *copy_type, const char *arg)
  1152. {
  1153. if (!strcasecmp(arg, "none"))
  1154. *copy_type = EXT_COPY_NONE;
  1155. else if (!strcasecmp(arg, "copy"))
  1156. *copy_type = EXT_COPY_ADD;
  1157. else if (!strcasecmp(arg, "copyall"))
  1158. *copy_type = EXT_COPY_ALL;
  1159. else
  1160. return 0;
  1161. return 1;
  1162. }
  1163. int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
  1164. {
  1165. STACK_OF(X509_EXTENSION) *exts = NULL;
  1166. X509_EXTENSION *ext, *tmpext;
  1167. ASN1_OBJECT *obj;
  1168. int i, idx, ret = 0;
  1169. if (!x || !req || (copy_type == EXT_COPY_NONE))
  1170. return 1;
  1171. exts = X509_REQ_get_extensions(req);
  1172. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  1173. ext = sk_X509_EXTENSION_value(exts, i);
  1174. obj = X509_EXTENSION_get_object(ext);
  1175. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1176. /* Does extension exist? */
  1177. if (idx != -1) {
  1178. /* If normal copy don't override existing extension */
  1179. if (copy_type == EXT_COPY_ADD)
  1180. continue;
  1181. /* Delete all extensions of same type */
  1182. do {
  1183. tmpext = X509_get_ext(x, idx);
  1184. X509_delete_ext(x, idx);
  1185. X509_EXTENSION_free(tmpext);
  1186. idx = X509_get_ext_by_OBJ(x, obj, -1);
  1187. } while (idx != -1);
  1188. }
  1189. if (!X509_add_ext(x, ext, -1))
  1190. goto end;
  1191. }
  1192. ret = 1;
  1193. end:
  1194. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  1195. return ret;
  1196. }
  1197. static int set_multi_opts(unsigned long *flags, const char *arg,
  1198. const NAME_EX_TBL * in_tbl)
  1199. {
  1200. STACK_OF(CONF_VALUE) *vals;
  1201. CONF_VALUE *val;
  1202. int i, ret = 1;
  1203. if (!arg)
  1204. return 0;
  1205. vals = X509V3_parse_list(arg);
  1206. for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
  1207. val = sk_CONF_VALUE_value(vals, i);
  1208. if (!set_table_opts(flags, val->name, in_tbl))
  1209. ret = 0;
  1210. }
  1211. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  1212. return ret;
  1213. }
  1214. static int set_table_opts(unsigned long *flags, const char *arg,
  1215. const NAME_EX_TBL * in_tbl)
  1216. {
  1217. char c;
  1218. const NAME_EX_TBL *ptbl;
  1219. c = arg[0];
  1220. if (c == '-') {
  1221. c = 0;
  1222. arg++;
  1223. } else if (c == '+') {
  1224. c = 1;
  1225. arg++;
  1226. } else
  1227. c = 1;
  1228. for (ptbl = in_tbl; ptbl->name; ptbl++) {
  1229. if (!strcasecmp(arg, ptbl->name)) {
  1230. *flags &= ~ptbl->mask;
  1231. if (c)
  1232. *flags |= ptbl->flag;
  1233. else
  1234. *flags &= ~ptbl->flag;
  1235. return 1;
  1236. }
  1237. }
  1238. return 0;
  1239. }
  1240. void print_name(BIO *out, const char *title, X509_NAME *nm,
  1241. unsigned long lflags)
  1242. {
  1243. char *buf;
  1244. char mline = 0;
  1245. int indent = 0;
  1246. if (title)
  1247. BIO_puts(out, title);
  1248. if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  1249. mline = 1;
  1250. indent = 4;
  1251. }
  1252. if (lflags == XN_FLAG_COMPAT) {
  1253. buf = X509_NAME_oneline(nm, 0, 0);
  1254. BIO_puts(out, buf);
  1255. BIO_puts(out, "\n");
  1256. OPENSSL_free(buf);
  1257. } else {
  1258. if (mline)
  1259. BIO_puts(out, "\n");
  1260. X509_NAME_print_ex(out, nm, indent, lflags);
  1261. BIO_puts(out, "\n");
  1262. }
  1263. }
  1264. X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
  1265. {
  1266. X509_STORE *store;
  1267. X509_LOOKUP *lookup;
  1268. if (!(store = X509_STORE_new()))
  1269. goto end;
  1270. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  1271. if (lookup == NULL)
  1272. goto end;
  1273. if (CAfile) {
  1274. if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) {
  1275. BIO_printf(bp, "Error loading file %s\n", CAfile);
  1276. goto end;
  1277. }
  1278. } else
  1279. X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
  1280. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  1281. if (lookup == NULL)
  1282. goto end;
  1283. if (CApath) {
  1284. if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) {
  1285. BIO_printf(bp, "Error loading directory %s\n", CApath);
  1286. goto end;
  1287. }
  1288. } else
  1289. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  1290. ERR_clear_error();
  1291. return store;
  1292. end:
  1293. X509_STORE_free(store);
  1294. return NULL;
  1295. }
  1296. #ifndef OPENSSL_NO_ENGINE
  1297. /* Try to load an engine in a shareable library */
  1298. static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
  1299. {
  1300. ENGINE *e = ENGINE_by_id("dynamic");
  1301. if (e) {
  1302. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
  1303. || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) {
  1304. ENGINE_free(e);
  1305. e = NULL;
  1306. }
  1307. }
  1308. return e;
  1309. }
  1310. ENGINE *setup_engine(BIO *err, const char *engine, int debug)
  1311. {
  1312. ENGINE *e = NULL;
  1313. if (engine) {
  1314. if (strcmp(engine, "auto") == 0) {
  1315. BIO_printf(err, "enabling auto ENGINE support\n");
  1316. ENGINE_register_all_complete();
  1317. return NULL;
  1318. }
  1319. if ((e = ENGINE_by_id(engine)) == NULL
  1320. && (e = try_load_engine(err, engine, debug)) == NULL) {
  1321. BIO_printf(err, "invalid engine \"%s\"\n", engine);
  1322. ERR_print_errors(err);
  1323. return NULL;
  1324. }
  1325. if (debug) {
  1326. ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, err, 0);
  1327. }
  1328. ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
  1329. if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
  1330. BIO_printf(err, "can't use that engine\n");
  1331. ERR_print_errors(err);
  1332. ENGINE_free(e);
  1333. return NULL;
  1334. }
  1335. BIO_printf(err, "engine \"%s\" set.\n", ENGINE_get_id(e));
  1336. /* Free our "structural" reference. */
  1337. ENGINE_free(e);
  1338. }
  1339. return e;
  1340. }
  1341. #endif
  1342. int load_config(BIO *err, CONF *cnf)
  1343. {
  1344. static int load_config_called = 0;
  1345. if (load_config_called)
  1346. return 1;
  1347. load_config_called = 1;
  1348. if (!cnf)
  1349. cnf = config;
  1350. if (!cnf)
  1351. return 1;
  1352. OPENSSL_load_builtin_modules();
  1353. if (CONF_modules_load(cnf, NULL, 0) <= 0) {
  1354. BIO_printf(err, "Error configuring OpenSSL\n");
  1355. ERR_print_errors(err);
  1356. return 0;
  1357. }
  1358. return 1;
  1359. }
  1360. char *make_config_name()
  1361. {
  1362. const char *t = X509_get_default_cert_area();
  1363. size_t len;
  1364. char *p;
  1365. len = strlen(t) + strlen(OPENSSL_CONF) + 2;
  1366. p = OPENSSL_malloc(len);
  1367. if (p == NULL)
  1368. return NULL;
  1369. BUF_strlcpy(p, t, len);
  1370. #ifndef OPENSSL_SYS_VMS
  1371. BUF_strlcat(p, "/", len);
  1372. #endif
  1373. BUF_strlcat(p, OPENSSL_CONF, len);
  1374. return p;
  1375. }
  1376. static unsigned long index_serial_hash(const OPENSSL_CSTRING *a)
  1377. {
  1378. const char *n;
  1379. n = a[DB_serial];
  1380. while (*n == '0')
  1381. n++;
  1382. return (lh_strhash(n));
  1383. }
  1384. static int index_serial_cmp(const OPENSSL_CSTRING *a,
  1385. const OPENSSL_CSTRING *b)
  1386. {
  1387. const char *aa, *bb;
  1388. for (aa = a[DB_serial]; *aa == '0'; aa++) ;
  1389. for (bb = b[DB_serial]; *bb == '0'; bb++) ;
  1390. return (strcmp(aa, bb));
  1391. }
  1392. static int index_name_qual(char **a)
  1393. {
  1394. return (a[0][0] == 'V');
  1395. }
  1396. static unsigned long index_name_hash(const OPENSSL_CSTRING *a)
  1397. {
  1398. return (lh_strhash(a[DB_name]));
  1399. }
  1400. int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b)
  1401. {
  1402. return (strcmp(a[DB_name], b[DB_name]));
  1403. }
  1404. static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING)
  1405. static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING)
  1406. static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING)
  1407. static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING)
  1408. #undef BSIZE
  1409. #define BSIZE 256
  1410. BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
  1411. {
  1412. BIO *in = NULL;
  1413. BIGNUM *ret = NULL;
  1414. MS_STATIC char buf[1024];
  1415. ASN1_INTEGER *ai = NULL;
  1416. ai = ASN1_INTEGER_new();
  1417. if (ai == NULL)
  1418. goto err;
  1419. if ((in = BIO_new(BIO_s_file())) == NULL) {
  1420. ERR_print_errors(bio_err);
  1421. goto err;
  1422. }
  1423. if (BIO_read_filename(in, serialfile) <= 0) {
  1424. if (!create) {
  1425. perror(serialfile);
  1426. goto err;
  1427. } else {
  1428. ret = BN_new();
  1429. if (ret == NULL || !rand_serial(ret, ai))
  1430. BIO_printf(bio_err, "Out of memory\n");
  1431. }
  1432. } else {
  1433. if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) {
  1434. BIO_printf(bio_err, "unable to load number from %s\n",
  1435. serialfile);
  1436. goto err;
  1437. }
  1438. ret = ASN1_INTEGER_to_BN(ai, NULL);
  1439. if (ret == NULL) {
  1440. BIO_printf(bio_err,
  1441. "error converting number from bin to BIGNUM\n");
  1442. goto err;
  1443. }
  1444. }
  1445. if (ret && retai) {
  1446. *retai = ai;
  1447. ai = NULL;
  1448. }
  1449. err:
  1450. if (in != NULL)
  1451. BIO_free(in);
  1452. if (ai != NULL)
  1453. ASN1_INTEGER_free(ai);
  1454. return (ret);
  1455. }
  1456. int save_serial(char *serialfile, char *suffix, BIGNUM *serial,
  1457. ASN1_INTEGER **retai)
  1458. {
  1459. char buf[1][BSIZE];
  1460. BIO *out = NULL;
  1461. int ret = 0;
  1462. ASN1_INTEGER *ai = NULL;
  1463. int j;
  1464. if (suffix == NULL)
  1465. j = strlen(serialfile);
  1466. else
  1467. j = strlen(serialfile) + strlen(suffix) + 1;
  1468. if (j >= BSIZE) {
  1469. BIO_printf(bio_err, "file name too long\n");
  1470. goto err;
  1471. }
  1472. if (suffix == NULL)
  1473. BUF_strlcpy(buf[0], serialfile, BSIZE);
  1474. else {
  1475. #ifndef OPENSSL_SYS_VMS
  1476. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
  1477. #else
  1478. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
  1479. #endif
  1480. }
  1481. #ifdef RL_DEBUG
  1482. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1483. #endif
  1484. out = BIO_new(BIO_s_file());
  1485. if (out == NULL) {
  1486. ERR_print_errors(bio_err);
  1487. goto err;
  1488. }
  1489. if (BIO_write_filename(out, buf[0]) <= 0) {
  1490. perror(serialfile);
  1491. goto err;
  1492. }
  1493. if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) {
  1494. BIO_printf(bio_err, "error converting serial to ASN.1 format\n");
  1495. goto err;
  1496. }
  1497. i2a_ASN1_INTEGER(out, ai);
  1498. BIO_puts(out, "\n");
  1499. ret = 1;
  1500. if (retai) {
  1501. *retai = ai;
  1502. ai = NULL;
  1503. }
  1504. err:
  1505. if (out != NULL)
  1506. BIO_free_all(out);
  1507. if (ai != NULL)
  1508. ASN1_INTEGER_free(ai);
  1509. return (ret);
  1510. }
  1511. int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
  1512. {
  1513. char buf[5][BSIZE];
  1514. int i, j;
  1515. i = strlen(serialfile) + strlen(old_suffix);
  1516. j = strlen(serialfile) + strlen(new_suffix);
  1517. if (i > j)
  1518. j = i;
  1519. if (j + 1 >= BSIZE) {
  1520. BIO_printf(bio_err, "file name too long\n");
  1521. goto err;
  1522. }
  1523. #ifndef OPENSSL_SYS_VMS
  1524. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, new_suffix);
  1525. #else
  1526. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, new_suffix);
  1527. #endif
  1528. #ifndef OPENSSL_SYS_VMS
  1529. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", serialfile, old_suffix);
  1530. #else
  1531. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", serialfile, old_suffix);
  1532. #endif
  1533. #ifdef RL_DEBUG
  1534. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1535. serialfile, buf[1]);
  1536. #endif
  1537. if (rename(serialfile, buf[1]) < 0 && errno != ENOENT
  1538. #ifdef ENOTDIR
  1539. && errno != ENOTDIR
  1540. #endif
  1541. ) {
  1542. BIO_printf(bio_err,
  1543. "unable to rename %s to %s\n", serialfile, buf[1]);
  1544. perror("reason");
  1545. goto err;
  1546. }
  1547. #ifdef RL_DEBUG
  1548. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
  1549. buf[0], serialfile);
  1550. #endif
  1551. if (rename(buf[0], serialfile) < 0) {
  1552. BIO_printf(bio_err,
  1553. "unable to rename %s to %s\n", buf[0], serialfile);
  1554. perror("reason");
  1555. rename(buf[1], serialfile);
  1556. goto err;
  1557. }
  1558. return 1;
  1559. err:
  1560. return 0;
  1561. }
  1562. int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
  1563. {
  1564. BIGNUM *btmp;
  1565. int ret = 0;
  1566. if (b)
  1567. btmp = b;
  1568. else
  1569. btmp = BN_new();
  1570. if (!btmp)
  1571. return 0;
  1572. if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
  1573. goto error;
  1574. if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
  1575. goto error;
  1576. ret = 1;
  1577. error:
  1578. if (!b)
  1579. BN_free(btmp);
  1580. return ret;
  1581. }
  1582. CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
  1583. {
  1584. CA_DB *retdb = NULL;
  1585. TXT_DB *tmpdb = NULL;
  1586. BIO *in = BIO_new(BIO_s_file());
  1587. CONF *dbattr_conf = NULL;
  1588. char buf[1][BSIZE];
  1589. long errorline = -1;
  1590. if (in == NULL) {
  1591. ERR_print_errors(bio_err);
  1592. goto err;
  1593. }
  1594. if (BIO_read_filename(in, dbfile) <= 0) {
  1595. perror(dbfile);
  1596. BIO_printf(bio_err, "unable to open '%s'\n", dbfile);
  1597. goto err;
  1598. }
  1599. if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
  1600. goto err;
  1601. #ifndef OPENSSL_SYS_VMS
  1602. BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
  1603. #else
  1604. BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
  1605. #endif
  1606. dbattr_conf = NCONF_new(NULL);
  1607. if (NCONF_load(dbattr_conf, buf[0], &errorline) <= 0) {
  1608. if (errorline > 0) {
  1609. BIO_printf(bio_err,
  1610. "error on line %ld of db attribute file '%s'\n",
  1611. errorline, buf[0]);
  1612. goto err;
  1613. } else {
  1614. NCONF_free(dbattr_conf);
  1615. dbattr_conf = NULL;
  1616. }
  1617. }
  1618. if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL) {
  1619. fprintf(stderr, "Out of memory\n");
  1620. goto err;
  1621. }
  1622. retdb->db = tmpdb;
  1623. tmpdb = NULL;
  1624. if (db_attr)
  1625. retdb->attributes = *db_attr;
  1626. else {
  1627. retdb->attributes.unique_subject = 1;
  1628. }
  1629. if (dbattr_conf) {
  1630. char *p = NCONF_get_string(dbattr_conf, NULL, "unique_subject");
  1631. if (p) {
  1632. #ifdef RL_DEBUG
  1633. BIO_printf(bio_err,
  1634. "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
  1635. #endif
  1636. retdb->attributes.unique_subject = parse_yesno(p, 1);
  1637. }
  1638. }
  1639. err:
  1640. if (dbattr_conf)
  1641. NCONF_free(dbattr_conf);
  1642. if (tmpdb)
  1643. TXT_DB_free(tmpdb);
  1644. if (in)
  1645. BIO_free_all(in);
  1646. return retdb;
  1647. }
  1648. int index_index(CA_DB *db)
  1649. {
  1650. if (!TXT_DB_create_index(db->db, DB_serial, NULL,
  1651. LHASH_HASH_FN(index_serial),
  1652. LHASH_COMP_FN(index_serial))) {
  1653. BIO_printf(bio_err,
  1654. "error creating serial number index:(%ld,%ld,%ld)\n",
  1655. db->db->error, db->db->arg1, db->db->arg2);
  1656. return 0;
  1657. }
  1658. if (db->attributes.unique_subject
  1659. && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
  1660. LHASH_HASH_FN(index_name),
  1661. LHASH_COMP_FN(index_name))) {
  1662. BIO_printf(bio_err, "error creating name index:(%ld,%ld,%ld)\n",
  1663. db->db->error, db->db->arg1, db->db->arg2);
  1664. return 0;
  1665. }
  1666. return 1;
  1667. }
  1668. int save_index(const char *dbfile, const char *suffix, CA_DB *db)
  1669. {
  1670. char buf[3][BSIZE];
  1671. BIO *out = BIO_new(BIO_s_file());
  1672. int j;
  1673. if (out == NULL) {
  1674. ERR_print_errors(bio_err);
  1675. goto err;
  1676. }
  1677. j = strlen(dbfile) + strlen(suffix);
  1678. if (j + 6 >= BSIZE) {
  1679. BIO_printf(bio_err, "file name too long\n");
  1680. goto err;
  1681. }
  1682. #ifndef OPENSSL_SYS_VMS
  1683. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
  1684. #else
  1685. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
  1686. #endif
  1687. #ifndef OPENSSL_SYS_VMS
  1688. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
  1689. #else
  1690. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
  1691. #endif
  1692. #ifndef OPENSSL_SYS_VMS
  1693. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
  1694. #else
  1695. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
  1696. #endif
  1697. #ifdef RL_DEBUG
  1698. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
  1699. #endif
  1700. if (BIO_write_filename(out, buf[0]) <= 0) {
  1701. perror(dbfile);
  1702. BIO_printf(bio_err, "unable to open '%s'\n", dbfile);
  1703. goto err;
  1704. }
  1705. j = TXT_DB_write(out, db->db);
  1706. if (j <= 0)
  1707. goto err;
  1708. BIO_free(out);
  1709. out = BIO_new(BIO_s_file());
  1710. #ifdef RL_DEBUG
  1711. BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
  1712. #endif
  1713. if (BIO_write_filename(out, buf[1]) <= 0) {
  1714. perror(buf[2]);
  1715. BIO_printf(bio_err, "unable to open '%s'\n", buf[2]);
  1716. goto err;
  1717. }
  1718. BIO_printf(out, "unique_subject = %s\n",
  1719. db->attributes.unique_subject ? "yes" : "no");
  1720. BIO_free(out);
  1721. return 1;
  1722. err:
  1723. return 0;
  1724. }
  1725. int rotate_index(const char *dbfile, const char *new_suffix,
  1726. const char *old_suffix)
  1727. {
  1728. char buf[5][BSIZE];
  1729. int i, j;
  1730. i = strlen(dbfile) + strlen(old_suffix);
  1731. j = strlen(dbfile) + strlen(new_suffix);
  1732. if (i > j)
  1733. j = i;
  1734. if (j + 6 >= BSIZE) {
  1735. BIO_printf(bio_err, "file name too long\n");
  1736. goto err;
  1737. }
  1738. #ifndef OPENSSL_SYS_VMS
  1739. j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
  1740. #else
  1741. j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
  1742. #endif
  1743. #ifndef OPENSSL_SYS_VMS
  1744. j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s", dbfile, new_suffix);
  1745. #else
  1746. j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s", dbfile, new_suffix);
  1747. #endif
  1748. #ifndef OPENSSL_SYS_VMS
  1749. j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, new_suffix);
  1750. #else
  1751. j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, new_suffix);
  1752. #endif
  1753. #ifndef OPENSSL_SYS_VMS
  1754. j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", dbfile, old_suffix);
  1755. #else
  1756. j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", dbfile, old_suffix);
  1757. #endif
  1758. #ifndef OPENSSL_SYS_VMS
  1759. j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s", dbfile, old_suffix);
  1760. #else
  1761. j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s", dbfile, old_suffix);
  1762. #endif
  1763. #ifdef RL_DEBUG
  1764. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", dbfile, buf[1]);
  1765. #endif
  1766. if (rename(dbfile, buf[1]) < 0 && errno != ENOENT
  1767. #ifdef ENOTDIR
  1768. && errno != ENOTDIR
  1769. #endif
  1770. ) {
  1771. BIO_printf(bio_err, "unable to rename %s to %s\n", dbfile, buf[1]);
  1772. perror("reason");
  1773. goto err;
  1774. }
  1775. #ifdef RL_DEBUG
  1776. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[0], dbfile);
  1777. #endif
  1778. if (rename(buf[0], dbfile) < 0) {
  1779. BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], dbfile);
  1780. perror("reason");
  1781. rename(buf[1], dbfile);
  1782. goto err;
  1783. }
  1784. #ifdef RL_DEBUG
  1785. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[4], buf[3]);
  1786. #endif
  1787. if (rename(buf[4], buf[3]) < 0 && errno != ENOENT
  1788. #ifdef ENOTDIR
  1789. && errno != ENOTDIR
  1790. #endif
  1791. ) {
  1792. BIO_printf(bio_err, "unable to rename %s to %s\n", buf[4], buf[3]);
  1793. perror("reason");
  1794. rename(dbfile, buf[0]);
  1795. rename(buf[1], dbfile);
  1796. goto err;
  1797. }
  1798. #ifdef RL_DEBUG
  1799. BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[2], buf[4]);
  1800. #endif
  1801. if (rename(buf[2], buf[4]) < 0) {
  1802. BIO_printf(bio_err, "unable to rename %s to %s\n", buf[2], buf[4]);
  1803. perror("reason");
  1804. rename(buf[3], buf[4]);
  1805. rename(dbfile, buf[0]);
  1806. rename(buf[1], dbfile);
  1807. goto err;
  1808. }
  1809. return 1;
  1810. err:
  1811. return 0;
  1812. }
  1813. void free_index(CA_DB *db)
  1814. {
  1815. if (db) {
  1816. if (db->db)
  1817. TXT_DB_free(db->db);
  1818. OPENSSL_free(db);
  1819. }
  1820. }
  1821. int parse_yesno(const char *str, int def)
  1822. {
  1823. int ret = def;
  1824. if (str) {
  1825. switch (*str) {
  1826. case 'f': /* false */
  1827. case 'F': /* FALSE */
  1828. case 'n': /* no */
  1829. case 'N': /* NO */
  1830. case '0': /* 0 */
  1831. ret = 0;
  1832. break;
  1833. case 't': /* true */
  1834. case 'T': /* TRUE */
  1835. case 'y': /* yes */
  1836. case 'Y': /* YES */
  1837. case '1': /* 1 */
  1838. ret = 1;
  1839. break;
  1840. default:
  1841. ret = def;
  1842. break;
  1843. }
  1844. }
  1845. return ret;
  1846. }
  1847. /*
  1848. * subject is expected to be in the format /type0=value0/type1=value1/type2=...
  1849. * where characters may be escaped by \
  1850. */
  1851. X509_NAME *parse_name(char *subject, long chtype, int multirdn)
  1852. {
  1853. size_t buflen = strlen(subject) + 1; /* to copy the types and values
  1854. * into. due to escaping, the copy
  1855. * can only become shorter */
  1856. char *buf = OPENSSL_malloc(buflen);
  1857. size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
  1858. char **ne_types = OPENSSL_malloc(max_ne * sizeof(char *));
  1859. char **ne_values = OPENSSL_malloc(max_ne * sizeof(char *));
  1860. int *mval = OPENSSL_malloc(max_ne * sizeof(int));
  1861. char *sp = subject, *bp = buf;
  1862. int i, ne_num = 0;
  1863. X509_NAME *n = NULL;
  1864. int nid;
  1865. if (!buf || !ne_types || !ne_values || !mval) {
  1866. BIO_printf(bio_err, "malloc error\n");
  1867. goto error;
  1868. }
  1869. if (*subject != '/') {
  1870. BIO_printf(bio_err, "Subject does not start with '/'.\n");
  1871. goto error;
  1872. }
  1873. sp++; /* skip leading / */
  1874. /* no multivalued RDN by default */
  1875. mval[ne_num] = 0;
  1876. while (*sp) {
  1877. /* collect type */
  1878. ne_types[ne_num] = bp;
  1879. while (*sp) {
  1880. if (*sp == '\\') { /* is there anything to escape in the
  1881. * type...? */
  1882. if (*++sp)
  1883. *bp++ = *sp++;
  1884. else {
  1885. BIO_printf(bio_err,
  1886. "escape character at end of string\n");
  1887. goto error;
  1888. }
  1889. } else if (*sp == '=') {
  1890. sp++;
  1891. *bp++ = '\0';
  1892. break;
  1893. } else
  1894. *bp++ = *sp++;
  1895. }
  1896. if (!*sp) {
  1897. BIO_printf(bio_err,
  1898. "end of string encountered while processing type of subject name element #%d\n",
  1899. ne_num);
  1900. goto error;
  1901. }
  1902. ne_values[ne_num] = bp;
  1903. while (*sp) {
  1904. if (*sp == '\\') {
  1905. if (*++sp)
  1906. *bp++ = *sp++;
  1907. else {
  1908. BIO_printf(bio_err,
  1909. "escape character at end of string\n");
  1910. goto error;
  1911. }
  1912. } else if (*sp == '/') {
  1913. sp++;
  1914. /* no multivalued RDN by default */
  1915. mval[ne_num + 1] = 0;
  1916. break;
  1917. } else if (*sp == '+' && multirdn) {
  1918. /*
  1919. * a not escaped + signals a mutlivalued RDN
  1920. */
  1921. sp++;
  1922. mval[ne_num + 1] = -1;
  1923. break;
  1924. } else
  1925. *bp++ = *sp++;
  1926. }
  1927. *bp++ = '\0';
  1928. ne_num++;
  1929. }
  1930. if (!(n = X509_NAME_new()))
  1931. goto error;
  1932. for (i = 0; i < ne_num; i++) {
  1933. if ((nid = OBJ_txt2nid(ne_types[i])) == NID_undef) {
  1934. BIO_printf(bio_err,
  1935. "Subject Attribute %s has no known NID, skipped\n",
  1936. ne_types[i]);
  1937. continue;
  1938. }
  1939. if (!*ne_values[i]) {
  1940. BIO_printf(bio_err,
  1941. "No value provided for Subject Attribute %s, skipped\n",
  1942. ne_types[i]);
  1943. continue;
  1944. }
  1945. if (!X509_NAME_add_entry_by_NID
  1946. (n, nid, chtype, (unsigned char *)ne_values[i], -1, -1, mval[i]))
  1947. goto error;
  1948. }
  1949. OPENSSL_free(ne_values);
  1950. OPENSSL_free(ne_types);
  1951. OPENSSL_free(buf);
  1952. OPENSSL_free(mval);
  1953. return n;
  1954. error:
  1955. X509_NAME_free(n);
  1956. if (ne_values)
  1957. OPENSSL_free(ne_values);
  1958. if (ne_types)
  1959. OPENSSL_free(ne_types);
  1960. if (mval)
  1961. OPENSSL_free(mval);
  1962. if (buf)
  1963. OPENSSL_free(buf);
  1964. return NULL;
  1965. }
  1966. int args_verify(char ***pargs, int *pargc,
  1967. int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
  1968. {
  1969. ASN1_OBJECT *otmp = NULL;
  1970. unsigned long flags = 0;
  1971. int i;
  1972. int purpose = 0, depth = -1;
  1973. char **oldargs = *pargs;
  1974. char *arg = **pargs, *argn = (*pargs)[1];
  1975. if (!strcmp(arg, "-policy")) {
  1976. if (!argn)
  1977. *badarg = 1;
  1978. else {
  1979. otmp = OBJ_txt2obj(argn, 0);
  1980. if (!otmp) {
  1981. BIO_printf(err, "Invalid Policy \"%s\"\n", argn);
  1982. *badarg = 1;
  1983. }
  1984. }
  1985. (*pargs)++;
  1986. } else if (strcmp(arg, "-purpose") == 0) {
  1987. X509_PURPOSE *xptmp;
  1988. if (!argn)
  1989. *badarg = 1;
  1990. else {
  1991. i = X509_PURPOSE_get_by_sname(argn);
  1992. if (i < 0) {
  1993. BIO_printf(err, "unrecognized purpose\n");
  1994. *badarg = 1;
  1995. } else {
  1996. xptmp = X509_PURPOSE_get0(i);
  1997. purpose = X509_PURPOSE_get_id(xptmp);
  1998. }
  1999. }
  2000. (*pargs)++;
  2001. } else if (strcmp(arg, "-verify_depth") == 0) {
  2002. if (!argn)
  2003. *badarg = 1;
  2004. else {
  2005. depth = atoi(argn);
  2006. if (depth < 0) {
  2007. BIO_printf(err, "invalid depth\n");
  2008. *badarg = 1;
  2009. }
  2010. }
  2011. (*pargs)++;
  2012. } else if (!strcmp(arg, "-ignore_critical"))
  2013. flags |= X509_V_FLAG_IGNORE_CRITICAL;
  2014. else if (!strcmp(arg, "-issuer_checks"))
  2015. flags |= X509_V_FLAG_CB_ISSUER_CHECK;
  2016. else if (!strcmp(arg, "-crl_check"))
  2017. flags |= X509_V_FLAG_CRL_CHECK;
  2018. else if (!strcmp(arg, "-crl_check_all"))
  2019. flags |= X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL;
  2020. else if (!strcmp(arg, "-policy_check"))
  2021. flags |= X509_V_FLAG_POLICY_CHECK;
  2022. else if (!strcmp(arg, "-explicit_policy"))
  2023. flags |= X509_V_FLAG_EXPLICIT_POLICY;
  2024. else if (!strcmp(arg, "-inhibit_any"))
  2025. flags |= X509_V_FLAG_INHIBIT_ANY;
  2026. else if (!strcmp(arg, "-inhibit_map"))
  2027. flags |= X509_V_FLAG_INHIBIT_MAP;
  2028. else if (!strcmp(arg, "-x509_strict"))
  2029. flags |= X509_V_FLAG_X509_STRICT;
  2030. else if (!strcmp(arg, "-extended_crl"))
  2031. flags |= X509_V_FLAG_EXTENDED_CRL_SUPPORT;
  2032. else if (!strcmp(arg, "-use_deltas"))
  2033. flags |= X509_V_FLAG_USE_DELTAS;
  2034. else if (!strcmp(arg, "-policy_print"))
  2035. flags |= X509_V_FLAG_NOTIFY_POLICY;
  2036. else if (!strcmp(arg, "-check_ss_sig"))
  2037. flags |= X509_V_FLAG_CHECK_SS_SIGNATURE;
  2038. else
  2039. return 0;
  2040. if (*badarg) {
  2041. if (*pm)
  2042. X509_VERIFY_PARAM_free(*pm);
  2043. *pm = NULL;
  2044. goto end;
  2045. }
  2046. if (!*pm && !(*pm = X509_VERIFY_PARAM_new())) {
  2047. *badarg = 1;
  2048. goto end;
  2049. }
  2050. if (otmp)
  2051. X509_VERIFY_PARAM_add0_policy(*pm, otmp);
  2052. if (flags)
  2053. X509_VERIFY_PARAM_set_flags(*pm, flags);
  2054. if (purpose)
  2055. X509_VERIFY_PARAM_set_purpose(*pm, purpose);
  2056. if (depth >= 0)
  2057. X509_VERIFY_PARAM_set_depth(*pm, depth);
  2058. end:
  2059. (*pargs)++;
  2060. if (pargc)
  2061. *pargc -= *pargs - oldargs;
  2062. return 1;
  2063. }
  2064. /*
  2065. * Read whole contents of a BIO into an allocated memory buffer and return
  2066. * it.
  2067. */
  2068. int bio_to_mem(unsigned char **out, int maxlen, BIO *in)
  2069. {
  2070. BIO *mem;
  2071. int len, ret;
  2072. unsigned char tbuf[1024];
  2073. mem = BIO_new(BIO_s_mem());
  2074. if (!mem)
  2075. return -1;
  2076. for (;;) {
  2077. if ((maxlen != -1) && maxlen < 1024)
  2078. len = maxlen;
  2079. else
  2080. len = 1024;
  2081. len = BIO_read(in, tbuf, len);
  2082. if (len <= 0)
  2083. break;
  2084. if (BIO_write(mem, tbuf, len) != len) {
  2085. BIO_free(mem);
  2086. return -1;
  2087. }
  2088. maxlen -= len;
  2089. if (maxlen == 0)
  2090. break;
  2091. }
  2092. ret = BIO_get_mem_data(mem, (char **)out);
  2093. BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY);
  2094. BIO_free(mem);
  2095. return ret;
  2096. }
  2097. int pkey_ctrl_string(EVP_PKEY_CTX *ctx, char *value)
  2098. {
  2099. int rv;
  2100. char *stmp, *vtmp = NULL;
  2101. stmp = BUF_strdup(value);
  2102. if (!stmp)
  2103. return -1;
  2104. vtmp = strchr(stmp, ':');
  2105. if (vtmp) {
  2106. *vtmp = 0;
  2107. vtmp++;
  2108. }
  2109. rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);
  2110. OPENSSL_free(stmp);
  2111. return rv;
  2112. }
  2113. static void nodes_print(BIO *out, const char *name,
  2114. STACK_OF(X509_POLICY_NODE) *nodes)
  2115. {
  2116. X509_POLICY_NODE *node;
  2117. int i;
  2118. BIO_printf(out, "%s Policies:", name);
  2119. if (nodes) {
  2120. BIO_puts(out, "\n");
  2121. for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) {
  2122. node = sk_X509_POLICY_NODE_value(nodes, i);
  2123. X509_POLICY_NODE_print(out, node, 2);
  2124. }
  2125. } else
  2126. BIO_puts(out, " <empty>\n");
  2127. }
  2128. void policies_print(BIO *out, X509_STORE_CTX *ctx)
  2129. {
  2130. X509_POLICY_TREE *tree;
  2131. int explicit_policy;
  2132. int free_out = 0;
  2133. if (out == NULL) {
  2134. out = BIO_new_fp(stderr, BIO_NOCLOSE);
  2135. free_out = 1;
  2136. }
  2137. tree = X509_STORE_CTX_get0_policy_tree(ctx);
  2138. explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
  2139. BIO_printf(out, "Require explicit Policy: %s\n",
  2140. explicit_policy ? "True" : "False");
  2141. nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree));
  2142. nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree));
  2143. if (free_out)
  2144. BIO_free(out);
  2145. }
  2146. #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
  2147. static JPAKE_CTX *jpake_init(const char *us, const char *them,
  2148. const char *secret)
  2149. {
  2150. BIGNUM *p = NULL;
  2151. BIGNUM *g = NULL;
  2152. BIGNUM *q = NULL;
  2153. BIGNUM *bnsecret = BN_new();
  2154. JPAKE_CTX *ctx;
  2155. /* Use a safe prime for p (that we found earlier) */
  2156. BN_hex2bn(&p,
  2157. "F9E5B365665EA7A05A9C534502780FEE6F1AB5BD4F49947FD036DBD7E905269AF46EF28B0FC07487EE4F5D20FB3C0AF8E700F3A2FA3414970CBED44FEDFF80CE78D800F184BB82435D137AADA2C6C16523247930A63B85661D1FC817A51ACD96168E95898A1F83A79FFB529368AA7833ABD1B0C3AEDDB14D2E1A2F71D99F763F");
  2158. g = BN_new();
  2159. BN_set_word(g, 2);
  2160. q = BN_new();
  2161. BN_rshift1(q, p);
  2162. BN_bin2bn((const unsigned char *)secret, strlen(secret), bnsecret);
  2163. ctx = JPAKE_CTX_new(us, them, p, g, q, bnsecret);
  2164. BN_free(bnsecret);
  2165. BN_free(q);
  2166. BN_free(g);
  2167. BN_free(p);
  2168. return ctx;
  2169. }
  2170. static void jpake_send_part(BIO *conn, const JPAKE_STEP_PART *p)
  2171. {
  2172. BN_print(conn, p->gx);
  2173. BIO_puts(conn, "\n");
  2174. BN_print(conn, p->zkpx.gr);
  2175. BIO_puts(conn, "\n");
  2176. BN_print(conn, p->zkpx.b);
  2177. BIO_puts(conn, "\n");
  2178. }
  2179. static void jpake_send_step1(BIO *bconn, JPAKE_CTX *ctx)
  2180. {
  2181. JPAKE_STEP1 s1;
  2182. JPAKE_STEP1_init(&s1);
  2183. JPAKE_STEP1_generate(&s1, ctx);
  2184. jpake_send_part(bconn, &s1.p1);
  2185. jpake_send_part(bconn, &s1.p2);
  2186. (void)BIO_flush(bconn);
  2187. JPAKE_STEP1_release(&s1);
  2188. }
  2189. static void jpake_send_step2(BIO *bconn, JPAKE_CTX *ctx)
  2190. {
  2191. JPAKE_STEP2 s2;
  2192. JPAKE_STEP2_init(&s2);
  2193. JPAKE_STEP2_generate(&s2, ctx);
  2194. jpake_send_part(bconn, &s2);
  2195. (void)BIO_flush(bconn);
  2196. JPAKE_STEP2_release(&s2);
  2197. }
  2198. static void jpake_send_step3a(BIO *bconn, JPAKE_CTX *ctx)
  2199. {
  2200. JPAKE_STEP3A s3a;
  2201. JPAKE_STEP3A_init(&s3a);
  2202. JPAKE_STEP3A_generate(&s3a, ctx);
  2203. BIO_write(bconn, s3a.hhk, sizeof s3a.hhk);
  2204. (void)BIO_flush(bconn);
  2205. JPAKE_STEP3A_release(&s3a);
  2206. }
  2207. static void jpake_send_step3b(BIO *bconn, JPAKE_CTX *ctx)
  2208. {
  2209. JPAKE_STEP3B s3b;
  2210. JPAKE_STEP3B_init(&s3b);
  2211. JPAKE_STEP3B_generate(&s3b, ctx);
  2212. BIO_write(bconn, s3b.hk, sizeof s3b.hk);
  2213. (void)BIO_flush(bconn);
  2214. JPAKE_STEP3B_release(&s3b);
  2215. }
  2216. static void readbn(BIGNUM **bn, BIO *bconn)
  2217. {
  2218. char buf[10240];
  2219. int l;
  2220. l = BIO_gets(bconn, buf, sizeof buf);
  2221. assert(l > 0);
  2222. assert(buf[l - 1] == '\n');
  2223. buf[l - 1] = '\0';
  2224. BN_hex2bn(bn, buf);
  2225. }
  2226. static void jpake_receive_part(JPAKE_STEP_PART *p, BIO *bconn)
  2227. {
  2228. readbn(&p->gx, bconn);
  2229. readbn(&p->zkpx.gr, bconn);
  2230. readbn(&p->zkpx.b, bconn);
  2231. }
  2232. static void jpake_receive_step1(JPAKE_CTX *ctx, BIO *bconn)
  2233. {
  2234. JPAKE_STEP1 s1;
  2235. JPAKE_STEP1_init(&s1);
  2236. jpake_receive_part(&s1.p1, bconn);
  2237. jpake_receive_part(&s1.p2, bconn);
  2238. if (!JPAKE_STEP1_process(ctx, &s1)) {
  2239. ERR_print_errors(bio_err);
  2240. exit(1);
  2241. }
  2242. JPAKE_STEP1_release(&s1);
  2243. }
  2244. static void jpake_receive_step2(JPAKE_CTX *ctx, BIO *bconn)
  2245. {
  2246. JPAKE_STEP2 s2;
  2247. JPAKE_STEP2_init(&s2);
  2248. jpake_receive_part(&s2, bconn);
  2249. if (!JPAKE_STEP2_process(ctx, &s2)) {
  2250. ERR_print_errors(bio_err);
  2251. exit(1);
  2252. }
  2253. JPAKE_STEP2_release(&s2);
  2254. }
  2255. static void jpake_receive_step3a(JPAKE_CTX *ctx, BIO *bconn)
  2256. {
  2257. JPAKE_STEP3A s3a;
  2258. int l;
  2259. JPAKE_STEP3A_init(&s3a);
  2260. l = BIO_read(bconn, s3a.hhk, sizeof s3a.hhk);
  2261. assert(l == sizeof s3a.hhk);
  2262. if (!JPAKE_STEP3A_process(ctx, &s3a)) {
  2263. ERR_print_errors(bio_err);
  2264. exit(1);
  2265. }
  2266. JPAKE_STEP3A_release(&s3a);
  2267. }
  2268. static void jpake_receive_step3b(JPAKE_CTX *ctx, BIO *bconn)
  2269. {
  2270. JPAKE_STEP3B s3b;
  2271. int l;
  2272. JPAKE_STEP3B_init(&s3b);
  2273. l = BIO_read(bconn, s3b.hk, sizeof s3b.hk);
  2274. assert(l == sizeof s3b.hk);
  2275. if (!JPAKE_STEP3B_process(ctx, &s3b)) {
  2276. ERR_print_errors(bio_err);
  2277. exit(1);
  2278. }
  2279. JPAKE_STEP3B_release(&s3b);
  2280. }
  2281. void jpake_client_auth(BIO *out, BIO *conn, const char *secret)
  2282. {
  2283. JPAKE_CTX *ctx;
  2284. BIO *bconn;
  2285. BIO_puts(out, "Authenticating with JPAKE\n");
  2286. ctx = jpake_init("client", "server", secret);
  2287. bconn = BIO_new(BIO_f_buffer());
  2288. BIO_push(bconn, conn);
  2289. jpake_send_step1(bconn, ctx);
  2290. jpake_receive_step1(ctx, bconn);
  2291. jpake_send_step2(bconn, ctx);
  2292. jpake_receive_step2(ctx, bconn);
  2293. jpake_send_step3a(bconn, ctx);
  2294. jpake_receive_step3b(ctx, bconn);
  2295. BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n");
  2296. psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx));
  2297. BIO_pop(bconn);
  2298. BIO_free(bconn);
  2299. JPAKE_CTX_free(ctx);
  2300. }
  2301. void jpake_server_auth(BIO *out, BIO *conn, const char *secret)
  2302. {
  2303. JPAKE_CTX *ctx;
  2304. BIO *bconn;
  2305. BIO_puts(out, "Authenticating with JPAKE\n");
  2306. ctx = jpake_init("server", "client", secret);
  2307. bconn = BIO_new(BIO_f_buffer());
  2308. BIO_push(bconn, conn);
  2309. jpake_receive_step1(ctx, bconn);
  2310. jpake_send_step1(bconn, ctx);
  2311. jpake_receive_step2(ctx, bconn);
  2312. jpake_send_step2(bconn, ctx);
  2313. jpake_receive_step3a(ctx, bconn);
  2314. jpake_send_step3b(bconn, ctx);
  2315. BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n");
  2316. psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx));
  2317. BIO_pop(bconn);
  2318. BIO_free(bconn);
  2319. JPAKE_CTX_free(ctx);
  2320. }
  2321. #endif
  2322. /*
  2323. * Platform-specific sections
  2324. */
  2325. #if defined(_WIN32)
  2326. # ifdef fileno
  2327. # undef fileno
  2328. # define fileno(a) (int)_fileno(a)
  2329. # endif
  2330. # include <windows.h>
  2331. # include <tchar.h>
  2332. static int WIN32_rename(const char *from, const char *to)
  2333. {
  2334. TCHAR *tfrom = NULL, *tto;
  2335. DWORD err;
  2336. int ret = 0;
  2337. if (sizeof(TCHAR) == 1) {
  2338. tfrom = (TCHAR *)from;
  2339. tto = (TCHAR *)to;
  2340. } else { /* UNICODE path */
  2341. size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1;
  2342. tfrom = (TCHAR *)malloc(sizeof(TCHAR) * (flen + tlen));
  2343. if (tfrom == NULL)
  2344. goto err;
  2345. tto = tfrom + flen;
  2346. # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2347. if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen))
  2348. # endif
  2349. for (i = 0; i < flen; i++)
  2350. tfrom[i] = (TCHAR)from[i];
  2351. # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2352. if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen))
  2353. # endif
  2354. for (i = 0; i < tlen; i++)
  2355. tto[i] = (TCHAR)to[i];
  2356. }
  2357. if (MoveFile(tfrom, tto))
  2358. goto ok;
  2359. err = GetLastError();
  2360. if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) {
  2361. if (DeleteFile(tto) && MoveFile(tfrom, tto))
  2362. goto ok;
  2363. err = GetLastError();
  2364. }
  2365. if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
  2366. errno = ENOENT;
  2367. else if (err == ERROR_ACCESS_DENIED)
  2368. errno = EACCES;
  2369. else
  2370. errno = EINVAL; /* we could map more codes... */
  2371. err:
  2372. ret = -1;
  2373. ok:
  2374. if (tfrom != NULL && tfrom != (TCHAR *)from)
  2375. free(tfrom);
  2376. return ret;
  2377. }
  2378. #endif
  2379. /* app_tminterval section */
  2380. #if defined(_WIN32)
  2381. double app_tminterval(int stop, int usertime)
  2382. {
  2383. FILETIME now;
  2384. double ret = 0;
  2385. static ULARGE_INTEGER tmstart;
  2386. static int warning = 1;
  2387. # ifdef _WIN32_WINNT
  2388. static HANDLE proc = NULL;
  2389. if (proc == NULL) {
  2390. if (check_winnt())
  2391. proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
  2392. GetCurrentProcessId());
  2393. if (proc == NULL)
  2394. proc = (HANDLE) - 1;
  2395. }
  2396. if (usertime && proc != (HANDLE) - 1) {
  2397. FILETIME junk;
  2398. GetProcessTimes(proc, &junk, &junk, &junk, &now);
  2399. } else
  2400. # endif
  2401. {
  2402. SYSTEMTIME systime;
  2403. if (usertime && warning) {
  2404. BIO_printf(bio_err, "To get meaningful results, run "
  2405. "this program on idle system.\n");
  2406. warning = 0;
  2407. }
  2408. GetSystemTime(&systime);
  2409. SystemTimeToFileTime(&systime, &now);
  2410. }
  2411. if (stop == TM_START) {
  2412. tmstart.u.LowPart = now.dwLowDateTime;
  2413. tmstart.u.HighPart = now.dwHighDateTime;
  2414. } else {
  2415. ULARGE_INTEGER tmstop;
  2416. tmstop.u.LowPart = now.dwLowDateTime;
  2417. tmstop.u.HighPart = now.dwHighDateTime;
  2418. ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7;
  2419. }
  2420. return (ret);
  2421. }
  2422. #elif defined(OPENSSL_SYS_NETWARE)
  2423. # include <time.h>
  2424. double app_tminterval(int stop, int usertime)
  2425. {
  2426. double ret = 0;
  2427. static clock_t tmstart;
  2428. static int warning = 1;
  2429. if (usertime && warning) {
  2430. BIO_printf(bio_err, "To get meaningful results, run "
  2431. "this program on idle system.\n");
  2432. warning = 0;
  2433. }
  2434. if (stop == TM_START)
  2435. tmstart = clock();
  2436. else
  2437. ret = (clock() - tmstart) / (double)CLOCKS_PER_SEC;
  2438. return (ret);
  2439. }
  2440. #elif defined(OPENSSL_SYSTEM_VXWORKS)
  2441. # include <time.h>
  2442. double app_tminterval(int stop, int usertime)
  2443. {
  2444. double ret = 0;
  2445. # ifdef CLOCK_REALTIME
  2446. static struct timespec tmstart;
  2447. struct timespec now;
  2448. # else
  2449. static unsigned long tmstart;
  2450. unsigned long now;
  2451. # endif
  2452. static int warning = 1;
  2453. if (usertime && warning) {
  2454. BIO_printf(bio_err, "To get meaningful results, run "
  2455. "this program on idle system.\n");
  2456. warning = 0;
  2457. }
  2458. # ifdef CLOCK_REALTIME
  2459. clock_gettime(CLOCK_REALTIME, &now);
  2460. if (stop == TM_START)
  2461. tmstart = now;
  2462. else
  2463. ret = ((now.tv_sec + now.tv_nsec * 1e-9)
  2464. - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9));
  2465. # else
  2466. now = tickGet();
  2467. if (stop == TM_START)
  2468. tmstart = now;
  2469. else
  2470. ret = (now - tmstart) / (double)sysClkRateGet();
  2471. # endif
  2472. return (ret);
  2473. }
  2474. #elif defined(OPENSSL_SYSTEM_VMS)
  2475. # include <time.h>
  2476. # include <times.h>
  2477. double app_tminterval(int stop, int usertime)
  2478. {
  2479. static clock_t tmstart;
  2480. double ret = 0;
  2481. clock_t now;
  2482. # ifdef __TMS
  2483. struct tms rus;
  2484. now = times(&rus);
  2485. if (usertime)
  2486. now = rus.tms_utime;
  2487. # else
  2488. if (usertime)
  2489. now = clock(); /* sum of user and kernel times */
  2490. else {
  2491. struct timeval tv;
  2492. gettimeofday(&tv, NULL);
  2493. now = (clock_t)((unsigned long long)tv.tv_sec * CLK_TCK +
  2494. (unsigned long long)tv.tv_usec * (1000000 / CLK_TCK)
  2495. );
  2496. }
  2497. # endif
  2498. if (stop == TM_START)
  2499. tmstart = now;
  2500. else
  2501. ret = (now - tmstart) / (double)(CLK_TCK);
  2502. return (ret);
  2503. }
  2504. #elif defined(_SC_CLK_TCK) /* by means of unistd.h */
  2505. # include <sys/times.h>
  2506. double app_tminterval(int stop, int usertime)
  2507. {
  2508. double ret = 0;
  2509. struct tms rus;
  2510. clock_t now = times(&rus);
  2511. static clock_t tmstart;
  2512. if (usertime)
  2513. now = rus.tms_utime;
  2514. if (stop == TM_START)
  2515. tmstart = now;
  2516. else {
  2517. long int tck = sysconf(_SC_CLK_TCK);
  2518. ret = (now - tmstart) / (double)tck;
  2519. }
  2520. return (ret);
  2521. }
  2522. #else
  2523. # include <sys/time.h>
  2524. # include <sys/resource.h>
  2525. double app_tminterval(int stop, int usertime)
  2526. {
  2527. double ret = 0;
  2528. struct rusage rus;
  2529. struct timeval now;
  2530. static struct timeval tmstart;
  2531. if (usertime)
  2532. getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime;
  2533. else
  2534. gettimeofday(&now, NULL);
  2535. if (stop == TM_START)
  2536. tmstart = now;
  2537. else
  2538. ret = ((now.tv_sec + now.tv_usec * 1e-6)
  2539. - (tmstart.tv_sec + tmstart.tv_usec * 1e-6));
  2540. return ret;
  2541. }
  2542. #endif
  2543. /* app_isdir section */
  2544. #ifdef _WIN32
  2545. int app_isdir(const char *name)
  2546. {
  2547. HANDLE hList;
  2548. WIN32_FIND_DATA FileData;
  2549. # if defined(UNICODE) || defined(_UNICODE)
  2550. size_t i, len_0 = strlen(name) + 1;
  2551. if (len_0 > sizeof(FileData.cFileName) / sizeof(FileData.cFileName[0]))
  2552. return -1;
  2553. # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
  2554. if (!MultiByteToWideChar
  2555. (CP_ACP, 0, name, len_0, FileData.cFileName, len_0))
  2556. # endif
  2557. for (i = 0; i < len_0; i++)
  2558. FileData.cFileName[i] = (WCHAR)name[i];
  2559. hList = FindFirstFile(FileData.cFileName, &FileData);
  2560. # else
  2561. hList = FindFirstFile(name, &FileData);
  2562. # endif
  2563. if (hList == INVALID_HANDLE_VALUE)
  2564. return -1;
  2565. FindClose(hList);
  2566. return ((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  2567. }
  2568. #else
  2569. # include <sys/stat.h>
  2570. # ifndef S_ISDIR
  2571. # if defined(_S_IFMT) && defined(_S_IFDIR)
  2572. # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR)
  2573. # else
  2574. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  2575. # endif
  2576. # endif
  2577. int app_isdir(const char *name)
  2578. {
  2579. # if defined(S_ISDIR)
  2580. struct stat st;
  2581. if (stat(name, &st) == 0)
  2582. return S_ISDIR(st.st_mode);
  2583. else
  2584. return -1;
  2585. # else
  2586. return -1;
  2587. # endif
  2588. }
  2589. #endif
  2590. /* raw_read|write section */
  2591. #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
  2592. int raw_read_stdin(void *buf, int siz)
  2593. {
  2594. DWORD n;
  2595. if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL))
  2596. return (n);
  2597. else
  2598. return (-1);
  2599. }
  2600. #else
  2601. int raw_read_stdin(void *buf, int siz)
  2602. {
  2603. return read(fileno(stdin), buf, siz);
  2604. }
  2605. #endif
  2606. #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
  2607. int raw_write_stdout(const void *buf, int siz)
  2608. {
  2609. DWORD n;
  2610. if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL))
  2611. return (n);
  2612. else
  2613. return (-1);
  2614. }
  2615. #else
  2616. int raw_write_stdout(const void *buf, int siz)
  2617. {
  2618. return write(fileno(stdout), buf, siz);
  2619. }
  2620. #endif