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
| // Etablissement de la connexion
int opening_connexion()
{
// Initialisations SSL
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
// Création du contexte SSL
SSL *ssl = NULL;
SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
if (ctx == NULL) {
fprintf(stderr, "\033[31m\nError in opening_connexion() : SSL_CTX_new()"
"\nError: %s\033[0m", ERR_reason_error_string(ERR_get_error()));
return EXIT_FAILURE;
}
// Chargement du certificat
if(! SSL_CTX_load_verify_locations(ctx, CERTIFICAT, NULL)) {
fprintf(stderr, "\033[31m\nError in opening_connexion() : SSL_CTX_load_verify()"
"\nUnable to load this certificate : %s", CERTIFICAT);
fprintf(stderr, "\nError : %s\033[0m", ERR_reason_error_string(ERR_get_error()));
SSL_CTX_free(ctx);
return EXIT_FAILURE;
}
// Initialisation de la connexion
BIO *bio = NULL;
bio = BIO_new_ssl_connect(ctx);
if (bio == NULL) {
fprintf(stderr, "\033[31m\nError in opening_connexion() : BIO_new_ssl_connect()033[0m");
SSL_CTX_free(ctx);
return(EXIT_FAILURE);
}
// Configuration
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
// Connexion
BIO_set_conn_hostname(bio, SERVER);
fprintf(stdout, "\033[33m\nOpening connexion : \033[0m");
fflush(stdout);
if (BIO_do_connect(bio) <= 0) {
fprintf(stderr, "\033[31mFAIL\nError in Openning_connexion() : BIO_new_connect()\nRetrying later..\033[0m\n");
SSL_CTX_free(ctx);
return EXIT_REPORT;
}
fprintf(stdout, "\033[33mOK\033[0m");
fflush(stdout);
// Vérification de la validité du certificat
if(SSL_get_verify_result(ssl) != X509_V_OK)
{
fprintf(stderr, "\033[31m\nError in openning_connexion() : SSL_get_verify_result()\nInvalid certificate\nTant pis pour le moment\033[0m");
//BIO_free_all(bio);
//SSL_CTX_free(ctx);
//return EXIT_FAILURE;
} |
Partager