2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include "ssl_certificate.h"
35 static SSL_CTX *ssl_ctx;
42 /* Global system initialization*/
44 SSL_load_error_strings();
46 /* Create our context*/
47 meth = SSLv23_client_method();
48 ssl_ctx = SSL_CTX_new(meth);
50 /* Set default certificate paths */
51 SSL_CTX_set_default_verify_paths(ssl_ctx);
53 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
54 SSL_CTX_set_verify_depth(ctx,1);
63 SSL_CTX_free(ssl_ctx);
66 gboolean ssl_init_socket(SockInfo *sockinfo)
68 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
71 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
76 ssl = SSL_new(ssl_ctx);
78 g_warning(_("Error creating ssl context\n"));
83 case SSL_METHOD_SSLv23:
84 debug_print("Setting SSLv23 client method\n");
85 SSL_set_ssl_method(ssl, SSLv23_client_method());
87 case SSL_METHOD_TLSv1:
88 debug_print("Setting TLSv1 client method\n");
89 SSL_set_ssl_method(ssl, TLSv1_client_method());
95 SSL_set_fd(ssl, sockinfo->sock);
96 if (SSL_connect(ssl) == -1) {
97 g_warning(_("SSL connect failed (%s)\n"),
98 ERR_error_string(ERR_get_error(), NULL));
105 debug_print(_("SSL connection using %s\n"),
106 SSL_get_cipher(ssl));
108 /* Get server's certificate (note: beware of dynamic allocation) */
109 if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
110 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
116 if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
117 X509_free(server_cert);
123 X509_free(server_cert);
129 void ssl_done_socket(SockInfo *sockinfo)
132 SSL_free(sockinfo->ssl);
136 #endif /* USE_OPENSSL */