ck_ssl.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439
  1. char *cksslv = "SSL/TLS support, 9.0.233, 24 Dec 2015";
  2. /*
  3. C K _ S S L . C -- OpenSSL Interface for C-Kermit
  4. Copyright (C) 1985, 2017,
  5. Trustees of Columbia University in the City of New York.
  6. All rights reserved. See the C-Kermit COPYING.TXT file or the
  7. copyright text in the ckcmai.c module for disclaimer and permissions.
  8. Author: Jeffrey E Altman (jaltman@secure-endpoints.com)
  9. Secure Endpoints Inc., New York City
  10. Provides:
  11. . Telnet Auth SSL option compatible with Tim Hudson's hack.
  12. . Telnet START_TLS option
  13. . Configuration of certificate and key files
  14. . Certificate verification and revocation list checks
  15. . Client certificate to user id routine
  16. Note: This code is written to be compatible with OpenSSL 0.9.6[abcdefgh]
  17. and 0.9.7 beta 5 and later, and (since July 2012) 1.0.x.
  18. It will also compile with version 0.9.5 although that is discouraged
  19. due to security weaknesses in that release.
  20. Adapted for LibreSSL by Bernard Spil, December 2015 (search "Spil")
  21. */
  22. #include "ckcsym.h"
  23. #include "ckcdeb.h"
  24. #ifdef CK_SSL
  25. #include "ckcnet.h"
  26. #include "ckuath.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #ifdef UNIX
  30. #include <netinet/in.h>
  31. #ifndef FREEBSD4
  32. #include <arpa/inet.h>
  33. #endif /* FREEBSD4 */
  34. #endif /* UNIX */
  35. #ifdef DEC_TCPIP
  36. #include <time.h>
  37. #include <inet.h>
  38. #endif /* DEC_TCPIP */
  39. #ifdef OS2
  40. extern char exedir[];
  41. #ifdef NT
  42. char * GetAppData(int);
  43. #endif
  44. #endif /* OS2 */
  45. extern int quiet; /* fdc - Mon Nov 28 11:44:15 2005 */
  46. static int ssl_installed = 1;
  47. #endif /* CK_SSL */
  48. int
  49. ck_ssh_is_installed()
  50. {
  51. #ifdef SSHBUILTIN
  52. #ifdef SSLDLL
  53. #ifdef NT
  54. extern HINSTANCE hCRYPTO;
  55. #else /* NT */
  56. extern HMODULE hCRYPTO;
  57. #endif /* NT */
  58. debug(F111,"ck_ssh_is_installed","hCRYPTO",hCRYPTO);
  59. return(ssl_installed && (hCRYPTO != NULL));
  60. #else /* SSLDLL */
  61. return(ssl_installed);
  62. #endif /* SSLDLL */
  63. #else
  64. return 0;
  65. #endif
  66. }
  67. int
  68. #ifdef CK_ANSIC
  69. ck_ssleay_is_installed(void)
  70. #else
  71. ck_ssleay_is_installed()
  72. #endif
  73. {
  74. #ifdef CK_SSL
  75. #ifdef SSLDLL
  76. #ifdef NT
  77. extern HINSTANCE hSSL, hCRYPTO;
  78. #else /* NT */
  79. extern HMODULE hSSL, hCRYPTO;
  80. #endif /* NT */
  81. debug(F111,"ck_ssleay_is_installed","hSSL",hSSL);
  82. debug(F111,"ck_ssleay_is_installed","hCRYPTO",hCRYPTO);
  83. return(ssl_installed && (hSSL != NULL) && (hCRYPTO != NULL));
  84. #else /* SSLDLL */
  85. return(ssl_installed);
  86. #endif /* SSLDLL */
  87. #else /* CK_SSL */
  88. return(0);
  89. #endif /* CK_SSL */
  90. }
  91. #ifdef CK_SSL
  92. #include "ckcker.h"
  93. #include "ckucmd.h" /* For struct keytab */
  94. #include "ckctel.h"
  95. #include "ck_ssl.h"
  96. #ifdef UNIX
  97. #include <pwd.h> /* Password file for home directory */
  98. #endif /* UNIX */
  99. #ifdef OS2
  100. #include <process.h>
  101. #endif /* OS2 */
  102. #ifdef OS2ONLY
  103. #include "ckotcp.h"
  104. #endif /* OS2ONLY */
  105. #ifdef SSLDLL
  106. int ssl_finished_messages = 0;
  107. #else /* SSLDLL */
  108. #ifdef OPENSSL_VERSION_NUMBER
  109. int ssl_finished_messages = (OPENSSL_VERSION_NUMBER >= 0x0090581fL);
  110. #else
  111. !ERROR This module requires OpenSSL 0.9.5a or higher
  112. #endif /* OPENSSL_VERSION_NUMBER */
  113. #endif /* SSLDLL */
  114. static int auth_ssl_valid = 0;
  115. static char *auth_ssl_name = 0; /* this holds the oneline name */
  116. char ssl_err[SSL_ERR_BFSZ]="";
  117. BIO *bio_err=NULL;
  118. X509_STORE *crl_store = NULL;
  119. #ifndef NOFTP
  120. #ifndef SYSFTP
  121. SSL *ssl_ftp_con = NULL;
  122. SSL_CTX *ssl_ftp_ctx = NULL;
  123. SSL *ssl_ftp_data_con = NULL;
  124. int ssl_ftp_active_flag = 0;
  125. int ssl_ftp_data_active_flag = 0;
  126. #endif /* SYSFTP */
  127. #endif /* NOFTP */
  128. #ifndef NOHTTP
  129. SSL *tls_http_con = NULL;
  130. SSL_CTX *tls_http_ctx = NULL;
  131. int tls_http_active_flag = 0;
  132. int ssl_http_initialized = 0;
  133. #endif /* NOHTTP */
  134. SSL_CTX *ssl_ctx = NULL;
  135. SSL *ssl_con = NULL;
  136. int ssl_debug_flag = 0;
  137. int ssl_verbose_flag = 0;
  138. int ssl_only_flag = 0;
  139. int ssl_raw_flag = 0;
  140. int ssl_active_flag = 0;
  141. int ssl_verify_flag = SSL_VERIFY_PEER;
  142. int ssl_certsok_flag = 0;
  143. char *ssl_rsa_cert_file = NULL;
  144. char *ssl_rsa_cert_chain_file = NULL;
  145. char *ssl_rsa_key_file = NULL;
  146. char *ssl_dsa_cert_file = NULL;
  147. char *ssl_dsa_cert_chain_file = NULL;
  148. char *ssl_dh_key_file = NULL;
  149. char *ssl_crl_file = NULL;
  150. char *ssl_crl_dir = NULL;
  151. char *ssl_verify_file = NULL;
  152. char *ssl_verify_dir = NULL;
  153. char *ssl_dh_param_file = NULL;
  154. char *ssl_cipher_list = NULL;
  155. char *ssl_rnd_file = NULL;
  156. SSL_CTX *tls_ctx = NULL;
  157. SSL *tls_con = NULL;
  158. int tls_only_flag = 0;
  159. int tls_raw_flag = 0;
  160. int tls_active_flag = 0;
  161. int ssl_initialized = 0;
  162. int ssl_verify_depth = -1; /* used to track depth in verify routines */
  163. /* compile this set to 1 to negotiate SSL/TLS but not actually start it */
  164. int ssl_dummy_flag=0;
  165. extern int inserver;
  166. extern int debses;
  167. extern int accept_complete;
  168. extern char szHostName[], szUserNameRequested[], szUserNameAuthenticated[];
  169. _PROTOTYP(int X509_to_user,(X509 *, char *, int));
  170. static int verbosity = 0; /* Message control */
  171. static VOID
  172. setverbosity() {
  173. verbosity = ssl_verbose_flag;
  174. if (quiet) verbosity = 0;
  175. }
  176. int
  177. #ifdef CK_ANSIC
  178. ssl_server_verify_callback(int ok, X509_STORE_CTX * ctx)
  179. #else /* CK_ANSIC */
  180. ssl_server_verify_callback(ok, ctx)
  181. int ok;
  182. X509_STORE_CTX *ctx;
  183. #endif /* CK_ANSIC */
  184. {
  185. static char *saved_subject=NULL;
  186. char *subject=NULL, *issuer=NULL;
  187. int depth,error;
  188. X509 *xs = NULL;
  189. if ( ssl_certsok_flag )
  190. return(1);
  191. setverbosity();
  192. error=X509_STORE_CTX_get_error(ctx);
  193. depth=X509_STORE_CTX_get_error_depth(ctx);
  194. xs=X509_STORE_CTX_get_current_cert(ctx);
  195. if (depth==0) {
  196. /* clear things */
  197. if (saved_subject!=NULL) {
  198. free(saved_subject);
  199. saved_subject=NULL;
  200. }
  201. if (auth_ssl_name!=NULL) {
  202. free(auth_ssl_name);
  203. auth_ssl_name=NULL;
  204. }
  205. }
  206. if (ssl_debug_flag && !inserver) {
  207. printf("ssl:server_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
  208. depth,ok,error,X509_verify_cert_error_string(error));
  209. }
  210. /* first thing is to have a meaningful name for the current
  211. * certificate that is being verified ... and if we cannot
  212. * determine that then something is seriously wrong!
  213. */
  214. makestr(&subject,
  215. (char *)X509_NAME_oneline(X509_get_subject_name(xs),NULL,0));
  216. makestr(&issuer,
  217. (char *)X509_NAME_oneline(X509_get_issuer_name(xs),NULL,0));
  218. if (!subject || !subject[0] || !issuer || !issuer[0]) {
  219. ok = 0;
  220. goto return_time;
  221. }
  222. if (verbosity && !inserver && depth != ssl_verify_depth) {
  223. printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
  224. printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
  225. ssl_verify_depth = depth;
  226. }
  227. /* make sure that the certificate that has been presented */
  228. /* has not been revoked (if we have been given a CRL. */
  229. ok = ssl_verify_crl(ok, ctx);
  230. /* if we have any form of error in secure mode we reject the connection */
  231. if (error!=X509_V_OK) {
  232. if (inserver) {
  233. #ifdef CKSYSLOG
  234. if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  235. cksyslog(SYSLG_LI, 0,
  236. "X.509 Certificate verify failure",
  237. (char *) subject,
  238. (char *)X509_verify_cert_error_string(error)
  239. );
  240. }
  241. #endif /* CKSYSLOG */
  242. } else {
  243. if ( ssl_verify_flag &
  244. (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  245. printf("Error: ");
  246. else
  247. printf("Warning: ");
  248. switch (error) {
  249. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  250. printf("Certificate is self signed.\r\n");
  251. break;
  252. case X509_V_ERR_CERT_HAS_EXPIRED:
  253. printf("Certificate has expired.\r\n");
  254. break;
  255. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  256. printf(
  257. "Certificate issuer's certificate isn't available locally.\r\n");
  258. break;
  259. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  260. printf("Unable to verify leaf signature.\r\n");
  261. break;
  262. case X509_V_ERR_CERT_REVOKED:
  263. printf("Certificate revoked.\r\n");
  264. break;
  265. default:
  266. printf("Error %d while verifying certificate.\r\n",
  267. X509_STORE_CTX_get_error(ctx));
  268. break;
  269. }
  270. }
  271. ok = !(ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  272. } else {
  273. /* if we got all the way to the top of the tree then
  274. * we *can* use this certificate for a username to
  275. * match ... in all other cases we must not!
  276. */
  277. auth_ssl_name = saved_subject;
  278. saved_subject = NULL;
  279. }
  280. return_time:
  281. /* save the name if at least the first level is okay */
  282. if (depth == 0 && ok)
  283. makestr(&saved_subject,subject);
  284. /* clean up things */
  285. if (subject!=NULL)
  286. free(subject);
  287. if (issuer!=NULL)
  288. free(issuer);
  289. return ok;
  290. }
  291. int
  292. #ifdef CK_ANSIC
  293. ssl_client_verify_callback(int ok, X509_STORE_CTX * ctx)
  294. #else
  295. ssl_client_verify_callback(ok, ctx)
  296. int ok;
  297. X509_STORE_CTX *ctx;
  298. #endif
  299. {
  300. char subject[256]="", issuer[256]="";
  301. int depth, error, len;
  302. X509 *xs;
  303. setverbosity();
  304. xs=X509_STORE_CTX_get_current_cert(ctx);
  305. error=X509_STORE_CTX_get_error(ctx);
  306. depth=X509_STORE_CTX_get_error_depth(ctx);
  307. if ( ssl_debug_flag )
  308. printf("ssl:client_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
  309. depth,ok,error,X509_verify_cert_error_string(error));
  310. if ( ssl_certsok_flag ) {
  311. ok = 1;
  312. }
  313. /* first thing is to have a meaningful name for the current
  314. * certificate that is being verified ... and if we cannot
  315. * determine that then something is seriously wrong!
  316. */
  317. #ifdef XN_FLAG_SEP_MULTILINE
  318. X509_NAME_print_ex(bio_err,X509_get_subject_name(xs),4,
  319. XN_FLAG_SEP_MULTILINE);
  320. len = BIO_read(bio_err,subject,256);
  321. subject[len < 256 ? len : 255] = '\0';
  322. if (!subject[0]) {
  323. ERR_print_errors(bio_err);
  324. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  325. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  326. uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
  327. ok=0;
  328. goto return_time;
  329. }
  330. X509_NAME_print_ex(bio_err,X509_get_issuer_name(xs),4,
  331. XN_FLAG_SEP_MULTILINE);
  332. len = BIO_read(bio_err,issuer,256);
  333. issuer[len < 256 ? len : 255] = '\0';
  334. if (!issuer[0]) {
  335. ERR_print_errors(bio_err);
  336. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  337. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  338. uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
  339. ok=0;
  340. goto return_time;
  341. }
  342. #else /* XN_FLAG_SEP_MULTILINE */
  343. X509_NAME_oneline(X509_get_subject_name(xs),subject,256);
  344. if (!subject[0]) {
  345. int len;
  346. ERR_print_errors(bio_err);
  347. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  348. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  349. uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
  350. ok=0;
  351. goto return_time;
  352. }
  353. X509_NAME_oneline(X509_get_issuer_name(xs),issuer,256);
  354. if (!issuer[0]) {
  355. int len;
  356. ERR_print_errors(bio_err);
  357. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  358. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  359. uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
  360. ok=0;
  361. goto return_time;
  362. }
  363. #endif /* XN_FLAG_SEP_MULTILINE */
  364. if (verbosity && depth != ssl_verify_depth) {
  365. printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
  366. printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
  367. ssl_verify_depth = depth;
  368. }
  369. ok = ssl_verify_crl(ok, ctx);
  370. if ( !ok ) {
  371. char prefix[1024];
  372. /* if the server is using a self signed certificate then
  373. * we need to decide if that is good enough for us to
  374. * accept ...
  375. */
  376. switch ( error ) {
  377. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: {
  378. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  379. /* make 100% sure that in secure more we drop the
  380. * connection if the server does not have a
  381. * real certificate!
  382. */
  383. ckmakxmsg(prefix,1024,
  384. "Error: Server has a self-signed certificate\n",
  385. "[",ckitoa(depth),"] Certificate Subject=\n",subject,
  386. "\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
  387. NULL,NULL,NULL);
  388. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  389. /* sometimes it is really handy to be able to debug things
  390. * and still get a connection!
  391. */
  392. if (ssl_debug_flag) {
  393. printf("SSL: debug -> ignoring cert required!\r\n");
  394. ok=1;
  395. } else {
  396. ok=0;
  397. }
  398. goto return_time;
  399. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  400. ckmakxmsg(prefix,1024,
  401. "Warning: Server has a self-signed certificate\n",
  402. "[",ckitoa(depth),"] Certificate Subject=\n",subject,
  403. "\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
  404. NULL,NULL,NULL);
  405. ok = uq_ok(prefix,
  406. "Continue? (Y/N) ",
  407. 3, NULL, 0);
  408. if ( ok < 0 )
  409. ok = 0;
  410. goto return_time;
  411. }
  412. }
  413. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  414. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  415. /* make 100% sure that in secure more we drop the
  416. * connection if the server does not have a
  417. * real certificate!
  418. */
  419. ckmakxmsg(prefix,1024,
  420. "Error: ",
  421. (char *)X509_verify_cert_error_string(error),
  422. "\nCertificate Issuer=\n",issuer,
  423. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  424. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  425. /* sometimes it is really handy to be able to debug things
  426. * and still get a connection!
  427. */
  428. if (ssl_debug_flag) {
  429. printf("SSL: debug -> ignoring cert required!\r\n");
  430. ok=1;
  431. } else {
  432. ok=0;
  433. }
  434. goto return_time;
  435. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  436. ckmakxmsg(prefix,1024,
  437. "Warning: ",
  438. (char *)X509_verify_cert_error_string(error),
  439. "\nCertificate Issuer=\n",issuer,
  440. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  441. ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  442. goto return_time;
  443. }
  444. break;
  445. case X509_V_ERR_CERT_NOT_YET_VALID:
  446. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  447. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  448. int len;
  449. /* make 100% sure that in secure more we drop the
  450. * connection if the server does not have a
  451. * real certificate!
  452. */
  453. ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
  454. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  455. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  456. ckmakxmsg(prefix,1024,
  457. "Error: ",
  458. (char *)X509_verify_cert_error_string(error),
  459. "\nCertificate Subject=\n",subject,
  460. "\nnotBefore=",ssl_err,
  461. NULL,NULL,NULL,NULL,NULL,NULL);
  462. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  463. /* sometimes it is really handy to be able to debug things
  464. * and still get a connection!
  465. */
  466. if (ssl_debug_flag) {
  467. printf("SSL: debug -> ignoring cert required!\r\n");
  468. ok=1;
  469. } else {
  470. ok=0;
  471. }
  472. goto return_time;
  473. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  474. int len;
  475. ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
  476. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  477. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  478. ckmakxmsg(prefix,1024,
  479. "Warning: ",
  480. (char *)X509_verify_cert_error_string(error),
  481. "\nCertificate Subject=\n",subject,
  482. "\n notBefore=",ssl_err,
  483. NULL,NULL,NULL,NULL,NULL,NULL);
  484. ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  485. }
  486. break;
  487. case X509_V_ERR_CERT_HAS_EXPIRED:
  488. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  489. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  490. int len;
  491. /* make 100% sure that in secure more we drop the
  492. * connection if the server does not have a
  493. * real certificate!
  494. */
  495. ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
  496. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  497. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  498. ckmakxmsg(prefix,1024,
  499. "Error: ",
  500. (char *)X509_verify_cert_error_string(error),
  501. "\nCertificate Subject=\n",subject,
  502. "\n notAfter=",ssl_err,
  503. NULL,NULL,NULL,NULL,NULL,NULL);
  504. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  505. /* sometimes it is really handy to be able to debug things
  506. * and still get a connection!
  507. */
  508. if (ssl_debug_flag) {
  509. printf("SSL: debug -> ignoring cert required!\r\n");
  510. ok=1;
  511. } else {
  512. ok=0;
  513. }
  514. goto return_time;
  515. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  516. int len;
  517. ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
  518. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  519. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  520. ckmakxmsg(prefix,1024,
  521. "Warning: ",
  522. (char *)X509_verify_cert_error_string(error),
  523. "\nCertificate Subject=\n",subject,
  524. "\n notAfter=",ssl_err,
  525. NULL,NULL,NULL,NULL,NULL,NULL);
  526. ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  527. }
  528. break;
  529. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  530. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  531. /*
  532. * When an SSL server sends its certificates to the client there
  533. * are two" conventions": one is to send the complete certificate
  534. * chain and the other is to send the whole chain apart from the
  535. * root.
  536. *
  537. * You don't usually need the root because the root is normally
  538. * stored and trusted locally.
  539. *
  540. * So if you get the whole chain it will complain about the self
  541. * signed certificate whereas if the root is missing it says it
  542. * can't find the issuer certificate.
  543. */
  544. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  545. /* make 100% sure that in secure more we drop the
  546. * connection if the server does not have a
  547. * real certificate!
  548. */
  549. ckmakxmsg(prefix,1024,
  550. "Error: ",
  551. (char *)X509_verify_cert_error_string(error),
  552. "\nCertificate Issuer=\n",issuer,
  553. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  554. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  555. /* sometimes it is really handy to be able to debug things
  556. * and still get a connection!
  557. */
  558. if (ssl_debug_flag) {
  559. printf("SSL: debug -> ignoring cert required!\r\n");
  560. ok=1;
  561. } else {
  562. ok=0;
  563. }
  564. goto return_time;
  565. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  566. ckmakxmsg(prefix,1024,
  567. "Warning: ",
  568. (char *)X509_verify_cert_error_string(error),
  569. "\nCertificate Issuer=\n",issuer,
  570. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  571. ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  572. #ifdef NT
  573. if (ok) {
  574. /* if the user decides to accept the certificate
  575. * offer to store it for future connections in
  576. * the user's private store
  577. */
  578. ok = uq_ok(
  579. "Do you wish to store the certificate to verify future connections?",
  580. "Continue (Y/N)", 3, NULL, 0);
  581. if (ok)
  582. ck_X509_save_cert_to_user_store(xs);
  583. }
  584. #endif /* NT */
  585. }
  586. break;
  587. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  588. case X509_V_ERR_UNABLE_TO_GET_CRL:
  589. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  590. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  591. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  592. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  593. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  594. case X509_V_ERR_CRL_NOT_YET_VALID:
  595. case X509_V_ERR_CRL_HAS_EXPIRED:
  596. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  597. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  598. case X509_V_ERR_OUT_OF_MEM:
  599. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  600. case X509_V_ERR_CERT_REVOKED:
  601. case X509_V_ERR_APPLICATION_VERIFICATION:
  602. default:
  603. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  604. /* make 100% sure that in secure mode we drop the
  605. * connection if the server does not have a
  606. * real certificate!
  607. */
  608. ckmakxmsg(prefix,1024,
  609. "Error: ",
  610. (char *)X509_verify_cert_error_string(error),
  611. "\nCertificate Subject=\n",subject,
  612. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  613. uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  614. /* sometimes it is really handy to be able to debug things
  615. * and still get a connection!
  616. */
  617. if (ssl_debug_flag) {
  618. printf("SSL: debug -> ignoring cert required!\r\n");
  619. ok=1;
  620. } else {
  621. ok=0;
  622. }
  623. goto return_time;
  624. } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  625. ckmakxmsg(prefix,1024,
  626. "Warning: ",
  627. (char *)X509_verify_cert_error_string(error),
  628. "\nCertificate Subject=\n",subject,
  629. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  630. ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  631. }
  632. break;
  633. }
  634. }
  635. return_time:
  636. if ( ssl_debug_flag )
  637. printf("ssl:client_verify_callback => ok: %d\r\n",ok);
  638. return ok;
  639. }
  640. VOID
  641. #ifdef CK_ANSIC
  642. ssl_client_info_callback(const SSL *s, int where, int ret)
  643. #else
  644. ssl_client_info_callback(s,where,ret)
  645. const SSL *s;
  646. int where;
  647. int ret;
  648. #endif /* CK_ANSIC */
  649. {
  650. if (inserver || !ssl_debug_flag)
  651. return;
  652. setverbosity();
  653. switch ( where ) {
  654. case SSL_CB_CONNECT_LOOP:
  655. printf("SSL_connect:%s %s\r\n",
  656. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  657. break;
  658. case SSL_CB_CONNECT_EXIT:
  659. if (ret == 0) {
  660. printf("SSL_connect:failed in %s %s\r\n",
  661. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  662. } else if (ret < 0) {
  663. printf("SSL_connect:error in %s %s\r\n",
  664. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  665. }
  666. break;
  667. case SSL_CB_ACCEPT_LOOP:
  668. printf("SSL_accept:%s %s\r\n",
  669. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  670. break;
  671. case SSL_CB_ACCEPT_EXIT:
  672. if (ret == 0) {
  673. printf("SSL_accept:failed in %s %s\r\n",
  674. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  675. } else if (ret < 0) {
  676. printf("SSL_accept:error in %s %s\r\n",
  677. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  678. }
  679. break;
  680. case SSL_CB_READ_ALERT:
  681. printf("SSL_read_alert\r\n");
  682. break;
  683. case SSL_CB_WRITE_ALERT:
  684. printf("SSL_write_alert\r\n");
  685. break;
  686. case SSL_CB_HANDSHAKE_START:
  687. printf("SSL_handshake:%s %s\r\n",
  688. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  689. break;
  690. case SSL_CB_HANDSHAKE_DONE:
  691. printf("SSL_handshake:%s %s\r\n",
  692. SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  693. break;
  694. }
  695. }
  696. #ifdef USE_CERT_CB
  697. /* Return 1, client cert is available */
  698. /* Return 0, no client cert is available */
  699. /* Return -1, callback must be called again. SSL_want_x509_lookup() == 1 */
  700. int
  701. #ifdef CK_ANSIC
  702. ssl_client_cert_callback(SSL * s, X509 ** x509, EVP_PKEY ** pkey)
  703. #else /* CK_ANSIC */
  704. ssl_client_cert_callback(s, x509, pkey)
  705. SSL * s;
  706. X509 ** x509;
  707. EVP_PKEY ** pkey;
  708. #endif /* CK_ANSIC */
  709. {
  710. setverbosity();
  711. if ( ssl_debug_flag ) {
  712. const char * cipher_list=SSL_get_cipher(s);
  713. printf("ssl_client_cert_callback called (%s)\r\n",
  714. cipher_list?cipher_list:"UNKNOWN");
  715. }
  716. #ifdef COMMENT
  717. if ( s == tls_con ) {
  718. if (tls_load_certs(tls_cts,tls_con,0)) {
  719. *x509 = SSL_get_certificate(s);
  720. *pkey = SSL_get_privatekey(s);
  721. return(1);
  722. }
  723. } else if ( s == ssl_con ) {
  724. if (tls_load_certs(ssl_ctx,ssl_con,0)) {
  725. *x509 = SSL_get_certificate(s);
  726. *pkey = SSL_get_privatekey(s);
  727. return(1);
  728. }
  729. }
  730. return(0);
  731. #else /* COMMENT */
  732. return(0);
  733. #endif /* COMMENT */
  734. }
  735. #endif /* USE_CERT_CB */
  736. #ifndef MS_CALLBACK
  737. #define MS_CALLBACK
  738. #endif /* MS_CALLBACK */
  739. static RSA MS_CALLBACK *
  740. #ifdef CK_ANSIC
  741. tmp_rsa_cb(SSL * s, int export, int keylength)
  742. #else /* CK_ANSIC */
  743. tmp_rsa_cb(s,export,keylength)
  744. SSL *s;
  745. int export;
  746. int keylength;
  747. #endif /* CK_ANSIC */
  748. {
  749. static RSA *rsa_tmp=NULL;
  750. #ifndef NO_RSA
  751. if (rsa_tmp == NULL)
  752. {
  753. if (ssl_debug_flag)
  754. printf("Generating temporary (%d bit) RSA key...\r\n",keylength);
  755. rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
  756. if (ssl_debug_flag)
  757. printf("\r\n");
  758. }
  759. #else /* NO_RSA */
  760. if (ssl_debug_flag)
  761. printf("Unable to generate temporary RSA key...\r\n");
  762. #endif
  763. return(rsa_tmp);
  764. }
  765. #ifndef NO_DH
  766. static unsigned char dh512_p[]={
  767. 0xE9,0x4E,0x3A,0x64,0xFA,0x65,0x5F,0xA6,0x44,0xC7,0xFC,0xF1,
  768. 0x16,0x8B,0x11,0x11,0x7A,0xF0,0xB2,0x49,0x80,0x56,0xA3,0xF8,
  769. 0x0F,0x7D,0x01,0x68,0x5D,0xF6,0x8A,0xEA,0x8C,0xDD,0x01,0xDC,
  770. 0x43,0x18,0xE0,0xC4,0x89,0x80,0xE6,0x2D,0x44,0x77,0x45,0xFD,
  771. 0xBA,0xFC,0x43,0x35,0x12,0xC0,0xED,0x32,0xD3,0x16,0xEF,0x51,
  772. 0x09,0x44,0xA2,0xDB,
  773. };
  774. static unsigned char dh512_g[]={
  775. 0x05,
  776. };
  777. static unsigned char dh768_p[]={
  778. 0x8B,0x2A,0x8C,0x6C,0x0F,0x87,0xC7,0x34,0xEE,0x2E,0xFB,0x60,
  779. 0x94,0xB3,0xBF,0x95,0xBA,0x84,0x74,0x86,0xEA,0xE0,0xA4,0x33,
  780. 0xE0,0x8F,0x7C,0x79,0x5C,0x62,0xE2,0x91,0xC5,0x6D,0x68,0xB9,
  781. 0x6C,0x5E,0x4E,0x94,0x0C,0x8E,0x56,0x8E,0xEB,0x98,0x7C,0x6E,
  782. 0x0E,0xF2,0xD5,0xAA,0x22,0x27,0x3F,0x0F,0xAF,0x10,0xB5,0x0B,
  783. 0x16,0xCC,0x05,0x27,0xBB,0x58,0x6D,0x61,0x4B,0x2B,0xAB,0xDC,
  784. 0x6A,0x15,0xBC,0x36,0x75,0x4D,0xEC,0xAB,0xFA,0xB6,0xE1,0xB1,
  785. 0x13,0x70,0xD8,0x77,0xCD,0x5E,0x51,0x77,0x81,0x0D,0x77,0x43,
  786. };
  787. static unsigned char dh768_g[]={
  788. 0x05,
  789. };
  790. static unsigned char dh1024_p[]={
  791. 0xA4,0x75,0xCF,0x35,0x00,0xAF,0x3C,0x17,0xCE,0xB0,0xD0,0x52,
  792. 0x43,0xA0,0x0E,0xFA,0xA2,0xC9,0xBE,0x0B,0x76,0x7A,0xD9,0x2E,
  793. 0xF4,0x97,0xAC,0x02,0x24,0x69,0xF6,0x36,0x4F,0xAB,0xCC,0x43,
  794. 0xC1,0x74,0xFF,0xA3,0xD4,0x04,0x0F,0x11,0x2B,0x6D,0x8C,0x47,
  795. 0xC9,0xCF,0x40,0x93,0x9B,0x7D,0x1E,0x52,0x85,0xB2,0x17,0x55,
  796. 0x9C,0xF2,0x41,0x02,0x2A,0x9D,0x5F,0x24,0x22,0xC6,0x04,0xC4,
  797. 0xAB,0x92,0x6D,0xC7,0xC8,0xF3,0x41,0x58,0x6C,0x86,0xFD,0xB8,
  798. 0x0F,0x2D,0xDD,0xBF,0xA8,0x40,0x0C,0x58,0xC8,0xF2,0x3F,0x18,
  799. 0xEF,0xF1,0x93,0x3E,0xBA,0x16,0x41,0xBE,0x32,0x6C,0xC5,0x63,
  800. 0xFF,0x8A,0x02,0x3D,0xAC,0xD5,0x5A,0x49,0x64,0x34,0x14,0x2E,
  801. 0xFB,0x2E,0xE7,0x39,0x1A,0x0F,0x3C,0x33,
  802. };
  803. static unsigned char dh1024_g[]={
  804. 0x05,
  805. };
  806. static unsigned char dh1536_p[]={
  807. 0xA3,0x2B,0x75,0x0E,0x7B,0x31,0x82,0xCA,0xF2,0xFC,0xF3,0x3D,
  808. 0xCE,0x5F,0xCD,0x5B,0x95,0xF6,0x2F,0xA4,0x5D,0x08,0x26,0xD2,
  809. 0x5F,0xC0,0x3F,0xC5,0xD8,0xA2,0xFE,0x83,0x26,0xBC,0xEB,0x7D,
  810. 0xF0,0x4E,0xD2,0xA6,0xBB,0x3C,0x88,0x63,0xCE,0x98,0xDE,0x08,
  811. 0xE2,0xE1,0xAF,0xE2,0x38,0xA8,0xFA,0x68,0x76,0x8D,0xBF,0xDF,
  812. 0xBB,0x30,0x15,0xFE,0xBD,0x22,0xCC,0x03,0x4E,0x5E,0x33,0xA3,
  813. 0x6D,0xD6,0x68,0x12,0x97,0x17,0x4B,0xB5,0x84,0x5F,0x5F,0xA3,
  814. 0x5C,0x2F,0xA4,0x10,0xC1,0xAD,0xBF,0xAC,0x30,0xCA,0x47,0x64,
  815. 0x63,0xFE,0xEE,0xEE,0xA1,0x64,0x73,0x70,0xAA,0xF9,0xFE,0xC6,
  816. 0xAD,0x5E,0xF6,0xF3,0x9C,0xDF,0x34,0x53,0x34,0x72,0xA6,0xA4,
  817. 0xBB,0x81,0x5A,0x43,0x41,0xFD,0x41,0x05,0x5B,0x77,0x7B,0x84,
  818. 0x03,0xFA,0x8A,0xFA,0xF7,0x8E,0x0F,0xCB,0x51,0xA2,0xB8,0x45,
  819. 0xFF,0x59,0x42,0xEF,0xCF,0xF6,0x25,0x37,0xE2,0x6D,0xFF,0x69,
  820. 0x11,0xF5,0x77,0x59,0x79,0x1C,0x5F,0x05,0xFC,0x7A,0x65,0x81,
  821. 0x03,0x4A,0x78,0xC6,0xE9,0x48,0x73,0xF6,0x10,0xBC,0x99,0x1C,
  822. 0xEE,0x44,0x2F,0x8B,0x70,0xCA,0xA8,0xB6,0x02,0x83,0x3E,0x0B,
  823. };
  824. static unsigned char dh1536_g[]={
  825. 0x05,
  826. };
  827. static unsigned char dh2048_p[]={
  828. 0xFA,0x4E,0xE4,0x3B,0xFA,0xC1,0x87,0xDD,0xE7,0xC6,0x8B,0xE6,
  829. 0x13,0x85,0xBC,0x9B,0x2B,0x8B,0x5B,0x46,0xBB,0x8B,0x86,0x6D,
  830. 0xD7,0xB6,0xD5,0x49,0xC5,0x54,0xF2,0x3E,0xD2,0x39,0x64,0x9B,
  831. 0x0E,0x33,0x39,0x8F,0xFA,0xFA,0xD9,0x78,0xED,0x34,0x82,0x29,
  832. 0x37,0x58,0x4D,0x5D,0x40,0xCB,0x69,0xE3,0x8A,0x9F,0x17,0x0C,
  833. 0x01,0x23,0x6B,0x05,0x01,0xAF,0x33,0xDE,0xDF,0x1A,0xBB,0x7B,
  834. 0x6A,0x9F,0xD8,0xED,0x8D,0x5E,0x44,0x19,0x5B,0xE0,0xB6,0x23,
  835. 0xF9,0x7A,0x96,0x6E,0x94,0x33,0x31,0x49,0xBA,0x84,0xD5,0x12,
  836. 0xD7,0x6D,0xDC,0x35,0x54,0x64,0xA3,0xD8,0x04,0x26,0xC5,0xAF,
  837. 0x7F,0xE3,0xFE,0x6F,0xBE,0xD5,0x17,0x72,0x4B,0xA6,0xD0,0xA7,
  838. 0x5F,0x18,0xF5,0xF0,0x2D,0x11,0x9A,0xF6,0xD5,0x3B,0x6C,0x61,
  839. 0x3C,0x6F,0x8E,0x09,0x4F,0x2C,0xE1,0x26,0x06,0x51,0xB3,0x19,
  840. 0x85,0x85,0x13,0xF9,0xC2,0x6E,0x80,0x28,0x9E,0x8A,0xA0,0x01,
  841. 0x46,0xD1,0x85,0x44,0x8C,0xE6,0xEE,0x7E,0x1E,0x17,0x3D,0xBA,
  842. 0x54,0xFF,0xE8,0x0E,0xDD,0x51,0xF3,0x74,0x7F,0x0D,0x0B,0xAB,
  843. 0xCA,0x84,0x8D,0x24,0x5D,0x56,0xD4,0x47,0x02,0xFC,0x93,0x9F,
  844. 0xAE,0x9B,0x5C,0xDB,0x63,0xEB,0x65,0x01,0x38,0xC2,0x7B,0x30,
  845. 0x1E,0x17,0x1C,0x75,0xF5,0x16,0x3B,0x4F,0x5F,0x41,0x32,0xB5,
  846. 0xFF,0x9E,0x61,0xFD,0xD2,0x62,0x6E,0xFD,0x8A,0x28,0x93,0x59,
  847. 0x2D,0x70,0x14,0x4D,0xE1,0x86,0xD5,0x90,0xB4,0xDF,0x72,0x71,
  848. 0xE0,0xB4,0xD0,0xD6,0x82,0x3A,0x4A,0x04,0x58,0x32,0x0B,0xD3,
  849. 0x51,0x13,0x32,0x63,
  850. };
  851. static unsigned char dh2048_g[]={
  852. 0x02,
  853. };
  854. static DH *
  855. get_dh512()
  856. {
  857. DH *dh=NULL;
  858. BIGNUM *p, *g;
  859. if ((dh=DH_new()) == NULL)
  860. return(NULL);
  861. p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  862. g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  863. if ((p == NULL) || (g == NULL))
  864. return(NULL);
  865. DH_set0_pqg(dh, p, NULL, g);
  866. return(dh);
  867. }
  868. static DH *
  869. get_dh768()
  870. {
  871. DH *dh=NULL;
  872. BIGNUM *p, *g;
  873. if ((dh=DH_new()) == NULL)
  874. return(NULL);
  875. p=BN_bin2bn(dh768_p,sizeof(dh768_p),NULL);
  876. g=BN_bin2bn(dh768_g,sizeof(dh768_g),NULL);
  877. if ((p == NULL) || (g == NULL))
  878. return(NULL);
  879. DH_set0_pqg(dh, p, NULL, g);
  880. return(dh);
  881. }
  882. static DH *
  883. get_dh1024()
  884. {
  885. DH *dh=NULL;
  886. BIGNUM *p, *g;
  887. if ((dh=DH_new()) == NULL)
  888. return(NULL);
  889. p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  890. g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  891. if ((p == NULL) || (g == NULL))
  892. return(NULL);
  893. DH_set0_pqg(dh, p, NULL, g);
  894. return(dh);
  895. }
  896. static DH *
  897. get_dh1536()
  898. {
  899. DH *dh=NULL;
  900. BIGNUM *p, *g;
  901. if ((dh=DH_new()) == NULL)
  902. return(NULL);
  903. p=BN_bin2bn(dh1536_p,sizeof(dh1536_p),NULL);
  904. g=BN_bin2bn(dh1536_g,sizeof(dh1536_g),NULL);
  905. if ((p == NULL) || (g == NULL))
  906. return(NULL);
  907. DH_set0_pqg(dh, p, NULL, g);
  908. return(dh);
  909. }
  910. static DH *
  911. get_dh2048()
  912. {
  913. DH *dh=NULL;
  914. BIGNUM *p, *g;
  915. if ((dh=DH_new()) == NULL)
  916. return(NULL);
  917. p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
  918. g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
  919. if ((p == NULL) || (g == NULL))
  920. return(NULL);
  921. DH_set0_pqg(dh, p, NULL, g);
  922. return(dh);
  923. }
  924. #endif /* NO_DH */
  925. static DH MS_CALLBACK *
  926. #ifdef CK_ANSIC
  927. tmp_dh_cb(SSL * s, int export, int keylength)
  928. #else /* CK_ANSIC */
  929. tmp_dh_cb(s,export,keylength)
  930. SSL *s;
  931. int export;
  932. int keylength;
  933. #endif /* CK_ANSIC */
  934. {
  935. static DH *dh_tmp=NULL;
  936. BIO *bio=NULL;
  937. #ifndef NO_DH
  938. if (dh_tmp == NULL)
  939. {
  940. if (ssl_dh_param_file &&
  941. (bio=BIO_new_file(ssl_dh_param_file,"r")) != NULL)
  942. dh_tmp=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  943. if (bio != NULL)
  944. BIO_free(bio);
  945. if ( dh_tmp == NULL ) {
  946. if ( keylength < 768 )
  947. dh_tmp = get_dh512();
  948. else if ( keylength < 1024 )
  949. dh_tmp = get_dh768();
  950. else if ( keylength < 1536 )
  951. dh_tmp = get_dh1024();
  952. else if ( keylength < 2048 )
  953. dh_tmp = get_dh1536();
  954. else
  955. dh_tmp = get_dh2048();
  956. }
  957. }
  958. #else /* NO_DH */
  959. if (ssl_debug_flag)
  960. printf("DH not supported...\r\n");
  961. #endif /* NO_DH */
  962. return(dh_tmp);
  963. }
  964. static void
  965. ssl_display_comp(SSL * ssl)
  966. {
  967. if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
  968. return;
  969. if ( !ck_ssleay_is_installed() )
  970. return;
  971. if (ssl == NULL)
  972. return;
  973. #ifndef OPENSSL_NO_COMP /* ifdefs Bernard Spil 12/2015 */
  974. const COMP_METHOD *x = SSL_get_current_expansion(ssl);
  975. if (!x)
  976. #endif /* OPENSSL_NO_COMP */
  977. printf("Compression: None\r\n");
  978. #ifndef OPENSSL_NO_COMP /* ifdefs Bernard Spil 12/2015 */
  979. else {
  980. printf("Compression: %s\r\n",SSL_COMP_get_name(x));
  981. }
  982. #endif /* OPENSSL_NO_COMP */
  983. }
  984. int
  985. #ifdef CK_ANSIC
  986. ssl_display_connect_details(SSL * ssl_con, int server, int verbose)
  987. #else /* CK_ANSIC */
  988. ssl_display_connect_details(ssl_con,server,verbose)
  989. SSL *ssl_con;
  990. int server;
  991. int verbose;
  992. #endif /* CK_ANSIC */
  993. {
  994. X509 *peer;
  995. SSL_CIPHER * cipher;
  996. const char *cipher_list;
  997. char buf[512]="";
  998. if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
  999. return(0);
  1000. if ( !ck_ssleay_is_installed() )
  1001. return(0);
  1002. if ( inserver && !tn_deb )
  1003. return(0);
  1004. /* the cipher list *can* be NULL ... useless but it happens! */
  1005. cipher = SSL_get_current_cipher(ssl_con);
  1006. cipher_list = SSL_CIPHER_get_name(cipher);
  1007. SSL_CIPHER_description(cipher,buf,sizeof(buf));
  1008. if (cipher_list==NULL)
  1009. cipher_list="<NULL>";
  1010. printf("[TLS - %s",buf);
  1011. ssl_display_comp(ssl_con);
  1012. if ( server ) {
  1013. cipher_list=SSL_get_shared_ciphers(ssl_con,buf,512);
  1014. if (cipher_list==NULL)
  1015. cipher_list="<NULL>";
  1016. printf("[TLS - shared ciphers=%s]\r\n",
  1017. cipher_list);
  1018. }
  1019. if ( server || tn_deb ) {
  1020. peer=SSL_get_peer_certificate(ssl_con);
  1021. if (peer != NULL) {
  1022. X509_NAME_oneline(X509_get_subject_name(peer),buf,512);
  1023. printf("[TLS - subject=%s]\r\n",buf);
  1024. X509_NAME_oneline(X509_get_issuer_name(peer),buf,512);
  1025. printf("[TLS - issuer=%s]\r\n",buf);
  1026. /* X509_free(peer); */
  1027. } else if (!tls_is_krb5(0)) {
  1028. if ( !sstelnet && !tcp_incoming ) {
  1029. printf("[TLS - No certificate provided.]\r\n");
  1030. printf(
  1031. "[TLS - The identity of the host could not be verified.]\r\n");
  1032. }
  1033. }
  1034. }
  1035. return(0);
  1036. }
  1037. /*
  1038. * Use SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void * userdata)
  1039. * to set the value of the userdata. We are going to use it to store the
  1040. * prompt.
  1041. */
  1042. int
  1043. #ifdef CK_ANSIC
  1044. ssl_passwd_callback(char *buf, int len, int rwflag, VOID * userdata)
  1045. #else /* CK_ANSIC */
  1046. ssl_passwd_callback(buf,len,rwflag,userdata)
  1047. char * buf; int len; int rwflag; VOID *userdata;
  1048. #endif /* CK_ANSIC */
  1049. {
  1050. extern char pwbuf[];
  1051. extern int pwflg, pwcrypt;
  1052. int ok;
  1053. char *prompt=NULL;
  1054. if ( pwbuf[0] && pwflg ) {
  1055. int n;
  1056. n = ckstrncpy(buf,pwbuf,len);
  1057. #ifdef OS2
  1058. if ( pwcrypt )
  1059. ck_encrypt((char *)buf);
  1060. #endif /* OS2 */
  1061. return(n);
  1062. }
  1063. if ( userdata == NULL )
  1064. prompt="Enter certificate passphrase: ";
  1065. else
  1066. prompt=(char*)userdata;
  1067. ok = uq_txt(NULL,prompt,2,NULL,buf,len,NULL,DEFAULT_UQ_TIMEOUT);
  1068. return(ok > 0 ? strlen(buf) : 0);
  1069. }
  1070. /* Attempts to load certificate data into the TLS context structures */
  1071. /* Returns 1 on success; 0 on failure */
  1072. int
  1073. tls_load_certs(SSL_CTX * ctx, SSL * con, int server)
  1074. {
  1075. int rc = 1;
  1076. if ( !ck_ssleay_is_installed() )
  1077. return(0);
  1078. debug(F110,"tls_load_certs","SSL_CTX",0);
  1079. debug(F110,"tls_load_certs","SSL",0);
  1080. debug(F110,"tls_load_certs","server",0);
  1081. if ( con ) {
  1082. if (ssl_rsa_cert_file) {
  1083. if ( ssl_debug_flag )
  1084. printf("Loading RSA certificate into SSL\r\n");
  1085. rc = SSL_use_certificate_file(con, ssl_rsa_cert_file,
  1086. X509_FILETYPE_PEM);
  1087. if (!rc)
  1088. {
  1089. if ( !quiet || ssl_debug_flag )
  1090. printf("Error loading certificate from %s\r\n",
  1091. ssl_rsa_cert_file);
  1092. } else {
  1093. if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
  1094. makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
  1095. rc = SSL_use_PrivateKey_file(con, ssl_rsa_key_file,
  1096. X509_FILETYPE_PEM);
  1097. if (!rc)
  1098. rc = SSL_use_PrivateKey_file(con, ssl_rsa_cert_file,
  1099. X509_FILETYPE_PEM);
  1100. if (!rc)
  1101. {
  1102. if ( !quiet || ssl_debug_flag )
  1103. printf("Error loading key from %s\r\n",
  1104. ssl_rsa_key_file);
  1105. } else {
  1106. rc = SSL_check_private_key(con);
  1107. if (!rc)
  1108. {
  1109. if ( ssl_debug_flag )
  1110. printf(
  1111. "Private key does not match the certificate public key\r\n");
  1112. }
  1113. }
  1114. }
  1115. }
  1116. if (ssl_dsa_cert_file) {
  1117. if ( ssl_debug_flag )
  1118. printf("Loading DSA certificate into SSL\r\n");
  1119. rc = SSL_use_certificate_file(con, ssl_dsa_cert_file,
  1120. X509_FILETYPE_PEM);
  1121. if (!rc)
  1122. {
  1123. if ( ssl_debug_flag ) {
  1124. printf("Error loading certificate from %s\r\n",
  1125. ssl_dsa_cert_file);
  1126. }
  1127. } else {
  1128. if (!ssl_dh_key_file || !ssl_dh_key_file[0])
  1129. makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
  1130. rc = SSL_use_PrivateKey_file(con, ssl_dh_key_file,
  1131. X509_FILETYPE_PEM);
  1132. if (!rc)
  1133. rc = SSL_use_PrivateKey_file(con, ssl_dsa_cert_file,
  1134. X509_FILETYPE_PEM);
  1135. if (!rc)
  1136. {
  1137. if ( !quiet || ssl_debug_flag ) {
  1138. printf("Error loading key from %s\r\n",
  1139. ssl_dh_key_file);
  1140. }
  1141. } else {
  1142. rc = SSL_check_private_key(con);
  1143. if (!rc)
  1144. {
  1145. if ( ssl_debug_flag )
  1146. printf(
  1147. "Private key does not match the certificate public key\n");
  1148. }
  1149. }
  1150. }
  1151. }
  1152. } else {
  1153. if (ssl_rsa_cert_file) {
  1154. if ( ssl_debug_flag )
  1155. printf("Loading RSA certificate into SSL\r\n");
  1156. rc = SSL_CTX_use_certificate_file(ctx, ssl_rsa_cert_file,
  1157. X509_FILETYPE_PEM);
  1158. if (!rc)
  1159. {
  1160. if ( !quiet || ssl_debug_flag )
  1161. printf("Error loading certificate from %s\r\n",
  1162. ssl_rsa_cert_file);
  1163. } else {
  1164. if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
  1165. makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
  1166. rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_key_file,
  1167. X509_FILETYPE_PEM);
  1168. if (!rc)
  1169. rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_cert_file,
  1170. X509_FILETYPE_PEM);
  1171. if (!rc) {
  1172. if ( ssl_debug_flag )
  1173. printf("Error loading key from %s\r\n",ssl_rsa_key_file);
  1174. } else {
  1175. rc = SSL_CTX_check_private_key(ctx);
  1176. if (!rc) {
  1177. if ( ssl_debug_flag )
  1178. printf(
  1179. "Private key does not match the certificate public key\r\n");
  1180. }
  1181. }
  1182. }
  1183. }
  1184. if (ssl_dsa_cert_file) {
  1185. if ( ssl_debug_flag )
  1186. printf("Loading DSA certificate into SSL\r\n");
  1187. rc = SSL_CTX_use_certificate_file(ctx, ssl_dsa_cert_file,
  1188. X509_FILETYPE_PEM);
  1189. if (!rc) {
  1190. if ( ssl_debug_flag ) {
  1191. printf("Error loading certificate from %s\r\n",
  1192. ssl_dsa_cert_file);
  1193. }
  1194. } else {
  1195. if (!ssl_dh_key_file || !ssl_dh_key_file[0])
  1196. makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
  1197. rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dh_key_file,
  1198. X509_FILETYPE_PEM);
  1199. if (!rc)
  1200. rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dsa_cert_file,
  1201. X509_FILETYPE_PEM);
  1202. if (!rc) {
  1203. if ( ssl_debug_flag )
  1204. printf("Error loading key from %s\r\n",ssl_dh_key_file);
  1205. } else {
  1206. rc = SSL_CTX_check_private_key(ctx);
  1207. if (!rc) {
  1208. if ( ssl_debug_flag )
  1209. printf(
  1210. "Private key does not match the certificate public key\n");
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. if (ssl_rsa_cert_chain_file && server) {
  1217. int skip1st = 0;
  1218. if (ssl_debug_flag)
  1219. printf("Loading RSA Certificate Chain into SSL\r\n");
  1220. if (!ckstrcmp(ssl_rsa_cert_chain_file,ssl_rsa_cert_file,-1,
  1221. #ifdef OS2
  1222. 0
  1223. #else
  1224. 1
  1225. #endif /* OS2 */
  1226. ))
  1227. skip1st = 1;
  1228. rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_rsa_cert_chain_file);
  1229. if (!rc && ssl_debug_flag)
  1230. printf("Error loading RSA Certificate Chain into SSL\r\n");
  1231. }
  1232. if (ssl_dsa_cert_chain_file && server) {
  1233. int skip1st = 0;
  1234. if (ssl_debug_flag)
  1235. printf("Loading DSA Certificate Chain into SSL\r\n");
  1236. if (!ckstrcmp(ssl_dsa_cert_chain_file,ssl_dsa_cert_file,-1,
  1237. #ifdef OS2
  1238. 0
  1239. #else
  1240. 1
  1241. #endif /* OS2 */
  1242. ))
  1243. skip1st = 1;
  1244. rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_dsa_cert_chain_file);
  1245. if (!rc && ssl_debug_flag)
  1246. printf("Error loading DSA Certificate Chain into SSL\r\n");
  1247. }
  1248. return(rc);
  1249. }
  1250. VOID
  1251. #ifdef CK_ANSIC
  1252. ssl_once_init(void)
  1253. #else
  1254. ssl_once_init()
  1255. #endif /* CK_ANSIC */
  1256. {
  1257. COMP_METHOD * cm;
  1258. char * s;
  1259. if ( !ck_ssleay_is_installed() )
  1260. return;
  1261. /*
  1262. Pre-OpenSSL 1.0.0 comment:
  1263. OpenSSL does not provide for ABI compatibility between releases prior
  1264. to version 1.0.0. If the version does not match, it is not safe to
  1265. assume that any function you call takes the same parameters or does
  1266. the same thing with them. Removing this test prior to the OpenSSL 1.0.0
  1267. release will result in an increase in unexplained or incorrect behaviors.
  1268. The test should be revised once OpenSSL 1.0.0 is released and we see what
  1269. its claims are as to ABI compatibility.
  1270. */
  1271. /*
  1272. Post-OpenSSL 1.0.0 comment:
  1273. OpenSSL does not provide for ABI compatibility between releases prior
  1274. to version 1.0.0. After 1.0, the following holds:
  1275. Changes to last letter: security and bugfix only, no new features.
  1276. E.g. 1.0.0->1.0.0a
  1277. Changes to last number: new ABI compatible features.
  1278. E.g. 1.0.0->1.0.1
  1279. Changes to middle number: major release, ABI compatibility not guaranteed.
  1280. E.g. 1.0.0->1.1.0
  1281. (per Dr. Stephen Henson)
  1282. */
  1283. debug(F111,"Kermit built for OpenSSL",OPENSSL_VERSION_TEXT,SSLEAY_VERSION_NUMBER);
  1284. #ifndef OS2ONLY
  1285. debug(F111,"OpenSSL Library",SSLeay_version(SSLEAY_VERSION),
  1286. SSLeay());
  1287. debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_BUILT_ON),0);
  1288. debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_CFLAGS),0);
  1289. debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_PLATFORM),0);
  1290. /* The following test is suggested by Richard Levitte */
  1291. /* if (((OPENSSL_VERSION_NUMBER ^ SSLeay()) & 0xffffff0f) */
  1292. /* Modified by Adam Friedlander for OpenSSL >= 1.0.0 */
  1293. if (OPENSSL_VERSION_NUMBER > SSLeay()
  1294. || ((OPENSSL_VERSION_NUMBER ^ SSLeay()) & COMPAT_VERSION_MASK)
  1295. #ifdef OS2
  1296. || ckstrcmp(OPENSSL_VERSION_TEXT,(char *)SSLeay_version(SSLEAY_VERSION),-1,1)
  1297. #endif /* OS2 */
  1298. ) {
  1299. ssl_installed = 0;
  1300. debug(F111,"OpenSSL Version does not match. Built with",
  1301. SSLeay_version(SSLEAY_VERSION),SSLEAY_VERSION_NUMBER);
  1302. printf("?OpenSSL libraries do not match required version:\r\n");
  1303. printf(" . C-Kermit built with %s\r\n",OPENSSL_VERSION_TEXT);
  1304. printf(" . Version found %s\r\n",SSLeay_version(SSLEAY_VERSION));
  1305. #ifdef OPENSSL_100
  1306. printf(" OpenSSL versions 1.0.0 or newer must be the same\r\n");
  1307. printf(" major and minor version number, and Kermit may not\r\n");
  1308. printf(" be used with a version of OpenSSL older than the one\r\n");
  1309. printf(" supplied at compile time.\r\n");
  1310. #else
  1311. printf(" OpenSSL versions prior to 1.0.0 must be the same.\r\n");
  1312. #endif /* OPENSSL_100 */
  1313. s = "R";
  1314. #ifdef SOLARIS
  1315. printf(" Set CD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
  1316. s = " Or r";
  1317. #endif /* SOLARIS */
  1318. #ifdef HPUX
  1319. printf(" Set SHLIB_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
  1320. s = " Or r";
  1321. #endif /* HPUX */
  1322. #ifdef AIX
  1323. printf(" Set LIBPATH for %s.\r\n",OPENSSL_VERSION_TEXT);
  1324. s = " Or r";
  1325. #endif /* AIX */
  1326. #ifdef LINUX
  1327. printf(" Set LD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
  1328. s = " Or r";
  1329. #endif /* LINUX */
  1330. printf(" %sebuild C-Kermit from source on this computer to make \
  1331. versions agree.\r\n",s);
  1332. #ifdef KTARGET
  1333. {
  1334. char * s;
  1335. s = KTARGET;
  1336. if (!s) s = "";
  1337. if (!*s) s = "(unknown)";
  1338. printf(" C-Kermit makefile target: %s\r\n",s);
  1339. }
  1340. #endif /* KTARGET */
  1341. printf(" Or if that is what you did then try to find out why\r\n");
  1342. printf(" the program loader (image activator) is choosing a\r\n");
  1343. printf(" different OpenSSL library than the one specified in \
  1344. the build.\r\n\r\n");
  1345. printf(" All SSL/TLS features disabled.\r\n\r\n");
  1346. bleep(BP_FAIL);
  1347. #ifdef SSLDLL
  1348. ck_ssl_unloaddll();
  1349. ck_crypto_unloaddll();
  1350. #endif /* SSLDLL */
  1351. return;
  1352. }
  1353. #endif /* OS2ONLY */
  1354. /* init things so we will get meaningful error messages
  1355. * rather than numbers
  1356. */
  1357. SSL_load_error_strings();
  1358. #ifdef SSHBUILTIN
  1359. OPENSSL_add_all_algorithms_noconf();
  1360. #else
  1361. /* SSL_library_init() only loads those ciphers needs for SSL */
  1362. /* These happen to be a similar set to those required for SSH */
  1363. /* but they are not a complete set of ciphers provided by the */
  1364. /* crypto library. */
  1365. SSL_library_init();
  1366. #endif /* SSHBUILTIN */
  1367. #ifdef ZLIB
  1368. cm = COMP_zlib();
  1369. if (cm != NULL && COMP_get_type(cm) != NID_undef) {
  1370. SSL_COMP_add_compression_method(0xe0, cm); /* EAY's ZLIB ID */
  1371. }
  1372. #endif /* ZLIB */
  1373. #if 0 /* COMP_rle has apparently been removed in OpenSSL 1.1 */
  1374. cm = COMP_rle();
  1375. if (cm != NULL && cm->type != NID_undef)
  1376. SSL_COMP_add_compression_method(0xe1, cm); /* EAY's RLE ID */
  1377. #endif
  1378. /* Ensure the Random number generator has enough entropy */
  1379. if ( !RAND_status() ) {
  1380. char buffer[256]="";
  1381. char randombytes[256];
  1382. int rc1 = -1, rc2 = 1; /* assume failure and success */
  1383. debug(F110,"ssl_once_init","!RAND_status()",0);
  1384. if ( ssl_rnd_file == NULL ) {
  1385. debug(F110,"ssl_rnd_file","ssl_rnd_file is NULL",0);
  1386. RAND_file_name(buffer,256);
  1387. if ( buffer[0] )
  1388. makestr(&ssl_rnd_file, buffer);
  1389. else
  1390. makestr(&ssl_rnd_file,".rnd");
  1391. }
  1392. debug(F110,"ssl_rnd_file",ssl_rnd_file,0);
  1393. #ifndef OPENSSL_NO_EGD /* ifdef Bernard Spil 12/2015 */
  1394. rc1 = RAND_egd(ssl_rnd_file);
  1395. debug(F111,"ssl_once_init","RAND_egd()",rc1);
  1396. if ( rc1 <= 0 ) {
  1397. rc2 = RAND_load_file(ssl_rnd_file, -1);
  1398. debug(F111,"ssl_once_init","RAND_load_file()",rc1);
  1399. }
  1400. #endif /* OPENSSL_NO_EGD */
  1401. if ( rc1 <= 0 && !rc2 )
  1402. {
  1403. time_t t = time(NULL);
  1404. int tlen = sizeof(time_t);
  1405. int pid = getpid();
  1406. int plen = sizeof(int);
  1407. int n;
  1408. #ifndef RAND_MAX
  1409. #define RAND_MAX 0x7FFF
  1410. #endif
  1411. debug(F110,"ssl_once_init","calling RAND_seed()",0);
  1412. RAND_seed((unsigned char *)&t, tlen);
  1413. RAND_seed((unsigned char *)&pid, plen);
  1414. srand((unsigned int)t);
  1415. sprintf(buffer, "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*
  1416. (sizeof(randombytes)-128-1)));
  1417. n = (atoi(buffer)+1)%(sizeof(randombytes)-128-1);
  1418. RAND_seed(randombytes, 128);
  1419. }
  1420. if ( !RAND_status() ) {
  1421. debug(F110,"ssl_once_init","Unable to initialize PRNG",0);
  1422. printf(" Unable to load 'random state'\n");
  1423. printf(" SSL and TLS are unavailble.\n");
  1424. printf(" Use SET AUTH SSL RANDOM-FILE <file> command to provide random data.\n");
  1425. printf(" Specified file will be overwritten with new random data after use.\n");
  1426. return;
  1427. }
  1428. if ( ssl_rnd_file ) {
  1429. int rc = RAND_write_file(ssl_rnd_file);
  1430. debug(F111,"ssl_once_init","RAND_write_file()",rc);
  1431. }
  1432. }
  1433. #ifdef NT
  1434. // Initialize additional OID types for use when saving certs to a file
  1435. OBJ_create("2.99999.3","SET.ex3","SET x509v3 extension 3");
  1436. #endif /* NT */
  1437. /* make sure we have somewhere we can log errors to */
  1438. bio_err=BIO_new(BIO_s_mem());
  1439. debug(F100,"ssl_once_init() complete","",0);
  1440. }
  1441. int
  1442. #ifdef CK_ANSIC
  1443. ssl_tn_init(int mode)
  1444. #else
  1445. ssl_tn_init(mode) int mode;
  1446. #endif /* CK_ANSIC */
  1447. {
  1448. #ifdef KRB5
  1449. extern char * k5_keytab;
  1450. extern char * krb5_d_srv;
  1451. #endif /* KRB5 */
  1452. static int last_ssl_mode = -1;
  1453. SSL * ssl_conx=NULL, * tls_conx=NULL;
  1454. ssl_initialized = 0;
  1455. if ( !ck_ssleay_is_installed() )
  1456. return(0);
  1457. debug(F111,"ssl_tn_init","mode",mode);
  1458. if (ssl_debug_flag)
  1459. printf("SSL_DEBUG_FLAG on\r\n");
  1460. if (last_ssl_mode != mode) {
  1461. if (ssl_ctx) {
  1462. SSL_CTX_free(ssl_ctx);
  1463. ssl_ctx = NULL;
  1464. }
  1465. if (tls_ctx) {
  1466. SSL_CTX_free(tls_ctx);
  1467. tls_ctx = NULL;
  1468. }
  1469. }
  1470. if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
  1471. if ( mode == SSL_CLIENT ) {
  1472. ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
  1473. /* This can fail because we do not have RSA available */
  1474. if ( !ssl_ctx ) {
  1475. debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
  1476. #ifndef OPENSSL_NO_SSL3 /* ifdef Bernard Spil 12/2015 */
  1477. ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
  1478. #endif /* OPENSSL_NO_SSL3 */
  1479. }
  1480. if ( !ssl_ctx ) {
  1481. debug(F110,"ssl_tn_init","SSLv3_client_method failed",0);
  1482. last_ssl_mode = -1;
  1483. return(0);
  1484. }
  1485. /*
  1486. TLS 1.0 is the new default as of 5 Feb 2015.
  1487. Previously this was commented out because
  1488. "too many web servers still do not support TLSv1".
  1489. Now we try TLS 1.0 first, falling back to SSL 2.3
  1490. and SSL 3.0 in that order. Maybe there should be
  1491. an option not to fall back.
  1492. */
  1493. tls_ctx=(SSL_CTX *)SSL_CTX_new(TLS_client_method());
  1494. if ( tls_ctx ) {
  1495. debug(F110,"ssl_tn_init","TLS_client_method OK",0);
  1496. } else {
  1497. debug(F110,"ssl_tn_init","TLS_client_method failed",0);
  1498. /* This can fail because we do not have RSA available */
  1499. tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
  1500. if ( !tls_ctx ) {
  1501. debug(F110,"ssl_tn_init","SSLv23_client_method OK",0);
  1502. } else {
  1503. debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
  1504. #ifndef OPENSSL_NO_SSL3 /* ifdef Bernard Spil 12/2015 */
  1505. tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
  1506. #endif /* OPENSSL_NO_SSL3 */
  1507. if ( !tls_ctx ) {
  1508. debug(F110,
  1509. "ssl_tn_init","TLS_client_method failed",0);
  1510. debug(F110,
  1511. "ssl_tn_init","All SSL client methods failed",0);
  1512. last_ssl_mode = -1;
  1513. return(0);
  1514. }
  1515. }
  1516. }
  1517. #ifdef USE_CERT_CB
  1518. SSL_CTX_set_client_cert_cb(ssl_ctx,ssl_client_cert_callback);
  1519. SSL_CTX_set_client_cert_cb(tls_ctx,ssl_client_cert_callback);
  1520. #endif /* USE_CERT_CB */
  1521. } else if (mode == SSL_SERVER) {
  1522. /* We are a server */
  1523. ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
  1524. /* This can fail because we do not have RSA available */
  1525. if ( !ssl_ctx ) {
  1526. debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
  1527. #ifndef OPENSSL_NO_SSL3 /* ifdef Bernard Spil 12/2015 */
  1528. ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_server_method());
  1529. #endif /* OPENSSL_NO_SSL3 */
  1530. }
  1531. if ( !ssl_ctx ) {
  1532. debug(F110,"ssl_tn_init","SSLv3_server_method failed",0);
  1533. last_ssl_mode = -1;
  1534. return(0);
  1535. }
  1536. #ifdef COMMENT
  1537. tls_ctx=(SSL_CTX *)SSL_CTX_new(TLS_server_method());
  1538. #else /* COMMENT */
  1539. tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
  1540. /* This can fail because we do not have RSA available */
  1541. if ( !tls_ctx ) {
  1542. debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
  1543. tls_ctx=(SSL_CTX *)SSL_CTX_new(TLS_server_method());
  1544. }
  1545. #endif /* COMMENT */
  1546. if ( !tls_ctx ) {
  1547. debug(F110,"ssl_tn_init","TLS_server_method failed",0);
  1548. last_ssl_mode = -1;
  1549. return(0);
  1550. }
  1551. } else /* Unknown mode */
  1552. return(0);
  1553. if ( !inserver ) {
  1554. SSL_CTX_set_default_passwd_cb(ssl_ctx,
  1555. (pem_password_cb *)ssl_passwd_callback);
  1556. SSL_CTX_set_default_passwd_cb(tls_ctx,
  1557. (pem_password_cb *)ssl_passwd_callback);
  1558. }
  1559. /* for SSL switch on all the interoperability and bug
  1560. * workarounds so that we will communicate with people
  1561. * that cannot read poorly written specs :-)
  1562. * for TLS be sure to prevent use of SSLv2
  1563. */
  1564. SSL_CTX_set_options(ssl_ctx,SSL_OP_ALL|SSL_OP_NO_SSLv2);
  1565. SSL_CTX_set_options(tls_ctx,
  1566. SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
  1567. SSL_CTX_set_info_callback(ssl_ctx,ssl_client_info_callback);
  1568. SSL_CTX_set_info_callback(tls_ctx,ssl_client_info_callback);
  1569. #ifndef COMMENT
  1570. /* Set the proper caching mode */
  1571. if ( mode == SSL_SERVER ) {
  1572. SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_SERVER);
  1573. SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_SERVER);
  1574. } else {
  1575. SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_CLIENT);
  1576. SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_CLIENT);
  1577. }
  1578. SSL_CTX_set_session_id_context(ssl_ctx,(CHAR *)"1",1);
  1579. SSL_CTX_set_session_id_context(tls_ctx,(CHAR *)"2",1);
  1580. #else /* COMMENT */
  1581. SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_OFF);
  1582. SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_OFF);
  1583. #endif /* COMMENT */
  1584. }
  1585. /* The server uses defaults for the certificate files. */
  1586. /* The client does not. */
  1587. if (mode == SSL_SERVER) {
  1588. char cert_filepath[1024];
  1589. const char * defdir = NULL;
  1590. DH * dh = NULL;
  1591. defdir = getenv("SSL_CERT_DIR");
  1592. if ( !defdir ) {
  1593. #ifdef OS2
  1594. defdir = exedir;
  1595. #else /* OS2 */
  1596. defdir = X509_get_default_cert_dir();
  1597. #endif /* OS2 */
  1598. debug(F110,"ssl_tn_init - setting default directory to",defdir,0);
  1599. }
  1600. if ( !defdir )
  1601. defdir = "";
  1602. if (!ssl_rsa_cert_file) {
  1603. /* we need to know the fullpath to the location of the
  1604. * certificate that we will be running with as we cannot
  1605. * be sure of the cwd when we are launched
  1606. */
  1607. sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa.pem");
  1608. if (zchki(cert_filepath) > 0)
  1609. makestr(&ssl_rsa_cert_file,cert_filepath);
  1610. }
  1611. if (ssl_rsa_cert_file && !ssl_rsa_key_file) {
  1612. /* we need to know the fullpath to the location of the
  1613. * certificate that we will be running with as we cannot
  1614. * be sure of the cwd when we are launched
  1615. */
  1616. sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa-key.pem");
  1617. if (zchki(cert_filepath) > 0)
  1618. makestr(&ssl_rsa_key_file,cert_filepath);
  1619. }
  1620. if (!ssl_dsa_cert_file) {
  1621. /* we need to know the fullpath to the location of the
  1622. * certificate that we will be running with as we cannot
  1623. * be sure of the cwd when we are launched
  1624. */
  1625. sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa.pem");
  1626. if (zchki(cert_filepath) > 0)
  1627. makestr(&ssl_dsa_cert_file,cert_filepath);
  1628. }
  1629. if (ssl_dsa_cert_file && !ssl_dh_key_file) {
  1630. /* we need to know the fullpath to the location of the
  1631. * certificate that we will be running with as we cannot
  1632. * be sure of the cwd when we are launched
  1633. */
  1634. sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa-key.pem");
  1635. if (zchki(cert_filepath) > 0)
  1636. makestr(&ssl_dh_key_file,cert_filepath);
  1637. }
  1638. if (!ssl_crl_dir) {
  1639. /* we need to know the fullpath to the location of the
  1640. * certificate that we will be running with as we cannot
  1641. * be sure of the cwd when we are launched
  1642. */
  1643. sprintf(cert_filepath,"%s/crl",defdir);
  1644. if (zchki(cert_filepath) > 0)
  1645. makestr(&ssl_crl_dir,cert_filepath);
  1646. }
  1647. if (ssl_only_flag && !tls_load_certs(ssl_ctx,ssl_con,1)) {
  1648. debug(F110,"ssl_tn_init","Unable to load SSL certs",0);
  1649. last_ssl_mode = -1;
  1650. return(0);
  1651. }
  1652. if (tls_only_flag && !tls_load_certs(tls_ctx,tls_con,1)) {
  1653. debug(F110,"ssl_tn_init","Unable to load TLS certs",0);
  1654. last_ssl_mode = -1;
  1655. return(0);
  1656. }
  1657. if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
  1658. /* we may require a temp 512 bit RSA key because of the
  1659. * wonderful way export things work ... if so we generate
  1660. * one now!
  1661. */
  1662. SSL_CTX_set_tmp_rsa_callback(ssl_ctx, tmp_rsa_cb);
  1663. SSL_CTX_set_tmp_dh_callback( ssl_ctx, tmp_dh_cb);
  1664. SSL_CTX_set_tmp_rsa_callback(tls_ctx, tmp_rsa_cb);
  1665. SSL_CTX_set_tmp_dh_callback( tls_ctx, tmp_dh_cb);
  1666. dh = tmp_dh_cb(NULL,0,512);
  1667. SSL_CTX_set_tmp_dh(ssl_ctx,dh);
  1668. SSL_CTX_set_tmp_dh(tls_ctx,dh);
  1669. /* The following code is only called if we are using a
  1670. * certificate with an RSA public key and where the
  1671. * certificate has a key length less than 512 bits or is
  1672. * marked for signing only. This is so we can support
  1673. * the greatest legal privacy level with exportable clients.
  1674. */
  1675. if (SSL_CTX_need_tmp_RSA(ssl_ctx) ||
  1676. SSL_CTX_need_tmp_RSA(tls_ctx))
  1677. {
  1678. RSA *rsa;
  1679. if ( ssl_debug_flag )
  1680. printf("Generating temp (512 bit) RSA key ...\r\n");
  1681. rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);
  1682. if ( ssl_debug_flag )
  1683. printf("Generation of temp (512 bit) RSA key done\r\n");
  1684. if (SSL_CTX_need_tmp_RSA(ssl_ctx)) {
  1685. if (!SSL_CTX_set_tmp_rsa(ssl_ctx,rsa)) {
  1686. if ( ssl_debug_flag )
  1687. printf(
  1688. "Failed to assign generated temp RSA key to SSL!\r\n");
  1689. }
  1690. }
  1691. if (SSL_CTX_need_tmp_RSA(tls_ctx)) {
  1692. if (!SSL_CTX_set_tmp_rsa(tls_ctx,rsa)) {
  1693. if ( ssl_debug_flag )
  1694. printf(
  1695. "Failed to assign generated temp RSA key to TLS!\r\n");
  1696. }
  1697. }
  1698. RSA_free(rsa);
  1699. if ( ssl_debug_flag )
  1700. printf("Assigned temp (512 bit) RSA key\r\n");
  1701. }
  1702. }
  1703. }
  1704. /* make sure we will find certificates in the standard
  1705. * location ... otherwise we don't look anywhere for
  1706. * these things which is going to make client certificate
  1707. * exchange rather useless :-)
  1708. * In OS2, default values for ssl_verify_file and ssl_verify_path.
  1709. */
  1710. #ifdef OS2
  1711. #ifdef NT
  1712. {
  1713. /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  1714. char path[CKMAXPATH];
  1715. ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  1716. if (isdir(path) &&
  1717. SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
  1718. debug(F110,"ssl_tn_init certificate verify dir",path,0);
  1719. if (ssl_debug_flag)
  1720. printf(" Certificate Verification Directory: %s\r\n",path);
  1721. SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1722. }
  1723. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
  1724. if (isdir(path) &&
  1725. SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
  1726. debug(F110,"ssl_tn_init certificate verify dir",path,0);
  1727. if (ssl_debug_flag)
  1728. printf(" Certificate Verification Directory: %s\r\n",path);
  1729. SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1730. }
  1731. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
  1732. if (isdir(path) &&
  1733. SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
  1734. debug(F110,"ssl_tn_init certificate verify dir",path,0);
  1735. if (ssl_debug_flag)
  1736. printf(" Certificate Verification Directory: %s\r\n",path);
  1737. SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1738. }
  1739. ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  1740. if (zchki(path) > 0 &&
  1741. SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
  1742. debug(F110,"ssl_tn_init certificate verify file",path,0);
  1743. if (ssl_debug_flag)
  1744. printf(" Certificate Verification File: %s\r\n",path);
  1745. SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1746. }
  1747. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
  1748. if (zchki(path) > 0 &&
  1749. SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
  1750. debug(F110,"ssl_tn_init certificate verify file",path,0);
  1751. if (ssl_debug_flag)
  1752. printf(" Certificate Verification File: %s\r\n",path);
  1753. SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1754. }
  1755. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
  1756. if (zchki(path) > 0 &&
  1757. SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
  1758. debug(F110,"ssl_tn_init certificate verify file",path,0);
  1759. if (ssl_debug_flag)
  1760. printf(" Certificate Verification File: %s\r\n",path);
  1761. SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1762. }
  1763. }
  1764. #else /* NT */
  1765. {
  1766. /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  1767. char path[CKMAXPATH];
  1768. ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  1769. if (isdir(path) &&
  1770. SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
  1771. debug(F110,"ssl_tn_init certificate verify dir",path,0);
  1772. if (ssl_debug_flag)
  1773. printf(" Certificate Verification Directory: %s\r\n",path);
  1774. SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1775. }
  1776. ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  1777. if (zchki(path) > 0 &&
  1778. SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
  1779. debug(F110,"ssl_tn_init certificate verify file",path,0);
  1780. if (ssl_debug_flag)
  1781. printf(" Certificate Verification File: %s\r\n",path);
  1782. SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1783. }
  1784. }
  1785. #endif /* NT */
  1786. #else /* OS2 */
  1787. SSL_CTX_set_default_verify_paths(ssl_ctx);
  1788. SSL_CTX_set_default_verify_paths(tls_ctx);
  1789. #endif /* OS2 */
  1790. if (ssl_verify_file) {
  1791. if (zchki(ssl_verify_file) > 0 &&
  1792. SSL_CTX_load_verify_locations(tls_ctx,ssl_verify_file,NULL) == 1) {
  1793. debug(F110,"ssl_tn_init certificate verify file",ssl_verify_file,0);
  1794. if (ssl_debug_flag)
  1795. printf(" Certificate Verification File: %s\r\n",ssl_verify_file);
  1796. SSL_CTX_load_verify_locations(ssl_ctx,ssl_verify_file,NULL);
  1797. }
  1798. }
  1799. if (ssl_verify_dir && isdir(ssl_verify_dir)) {
  1800. if (SSL_CTX_load_verify_locations(tls_ctx,NULL,ssl_verify_dir) == 1) {
  1801. debug(F110,"ssl_tn_init certificate verify dir",ssl_verify_dir,0);
  1802. if (ssl_debug_flag)
  1803. printf(" Certificate Verification Directory: %s\r\n",ssl_verify_dir);
  1804. SSL_CTX_load_verify_locations(ssl_ctx,NULL,ssl_verify_dir);
  1805. }
  1806. }
  1807. if (mode == SSL_SERVER) {
  1808. SSL_CTX_set_verify(ssl_ctx,
  1809. ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
  1810. ssl_server_verify_callback);
  1811. SSL_CTX_set_verify(tls_ctx,
  1812. ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
  1813. ssl_server_verify_callback);
  1814. } else {
  1815. SSL_CTX_set_verify(ssl_ctx,ssl_verify_flag,
  1816. ssl_client_verify_callback);
  1817. SSL_CTX_set_verify(tls_ctx,ssl_verify_flag,
  1818. ssl_client_verify_callback);
  1819. }
  1820. /* Free the existing CRL Store */
  1821. if (crl_store) {
  1822. X509_STORE_free(crl_store);
  1823. crl_store = NULL;
  1824. }
  1825. /* set up the new CRL Store */
  1826. crl_store = X509_STORE_new();
  1827. if (crl_store) {
  1828. #ifdef OS2
  1829. char path[CKMAXPATH];
  1830. ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  1831. if (isdir(path) &&
  1832. X509_STORE_load_locations(crl_store,NULL,path) == 1) {
  1833. debug(F110,"ssl_tn_init crl dir",path,0);
  1834. if (ssl_debug_flag)
  1835. printf(" CRL Directory: %s\r\n",path);
  1836. }
  1837. #ifdef NT
  1838. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
  1839. if (isdir(path) &&
  1840. X509_STORE_load_locations(crl_store,NULL,path) == 1) {
  1841. debug(F110,"ssl_tn_init crl dir",path,0);
  1842. if (ssl_debug_flag)
  1843. printf(" CRL Directory: %s\r\n",path);
  1844. }
  1845. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
  1846. if (isdir(path) &&
  1847. X509_STORE_load_locations(crl_store,NULL,path) == 1) {
  1848. debug(F110,"ssl_tn_init crl dir",path,0);
  1849. if (ssl_debug_flag)
  1850. printf(" CRL Directory: %s\r\n",path);
  1851. }
  1852. #endif /* NT */
  1853. ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  1854. if (zchki(path) > 0 &&
  1855. X509_STORE_load_locations(crl_store,path,NULL) == 1) {
  1856. debug(F110,"ssl_tn_init crl file",path,0);
  1857. if (ssl_debug_flag)
  1858. printf(" CRL File: %s\r\n",path);
  1859. }
  1860. #ifdef NT
  1861. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
  1862. if (zchki(path) > 0 &&
  1863. X509_STORE_load_locations(crl_store,path,NULL) == 1) {
  1864. debug(F110,"ssl_tn_init crl file",path,0);
  1865. if (ssl_debug_flag)
  1866. printf(" CRL File: %s\r\n",path);
  1867. }
  1868. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
  1869. if (zchki(path) > 0 &&
  1870. X509_STORE_load_locations(crl_store,path,NULL) == 1) {
  1871. debug(F110,"ssl_tn_init crl file",path,0);
  1872. if (ssl_debug_flag)
  1873. printf(" CRL File: %s\r\n",path);
  1874. }
  1875. #endif /* NT */
  1876. #endif /* OS2 */
  1877. if (ssl_crl_file || ssl_crl_dir) {
  1878. if (ssl_crl_file && zchki(ssl_crl_file) > 0 &&
  1879. X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 1) {
  1880. debug(F110,"ssl_tn_init crl file",ssl_crl_file,0);
  1881. if (ssl_debug_flag)
  1882. printf(" CRL File: %s\r\n",ssl_crl_file);
  1883. }
  1884. if (ssl_crl_dir && isdir(ssl_crl_dir) &&
  1885. X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 1) {
  1886. debug(F110,"ssl_tn_init crl dir",ssl_crl_dir,0);
  1887. if (ssl_debug_flag)
  1888. printf(" CRL Directory: %s\r\n",ssl_crl_dir);
  1889. }
  1890. }
  1891. #ifndef OS2
  1892. else {
  1893. X509_STORE_set_default_paths(crl_store);
  1894. }
  1895. #endif /* OS2 */
  1896. }
  1897. #ifndef COMMENT
  1898. ssl_conx = ssl_con;
  1899. ssl_con=(SSL *)SSL_new(ssl_ctx);
  1900. if ( !ssl_con ) {
  1901. debug(F110,"ssl_tn_init","SSL_new(ssl_con) failed",0);
  1902. last_ssl_mode = -1;
  1903. ssl_con = ssl_conx;
  1904. return(0);
  1905. }
  1906. if (ssl_conx) {
  1907. if ( mode == SSL_CLIENT ) {
  1908. SSL_set_session(ssl_con, SSL_get_session(ssl_conx));
  1909. }
  1910. #ifdef SSL_KRB5
  1911. if (ssl_conx->kssl_ctx) {
  1912. kssl_ctx_free(ssl_conx->kssl_ctx);
  1913. ssl_conx->kssl_ctx = NULL;
  1914. }
  1915. #endif /* SSL_KRB5 */
  1916. SSL_free(ssl_conx);
  1917. ssl_conx = NULL;
  1918. }
  1919. tls_conx = tls_con;
  1920. tls_con=(SSL *)SSL_new(tls_ctx);
  1921. if ( !tls_con ) {
  1922. debug(F110,"ssl_tn_init","SSL_new(tls_con) failed",0);
  1923. last_ssl_mode = -1;
  1924. tls_con = tls_conx;
  1925. return(0);
  1926. }
  1927. if (tls_conx) {
  1928. if ( mode == SSL_CLIENT )
  1929. SSL_set_session(tls_con, SSL_get_session(tls_conx));
  1930. #ifdef SSL_KRB5
  1931. if (tls_conx->kssl_ctx) {
  1932. kssl_ctx_free(tls_conx->kssl_ctx);
  1933. tls_conx->kssl_ctx = NULL;
  1934. }
  1935. #endif /* SSL_KRB5 */
  1936. SSL_free(tls_conx);
  1937. tls_conx = NULL;
  1938. }
  1939. #else /* COMMENT */
  1940. /* I don't know why this does not work to reuse the connection. */
  1941. if ( ssl_con ) {
  1942. SSL_clear(ssl_con);
  1943. SSL_set_session(ssl_con,NULL);
  1944. SSL_set_accept_state(ssl_con) ;
  1945. } else {
  1946. ssl_con=(SSL *)SSL_new(ssl_ctx);
  1947. if (!ssl_con) {
  1948. debug(F110,"ssl_tn_init","SSL_new(ssl_ctx) failed",0);
  1949. last_ssl_mode = -1;
  1950. ssl_con = ssl_conx;
  1951. return(0);
  1952. }
  1953. }
  1954. if ( tls_con ) {
  1955. SSL_clear(tls_con);
  1956. SSL_set_session(tls_con,NULL);
  1957. SSL_set_accept_state(tls_con) ;
  1958. } else {
  1959. tls_con=(SSL *)SSL_new(tls_ctx);
  1960. if ( !tls_con ) {
  1961. debug(F110,"ssl_tn_init","SSL_new(tls_ctx) failed",0);
  1962. last_ssl_mode = -1;
  1963. tls_con = tls_conx;
  1964. return(0);
  1965. }
  1966. }
  1967. #endif /* COMMENT */
  1968. #ifdef SSL_KRB5
  1969. #ifndef KRB5_SERVICE_NAME
  1970. #define KRB5_SERVICE_NAME "host"
  1971. #endif
  1972. if (ssl_con->kssl_ctx == NULL)
  1973. ssl_con->kssl_ctx = kssl_ctx_new();
  1974. if (tls_con->kssl_ctx == NULL)
  1975. tls_con->kssl_ctx = kssl_ctx_new();
  1976. if (mode == SSL_SERVER) {
  1977. if (ssl_con->kssl_ctx != NULL)
  1978. kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
  1979. if (tls_con->kssl_ctx != NULL)
  1980. kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
  1981. } else {
  1982. if (ssl_con->kssl_ctx != NULL)
  1983. kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVER, szHostName);
  1984. if (tls_con->kssl_ctx != NULL)
  1985. kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVER, szHostName);
  1986. }
  1987. kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVICE,
  1988. krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  1989. kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVICE,
  1990. krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  1991. #endif /* SSL_KRB5 */
  1992. if (ssl_cipher_list) {
  1993. SSL_set_cipher_list(ssl_con,ssl_cipher_list);
  1994. SSL_set_cipher_list(tls_con,ssl_cipher_list);
  1995. } else {
  1996. char * p;
  1997. if (p = getenv("SSL_CIPHER")) {
  1998. SSL_set_cipher_list(ssl_con,p);
  1999. SSL_set_cipher_list(tls_con,p);
  2000. } else {
  2001. SSL_set_cipher_list(ssl_con,DEFAULT_CIPHER_LIST);
  2002. SSL_set_cipher_list(tls_con,DEFAULT_CIPHER_LIST);
  2003. }
  2004. }
  2005. ssl_verify_depth = -1;
  2006. if ( ssl_debug_flag )
  2007. printf("SSL/TLS init done!\r\n");
  2008. ssl_initialized = 1;
  2009. last_ssl_mode = mode;
  2010. debug(F110,"ssl_tn_init","done",0);
  2011. return(1);
  2012. }
  2013. #ifndef NOHTTP
  2014. int
  2015. #ifdef CK_ANSIC
  2016. ssl_http_init(char * hostname)
  2017. #else
  2018. ssl_http_init(hostname) char * hostname;
  2019. #endif /* CK_ANSIC */
  2020. {
  2021. #ifdef KRB5
  2022. extern char * k5_keytab;
  2023. extern char * krb5_d_srv;
  2024. #endif /* KRB5 */
  2025. SSL * tls_conx=NULL;
  2026. ssl_http_initialized = 0;
  2027. if ( !ck_ssleay_is_installed() )
  2028. return(0);
  2029. debug(F110,"ssl_http_init",hostname,0);
  2030. if (ssl_debug_flag)
  2031. printf("SSL_DEBUG_FLAG on\r\n");
  2032. if (!tls_http_ctx ) {
  2033. /*
  2034. TLS 1.0 is the new default as of 5 Feb 2015.
  2035. Previously this was commented out because
  2036. "too many web servers still do not support TLSv1".
  2037. Now we try TLS 1.0 first, falling back to SSL 2.3
  2038. and SSL 3.0 in that order. Maybe there should be
  2039. an option not to fall back.
  2040. */
  2041. tls_http_ctx=(SSL_CTX *)SSL_CTX_new(TLS_client_method());
  2042. if ( tls_http_ctx ) {
  2043. debug(F110,"ssl_http_init","TLS_client_method OK",0);
  2044. }
  2045. }
  2046. SSL_CTX_set_default_passwd_cb(tls_http_ctx,
  2047. (pem_password_cb *)ssl_passwd_callback);
  2048. /* for SSL switch on all the interoperability and bug
  2049. * workarounds so that we will communicate with people
  2050. * who cannot read poorly written specs :-)
  2051. * for TLS be sure to prevent use of SSLv2
  2052. */
  2053. SSL_CTX_set_options(tls_http_ctx,
  2054. SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
  2055. SSL_CTX_set_info_callback(tls_http_ctx,ssl_client_info_callback);
  2056. #ifndef COMMENT
  2057. SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_CLIENT);
  2058. SSL_CTX_set_session_id_context(tls_http_ctx,(CHAR *)"3",1);
  2059. #else /* COMMENT */
  2060. SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_OFF);
  2061. #endif /* COMMENT */
  2062. /* make sure we will find certificates in the standard
  2063. * location ... otherwise we don't look anywhere for
  2064. * these things which is going to make client certificate
  2065. * exchange rather useless :-)
  2066. */
  2067. #ifdef OS2
  2068. #ifdef NT
  2069. {
  2070. /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  2071. char path[CKMAXPATH];
  2072. ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  2073. if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
  2074. debug(F110,"ssl_http_init unable to load path",path,0);
  2075. if (ssl_debug_flag)
  2076. printf("?Unable to load verify-dir: %s\r\n",path);
  2077. }
  2078. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
  2079. if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
  2080. debug(F110,"ssl_http_init unable to load path",path,0);
  2081. if (ssl_debug_flag)
  2082. printf("?Unable to load verify-dir: %s\r\n",path);
  2083. }
  2084. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
  2085. if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
  2086. debug(F110,"ssl_http_init unable to load path",path,0);
  2087. if (ssl_debug_flag)
  2088. printf("?Unable to load verify-dir: %s\r\n",path);
  2089. }
  2090. ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  2091. if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2092. debug(F110,"ssl_http_init unable to load path",path,0);
  2093. if (ssl_debug_flag)
  2094. printf("?Unable to load verify-file: %s\r\n",path);
  2095. }
  2096. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
  2097. if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2098. debug(F110,"ssl_http_init unable to load path",path,0);
  2099. if (ssl_debug_flag)
  2100. printf("?Unable to load verify-file: %s\r\n",path);
  2101. }
  2102. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
  2103. if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2104. debug(F110,"ssl_http_init unable to load path",path,0);
  2105. if (ssl_debug_flag)
  2106. printf("?Unable to load verify-file: %s\r\n",path);
  2107. }
  2108. }
  2109. #else /* NT */
  2110. {
  2111. /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  2112. char path[CKMAXPATH];
  2113. ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  2114. if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
  2115. debug(F110,"ssl_http_init unable to load path",path,0);
  2116. if (ssl_debug_flag)
  2117. printf("?Unable to load verify-dir: %s\r\n",path);
  2118. }
  2119. ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  2120. if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2121. debug(F110,"ssl_http_init unable to load path",path,0);
  2122. if (ssl_debug_flag)
  2123. printf("?Unable to load verify-file: %s\r\n",path);
  2124. }
  2125. }
  2126. #endif /* NT */
  2127. #else /* OS2 */
  2128. SSL_CTX_set_default_verify_paths(tls_http_ctx);
  2129. #endif /* OS2 */
  2130. if (ssl_verify_file &&
  2131. SSL_CTX_load_verify_locations(tls_http_ctx,ssl_verify_file,NULL) == 0) {
  2132. debug(F110,"ssl_http_init unable to load ssl_verify_file",ssl_verify_file,0);
  2133. if (ssl_debug_flag)
  2134. printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  2135. }
  2136. if (ssl_verify_dir &&
  2137. SSL_CTX_load_verify_locations(tls_http_ctx,NULL,ssl_verify_dir) == 0) {
  2138. debug(F110,"ssl_http_init unable to load ssl_verify_dir",ssl_verify_dir,0);
  2139. if (ssl_debug_flag)
  2140. printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  2141. }
  2142. SSL_CTX_set_verify(tls_http_ctx,ssl_verify_flag,
  2143. ssl_client_verify_callback);
  2144. /* Free the existing CRL Store */
  2145. if (crl_store) {
  2146. X509_STORE_free(crl_store);
  2147. crl_store = NULL;
  2148. }
  2149. /* set up the new CRL Store */
  2150. crl_store = X509_STORE_new();
  2151. if (crl_store) {
  2152. #ifdef OS2
  2153. char path[CKMAXPATH];
  2154. ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  2155. if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2156. debug(F110,"ssl_http_init unable to load dir",path,0);
  2157. if (ssl_debug_flag)
  2158. printf("?Unable to load crl-dir: %s\r\n",path);
  2159. }
  2160. #ifdef NT
  2161. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
  2162. if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2163. debug(F110,"ssl_http_init unable to load dir",path,0);
  2164. if (ssl_debug_flag)
  2165. printf("?Unable to load crl-dir: %s\r\n",path);
  2166. }
  2167. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
  2168. if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2169. debug(F110,"ssl_http_init unable to load dir",path,0);
  2170. if (ssl_debug_flag)
  2171. printf("?Unable to load crl-dir: %s\r\n",path);
  2172. }
  2173. #endif /* NT */
  2174. ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  2175. if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2176. debug(F110,"ssl_http_init unable to load file",path,0);
  2177. if (ssl_debug_flag)
  2178. printf("?Unable to load crl-file: %s\r\n",path);
  2179. }
  2180. #ifdef NT
  2181. ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
  2182. if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2183. debug(F110,"ssl_http_init unable to load file",path,0);
  2184. if (ssl_debug_flag)
  2185. printf("?Unable to load crl-file: %s\r\n",path);
  2186. }
  2187. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
  2188. if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2189. debug(F110,"ssl_http_init unable to load file",path,0);
  2190. if (ssl_debug_flag)
  2191. printf("?Unable to load crl-file: %s\r\n",path);
  2192. }
  2193. #endif /* NT */
  2194. #endif /* OS2 */
  2195. if (ssl_crl_file || ssl_crl_dir) {
  2196. if (ssl_crl_file &&
  2197. X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  2198. debug(F110,"ssl_http_init unable to load ssl_crl_file",ssl_crl_file,0);
  2199. if (ssl_debug_flag)
  2200. printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  2201. }
  2202. if (ssl_crl_dir &&
  2203. X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  2204. debug(F110,"ssl_http_init unable to load ssl_crl_dir",ssl_crl_dir,0);
  2205. if (ssl_debug_flag)
  2206. printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  2207. }
  2208. } else {
  2209. X509_STORE_set_default_paths(crl_store);
  2210. }
  2211. }
  2212. #ifndef COMMENT
  2213. tls_conx = tls_http_con;
  2214. tls_http_con=(SSL *)SSL_new(tls_http_ctx);
  2215. if ( !tls_http_con ) {
  2216. debug(F110,"ssl_http_init","SSL_new(tls_http_con) failed",0);
  2217. tls_http_con = tls_conx;
  2218. return(0);
  2219. }
  2220. if (tls_conx) {
  2221. SSL_set_session(tls_http_con, SSL_get_session(tls_conx));
  2222. #ifdef SSL_KRB5
  2223. if (tls_conx->kssl_ctx) {
  2224. kssl_ctx_free(tls_conx->kssl_ctx);
  2225. tls_conx->kssl_ctx = NULL;
  2226. }
  2227. #endif /* SSL_KRB5 */
  2228. SSL_free(tls_conx);
  2229. tls_conx = NULL;
  2230. }
  2231. #else /* COMMENT */
  2232. /* I don't know why this does not work to reuse the connection. */
  2233. if ( tls_http_con ) {
  2234. SSL_clear(tls_http_con);
  2235. SSL_set_session(tls_http_con,NULL);
  2236. SSL_set_accept_state(tls_http_con) ;
  2237. } else {
  2238. tls_http_con=(SSL *)SSL_new(tls_http_ctx);
  2239. if ( !tls_http_con ) {
  2240. debug(F110,"ssl_http_init","SSL_new(tls_http_ctx) failed",0);
  2241. tls_http_con = tls_conx;
  2242. return(0);
  2243. }
  2244. }
  2245. #endif /* COMMENT */
  2246. #ifdef SSL_KRB5
  2247. #ifndef KRB5_SERVICE_NAME
  2248. #define KRB5_SERVICE_NAME "host"
  2249. #endif
  2250. if (tls_http_con->kssl_ctx == NULL)
  2251. tls_http_con->kssl_ctx = kssl_ctx_new();
  2252. if (tls_http_con->kssl_ctx != NULL)
  2253. kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVER, hostname);
  2254. kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVICE,
  2255. krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  2256. #endif /* SSL_KRB5 */
  2257. if (ssl_cipher_list)
  2258. SSL_set_cipher_list(tls_http_con,ssl_cipher_list);
  2259. else {
  2260. char * p;
  2261. if (p = getenv("SSL_CIPHER")) {
  2262. SSL_set_cipher_list(tls_http_con,p);
  2263. } else {
  2264. SSL_set_cipher_list(tls_http_con,DEFAULT_CIPHER_LIST);
  2265. }
  2266. }
  2267. ssl_verify_depth = -1;
  2268. if ( ssl_debug_flag )
  2269. printf("SSL/TLS init done!\r\n");
  2270. ssl_http_initialized = 1;
  2271. return(1);
  2272. }
  2273. #endif /* NOHTTP */
  2274. char *
  2275. ssl_get_dNSName(ssl) SSL * ssl;
  2276. {
  2277. static char *dns = NULL;
  2278. X509 *server_cert = NULL;
  2279. int i;
  2280. X509_EXTENSION *ext = NULL;
  2281. STACK_OF(GENERAL_NAME) *ialt = NULL;
  2282. GENERAL_NAME *gen = NULL;
  2283. if ( dns ) {
  2284. free(dns);
  2285. dns = NULL;
  2286. }
  2287. if (server_cert = SSL_get_peer_certificate(ssl)) {
  2288. if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1))<0)
  2289. return NULL;
  2290. if (!(ext = X509_get_ext(server_cert, i)))
  2291. return NULL;
  2292. X509V3_add_standard_extensions();
  2293. if (!(ialt = X509V3_EXT_d2i(ext)))
  2294. return NULL;
  2295. for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
  2296. gen = sk_GENERAL_NAME_value(ialt, i);
  2297. if (gen->type == GEN_DNS) {
  2298. if (!gen->d.ia5 || !gen->d.ia5->length)
  2299. break;
  2300. if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
  2301. /* Ignoring IA5String containing null character */
  2302. continue;
  2303. }
  2304. dns = malloc(gen->d.ia5->length + 1);
  2305. if (dns) {
  2306. memcpy(dns, gen->d.ia5->data, gen->d.ia5->length);
  2307. dns[gen->d.ia5->length] = 0;
  2308. }
  2309. break;
  2310. }
  2311. }
  2312. X509V3_EXT_cleanup();
  2313. }
  2314. cleanup:
  2315. if (ialt) sk_GENERAL_NAME_free(ialt);
  2316. if (server_cert) X509_free(server_cert);
  2317. return dns;
  2318. }
  2319. char *
  2320. ssl_get_commonName(ssl) SSL * ssl; {
  2321. static char name[256];
  2322. int name_text_len;
  2323. int err;
  2324. X509 *server_cert;
  2325. name_text_len = 0;
  2326. if (server_cert = SSL_get_peer_certificate(ssl)) {
  2327. name_text_len =
  2328. X509_NAME_get_text_by_NID(X509_get_subject_name(server_cert),
  2329. NID_commonName, name, sizeof(name));
  2330. X509_free(server_cert);
  2331. }
  2332. if (name_text_len <= 0) {
  2333. /* Common Name was empty or not retrieved */
  2334. err = 0;
  2335. } else if (strlen(name) != name_text_len) {
  2336. /* Ignoring Common Name containing null character */
  2337. err = 0;
  2338. } else {
  2339. err = 1;
  2340. }
  2341. if (err > 0)
  2342. return name;
  2343. else
  2344. return NULL;
  2345. }
  2346. char *
  2347. ssl_get_issuer_name(ssl) SSL * ssl;
  2348. {
  2349. static char name[256];
  2350. X509 *server_cert;
  2351. name[0] = '\0';
  2352. if (server_cert = SSL_get_peer_certificate(ssl)) {
  2353. X509_NAME_oneline(X509_get_issuer_name(server_cert),name,sizeof(name));
  2354. X509_free(server_cert);
  2355. return name;
  2356. }
  2357. else {
  2358. #ifdef COMMENT
  2359. fprintf(stderr, "Warning: No certificate from server!\r\n");
  2360. #endif /* COMMENT */
  2361. return NULL;
  2362. }
  2363. }
  2364. char *
  2365. ssl_get_subject_name(ssl) SSL * ssl;
  2366. {
  2367. static char name[256];
  2368. X509 *server_cert;
  2369. name[0] = '\0';
  2370. if (server_cert = SSL_get_peer_certificate(ssl)) {
  2371. X509_NAME_oneline(X509_get_subject_name(server_cert),name,sizeof(name));
  2372. X509_free(server_cert);
  2373. return name;
  2374. }
  2375. else
  2376. return NULL;
  2377. }
  2378. #ifdef COMMENT
  2379. #ifdef CK_SSL
  2380. && !(ck_ssleay_is_installed() &&
  2381. (tls_active_flag || ssl_active_flag) &&
  2382. ssl_anonymous_cipher(tls_active_flag?tls_con:ssl_con))
  2383. #endif /* CK_SSL */
  2384. int
  2385. ssl_anonymous_cipher(ssl) SSL * ssl;
  2386. {
  2387. X509 * cert;
  2388. if (sstelnet)
  2389. cert = SSL_get_certificate(ssl);
  2390. else
  2391. cert = SSL_get_peer_certificate(ssl);
  2392. if ( cert ) {
  2393. X509_free(cert);
  2394. return 0;
  2395. }
  2396. return 1;
  2397. }
  2398. #endif /* COMMENT */
  2399. /*
  2400. This one is (very much!) based on work by
  2401. Ralf S. Engelschall <rse@engelschall.com>.
  2402. Comments by Ralf.
  2403. */
  2404. int
  2405. ssl_verify_crl(int ok, X509_STORE_CTX *ctx)
  2406. {
  2407. X509_OBJECT *obj;
  2408. X509_NAME *subject = NULL;
  2409. X509_NAME *issuer = NULL;
  2410. X509 *xs = NULL;
  2411. X509_CRL *crl = NULL;
  2412. X509_REVOKED *revoked = NULL;
  2413. X509_STORE_CTX * store_ctx = NULL;
  2414. long serial;
  2415. BIO *bio = NULL;
  2416. int i, n, rc;
  2417. char *cp;
  2418. char *cp2;
  2419. /*
  2420. * Unless a revocation store for CRLs was created we
  2421. * cannot do any CRL-based verification, of course.
  2422. */
  2423. if (!crl_store)
  2424. return ok;
  2425. store_ctx = X509_STORE_CTX_new();
  2426. if ( !store_ctx )
  2427. return(ok);
  2428. /*
  2429. * Determine certificate ingredients in advance
  2430. */
  2431. xs = X509_STORE_CTX_get_current_cert(ctx);
  2432. subject = X509_get_subject_name(xs);
  2433. issuer = X509_get_issuer_name(xs);
  2434. /*
  2435. * OpenSSL provides the general mechanism to deal with CRLs but does not
  2436. * use them automatically when verifying certificates, so we do it
  2437. * explicitly here. We will check the CRL for the currently checked
  2438. * certificate, if there is such a CRL in the store.
  2439. *
  2440. * We come through this procedure for each certificate in the certificate
  2441. * chain, starting with the root-CA's certificate. At each step we've to
  2442. * both verify the signature on the CRL (to make sure it's a valid CRL)
  2443. * and it's revocation list (to make sure the current certificate isn't
  2444. * revoked). But because to check the signature on the CRL we need the
  2445. * public key of the issuing CA certificate (which was already processed
  2446. * one round before), we've a little problem. But we can both solve it and
  2447. * at the same time optimize the processing by using the following
  2448. * verification scheme (idea and code snippets borrowed from the GLOBUS
  2449. * project):
  2450. *
  2451. * 1. We'll check the signature of a CRL in each step when we find a CRL
  2452. * through the _subject_ name of the current certificate. This CRL
  2453. * itself will be needed the first time in the next round, of course.
  2454. * But we do the signature processing one round before this where the
  2455. * public key of the CA is available.
  2456. *
  2457. * 2. We'll check the revocation list of a CRL in each step when
  2458. * we find a CRL through the _issuer_ name of the current certificate.
  2459. * This CRLs signature was then already verified one round before.
  2460. *
  2461. * This verification scheme allows a CA to revoke its own certificate as
  2462. * well, of course.
  2463. */
  2464. /*
  2465. * Try to retrieve a CRL corresponding to the _subject_ of
  2466. * the current certificate in order to verify it's integrity.
  2467. */
  2468. obj = X509_OBJECT_new();
  2469. if (!obj) {
  2470. X509_STORE_CTX_free(store_ctx);
  2471. return ok;
  2472. }
  2473. X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
  2474. rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
  2475. X509_STORE_CTX_cleanup(store_ctx);
  2476. crl = X509_OBJECT_get0_X509_CRL(obj);
  2477. if (rc > 0 && crl != NULL) {
  2478. /*
  2479. * Verify the signature on this CRL
  2480. */
  2481. if (X509_CRL_verify(crl, X509_get_pubkey(xs)) <= 0) {
  2482. fprintf(stderr, "Invalid signature on CRL!\n");
  2483. X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
  2484. X509_OBJECT_free(obj);
  2485. X509_STORE_CTX_free(store_ctx);
  2486. return 0;
  2487. }
  2488. /*
  2489. * Check date of CRL to make sure it's not expired
  2490. */
  2491. i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
  2492. if (i == 0) {
  2493. fprintf(stderr, "Found CRL has invalid nextUpdate field.\n");
  2494. X509_STORE_CTX_set_error(ctx,
  2495. X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
  2496. X509_OBJECT_free(obj);
  2497. X509_STORE_CTX_free(store_ctx);
  2498. return 0;
  2499. }
  2500. if (i < 0) {
  2501. fprintf(stderr,
  2502. "Found CRL is expired - revoking all certificates until you get updated CRL.\n"
  2503. );
  2504. X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
  2505. X509_OBJECT_free(obj);
  2506. X509_STORE_CTX_free(store_ctx);
  2507. return 0;
  2508. }
  2509. }
  2510. /*
  2511. * Try to retrieve a CRL corresponding to the _issuer_ of
  2512. * the current certificate in order to check for revocation.
  2513. */
  2514. X509_OBJECT_free(obj);
  2515. obj = X509_OBJECT_new();
  2516. if (!obj) {
  2517. X509_STORE_CTX_free(store_ctx);
  2518. return ok;
  2519. }
  2520. X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
  2521. rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
  2522. X509_STORE_CTX_free(store_ctx); /* calls X509_STORE_CTX_cleanup() */
  2523. crl = X509_OBJECT_get0_X509_CRL(obj);
  2524. if (rc > 0 && crl != NULL) {
  2525. /*
  2526. * Check if the current certificate is revoked by this CRL
  2527. */
  2528. n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
  2529. for (i = 0; i < n; i++) {
  2530. revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
  2531. if (ASN1_INTEGER_cmp(X509_REVOKED_get0_serialNumber(revoked),
  2532. X509_get_serialNumber(xs)) == 0) {
  2533. serial = ASN1_INTEGER_get(X509_REVOKED_get0_serialNumber(revoked));
  2534. cp = X509_NAME_oneline(issuer, NULL, 0);
  2535. free(cp);
  2536. X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
  2537. X509_OBJECT_free(obj);
  2538. return 0;
  2539. }
  2540. }
  2541. }
  2542. X509_OBJECT_free(obj);
  2543. return ok;
  2544. }
  2545. char *
  2546. tls_userid_from_client_cert(ssl) SSL * ssl;
  2547. {
  2548. #ifndef OS2 /* [jt] 2013/11/21 - K-95 doesn't have X509_to_user */
  2549. static char cn[256];
  2550. static char *r = cn;
  2551. int err;
  2552. X509 *client_cert;
  2553. if (client_cert = SSL_get_peer_certificate(ssl)) {
  2554. /* call the custom function */
  2555. err = X509_to_user(client_cert, cn, sizeof(cn));
  2556. X509_free(client_cert);
  2557. if (err)
  2558. return r = NULL;
  2559. else
  2560. return r;
  2561. }
  2562. else
  2563. return r = NULL;
  2564. #else
  2565. return NULL;
  2566. #endif /* OS2 */
  2567. }
  2568. unsigned char **
  2569. tls_get_SAN_objs(SSL * ssl, int type)
  2570. /* returns NULL or an array of malloc'ed objects of type `type' from the server's
  2571. * subjectAltName, remember to free() them all!
  2572. */
  2573. {
  2574. #define NUM_SAN_OBJS 64
  2575. static unsigned char *objs[NUM_SAN_OBJS];
  2576. unsigned char **rv = NULL;
  2577. X509 *server_cert = NULL;
  2578. int i, j;
  2579. X509_EXTENSION *ext = NULL;
  2580. STACK_OF(GENERAL_NAME) *ialt = NULL;
  2581. GENERAL_NAME *gen = NULL;
  2582. memset(objs, 0, sizeof(objs));
  2583. if (server_cert = SSL_get_peer_certificate(ssl)) {
  2584. if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1)) < 0)
  2585. goto eject;
  2586. if (!(ext = X509_get_ext(server_cert, i)))
  2587. goto eject;
  2588. X509V3_add_standard_extensions();
  2589. if (!(ialt = X509V3_EXT_d2i(ext)))
  2590. goto eject;
  2591. rv = objs;
  2592. for (i = 0, j = 0; i < sk_GENERAL_NAME_num(ialt) && j < NUM_SAN_OBJS - 2; i++) {
  2593. gen = sk_GENERAL_NAME_value(ialt, i);
  2594. /* The use of V_ASN1_CONTEXT_SPECIFIC is because OpenSSL 0.9.6 defined its
  2595. * types | V_ASN1_CONTEXT_SPECIFIC. 0.9.7 does not. In case, we are built
  2596. * with one and linked to the other we use this hack.
  2597. */
  2598. if ((gen->type | V_ASN1_CONTEXT_SPECIFIC) == (type | V_ASN1_CONTEXT_SPECIFIC)) {
  2599. if (!gen->d.ia5 || !gen->d.ia5->length)
  2600. break;
  2601. if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
  2602. /* Ignoring IA5String containing null character */
  2603. continue;
  2604. }
  2605. objs[j] = malloc(gen->d.ia5->length + 1);
  2606. if (objs[j]) {
  2607. memcpy(objs[j], gen->d.ia5->data, gen->d.ia5->length);
  2608. objs[j][gen->d.ia5->length] = 0;
  2609. j++;
  2610. }
  2611. }
  2612. }
  2613. X509V3_EXT_cleanup();
  2614. }
  2615. eject:
  2616. if (ialt) sk_GENERAL_NAME_free(ialt);
  2617. if (server_cert) X509_free(server_cert);
  2618. return rv;
  2619. }
  2620. static int
  2621. dNSName_cmp(const char *host, const char *dNSName)
  2622. {
  2623. int c1 = 1, c2 = 1, num_comp, rv = -1;
  2624. char *p, *p1, *p2, *host_copy=NULL, *dNSName_copy=NULL;
  2625. /* first we count the number of domain name components in both parameters.
  2626. * they should be equal many, or it's not a match
  2627. */
  2628. p = (char *) host;
  2629. while (p = strstr(p, ".")) {
  2630. c1++;
  2631. p++;
  2632. }
  2633. p = (char *) dNSName;
  2634. while (p = strstr(p, ".")) {
  2635. c2++;
  2636. p++;
  2637. }
  2638. if (c1 != c2)
  2639. return -1;
  2640. num_comp = c1;
  2641. makestr(&host_copy,host);
  2642. makestr(&dNSName_copy,dNSName);
  2643. if (host_copy == NULL || dNSName_copy == NULL)
  2644. goto eject;
  2645. /* make substrings by replacing '.' with '\0' */
  2646. p = dNSName_copy;
  2647. while (p = strstr(p, ".")) {
  2648. *p = '\0';
  2649. p++;
  2650. }
  2651. p = host_copy;
  2652. while (p = strstr(p, ".")) {
  2653. *p = '\0';
  2654. p++;
  2655. }
  2656. /* compare each component */
  2657. p1 = host_copy;
  2658. p2 = dNSName_copy;
  2659. for (; num_comp; num_comp--) {
  2660. if (!ckmatch(p2, p1,0,1))
  2661. /* failed match */
  2662. goto eject;
  2663. p1 += strlen(p1) + 1;
  2664. p2 += strlen(p2) + 1;
  2665. }
  2666. /* match ok */
  2667. rv = 0;
  2668. eject:
  2669. if (dNSName_copy) free(dNSName_copy);
  2670. if (host_copy) free(host_copy);
  2671. return rv;
  2672. }
  2673. static int
  2674. show_hostname_warning(char *s1, char *s2)
  2675. {
  2676. char prefix[1024];
  2677. int ok = 1;
  2678. setverbosity();
  2679. ckmakxmsg(prefix,1024,
  2680. "Warning: Hostname (\"", s1,
  2681. "\") does not match server's certificate (\"", s2, "\")",
  2682. NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  2683. if (ssl_verify_flag)
  2684. ok = uq_ok(prefix,
  2685. "Continue? (Y/N) ",
  2686. 3, NULL, 0);
  2687. else if (verbosity)
  2688. printf(prefix);
  2689. return(ok);
  2690. }
  2691. #ifndef OSF50
  2692. #ifndef HPUX10
  2693. #ifndef HPUX1100
  2694. #ifndef SCO_OSR505
  2695. #ifndef OpenBSD
  2696. #ifndef FREEBSD4
  2697. #ifndef NETBSD15
  2698. #ifndef LINUX
  2699. #ifndef AIX41
  2700. #ifndef UW7
  2701. #ifndef IRIX65
  2702. #ifndef SOLARIS9
  2703. #ifndef SOLARIS8
  2704. #ifndef SOLARIS7
  2705. #ifndef MACOSX
  2706. #ifdef DEC_TCPIP
  2707. #define inet_aton INET_ATON
  2708. #endif /* DEC_TCPIP */
  2709. #ifndef NO_DCL_INET_ATON
  2710. static int
  2711. inet_aton(char * ipaddress, struct in_addr * ia) {
  2712. struct stringarray * q;
  2713. union {
  2714. unsigned long l;
  2715. unsigned char b[4];
  2716. } dummy;
  2717. q = cksplit(1,0,ipaddress,".","0123456789abcdefACDEF",8,0,0,0);
  2718. if (q->a_size == 4) {
  2719. dummy.b[0] = atoi(q->a_head[1]);
  2720. dummy.b[1] = atoi(q->a_head[2]);
  2721. dummy.b[2] = atoi(q->a_head[3]);
  2722. dummy.b[3] = atoi(q->a_head[4]);
  2723. ia->s_addr = dummy.l;
  2724. return(ia->s_addr != 0);
  2725. }
  2726. return(0);
  2727. }
  2728. #endif /* NO_DCL_INET_ATON */
  2729. #endif /* MACOSX */
  2730. #endif /* SOLARIS7 */
  2731. #endif /* SOLARIS8 */
  2732. #endif /* SOLARIS9 */
  2733. #endif /* IRIX65 */
  2734. #endif /* UW7 */
  2735. #endif /* AIX41 */
  2736. #endif /* LINUX */
  2737. #endif /* NETBSD15 */
  2738. #endif /* FREEBSD4 */
  2739. #endif /* OpenBSD */
  2740. #endif /* SCO_OSR505 */
  2741. #endif /* HPUX1100 */
  2742. #endif /* HPUX10 */
  2743. #endif /* OSF50 */
  2744. int
  2745. ssl_check_server_name(SSL * ssl, char * hostname)
  2746. /* returns 0 if hostname and server's cert matches, else -1 */
  2747. {
  2748. char * commonName;
  2749. unsigned char ** dNSName;
  2750. unsigned char ** ipAddress;
  2751. struct in_addr ia;
  2752. int rv;
  2753. setverbosity();
  2754. if (verbosity && !inserver) {
  2755. if (dNSName = tls_get_SAN_objs(ssl,GEN_DNS)) {
  2756. int i = 0;
  2757. for (i = 0; dNSName[i]; i++) {
  2758. printf("Certificate[0] altSubjectName DNS=%s\r\n",dNSName[i]);
  2759. free(dNSName[i]);
  2760. }
  2761. }
  2762. if (ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD)) {
  2763. int i = 0;
  2764. char *server_ip;
  2765. struct in_addr ia;
  2766. for (i = 0; ipAddress[i]; i++) {
  2767. if (ipAddress[i]) {
  2768. ia.s_addr = *(unsigned long *)ipAddress[i];
  2769. server_ip = inet_ntoa(ia);
  2770. printf("Certificate[0] altSubjectName IPAddr=%s\r\n",server_ip);
  2771. }
  2772. free(ipAddress[i]);
  2773. }
  2774. /* ipAddress points to a static - don't free */
  2775. }
  2776. if (dNSName = tls_get_SAN_objs(ssl,GEN_EMAIL)) {
  2777. int i = 0;
  2778. for (i = 0; dNSName[i]; i++) {
  2779. printf("Certificate[0] altSubjectName Email=%s\r\n",dNSName[i]);
  2780. free(dNSName[i]);
  2781. }
  2782. }
  2783. if (dNSName = tls_get_SAN_objs(ssl,GEN_URI)) {
  2784. int i = 0;
  2785. for (i = 0; dNSName[i]; i++) {
  2786. printf("Certificate[0] altSubjectName URI=%s\r\n",dNSName[i]);
  2787. free(dNSName[i]);
  2788. }
  2789. }
  2790. if (dNSName = tls_get_SAN_objs(ssl,GEN_OTHERNAME)) {
  2791. int i = 0;
  2792. for (i = 0; dNSName[i]; i++) {
  2793. printf("Certificate[0] altSubjectName Other=%s\r\n",dNSName[i]);
  2794. free(dNSName[i]);
  2795. }
  2796. }
  2797. }
  2798. /* first we check if `hostname' is in fact an ip address */
  2799. if (inet_aton(hostname, &ia)) {
  2800. ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD);
  2801. if (ipAddress) {
  2802. int i = 0;
  2803. char *server_ip = "UNKNOWN";
  2804. for (i = 0; ipAddress[i]; i++)
  2805. if (*(unsigned long *)ipAddress[i] == ia.s_addr)
  2806. return 0;
  2807. if (ipAddress[i - 1]) {
  2808. ia.s_addr = *(unsigned long *)ipAddress[i - 1];
  2809. server_ip = inet_ntoa(ia);
  2810. }
  2811. rv = show_hostname_warning(hostname, server_ip) ? 0 : -1;
  2812. for (i = 0; ipAddress[i]; i++)
  2813. free(ipAddress[i]);
  2814. } else {
  2815. rv = show_hostname_warning(hostname, "NO IP IN CERT") ? 0 : -1;
  2816. }
  2817. return(rv);
  2818. }
  2819. /* look for dNSName(s) in subjectAltName in the server's certificate */
  2820. dNSName = tls_get_SAN_objs(ssl,GEN_DNS);
  2821. if (dNSName) {
  2822. int i = 0;
  2823. for (i = 0; dNSName[i]; i++) {
  2824. if (!dNSName_cmp(hostname,(char *)dNSName[i]))
  2825. return 0;
  2826. }
  2827. rv = show_hostname_warning(hostname,
  2828. (char *)((dNSName[i - 1] == NULL) ?
  2829. (char *)"UNKNOWN" : (char *)dNSName[i - 1]))
  2830. ? 0 : -1;
  2831. for (i = 0; dNSName[i]; i++)
  2832. free(dNSName[i]);
  2833. return rv;
  2834. } else if ((commonName = ssl_get_commonName(ssl))) {
  2835. /* so the server didn't have any dNSName's, check the commonName */
  2836. if (!dNSName_cmp(hostname, commonName))
  2837. return 0;
  2838. else
  2839. return (show_hostname_warning(hostname, commonName) ? 0 : -1);
  2840. }
  2841. return -1;
  2842. }
  2843. /* Is 'user' authorized to access the system without a login */
  2844. int
  2845. tls_is_user_valid(SSL * ssl, const char *user)
  2846. {
  2847. #ifndef OS2 /* [jt] 2013/11/21 - K-95 doesn't have X509_userok */
  2848. X509 *client_cert;
  2849. int r = 0;
  2850. if ( !ssl || !user || !user[0] )
  2851. return(0);
  2852. if (!(client_cert = SSL_get_peer_certificate(ssl)))
  2853. return 0;
  2854. /* Use user supplied function */
  2855. r = X509_userok(client_cert,user);
  2856. X509_free(client_cert);
  2857. return r;
  2858. #else
  2859. return 0;
  2860. #endif /* OS2 */
  2861. }
  2862. int
  2863. tls_is_anon(int x)
  2864. {
  2865. char buf[128];
  2866. SSL_CIPHER * cipher;
  2867. SSL * ssl = NULL;
  2868. switch ( x ) {
  2869. #ifndef NOFTP
  2870. #ifndef SYSFTP
  2871. case 1: /* ftp command */
  2872. if ( ssl_ftp_active_flag )
  2873. ssl = ssl_ftp_con;
  2874. else
  2875. return(0);
  2876. break;
  2877. case 2: /* ftp data */
  2878. if ( ssl_ftp_data_active_flag )
  2879. ssl = ssl_ftp_data_con;
  2880. else
  2881. return(0);
  2882. break;
  2883. #endif /* SYSFTP */
  2884. #endif /* NOFTP */
  2885. default:
  2886. if (tls_active_flag)
  2887. ssl = tls_con;
  2888. else if (ssl_active_flag)
  2889. ssl = ssl_con;
  2890. else
  2891. return(0);
  2892. }
  2893. cipher = SSL_get_current_cipher(ssl);
  2894. if (SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
  2895. if (ckindex("Au=None",buf,0,0,0) != 0)
  2896. return(1); /* anonymous */
  2897. return(0); /* known */
  2898. } else {
  2899. /* could not get cipher description. Assume anonymous */
  2900. return(1);
  2901. }
  2902. }
  2903. int
  2904. tls_is_krb5(int x)
  2905. {
  2906. char buf[128];
  2907. SSL_CIPHER * cipher;
  2908. SSL * ssl = NULL;
  2909. switch ( x ) {
  2910. #ifndef NOFTP
  2911. #ifndef SYSFTP
  2912. case 1: /* ftp command */
  2913. if ( ssl_ftp_active_flag )
  2914. ssl = ssl_ftp_con;
  2915. else
  2916. return(0);
  2917. break;
  2918. case 2: /* ftp data */
  2919. if ( ssl_ftp_data_active_flag )
  2920. ssl = ssl_ftp_data_con;
  2921. else
  2922. return(0);
  2923. break;
  2924. #endif /* SYSFTP */
  2925. #endif /* NOFTP */
  2926. #ifndef NOHTTP
  2927. case 3:
  2928. if ( tls_http_active_flag )
  2929. ssl = tls_http_con;
  2930. break;
  2931. #endif /* NOHTTP */
  2932. default:
  2933. if (tls_active_flag)
  2934. ssl = tls_con;
  2935. else if (ssl_active_flag)
  2936. ssl = ssl_con;
  2937. else
  2938. return(0);
  2939. }
  2940. cipher = SSL_get_current_cipher(ssl);
  2941. if (cipher && SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
  2942. if (ckindex("Au=KRB5",buf,0,0,0) != 0)
  2943. return(1); /* krb5 */
  2944. }
  2945. return(0); /* not */
  2946. }
  2947. int
  2948. ssl_get_client_finished(char *buf, int count)
  2949. {
  2950. #ifdef NO_GET_FINISHED
  2951. return(0);
  2952. #else
  2953. if (sstelnet || tcp_incoming) {
  2954. return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
  2955. buf,count));
  2956. } else {
  2957. return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
  2958. buf,count));
  2959. }
  2960. #endif /* NO_GET_FINISHED */
  2961. }
  2962. int
  2963. ssl_get_server_finished(char *buf, int count)
  2964. {
  2965. #ifdef NO_GET_FINISHED
  2966. return(0);
  2967. #else
  2968. if (sstelnet || tcp_incoming) {
  2969. return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
  2970. buf,count));
  2971. } else {
  2972. return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
  2973. buf,count));
  2974. }
  2975. #endif /* NO_GET_FINISHED */
  2976. }
  2977. #ifdef CK_AUTHENTICATION
  2978. int
  2979. #ifdef CK_ANSIC
  2980. ssl_reply(int how, unsigned char *data, int cnt)
  2981. #else
  2982. ssl_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  2983. #endif
  2984. {
  2985. char * str=NULL;
  2986. setverbosity();
  2987. data += 4; /* Point to status byte */
  2988. cnt -= 4;
  2989. if(cnt-- < 1) {
  2990. auth_finished(AUTH_REJECT);
  2991. return AUTH_FAILURE;
  2992. }
  2993. switch(*data++) {
  2994. case SSL_ACCEPT:
  2995. if (tn_deb || debses)
  2996. tn_debug("[SSL - handshake starting]");
  2997. else if ( verbosity )
  2998. printf("[SSL - handshake starting]\r\n");
  2999. debug(F110,"ssl_reply","[SSL - handshake starting]",0);
  3000. /* right ... now we drop into the SSL library */
  3001. if (!ssl_only_flag) {
  3002. if (ssl_dummy_flag) {
  3003. if (tn_deb || debses)
  3004. tn_debug("[SSL - Dummy Connected]");
  3005. else if ( verbosity ) {
  3006. printf("[SSL - Dummy Connected]\r\n");
  3007. }
  3008. debug(F110,"ssl_reply","[SSL - Dummy Connected]",0);
  3009. auth_finished(AUTH_UNKNOWN);
  3010. accept_complete = 1;
  3011. return AUTH_SUCCESS;
  3012. }
  3013. if (SSL_connect(ssl_con) <= 0) {
  3014. int len;
  3015. if (tn_deb || debses) {
  3016. tn_debug("[SSL - FAILED]");
  3017. ERR_print_errors(bio_err);
  3018. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3019. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3020. printf(ssl_err);
  3021. } else if ( verbosity ) {
  3022. printf("[SSL - FAILED]\r\n");
  3023. ERR_print_errors(bio_err);
  3024. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3025. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3026. printf(ssl_err);
  3027. }
  3028. debug(F110,"ssl_reply","[SSL - FAILED]",0);
  3029. auth_finished(AUTH_REJECT);
  3030. ttclos(0);
  3031. return AUTH_FAILURE;
  3032. } else {
  3033. if (tn_deb || debses)
  3034. tn_debug("[SSL - OK]");
  3035. else if ( verbosity ) {
  3036. printf("[SSL - OK]\r\n");
  3037. }
  3038. debug(F110,"ssl_reply","[SSL - OK]",0);
  3039. ssl_active_flag = 1;
  3040. ssl_display_connect_details(ssl_con,0,verbosity);
  3041. }
  3042. }
  3043. auth_finished(AUTH_UNKNOWN);
  3044. accept_complete = 1;
  3045. break;
  3046. case SSL_REJECT:
  3047. if (tn_deb || debses) {
  3048. tn_debug(
  3049. "[SSL - failed to switch on SSL - trying plaintext login]");
  3050. } else if ( verbosity ) {
  3051. printf("[SSL - failed to switch on SSL]\r\n");
  3052. printf("Trying plaintext login:\r\n");
  3053. }
  3054. debug(F110,"ssl_reply","[SSL - failed to switch on SSL]",0);
  3055. auth_finished(AUTH_REJECT);
  3056. return AUTH_FAILURE;
  3057. default:
  3058. return AUTH_FAILURE;
  3059. }
  3060. return AUTH_SUCCESS;
  3061. }
  3062. int
  3063. #ifdef CK_ANSIC
  3064. ssl_is(unsigned char *data, int cnt)
  3065. #else
  3066. ssl_is(data,cnt) unsigned char *data; int cnt;
  3067. #endif
  3068. {
  3069. if ((cnt -= 4) < 1)
  3070. return AUTH_FAILURE;
  3071. setverbosity();
  3072. data += 4;
  3073. switch(*data++) {
  3074. case SSL_START:
  3075. /* server starts the SSL stuff now ... */
  3076. if (!ssl_only_flag) {
  3077. if ( !tls_load_certs(ssl_ctx,ssl_con,1) ) {
  3078. auth_finished(AUTH_REJECT);
  3079. return AUTH_FAILURE;
  3080. }
  3081. if (tn_deb || debses)
  3082. tn_debug("[SSL - handshake starting]");
  3083. else if ( verbosity )
  3084. printf("[SSL - handshake starting]\r\n");
  3085. debug(F110,"ssl_is","[SSL - handshake starting]",0);
  3086. SendSSLAuthSB(SSL_ACCEPT, (void *)0, 0);
  3087. auth_ssl_valid = 1;
  3088. if (ssl_dummy_flag) {
  3089. if (tn_deb || debses)
  3090. tn_debug("[SSL - Dummy Connected]");
  3091. else if ( verbosity ) {
  3092. printf("[SSL - Dummy Connected]\r\n");
  3093. }
  3094. debug(F110,"ssl_is","[SSL - Dummy Connected]",0);
  3095. accept_complete = 1;
  3096. auth_finished(AUTH_UNKNOWN);
  3097. return AUTH_SUCCESS;
  3098. }
  3099. if (SSL_accept(ssl_con) <= 0) {
  3100. char errbuf[1024];
  3101. sprintf(errbuf,"[SSL - SSL_accept error: %s",
  3102. ERR_error_string(ERR_get_error(),NULL));
  3103. if (tn_deb || debses)
  3104. tn_debug(errbuf);
  3105. else if ( ssl_debug_flag )
  3106. printf("%s\r\n",errbuf);
  3107. else if ( verbosity )
  3108. printf("[SSL - SSL_accept error]\r\n");
  3109. debug(F110,"ssl_is",errbuf,0);
  3110. auth_finished(AUTH_REJECT);
  3111. ttclos(0);
  3112. return AUTH_FAILURE;
  3113. }
  3114. if (tn_deb || debses)
  3115. tn_debug("[SSL - OK]");
  3116. else if ( verbosity ) {
  3117. printf("[SSL - OK]\r\n");
  3118. }
  3119. debug(F110,"ssl_is","[SSL - OK]",0);
  3120. ssl_active_flag = 1;
  3121. ssl_display_connect_details(ssl_con,1,verbosity);
  3122. /* now check to see that we got exactly what we
  3123. * wanted from the caller ... if a certificate is
  3124. * required then we make 100% sure that we were
  3125. * given one during the handshake (as it is an optional
  3126. * part of SSL)
  3127. */
  3128. #ifdef SSL_KRB5
  3129. if ( tls_is_krb5(0) ) {
  3130. if (ssl_con->kssl_ctx->client_princ)
  3131. debug(F110,"ssl_is KRB5",ssl_con->kssl_ctx->client_princ,0);
  3132. } else
  3133. #endif /* SSL_KRB5 */
  3134. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3135. X509 * peer = SSL_get_peer_certificate(ssl_con);
  3136. if (peer == NULL) {
  3137. if (tn_deb || debses)
  3138. tn_debug("[SSL - peer check failed]");
  3139. else if (ssl_debug_flag)
  3140. printf("[SSL - peer check failed]\r\n");
  3141. debug(F110,"ssl_is","[SSL - peer check failed]",0);
  3142. /* LOGGING REQUIRED HERE! */
  3143. auth_finished(AUTH_REJECT);
  3144. return AUTH_FAILURE;
  3145. }
  3146. }
  3147. auth_finished(AUTH_UNKNOWN);
  3148. accept_complete = 1;
  3149. }
  3150. break;
  3151. default:
  3152. SendSSLAuthSB(SSL_REJECT, (void *) "Unknown option received", -1);
  3153. if (tn_deb || debses)
  3154. tn_debug("[SSL - Unknown option received]");
  3155. else
  3156. printf("Unknown SSL option %d\r\n", data[-1]);
  3157. debug(F111,"ssl_is","[SSL - Unknown option received]",data[-1]);
  3158. auth_ssl_valid = 0;
  3159. auth_finished(AUTH_REJECT);
  3160. return(AUTH_FAILURE);
  3161. }
  3162. return AUTH_SUCCESS;
  3163. }
  3164. #endif /* CK_AUTHENTICATION */
  3165. int
  3166. ck_tn_tls_negotiate(VOID)
  3167. {
  3168. X509 * peer = NULL;
  3169. char str[256], *uid=NULL;
  3170. extern int sstelnet;
  3171. if ( !ck_ssleay_is_installed() )
  3172. return(-1);
  3173. setverbosity();
  3174. if (sstelnet) {
  3175. /* server starts the TLS stuff now ... */
  3176. if (!tls_only_flag) {
  3177. if ( !tls_load_certs(tls_ctx,tls_con,1) ) {
  3178. auth_finished(AUTH_REJECT);
  3179. return -1;
  3180. }
  3181. if (tn_deb || debses)
  3182. tn_debug("[TLS - handshake starting]");
  3183. else if ( verbosity )
  3184. printf("[TLS - handshake starting]\r\n");
  3185. debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
  3186. if (ssl_dummy_flag) {
  3187. if (tn_deb || debses)
  3188. tn_debug("[TLS - Dummy Connected]");
  3189. else if ( verbosity ) {
  3190. printf("[TLS - Dummy Connected]\r\n");
  3191. }
  3192. debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
  3193. accept_complete = 1;
  3194. auth_finished(AUTH_REJECT);
  3195. return 0;
  3196. }
  3197. if (SSL_accept(tls_con) <= 0) {
  3198. char errbuf[1024];
  3199. sprintf(errbuf,"[TLS - SSL_accept error: %s",
  3200. ERR_error_string(ERR_get_error(),NULL));
  3201. if (tn_deb || debses)
  3202. tn_debug(errbuf);
  3203. else if ( ssl_debug_flag )
  3204. printf("%s\r\n",errbuf);
  3205. else if ( verbosity )
  3206. printf("[TLS - SSL_accept error]\r\n");
  3207. debug(F110,"ck_tn_tls_negotiate",errbuf,0);
  3208. auth_finished(AUTH_REJECT);
  3209. return -1;
  3210. }
  3211. if (tn_deb || debses)
  3212. tn_debug("[TLS - OK]");
  3213. else if ( verbosity ) {
  3214. printf("[TLS - OK]\r\n");
  3215. }
  3216. debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
  3217. tls_active_flag = 1;
  3218. ssl_display_connect_details(tls_con,1,verbosity);
  3219. #ifdef SSL_KRB5
  3220. if ( tls_is_krb5(0) ) {
  3221. if (tls_con->kssl_ctx->client_princ) {
  3222. char *p;
  3223. ckstrncpy(szUserNameAuthenticated,
  3224. tls_con->kssl_ctx->client_princ,
  3225. UIDBUFLEN);
  3226. ckstrncpy(szUserNameRequested,
  3227. tls_con->kssl_ctx->client_princ,
  3228. UIDBUFLEN);
  3229. for ( p = szUserNameRequested; *p ; p++ ) {
  3230. if ( *p == '@' || *p == '/' ) {
  3231. *p = '\0';
  3232. break;
  3233. }
  3234. }
  3235. } else {
  3236. szUserNameRequested[0] = '\0';
  3237. szUserNameAuthenticated[0] = '\0';
  3238. }
  3239. #ifdef CK_LOGIN
  3240. if (zvuser(szUserNameRequested))
  3241. auth_finished(AUTH_VALID);
  3242. else
  3243. #endif /* CK_LOGIN */
  3244. auth_finished(AUTH_USER);
  3245. } else
  3246. #endif /* SSL_KRB5 */
  3247. {
  3248. /* now check to see that we got exactly what we
  3249. * wanted from the caller ... if a certificate is
  3250. * required then we make 100% sure that we were
  3251. * given one during the handshake (as it is an optional
  3252. * part of TLS)
  3253. */
  3254. peer=SSL_get_peer_certificate(tls_con);
  3255. if (peer == NULL) {
  3256. debug(F100,"SSL_get_peer_certificate() == NULL","",0);
  3257. auth_finished(AUTH_REJECT);
  3258. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3259. if (tn_deb || debses)
  3260. tn_debug("[TLS - peer check failed]");
  3261. else if (ssl_debug_flag) {
  3262. printf("[TLS - peer check failed]\r\n");
  3263. }
  3264. debug(F110,
  3265. "ck_tn_tls_negotiate",
  3266. "[TLS - peer check failed]",
  3267. 0
  3268. );
  3269. /* LOGGING REQUIRED HERE! */
  3270. return -1;
  3271. }
  3272. } else {
  3273. debug(F100,"SSL_get_peer_certificate() != NULL","",0);
  3274. X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3275. NID_commonName,str,
  3276. 256
  3277. );
  3278. if ( verbosity )
  3279. printf("[TLS - commonName=%s]\r\n",str);
  3280. X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3281. #ifndef NID_x500UniqueIdentifier
  3282. NID_uniqueIdentifier,
  3283. #else
  3284. NID_x500UniqueIdentifier,
  3285. #endif
  3286. str,
  3287. 256
  3288. );
  3289. if ( verbosity )
  3290. printf("[TLS - uniqueIdentifier=%s]\r\n",str);
  3291. /* Try to determine user name */
  3292. uid = tls_userid_from_client_cert(tls_con);
  3293. if ( uid ) {
  3294. /* This code is very questionable.
  3295. * How should it behave?
  3296. * The client has presented a certificate that
  3297. * contains a username. We have validated the
  3298. * certificate but we do not automatically
  3299. * log the user in unless there is a .tlslogin
  3300. * file.
  3301. */
  3302. ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
  3303. #ifdef CK_LOGIN
  3304. if (zvuser(uid))
  3305. auth_finished(AUTH_VALID);
  3306. else
  3307. #endif /* CK_LOGIN */
  3308. auth_finished(AUTH_USER);
  3309. }
  3310. else {
  3311. szUserNameRequested[0] = '\0';
  3312. auth_finished(AUTH_REJECT);
  3313. }
  3314. }
  3315. }
  3316. }
  3317. } else {
  3318. char * str=NULL;
  3319. if (tn_deb || debses)
  3320. tn_debug("[TLS - handshake starting]");
  3321. else if ( verbosity )
  3322. printf("[TLS - handshake starting]\r\n");
  3323. debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
  3324. /* right ... now we drop into the SSL library */
  3325. if (!tls_only_flag) {
  3326. char *subject=NULL, *issuer=NULL, *commonName=NULL, *dNSName=NULL;
  3327. if (ssl_dummy_flag) {
  3328. if (tn_deb || debses)
  3329. tn_debug("[TLS - Dummy Connected]");
  3330. else if ( verbosity ) {
  3331. printf("[TLS - Dummy Connected]\r\n");
  3332. }
  3333. debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
  3334. auth_finished(AUTH_REJECT);
  3335. accept_complete = 1;
  3336. return 0;
  3337. }
  3338. #ifndef USE_CERT_CB
  3339. if (!tls_load_certs(tls_ctx,tls_con,0))
  3340. return(-1);
  3341. #endif /* USE_CERT_CB */
  3342. if (SSL_connect(tls_con) <= 0) {
  3343. int len;
  3344. if (tn_deb || debses) {
  3345. tn_debug("[TLS - FAILED]");
  3346. ERR_print_errors(bio_err);
  3347. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3348. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3349. printf(ssl_err);
  3350. } else if ( verbosity ) {
  3351. printf("[TLS - FAILED]\r\n");
  3352. ERR_print_errors(bio_err);
  3353. len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3354. ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3355. printf(ssl_err);
  3356. }
  3357. debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3358. auth_finished(AUTH_REJECT);
  3359. return -1;
  3360. }
  3361. tls_active_flag = 1;
  3362. if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER)
  3363. && !tls_is_krb5(0)) {
  3364. char prmpt[1024];
  3365. subject = ssl_get_subject_name(tls_con);
  3366. if (!subject) {
  3367. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3368. {
  3369. if (tn_deb || debses)
  3370. tn_debug("[TLS - FAILED]");
  3371. else if ( verbosity )
  3372. printf("[TLS - FAILED]\r\n");
  3373. debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3374. auth_finished(AUTH_REJECT);
  3375. return -1;
  3376. } else {
  3377. int ok;
  3378. ok = uq_ok("Warning: Server didn't provide a certificate",
  3379. "Continue? (Y/N)", 3, NULL, 0);
  3380. if (!ok) {
  3381. if (tn_deb || debses)
  3382. tn_debug("[TLS - FAILED]");
  3383. else if ( verbosity )
  3384. printf("[TLS - FAILED]\r\n");
  3385. debug(F110,
  3386. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3387. auth_finished(AUTH_REJECT);
  3388. return -1;
  3389. }
  3390. }
  3391. } else if (ssl_check_server_name(tls_con, szHostName)) {
  3392. if (tn_deb || debses)
  3393. tn_debug("[TLS - FAILED]");
  3394. else if ( verbosity )
  3395. printf("[TLS - FAILED]\r\n");
  3396. debug(F110,
  3397. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3398. auth_finished(AUTH_REJECT);
  3399. return -1;
  3400. }
  3401. }
  3402. if ( ssl_debug_flag && ssl_finished_messages) {
  3403. char msg[32];
  3404. int i, len=32;
  3405. extern char tn_msg[], hexbuf[];
  3406. tn_msg[0] = '\0';
  3407. len = ssl_get_client_finished(msg,len);
  3408. if ( len > 0 ) {
  3409. for ( i=0;i<len;i++ ) {
  3410. sprintf(hexbuf,"%02X ",msg[i]);
  3411. ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  3412. }
  3413. printf("TLS client finished: %s\r\n",tn_msg);
  3414. }
  3415. tn_msg[0] = '\0';
  3416. len = ssl_get_server_finished(msg,len);
  3417. if ( len > 0 ) {
  3418. for ( i=0;i<len;i++ ) {
  3419. sprintf(hexbuf,"%02X ",msg[i]);
  3420. ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  3421. }
  3422. printf("TLS server finished: %s\r\n",tn_msg);
  3423. }
  3424. }
  3425. if (tn_deb || debses)
  3426. tn_debug("[TLS - OK]");
  3427. else if ( verbosity )
  3428. printf("[TLS - OK]\r\n");
  3429. debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
  3430. ssl_display_connect_details(tls_con,0,verbosity);
  3431. }
  3432. auth_finished(AUTH_REJECT);
  3433. }
  3434. accept_complete = 1;
  3435. auth_ssl_valid = 1;
  3436. return(0);
  3437. }
  3438. int
  3439. ck_ssl_incoming(fd) int fd;
  3440. {
  3441. /* if we are not running in debug then any error
  3442. * stuff from SSL debug *must* not go down
  3443. * the socket (which 0,1,2 are all pointing to by
  3444. * default)
  3445. */
  3446. int timo = 2000;
  3447. setverbosity();
  3448. if ( !ck_ssleay_is_installed() )
  3449. return(-1);
  3450. /* do the SSL stuff now ... before we play with pty's */
  3451. SSL_set_fd(ssl_con,fd);
  3452. SSL_set_fd(tls_con,fd);
  3453. if (tls_only_flag) {
  3454. if (tn_deb || debses)
  3455. tn_debug("[TLS - handshake starting]");
  3456. else if ( verbosity )
  3457. printf("[TLS - handshake starting]\r\n");
  3458. debug(F110,"ck_ssl_incoming","[TLS - handshake starting]",0);
  3459. /* hmm ... only when running talking to things like
  3460. * https servers should we hit this code and then
  3461. * we really don't care *who* we talk to :-)
  3462. */
  3463. if (SSL_accept(tls_con) <= 0) {
  3464. char errbuf[1024];
  3465. sprintf(errbuf,"[TLS - SSL_accept error: %s",
  3466. ERR_error_string(ERR_get_error(),NULL));
  3467. if (tn_deb || debses)
  3468. tn_debug(errbuf);
  3469. else if ( ssl_debug_flag )
  3470. printf("%s\r\n",errbuf);
  3471. else if ( verbosity )
  3472. printf("[TLS - SSL_accept error]\r\n");
  3473. debug(F110,"ck_ssl_incoming",errbuf,0);
  3474. return(-1);
  3475. } else {
  3476. if (tn_deb || debses)
  3477. tn_debug("[TLS - OK]");
  3478. else if ( verbosity )
  3479. printf("[TLS - OK]\r\n");
  3480. debug(F110,"ck_ssl_incoming","[TLS - OK]",0);
  3481. tls_active_flag = 1;
  3482. }
  3483. } else if (ssl_only_flag) {
  3484. if (tn_deb || debses)
  3485. tn_debug("[SSL - handshake starting]");
  3486. else if ( verbosity )
  3487. printf("[SSL - handshake starting]\r\n");
  3488. debug(F110,"ck_ssl_incoming","[SSL - handshake starting]",0);
  3489. /* hmm ... only when running talking to things like
  3490. * https servers should we hit this code and then
  3491. * we really don't care *who* we talk to :-)
  3492. */
  3493. if (SSL_accept(ssl_con) <= 0) {
  3494. char errbuf[1024];
  3495. sprintf(errbuf,"[SSL - SSL_accept error: %s",
  3496. ERR_error_string(ERR_get_error(),NULL));
  3497. if (tn_deb || debses)
  3498. tn_debug(errbuf);
  3499. else if ( ssl_debug_flag )
  3500. printf("%s\r\n",errbuf);
  3501. else if ( verbosity )
  3502. printf("[SSL - SSL_accept error]\r\n");
  3503. debug(F110,"ck_ssl_incoming",errbuf,0);
  3504. return(-1);
  3505. } else {
  3506. if (tn_deb || debses)
  3507. tn_debug("[SSL - OK]");
  3508. else if ( verbosity )
  3509. printf("[SSL - OK]\r\n");
  3510. debug(F110,"ssl_is","[SSL - OK]",0);
  3511. ssl_active_flag = 1;
  3512. }
  3513. }
  3514. if (ssl_active_flag || tls_active_flag) {
  3515. X509 *peer;
  3516. char str[256], *uid=NULL;
  3517. /* now check to see that we got exactly what we
  3518. * wanted from the caller ... if a certificate is
  3519. * required then we make 100% sure that we were
  3520. * given on during the handshake (as it is an optional
  3521. * part of SSL and TLS)
  3522. */
  3523. if ( tls_active_flag ) {
  3524. peer=SSL_get_peer_certificate(tls_con);
  3525. } else if ( ssl_active_flag ) {
  3526. peer=SSL_get_peer_certificate(ssl_con);
  3527. }
  3528. if (peer == NULL) {
  3529. debug(F100,"SSL_get_peer_certificate() == NULL","",0);
  3530. auth_finished(AUTH_REJECT);
  3531. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3532. if (tn_deb || debses)
  3533. tn_debug("[SSL/TLS - peer check failed]");
  3534. else if (ssl_debug_flag) {
  3535. printf("[SSL/TLS - peer check failed]\r\n");
  3536. }
  3537. debug(F110,
  3538. "ck_tn_tls_negotiate",
  3539. "[SSL/TLS - peer check failed]",
  3540. 0
  3541. );
  3542. /* LOGGING REQUIRED HERE! */
  3543. return -1;
  3544. }
  3545. } else {
  3546. debug(F100,"SSL_get_peer_certificate() != NULL","",0);
  3547. X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3548. NID_commonName,str,
  3549. 256
  3550. );
  3551. printf("[TLS - commonName=%s]\r\n",str);
  3552. X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3553. #ifndef NID_x500UniqueIdentifier
  3554. NID_uniqueIdentifier,
  3555. #else
  3556. NID_x500UniqueIdentifier,
  3557. #endif
  3558. str,256
  3559. );
  3560. printf("[TLS - uniqueIdentifier=%s]\r\n",str);
  3561. /* Try to determine user name */
  3562. uid = tls_userid_from_client_cert(tls_con);
  3563. if ( uid ) {
  3564. /* This code is very questionable.
  3565. * How should it behave?
  3566. * The client has presented a certificate that
  3567. * contains a username. We have validated the
  3568. * certificate but we do not automatically
  3569. * log the user in unless there is a .tlslogin
  3570. * file.
  3571. */
  3572. ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
  3573. #ifdef CK_LOGIN
  3574. if (zvuser(uid))
  3575. auth_finished(AUTH_VALID);
  3576. else
  3577. #endif /* CK_LOGIN */
  3578. auth_finished(AUTH_USER);
  3579. }
  3580. else {
  3581. szUserNameRequested[0] = '\0';
  3582. auth_finished(AUTH_REJECT);
  3583. }
  3584. }
  3585. }
  3586. return(0); /* success */
  3587. }
  3588. int
  3589. ck_ssl_outgoing(fd) int fd;
  3590. {
  3591. int timo = 2000;
  3592. setverbosity();
  3593. if ( !ck_ssleay_is_installed() )
  3594. return(-1);
  3595. /* bind in the network descriptor */
  3596. SSL_set_fd(ssl_con,fd);
  3597. SSL_set_fd(tls_con,fd);
  3598. /* If we are doing raw TLS then start it now ... */
  3599. if (tls_only_flag) {
  3600. #ifndef USE_CERT_CB
  3601. if (!tls_load_certs(tls_ctx,tls_con,0)) {
  3602. debug(F110,"ck_ssl_outgoing","tls_load_certs() failed",0);
  3603. return(-1);
  3604. }
  3605. #endif /* USE_CERT_CB */
  3606. if (tn_deb || debses)
  3607. tn_debug("[TLS - handshake starting]");
  3608. else if (verbosity)
  3609. printf("[TLS - handshake starting]\r\n");
  3610. debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
  3611. if (SSL_connect(tls_con) <= 0) {
  3612. char errbuf[1024];
  3613. sprintf(errbuf,"[TLS - SSL_connect error: %s",
  3614. ERR_error_string(ERR_get_error(),NULL));
  3615. if (tn_deb || debses)
  3616. tn_debug(errbuf);
  3617. else if ( ssl_debug_flag )
  3618. printf("%s\r\n",errbuf);
  3619. if (tn_deb || debses)
  3620. tn_debug("[TLS - FAILED]");
  3621. else if ( verbosity )
  3622. printf("[TLS - FAILED]\r\n");
  3623. debug(F110,"ck_ssl_outgoing","[TLS - FAILED]",0);
  3624. netclos();
  3625. return(-1);
  3626. } else {
  3627. tls_active_flag = 1;
  3628. if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
  3629. !tls_is_krb5(0) ) {
  3630. char *subject = ssl_get_subject_name(tls_con);
  3631. if (!subject) {
  3632. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3633. {
  3634. if (tn_deb || debses)
  3635. tn_debug("[TLS - FAILED]");
  3636. else if ( verbosity )
  3637. printf("[TLS - FAILED]\r\n");
  3638. debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3639. auth_finished(AUTH_REJECT);
  3640. return -1;
  3641. } else {
  3642. char prmpt[1024];
  3643. int ok;
  3644. ok = uq_ok("Warning: Server didn't provide a certificate",
  3645. "Continue? (Y/N)", 3, NULL, 0);
  3646. if (!ok) {
  3647. if (tn_deb || debses)
  3648. tn_debug("[TLS - FAILED]");
  3649. else if ( verbosity )
  3650. printf("[TLS - FAILED]\r\n");
  3651. debug(F110,
  3652. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3653. auth_finished(AUTH_REJECT);
  3654. return -1;
  3655. }
  3656. }
  3657. } else if (ssl_check_server_name(tls_con, szHostName)) {
  3658. if (tn_deb || debses)
  3659. tn_debug("[TLS - FAILED]");
  3660. else if ( verbosity )
  3661. printf("[TLS - FAILED]\r\n");
  3662. debug(F110,
  3663. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3664. auth_finished(AUTH_REJECT);
  3665. return -1;
  3666. }
  3667. }
  3668. if (tn_deb || debses)
  3669. tn_debug("[TLS - OK]");
  3670. else if (!quiet)
  3671. printf("[TLS - OK]\r\n");
  3672. debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
  3673. ssl_display_connect_details(tls_con,0,verbosity);
  3674. }
  3675. }
  3676. /* if we are doing raw SSL then start it now ... */
  3677. else if (ssl_only_flag) {
  3678. #ifndef USE_CERT_CB
  3679. if (!tls_load_certs(ssl_ctx,ssl_con,0))
  3680. return(-1);
  3681. #endif /* USE_CERT_CB */
  3682. if (tn_deb || debses)
  3683. tn_debug("[SSL - handshake starting]");
  3684. else if ( verbosity )
  3685. printf("[SSL - handshake starting]\r\n");
  3686. debug(F110,"ck_ssl_outgoing","[SSL - handshake starting]",0);
  3687. if (SSL_connect(ssl_con) <= 0) {
  3688. if ( ssl_debug_flag ) {
  3689. char errbuf[1024];
  3690. sprintf(errbuf,"[SSL - SSL_connect error: %s",
  3691. ERR_error_string(ERR_get_error(),NULL));
  3692. printf("%s\r\n",errbuf);
  3693. }
  3694. if (tn_deb || debses)
  3695. tn_debug("[SSL - FAILED]");
  3696. else if ( verbosity )
  3697. printf("[SSL - FAILED]\r\n");
  3698. debug(F110,"ck_ssl_outgoing","[SSL - FAILED]",0);
  3699. return(-1);
  3700. } else {
  3701. ssl_active_flag = 1;
  3702. if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
  3703. !tls_is_krb5(0)) {
  3704. char *subject = ssl_get_subject_name(ssl_con);
  3705. if (!subject) {
  3706. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3707. {
  3708. if (tn_deb || debses)
  3709. tn_debug("[SSL - FAILED]");
  3710. else if ( verbosity )
  3711. printf("[SSL - FAILED]\r\n");
  3712. debug(F110,"ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3713. auth_finished(AUTH_REJECT);
  3714. return -1;
  3715. } else {
  3716. char prmpt[1024];
  3717. int ok;
  3718. ok = uq_ok("Warning: Server didn't provide a certificate",
  3719. "Continue? (Y/N)", 3, NULL, 0);
  3720. if (!ok) {
  3721. if (tn_deb || debses)
  3722. tn_debug("[SSL - FAILED]");
  3723. else if ( verbosity )
  3724. printf("[SSL - FAILED]\r\n");
  3725. debug(F110,
  3726. "ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3727. auth_finished(AUTH_REJECT);
  3728. return -1;
  3729. }
  3730. }
  3731. } else if (ssl_check_server_name(ssl_con, szHostName)) {
  3732. if (tn_deb || debses)
  3733. tn_debug("[SSL - FAILED]");
  3734. else if ( verbosity )
  3735. printf("[SSL - FAILED]\r\n");
  3736. debug(F110, "ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3737. auth_finished(AUTH_REJECT);
  3738. return -1;
  3739. }
  3740. }
  3741. if (tn_deb || debses)
  3742. tn_debug("[SSL - OK]");
  3743. else if (!quiet)
  3744. printf("[SSL - OK]\r\n");
  3745. debug(F110,"ck_ssl_outgoing","[SSL - OK]",0);
  3746. ssl_display_connect_details(ssl_con,0,verbosity);
  3747. }
  3748. }
  3749. return(0); /* success */
  3750. }
  3751. #ifndef NOHTTP
  3752. int
  3753. ck_ssl_http_client(fd, hostname) int fd; char * hostname;
  3754. {
  3755. int timo = 2000;
  3756. if ( !ck_ssleay_is_installed() )
  3757. return(-1);
  3758. setverbosity();
  3759. /* bind in the network descriptor */
  3760. SSL_set_fd(tls_http_con,fd);
  3761. /* If we are doing raw TLS then start it now ... */
  3762. if (1) {
  3763. #ifndef USE_CERT_CB
  3764. if (!tls_load_certs(tls_http_ctx,tls_http_con,0)) {
  3765. debug(F110,"ck_ssl_http_client","tls_load_certs() failed",0);
  3766. return(-1);
  3767. }
  3768. #endif /* USE_CERT_CB */
  3769. if (tn_deb || debses)
  3770. tn_debug("[TLS - handshake starting]");
  3771. else if (verbosity)
  3772. printf("[TLS - handshake starting]\r\n");
  3773. debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
  3774. if (SSL_connect(tls_http_con) <= 0) {
  3775. char errbuf[1024];
  3776. sprintf(errbuf,"[TLS - SSL_connect error: %s",
  3777. ERR_error_string(ERR_get_error(),NULL));
  3778. if (tn_deb || debses)
  3779. tn_debug(errbuf);
  3780. else if ( ssl_debug_flag )
  3781. printf("%s\r\n",errbuf);
  3782. if (tn_deb || debses)
  3783. tn_debug("[TLS - FAILED]");
  3784. else if ( verbosity )
  3785. printf("[TLS - FAILED]\r\n");
  3786. debug(F110,"ck_ssl_http_client","[TLS - FAILED]",0);
  3787. http_close();
  3788. return(-1);
  3789. } else {
  3790. tls_http_active_flag = 1;
  3791. if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
  3792. !tls_is_krb5(3) ) {
  3793. char *subject = ssl_get_subject_name(tls_http_con);
  3794. if (!subject) {
  3795. if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3796. {
  3797. if (tn_deb || debses)
  3798. tn_debug("[TLS - FAILED]");
  3799. else if ( verbosity )
  3800. printf("[TLS - FAILED]\r\n");
  3801. debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3802. return -1;
  3803. } else {
  3804. char prmpt[1024];
  3805. int ok;
  3806. ok = uq_ok("Warning: Server didn't provide a certificate",
  3807. "Continue? (Y/N)", 3, NULL, 0);
  3808. if (!ok) {
  3809. if (tn_deb || debses)
  3810. tn_debug("[TLS - FAILED]");
  3811. else if ( verbosity )
  3812. printf("[TLS - FAILED]\r\n");
  3813. debug(F110,
  3814. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3815. return -1;
  3816. }
  3817. }
  3818. } else if (ssl_check_server_name(tls_http_con, hostname)) {
  3819. if (tn_deb || debses)
  3820. tn_debug("[TLS - FAILED]");
  3821. else if ( verbosity )
  3822. printf("[TLS - FAILED]\r\n");
  3823. debug(F110,
  3824. "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3825. return -1;
  3826. }
  3827. }
  3828. printf("[TLS - OK]\r\n");
  3829. if (tn_deb || debses)
  3830. tn_debug("[TLS - OK]");
  3831. debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
  3832. ssl_display_connect_details(tls_http_con,0,verbosity);
  3833. }
  3834. }
  3835. return(0); /* success */
  3836. }
  3837. #endif /* NOHTTP */
  3838. int
  3839. ck_ssl_renegotiate_ciphers()
  3840. {
  3841. if ( !ck_ssleay_is_installed() )
  3842. return(0);
  3843. if ( !sstelnet )
  3844. return(0);
  3845. if ( ssl_active_flag )
  3846. return SSL_renegotiate(ssl_con);
  3847. else if ( tls_active_flag )
  3848. return SSL_renegotiate(tls_con);
  3849. return(0);
  3850. }
  3851. #ifdef NT
  3852. int
  3853. ck_X509_save_cert_to_user_store(X509 *cert)
  3854. {
  3855. #ifdef X509V3_EXT_DUMP_UNKNOWN
  3856. char path[CKMAXPATH];
  3857. char hash[16];
  3858. char * GetAppData(int);
  3859. BIO * out=NULL;
  3860. if ( cert == NULL )
  3861. return(0);
  3862. sprintf(hash,"%08lx",X509_subject_name_hash(cert));
  3863. ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs/",
  3864. hash,".0");
  3865. out=BIO_new(BIO_s_file());
  3866. if (out == NULL)
  3867. {
  3868. ERR_print_errors(bio_err);
  3869. return(0);
  3870. }
  3871. if (BIO_write_filename(out,path) <= 0) {
  3872. perror(path);
  3873. return(0);
  3874. }
  3875. X509_print_ex(out, cert, XN_FLAG_SEP_MULTILINE, X509V3_EXT_DUMP_UNKNOWN);
  3876. if (!PEM_write_bio_X509(out,cert)) {
  3877. BIO_printf(bio_err,"unable to write certificate\n");
  3878. ERR_print_errors(bio_err);
  3879. BIO_free_all(out);
  3880. return(0);
  3881. }
  3882. BIO_free_all(out);
  3883. return(1);
  3884. #else /* X509V3_EXT_DUMP_UNKNOWN */
  3885. return(0);
  3886. #endif /* X509V3_EXT_DUMP_UNKNOWN */
  3887. }
  3888. #endif /* NT */
  3889. #ifndef OS2
  3890. /* The following function should be replaced by institution specific */
  3891. /* code that will convert an X509 cert structure to a userid for the */
  3892. /* purposes of client to host login. The example code included */
  3893. /* simply returns the UID field of the Subject if it exists. */
  3894. /* X509_to_user() returns 0 if valid userid in 'userid', else -1 */
  3895. int
  3896. X509_to_user(X509 *peer_cert, char *userid, int len)
  3897. {
  3898. #ifdef X509_UID_TO_USER
  3899. /* BEGIN EXAMPLE */
  3900. int err;
  3901. if (!(peer_cert && userid) || len <= 0)
  3902. return -1;
  3903. userid[0] = '\0';
  3904. debug(F110,"X509_to_user() subject",
  3905. X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
  3906. /* userid is in cert subject /UID */
  3907. err = X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
  3908. #ifndef NID_x500UniqueIdentifier
  3909. NID_uniqueIdentifier,
  3910. #else
  3911. NID_x500UniqueIdentifier,
  3912. #endif
  3913. userid, len);
  3914. debug(F111,"X509_to_user() userid",userid,err);
  3915. if (err > 0)
  3916. return 0;
  3917. /* END EXAMPLE */
  3918. #else /* X509_UID_TO_USER */
  3919. #ifdef X509_SUBJECT_ALT_NAME_TO_USER
  3920. /* BEGIN EXAMPLE */
  3921. int i;
  3922. X509_EXTENSION *ext = NULL;
  3923. STACK_OF(GENERAL_NAME) *ialt = NULL;
  3924. GENERAL_NAME *gen = NULL;
  3925. char email[256];
  3926. if (!(peer_cert && userid) || len <= 0)
  3927. return -1;
  3928. userid[0] = '\0';
  3929. email[0] = '\0';
  3930. debug(F110,"X509_to_user() subject",
  3931. X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
  3932. if ((i = X509_get_ext_by_NID(peer_cert, NID_subject_alt_name, -1))<0)
  3933. return -1;
  3934. if (!(ext = X509_get_ext(peer_cert, i)))
  3935. return -1;
  3936. X509V3_add_standard_extensions();
  3937. if (!(ialt = X509V3_EXT_d2i(ext)))
  3938. return -1;
  3939. for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
  3940. gen = sk_GENERAL_NAME_value(ialt, i);
  3941. if (gen->type == GEN_DNS) {
  3942. if (!gen->d.ia5 || !gen->d.ia5->length)
  3943. break;
  3944. if (strlen(gen->d.ia5->data) != gen->d.ia5->length) {
  3945. /* Ignoring IA5String containing null character */
  3946. continue;
  3947. }
  3948. if ( gen->d.ia5->length + 1 > sizeof(email) ) {
  3949. goto cleanup;
  3950. }
  3951. memcpy(email, gen->d.ia5->data, gen->d.ia5->length);
  3952. email[gen->d.ia5->length] = 0;
  3953. break;
  3954. }
  3955. }
  3956. cleanup:
  3957. X509V3_EXT_cleanup();
  3958. if (ialt)
  3959. sk_GENERAL_NAME_free(ialt);
  3960. debug(F110,"X509_to_user() email",email,0);
  3961. if ( email[0] ) {
  3962. char * domain = NULL;
  3963. /* Find domain */
  3964. for ( i=0 ; email[i] ; i++ ) {
  3965. if ( email[i] == '@' ) {
  3966. email[i] = '\0';
  3967. domain = &email[i+1];
  3968. break;
  3969. }
  3970. }
  3971. if ( domain ) {
  3972. /* XXX - Put code to Verify domain here */
  3973. if ( /* domain is okay */ 1 )
  3974. ckstrncpy(userid,email,len);
  3975. }
  3976. }
  3977. return(userid[0] ? 0 : -1);
  3978. /* END EXAMPLE */
  3979. #endif /* X509_SUBJECT_ALT_NAME_TO_USER */
  3980. #endif /* X509_UID_TO_USER */
  3981. return -1;
  3982. }
  3983. /* The following function should be replaced by institution specific */
  3984. /* code that will determine whether or not the combination of the */
  3985. /* provided X509 certificate and username is valid for automatic */
  3986. /* login. Whereas X509_to_user() is used to provide authentication */
  3987. /* of the user, the X509_userok() function is used to provide */
  3988. /* authorization. The certificate passed into X509_userok() does */
  3989. /* need to map to a userid; nor would the userid it would map to */
  3990. /* need to match the userid provided to the function. There are */
  3991. /* numerous circumstances in which it is beneficial to have the ability */
  3992. /* for multiple users to gain access to a common account such as */
  3993. /* 'root' on Unix; or a class account on a web server. In Unix we */
  3994. /* implement this capability with the ~userid/.tlslogin file which */
  3995. /* a list of X509 certificates which may be used to access the */
  3996. /* account 'userid'. */
  3997. /* X509_to_user() returns 0 if access is denied; 1 is access is permitted */
  3998. int
  3999. X509_userok(X509 * peer_cert, const char * userid)
  4000. {
  4001. #ifndef VMS
  4002. /* check if clients cert is in "user"'s ~/.tlslogin file */
  4003. char buf[512];
  4004. int r = 0;
  4005. FILE *fp;
  4006. struct passwd *pwd;
  4007. X509 *file_cert;
  4008. const ASN1_BIT_STRING *peer_sig, *file_sig;
  4009. if ( peer_cert == NULL )
  4010. return(0);
  4011. X509_get0_signature(&peer_sig, NULL, peer_cert);
  4012. if (!(pwd = getpwnam(userid)))
  4013. return 0;
  4014. if (strlen(pwd->pw_dir) > 500)
  4015. return(0);
  4016. sprintf(buf, "%s/.tlslogin", pwd->pw_dir);
  4017. if (!(fp = fopen(buf, "r")))
  4018. return 0;
  4019. while (!r && (file_cert = PEM_read_X509(fp, NULL, NULL, NULL))) {
  4020. X509_get0_signature(&file_sig, NULL, file_cert);
  4021. if (!ASN1_STRING_cmp(peer_sig, file_sig))
  4022. r = 1;
  4023. X509_free(file_cert);
  4024. }
  4025. fclose(fp);
  4026. return(r);
  4027. #else /* VMS */
  4028. /* Need to implement an appropriate function for VMS */
  4029. return(0);
  4030. #endif /* VMS */
  4031. }
  4032. #endif /* OS2 */
  4033. #endif /* CK_SSL */