2
0

wolfSSL-DTLS-PSK-Server.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* wolfSSL-DTLS-PSK-Server.cs
  2. *
  3. * Copyright (C) 2006-2022 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_PSK_Server
  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="identity">identity of client connecting</param>
  36. /// <param name="key">buffer to hold key</param>
  37. /// <param name="max_key">max key size</param>
  38. /// <returns>size of key set</returns>
  39. public static uint my_psk_server_cb(IntPtr ssl, string identity, IntPtr key, uint max_key)
  40. {
  41. /* perform a check on the identity sent across
  42. * log function must be set for print out of logging information
  43. */
  44. wolfssl.log(wolfssl.INFO_LOG, "PSK Client Identity = " + identity);
  45. /* Use desired key, note must be a key smaller than max key size parameter
  46. Replace this with desired key. Is trivial one for testing */
  47. if (max_key < 4)
  48. return 0;
  49. byte[] tmp = { 26, 43, 60, 77 };
  50. Marshal.Copy(tmp, 0, key, 4);
  51. return (uint)4;
  52. }
  53. private static void clean(IntPtr ssl, IntPtr ctx)
  54. {
  55. wolfssl.free(ssl);
  56. wolfssl.CTX_free(ctx);
  57. wolfssl.Cleanup();
  58. }
  59. public static void Main(string[] args)
  60. {
  61. IntPtr ctx;
  62. IntPtr ssl;
  63. /* These paths should be changed according to use */
  64. string fileCert = @"server-cert.pem";
  65. string fileKey = @"server-key.pem";
  66. StringBuilder dhparam = new StringBuilder("dh2048.pem");
  67. wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
  68. StringBuilder buff = new StringBuilder(1024);
  69. StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
  70. wolfssl.Init();
  71. Console.WriteLine("Calling ctx Init from wolfSSL");
  72. ctx = wolfssl.CTX_dtls_new(wolfssl.useDTLSv1_2_server());
  73. if (ctx == IntPtr.Zero)
  74. {
  75. Console.WriteLine("Error creating ctx structure");
  76. return;
  77. }
  78. Console.WriteLine("Finished init of ctx .... now load in cert and key");
  79. if (!File.Exists(fileCert) || !File.Exists(fileKey))
  80. {
  81. Console.WriteLine("Could not find cert or key file");
  82. wolfssl.CTX_free(ctx);
  83. return;
  84. }
  85. if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  86. {
  87. Console.WriteLine("Error setting cert file");
  88. wolfssl.CTX_free(ctx);
  89. return;
  90. }
  91. if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  92. {
  93. Console.WriteLine("Error setting key file");
  94. wolfssl.CTX_free(ctx);
  95. return;
  96. }
  97. /* Test psk use with DHE */
  98. StringBuilder hint = new StringBuilder("cyassl server");
  99. if (wolfssl.CTX_use_psk_identity_hint(ctx, hint) != wolfssl.SUCCESS)
  100. {
  101. Console.WriteLine("Error setting hint");
  102. wolfssl.CTX_free(ctx);
  103. return;
  104. }
  105. wolfssl.CTX_set_psk_server_callback(ctx, psk_cb);
  106. short minDhKey = 128;
  107. wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
  108. Console.Write("Setting cipher suite to ");
  109. StringBuilder set_cipher = new StringBuilder("DHE-PSK-AES128-CBC-SHA256");
  110. Console.WriteLine(set_cipher);
  111. if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS)
  112. {
  113. Console.WriteLine("Failed to set cipher suite");
  114. wolfssl.CTX_free(ctx);
  115. return;
  116. }
  117. IPAddress ip = IPAddress.Parse("0.0.0.0");
  118. UdpClient udp = new UdpClient(11111);
  119. IPEndPoint ep = new IPEndPoint(ip, 11111);
  120. Console.WriteLine("Started UDP and waiting for a connection");
  121. ssl = wolfssl.new_ssl(ctx);
  122. if (ssl == IntPtr.Zero)
  123. {
  124. Console.WriteLine("Error creating ssl object");
  125. udp.Close();
  126. wolfssl.CTX_free(ctx);
  127. return;
  128. }
  129. if (wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
  130. {
  131. Console.WriteLine("Error in setting dhparam");
  132. Console.WriteLine(wolfssl.get_error(ssl));
  133. udp.Close();
  134. clean(ssl, ctx);
  135. return;
  136. }
  137. if (wolfssl.set_dtls_fd(ssl, udp, ep) != wolfssl.SUCCESS)
  138. {
  139. Console.WriteLine(wolfssl.get_error(ssl));
  140. udp.Close();
  141. clean(ssl, ctx);
  142. return;
  143. }
  144. if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
  145. {
  146. Console.WriteLine(wolfssl.get_error(ssl));
  147. udp.Close();
  148. clean(ssl, ctx);
  149. return;
  150. }
  151. /* print out results of TLS/SSL accept */
  152. Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
  153. Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
  154. /* get connection information and print ip - port */
  155. wolfssl.DTLS_con con = wolfssl.get_dtls_fd(ssl);
  156. Console.Write("Connected to ip ");
  157. Console.Write(con.ep.Address.ToString());
  158. Console.Write(" on port ");
  159. Console.WriteLine(con.ep.Port.ToString());
  160. /* read information sent and send a reply */
  161. if (wolfssl.read(ssl, buff, 1023) < 0)
  162. {
  163. Console.WriteLine("Error reading message");
  164. Console.WriteLine(wolfssl.get_error(ssl));
  165. udp.Close();
  166. clean(ssl, ctx);
  167. return;
  168. }
  169. Console.WriteLine(buff);
  170. if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
  171. {
  172. Console.WriteLine("Error writing message");
  173. Console.WriteLine(wolfssl.get_error(ssl));
  174. udp.Close();
  175. clean(ssl, ctx);
  176. return;
  177. }
  178. Console.WriteLine("At the end freeing stuff");
  179. wolfssl.shutdown(ssl);
  180. udp.Close();
  181. clean(ssl, ctx);
  182. }
  183. }