init.c
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como - e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*
* References:
*
* http://www.openssl.org/docs/ssl/
* https://stackoverflow.com/questions/4389954/does-openssl-automatically-handle-crls-certificate-revocation-lists-now
*
*/
/**
* @brief OpenSSL initialization for linux.
*
*/
#include <winsock2.h>
#include <windows.h>
#include <config.h>
#if defined(HAVE_LIBSSL)
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509_vfy.h>
#ifndef SSL_ST_OK
#define SSL_ST_OK 3
#endif // !SSL_ST_OK
#include <internals.h>
#include <errno.h>
#include <lib3270.h>
#include <lib3270/internals.h>
#include <lib3270/trace.h>
#include <lib3270/log.h>
#include <dirent.h>
#include "trace_dsc.h"
#include <openssl/x509.h>
/*--[ Implement ]------------------------------------------------------------------------------------*/
/**
* @brief Initialize openssl library.
*
* @return 0 if ok, non zero if fails.
*
*/
int ssl_ctx_init(H3270 *hSession, SSL_ERROR_MESSAGE * message)
{
debug("%s ssl_ctx=%p",__FUNCTION__,ssl_ctx);
if(ssl_ctx)
return 0;
trace_ssl(hSession,"Initializing SSL context.\n");
SSL_load_error_strings();
SSL_library_init();
ssl_ctx = SSL_CTX_new(SSLv23_method());
if(ssl_ctx == NULL)
{
message->error = hSession->ssl.error = ERR_get_error();
message->title = N_( "Security error" );
message->text = N_( "Cant initialize the SSL context." );
return -1;
}
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL);
SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
// SSL_CTX_set_default_verify_paths(ssl_ctx);
// Load certs
// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store
X509_STORE * store = SSL_CTX_get_cert_store(ssl_ctx);
lib3270_autoptr(char) certpath = lib3270_build_data_filename("certs","*.der",NULL);
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(certpath, &ffd);
if(hFind == INVALID_HANDLE_VALUE)
{
message->title = N_( "Security error" );
message->text = N_( "Cant open custom certificate directory." );
trace_ssl(hSession, _( "Can't open \"%s\" (The Windows error code was %ld)" ), certpath, (long) GetLastError());
}
else
{
do
{
char * filename = lib3270_build_data_filename("certs", ffd.cFileName, NULL);
debug("Loading \"%s\"",filename);
FILE *fp = fopen(filename,"r");
if(!fp) {
trace_ssl(hSession, _( "Can't open \"%s\": %s" ), filename, strerror(errno));
}
else
{
X509 * cert = d2i_X509_fp(fp, NULL);
if(!cert)
{
message->error = hSession->ssl.error = ERR_get_error();
message->title = N_( "Security error" );
message->text = N_( "Cant read custom certificate file." );
trace_ssl(hSession, _( "Can't read \"%s\": %s" ), filename, ERR_lib_error_string(hSession->ssl.error));
}
else
{
if(X509_STORE_add_cert(store, cert) != 1)
{
message->error = hSession->ssl.error = ERR_get_error();
message->title = N_( "Security error" );
message->text = N_( "Cant load custom certificate file." );
trace_ssl(hSession, _( "Can't load \"%s\": %s" ), filename, ERR_lib_error_string(hSession->ssl.error));
}
X509_free(cert);
}
fclose(fp);
}
lib3270_free(filename);
}
while (FindNextFile(hFind, &ffd) != 0);
}
ssl_3270_ex_index = SSL_get_ex_new_index(0,NULL,NULL,NULL,NULL);
#ifdef SSL_ENABLE_CRL_CHECK
// Enable CRL check
X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
X509_STORE_set1_param(store, param);
X509_VERIFY_PARAM_free(param);
trace_ssl(hSession,"CRL CHECK was enabled\n");
#endif // SSL_ENABLE_CRL_CHECK
return 0;
}
#endif // HAVE_LIBSSL