181 lines
5.2 KiB
C
181 lines
5.2 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 "../kniznica/kryptografia.h"
|
|
#include "../kniznica/komunikacia.h"
|
|
|
|
#include <wolfssl/ssl.h>
|
|
|
|
#define RSA_VELKOST 1024
|
|
#define ECC_VELKOST 32
|
|
#define RSA_EXPONENT 65537
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
int autentizacia_klienta = 0;
|
|
int generovanie_certifikatu = 0;
|
|
int nacitanie_zo_suboru = 0;
|
|
int ip = 0;
|
|
int port = 0;
|
|
|
|
#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;
|
|
char *ip_adresa = NULL;
|
|
const char *subor_certifikat = NULL;
|
|
const char *subor_kluc = NULL;
|
|
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
|
|
for(int i = 0; i < argc; i++)
|
|
{
|
|
if( (!strcmp(argv[i], "-ip")) )
|
|
{
|
|
port = 1;
|
|
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")) )
|
|
{
|
|
ip = 1;
|
|
if((argv[i+1] == NULL) )
|
|
{
|
|
printf("Nezadali ste cislo portu\n");
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
cislo_portu = atoi(argv[i+1]);
|
|
}
|
|
}
|
|
else if( (!strcmp(argv[i], "-n")) )
|
|
{
|
|
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"))
|
|
{
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../certifikaty/autorita_rsa.pem", NULL);
|
|
subor_certifikat = "../certifikaty/server_rsa.pem";
|
|
subor_kluc = "../certifikaty/server_rsa.key";
|
|
if(nacitat_certifikaty(ctx, subor_certifikat, subor_kluc) == -1) return -1;
|
|
}
|
|
else if(!strcmp(argv[i+1], "ecc"))
|
|
{
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../certifikaty/autorita_ecc.pem", NULL);
|
|
subor_certifikat = "../certifikaty/server_ecc.pem";
|
|
subor_kluc = "../certifikaty/server_ecc.key";
|
|
if(nacitat_certifikaty(ctx, subor_certifikat, subor_kluc) == -1) return -1;
|
|
}
|
|
else
|
|
{
|
|
printf("Zadali ste nespravny typ certifikatu\n");
|
|
return -1;
|
|
}
|
|
}
|
|
else if( (!strcmp(argv[i], "-g")) )
|
|
{
|
|
generovanie_certifikatu = 1;
|
|
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"))
|
|
{
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../certifikaty/autorita_rsa.pem", NULL);
|
|
if(generovat_rsa_certifikat(ctx, RSA_VELKOST, RSA_EXPONENT, CTC_SHA256wRSA, "SR", "Kosice", "Klient.sk", "klient@klient.sk") == -1)
|
|
return -1;
|
|
}
|
|
else if(!strcmp(argv[i+1], "ecc"))
|
|
{
|
|
wolfSSL_CTX_load_verify_locations(ctx, "../certifikaty/autorita_ecc.pem", NULL);
|
|
if(generovat_ecc_certifikat(ctx, ECC_VELKOST, ECC_PRIME239V1, CTC_SHAwECDSA, "SR", "Kosice", "Klient.sk", "klient@klient.sk") == -1)
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
printf("Zadali ste nespravny typ certifikatu\n");
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!ip)
|
|
{
|
|
printf("Nebola zadana ip adresa servera\n");
|
|
return -1;
|
|
}
|
|
else if(!port)
|
|
{
|
|
printf("Nebol urceny port\n");
|
|
return -1;
|
|
}
|
|
if(!generovanie_certifikatu && !nacitanie_zo_suboru)
|
|
{
|
|
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;
|
|
}
|
|
//umoznuje vybrat sifry ktore sa budu nachadzat v sifrovacom subore
|
|
//nastav_sifry(ctx, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
|
|
|
|
cislo_soketu = pripojit_na_server(ip_adresa, cislo_portu, 10);
|
|
|
|
if(!cislo_soketu) return -1;
|
|
ssl = wolfSSL_new(ctx);
|
|
wolfSSL_set_fd(ssl, cislo_soketu);
|
|
int uspech = wolfSSL_connect(ssl);
|
|
if(uspech != SSL_SUCCESS)
|
|
{
|
|
fprintf(stderr, "Nastala chyba v spojeni.\n");
|
|
printf("Skontrolujte certifikaty.\n");
|
|
return -1;
|
|
}
|
|
zobraz_sifru(ssl);
|
|
zobraz_certifikat(ssl);
|
|
if(poslat_subor(ssl, ctx, "nieco.txt") == -1) return -1;
|
|
ukoncit_spojenie(ssl, ctx);
|
|
}
|
|
|
|
#if defined (_WIN32)
|
|
WSACleanup();
|
|
#endif
|
|
return 0;
|
|
}
|