X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fssl.c;h=d4cd91a232af29ed06ca76e9d9845fcf944b798f;hb=HEAD;hp=245ea8d8a9893efedad82a4d80e137f052fc7b24;hpb=a1a7d5dc60fb40d39c9f1cbc6b1879bc562299a7;p=dockapps%2Fwmnotify.git diff --git a/src/ssl.c b/src/ssl.c index 245ea8d..d4cd91a 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -1,99 +1,101 @@ -/* ssl.c */ - -/* Based on ssl_client.c (Sean Walton and Macmillan Publishers). */ - - -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#if HAVE_SSL - -/* Define filename_M */ -#define SSL_M 1 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "wmnotify.h" -#include "ssl.h" - - -/* InitCTX - initialize the SSL engine. */ -SSL_CTX * -InitCTX( void ) -{ - SSL_METHOD *method; - SSL_CTX *ctx; - - OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */ - SSL_load_error_strings(); /* Bring in and register error messages */ - method = SSLv2_client_method(); /* Create new client-method instance */ - ctx = SSL_CTX_new(method); /* Create new context */ - if( ctx == NULL ) { - ERR_print_errors_fp(stderr); - abort(); - } - return ctx; -} - - -/* ShowCerts - print out the certificates. */ -void -ShowCerts( SSL *ssl ) -{ - X509 *cert; - char *line; - - cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */ - if ( cert != NULL ) { - printf("Server certificates:\n"); - line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); - printf("Subject: %s\n", line); - free(line); /* free the malloc'ed string */ - line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); - printf("Issuer: %s\n", line); - free(line); /* free the malloc'ed string */ - X509_free(cert); /* free the malloc'ed certificate copy */ - } - else { - printf("No certificates.\n"); - } -} - - -int -InitSSL( int sock_fd ) -{ - ssl_infos.ctx = InitCTX(); - ssl_infos.ssl = SSL_new( ssl_infos.ctx ); /* create new SSL connection state */ - if( ssl_infos.ssl == NULL ) { - printf( "%s: Error in SSL_new()\n", PACKAGE ); - return EXIT_FAILURE; - } - - SSL_set_fd( ssl_infos.ssl, sock_fd ); /* attach the socket descriptor */ - if( SSL_connect( ssl_infos.ssl ) == FAIL ) { /* perform the connection */ - ERR_print_errors_fp(stderr); - return EXIT_FAILURE; - } - - if( wmnotify_infos.debug ) { - printf("Connected with %s encryption\n", SSL_get_cipher( ssl_infos.ssl )); - ShowCerts( ssl_infos.ssl ); /* get any certs */ - } - - return EXIT_SUCCESS; -} - - -#endif /* HAVE_SSL */ +/* + * ssl.c + * + * Copyright (C) 2003 Hugo Villeneuve + * Based on ssl_client.c (Sean Walton and Macmillan Publishers). + * + * This file is released under the GPLv2 + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif /* */ + +#if HAVE_SSL + +/* Define filename_M */ +#define SSL_M 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "wmnotify.h" +#include "ssl.h" + +/* InitCTX - initialize the SSL engine. */ +SSL_CTX *InitCTX(void) +{ + const SSL_METHOD *method; + SSL_CTX *ctx; + SSL_library_init(); /* Load cryptos, et.al. */ + SSL_load_error_strings(); /* Bring in and register error messages */ + method = SSLv23_client_method(); /* + * Indicate we support SSLv2, SSLv3 and + * TLSv1 methods. + */ + ctx = SSL_CTX_new(method); /* Create new context */ + if (ctx == NULL) { + ERR_print_errors_fp(stderr); + abort(); + } + return ctx; +} + + +/* ShowCerts - print out the certificates. */ +void ShowCerts(SSL *ssl) +{ + X509 *cert; + char *line; + cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */ + if (cert != NULL) { + printf("Server certificates:\n"); + line = + X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); + printf("Subject: %s\n", line); + free(line); /* free the malloc'ed string */ + line = + X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); + printf("Issuer: %s\n", line); + free(line); /* free the malloc'ed string */ + X509_free(cert); /* free the malloc'ed certificate copy */ + } else { + printf("No certificates.\n"); + } +} + +int InitSSL(int sock_fd) +{ + ssl_infos.ctx = InitCTX(); + ssl_infos.ssl = SSL_new(ssl_infos.ctx); /* + * create new SSL connection + * state + */ + if (ssl_infos.ssl == NULL) { + printf("%s: Error in SSL_new()\n", PACKAGE); + return EXIT_FAILURE; + } + SSL_set_fd(ssl_infos.ssl, sock_fd); /* attach the socket descriptor */ + if (SSL_connect(ssl_infos.ssl) == FAIL) { /* perform the connection */ + ERR_print_errors_fp(stderr); + return EXIT_FAILURE; + } + if (wmnotify_infos.debug) { + printf("Connected with %s encryption\n", + SSL_get_cipher(ssl_infos.ssl)); + ShowCerts(ssl_infos.ssl); /* get any certs */ + } + return EXIT_SUCCESS; +} + + +#endif /* HAVE_SSL */