1
0

wolfSSL-TLS-ServerThreaded.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* wolfSSL-TLS-ServerThreaded.cs
  2. *
  3. * Copyright (C) 2006-2022 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 = @"server-cert.pem";
  105. string fileKey = @"server-key.pem";
  106. StringBuilder dhparam = new StringBuilder("dh2048.pem");
  107. /* example of function used for setting logging */
  108. wolfssl.SetLogging(standard_log);
  109. wolfssl.Init();
  110. Console.WriteLine("Calling ctx Init from wolfSSL");
  111. ctx = wolfssl.CTX_new(wolfssl.usev23_server());
  112. if (ctx == IntPtr.Zero)
  113. {
  114. Console.WriteLine("Error in creating ctx structure");
  115. return;
  116. }
  117. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  118. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  119. {
  120. Console.WriteLine("Could not find cert or key file");
  121. wolfssl.CTX_free(ctx);
  122. return;
  123. }
  124. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  125. {
  126. Console.WriteLine("Error in setting cert file");
  127. wolfssl.CTX_free(ctx);
  128. return;
  129. }
  130. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  131. {
  132. Console.WriteLine("Error in setting key file");
  133. wolfssl.CTX_free(ctx);
  134. return;
  135. }
  136. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  137. wolfssl.get_ciphers(ciphers, 4096);
  138. Console.WriteLine("Ciphers : " + ciphers.ToString());
  139. short minDhKey = 128;
  140. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  141. wolfssl.CTX_SetTmpDH_file(ctx, dhparam, wolfssl.SSL_FILETYPE_PEM);
  142. /* set up TCP socket */
  143. IPAddress ip = IPAddress.Parse("0.0.0.0"); /* bind to any */
  144. TcpListener tcp = new TcpListener(ip, 11111);
  145. tcp.Start();
  146. Console.WriteLine("Started TCP and waiting for a connection");
  147. while (true) {
  148. try
  149. {
  150. Socket fd = tcp.AcceptSocket();
  151. Console.WriteLine("Got client connection");
  152. /* Spin up thread for client */
  153. wolfSSL_TLS_ServerThread thread = new wolfSSL_TLS_ServerThread(ctx, fd);
  154. Thread thr = new Thread(new ThreadStart(thread.start_client));
  155. thr.Start();
  156. }
  157. catch(Exception ex)
  158. {
  159. Console.WriteLine("Server Exception " + ex.ToString());
  160. break;
  161. }
  162. }
  163. tcp.Stop();
  164. wolfssl.CTX_free(ctx);
  165. wolfssl.Cleanup();
  166. }
  167. }