105 lines
2.7 KiB
C
105 lines
2.7 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/options.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 = 2;
|
|
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("Klient posiela ...\n");
|
|
int n = RS232_SendBuf(cislo_portu, buf, sz);
|
|
printf("Klient 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("Klienta prijima...\n");
|
|
int n;
|
|
while(n <= 0)
|
|
n = RS232_PollComport(cislo_portu, buf, sz);
|
|
printf("Klienta prijal %d bajtov.\n", n);
|
|
return n;
|
|
}
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
WOLFSSL *ssl;
|
|
WOLFSSL_CTX *ctx = NULL;
|
|
|
|
if(RS232_OpenComport(cislo_portu, baud_frekvencia, rezim, 1))
|
|
{
|
|
printf("Nebolo mozne otvorit serialovy port\n");
|
|
}
|
|
|
|
if((ctx = nastavit_ctx_klient()) == 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;
|
|
}
|
|
|
|
wolfSSL_set_fd(ssl, *(int*)ctx);
|
|
wolfSSL_set_using_nonblock(ssl, *(int*)ctx);
|
|
|
|
int uspech = 0;
|
|
if(wolfSSL_connect(ssl) != SSL_SUCCESS)
|
|
{
|
|
printf("nepodarilo sa pripojit\n");
|
|
return -1;
|
|
}
|
|
|
|
RS232_CloseComport(cislo_portu);
|
|
return 0;
|
|
} |