2
0

wolfSSL-TLS-ServerThreaded.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* wolfSSL-TLS-ServerThreaded.cs
  2. *
  3. * Copyright (C) 2006-2024 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.IO;
  25. using System.Net;
  26. using System.Net.Sockets;
  27. using System.Threading;
  28. using wolfSSL.CSharp;
  29. public class wolfSSL_TLS_ServerThread
  30. {
  31. private IntPtr _ctx;
  32. private Socket _fd;
  33. public wolfSSL_TLS_ServerThread(IntPtr ctx, Socket fd)
  34. {
  35. _ctx = ctx;
  36. _fd = fd;
  37. }
  38. private const int kEchoBufSz = 1024;
  39. public void start_client()
  40. {
  41. StringBuilder buff = new StringBuilder(kEchoBufSz);
  42. IntPtr ssl = wolfssl.new_ssl(_ctx);
  43. if (ssl == IntPtr.Zero)
  44. {
  45. Console.WriteLine("Error in creating ssl object");
  46. return;
  47. }
  48. if (wolfssl.set_fd(ssl, _fd) != wolfssl.SUCCESS)
  49. {
  50. /* get and print out the error */
  51. Console.WriteLine(wolfssl.get_error(ssl));
  52. _fd.Close();
  53. wolfssl.free(ssl);
  54. return;
  55. }
  56. Console.WriteLine("Starting TLS handshake");
  57. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  58. {
  59. /* get and print out the error */
  60. Console.WriteLine("Failed " + wolfssl.get_error(ssl));
  61. _fd.Close();
  62. wolfssl.free(ssl);
  63. return;
  64. }
  65. /* print out results of TLS/SSL accept */
  66. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  67. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  68. /* echo data until error */
  69. while (true)
  70. {
  71. /* read and print out the message then reply */
  72. if (wolfssl.read(ssl, buff, kEchoBufSz-1) < 0)
  73. {
  74. Console.WriteLine("Error in read");
  75. break;
  76. }
  77. Console.WriteLine(buff);
  78. if (wolfssl.write(ssl, buff, buff.Length) != buff.Length)
  79. {
  80. Console.WriteLine("Error in write");
  81. break;
  82. }
  83. }
  84. Console.WriteLine("Closing " + wolfssl.get_error(ssl));
  85. _fd.Close();
  86. wolfssl.free(ssl);
  87. }
  88. }
  89. public class wolfSSL_TLS_ServerThreaded
  90. {
  91. /// <summary>
  92. /// Example of a logging function
  93. /// </summary>
  94. /// <param name="lvl">level of log</param>
  95. /// <param name="msg">message to log</param>
  96. public static void standard_log(int lvl, StringBuilder msg)
  97. {
  98. Console.WriteLine(msg);
  99. }
  100. public static void Main(string[] args)
  101. {
  102. IntPtr ctx;
  103. /* These paths should be changed for use */
  104. string fileCert = wolfssl.setPath("server-cert.pem");
  105. string fileKey = wolfssl.setPath("server-key.pem");
  106. StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
  107. if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
  108. Console.WriteLine("Platform not supported");
  109. return;
  110. }
  111. /* example of function used for setting logging */
  112. wolfssl.SetLogging(standard_log);
  113. wolfssl.Init();
  114. Console.WriteLine("Calling ctx Init from wolfSSL");
  115. ctx = wolfssl.CTX_new(wolfssl.usev23_server());
  116. if (ctx == IntPtr.Zero)
  117. {
  118. Console.WriteLine("Error in creating ctx structure");
  119. return;
  120. }
  121. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  122. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  123. {
  124. Console.WriteLine("Could not find cert or key file");
  125. wolfssl.CTX_free(ctx);
  126. return;
  127. }
  128. if (!File.Exists(dhparam.ToString())) {
  129. Console.WriteLine("Could not find dh file");
  130. wolfssl.CTX_free(ctx);
  131. return;
  132. }
  133. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  134. {
  135. Console.WriteLine("Error in setting cert file");
  136. wolfssl.CTX_free(ctx);
  137. return;
  138. }
  139. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  140. {
  141. Console.WriteLine("Error in setting key file");
  142. wolfssl.CTX_free(ctx);
  143. return;
  144. }
  145. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  146. wolfssl.get_ciphers(ciphers, 4096);
  147. Console.WriteLine("Ciphers : " + ciphers.ToString());
  148. short minDhKey = 128;
  149. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  150. wolfssl.CTX_SetTmpDH_file(ctx, dhparam, wolfssl.SSL_FILETYPE_PEM);
  151. /* set up TCP socket */
  152. IPAddress ip = IPAddress.Parse("0.0.0.0"); /* bind to any */
  153. TcpListener tcp = new TcpListener(ip, 11111);
  154. tcp.Start();
  155. Console.WriteLine("Started TCP and waiting for a connection");
  156. while (true) {
  157. try
  158. {
  159. Socket fd = tcp.AcceptSocket();
  160. Console.WriteLine("Got client connection");
  161. /* Spin up thread for client */
  162. wolfSSL_TLS_ServerThread thread = new wolfSSL_TLS_ServerThread(ctx, fd);
  163. Thread thr = new Thread(new ThreadStart(thread.start_client));
  164. thr.Start();
  165. }
  166. catch(Exception ex)
  167. {
  168. Console.WriteLine("Server Exception " + ex.ToString());
  169. break;
  170. }
  171. }
  172. tcp.Stop();
  173. wolfssl.CTX_free(ctx);
  174. wolfssl.Cleanup();
  175. }
  176. }