wolfSSL-TLS-PSK-Client.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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("dh2048.pem");
  69. StringBuilder buff = new StringBuilder(1024);
  70. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper");
  71. wolfssl.Init();
  72. Console.WriteLine("Calling ctx Init from wolfSSL");
  73. ctx = wolfssl.CTX_new(wolfssl.usev23_client());
  74. if (ctx == IntPtr.Zero)
  75. {
  76. Console.WriteLine("Error creating ctx structure");
  77. return;
  78. }
  79. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  80. StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
  81. wolfssl.get_ciphers(ciphers, 4096);
  82. Console.WriteLine("Ciphers : " + ciphers.ToString());
  83. short minDhKey = 128;
  84. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  85. Console.Write("Setting cipher suite to ");
  86. /* In order to use static PSK build wolfSSL with the preprocessor flag WOLFSSL_STATIC_PSK */
  87. StringBuilder set_cipher = new StringBuilder("DHE-PSK-AES128-CBC-SHA256");
  88. Console.WriteLine(set_cipher);
  89. if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS)
  90. {
  91. Console.WriteLine("Failed to set cipher suite");
  92. return;
  93. }
  94. /* Test psk use with DHE */
  95. wolfssl.CTX_set_psk_client_callback(ctx, psk_cb);
  96. /* set up TCP socket */
  97. tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
  98. ProtocolType.Tcp);
  99. try
  100. {
  101. tcp.Connect("localhost", 11111);
  102. }
  103. catch (Exception e)
  104. {
  105. Console.WriteLine("tcp.Connect() error " + e.ToString());
  106. wolfssl.CTX_free(ctx);
  107. return;
  108. }
  109. if (!tcp.Connected)
  110. {
  111. Console.WriteLine("tcp.Connect() failed!");
  112. tcp.Close();
  113. wolfssl.CTX_free(ctx);
  114. return;
  115. }
  116. Console.WriteLine("Connected TCP");
  117. ssl = wolfssl.new_ssl(ctx);
  118. if (ssl == IntPtr.Zero)
  119. {
  120. Console.WriteLine("Error in creating ssl object");
  121. wolfssl.CTX_free(ctx);
  122. return;
  123. }
  124. if (wolfssl.set_fd(ssl, tcp) != wolfssl.SUCCESS)
  125. {
  126. /* get and print out the error */
  127. Console.WriteLine(wolfssl.get_error(ssl));
  128. tcp.Close();
  129. clean(ssl, ctx);
  130. return;
  131. }
  132. wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
  133. if (wolfssl.connect(ssl) != wolfssl.SUCCESS)
  134. {
  135. /* get and print out the error */
  136. Console.WriteLine(wolfssl.get_error(ssl));
  137. tcp.Close();
  138. clean(ssl, ctx);
  139. return;
  140. }
  141. /* print out results of TLS/SSL accept */
  142. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  143. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  144. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  145. {
  146. Console.WriteLine("Error in write");
  147. tcp.Close();
  148. clean(ssl, ctx);
  149. return;
  150. }
  151. /* read and print out the message then reply */
  152. if (wolfssl.read(ssl, buff, 1023) < 0)
  153. {
  154. Console.WriteLine("Error in read");
  155. tcp.Close();
  156. clean(ssl, ctx);
  157. return;
  158. }
  159. Console.WriteLine(buff);
  160. wolfssl.shutdown(ssl);
  161. tcp.Close();
  162. clean(ssl, ctx);
  163. }
  164. }