wolfSSL-TLS-PSK-Client.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* wolfSSL-TLS-PSK-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.Threading;
  25. using System.IO;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. using wolfSSL.CSharp;
  29. public class wolfSSL_TLS_PSK_Client
  30. {
  31. /// <summary>
  32. /// Example of a PSK function call back
  33. /// </summary>
  34. /// <param name="ssl">pointer to ssl structure</param>
  35. /// <param name="hint">hint if any from connecting</param>
  36. /// <param name="identity">identity to send to server</param>
  37. /// <param name="id_max">max length of identity</param>
  38. /// <param name="key">buffer to hold key</param>
  39. /// <param name="max_key">max key size</param>
  40. /// <returns>size of key set</returns>
  41. public static uint my_psk_client_cb(IntPtr ssl, string hint, IntPtr identity, uint id_max, IntPtr key, uint max_key)
  42. {
  43. /* C# client */
  44. byte[] id = { 67, 35, 32, 99, 108, 105, 101, 110, 116 };
  45. if (id_max < 9)
  46. return 0;
  47. Marshal.Copy(id, 0, identity, 9);
  48. /* Use desired key, note must be a key smaller than max key size parameter
  49. Replace this with desired key. Is trivial one for testing */
  50. if (max_key < 4)
  51. return 0;
  52. byte[] tmp = { 26, 43, 60, 77 };
  53. Marshal.Copy(tmp, 0, key, 4);
  54. return (uint)4;
  55. }
  56. private static void clean(IntPtr ssl, IntPtr ctx)
  57. {
  58. wolfssl.free(ssl);
  59. wolfssl.CTX_free(ctx);
  60. wolfssl.Cleanup();
  61. }
  62. public static void Main(string[] args)
  63. {
  64. IntPtr ctx;
  65. IntPtr ssl;
  66. Socket tcp;
  67. wolfssl.psk_client_delegate psk_cb = new wolfssl.psk_client_delegate(my_psk_client_cb);
  68. StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
  69. if (dhparam.Length == 0) {
  70. Console.WriteLine("Platform not supported");
  71. return;
  72. }
  73. StringBuilder buff = new StringBuilder(1024);
  74. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper");
  75. wolfssl.Init();
  76. Console.WriteLine("Calling ctx Init from wolfSSL");
  77. ctx = wolfssl.CTX_new(wolfssl.usev23_client());
  78. if (ctx == IntPtr.Zero)
  79. {
  80. Console.WriteLine("Error creating ctx structure");
  81. return;
  82. }
  83. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  84. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  85. wolfssl.get_ciphers(ciphers, 4096);
  86. Console.WriteLine("Ciphers : " + ciphers.ToString());
  87. short minDhKey = 128;
  88. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  89. Console.Write("Setting cipher suite to ");
  90. /* In order to use static PSK build wolfSSL with the preprocessor flag WOLFSSL_STATIC_PSK */
  91. StringBuilder set_cipher = new StringBuilder("DHE-PSK-AES128-CBC-SHA256");
  92. Console.WriteLine(set_cipher);
  93. if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS)
  94. {
  95. Console.WriteLine("Failed to set cipher suite");
  96. return;
  97. }
  98. /* Test psk use with DHE */
  99. wolfssl.CTX_set_psk_client_callback(ctx, psk_cb);
  100. /* set up TCP socket */
  101. tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
  102. ProtocolType.Tcp);
  103. try
  104. {
  105. tcp.Connect("localhost", 11111);
  106. }
  107. catch (Exception e)
  108. {
  109. Console.WriteLine("tcp.Connect() error " + e.ToString());
  110. wolfssl.CTX_free(ctx);
  111. return;
  112. }
  113. if (!tcp.Connected)
  114. {
  115. Console.WriteLine("tcp.Connect() failed!");
  116. tcp.Close();
  117. wolfssl.CTX_free(ctx);
  118. return;
  119. }
  120. Console.WriteLine("Connected TCP");
  121. ssl = wolfssl.new_ssl(ctx);
  122. if (ssl == IntPtr.Zero)
  123. {
  124. Console.WriteLine("Error in creating ssl object");
  125. wolfssl.CTX_free(ctx);
  126. return;
  127. }
  128. if (wolfssl.set_fd(ssl, tcp) != wolfssl.SUCCESS)
  129. {
  130. /* get and print out the error */
  131. Console.WriteLine(wolfssl.get_error(ssl));
  132. tcp.Close();
  133. clean(ssl, ctx);
  134. return;
  135. }
  136. if (!File.Exists(dhparam.ToString())) {
  137. Console.WriteLine("Could not find dh file");
  138. wolfssl.CTX_free(ctx);
  139. return;
  140. }
  141. wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
  142. if (wolfssl.connect(ssl) != wolfssl.SUCCESS)
  143. {
  144. /* get and print out the error */
  145. Console.WriteLine(wolfssl.get_error(ssl));
  146. tcp.Close();
  147. clean(ssl, ctx);
  148. return;
  149. }
  150. /* print out results of TLS/SSL accept */
  151. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  152. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  153. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  154. {
  155. Console.WriteLine("Error in write");
  156. tcp.Close();
  157. clean(ssl, ctx);
  158. return;
  159. }
  160. /* read and print out the message then reply */
  161. if (wolfssl.read(ssl, buff, 1023) < 0)
  162. {
  163. Console.WriteLine("Error in read");
  164. tcp.Close();
  165. clean(ssl, ctx);
  166. return;
  167. }
  168. Console.WriteLine(buff);
  169. wolfssl.shutdown(ssl);
  170. tcp.Close();
  171. clean(ssl, ctx);
  172. }
  173. }