1
0

wolfSSL-DTLS-Server.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* wolfSSL-DTLS-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.Threading;
  25. using System.IO;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. using wolfSSL.CSharp;
  29. public class wolfSSL_DTLS_Server
  30. {
  31. /// <summary>
  32. /// Example of a logging function
  33. /// </summary>
  34. /// <param name="lvl">level of log</param>
  35. /// <param name="msg">message to log</param>
  36. public static void standard_log(int lvl, StringBuilder msg)
  37. {
  38. Console.WriteLine(msg);
  39. }
  40. private static void clean(IntPtr ssl, IntPtr ctx)
  41. {
  42. wolfssl.free(ssl);
  43. wolfssl.CTX_free(ctx);
  44. wolfssl.Cleanup();
  45. }
  46. public static void Main(string[] args)
  47. {
  48. IntPtr ctx;
  49. IntPtr ssl;
  50. /* These paths should be changed for use */
  51. string fileCert = wolfssl.setPath("server-cert.pem");
  52. string fileKey = wolfssl.setPath(@"server-key.pem");
  53. StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
  54. if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
  55. Console.WriteLine("Platform not supported");
  56. return;
  57. }
  58. StringBuilder buff = new StringBuilder(1024);
  59. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
  60. //example of function used for setting logging
  61. wolfssl.SetLogging(standard_log);
  62. wolfssl.Init();
  63. Console.WriteLine("Calling ctx Init from wolfSSL");
  64. ctx = wolfssl.CTX_dtls_new(wolfssl.useDTLSv1_2_server());
  65. if (ctx == IntPtr.Zero)
  66. {
  67. Console.WriteLine("Error creating ctx structure");
  68. wolfssl.CTX_free(ctx);
  69. return;
  70. }
  71. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  72. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  73. {
  74. Console.WriteLine("Could not find cert or key file");
  75. wolfssl.CTX_free(ctx);
  76. return;
  77. }
  78. if (!File.Exists(dhparam.ToString())) {
  79. Console.WriteLine("Could not find dh file");
  80. wolfssl.CTX_free(ctx);
  81. return;
  82. }
  83. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  84. {
  85. Console.WriteLine("Error setting cert file");
  86. wolfssl.CTX_free(ctx);
  87. return;
  88. }
  89. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  90. {
  91. Console.WriteLine("Error setting key file");
  92. wolfssl.CTX_free(ctx);
  93. return;
  94. }
  95. short minDhKey = 128;
  96. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  97. IPAddress ip = IPAddress.Parse("0.0.0.0");
  98. UdpClient udp = new UdpClient(11111);
  99. IPEndPoint ep = new IPEndPoint(ip, 11111);
  100. Console.WriteLine("Started UDP and waiting for a connection");
  101. ssl = wolfssl.new_ssl(ctx);
  102. if (ssl == IntPtr.Zero)
  103. {
  104. Console.WriteLine("Error creating ssl object");
  105. wolfssl.CTX_free(ctx);
  106. return;
  107. }
  108. if (wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  109. {
  110. Console.WriteLine("Error in setting dhparam");
  111. Console.WriteLine(wolfssl.get_error(ssl));
  112. udp.Close();
  113. clean(ssl, ctx);
  114. return;
  115. }
  116. if (wolfssl.set_dtls_fd(ssl, udp, ep) != wolfssl.SUCCESS)
  117. {
  118. Console.WriteLine(wolfssl.get_error(ssl));
  119. udp.Close();
  120. clean(ssl, ctx);
  121. return;
  122. }
  123. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  124. {
  125. Console.WriteLine(wolfssl.get_error(ssl));
  126. udp.Close();
  127. clean(ssl, ctx);
  128. return;
  129. }
  130. /* print out results of TLS/SSL accept */
  131. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  132. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  133. /* get connection information and print ip - port */
  134. wolfssl.DTLS_con con = wolfssl.get_dtls_fd(ssl);
  135. Console.Write("Connected to ip ");
  136. Console.Write(con.ep.Address.ToString());
  137. Console.Write(" on port ");
  138. Console.WriteLine(con.ep.Port.ToString());
  139. /* read information sent and send a reply */
  140. if (wolfssl.read(ssl, buff, 1023) < 0)
  141. {
  142. Console.WriteLine("Error reading message");
  143. Console.WriteLine(wolfssl.get_error(ssl));
  144. udp.Close();
  145. clean(ssl, ctx);
  146. return;
  147. }
  148. Console.WriteLine(buff);
  149. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  150. {
  151. Console.WriteLine("Error writing message");
  152. Console.WriteLine(wolfssl.get_error(ssl));
  153. udp.Close();
  154. clean(ssl, ctx);
  155. return;
  156. }
  157. Console.WriteLine("At the end freeing stuff");
  158. wolfssl.shutdown(ssl);
  159. udp.Close();
  160. clean(ssl, ctx);
  161. }
  162. }