test existence of cert.pem (problem seems common, Paul and me already
[claws.git] / src / common / 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_OPENSSL
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 "log.h"
34 #include "ssl_certificate.h"
35
36 static SSL_CTX *ssl_ctx;
37
38 void ssl_init(void)
39 {
40         SSL_METHOD *meth;
41         FILE *cert_test;
42
43         /* Global system initialization*/
44         SSL_library_init();
45         SSL_load_error_strings();
46         
47         /* Create our context*/
48         meth = SSLv23_client_method();
49         ssl_ctx = SSL_CTX_new(meth);
50
51         /* Set default certificate paths */
52         SSL_CTX_set_default_verify_paths(ssl_ctx);
53         
54         /* this problem seems quite common */
55         cert_test = fopen (X509_get_default_cert_file(), "r");
56         if (cert_test != NULL)
57                 fclose(cert_test);
58         else {
59                 printf("ssl_init: warning, can't open %s\n", X509_get_default_cert_file());
60                 printf("ssl_init: it means that certificates' signatures won't appear as Correct,\n");
61                 printf("ssl_init: even if they should. Check your openssl install.\n");
62         }
63 #if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
64         SSL_CTX_set_verify_depth(ctx,1);
65 #endif
66 }
67
68 void ssl_done(void)
69 {
70         if (!ssl_ctx)
71                 return;
72         
73         SSL_CTX_free(ssl_ctx);
74 }
75
76 gboolean ssl_init_socket(SockInfo *sockinfo)
77 {
78         return ssl_init_socket_with_method(sockinfo, SSL_METHOD_SSLv23);
79 }
80
81 gboolean ssl_init_socket_with_method(SockInfo *sockinfo, SSLMethod method)
82 {
83         X509 *server_cert;
84         SSL *ssl;
85
86         ssl = SSL_new(ssl_ctx);
87         if (ssl == NULL) {
88                 log_warning(_("Error creating ssl context\n"));
89                 return FALSE;
90         }
91
92         switch (method) {
93         case SSL_METHOD_SSLv23:
94                 debug_print("Setting SSLv23 client method\n");
95                 SSL_set_ssl_method(ssl, SSLv23_client_method());
96                 break;
97         case SSL_METHOD_TLSv1:
98                 debug_print("Setting TLSv1 client method\n");
99                 SSL_set_ssl_method(ssl, TLSv1_client_method());
100                 break;
101         default:
102                 break;
103         }
104
105         SSL_set_fd(ssl, sockinfo->sock);
106         if (SSL_connect(ssl) == -1) {
107                 log_warning(_("SSL connect failed (%s)\n"),
108                             ERR_error_string(ERR_get_error(), NULL));
109                 SSL_free(ssl);
110                 return FALSE;
111         }
112
113         /* Get the cipher */
114         log_print(_("SSL connection using %s\n"), SSL_get_cipher(ssl));
115
116         /* Get server's certificate (note: beware of dynamic allocation) */
117         if ((server_cert = SSL_get_peer_certificate(ssl)) == NULL) {
118                 debug_print("server_cert is NULL ! this _should_not_ happen !\n");
119                 SSL_free(ssl);
120                 return FALSE;
121         }
122
123         if (!ssl_certificate_check(server_cert, sockinfo->hostname, sockinfo->port)) {
124                 X509_free(server_cert);
125                 SSL_free(ssl);
126                 return FALSE;
127         }
128
129         X509_free(server_cert);
130         sockinfo->ssl = ssl;
131
132         return TRUE;
133 }
134
135 void ssl_done_socket(SockInfo *sockinfo)
136 {
137         if (sockinfo->ssl) {
138                 SSL_free(sockinfo->ssl);
139         }
140 }
141
142 #endif /* USE_OPENSSL */