wolfSSL-TLS-Client.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* wolfSSL-TLS-Client.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_Client
  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. /// Verification callback
  47. /// </summary>
  48. /// <param name="preverify">1=Verify Okay, 0=Failure</param>
  49. /// <param name="x509_ctx">Certificate in WOLFSSL_X509_STORE_CTX format</param>
  50. private static int myVerify(int preverify, IntPtr x509_ctx)
  51. {
  52. /* Use the provided verification */
  53. /* Can optionally override failures by returning non-zero value */
  54. return preverify;
  55. }
  56. public static void Main(string[] args)
  57. {
  58. IntPtr ctx;
  59. IntPtr ssl;
  60. Socket tcp;
  61. /* These paths should be changed for use */
  62. string caCert = @"ca-cert.pem";
  63. StringBuilder dhparam = new StringBuilder("dh2048.pem");
  64. StringBuilder buff = new StringBuilder(1024);
  65. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
  66. //example of function used for setting logging
  67. wolfssl.SetLogging(standard_log);
  68. wolfssl.Init();
  69. Console.WriteLine("Calling ctx Init from wolfSSL");
  70. ctx = wolfssl.CTX_new(wolfssl.usev23_client());
  71. if (ctx == IntPtr.Zero)
  72. {
  73. Console.WriteLine("Error in creating ctx structure");
  74. return;
  75. }
  76. Console.WriteLine("Finished init of ctx .... now load in CA");
  77. if (!File.Exists(caCert))
  78. {
  79. Console.WriteLine("Could not find CA cert file");
  80. wolfssl.CTX_free(ctx);
  81. return;
  82. }
  83. if (wolfssl.CTX_load_verify_locations(ctx, caCert, null)
  84. != wolfssl.SUCCESS)
  85. {
  86. Console.WriteLine("Error loading CA cert");
  87. }
  88. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  89. wolfssl.get_ciphers(ciphers, 4096);
  90. Console.WriteLine("Ciphers : " + ciphers.ToString());
  91. /* Uncomment Section to enable specific cipher suite */
  92. #if false
  93. ciphers = new StringBuilder("ECDHE-ECDSA-AES128-GCM-SHA256");
  94. if (wolfssl.CTX_set_cipher_list(ctx, ciphers) != wolfssl.SUCCESS)
  95. {
  96. Console.WriteLine("ERROR CTX_set_cipher_list()");
  97. wolfssl.CTX_free(ctx);
  98. return;
  99. }
  100. #endif
  101. short minDhKey = 128;
  102. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  103. /* Setup Verify Callback */
  104. if (wolfssl.CTX_set_verify(ctx, wolfssl.SSL_VERIFY_PEER, myVerify)
  105. != wolfssl.SUCCESS)
  106. {
  107. Console.WriteLine("Error setting verify callback!");
  108. }
  109. /* set up TCP socket */
  110. tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
  111. ProtocolType.Tcp);
  112. try
  113. {
  114. tcp.Connect("localhost", 11111);
  115. }
  116. catch (Exception e)
  117. {
  118. Console.WriteLine("tcp.Connect() error " + e.ToString());
  119. wolfssl.CTX_free(ctx);
  120. return;
  121. }
  122. if (!tcp.Connected)
  123. {
  124. Console.WriteLine("tcp.Connect() failed!");
  125. tcp.Close();
  126. wolfssl.CTX_free(ctx);
  127. return;
  128. }
  129. Console.WriteLine("Connected TCP");
  130. ssl = wolfssl.new_ssl(ctx);
  131. if (ssl == IntPtr.Zero)
  132. {
  133. Console.WriteLine("Error in creating ssl object");
  134. wolfssl.CTX_free(ctx);
  135. return;
  136. }
  137. Console.WriteLine("Connection made wolfSSL_connect ");
  138. if (wolfssl.set_fd(ssl, tcp) != wolfssl.SUCCESS)
  139. {
  140. /* get and print out the error */
  141. Console.WriteLine(wolfssl.get_error(ssl));
  142. tcp.Close();
  143. clean(ssl, ctx);
  144. return;
  145. }
  146. wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
  147. if (wolfssl.connect(ssl) != wolfssl.SUCCESS)
  148. {
  149. /* get and print out the error */
  150. Console.WriteLine(wolfssl.get_error(ssl));
  151. tcp.Close();
  152. clean(ssl, ctx);
  153. return;
  154. }
  155. /* print out results of TLS/SSL accept */
  156. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  157. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  158. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  159. {
  160. Console.WriteLine("Error in write");
  161. tcp.Close();
  162. clean(ssl, ctx);
  163. return;
  164. }
  165. /* read and print out the message then reply */
  166. if (wolfssl.read(ssl, buff, 1023) < 0)
  167. {
  168. Console.WriteLine("Error in read");
  169. tcp.Close();
  170. clean(ssl, ctx);
  171. return;
  172. }
  173. Console.WriteLine(buff);
  174. wolfssl.shutdown(ssl);
  175. tcp.Close();
  176. clean(ssl, ctx);
  177. }
  178. }