2
0

wolfSSL.cs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /* wolfSSL.cs
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. using System;
  22. using System.Runtime.InteropServices;
  23. using System.Text;
  24. using System.Threading;
  25. using System.IO;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. namespace wolfSSL.CSharp {
  29. public class wolfssl
  30. {
  31. private const string wolfssl_dll = "wolfssl.dll";
  32. /* wait for 6 seconds default on TCP socket state poll if timeout not set */
  33. private const int WC_WAIT = 6000000;
  34. /********************************
  35. * Class for DTLS connections
  36. */
  37. /// <summary>
  38. /// Contains information regarding a DTLS connection having UdpClient udp and IPEndPoint ep.
  39. /// Used to keep memory alive.
  40. /// </summary>
  41. public class DTLS_con
  42. {
  43. public UdpClient udp;
  44. public IPEndPoint ep;
  45. }
  46. /********************************
  47. * Class for keeping ctx handles alive
  48. */
  49. [StructLayout(LayoutKind.Sequential)]
  50. private class ctx_handle
  51. {
  52. private GCHandle rec_cb;
  53. private GCHandle snd_cb;
  54. private GCHandle psk_cb;
  55. private GCHandle vrf_cb;
  56. private IntPtr ctx;
  57. public void set_receive(GCHandle input)
  58. {
  59. this.rec_cb = input;
  60. }
  61. public GCHandle get_receive()
  62. {
  63. return this.rec_cb;
  64. }
  65. public void set_send(GCHandle input)
  66. {
  67. this.snd_cb = input;
  68. }
  69. public GCHandle get_send()
  70. {
  71. return this.snd_cb;
  72. }
  73. public void set_psk(GCHandle input)
  74. {
  75. this.psk_cb = input;
  76. }
  77. public GCHandle get_psk()
  78. {
  79. return this.psk_cb;
  80. }
  81. public void set_vrf(GCHandle input)
  82. {
  83. if (!Object.Equals(this.vrf_cb, default(GCHandle)))
  84. {
  85. this.vrf_cb.Free();
  86. }
  87. this.vrf_cb = input;
  88. }
  89. public GCHandle get_vrf()
  90. {
  91. return this.vrf_cb;
  92. }
  93. public void set_ctx(IntPtr input)
  94. {
  95. this.ctx = input;
  96. }
  97. public IntPtr get_ctx()
  98. {
  99. return this.ctx;
  100. }
  101. /// <summary>
  102. /// Called to free the pointers keeping handles alive
  103. /// </summary>
  104. public void free()
  105. {
  106. log(INFO_LOG, "freeing ctx handle");
  107. if (!Object.Equals(this.rec_cb, default(GCHandle)))
  108. {
  109. this.rec_cb.Free();
  110. }
  111. if (!Object.Equals(this.snd_cb, default(GCHandle)))
  112. {
  113. this.snd_cb.Free();
  114. }
  115. if (!Object.Equals(this.psk_cb, default(GCHandle)))
  116. {
  117. this.psk_cb.Free();
  118. }
  119. if (!Object.Equals(this.vrf_cb, default(GCHandle)))
  120. {
  121. this.vrf_cb.Free();
  122. }
  123. }
  124. }
  125. /********************************
  126. * Class for keeping ssl handle alive
  127. */
  128. [StructLayout(LayoutKind.Sequential)]
  129. private class ssl_handle
  130. {
  131. private GCHandle fd_pin;
  132. private GCHandle psk_cb;
  133. private GCHandle vrf_cb;
  134. private IntPtr ssl;
  135. public void set_fd(GCHandle input)
  136. {
  137. this.fd_pin = input;
  138. }
  139. public GCHandle get_fd()
  140. {
  141. return this.fd_pin;
  142. }
  143. public void set_psk(GCHandle input)
  144. {
  145. this.psk_cb = input;
  146. }
  147. public GCHandle get_psk()
  148. {
  149. return this.psk_cb;
  150. }
  151. public void set_vrf(GCHandle input)
  152. {
  153. if (!Object.Equals(this.vrf_cb, default(GCHandle)))
  154. {
  155. this.vrf_cb.Free();
  156. }
  157. this.vrf_cb = input;
  158. }
  159. public GCHandle get_vrf()
  160. {
  161. return this.vrf_cb;
  162. }
  163. public void set_ssl(IntPtr input)
  164. {
  165. this.ssl = input;
  166. }
  167. public IntPtr get_ssl()
  168. {
  169. return this.ssl;
  170. }
  171. public void free()
  172. {
  173. log(INFO_LOG, "freeing ssl handle");
  174. if (!Object.Equals(this.fd_pin, default(GCHandle)))
  175. {
  176. this.fd_pin.Free();
  177. }
  178. if (!Object.Equals(this.psk_cb, default(GCHandle)))
  179. {
  180. this.psk_cb.Free();
  181. }
  182. if (!Object.Equals(this.vrf_cb, default(GCHandle)))
  183. {
  184. this.vrf_cb.Free();
  185. }
  186. }
  187. }
  188. /********************************
  189. * Init wolfSSL library
  190. */
  191. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  192. private extern static int wolfSSL_Init();
  193. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  194. private extern static int wolfSSL_Cleanup();
  195. /********************************
  196. * Methods of connection
  197. */
  198. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  199. private extern static IntPtr wolfTLSv1_2_server_method();
  200. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  201. private extern static IntPtr wolfTLSv1_3_server_method();
  202. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  203. private extern static IntPtr wolfSSLv23_server_method();
  204. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  205. private extern static IntPtr wolfTLSv1_2_client_method();
  206. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  207. private extern static IntPtr wolfTLSv1_3_client_method();
  208. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  209. private extern static IntPtr wolfSSLv23_client_method();
  210. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  211. private extern static IntPtr wolfDTLSv1_2_server_method();
  212. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  213. private extern static IntPtr wolfDTLSv1_2_client_method();
  214. /********************************
  215. * Call backs
  216. */
  217. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  218. public delegate int CallbackIORecv_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
  219. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  220. private extern static int wolfSSL_CTX_SetIORecv(IntPtr ctx, CallbackIORecv_delegate recv);
  221. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  222. private extern static int wolfSSL_SetIOReadCtx(IntPtr ssl, IntPtr rctx);
  223. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  224. private extern static IntPtr wolfSSL_GetIOReadCtx(IntPtr ssl);
  225. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  226. public delegate int CallbackIOSend_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx);
  227. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  228. private extern static int wolfSSL_CTX_SetIOSend(IntPtr ctx, CallbackIOSend_delegate send);
  229. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  230. private extern static int wolfSSL_SetIOWriteCtx(IntPtr ssl, IntPtr wctx);
  231. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  232. private extern static IntPtr wolfSSL_GetIOWriteCtx(IntPtr ssl);
  233. /********************************
  234. * CTX structure
  235. */
  236. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  237. private extern static IntPtr wolfSSL_CTX_new(IntPtr method);
  238. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  239. private extern static int wolfSSL_CTX_use_certificate_file(IntPtr ctx, string file, int type);
  240. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  241. private extern static int wolfSSL_CTX_load_verify_locations(IntPtr ctx, string file, string path);
  242. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  243. private extern static int wolfSSL_CTX_use_PrivateKey_file(IntPtr ctx, string file, int type);
  244. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  245. private extern static void wolfSSL_CTX_free(IntPtr ctx);
  246. /********************************
  247. * PSK
  248. */
  249. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  250. public delegate uint psk_delegate(IntPtr ssl, string identity, IntPtr key, uint max_sz);
  251. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  252. public delegate uint psk_client_delegate(IntPtr ssl, string hint, IntPtr identity, uint id_max_len, IntPtr key, uint max_sz);
  253. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  254. private extern static void wolfSSL_set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb);
  255. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  256. private extern static void wolfSSL_CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb);
  257. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  258. private extern static void wolfSSL_CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb);
  259. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  260. private extern static int wolfSSL_CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder identity);
  261. /********************************
  262. * SSL Structure
  263. */
  264. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  265. private extern static IntPtr wolfSSL_new(IntPtr ctx);
  266. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  267. private extern static int wolfSSL_accept(IntPtr ssl);
  268. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  269. private extern static int wolfSSL_connect(IntPtr ssl);
  270. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  271. private extern static int wolfSSL_read(IntPtr ssl, IntPtr buf, int sz);
  272. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  273. private extern static int wolfSSL_write(IntPtr ssl, IntPtr buf, int sz);
  274. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  275. private extern static int wolfSSL_shutdown(IntPtr ssl);
  276. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  277. private extern static void wolfSSL_free(IntPtr ssl);
  278. /********************************
  279. * Cipher lists
  280. */
  281. /* only supports full name from cipher_name[] delimited by : */
  282. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  283. private extern static int wolfSSL_CTX_set_cipher_list(IntPtr ctx, StringBuilder ciphers);
  284. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  285. private extern static int wolfSSL_set_cipher_list(IntPtr ssl, StringBuilder ciphers);
  286. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  287. private extern static int wolfSSL_get_ciphers(StringBuilder ciphers, int sz);
  288. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  289. private extern static IntPtr wolfSSL_get_cipher(IntPtr ssl);
  290. [DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  291. private extern static IntPtr wolfSSL_CIPHER_get_name(IntPtr cipher);
  292. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  293. private extern static IntPtr wolfSSL_get_current_cipher(IntPtr ssl);
  294. [DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  295. private extern static IntPtr wolfSSL_get_version(IntPtr ssl);
  296. [DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  297. private extern static IntPtr wolfSSL_get_cipher_list(IntPtr ssl);
  298. /********************************
  299. * Error logging
  300. */
  301. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  302. private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut);
  303. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  304. private extern static int wolfSSL_get_error(IntPtr ssl, int err);
  305. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  306. public delegate void loggingCb(int lvl, StringBuilder msg);
  307. private static loggingCb internal_log;
  308. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  309. private extern static void wolfSSL_Debugging_ON();
  310. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  311. private extern static void wolfSSL_Debugging_OFF();
  312. /********************************
  313. * DH
  314. */
  315. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  316. private extern static int wolfSSL_CTX_SetMinDhKey_Sz(IntPtr ctx, short size);
  317. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  318. private extern static int wolfSSL_SetTmpDH_file(IntPtr ssl, StringBuilder dhParam, int type);
  319. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  320. private extern static int wolfSSL_CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhParam, int type);
  321. /********************************
  322. * Verify Callback
  323. */
  324. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  325. public delegate int CallbackVerify_delegate(int ret, IntPtr x509_ctx);
  326. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  327. private extern static void wolfSSL_CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc);
  328. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  329. private extern static void wolfSSL_set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc);
  330. /********************************
  331. * X509 Store
  332. */
  333. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  334. private extern static IntPtr wolfSSL_X509_STORE_CTX_get_current_cert(IntPtr x509Ctx);
  335. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  336. private extern static int wolfSSL_X509_STORE_CTX_get_error(IntPtr sk);
  337. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  338. private extern static IntPtr wolfSSL_X509_STORE_GetCerts(IntPtr x509Ctx);
  339. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  340. private extern static int wolfSSL_sk_X509_num(IntPtr sk);
  341. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  342. private extern static void wolfSSL_sk_X509_free(IntPtr sk);
  343. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  344. private extern static IntPtr wolfSSL_sk_X509_pop(IntPtr sk);
  345. /********************************
  346. * Enum types from wolfSSL library
  347. */
  348. public static readonly int SSL_FILETYPE_PEM = 1;
  349. public static readonly int SSL_FILETYPE_ASN1 = 2;
  350. public static readonly int SSL_FILETYPE_RAW = 3;
  351. public static readonly int SSL_VERIFY_NONE = 0;
  352. public static readonly int SSL_VERIFY_PEER = 1;
  353. public static readonly int SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 2;
  354. public static readonly int SSL_VERIFY_CLIENT_ONCE = 4;
  355. public static readonly int SSL_VERIFY_POST_HANDSHAKE = 8;
  356. public static readonly int SSL_VERIFY_FAIL_EXCEPT_PSK = 16;
  357. public static readonly int CBIO_ERR_GENERAL = -1;
  358. public static readonly int CBIO_ERR_WANT_READ = -2;
  359. public static readonly int CBIO_ERR_WANT_WRITE = -2;
  360. public static readonly int CBIO_ERR_CONN_RST = -3;
  361. public static readonly int CBIO_ERR_ISR = -4;
  362. public static readonly int CBIO_ERR_CONN_CLOSE = -5;
  363. public static readonly int CBIO_ERR_TIMEOUT = -6;
  364. public static readonly int ERROR_LOG = 0;
  365. public static readonly int INFO_LOG = 1;
  366. public static readonly int ENTER_LOG = 2;
  367. public static readonly int LEAVE_LOG = 3;
  368. public static readonly int OTHER_LOG = 4;
  369. public static readonly int SUCCESS = 1;
  370. public static readonly int FAILURE = 0;
  371. private static IntPtr unwrap_ctx(IntPtr ctx)
  372. {
  373. try {
  374. GCHandle gch = GCHandle.FromIntPtr(ctx);
  375. ctx_handle handles = (ctx_handle)gch.Target;
  376. return handles.get_ctx();
  377. } catch (Exception e)
  378. {
  379. log(ERROR_LOG, "wolfssl ctx pointer is incorrect " + e);
  380. return IntPtr.Zero;
  381. }
  382. }
  383. private static IntPtr unwrap_ssl(IntPtr ssl)
  384. {
  385. try {
  386. GCHandle gch = GCHandle.FromIntPtr(ssl);
  387. ssl_handle handles = (ssl_handle)gch.Target;
  388. return handles.get_ssl();
  389. } catch (Exception e)
  390. {
  391. log(ERROR_LOG, "wolfssl pointer is incorrect " + e);
  392. return IntPtr.Zero;
  393. }
  394. }
  395. /// <summary>
  396. /// Call back to allow receiving TLS information
  397. /// </summary>
  398. /// <param name="ssl">structure of ssl passed in</param>
  399. /// <param name="buf">buffer to contain received msg</param>
  400. /// <param name="sz">size of buffer</param>
  401. /// <param name="ctx">optional information passed in</param>
  402. /// <returns>size of message received</returns>
  403. private static int wolfSSLCbIORecv(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
  404. {
  405. if (sz <= 0)
  406. {
  407. log(ERROR_LOG, "wolfssl receive error, size less than 0");
  408. return wolfssl.CBIO_ERR_GENERAL;
  409. }
  410. int amtRecv = 0;
  411. try
  412. {
  413. System.Runtime.InteropServices.GCHandle gch;
  414. gch = GCHandle.FromIntPtr(ctx);
  415. Socket con = (System.Net.Sockets.Socket)gch.Target;
  416. Byte[] msg = new Byte[sz];
  417. amtRecv = con.Receive(msg, msg.Length, 0);
  418. if (amtRecv == 0)
  419. {
  420. /* No data received so check for a response to see if connection is still open */
  421. if (con.Poll((con.ReceiveTimeout > 0) ? con.ReceiveTimeout : WC_WAIT, SelectMode.SelectRead))
  422. {
  423. log(ERROR_LOG, "socket connection issue, suspected connection termination.");
  424. return wolfssl.CBIO_ERR_CONN_CLOSE;
  425. }
  426. }
  427. Marshal.Copy(msg, 0, buf, sz);
  428. }
  429. catch (Exception e)
  430. {
  431. log(ERROR_LOG, "Error in receive " + e.ToString());
  432. return wolfssl.CBIO_ERR_CONN_CLOSE;
  433. }
  434. return amtRecv;
  435. }
  436. /// <summary>
  437. /// Call back used for sending TLS information
  438. /// </summary>
  439. /// <param name="ssl">pointer to ssl struct</param>
  440. /// <param name="buf">buffer containing information to send</param>
  441. /// <param name="sz">size of buffer to send</param>
  442. /// <param name="ctx">optional information</param>
  443. /// <returns>amount of information sent</returns>
  444. private static int wolfSSLCbIOSend(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
  445. {
  446. if (sz <= 0)
  447. {
  448. log(ERROR_LOG, "wolfssl send error, size less than 0");
  449. return wolfssl.CBIO_ERR_GENERAL;
  450. }
  451. try
  452. {
  453. System.Runtime.InteropServices.GCHandle gch;
  454. gch = GCHandle.FromIntPtr(ctx);
  455. Socket con = (System.Net.Sockets.Socket)gch.Target;
  456. Byte[] msg = new Byte[sz];
  457. Marshal.Copy(buf, msg, 0, sz);
  458. if (con.Send(msg, 0, msg.Length, SocketFlags.None) == 0 && sz != 0)
  459. {
  460. /* no data sent and msg size is larger then 0, check for lost connection */
  461. if (con.Poll((con.SendTimeout > 0) ? con.SendTimeout : WC_WAIT, SelectMode.SelectWrite))
  462. {
  463. log(ERROR_LOG, "socket connection issue, suspect connection termination");
  464. return wolfssl.CBIO_ERR_CONN_CLOSE;
  465. }
  466. }
  467. return sz;
  468. }
  469. catch (Exception e)
  470. {
  471. log(ERROR_LOG, "socket connection issue " + e.ToString());
  472. return wolfssl.CBIO_ERR_CONN_CLOSE;
  473. }
  474. }
  475. /// <summary>
  476. /// Call back used for sending DTLS information
  477. /// </summary>
  478. /// <param name="ssl">pointer to ssl struct</param>
  479. /// <param name="buf">buffer containing information to send</param>
  480. /// <param name="sz">size of buffer to send</param>
  481. /// <param name="ctx">optional information</param>
  482. /// <returns>amount of information sent</returns>
  483. private static int wolfSSL_dtlsCbIOSend(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
  484. {
  485. if (sz <= 0)
  486. {
  487. log(ERROR_LOG, "wolfssl dtls send error, size less than 0");
  488. return wolfssl.CBIO_ERR_GENERAL;
  489. }
  490. try
  491. {
  492. System.Runtime.InteropServices.GCHandle gch;
  493. gch = GCHandle.FromIntPtr(ctx);
  494. DTLS_con con = (DTLS_con)gch.Target;
  495. Byte[] msg = new Byte[sz];
  496. Marshal.Copy(buf, msg, 0, sz);
  497. con.udp.Send(msg, msg.Length, con.ep);
  498. return msg.Length;
  499. }
  500. catch (Exception e)
  501. {
  502. log(ERROR_LOG, "socket connection issue " + e.ToString());
  503. return wolfssl.CBIO_ERR_CONN_CLOSE;
  504. }
  505. }
  506. /// <summary>
  507. /// Call back to allow receiving DTLS information
  508. /// </summary>
  509. /// <param name="ssl">structure of ssl passed in</param>
  510. /// <param name="buf">buffer to contain received msg</param>
  511. /// <param name="sz">size of buffer</param>
  512. /// <param name="ctx">optional information passed in</param>
  513. /// <returns>size of message received</returns>
  514. private static int wolfSSL_dtlsCbIORecv(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx)
  515. {
  516. if (sz <= 0)
  517. {
  518. log(ERROR_LOG, "wolfssl dtls receive error, size less than 0");
  519. return wolfssl.CBIO_ERR_GENERAL;
  520. }
  521. try
  522. {
  523. System.Runtime.InteropServices.GCHandle gch;
  524. gch = GCHandle.FromIntPtr(ctx);
  525. DTLS_con con = (DTLS_con)gch.Target;
  526. Byte[] msg = con.udp.Receive(ref con.ep);
  527. if (msg.Length > sz)
  528. {
  529. log(ERROR_LOG, "wolfssl DTLS packet received was larger than buffer");
  530. return wolfssl.CBIO_ERR_GENERAL;
  531. }
  532. Marshal.Copy(msg, 0, buf, msg.Length);
  533. return msg.Length;
  534. }
  535. catch (Exception e)
  536. {
  537. /* issue with receive or size of buffer */
  538. log(ERROR_LOG, "socket read issue " + e.ToString());
  539. return wolfssl.CBIO_ERR_CONN_CLOSE;
  540. }
  541. }
  542. /// <summary>
  543. /// Create a new ssl structure
  544. /// </summary>
  545. /// <param name="ctx">structure to create ssl structure from</param>
  546. /// <returns>pointer to ssl structure</returns>
  547. public static IntPtr new_ssl(IntPtr ctx)
  548. {
  549. if (ctx == IntPtr.Zero)
  550. return IntPtr.Zero;
  551. try
  552. {
  553. ssl_handle io;
  554. IntPtr local_ctx = unwrap_ctx(ctx);
  555. if (local_ctx == IntPtr.Zero)
  556. {
  557. log(ERROR_LOG, "new_ssl ctx unwrap error");
  558. return IntPtr.Zero;
  559. }
  560. io = new ssl_handle();
  561. io.set_ssl(wolfSSL_new(local_ctx));
  562. /* check if null */
  563. if (io.get_ssl() == IntPtr.Zero)
  564. {
  565. return IntPtr.Zero;
  566. }
  567. /* keep memory pinned to be able to reference by address */
  568. return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
  569. }
  570. catch (Exception e)
  571. {
  572. log(ERROR_LOG, e.ToString());
  573. return IntPtr.Zero;
  574. }
  575. }
  576. /// <summary>
  577. /// Used for a server to accept a connection
  578. /// </summary>
  579. /// <param name="ssl">structure containing info for connection</param>
  580. /// <returns>1 on success</returns>
  581. public static int accept(IntPtr ssl)
  582. {
  583. if (ssl == IntPtr.Zero)
  584. return FAILURE;
  585. try
  586. {
  587. IntPtr sslCtx = unwrap_ssl(ssl);
  588. if (sslCtx == IntPtr.Zero)
  589. {
  590. log(ERROR_LOG, "accept ssl unwrap error");
  591. return FAILURE;
  592. }
  593. return wolfSSL_accept(sslCtx);
  594. }
  595. catch (Exception e)
  596. {
  597. log(ERROR_LOG, "accept error " + e.ToString());
  598. return FAILURE;
  599. }
  600. }
  601. /// <summary>
  602. /// Used for a client to connect
  603. /// </summary>
  604. /// <param name="ssl">structure containing connection info</param>
  605. /// <returns>1 on success</returns>
  606. public static int connect(IntPtr ssl)
  607. {
  608. if (ssl == IntPtr.Zero)
  609. return FAILURE;
  610. try
  611. {
  612. IntPtr sslCtx = unwrap_ssl(ssl);
  613. if (sslCtx == IntPtr.Zero)
  614. {
  615. log(ERROR_LOG, "connect ssl unwrap error");
  616. return FAILURE;
  617. }
  618. return wolfSSL_connect(sslCtx);
  619. }
  620. catch (Exception e)
  621. {
  622. log(ERROR_LOG, "connect error " + e.ToString());
  623. return FAILURE;
  624. }
  625. }
  626. /// <summary>
  627. /// Read message from secure connection
  628. /// </summary>
  629. /// <param name="ssl">structure containing info about connection</param>
  630. /// <param name="buf">object to hold incoming message (Unicode format)</param>
  631. /// <param name="sz">size of available memory in buf</param>
  632. /// <returns>amount of data read on success</returns>
  633. public static int read(IntPtr ssl, StringBuilder buf, int sz)
  634. {
  635. if (ssl == IntPtr.Zero)
  636. return FAILURE;
  637. try
  638. {
  639. IntPtr sslCtx = unwrap_ssl(ssl);
  640. IntPtr data;
  641. int ret;
  642. byte[] msg;
  643. buf.Clear(); /* Clear incoming buffer */
  644. if (sslCtx == IntPtr.Zero)
  645. {
  646. log(ERROR_LOG, "read ssl unwrap error");
  647. return FAILURE;
  648. }
  649. data = Marshal.AllocHGlobal(sz);
  650. ret = wolfSSL_read(sslCtx, data, sz);
  651. if (ret >= 0)
  652. {
  653. /* Get data that was sent across and store it using a literal read of
  654. * the conversion from bytes to character. Takes care of if
  655. * a null terminator is part of the message read.
  656. */
  657. msg = new byte[ret];
  658. Marshal.Copy(data, msg, 0, ret);
  659. for (int i = 0; i < ret; i++)
  660. {
  661. buf.Append(@Convert.ToChar(msg[i]));
  662. }
  663. }
  664. Marshal.FreeHGlobal(data);
  665. return ret;
  666. }
  667. catch (Exception e)
  668. {
  669. log(ERROR_LOG, "wolfssl read error " + e.ToString());
  670. return FAILURE;
  671. }
  672. }
  673. /// <summary>
  674. /// Read message from secure connection using a byte array
  675. /// </summary>
  676. /// <param name="ssl">structure containing info about connection</param>
  677. /// <param name="buf">object to hold incoming message (raw bytes)</param>
  678. /// <param name="sz">size of available memory in buf</param>
  679. /// <returns>amount of data read on success</returns>
  680. public static int read(IntPtr ssl, byte[] buf, int sz)
  681. {
  682. if (ssl == IntPtr.Zero)
  683. return FAILURE;
  684. try
  685. {
  686. IntPtr sslCtx = unwrap_ssl(ssl);
  687. IntPtr data;
  688. int ret;
  689. if (sslCtx == IntPtr.Zero)
  690. {
  691. log(ERROR_LOG, "read ssl unwrap error");
  692. return FAILURE;
  693. }
  694. data = Marshal.AllocHGlobal(sz);
  695. ret = wolfSSL_read(sslCtx, data, sz);
  696. if (ret >= 0)
  697. {
  698. Marshal.Copy(data, buf, 0, ret);
  699. }
  700. Marshal.FreeHGlobal(data);
  701. return ret;
  702. }
  703. catch (Exception e)
  704. {
  705. log(ERROR_LOG, "wolfssl read error " + e.ToString());
  706. return FAILURE;
  707. }
  708. }
  709. /// <summary>
  710. /// Write message to secure connection
  711. /// </summary>
  712. /// <param name="ssl">structure containing connection info</param>
  713. /// <param name="buf">message to send</param>
  714. /// <param name="sz">size of the message</param>
  715. /// <returns>amount sent on success</returns>
  716. public static int write(IntPtr ssl, StringBuilder buf, int sz)
  717. {
  718. if (ssl == IntPtr.Zero)
  719. return FAILURE;
  720. try
  721. {
  722. IntPtr sslCtx = unwrap_ssl(ssl);
  723. IntPtr data;
  724. int ret;
  725. if (sslCtx == IntPtr.Zero)
  726. {
  727. log(ERROR_LOG, "write ssl unwrap error");
  728. return FAILURE;
  729. }
  730. data = Marshal.AllocHGlobal(sz);
  731. Marshal.Copy(System.Text.Encoding.Default.GetBytes(buf.ToString()), 0,
  732. data, System.Text.Encoding.Default.GetByteCount(buf.ToString()));
  733. ret = wolfSSL_write(sslCtx, data, sz);
  734. Marshal.FreeHGlobal(data);
  735. return ret;
  736. }
  737. catch (Exception e)
  738. {
  739. log(ERROR_LOG, "wolfssl write error " + e.ToString());
  740. return FAILURE;
  741. }
  742. }
  743. /// <summary>
  744. /// Write message to secure connection
  745. /// </summary>
  746. /// <param name="ssl">structure containing connection info</param>
  747. /// <param name="buf">message to send</param>
  748. /// <param name="sz">size of the message</param>
  749. /// <returns>amount sent on success</returns>
  750. public static int write(IntPtr ssl, byte[] buf, int sz)
  751. {
  752. if (ssl == IntPtr.Zero)
  753. return FAILURE;
  754. try
  755. {
  756. IntPtr sslCtx = unwrap_ssl(ssl);
  757. IntPtr data;
  758. int ret;
  759. if (sslCtx == IntPtr.Zero)
  760. {
  761. log(ERROR_LOG, "write ssl unwrap error");
  762. return FAILURE;
  763. }
  764. data = Marshal.AllocHGlobal(sz);
  765. Marshal.Copy(buf, 0, data, sz);
  766. ret = wolfSSL_write(sslCtx, data, sz);
  767. Marshal.FreeHGlobal(data);
  768. return ret;
  769. }
  770. catch (Exception e)
  771. {
  772. log(ERROR_LOG, "wolfssl write error " + e.ToString());
  773. return FAILURE;
  774. }
  775. }
  776. /// <summary>
  777. /// Free information stored in ssl struct
  778. /// </summary>
  779. /// <param name="ssl">pointer to ssl struct to free</param>
  780. public static void free(IntPtr ssl)
  781. {
  782. try
  783. {
  784. IntPtr sslCtx;
  785. GCHandle gch = GCHandle.FromIntPtr(ssl);
  786. ssl_handle handles = (ssl_handle)gch.Target;
  787. sslCtx = handles.get_ssl();
  788. wolfSSL_free(sslCtx);
  789. handles.free();
  790. gch.Free();
  791. }
  792. catch (Exception e)
  793. {
  794. log(ERROR_LOG, "wolfssl free error " + e.ToString());
  795. }
  796. }
  797. /// <summary>
  798. /// Shutdown a connection
  799. /// </summary>
  800. /// <param name="ssl">pointer to ssl struct to close connection of</param>
  801. /// <returns>1 on success</returns>
  802. public static int shutdown(IntPtr ssl)
  803. {
  804. if (ssl == IntPtr.Zero)
  805. return FAILURE;
  806. try
  807. {
  808. IntPtr sslCtx = unwrap_ssl(ssl);
  809. if (sslCtx == IntPtr.Zero)
  810. {
  811. log(ERROR_LOG, "shutdown ssl unwrap error");
  812. return FAILURE;
  813. }
  814. return wolfSSL_shutdown(sslCtx);
  815. }
  816. catch (Exception e)
  817. {
  818. log(ERROR_LOG, "wolfssl shutdwon error " + e.ToString());
  819. return FAILURE;
  820. }
  821. }
  822. /// <summary>
  823. /// Optional, can be used to set a custom receive function
  824. /// </summary>
  825. /// <param name="ctx">structure to set receive function in</param>
  826. /// <param name="func">function to use when reading socket</param>
  827. public static void SetIORecv(IntPtr ctx, CallbackIORecv_delegate func)
  828. {
  829. try
  830. {
  831. GCHandle gch = GCHandle.FromIntPtr(ctx);
  832. ctx_handle handles = (ctx_handle)gch.Target;
  833. /* check if already stored handle needs freed */
  834. gch = handles.get_receive();
  835. if (!Object.Equals(gch, default(GCHandle)))
  836. {
  837. gch.Free();
  838. }
  839. /* keep new function alive */
  840. handles.set_receive(GCHandle.Alloc(func));
  841. wolfSSL_CTX_SetIORecv(handles.get_ctx(), func);
  842. }
  843. catch (Exception e)
  844. {
  845. log(ERROR_LOG, "wolfssl setIORecv error " + e.ToString());
  846. }
  847. }
  848. /// <summary>
  849. /// Optional, can be used to set a custom send function
  850. /// </summary>
  851. /// <param name="ctx">structure to set function in</param>
  852. /// <param name="func">function to use when sending data</param>
  853. public static void SetIOSend(IntPtr ctx, CallbackIOSend_delegate func)
  854. {
  855. try
  856. {
  857. GCHandle gch = GCHandle.FromIntPtr(ctx);
  858. ctx_handle handles = (ctx_handle)gch.Target;
  859. /* check if already stored handle needs freed */
  860. gch = handles.get_send();
  861. if (!Object.Equals(gch, default(GCHandle)))
  862. {
  863. gch.Free();
  864. }
  865. /* keep new function alive */
  866. handles.set_send(GCHandle.Alloc(func));
  867. wolfSSL_CTX_SetIOSend(handles.get_ctx(), func);
  868. }
  869. catch (Exception e)
  870. {
  871. log(ERROR_LOG, "wolfssl setIOSend error " + e.ToString());
  872. }
  873. }
  874. /// <summary>
  875. /// Create a new CTX structure
  876. /// </summary>
  877. /// <param name="method">method to use such as TLSv1.2</param>
  878. /// <returns>pointer to CTX structure</returns>
  879. public static IntPtr CTX_new(IntPtr method)
  880. {
  881. try
  882. {
  883. IntPtr ctx = wolfSSL_CTX_new(method);
  884. if (ctx == IntPtr.Zero)
  885. return ctx;
  886. ctx_handle io = new ctx_handle();
  887. io.set_ctx(ctx);
  888. CallbackIORecv_delegate recv = new CallbackIORecv_delegate(wolfssl.wolfSSLCbIORecv);
  889. io.set_receive(GCHandle.Alloc(recv));
  890. wolfSSL_CTX_SetIORecv(ctx, recv);
  891. CallbackIOSend_delegate send = new CallbackIOSend_delegate(wolfssl.wolfSSLCbIOSend);
  892. io.set_send(GCHandle.Alloc(send));
  893. wolfSSL_CTX_SetIOSend(ctx, send);
  894. /* keep memory pinned */
  895. return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
  896. }
  897. catch (Exception e)
  898. {
  899. log(ERROR_LOG, "ctx_new error " + e.ToString());
  900. return IntPtr.Zero;
  901. }
  902. }
  903. /// <summary>
  904. /// Create a new CTX structure for a DTLS connection
  905. /// </summary>
  906. /// <param name="method">Method to use in connection ie DTLSv1.2</param>
  907. /// <returns></returns>
  908. public static IntPtr CTX_dtls_new(IntPtr method)
  909. {
  910. try
  911. {
  912. IntPtr ctx = wolfSSL_CTX_new(method);
  913. if (ctx == IntPtr.Zero)
  914. return ctx;
  915. ctx_handle io = new ctx_handle();
  916. io.set_ctx(ctx);
  917. CallbackIORecv_delegate recv = new CallbackIORecv_delegate(wolfssl.wolfSSL_dtlsCbIORecv);
  918. io.set_receive(GCHandle.Alloc(recv));
  919. wolfSSL_CTX_SetIORecv(ctx, recv);
  920. CallbackIOSend_delegate send = new CallbackIOSend_delegate(wolfssl.wolfSSL_dtlsCbIOSend);
  921. io.set_send(GCHandle.Alloc(send));
  922. wolfSSL_CTX_SetIOSend(ctx, send);
  923. /* keep memory pinned */
  924. return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned));
  925. }
  926. catch (Exception e)
  927. {
  928. log(ERROR_LOG, "ctx_dtls_new error " + e.ToString());
  929. return IntPtr.Zero;
  930. }
  931. }
  932. /// <summary>
  933. /// Free information used in CTX structure
  934. /// </summary>
  935. /// <param name="ctx">structure to free</param>
  936. public static void CTX_free(IntPtr ctx)
  937. {
  938. try
  939. {
  940. GCHandle gch = GCHandle.FromIntPtr(ctx);
  941. ctx_handle handles = (ctx_handle)gch.Target;
  942. wolfSSL_CTX_free(handles.get_ctx());
  943. handles.free();
  944. gch.Free();
  945. }
  946. catch (Exception e)
  947. {
  948. log(ERROR_LOG, "wolfssl ctx free error " + e.ToString());
  949. }
  950. }
  951. /// <summary>
  952. /// Set identity hint to use
  953. /// </summary>
  954. /// <param name="ctx">pointer to structure of ctx to set hint in</param>
  955. /// <param name="hint">hint to use</param>
  956. /// <returns>1 on success</returns>
  957. public static int CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder hint)
  958. {
  959. try
  960. {
  961. IntPtr local_ctx = unwrap_ctx(ctx);
  962. if (local_ctx == IntPtr.Zero)
  963. {
  964. log(ERROR_LOG, "CTX use psk identity hint unwrap error");
  965. return FAILURE;
  966. }
  967. return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint);
  968. }
  969. catch (Exception e)
  970. {
  971. log(ERROR_LOG, "wolfssl psk identity hint error " + e.ToString());
  972. return FAILURE;
  973. }
  974. }
  975. /// <summary>
  976. /// Set the function to use for PSK connections
  977. /// </summary>
  978. /// <param name="ctx">pointer to CTX that the function is set in</param>
  979. /// <param name="psk_cb">PSK function to use</param>
  980. public static void CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb)
  981. {
  982. try
  983. {
  984. GCHandle gch = GCHandle.FromIntPtr(ctx);
  985. ctx_handle handles = (ctx_handle)gch.Target;
  986. handles.set_psk(GCHandle.Alloc(psk_cb));
  987. wolfSSL_CTX_set_psk_server_callback(handles.get_ctx(), psk_cb);
  988. }
  989. catch (Exception e)
  990. {
  991. log(ERROR_LOG, "wolfssl psk server callback error " + e.ToString());
  992. }
  993. }
  994. /// <summary>
  995. /// Set the function to use for PSK connections
  996. /// </summary>
  997. /// <param name="ctx">pointer to CTX that the function is set in</param>
  998. /// <param name="psk_cb">PSK function to use</param>
  999. public static void CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb)
  1000. {
  1001. try
  1002. {
  1003. GCHandle gch = GCHandle.FromIntPtr(ctx);
  1004. ctx_handle handles = (ctx_handle)gch.Target;
  1005. handles.set_psk(GCHandle.Alloc(psk_cb));
  1006. wolfSSL_CTX_set_psk_client_callback(handles.get_ctx(), psk_cb);
  1007. }
  1008. catch (Exception e)
  1009. {
  1010. log(ERROR_LOG, "wolfssl psk client callback error " + e.ToString());
  1011. }
  1012. }
  1013. /// <summary>
  1014. /// Set the function to use for PSK connections on a single TLS/DTLS connection
  1015. /// </summary>
  1016. /// <param name="ctx">pointer to SSL that the function is set in</param>
  1017. /// <param name="psk_cb">PSK function to use</param>
  1018. public static void set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb)
  1019. {
  1020. try
  1021. {
  1022. GCHandle gch = GCHandle.FromIntPtr(ssl);
  1023. ssl_handle handles = (ssl_handle)gch.Target;
  1024. handles.set_psk(GCHandle.Alloc(psk_cb));
  1025. wolfSSL_set_psk_server_callback(handles.get_ssl(), psk_cb);
  1026. }
  1027. catch (Exception e)
  1028. {
  1029. log(ERROR_LOG, "wolfssl psk server callback error " + e.ToString());
  1030. }
  1031. }
  1032. /// <summary>
  1033. /// Set Socket for TLS connection
  1034. /// </summary>
  1035. /// <param name="ssl">structure to set Socket in</param>
  1036. /// <param name="fd">Socket to use</param>
  1037. /// <returns>1 on success</returns>
  1038. public static int set_fd(IntPtr ssl, Socket fd)
  1039. {
  1040. /* sanity check on inputs */
  1041. if (ssl == IntPtr.Zero)
  1042. {
  1043. return FAILURE;
  1044. }
  1045. try
  1046. {
  1047. if (!fd.Equals(null))
  1048. {
  1049. GCHandle gch = GCHandle.FromIntPtr(ssl);
  1050. ssl_handle handles = (ssl_handle)gch.Target;
  1051. IntPtr sslCtx = handles.get_ssl();
  1052. IntPtr ptr;
  1053. GCHandle fd_pin = GCHandle.Alloc(fd);
  1054. if (sslCtx == IntPtr.Zero)
  1055. {
  1056. log(ERROR_LOG, "wolfssl error setting up fd!!");
  1057. return FAILURE;
  1058. }
  1059. handles.set_fd(fd_pin);
  1060. ptr = GCHandle.ToIntPtr(fd_pin);
  1061. wolfSSL_SetIOWriteCtx(sslCtx, ptr); //pass along the socket for writing to
  1062. wolfSSL_SetIOReadCtx(sslCtx, ptr); //pass along the socket for reading from
  1063. return SUCCESS;
  1064. }
  1065. return FAILURE;
  1066. }
  1067. catch (Exception e)
  1068. {
  1069. log(ERROR_LOG, "Error setting up fd!! " + e.ToString());
  1070. return FAILURE;
  1071. }
  1072. }
  1073. /// <summary>
  1074. /// Get socket of a TLS connection
  1075. /// </summary>
  1076. /// <param name="ssl">structure to get socket from</param>
  1077. /// <returns>Socket object used for connection</returns>
  1078. public static Socket get_fd(IntPtr ssl)
  1079. {
  1080. try
  1081. {
  1082. IntPtr ptr;
  1083. IntPtr sslCtx = unwrap_ssl(ssl);
  1084. if (sslCtx == IntPtr.Zero)
  1085. {
  1086. log(ERROR_LOG, "wolfssl get_fd error");
  1087. return null;
  1088. }
  1089. ptr = wolfSSL_GetIOReadCtx(sslCtx);
  1090. if (ptr != IntPtr.Zero)
  1091. {
  1092. GCHandle gch = GCHandle.FromIntPtr(ptr);
  1093. return (System.Net.Sockets.Socket)gch.Target;
  1094. }
  1095. return null;
  1096. }
  1097. catch (Exception e)
  1098. {
  1099. log(ERROR_LOG, "wolfssl get_fd error " + e.ToString());
  1100. return null;
  1101. }
  1102. }
  1103. /// <summary>
  1104. /// Set information needed to send and receive a DTLS connection
  1105. /// </summary>
  1106. /// <param name="ssl">structure to set information in</param>
  1107. /// <param name="udp">UDP object to send and receive</param>
  1108. /// <param name="ep">End point of connection</param>
  1109. /// <returns>1 on success</returns>
  1110. public static int set_dtls_fd(IntPtr ssl, UdpClient udp, IPEndPoint ep)
  1111. {
  1112. /* sanity check on inputs */
  1113. if (ssl == IntPtr.Zero)
  1114. {
  1115. return FAILURE;
  1116. }
  1117. try
  1118. {
  1119. if (!udp.Equals(null) && !ep.Equals(null))
  1120. {
  1121. IntPtr ptr;
  1122. DTLS_con con;
  1123. GCHandle gch = GCHandle.FromIntPtr(ssl);
  1124. ssl_handle handles = (ssl_handle)gch.Target;
  1125. GCHandle fd_pin;
  1126. con = new DTLS_con();
  1127. con.udp = udp;
  1128. con.ep = ep;
  1129. fd_pin = GCHandle.Alloc(con);
  1130. handles.set_fd(fd_pin);
  1131. ptr = GCHandle.ToIntPtr(fd_pin);
  1132. wolfSSL_SetIOWriteCtx(handles.get_ssl(), ptr); //pass along the socket for writing to
  1133. wolfSSL_SetIOReadCtx(handles.get_ssl(), ptr); //pass along the socket for reading from
  1134. return SUCCESS;
  1135. }
  1136. return FAILURE;
  1137. }
  1138. catch (Exception e)
  1139. {
  1140. log(ERROR_LOG, "Error setting up fd!! " + e.ToString());
  1141. return FAILURE;
  1142. }
  1143. }
  1144. /// <summary>
  1145. /// Get the pointer to DTLS_con class used for connection
  1146. /// </summary>
  1147. /// <param name="ssl">structure to get connection from</param>
  1148. /// <returns>DTLS_con object</returns>
  1149. public static DTLS_con get_dtls_fd(IntPtr ssl)
  1150. {
  1151. try
  1152. {
  1153. IntPtr ptr;
  1154. IntPtr sslCtx = unwrap_ssl(ssl);
  1155. if (sslCtx == IntPtr.Zero)
  1156. {
  1157. log(ERROR_LOG, "wolfssl get_dtls_fd error");
  1158. return null;
  1159. }
  1160. ptr = wolfSSL_GetIOReadCtx(sslCtx);
  1161. if (ptr != IntPtr.Zero)
  1162. {
  1163. GCHandle gch = GCHandle.FromIntPtr(ptr);
  1164. return (DTLS_con)gch.Target;
  1165. }
  1166. return null;
  1167. }
  1168. catch (Exception e)
  1169. {
  1170. log(ERROR_LOG, "wolfssl get_dtls_fd error " + e.ToString());
  1171. return null;
  1172. }
  1173. }
  1174. /// <summary>
  1175. /// Get available cipher suites
  1176. /// </summary>
  1177. /// <param name="list">list to fill with cipher suite names</param>
  1178. /// <param name="sz">size of list available to fill</param>
  1179. /// <returns>1 on success</returns>
  1180. public static int get_ciphers(StringBuilder list, int sz)
  1181. {
  1182. try
  1183. {
  1184. return wolfSSL_get_ciphers(list, sz);
  1185. }
  1186. catch (Exception e)
  1187. {
  1188. log(ERROR_LOG, "wolfssl get_ciphers error " + e.ToString());
  1189. return FAILURE;
  1190. }
  1191. }
  1192. /// <summary>
  1193. /// Initialize wolfSSL library
  1194. /// </summary>
  1195. /// <returns>1 on success</returns>
  1196. public static int Init()
  1197. {
  1198. try
  1199. {
  1200. return wolfSSL_Init();
  1201. }
  1202. catch (Exception e)
  1203. {
  1204. log(ERROR_LOG, "wolfssl init error " + e.ToString());
  1205. return FAILURE;
  1206. }
  1207. }
  1208. /// <summary>
  1209. /// Clean up wolfSSL library memory
  1210. /// </summary>
  1211. /// <returns>1 on success</returns>
  1212. public static int Cleanup()
  1213. {
  1214. try
  1215. {
  1216. return wolfSSL_Cleanup();
  1217. }
  1218. catch (Exception e)
  1219. {
  1220. log(ERROR_LOG, "wolfssl cleanup error " + e.ToString());
  1221. return FAILURE;
  1222. }
  1223. }
  1224. /// <summary>
  1225. /// Set up TLS version 1.2 method
  1226. /// </summary>
  1227. /// <returns>pointer to TLSv1.2 method</returns>
  1228. public static IntPtr useTLSv1_2_server()
  1229. {
  1230. try
  1231. {
  1232. return wolfTLSv1_2_server_method();
  1233. }
  1234. catch (Exception e)
  1235. {
  1236. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1237. return IntPtr.Zero;
  1238. }
  1239. }
  1240. /// <summary>
  1241. /// Set up TLS version 1.3 method
  1242. /// </summary>
  1243. /// <returns>pointer to TLSv1.3 method</returns>
  1244. public static IntPtr useTLSv1_3_server()
  1245. {
  1246. try
  1247. {
  1248. return wolfTLSv1_3_server_method();
  1249. }
  1250. catch (Exception e)
  1251. {
  1252. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1253. return IntPtr.Zero;
  1254. }
  1255. }
  1256. /// <summary>
  1257. /// Use any TLS version
  1258. /// </summary>
  1259. /// <returns>pointer to method</returns>
  1260. public static IntPtr usev23_server()
  1261. {
  1262. try
  1263. {
  1264. return wolfSSLv23_server_method();
  1265. }
  1266. catch (Exception e)
  1267. {
  1268. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1269. return IntPtr.Zero;
  1270. }
  1271. }
  1272. /// <summary>
  1273. /// Set up TLS version 1.2 method
  1274. /// </summary>
  1275. /// <returns>pointer to TLSv1.2 method</returns>
  1276. public static IntPtr useTLSv1_2_client()
  1277. {
  1278. try
  1279. {
  1280. return wolfTLSv1_2_client_method();
  1281. }
  1282. catch (Exception e)
  1283. {
  1284. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1285. return IntPtr.Zero;
  1286. }
  1287. }
  1288. /// <summary>
  1289. /// Set up TLS version 1.3 method
  1290. /// </summary>
  1291. /// <returns>pointer to TLSv1.3 method</returns>
  1292. public static IntPtr useTLSv1_3_client()
  1293. {
  1294. try
  1295. {
  1296. return wolfTLSv1_3_client_method();
  1297. }
  1298. catch (Exception e)
  1299. {
  1300. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1301. return IntPtr.Zero;
  1302. }
  1303. }
  1304. /// <summary>
  1305. /// Use any TLS version
  1306. /// </summary>
  1307. /// <returns>pointer to method</returns>
  1308. public static IntPtr usev23_client()
  1309. {
  1310. try
  1311. {
  1312. return wolfSSLv23_client_method();
  1313. }
  1314. catch (Exception e)
  1315. {
  1316. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1317. return IntPtr.Zero;
  1318. }
  1319. }
  1320. /// <summary>
  1321. /// Set up DTLS version 1.2
  1322. /// </summary>
  1323. /// <returns>pointer to DTLSv1.2 method</returns>
  1324. public static IntPtr useDTLSv1_2_server()
  1325. {
  1326. try
  1327. {
  1328. return wolfDTLSv1_2_server_method();
  1329. }
  1330. catch (Exception e)
  1331. {
  1332. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1333. return IntPtr.Zero;
  1334. }
  1335. }
  1336. /// <summary>
  1337. /// Set up DTLS version 1.2
  1338. /// </summary>
  1339. /// <returns>pointer to DTLSv1.2 method</returns>
  1340. public static IntPtr useDTLSv1_2_client()
  1341. {
  1342. try
  1343. {
  1344. return wolfDTLSv1_2_client_method();
  1345. }
  1346. catch (Exception e)
  1347. {
  1348. log(ERROR_LOG, "wolfssl error " + e.ToString());
  1349. return IntPtr.Zero;
  1350. }
  1351. }
  1352. /// <summary>
  1353. /// Gets the current cipher suite being used in connection
  1354. /// </summary>
  1355. /// <param name="ssl">SSL struct to get cipher suite from</param>
  1356. /// <returns>string containing current cipher suite</returns>
  1357. public static string get_current_cipher(IntPtr ssl)
  1358. {
  1359. if (ssl == IntPtr.Zero)
  1360. return null;
  1361. try
  1362. {
  1363. IntPtr ssl_cipher;
  1364. IntPtr ssl_cipher_ptr;
  1365. string ssl_cipher_str;
  1366. IntPtr sslCtx = unwrap_ssl(ssl);
  1367. if (sslCtx == IntPtr.Zero)
  1368. {
  1369. log(ERROR_LOG, "wolfssl get_current_cipher error");
  1370. return null;
  1371. }
  1372. ssl_cipher = wolfSSL_get_current_cipher(sslCtx);
  1373. ssl_cipher_ptr = wolfSSL_CIPHER_get_name(ssl_cipher);
  1374. ssl_cipher_str = Marshal.PtrToStringAnsi(ssl_cipher_ptr);
  1375. return ssl_cipher_str;
  1376. }
  1377. catch (Exception e)
  1378. {
  1379. log(ERROR_LOG, "wolfssl get current cipher error " + e.ToString());
  1380. return null;
  1381. }
  1382. }
  1383. /// <summary>
  1384. /// Set available cipher suites for all ssl structs created from ctx
  1385. /// </summary>
  1386. /// <param name="ctx">CTX structure to set</param>
  1387. /// <param name="list">List full of ciphers suites</param>
  1388. /// <returns>1 on success</returns>
  1389. public static int CTX_set_cipher_list(IntPtr ctx, StringBuilder list)
  1390. {
  1391. try
  1392. {
  1393. IntPtr local_ctx = unwrap_ctx(ctx);
  1394. if (local_ctx == IntPtr.Zero)
  1395. {
  1396. log(ERROR_LOG, "CTX set cipher list error");
  1397. return FAILURE;
  1398. }
  1399. return wolfSSL_CTX_set_cipher_list(local_ctx, list);
  1400. }
  1401. catch (Exception e)
  1402. {
  1403. log(ERROR_LOG, "wolfssl ctx set cipher list error " + e.ToString());
  1404. return FAILURE;
  1405. }
  1406. }
  1407. /// <summary>
  1408. /// Set available cipher suite in local connection
  1409. /// </summary>
  1410. /// <param name="ssl">Structure to set cipher suite in</param>
  1411. /// <param name="list">List of cipher suites</param>
  1412. /// <returns>1 on success</returns>
  1413. public static int set_cipher_list(IntPtr ssl, StringBuilder list)
  1414. {
  1415. try
  1416. {
  1417. IntPtr sslCtx = unwrap_ssl(ssl);
  1418. if (sslCtx == IntPtr.Zero)
  1419. {
  1420. log(ERROR_LOG, "wolfssl set_cipher_list error");
  1421. return FAILURE;
  1422. }
  1423. return wolfSSL_set_cipher_list(sslCtx, list);
  1424. }
  1425. catch (Exception e)
  1426. {
  1427. log(ERROR_LOG, "wolfssl set cipher error " + e.ToString());
  1428. return FAILURE;
  1429. }
  1430. }
  1431. /// <summary>
  1432. /// Gets the version of the connection made ie TLSv1.2
  1433. /// </summary>
  1434. /// <param name="ssl">SSL struct to get version of</param>
  1435. /// <returns>string containing version</returns>
  1436. public static string get_version(IntPtr ssl)
  1437. {
  1438. if (ssl == IntPtr.Zero)
  1439. return null;
  1440. try
  1441. {
  1442. IntPtr version_ptr;
  1443. string version;
  1444. IntPtr sslCtx = unwrap_ssl(ssl);
  1445. if (sslCtx == IntPtr.Zero)
  1446. {
  1447. log(ERROR_LOG, "wolfssl get_version error");
  1448. return null;
  1449. }
  1450. version_ptr = wolfSSL_get_version(sslCtx);
  1451. version = Marshal.PtrToStringAnsi(version_ptr);
  1452. return version;
  1453. }
  1454. catch (Exception e)
  1455. {
  1456. log(ERROR_LOG, "wolfssl get version error " + e.ToString());
  1457. return null;
  1458. }
  1459. }
  1460. /// <summary>
  1461. /// Get a string containing error value and reason
  1462. /// </summary>
  1463. /// <param name="ssl">SSL struct that had error</param>
  1464. /// <returns>String containing error value and reason</returns>
  1465. public static string get_error(IntPtr ssl)
  1466. {
  1467. if (ssl == IntPtr.Zero)
  1468. return null;
  1469. try
  1470. {
  1471. int err;
  1472. StringBuilder err_name;
  1473. StringBuilder ret;
  1474. IntPtr sslCtx = unwrap_ssl(ssl);
  1475. if (sslCtx == IntPtr.Zero)
  1476. {
  1477. log(ERROR_LOG, "wolfssl get_error error");
  1478. return null;
  1479. }
  1480. /* wolfSSL max error length is 80 */
  1481. ret = new StringBuilder(' ', 100);
  1482. err = wolfSSL_get_error(sslCtx, 0);
  1483. err_name = new StringBuilder(new String(' ', 80));
  1484. wolfSSL_ERR_error_string((uint)err, err_name);
  1485. ret.Append("Error " + err + " " + err_name.ToString());
  1486. return ret.ToString();
  1487. }
  1488. catch (Exception e)
  1489. {
  1490. log(ERROR_LOG, "wolfssl get error, error " + e.ToString());
  1491. return null;
  1492. }
  1493. }
  1494. /// <summary>
  1495. /// Used to load in the certificate file
  1496. /// </summary>
  1497. /// <param name="ctx">CTX structure for TLS/SSL connections</param>
  1498. /// <param name="fileCert">Name of the file to load including absolute path</param>
  1499. /// <param name="type">Type of file ie PEM or DER</param>
  1500. /// <returns>1 on success</returns>
  1501. public static int CTX_use_certificate_file(IntPtr ctx, string fileCert, int type)
  1502. {
  1503. try
  1504. {
  1505. IntPtr local_ctx = unwrap_ctx(ctx);
  1506. if (local_ctx == IntPtr.Zero)
  1507. {
  1508. log(ERROR_LOG, "CTX use certificate file error");
  1509. return FAILURE;
  1510. }
  1511. return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type);
  1512. }
  1513. catch (Exception e)
  1514. {
  1515. log(ERROR_LOG, "wolfssl ctx use cert file error " + e.ToString());
  1516. return FAILURE;
  1517. }
  1518. }
  1519. /// <summary>
  1520. /// Used to load in the peer trusted root file
  1521. /// </summary>
  1522. /// <param name="ctx">CTX structure for TLS/SSL connections</param>
  1523. /// <param name="fileCert">Name of the file to load including absolute path</param>
  1524. /// <param name="type">path to multiple certificates (try to load all in path) </param>
  1525. /// <returns>1 on success</returns>
  1526. public static int CTX_load_verify_locations(IntPtr ctx, string fileCert, string path)
  1527. {
  1528. try
  1529. {
  1530. IntPtr local_ctx = unwrap_ctx(ctx);
  1531. if (local_ctx == IntPtr.Zero)
  1532. {
  1533. log(ERROR_LOG, "CTX load verify locations certificate file error");
  1534. return FAILURE;
  1535. }
  1536. return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path);
  1537. }
  1538. catch (Exception e)
  1539. {
  1540. log(ERROR_LOG, "wolfssl ctx load verify locations file error " + e.ToString());
  1541. return FAILURE;
  1542. }
  1543. }
  1544. /// <summary>
  1545. /// Used to load in the private key from a file
  1546. /// </summary>
  1547. /// <param name="ctx">CTX structure for TLS/SSL connections </param>
  1548. /// <param name="fileKey">Name of the file, including absolute directory</param>
  1549. /// <param name="type">Type of file ie PEM or DER</param>
  1550. /// <returns>1 on success</returns>
  1551. public static int CTX_use_PrivateKey_file(IntPtr ctx, string fileKey, int type)
  1552. {
  1553. try
  1554. {
  1555. IntPtr local_ctx = unwrap_ctx(ctx);
  1556. if (local_ctx == IntPtr.Zero)
  1557. {
  1558. log(ERROR_LOG, "CTX use PrivateKey file error");
  1559. return FAILURE;
  1560. }
  1561. return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type);
  1562. }
  1563. catch (Exception e)
  1564. {
  1565. log(ERROR_LOG, "wolfssl ctx use key file error " + e.ToString());
  1566. return FAILURE;
  1567. }
  1568. }
  1569. /// <summary>
  1570. /// Set temporary DH parameters
  1571. /// </summary>
  1572. /// <param name="ssl">Structure to set in</param>
  1573. /// <param name="dhparam">file name</param>
  1574. /// <param name="file_type">type of file ie PEM</param>
  1575. /// <returns>1 on success</returns>
  1576. public static int SetTmpDH_file(IntPtr ssl, StringBuilder dhparam, int file_type)
  1577. {
  1578. try
  1579. {
  1580. IntPtr sslCtx = unwrap_ssl(ssl);
  1581. if (sslCtx == IntPtr.Zero)
  1582. {
  1583. log(ERROR_LOG, "SetTmpDH_file ssl unwrap error");
  1584. return FAILURE;
  1585. }
  1586. return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type);
  1587. }
  1588. catch (Exception e)
  1589. {
  1590. log(ERROR_LOG, "SetTmpDH_file error " + e.ToString());
  1591. return FAILURE;
  1592. }
  1593. }
  1594. /// <summary>
  1595. /// Set temporary DH parameters
  1596. /// </summary>
  1597. /// <param name="ctx">Structure to set in</param>
  1598. /// <param name="dhparam">file name</param>
  1599. /// <param name="file_type">type of file ie PEM</param>
  1600. /// <returns>1 on success</returns>
  1601. public static int CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhparam, int file_type)
  1602. {
  1603. try
  1604. {
  1605. IntPtr local_ctx = unwrap_ctx(ctx);
  1606. if (local_ctx == IntPtr.Zero)
  1607. {
  1608. log(ERROR_LOG, "CTX_SetTmpDH_file ctx unwrap error");
  1609. return FAILURE;
  1610. }
  1611. return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type);
  1612. }
  1613. catch (Exception e)
  1614. {
  1615. log(ERROR_LOG, "CTX_SetTmpDH_file error " + e.ToString());
  1616. return FAILURE;
  1617. }
  1618. }
  1619. /// <summary>
  1620. /// Used to set the minimum size of DH key
  1621. /// </summary>
  1622. /// <param name="ctx">Structure to store key size</param>
  1623. /// <param name="minDhKey">Min key size </param>
  1624. /// <returns>1 on success</returns>
  1625. public static int CTX_SetMinDhKey_Sz(IntPtr ctx, short minDhKey)
  1626. {
  1627. try
  1628. {
  1629. IntPtr local_ctx = unwrap_ctx(ctx);
  1630. if (local_ctx == IntPtr.Zero)
  1631. {
  1632. log(ERROR_LOG, "CTX SetMinDhKey_Sz error");
  1633. return FAILURE;
  1634. }
  1635. return wolfSSL_CTX_SetMinDhKey_Sz(local_ctx, minDhKey);
  1636. }
  1637. catch (Exception e)
  1638. {
  1639. log(ERROR_LOG, "wolfssl ctx set min dh key error " + e.ToString());
  1640. return FAILURE;
  1641. }
  1642. }
  1643. /// <summary>
  1644. /// Set the certificate verification mode and optional callback function
  1645. /// </summary>
  1646. /// <param name="ctx">pointer to CTX that the function is set in</param>
  1647. /// <param name="mode">See SSL_VERIFY options</param>
  1648. /// <param name="vc">Optional verify callback function to use</param>
  1649. public static int CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc)
  1650. {
  1651. try
  1652. {
  1653. GCHandle gch;
  1654. ctx_handle handles;
  1655. IntPtr local_ctx = unwrap_ctx(ctx);
  1656. if (local_ctx == IntPtr.Zero)
  1657. {
  1658. log(ERROR_LOG, "CTX set_verify error");
  1659. return FAILURE;
  1660. }
  1661. /* pin the verify callback to protect from garbage collection */
  1662. if (!vc.Equals(null)) {
  1663. gch = GCHandle.FromIntPtr(ctx);
  1664. handles = (ctx_handle)gch.Target;
  1665. handles.set_vrf(GCHandle.Alloc(vc));
  1666. }
  1667. wolfSSL_CTX_set_verify(local_ctx, mode, vc);
  1668. return SUCCESS;
  1669. }
  1670. catch (Exception e)
  1671. {
  1672. log(ERROR_LOG, "wolfssl ctx set verify error " + e.ToString());
  1673. return FAILURE;
  1674. }
  1675. }
  1676. /// <summary>
  1677. /// Set the certificate verification mode and optional callback function
  1678. /// </summary>
  1679. /// <param name="ctx">pointer to SSL object that the function is set in</param>
  1680. /// <param name="mode">See SSL_VERIFY options</param>
  1681. /// <param name="vc">Optional verify callback function to use</param>
  1682. public static int set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc)
  1683. {
  1684. try
  1685. {
  1686. GCHandle gch;
  1687. ssl_handle handles;
  1688. IntPtr local_ssl = unwrap_ssl(ssl);
  1689. if (local_ssl == IntPtr.Zero)
  1690. {
  1691. log(ERROR_LOG, "set_verify error");
  1692. return FAILURE;
  1693. }
  1694. /* pin the verify callback to protect from garbage collection */
  1695. if (!vc.Equals(null)) {
  1696. gch = GCHandle.FromIntPtr(ssl);
  1697. handles = (ssl_handle)gch.Target;
  1698. handles.set_vrf(GCHandle.Alloc(vc));
  1699. }
  1700. wolfSSL_set_verify(local_ssl, mode, vc);
  1701. return SUCCESS;
  1702. }
  1703. catch (Exception e)
  1704. {
  1705. log(ERROR_LOG, "wolfssl set verify error " + e.ToString());
  1706. return FAILURE;
  1707. }
  1708. }
  1709. /// <summary>
  1710. /// Set the certificate verification mode and optional callback function
  1711. /// </summary>
  1712. /// <param name="ctx">pointer to SSL object that the function is set in</param>
  1713. /// <param name="mode">See SSL_VERIFY options</param>
  1714. /// <param name="vc">Optional verify callback function to use</param>
  1715. public static X509 X509_STORE_CTX_get_current_cert(IntPtr x509Ctx)
  1716. {
  1717. X509 ret = null;
  1718. try
  1719. {
  1720. if (x509Ctx == IntPtr.Zero)
  1721. {
  1722. log(ERROR_LOG, "pointer passed in was not set");
  1723. return ret;
  1724. }
  1725. IntPtr x509 = wolfSSL_X509_STORE_CTX_get_current_cert(x509Ctx);
  1726. if (x509 != IntPtr.Zero) {
  1727. return new X509(x509, false);
  1728. }
  1729. return ret;
  1730. }
  1731. catch (Exception e)
  1732. {
  1733. log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
  1734. return ret;
  1735. }
  1736. }
  1737. /// <summary>
  1738. /// Gets all of the certificates from store
  1739. /// </summary>
  1740. /// <param name="x509Ctx">pointer to store to get certificates from</param>
  1741. public static X509[] X509_STORE_CTX_get_certs(IntPtr x509Ctx)
  1742. {
  1743. X509[] ret = null;
  1744. try
  1745. {
  1746. if (x509Ctx == IntPtr.Zero)
  1747. {
  1748. log(ERROR_LOG, "pointer passed in was not set");
  1749. return ret;
  1750. }
  1751. IntPtr sk = wolfSSL_X509_STORE_GetCerts(x509Ctx);
  1752. if (sk != IntPtr.Zero) {
  1753. int i;
  1754. int numCerts = wolfSSL_sk_X509_num(sk);
  1755. ret = new X509[numCerts];
  1756. for (i = 0; i < numCerts; i++) {
  1757. IntPtr current = wolfSSL_sk_X509_pop(sk);
  1758. if (current != IntPtr.Zero)
  1759. {
  1760. ret[i] = new X509(current, true);
  1761. }
  1762. }
  1763. wolfSSL_sk_X509_free(sk);
  1764. }
  1765. return ret;
  1766. }
  1767. catch (Exception e)
  1768. {
  1769. log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
  1770. return ret;
  1771. }
  1772. }
  1773. /// <summary>
  1774. /// Get the current WOLFSSL_X509_STORE_CTX error value
  1775. /// </summary>
  1776. /// <param name="x509Ctx">pointer to store to get error from</param>
  1777. public static int X509_STORE_CTX_get_error(IntPtr x509Ctx)
  1778. {
  1779. try
  1780. {
  1781. if (x509Ctx == IntPtr.Zero)
  1782. {
  1783. log(ERROR_LOG, "pointer passed in was not set");
  1784. return -1;
  1785. }
  1786. return wolfSSL_X509_STORE_CTX_get_error(x509Ctx);
  1787. }
  1788. catch (Exception e)
  1789. {
  1790. log(ERROR_LOG, "wolfssl WOLFSSL_X509_STORE_CTX error " + e.ToString());
  1791. return -1;
  1792. }
  1793. }
  1794. /// <summary>
  1795. /// Print low level C library debug messages to stdout when compiled with macro DEBUG_WOLFSSL
  1796. /// </summary>
  1797. public static void Debugging_ON()
  1798. {
  1799. wolfSSL_Debugging_ON();
  1800. }
  1801. /// <summary>
  1802. /// Turn off low level C debug messages
  1803. /// </summary>
  1804. public static void Debugging_OFF()
  1805. {
  1806. wolfSSL_Debugging_OFF();
  1807. }
  1808. /// <summary>
  1809. /// Set the function to use for logging
  1810. /// </summary>
  1811. /// <param name="input">Function that conforms as to loggingCb</param>
  1812. /// <returns>1 on success</returns>
  1813. public static int SetLogging(loggingCb input)
  1814. {
  1815. internal_log = input;
  1816. return SUCCESS;
  1817. }
  1818. /// <summary>
  1819. /// Log a message to set logging function
  1820. /// </summary>
  1821. /// <param name="lvl">Level of log message</param>
  1822. /// <param name="msg">Message to log</param>
  1823. public static void log(int lvl, string msg)
  1824. {
  1825. /* if log is not set then print nothing */
  1826. if (internal_log == null)
  1827. return;
  1828. StringBuilder ptr = new StringBuilder(msg);
  1829. internal_log(lvl, ptr);
  1830. }
  1831. }
  1832. }