118 lines
2.9 KiB
C
118 lines
2.9 KiB
C
|
//////////////////////////////////////////////////
|
||
|
// Bakalarska praca //
|
||
|
// Meno studenta: Tomas Lukac //
|
||
|
// Veduci BP: prof. Ing. Milos Drutarovsky CSc. //
|
||
|
// Skola: KEMT FEI TUKE //
|
||
|
// Datum poslednej upravy: 9.3.2020 //
|
||
|
//////////////////////////////////////////////////
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <wolfssl/wolfcrypt/settings.h>
|
||
|
#include <wolfssl/ssl.h>
|
||
|
#include <wolfssl/certs_test.h>
|
||
|
#include <wolfssl/wolfcrypt/types.h>
|
||
|
|
||
|
#ifdef _WIN32
|
||
|
#include <Windows.h>
|
||
|
#else
|
||
|
#include <unistd.h>
|
||
|
#endif
|
||
|
|
||
|
#include "../kniznica/kryptografia.h"
|
||
|
#include "../kniznica/rs232.h"
|
||
|
|
||
|
int cislo_portu = 3;
|
||
|
int baud_frekvencia = 9600;
|
||
|
char rezim[]={'8','N','1', 0};
|
||
|
|
||
|
/**
|
||
|
* rs232_zapis: umoznuje zapis dat do serialoveho portu
|
||
|
* @parameter WOLSSL* ssl : ukazuje na aktualnu relaciu
|
||
|
* @parameter char* buf : ukazuje na buffer, do kt. zapise wolfssl zasifrovany text na odoslanie
|
||
|
* @parameter int sz : velkost buffera
|
||
|
* @vrati int
|
||
|
*/
|
||
|
int rs232_zapis(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||
|
{
|
||
|
printf("Server posiela ...\n");
|
||
|
int n = RS232_SendBuf(cislo_portu, buf, sz);
|
||
|
printf("Server poslal %d bajtov\n", n);
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* rs232_citanie: umoznuje citanie dat zo serialoveho portu
|
||
|
* @parameter WOLFSSL* ssl : ukazuje na aktualnu relaciu
|
||
|
* @parameter char* buf : ukazuje na buffer, kde prichadzajuci zasifrovany text by mal byt nakopirovany
|
||
|
* aby ho wolfssl mohla odsifrovat
|
||
|
* @parameter int sz : velkost buffera
|
||
|
* @vrati int
|
||
|
*/
|
||
|
int rs232_citanie(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||
|
{
|
||
|
printf("Server prijima...\n");
|
||
|
int n;
|
||
|
int prijal = 0;
|
||
|
while(1)
|
||
|
{
|
||
|
n = RS232_PollComport(cislo_portu, buf, sz);
|
||
|
if(n > 0)
|
||
|
{
|
||
|
buf[n] = '\0'; /* always put a "null" at the end of a string! */
|
||
|
for(int i=0; i < n; i++)
|
||
|
{
|
||
|
if(buf[i] < 32) /* replace unreadable control-codes by dots */
|
||
|
{
|
||
|
buf[i] = '.';
|
||
|
}
|
||
|
}
|
||
|
printf("received %i bytes: %s\n", n, (char *)buf);
|
||
|
}
|
||
|
prijal += n;
|
||
|
|
||
|
}
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char const *argv[])
|
||
|
{
|
||
|
WOLFSSL *ssl;
|
||
|
WOLFSSL_CTX *ctx = NULL;
|
||
|
|
||
|
//otvorenie portu pre citanie
|
||
|
if(RS232_OpenComport(cislo_portu, baud_frekvencia, rezim, 1))
|
||
|
{
|
||
|
printf("Nebolo mozne otvorit serialovy port\n");
|
||
|
}
|
||
|
|
||
|
if((ctx = nastavit_ctx_server()) == NULL)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
const char* subor_certifikat = "../certifikaty/klient.pem";
|
||
|
const char* subor_kluc = "../certifikaty/klient.key";
|
||
|
if(!nacitat_certifikaty(ctx, subor_certifikat, subor_kluc)) return -1;
|
||
|
|
||
|
wolfSSL_SetIOSend(ctx, rs232_zapis);
|
||
|
wolfSSL_SetIORecv(ctx, rs232_citanie);
|
||
|
|
||
|
if ((ssl = wolfSSL_new(ctx)) == NULL)
|
||
|
{
|
||
|
printf("Nepodarilo sa vytvorit ssl relaciu\n");
|
||
|
wolfSSL_CTX_free(ctx);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int ret;
|
||
|
if(wolfSSL_accept(ssl) == -1)
|
||
|
{
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
RS232_CloseComport(cislo_portu);
|
||
|
|
||
|
return 0;
|
||
|
}
|