Nowe posty

Autor Wątek: klient serwer + DTLS (SSL po UDP)  (Przeczytany 3522 razy)

linuxnew1

  • Gość
klient serwer + DTLS (SSL po UDP)
« dnia: 2008-11-22, 15:57:14 »
witam, pisze program klient serwer komunikujący sie z zabezpieczeniem SSL w kanale UDP (DTLS). Po uruchomieniu wireshark przechwytuje tylko "DTLSclient hello" z komunikatem "UDP checksum incorrect". Serwerowi wygenerowalem prosty certyfikat ktory udostepnia klientowi. Ponizej kody klienta i serwera. Bardzo prosilbym o pomoc czy czegos brakuje, czy gdzies jest blad. Prosze nie zwracac uwagi na estetyke kodu gdyz jestem laikiem:

Serwer:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include

#define SERVER_CERT "cacert.pem"

void LoadCertificates(SSL_CTX* ctx, char* CertFile, char* KeyFile)
{
/* set the local certificate from CertFile */
if ( SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* set the private key from KeyFile (may be the same as CertFile) */
if ( SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* verify private key */
if ( !SSL_CTX_check_private_key(ctx) )
{
fprintf(stderr, "Private key does not match the public certificate\\n");
abort();
}
}

void ShowCerts(SSL* ssl)
{ X509 *certyfikat;
char *zawartosc;

certyfikat = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
if ( certyfikat != NULL )
{
printf("Certyfikat serwera:\\n");
zawartosc = X509_NAME_oneline(X509_get_subject_name(certyfikat), 0, 0);
printf("Temat: %s\\n", zawartosc);
free(zawartosc);
zawartosc = X509_NAME_oneline(X509_get_issuer_name(certyfikat), 0, 0);
printf("Issuer: %s\\n", zawartosc);
free(zawartosc);
X509_free(certyfikat);
}
else
printf("Brak certyfikatu\\n");
}

int main() {
int sock, sock_akceptacyjny, port;
struct hostent *host = NULL;

port=88455;
struct sockaddr_in sockaddr,clientaddr;
socklen_t wielkosc = sizeof(sockaddr);

if((sock = socket(AF_INET, SOCK_DGRAM, 0))==-1) {
printf("Blad podczas tworzenia gniazda udp");
return 0;
}
memset(&sockaddr,0,sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockaddr.sin_port = htons(port);

if(bind(sock,(struct sockaddr*)&sockaddr,sizeof(sockaddr)) != 0) {
printf("blad bindowania\\n");
return 0;
}
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSL_METHOD* meth = DTLSv1_server_method();
SSL_CTX* ctx = SSL_CTX_new(meth);
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
LoadCertificates(ctx, "servercert.pem", "servercert.pem");
/*if(listen(sock,1) != 0) {
printf("blad listen");
return 0;
}
printf("Oczekuje na polaczenie\\n");
sock_akceptacyjny = accept(sock,0,&wielkosc);
if(sock_akceptacyjny == -1) {
printf("blad accept\\n");
return 0;
}*/
//Function retrieves host information
host = gethostbyname("localhost");
if (host == NULL)
{
printf("Nie udało się wydobyć nazwy serwera\\n");
return 1;
}
printf("\\nSerwer pracuje na hoscie o adresie: %s",inet_ntoa(*((struct in_addr*)host->h_addr)));
printf("\\nSerwer nazywa sie: %s",inet_ntoa(*((struct in_addr*)host->h_name)));
printf("\\nNawiazano polaczenie%s:%d\\n",inet_ntoa(sockaddr.sin_addr), ntohs(sockaddr.sin_port));
SSL* ssl;
BIO *sbio = NULL;
ssl = SSL_new(ctx);

SSL_set_accept_state(ssl);
ShowCerts(ssl); /* get any certificates */

printf("Wersja protokolu: %s\\n", SSL_get_cipher_version(ssl));

char bufor[1024];
char odpowiedz_bufor[1024];
int ilosc,s;
const char* odpowiedz_serwera="To ja serwer, otrzymalem twoja wiadomosc\\n\\n";
//printf("Nawiazano polaczenie%s:%d\\n",inet_ntoa(sockaddr.sin_addr), ntohs(sockaddr.sin_port));
printf("wcisnij aby kontynuowac\\n");
//scanf("%s",&s);
etykieta:
ilosc = SSL_read(ssl, bufor, sizeof(bufor)); /* get request */
if (ilosc>0)
{
bufor[ilosc] = 0;
printf("Wiadomosc od klienta: \\"%s\\"\\n", bufor);
sprintf(odpowiedz_bufor, odpowiedz_serwera, bufor); /* construct reply */
SSL_write(ssl, odpowiedz_bufor, strlen(odpowiedz_bufor));/* send reply */
}

goto etykieta;
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
return 0;
}

================================================================
Klient:
#ifdef HAVE_CONFIG_H
#include
#endif

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


int sock;

void ShowCerts(SSL* ssl)
{ X509 *certyfikat;
char *line;

certyfikat = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( certyfikat != NULL )
{
printf("Certyfikat serwera:\\n");
line = X509_NAME_oneline(X509_get_subject_name(certyfikat), 0, 0);
printf("Temat: %s\\n", line);
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(certyfikat), 0, 0);
printf("Issuer: %s\\n", line);
free(line); /* free the malloc'ed string */
X509_free(certyfikat); /* free the malloc'ed certificate copy */
}
else
printf("Serwer nie ma certyfikatu.\\n");
}

int main() {
char bufor[1024];
char ip[512];
int ilosc, sock, wybor, port,a;

port=88455;
struct sockaddr_in sockaddr;

SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSLeay_add_ssl_algorithms();

BIO *sbio;

SSL_METHOD* meth = DTLSv1_client_method();

SSL_CTX* ctx = SSL_CTX_new(meth);
SSL* ssl;
ssl = SSL_new(ctx);

SSL_CTX_set_verify_depth(ctx, 1);
if((sock = socket(PF_INET, SOCK_DGRAM, 0))==-1) {
printf("Blad podczas tworzenia gniazda udp");
return 0;
};
fcntl(sock, F_SETFL, O_NONBLOCK);
memset(&sockaddr,0,sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockaddr.sin_port = htons(port);

SSL_set_connect_state(ssl);
if((connect(sock,(struct sockaddr*)&sockaddr, sizeof(sockaddr)))==-1) {
printf("blad connect");
};

SSL_set_fd(ssl,sock);

SSL_set_connect_state (ssl);

printf("Połączono z zabezpieczeniem: %s\\n", SSL_get_cipher(ssl));
printf("Wersja protokolu: %s\\n", SSL_get_cipher_version(ssl));
ShowCerts(ssl);
char *msg = "To ja klient";

while(1) {
SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
ilosc = SSL_read(ssl, bufor, sizeof(bufor)); /* get reply & decrypt */
bufor[ilosc] = 0;
printf("Odebralem od serwera: %s\\n", bufor);
}

SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
return 0;
}

darko

  • Gość
klient serwer + DTLS (SSL po UDP)
« Odpowiedź #1 dnia: 2008-11-22, 20:09:15 »
Cytat: linuxnew1
port=88455;
Jesteś tego pewny?

linuxnew1

  • Gość
klient serwer + DTLS (SSL po UDP)
« Odpowiedź #2 dnia: 2008-11-22, 20:32:54 »
To nie port bo na 2345 jest to samo takze nie tedy droga niestety, zbyt proste zeby bylo prawdziwe, nie wiem czy pominalem jakies funkcje... bo np przy polaczeniu przez zwykle SSL jest SSL_connect, accept itp a tu w kanale bezpolaczeniowym nie wiem jak to powinno sie odbywac. ma ktos jakies sugestie?