wolfSSL-DTLS-Server.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 = @"server-cert.pem";
  52. string fileKey = @"server-key.pem";
  53. StringBuilder dhparam = new StringBuilder("dh2048.pem");
  54. StringBuilder buff = new StringBuilder(1024);
  55. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
  56. //example of function used for setting logging
  57. wolfssl.SetLogging(standard_log);
  58. wolfssl.Init();
  59. Console.WriteLine("Calling ctx Init from wolfSSL");
  60. ctx = wolfssl.CTX_dtls_new(wolfssl.useDTLSv1_2_server());
  61. if (ctx == IntPtr.Zero)
  62. {
  63. Console.WriteLine("Error creating ctx structure");
  64. wolfssl.CTX_free(ctx);
  65. return;
  66. }
  67. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  68. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  69. {
  70. Console.WriteLine("Could not find cert or key file");
  71. wolfssl.CTX_free(ctx);
  72. return;
  73. }
  74. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  75. {
  76. Console.WriteLine("Error setting cert file");
  77. wolfssl.CTX_free(ctx);
  78. return;
  79. }
  80. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  81. {
  82. Console.WriteLine("Error setting key file");
  83. wolfssl.CTX_free(ctx);
  84. return;
  85. }
  86. short minDhKey = 128;
  87. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  88. IPAddress ip = IPAddress.Parse("0.0.0.0");
  89. UdpClient udp = new UdpClient(11111);
  90. IPEndPoint ep = new IPEndPoint(ip, 11111);
  91. Console.WriteLine("Started UDP and waiting for a connection");
  92. ssl = wolfssl.new_ssl(ctx);
  93. if (ssl == IntPtr.Zero)
  94. {
  95. Console.WriteLine("Error creating ssl object");
  96. wolfssl.CTX_free(ctx);
  97. return;
  98. }
  99. if (wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  100. {
  101. Console.WriteLine("Error in setting dhparam");
  102. Console.WriteLine(wolfssl.get_error(ssl));
  103. udp.Close();
  104. clean(ssl, ctx);
  105. return;
  106. }
  107. if (wolfssl.set_dtls_fd(ssl, udp, ep) != wolfssl.SUCCESS)
  108. {
  109. Console.WriteLine(wolfssl.get_error(ssl));
  110. udp.Close();
  111. clean(ssl, ctx);
  112. return;
  113. }
  114. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  115. {
  116. Console.WriteLine(wolfssl.get_error(ssl));
  117. udp.Close();
  118. clean(ssl, ctx);
  119. return;
  120. }
  121. /* print out results of TLS/SSL accept */
  122. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  123. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  124. /* get connection information and print ip - port */
  125. wolfssl.DTLS_con con = wolfssl.get_dtls_fd(ssl);
  126. Console.Write("Connected to ip ");
  127. Console.Write(con.ep.Address.ToString());
  128. Console.Write(" on port ");
  129. Console.WriteLine(con.ep.Port.ToString());
  130. /* read information sent and send a reply */
  131. if (wolfssl.read(ssl, buff, 1023) < 0)
  132. {
  133. Console.WriteLine("Error reading message");
  134. Console.WriteLine(wolfssl.get_error(ssl));
  135. udp.Close();
  136. clean(ssl, ctx);
  137. return;
  138. }
  139. Console.WriteLine(buff);
  140. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  141. {
  142. Console.WriteLine("Error writing message");
  143. Console.WriteLine(wolfssl.get_error(ssl));
  144. udp.Close();
  145. clean(ssl, ctx);
  146. return;
  147. }
  148. Console.WriteLine("At the end freeing stuff");
  149. wolfssl.shutdown(ssl);
  150. udp.Close();
  151. clean(ssl, ctx);
  152. }
  153. }