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_SSLv23;
36 static SSL_CTX *ssl_ctx_TLSv1;
41 SSL_load_error_strings();
43 ssl_ctx_SSLv23 = SSL_CTX_new(SSLv23_client_method());
44 if (ssl_ctx_SSLv23 == NULL) {
45 debug_print("SSLv23 not available\n");
47 debug_print("SSLv23 available\n");
50 ssl_ctx_TLSv1 = SSL_CTX_new(TLSv1_client_method());
51 if (ssl_ctx_TLSv1 == NULL) {
52 debug_print("TLSv1 not available\n");
54 debug_print("TLSv1 available\n");
61 SSL_CTX_free(ssl_ctx_SSLv23);
65 SSL_CTX_free(ssl_ctx_TLSv1);
69 gboolean ssl_init_socket(SockInfo *sockinfo)
71 return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
74 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
78 unsigned char md[EVP_MAX_MD_SIZE];
83 case SSL_METHOD_SSLv23:
84 if (!ssl_ctx_SSLv23) {
85 log_warning(_("SSL method not available\n"));
88 sockinfo->ssl = SSL_new(ssl_ctx_SSLv23);
90 case SSL_METHOD_TLSv1:
92 log_warning(_("SSL method not available\n"));
95 sockinfo->ssl = SSL_new(ssl_ctx_TLSv1);
98 log_warning(_("Unknown SSL method *PROGRAM BUG*\n"));
103 if (sockinfo->ssl == NULL) {
104 log_warning(_("Error creating ssl context\n"));
108 SSL_set_fd(sockinfo->ssl, sockinfo->sock);
109 if ((ret = SSL_connect(sockinfo->ssl)) == -1) {
110 log_warning(_("SSL connect failed (%s)\n"),
111 ERR_error_string(ERR_get_error(), NULL));
117 log_print(_("SSL connection using %s\n"), SSL_get_cipher(sockinfo->ssl));
119 /* Get server's certificate (note: beware of dynamic allocation) */
121 if ((server_cert = SSL_get_peer_certificate(sockinfo->ssl)) != NULL) {
124 EVP_MD *digest = EVP_md5();
126 log_print(_("Server certificate:\n"));
128 if ((subject = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0)) != NULL) {
129 log_print(_(" Subject: %s\n"), subject);
132 if ((issuer = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0)) != NULL) {
133 log_print(_(" Issuer: %s\n"), issuer);
136 if (!X509_digest(server_cert, digest, md, &n))
137 printf("Can't get fingerprint !\n");
139 ret = ssl_certificate_check (server_cert, sockinfo->hostname, issuer, subject, md);
140 X509_free(server_cert);
150 void ssl_done_socket(SockInfo *sockinfo)
153 SSL_free(sockinfo->ssl);