wolfSSL-TLS-Server.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* wolfSSL-TLS-Server.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.IO;
  25. using System.Net;
  26. using System.Net.Sockets;
  27. using wolfSSL.CSharp;
  28. public class wolfSSL_TLS_CSHarp
  29. {
  30. /// <summary>
  31. /// Example of a logging function
  32. /// </summary>
  33. /// <param name="lvl">level of log</param>
  34. /// <param name="msg">message to log</param>
  35. public static void standard_log(int lvl, StringBuilder msg)
  36. {
  37. Console.WriteLine(msg);
  38. }
  39. private static void clean(IntPtr ssl, IntPtr ctx)
  40. {
  41. wolfssl.free(ssl);
  42. wolfssl.CTX_free(ctx);
  43. wolfssl.Cleanup();
  44. }
  45. /// <summary>
  46. /// Checks if the SNI option was enabled via command line.
  47. /// Must be enabled with ./configure --enable-sni when configuring
  48. /// wolfSSL.
  49. /// <param name="args">Parameters passed via command line</param>
  50. /// </summary>
  51. private static bool haveSNI(string[] args)
  52. {
  53. bool sniON = false;
  54. for (int i = 0; i < args.Length; i++) {
  55. if (args[i] == "-S") {
  56. sniON = true;
  57. break;
  58. }
  59. }
  60. Console.WriteLine("SNI IS: " + sniON);
  61. return sniON;
  62. }
  63. /// <summary>
  64. /// Example of a SNI function call back
  65. /// </summary>
  66. /// <param name="ssl">pointer to ssl structure</param>
  67. /// <param name="ret">alert code</param>
  68. /// <param name="exArg">context arg, can be set with the function wolfssl.CTX_set_servername_arg</param>
  69. /// <returns></returns>
  70. public static int my_sni_server_cb(IntPtr ssl, IntPtr ret, IntPtr exArg) {
  71. /* Trivial callback just for testing */
  72. Console.WriteLine("my sni server callback");
  73. return 0;
  74. }
  75. public static void Main(string[] args)
  76. {
  77. IntPtr ctx;
  78. IntPtr ssl;
  79. Socket fd;
  80. IntPtr arg_sni;
  81. /* These paths should be changed for use */
  82. string fileCert = wolfssl.setPath("server-cert.pem");
  83. string fileKey = wolfssl.setPath("server-key.pem");
  84. StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
  85. if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
  86. Console.WriteLine("Platform not supported.");
  87. return;
  88. }
  89. StringBuilder buff = new StringBuilder(1024);
  90. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
  91. //example of function used for setting logging
  92. wolfssl.SetLogging(standard_log);
  93. wolfssl.Init();
  94. Console.WriteLine("Calling ctx Init from wolfSSL");
  95. ctx = wolfssl.CTX_new(wolfssl.usev23_server());
  96. if (ctx == IntPtr.Zero)
  97. {
  98. Console.WriteLine("Error in creating ctx structure");
  99. return;
  100. }
  101. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  102. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  103. {
  104. Console.WriteLine("Could not find cert or key file");
  105. wolfssl.CTX_free(ctx);
  106. return;
  107. }
  108. if (!File.Exists(dhparam.ToString())) {
  109. Console.WriteLine("Could not find dh file");
  110. wolfssl.CTX_free(ctx);
  111. return;
  112. }
  113. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  114. {
  115. Console.WriteLine("Error in setting cert file");
  116. wolfssl.CTX_free(ctx);
  117. return;
  118. }
  119. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  120. {
  121. Console.WriteLine("Error in setting key file");
  122. wolfssl.CTX_free(ctx);
  123. return;
  124. }
  125. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  126. wolfssl.get_ciphers(ciphers, 4096);
  127. Console.WriteLine("Ciphers : " + ciphers.ToString());
  128. short minDhKey = 128;
  129. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  130. /* set up TCP socket */
  131. IPAddress ip = IPAddress.Parse("0.0.0.0"); /* bind to any */
  132. TcpListener tcp = new TcpListener(ip, 11111);
  133. tcp.Start();
  134. Console.WriteLine("Started TCP and waiting for a connection");
  135. fd = tcp.AcceptSocket();
  136. ssl = wolfssl.new_ssl(ctx);
  137. if (ssl == IntPtr.Zero)
  138. {
  139. Console.WriteLine("Error in creating ssl object");
  140. wolfssl.CTX_free(ctx);
  141. return;
  142. }
  143. if (haveSNI(args))
  144. {
  145. // Allocating memory and setting SNI arg
  146. int test_value = 32;
  147. arg_sni = Marshal.AllocHGlobal(sizeof(int));
  148. Marshal.WriteInt32(arg_sni, test_value);
  149. if (wolfssl.CTX_set_servername_arg(ctx, arg_sni) == wolfssl.FAILURE) {
  150. Console.WriteLine("wolfssl.CTX_set_servername_arg failed");
  151. wolfssl.CTX_free(ctx);
  152. return;
  153. }
  154. // Setting SNI delegate
  155. wolfssl.sni_delegate sni_cb = new wolfssl.sni_delegate(my_sni_server_cb);
  156. wolfssl.CTX_set_servername_callback(ctx, sni_cb);
  157. }
  158. Console.WriteLine("Connection made wolfSSL_accept ");
  159. if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS)
  160. {
  161. /* get and print out the error */
  162. Console.WriteLine(wolfssl.get_error(ssl));
  163. tcp.Stop();
  164. clean(ssl, ctx);
  165. return;
  166. }
  167. if (wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  168. {
  169. Console.WriteLine("Error in setting dh2048Pem");
  170. Console.WriteLine(wolfssl.get_error(ssl));
  171. tcp.Stop();
  172. clean(ssl, ctx);
  173. return;
  174. }
  175. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  176. {
  177. /* get and print out the error */
  178. Console.WriteLine(wolfssl.get_error(ssl));
  179. tcp.Stop();
  180. clean(ssl, ctx);
  181. return;
  182. }
  183. /* print out results of TLS/SSL accept */
  184. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  185. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  186. /* read and print out the message then reply */
  187. if (wolfssl.read(ssl, buff, 1023) < 0)
  188. {
  189. Console.WriteLine("Error in read");
  190. tcp.Stop();
  191. clean(ssl, ctx);
  192. return;
  193. }
  194. Console.WriteLine(buff);
  195. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  196. {
  197. Console.WriteLine("Error in write");
  198. tcp.Stop();
  199. clean(ssl, ctx);
  200. return;
  201. }
  202. wolfssl.shutdown(ssl);
  203. fd.Close();
  204. tcp.Stop();
  205. clean(ssl, ctx);
  206. }
  207. }