wolfSSL-TLS-Server.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* wolfSSL-TLS-Server.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 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. /* get and print sni used by the client */
  184. if (haveSNI(args)) {
  185. IntPtr data = IntPtr.Zero;
  186. ushort size = wolfssl.SNI_GetRequest(ssl, 0, ref data);
  187. string dataStr = Marshal.PtrToStringAnsi(data);
  188. Console.WriteLine("(SNI_GetRequest) Size of SNI used by client: " + size);
  189. Console.WriteLine("(SNI_GetRequest) SNI used by client: " + dataStr);
  190. }
  191. /* print out results of TLS/SSL accept */
  192. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  193. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  194. /* read and print out the message then reply */
  195. if (wolfssl.read(ssl, buff, 1023) < 0)
  196. {
  197. Console.WriteLine("Error in read");
  198. tcp.Stop();
  199. clean(ssl, ctx);
  200. return;
  201. }
  202. Console.WriteLine(buff);
  203. /* get and print sni from a sample buffer, can be used by using the raw client hello */
  204. if (haveSNI(args)) {
  205. IntPtr result = Marshal.AllocHGlobal(32);
  206. IntPtr inOutSz = Marshal.AllocHGlobal(sizeof(int));
  207. Marshal.WriteInt32(inOutSz, 32);
  208. byte []buffer = { /* from TextMate website client hello example */
  209. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  210. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  211. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  212. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  213. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  214. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  215. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  216. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  217. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  218. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  219. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  220. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  221. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  222. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  223. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  224. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  225. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  226. };
  227. int ret = wolfssl.SNI_GetFromBuffer(buffer, 1024, 0, result, inOutSz);
  228. if (ret != wolfssl.SUCCESS) {
  229. Console.WriteLine("Error on reading SNI from buffer, ret value = " + ret);
  230. tcp.Stop();
  231. clean(ssl, ctx);
  232. return;
  233. }
  234. string resultStr = Marshal.PtrToStringAnsi(result);
  235. Console.WriteLine("(SNI_GetFromBuffer) SNI used by client: " + resultStr);
  236. }
  237. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  238. {
  239. Console.WriteLine("Error in write");
  240. tcp.Stop();
  241. clean(ssl, ctx);
  242. return;
  243. }
  244. wolfssl.shutdown(ssl);
  245. fd.Close();
  246. tcp.Stop();
  247. clean(ssl, ctx);
  248. }
  249. }