wolfSSL-TLS-Server.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public static void Main(string[] args)
  46. {
  47. IntPtr ctx;
  48. IntPtr ssl;
  49. Socket fd;
  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_new(wolfssl.usev23_server());
  61. if (ctx == IntPtr.Zero)
  62. {
  63. Console.WriteLine("Error in creating ctx structure");
  64. return;
  65. }
  66. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  67. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  68. {
  69. Console.WriteLine("Could not find cert or key file");
  70. wolfssl.CTX_free(ctx);
  71. return;
  72. }
  73. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  74. {
  75. Console.WriteLine("Error in setting cert file");
  76. wolfssl.CTX_free(ctx);
  77. return;
  78. }
  79. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  80. {
  81. Console.WriteLine("Error in setting key file");
  82. wolfssl.CTX_free(ctx);
  83. return;
  84. }
  85. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  86. wolfssl.get_ciphers(ciphers, 4096);
  87. Console.WriteLine("Ciphers : " + ciphers.ToString());
  88. short minDhKey = 128;
  89. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  90. /* set up TCP socket */
  91. IPAddress ip = IPAddress.Parse("0.0.0.0"); /* bind to any */
  92. TcpListener tcp = new TcpListener(ip, 11111);
  93. tcp.Start();
  94. Console.WriteLine("Started TCP and waiting for a connection");
  95. fd = tcp.AcceptSocket();
  96. ssl = wolfssl.new_ssl(ctx);
  97. if (ssl == IntPtr.Zero)
  98. {
  99. Console.WriteLine("Error in creating ssl object");
  100. wolfssl.CTX_free(ctx);
  101. return;
  102. }
  103. Console.WriteLine("Connection made wolfSSL_accept ");
  104. if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS)
  105. {
  106. /* get and print out the error */
  107. Console.WriteLine(wolfssl.get_error(ssl));
  108. tcp.Stop();
  109. clean(ssl, ctx);
  110. return;
  111. }
  112. wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
  113. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  114. {
  115. /* get and print out the error */
  116. Console.WriteLine(wolfssl.get_error(ssl));
  117. tcp.Stop();
  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. /* read and print out the message then reply */
  125. if (wolfssl.read(ssl, buff, 1023) < 0)
  126. {
  127. Console.WriteLine("Error in read");
  128. tcp.Stop();
  129. clean(ssl, ctx);
  130. return;
  131. }
  132. Console.WriteLine(buff);
  133. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  134. {
  135. Console.WriteLine("Error in write");
  136. tcp.Stop();
  137. clean(ssl, ctx);
  138. return;
  139. }
  140. wolfssl.shutdown(ssl);
  141. fd.Close();
  142. tcp.Stop();
  143. clean(ssl, ctx);
  144. }
  145. }