1 /* wolf_server.c
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 #include <stdio.h>
22 #include <string.h>
23 #include "r_t4_itcpip.h"
24
25 #include "wolfssl/wolfcrypt/settings.h"
26 #include "wolfssl/ssl.h"
27 #include "wolfssl/certs_test.h"
28 #include "wolfssl_demo.h"
29
30 static WOLFSSL_CTX *server_ctx;
31 static byte doCliCertCheck;
32
my_IORecv(WOLFSSL * ssl,char * buff,int sz,void * ctx)33 static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx)
34 {
35 int ret;
36 ID cepid;
37
38 if(ctx != NULL)
39 cepid = *(ID *)ctx;
40 else
41 return WOLFSSL_CBIO_ERR_GENERAL;
42
43 ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR);
44 if(ret == sz)
45 return ret;
46 else
47 return WOLFSSL_CBIO_ERR_GENERAL;
48 }
49
my_IOSend(WOLFSSL * ssl,char * buff,int sz,void * ctx)50 static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx)
51 {
52 int ret;
53 ID cepid;
54
55 if(ctx != NULL)
56 cepid = *(ID *)ctx;
57 else
58 return WOLFSSL_CBIO_ERR_GENERAL;
59
60 ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR);
61 if(ret == sz)
62 return ret;
63 else
64 return WOLFSSL_CBIO_ERR_GENERAL;
65 }
66
67
wolfSSL_TLS_server_init(byte doClientCheck)68 void wolfSSL_TLS_server_init(byte doClientCheck)
69 {
70
71 int ret;
72
73
74 #ifndef NO_FILESYSTEM
75 #ifdef USE_ECC_CERT
76 char *cert = "./certs/server-ecc-cert.pem";
77 char *key = "./certs/server-ecc-key.pem";
78 #else
79 char *cert = "./certs/server-cert.pem";
80 char *key = "./certs/server-key.pem";
81 #endif
82 char *clientCert = "./certs/client-cert.pem";
83 #else
84 #ifdef USE_ECC_CERT
85 char *cert = serv_ecc_der_256;
86 int sizeof_cert = sizeof_serv_ecc_der_256;
87 char *cert = serv_ecc_key_der_256;
88 int sizeof_key = sizeof_serv_ecc_key_der_256;
89 #else
90 const unsigned char *cert = server_cert_der_2048;
91 #define sizeof_cert sizeof_server_cert_der_2048
92 const unsigned char *key = server_key_der_2048;
93 #define sizeof_key sizeof_server_key_der_2048
94 const unsigned char *clientCert = client_cert_der_2048;
95 #define sizeof_clicert sizeof_client_cert_der_2048
96 #endif
97 #endif
98
99
100 wolfSSL_Init();
101 #ifdef DEBUG_WOLFSSL
102 wolfSSL_Debugging_ON();
103 #endif
104
105 /* Create and initialize WOLFSSL_CTX */
106 if ((server_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method_ex((void *)NULL)))
107 == NULL) {
108 printf("ERROR: failed to create WOLFSSL_CTX\n");
109 return;
110 }
111
112 #if !defined(NO_FILESYSTEM)
113 ret = wolfSSL_CTX_use_certificate_file(server_ctx, cert, 0);
114 #else
115 ret = wolfSSL_CTX_use_certificate_buffer(server_ctx, cert,
116 sizeof_cert, SSL_FILETYPE_ASN1);
117 #endif
118 if (ret != SSL_SUCCESS) {
119 printf("Error %d loading server-cert!\n", ret);
120 return;
121 }
122
123 /* Load server key into WOLFSSL_CTX */
124 #if !defined(NO_FILESYSTEM)
125 ret = wolfSSL_CTX_use_PrivateKey_file(server_ctx, key, 0);
126 #else
127 ret = wolfSSL_CTX_use_PrivateKey_buffer(server_ctx, key, sizeof_key,
128 SSL_FILETYPE_ASN1);
129 #endif
130 if (ret != SSL_SUCCESS) {
131 printf("Error %d loading server-key!\n", ret);
132 return;
133 }
134 #if defined(WOLFSSL_RENESAS_TSIP)
135 doCliCertCheck = 1;
136 #endif
137 if (doCliCertCheck) {
138 wolfSSL_CTX_set_verify(server_ctx, WOLFSSL_VERIFY_PEER |
139 WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
140 #if !defined(NO_FILESYSTEM)
141 if (wolfSSL_CTX_load_verify_locations(server_ctx, clientCert, 0)
142 != WOLFSSL_SUCCESS)
143 #else
144 if (wolfSSL_CTX_load_verify_buffer(server_ctx, clientCert,
145 sizeof_clicert,
146 SSL_FILETYPE_ASN1) != SSL_SUCCESS)
147 #endif
148 printf("can't load ca file, Please run from wolfSSL home dir\n");
149 }
150
151 /* Register callbacks */
152 wolfSSL_SetIORecv(server_ctx, my_IORecv);
153 wolfSSL_SetIOSend(server_ctx, my_IOSend);
154
155 }
156
wolfSSL_TLS_server()157 void wolfSSL_TLS_server( )
158 {
159 ID cepid = 1;
160 ID repid = 1;
161 ER ercd;
162 WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)server_ctx;
163
164 WOLFSSL *ssl;
165 int len;
166 #define BUFF_SIZE 256
167 char buff[BUFF_SIZE];
168 T_IPV4EP dst_addr = {0, 0};
169
170 if((ercd = tcp_acp_cep(cepid, repid, &dst_addr, TMO_FEVR)) != E_OK) {
171 printf("ERROR TCP Accept: %d\n", ercd);
172 return;
173 }
174
175 if((ssl = wolfSSL_new(ctx)) == NULL) {
176 printf("ERROR: failed wolfSSL_new\n");
177 return;
178 }
179
180 wolfSSL_SetIOReadCtx(ssl, (void *)&cepid);
181 wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid);
182
183 if (wolfSSL_accept(ssl) < 0) {
184 printf("ERROR: SSL Accept(%d)\n", wolfSSL_get_error(ssl, 0));
185 return;
186 }
187
188 if ((len = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) < 0) {
189 printf("ERROR: SSL Read(%d)\n", wolfSSL_get_error(ssl, 0));
190 return;
191 }
192
193 buff[len] = '\0';
194 printf("Received: %s\n", buff);
195
196 if (wolfSSL_write(ssl, buff, len) != len) {
197 printf("ERROR: SSL Write(%d)\n", wolfSSL_get_error(ssl, 0));
198 return;
199 }
200
201 wolfSSL_free(ssl);
202 tcp_sht_cep(cepid);
203 }
204