Shared folders support with a GUI.
[claws.git] / src / ssl.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 "intl.h"
27 #include "utils.h"
28 #include "ssl.h"
29
30 SSL_CTX *ssl_ctx;
31
32 void ssl_init() {
33     SSL_METHOD *meth;
34                 
35     SSL_library_init();
36     SSL_load_error_strings();
37     
38     ssl_ctx = SSL_CTX_new(SSLv23_client_method());
39     if(ssl_ctx == NULL) {
40         debug_print(_("SSL disabled\n"));
41     } else {
42         debug_print(_("SSL loaded\n"));
43     }
44 }
45
46 void ssl_done() {
47     if(!ssl_ctx)
48         return;
49         
50     SSL_CTX_free(ssl_ctx);
51 }
52
53 gboolean ssl_init_socket(SockInfo *sockinfo) {
54     X509 *server_cert;
55
56     if(ssl_ctx == NULL) {
57         log_warning(_("SSL not available\n"));
58
59         return FALSE;
60     }
61
62     sockinfo->ssl = SSL_new(ssl_ctx);
63     if(sockinfo->ssl == NULL) {
64         log_warning(_("Error creating ssl context\n"));
65
66         return FALSE;
67     }
68
69     SSL_set_fd(sockinfo->ssl, sockinfo->sock);
70     if(SSL_connect(sockinfo->ssl) == -1) {
71         log_warning(_("SSL connect failed\n"));
72
73         return FALSE;
74     }
75
76     /* Get the cipher */
77
78     log_print(_("SSL connection using %s\n"), SSL_get_cipher(sockinfo->ssl));
79
80     /* Get server's certificate (note: beware of dynamic allocation) */
81
82     if((server_cert = SSL_get_peer_certificate(sockinfo->ssl)) != NULL) {
83         char *str;
84         
85         log_print(_("Server certificate:\n"));
86
87         if((str = X509_NAME_oneline(X509_get_subject_name (server_cert),0,0)) != NULL) {
88                 log_print(_("  Subject: %s\n"), str);
89                 free(str);
90         }
91         
92         if((str = X509_NAME_oneline(X509_get_issuer_name  (server_cert),0,0)) != NULL) {
93                 log_print(_("  Issuer: %s\n"), str);
94                 free(str);
95         }
96
97         X509_free(server_cert);
98     }
99 }
100
101 void ssl_done_socket(SockInfo *sockinfo) {
102     if(sockinfo->ssl) {
103         SSL_free(sockinfo->ssl);
104     }
105 }
106
107 #endif /* USE_SSL */