2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Bakalarska praca //
|
|
|
|
// Meno studenta: Tomas Lukac //
|
|
|
|
// Veduci BP: prof. Ing. Milos Drutarovsky CSc. //
|
|
|
|
// Skola: KEMT FEI TUKE //
|
2020-05-06 10:53:08 +00:00
|
|
|
// Datum poslednej upravy: 6.5.2020 //
|
2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2020-04-16 09:25:05 +00:00
|
|
|
#include "../../kniznica/kryptografia.h"
|
|
|
|
#include "../../kniznica/komunikacia.h"
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
#include <wolfssl/ssl.h>
|
|
|
|
|
2020-03-13 09:25:19 +00:00
|
|
|
#define RSA_VELKOST 2048
|
2020-03-11 21:01:54 +00:00
|
|
|
#define ECC_VELKOST 32
|
|
|
|
#define RSA_EXPONENT 65537
|
|
|
|
|
2020-03-19 12:55:34 +00:00
|
|
|
//cesty ku suborom
|
2020-04-16 09:25:05 +00:00
|
|
|
#define RSA_KLUC "../../certifikaty/klient/klient_rsa.key"
|
|
|
|
#define RSA_CERTIFIKAT "../../certifikaty/klient/klient_rsa.pem"
|
|
|
|
#define ECC_KLUC "../../certifikaty/klient/klient_ecc.key"
|
|
|
|
#define ECC_CERTIFIKAT "../../certifikaty/klient/klient_ecc.pem"
|
|
|
|
#define VYGENEROVANY_KLUC "../../certifikaty/klient/vygenerovany_kluc.key"
|
|
|
|
#define VYGENEROVANY_CERTIFIKAT "../../certifikaty/klient/vygenerovany_certifikat.pem"
|
2020-05-17 13:12:06 +00:00
|
|
|
#define VYGENEROVANA_ZIADOST "../../certifikaty/klient/vygenerovana_ziadost.pem"
|
2020-03-19 12:55:34 +00:00
|
|
|
|
2020-03-11 21:01:54 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
int autentizacia = 0;
|
|
|
|
clock_t cas;
|
2020-03-19 12:55:34 +00:00
|
|
|
int uspech;
|
2020-03-11 21:01:54 +00:00
|
|
|
int generovanie_certifikatu = 0;
|
|
|
|
int nacitanie_zo_suboru = 0;
|
2020-03-12 16:22:08 +00:00
|
|
|
int uvedeny_subor = 0;
|
2020-03-11 21:01:54 +00:00
|
|
|
int ip = 0;
|
|
|
|
int port = 0;
|
2020-04-07 20:11:21 +00:00
|
|
|
nastavenia_aplikacie nastavenia;
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
#if defined (_WIN32)
|
|
|
|
WSADATA d;
|
|
|
|
if(WSAStartup(MAKEWORD(2,2), &d))
|
|
|
|
{
|
|
|
|
printf("Nastala chyba pri inicializacii winsocketu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int cislo_soketu = 0;
|
|
|
|
int cislo_portu = 0;
|
2020-03-12 16:22:08 +00:00
|
|
|
char* ip_adresa = NULL;
|
|
|
|
char* subor = NULL;
|
2020-03-11 21:01:54 +00:00
|
|
|
WOLFSSL *ssl; WOLFSSL_CTX *ctx;
|
|
|
|
|
|
|
|
if((ctx = nastavit_ctx_klient()) == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//skontroluje ci nebol zadany prepinac "-g" pre moznost generovanie certifikatu,
|
|
|
|
//ak ano pozrie aky typ certifikatu (v CLI argument hned za nim) bol zvoleny
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("---------------\n| Certifikaty |\n---------------\n");
|
2020-03-11 21:01:54 +00:00
|
|
|
for(int i = 0; i < argc; i++)
|
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
if( (!strcmp(argv[i], "-subor")) )
|
2020-03-12 16:22:08 +00:00
|
|
|
{
|
|
|
|
uvedeny_subor = 1;
|
|
|
|
if((argv[i+1] == NULL) )
|
|
|
|
{
|
|
|
|
printf("Nezadali ste cestu ku suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
subor = argv[i+1];
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 13:12:06 +00:00
|
|
|
if( (!strcmp(argv[i], "-ip-adr")) )
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
ip = 1;
|
2020-03-11 21:01:54 +00:00
|
|
|
if((argv[i+1] == NULL) )
|
|
|
|
{
|
|
|
|
printf("Nezadali ste ip adresu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ip_adresa = argv[i+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( (!strcmp(argv[i], "-port")) )
|
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
port = 1;
|
2020-03-11 21:01:54 +00:00
|
|
|
if((argv[i+1] == NULL) )
|
|
|
|
{
|
|
|
|
printf("Nezadali ste cislo portu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cislo_portu = atoi(argv[i+1]);
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 13:12:06 +00:00
|
|
|
else if( (!strcmp(argv[i], "-cert-aut")) )
|
|
|
|
{
|
|
|
|
if((argv[i+1] == NULL) || (i == argc-1))
|
|
|
|
{
|
|
|
|
printf("Nezadali ste typ certifikatu autority ktory chcete nacitat\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "rsa"))
|
|
|
|
{
|
|
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../../certifikaty/autorita/autorita_rsa.pem", NULL);
|
|
|
|
wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
|
|
|
|
autentizacia = 1;
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "ecc"))
|
|
|
|
{
|
|
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../../certifikaty/autorita/autorita_ecc.pem", NULL);
|
|
|
|
wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
|
|
|
|
autentizacia = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Zadali ste nespravny typ certifikatu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( (!strcmp(argv[i], "-cert")) )
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
nacitanie_zo_suboru = 1;
|
|
|
|
if(generovanie_certifikatu)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nie je mozne zvolit obidve metody nacitania certifikatov naraz\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if((argv[i+1] == NULL) || (i == argc-1))
|
|
|
|
{
|
|
|
|
printf("Nezadali ste typ certifikatu ktory chcete nacitat zo suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "rsa"))
|
|
|
|
{
|
2020-03-19 12:55:34 +00:00
|
|
|
if(nacitat_certifikaty(ctx, RSA_CERTIFIKAT, RSA_KLUC) == -1) return -1;
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "ecc"))
|
|
|
|
{
|
2020-03-19 12:55:34 +00:00
|
|
|
if(nacitat_certifikaty(ctx, ECC_CERTIFIKAT, ECC_KLUC) == -1) return -1;
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Zadali ste nespravny typ certifikatu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 13:12:06 +00:00
|
|
|
else if( (!strcmp(argv[i], "-cert-gen")) )
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
generovanie_certifikatu = 1;
|
2020-05-17 13:12:06 +00:00
|
|
|
if(nacitanie_zo_suboru)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nie je mozne zvolit obidve metody nacitania certifikatov naraz\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
if((argv[i+1] == NULL) || (i == argc-1))
|
|
|
|
{
|
|
|
|
printf("Nezadali ste typ certifikatu ktory chcete vygenerovat\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "rsa"))
|
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
if(generovat_rsa_certifikat(VYGENEROVANY_CERTIFIKAT, VYGENEROVANY_KLUC, RSA_VELKOST, RSA_EXPONENT, CTC_SHA256wRSA, "SR", "Kosice", "local.dev", "server@server.sk") == -1) return -1;
|
2020-03-19 12:55:34 +00:00
|
|
|
if(nacitat_certifikaty(ctx, VYGENEROVANY_CERTIFIKAT, VYGENEROVANY_KLUC) == -1) return -1;
|
2020-05-17 13:12:06 +00:00
|
|
|
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
else if(!strcmp(argv[i+1], "ecc"))
|
|
|
|
{
|
2020-05-17 13:12:06 +00:00
|
|
|
if(generovat_ecc_certifikat(VYGENEROVANY_CERTIFIKAT, VYGENEROVANY_KLUC, ECC_VELKOST, ECC_SECP256R1, CTC_SHAwECDSA, "SR", "Kosice", "local.dev", "server@server.sk") == -1) return -1;
|
2020-03-19 12:55:34 +00:00
|
|
|
if(nacitat_certifikaty(ctx, VYGENEROVANY_CERTIFIKAT, VYGENEROVANY_KLUC) == -1) return -1;
|
2020-03-11 21:01:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Zadali ste nespravny typ certifikatu\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 13:12:06 +00:00
|
|
|
|
|
|
|
//vypis upozornenia o autentizacii
|
|
|
|
if(!autentizacia)
|
|
|
|
{
|
|
|
|
printf("Nebol nacitany certifikat autority, takze nebude vykonana autentizacia druhej strany\n");
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
if(!ip)
|
|
|
|
{
|
|
|
|
printf("Nebola zadana ip adresa servera\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(!port)
|
|
|
|
{
|
|
|
|
printf("Nebol urceny port\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
else if(!generovanie_certifikatu && !nacitanie_zo_suboru)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebola zvolena metoda nacitania certifikatov\n");
|
|
|
|
printf("Zadajde prepinac -g (generovanie), alebo -n (nacitanie_zo_suboru) s parametrom rsa alebo ecc\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
else if(!uvedeny_subor)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebola uvedena cesta ku suboru na odoslanie\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
//umoznuje vybrat sifry ktore sa budu nachadzat v sifrovacom subore
|
|
|
|
//nastav_sifry(ctx, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
//pokus o inicializaciu TCP/IP prenosoveho kanala
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("----------------\n| TCP/IP kanal |\n----------------\n");
|
|
|
|
printf("Pokus o inicializaciu TCP/IP prenosoveho kanala\n");
|
2020-05-06 10:46:52 +00:00
|
|
|
cislo_soketu = pripojit_na_server(ip_adresa, cislo_portu, 5);
|
2020-03-11 21:01:54 +00:00
|
|
|
if(!cislo_soketu) return -1;
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("TCP/IP kanal bol uspesne vytvoreny\n");
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
//pokus o vytvorenie novej wolfSSL relacie
|
2020-03-11 21:01:54 +00:00
|
|
|
ssl = wolfSSL_new(ctx);
|
2020-04-16 09:25:05 +00:00
|
|
|
|
|
|
|
//priradenie file descriptora soketu ako I/O pre TLS spojenie
|
2020-03-11 21:01:54 +00:00
|
|
|
wolfSSL_set_fd(ssl, cislo_soketu);
|
2020-05-17 13:12:06 +00:00
|
|
|
wolfSSL_set_using_nonblock(ssl, 1);
|
2020-03-13 09:25:19 +00:00
|
|
|
|
2020-04-16 09:25:05 +00:00
|
|
|
//pokus o inizicalizaciu TLS handshaku so serverom
|
|
|
|
uspech = wolfSSL_connect(ssl);
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("-----------------\n| TLS Handshake |\n-----------------\n");
|
|
|
|
printf("Inicializacia TLS spojenia\n");
|
2020-03-11 21:01:54 +00:00
|
|
|
if(uspech != SSL_SUCCESS)
|
|
|
|
{
|
2020-03-13 09:25:19 +00:00
|
|
|
char* popis_chyby = calloc(100, sizeof(char));
|
|
|
|
int chyba = wolfSSL_get_error(ssl, 0);
|
|
|
|
wolfSSL_ERR_error_string(chyba, popis_chyby);
|
|
|
|
fprintf(stderr, "Nastala chyba v spojeni.\nCislo chyby: %d\nDovod chyby: %s\n", chyba, popis_chyby);
|
2020-03-11 21:01:54 +00:00
|
|
|
printf("Skontrolujte certifikaty.\n");
|
2020-04-16 09:25:05 +00:00
|
|
|
free(popis_chyby);
|
2020-03-11 21:01:54 +00:00
|
|
|
return -1;
|
2020-05-17 13:12:06 +00:00
|
|
|
}
|
|
|
|
printf("Podarilo sa vytvorit TLS spojenie\n");
|
2020-03-11 21:01:54 +00:00
|
|
|
zobraz_sifru(ssl);
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("----------------------------\n| Informacie o certifikate |\n----------------------------\n");
|
2020-03-11 21:01:54 +00:00
|
|
|
zobraz_certifikat(ssl);
|
2020-05-06 10:46:52 +00:00
|
|
|
|
|
|
|
//nastavenie funkcie pre vypocet kontrolneho suctu
|
|
|
|
//nastavime typ "crc" alebo "hash" a nazov funkcie, nazov druhej funkcie nastavime na NULL
|
|
|
|
nastav_funkciu(&nastavenia, "crc", (hashovacia_funkcia)NULL, funkcia_CRC32);
|
2020-05-17 13:12:06 +00:00
|
|
|
|
|
|
|
printf("-------------------\n| Poslanie suboru |\n-------------------\n");
|
|
|
|
|
|
|
|
if(poslat_subor(ssl, ctx, subor, &nastavenia, 16384) == -1) return -1;
|
2020-05-06 10:46:52 +00:00
|
|
|
|
2020-05-17 13:12:06 +00:00
|
|
|
printf("----------------------\n| Ukoncenie spojenia |\n----------------------\n");
|
2020-03-11 21:01:54 +00:00
|
|
|
ukoncit_spojenie(ssl, ctx);
|
|
|
|
}
|
2020-05-06 10:46:52 +00:00
|
|
|
ukoncit_soket(cislo_soketu);
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
#if defined (_WIN32)
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|