1 /* wolfSSL-TLS-Server.cs
2  *
3  * Copyright (C) 2006-2021 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 
22 
23 
24 using System;
25 using System.Runtime.InteropServices;
26 using System.Text;
27 using System.IO;
28 using System.Net;
29 using System.Net.Sockets;
30 using wolfSSL.CSharp;
31 
32 public class wolfSSL_TLS_CSHarp
33 {
34     /// <summary>
35     /// Example of a logging function
36     /// </summary>
37     /// <param name="lvl">level of log</param>
38     /// <param name="msg">message to log</param>
standard_log(int lvl, StringBuilder msg)39     public static void standard_log(int lvl, StringBuilder msg)
40     {
41         Console.WriteLine(msg);
42     }
43 
44 
clean(IntPtr ssl, IntPtr ctx)45     private static void clean(IntPtr ssl, IntPtr ctx)
46     {
47         wolfssl.free(ssl);
48         wolfssl.CTX_free(ctx);
49         wolfssl.Cleanup();
50     }
51 
52 
Main(string[] args)53     public static void Main(string[] args)
54     {
55         IntPtr ctx;
56         IntPtr ssl;
57         Socket fd;
58 
59         /* These paths should be changed for use */
60         string fileCert = @"server-cert.pem";
61         string fileKey = @"server-key.pem";
62         StringBuilder dhparam = new StringBuilder("dh2048.pem");
63 
64         StringBuilder buff = new StringBuilder(1024);
65         StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
66 
67         //example of function used for setting logging
68         wolfssl.SetLogging(standard_log);
69 
70         wolfssl.Init();
71 
72 
73         Console.WriteLine("Calling ctx Init from wolfSSL");
74         ctx = wolfssl.CTX_new(wolfssl.usev23_server());
75         if (ctx == IntPtr.Zero)
76         {
77             Console.WriteLine("Error in creating ctx structure");
78             return;
79         }
80         Console.WriteLine("Finished init of ctx .... now load in cert and key");
81 
82         if (!File.Exists(fileCert) || !File.Exists(fileKey))
83         {
84             Console.WriteLine("Could not find cert or key file");
85             wolfssl.CTX_free(ctx);
86             return;
87         }
88 
89         if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
90         {
91             Console.WriteLine("Error in setting cert file");
92             wolfssl.CTX_free(ctx);
93             return;
94         }
95 
96         if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
97         {
98             Console.WriteLine("Error in setting key file");
99             wolfssl.CTX_free(ctx);
100             return;
101         }
102 
103 
104         StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
105         wolfssl.get_ciphers(ciphers, 4096);
106         Console.WriteLine("Ciphers : " + ciphers.ToString());
107 
108         short minDhKey = 128;
109         wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey);
110 
111         /* set up TCP socket */
112         IPAddress ip = IPAddress.Parse("0.0.0.0"); /* bind to any */
113         TcpListener tcp = new TcpListener(ip, 11111);
114         tcp.Start();
115 
116         Console.WriteLine("Started TCP and waiting for a connection");
117         fd = tcp.AcceptSocket();
118         ssl = wolfssl.new_ssl(ctx);
119         if (ssl == IntPtr.Zero)
120         {
121             Console.WriteLine("Error in creating ssl object");
122             wolfssl.CTX_free(ctx);
123             return;
124         }
125 
126         Console.WriteLine("Connection made wolfSSL_accept ");
127         if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS)
128         {
129             /* get and print out the error */
130             Console.WriteLine(wolfssl.get_error(ssl));
131             tcp.Stop();
132             clean(ssl, ctx);
133             return;
134         }
135 
136         wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
137 
138         if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
139         {
140             /* get and print out the error */
141             Console.WriteLine(wolfssl.get_error(ssl));
142             tcp.Stop();
143             clean(ssl, ctx);
144             return;
145         }
146 
147         /* print out results of TLS/SSL accept */
148         Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
149         Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
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.Stop();
156             clean(ssl, ctx);
157             return;
158         }
159         Console.WriteLine(buff);
160 
161         if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
162         {
163             Console.WriteLine("Error in write");
164             tcp.Stop();
165             clean(ssl, ctx);
166             return;
167         }
168 
169         wolfssl.shutdown(ssl);
170         fd.Close();
171         tcp.Stop();
172         clean(ssl, ctx);
173     }
174 }
175