improve the English in the GUI
[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;
36
37 void ssl_init(void)
38 {
39         SSL_METHOD *meth;
40
41         /* Global system initialization*/
42         SSL_library_init();
43         SSL_load_error_strings();
44         
45         /* Create our context*/
46         meth = SSLv23_client_method();
47         ssl_ctx = SSL_CTX_new(meth);
48
49         /* Set default certificate paths */
50         SSL_CTX_set_default_verify_paths(ssl_ctx);
51 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
52         SSL_CTX_set_verify_depth(ctx,1);
53 #endif
54 }
55
56 void ssl_done(void)
57 {
58         if (!ssl_ctx)
59                 return;
60         
61         SSL_CTX_free(ssl_ctx);
62 }
63
64 gboolean ssl_init_socket(SockInfo *sockinfo)
65 {
66         return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
67 }
68
69 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
70 {
71         X509 *server_cert;
72         SSL *ssl;
73
74         ssl = SSL_new(ssl_ctx);
75         if (ssl == NULL) {
76                 log_warning(_("Error creating ssl context\n"));
77                 return FALSE;
78         }
79
80         switch (method) {
81         case SSL_METHOD_SSLv23:
82                 debug_print("Setting SSLv23 client method\n");
83                 SSL_set_ssl_method(ssl, SSLv23_client_method());
84                 break;
85         case SSL_METHOD_TLSv1:
86                 debug_print("Setting TLSv1 client method\n");
87                 SSL_set_ssl_method(ssl, TLSv1_client_method());
88                 break;
89         default:
90                 break;
91         }
92
93         SSL_set_fd(ssl, sockinfo->sock);
94         if (SSL_connect(ssl) == -1) {
95                 log_warning(_("SSL connect failed (%s)\n"),
96                             ERR_error_string(ERR_get_error(), NULL));
97                 SSL_free(ssl);
98                 return FALSE;
99         }
100
101         /* Get the cipher */
102         log_print(_("SSL connection using %s\n"), SSL_get_cipher(ssl));
103
104         /* Get server's certificate (note: beware of dynamic allocation) */
105         if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
106                 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
107                 SSL_free(ssl);
108                 return FALSE;
109         }
110
111         if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
112                 X509_free(server_cert);
113                 SSL_free(ssl);
114                 return FALSE;
115         }
116         
117         X509_free(server_cert);
118         sockinfo->ssl = ssl;
119
120         return TRUE;
121 }
122
123 void ssl_done_socket(SockInfo *sockinfo)
124 {
125         if (sockinfo->ssl) {
126                 SSL_free(sockinfo->ssl);
127         }
128 }
129
130 #endif /* USE_SSL */