0.8.5claws172
[claws.git] / src / ssl.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #if USE_SSL
25
26 #include "defs.h"
27
28 #include <glib.h>
29
30 #include "intl.h"
31 #include "utils.h"
32 #include "ssl.h"
33 #include "ssl_certificate.h"
34
35 static SSL_CTX *ssl_ctx_SSLv23;
36 static SSL_CTX *ssl_ctx_TLSv1;
37
38 void ssl_init(void)
39 {
40         SSL_library_init();
41         SSL_load_error_strings();
42
43         ssl_ctx_SSLv23 = SSL_CTX_new(SSLv23_client_method());
44         if (ssl_ctx_SSLv23 == NULL) {
45                 debug_print("SSLv23 not available\n");
46         } else {
47                 debug_print("SSLv23 available\n");
48         }
49
50         ssl_ctx_TLSv1 = SSL_CTX_new(TLSv1_client_method());
51         if (ssl_ctx_TLSv1 == NULL) {
52                 debug_print("TLSv1 not available\n");
53         } else {
54                 debug_print("TLSv1 available\n");
55         }
56 }
57
58 void ssl_done(void)
59 {
60         if (ssl_ctx_SSLv23) {
61                 SSL_CTX_free(ssl_ctx_SSLv23);
62         }
63
64         if (ssl_ctx_TLSv1) {
65                 SSL_CTX_free(ssl_ctx_TLSv1);
66         }
67 }
68
69 gboolean ssl_init_socket(SockInfo *sockinfo)
70 {
71         return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
72 }
73
74 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
75 {
76         X509 *server_cert;
77         gboolean ret;
78         unsigned char md[EVP_MAX_MD_SIZE];
79         char *issuer;
80         char *subject;
81
82         switch (method) {
83         case SSL_METHOD_SSLv23:
84                 if (!ssl_ctx_SSLv23) {
85                         log_warning(_("SSL method not available\n"));
86                         return FALSE;
87                 }
88                 sockinfo->ssl = SSL_new(ssl_ctx_SSLv23);
89                 break;
90         case SSL_METHOD_TLSv1:
91                 if (!ssl_ctx_TLSv1) {
92                         log_warning(_("SSL method not available\n"));
93                         return FALSE;
94                 }
95                 sockinfo->ssl = SSL_new(ssl_ctx_TLSv1);
96                 break;
97         default:
98                 log_warning(_("Unknown SSL method *PROGRAM BUG*\n"));
99                 return FALSE;
100                 break;
101         }
102
103         if (sockinfo->ssl == NULL) {
104                 log_warning(_("Error creating ssl context\n"));
105                 return FALSE;
106         }
107
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));
112                 return FALSE;
113         }
114
115         /* Get the cipher */
116
117         log_print(_("SSL connection using %s\n"), SSL_get_cipher(sockinfo->ssl));
118
119         /* Get server's certificate (note: beware of dynamic allocation) */
120
121         if ((server_cert = SSL_get_peer_certificate(sockinfo->ssl)) != NULL) {
122                 ret = ssl_certificate_check (server_cert, sockinfo->hostname, sockinfo->port);
123                 X509_free(server_cert);
124         } else {
125                 printf("server_cert is NULL ! this _should_not_ happen !\n");
126                 return FALSE;
127         }
128         return ret;
129 }
130
131 void ssl_done_socket(SockInfo *sockinfo)
132 {
133         if (sockinfo->ssl) {
134                 SSL_free(sockinfo->ssl);
135         }
136 }
137
138 #endif /* USE_SSL */