1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
| /*SERVEUR*/
#include "serveur_SSL.h"
//////////////////////////////////////////////////////////////////
// //
// Fonction Principale //
// //
//////////////////////////////////////////////////////////////////
int main()
{
int err;
int verify_client = OFF; /* To verify a client certificate, set ON */
int listen_sock;
int sock;
struct sockaddr_in sa_serv;
struct sockaddr_in sa_cli;
size_t client_len;
char *str;
char buf[100];
SSL_CTX *ctx;
SSL *ssl;
SSL_METHOD *meth;
X509 *client_cert = NULL;
short int s_port = 563;
int address_size;
FILE*fp,*fp_recu,*fpin,*fpout, *outfp, *infp, *decfp;
int recp_fich=0,ks=0,longueur=0,n=0;
unsigned char *cipher=NULL,*plain=NULL;
int size=0,len=0;
RSA *key=NULL;
long taille_fichier;
int flags1 = 0, flags2 = 0, outfd, infd, decfd;
mode_t mode;
bzero (&mode, sizeof (mode));
flags1 = flags1 | O_RDONLY;
flags2 = flags2 | O_RDONLY;
flags2 = flags2 | O_WRONLY;
flags2 = flags2 | O_CREAT;
mode = mode | S_IRUSR;
mode = mode | S_IWUSR;
////////////////////////////////////////////////////////////////
/*Initialization*/
/* Load encryption & hashing algorithms for the SSL program */
SSL_library_init();
/* Load the error strings for SSL & CRYPTO APIs */
SSL_load_error_strings();
/* Create a SSL_METHOD structure (choose a SSL/TLS protocol version) */
meth = SSLv3_method();
/* Create a SSL_CTX structure */
ctx = SSL_CTX_new(meth);
if (!ctx)
{
ERR_print_errors_fp(stderr);
exit(1);
}
/* Load the server certificate into the SSL_CTX structure */
if (SSL_CTX_use_certificate_file(ctx, RSA_SERVER_CERT, SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
exit(1);
}
/* Load the private-key corresponding to the server certificate */
if (SSL_CTX_use_PrivateKey_file(ctx, RSA_SERVER_KEY, SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
exit(1);
}
/* Check if the server certificate and private-key matches */
if (!SSL_CTX_check_private_key(ctx))
{
fprintf(stderr,"Private key does not match the certificate public key\n");
exit(1);
}
/*si on vérifie*/
if(verify_client == ON)
{
/* Load the RSA CA certificate into the SSL_CTX structure */
if (!SSL_CTX_load_verify_locations(ctx, RSA_SERVER_CA_CERT, NULL))
{
ERR_print_errors_fp(stderr);
exit(1);
}
/* Set to require peer (client) certificate verification */
SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL);
/* Set the verification depth to 1 */
SSL_CTX_set_verify_depth(ctx,1);
}
printf("Initialization terminate\n\n");
////////////////////////////////////////////////////////////////
/*SOCKET*/
/* Set up a TCP socket */
listen_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
RETURN_ERR(listen_sock, "socket");
memset (&sa_serv, '\0', sizeof(sa_serv));
sa_serv.sin_family = AF_INET;
sa_serv.sin_addr.s_addr = INADDR_ANY;
sa_serv.sin_port = htons (s_port);
/* Server Port number */
err = bind(listen_sock, (struct sockaddr*)&sa_serv,sizeof(sa_serv));
RETURN_ERR(err, "bind");
/* Wait for an incoming TCP connection. */
err = listen(listen_sock, 5);
RETURN_ERR(err, "listen");
client_len = sizeof(sa_cli);
printf("WAITING FOR CONNECTION\n\n");
/* Socket for a TCP/IP connection is created */
sock = accept(listen_sock, (struct sockaddr*)&sa_cli, &client_len);
RETURN_ERR(sock, "accept");
close (listen_sock);
printf ("Connection from %lx, port %x\n", sa_cli.sin_addr.s_addr,sa_cli.sin_port);
////////////////////////////////////////////////////////////////
/*SSL*/
/* TCP connection is ready. */
/* A SSL structure is created */
ssl = SSL_new(ctx);
RETURN_NULL(ssl);
/* Assign the socket into the SSL structure (SSL and socket without BIO) */
SSL_set_fd(ssl, sock);
/* Perform SSL Handshake on the SSL server */
err = SSL_accept(ssl); RETURN_SSL(err);
/* Informational output (optional) */
printf("SSL connection using %s\n", SSL_get_cipher (ssl));
if (verify_client == ON)
{
/* Get the client's certificate (optional) */
client_cert = SSL_get_peer_certificate(ssl);
if (client_cert != NULL)
{
printf ("Client certificate:\n");
str = X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0);
RETURN_NULL(str);
printf ("\t subject: %s\n", str);
free (str);
str = X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0);
RETURN_NULL(str);
printf ("\t issuer: %s\n", str);
free (str);
X509_free(client_cert);
}
else
printf("The SSL client does not have certificate.\n");
}
////////////////////////////////////////////////////////////////
/*------- DATA EXCHANGE - Receive message and send reply. -------*/
printf("\n************************************");
printf("\nDATA EXCHANGE\n");
/* Receive data from the SSL client */
err = SSL_read(ssl, buf, sizeof(buf) - 1);
RETURN_SSL(err); buf[err] = '\0';
printf ("Received %d chars:'%s'\n", err, buf);
/* Send data to the SSL client */
err = SSL_write(ssl, "This message is from the SSL server",strlen("This message is from the SSL server"));
RETURN_SSL(err);
//////////////////////////////////////////////////////////////////
/*GENERATION DES CLES*/
/*taille de la clé*/
ks = 1024;
printf("Generating RSA keys [size = %d bits]\n", ks);
/*appel de la fonction de génération des clés*/
genkey(ks);
/*affichage des fichiers contenant clé privée et clé publique*/
printf("Private Key saved in %s file.\n", CLE_PRIVE);
printf("Public Key saved in %s file.\n", CLE_PUBLIC);
printf("Done.\n\n");
//////////////////////////////////////////////////////////////////
/*ENVOI DE LA CLE PUBLIQUE*/
/*ouverture du fichier*/
fp=fopen(CLE_PUBLIC,"rb");
if (fp==NULL)
{
perror("call to open\n");
exit(1);
}
/*envoi de la taille du fichier*/
long taille =fsize(fp);
taille=htonl(taille);
longueur=sizeof(taille);
int env_taille=SSL_write(ssl, &taille, longueur);
if (env_taille==-1)
{
perror("call to send\n");
exit(1);
}
printf("send file size ok\n");
/*tant qu'on n'a pas tout envoyé, tant qu'on n'a pas atteint la fin du fichier*/
fseek(fp,0,SEEK_SET);
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
{
/*on envoi*/
int envoi=SSL_write(ssl, buf, n);
if (envoi==-1)
{
perror("call to send\n");
exit(1);
}
}
/*fermeture du fichier*/
fclose(fp);
printf("send Public Key OK\n\n");
//////////////////////////////////////////////////////////////////
/*RECEPTION DU FICHIER CHIFFRE*/
/*ouverture du fichier*/
fp_recu=fopen(FICHIER_CLE_SYMETRIQUE_ENC,"wb");
if (fp_recu==NULL)
{
perror("call to open\n");
exit(1);
}
/*reception de la taille du fichier*/
int recp_taille=SSL_read(ssl,&taille_fichier,sizeof(taille_fichier));
if (recp_taille==-1)
{
perror("call to rcv taille\n");
exit(1);
}
taille_fichier=ntohl(taille_fichier);
printf("size file %d receive\n",taille_fichier);
printf("Reception ...\n");
/*tant que l'on a pas tout reçu*/
while (taille_fichier > 0 && (recp_fich = SSL_read(ssl, buf, sizeof (buf))) > 0)
{
/*on recoi*/
if(recp_fich <0)
{
perror("call to rcv\n");
exit(1);
}
/*on écrit dans le fichier*/
fwrite(buf,1,recp_fich,fp_recu);
/*on diminue la taille*/
taille_fichier -= recp_fich;
/*RAZ buffer*/
memset(&buf,0,sizeof(buf));
}
/*fermeture du fichier*/
fclose(fp_recu);
printf("reception symétric key OK!\n\n");
//////////////////////////////////////////////////////////////////
/*DECHIFFREMENT DU-DIT FICHIER*/
/*lecture de la clé privée*/
key = readpemkeys(READSEC);
/*ouverture du fichier input*/
if(!(fpin = fopen(FICHIER_CLE_SYMETRIQUE_ENC, "rb")))
{
fprintf(stderr, "Error: Cannot locate input file.\n");
exit(EXIT_FAILURE);
}
/*ouverture du fichier output*/
fpout = fopen(FICHIER_CLE_SYMETRIQUE, "wb");
/*taille de la clé rsa*/
ks = RSA_size(key);
/*allocation de la mémoire*/
cipher = (unsigned char*)malloc(ks * sizeof(unsigned char));
plain = (unsigned char*)malloc(ks * sizeof(unsigned char));
printf("Decrypting '%s' file.\n", FICHIER_CLE_SYMETRIQUE_ENC);
/*tant qu'on n'a pas atteint la fin du fichier*/
while(!feof(fpin))
{
/*mise à zéro de cipher et plain*/
memset(cipher, '\0', ks);
memset(plain, '\0', ks);
/*lecture de l'info dans le fichier input*/
if ((len = fread(cipher, 1, ks, fpin)) == 0)
break;
/*déchiffrement*/
size = rsa_decrypt(key, cipher, len, &plain);
/*écriture dans le fichier out put*/
fwrite(plain, 1, size, fpout);
}
/*fermeture des fichiers*/
fclose(fpout);
fclose(fpin);
/*libération de la mémoire*/
free(plain);
free(cipher);
RSA_free(key);
printf("Decypting symetric file Done.\n\n");
/*ouverture du fichier*/
fp=fopen(FICHIER_CLE_SYMETRIQUE,"r");
if (fp==NULL)
{
perror("call to open\n");
exit(1);
}
fread(key2,sizeof(*key2),16,fp);
fread (iv,sizeof(*iv),8,fp);
int i=0;
for (i = 0; i < 16; i++)
printf ("%d \t", key2[i]);
printf("\n\n");
for (i = 0; i < 8; i++)
printf ("%d \t", iv[i]);
fclose(fp);
printf("\n\n");
//////////////////////////////////////////////////////////////////////
printf("Reception...\n");
/*ouverture du fichier*/
fp_recu=fopen(FICHIER_ENC,"wb");
if (fp_recu==NULL)
{
perror("call to open\n");
exit(1);
}
/*reception de la taille du fichier*/
recp_taille=SSL_read(ssl,&taille_fichier,sizeof(taille_fichier));
if (recp_taille==-1)
{
perror("call to rcv taille\n");
exit(1);
}
printf("taille fichier %d reçue\n",taille_fichier);
printf("Reception file...\n");
/*tant que l'on a pas tout reçu*/
while (taille_fichier > 0 && (recp_fich = SSL_read(ssl, buf, sizeof (buf))) > 0)
{
/*on recoi*/
if(recp_fich <0)
{
perror("call to rcv\n");
exit(1);
}
/*on écrit dans le fichier*/
fwrite(buf,1,recp_fich,fp_recu);
/*on diminue la taille*/
taille_fichier -= recp_fich;
bzero(&buf,sizeof(buf));
}
/*fermeture du fichier*/
fclose(fp_recu);
printf("Reception file ok\n\n");
//////////////////////////////////////////////////////////////////////////
/*déchiffrement*/
/* printf("Decrypting File...\n");
if ((outfp=fopen(FICHIER_ENC, "rb"))==NULL)
perror("open output file error");
if ((decfp=fopen(FICHIER,"wb"))==NULL)
perror("open dec file error");
decrypt(outfp, decfp);
fclose(outfp);
fclose(decfp);
printf("The file is decrypt\n\n");*/
if ((outfd = open (FICHIER_ENC, flags1, mode )) == -1)
perror ("open output file error");
if ((decfd = open (FICHIER, flags2, mode)) == -1)
perror ("open output file error");
decrypt (outfd, decfd);
close (outfd);
fsync (decfd);
close (decfd);
printf("The file is decrypt\n\n");
//////////////////////////////////////////////////////////////////////////
printf("\nEND DATA EXCHANGE\n");
printf("************************************\n");
////////////////////////////////////////////////////////////////
/*--------------- SSL closure ---------------*/
/* Shutdown this side (server) of the connection. */
err = SSL_shutdown(ssl);
RETURN_SSL(err);
/* Terminate communication on a socket */
err = close(sock);
RETURN_ERR(err, "close");
/* Free the SSL structure */
SSL_free(ssl);
/* Free the SSL_CTX structure */
SSL_CTX_free(ctx);
printf("Application terminate : good bye!\n");
/*END*/
} |
Partager