Commit f42e13da4663862a226854a91bc17281547f33db

Authored by Perry Werneck
1 parent c067d990

Refactoring windows CA certificates loader.

.gitignore
... ... @@ -55,4 +55,4 @@ doxygen/html
55 55 doxygen/doxyfile
56 56 win32-configure*
57 57 ValgrindOut.xml
58   -
  58 +certs
... ...
lib3270.cbp
... ... @@ -293,15 +293,15 @@
293 293 <Unit filename="src/selection/selection.c">
294 294 <Option compilerVar="CC" />
295 295 </Unit>
296   - <Unit filename="src/ssl/ctx_init.c">
297   - <Option compilerVar="CC" />
298   - </Unit>
299 296 <Unit filename="src/ssl/linux/curl.c">
300 297 <Option compilerVar="CC" />
301 298 </Unit>
302 299 <Unit filename="src/ssl/linux/getcrl.c">
303 300 <Option compilerVar="CC" />
304 301 </Unit>
  302 + <Unit filename="src/ssl/linux/init.c">
  303 + <Option compilerVar="CC" />
  304 + </Unit>
305 305 <Unit filename="src/ssl/linux/ldap.c">
306 306 <Option compilerVar="CC" />
307 307 </Unit>
... ... @@ -321,6 +321,9 @@
321 321 <Unit filename="src/ssl/windows/getcrl.c">
322 322 <Option compilerVar="CC" />
323 323 </Unit>
  324 + <Unit filename="src/ssl/windows/init.c">
  325 + <Option compilerVar="CC" />
  326 + </Unit>
324 327 <Unit filename="src/ssl/windows/ldap.c">
325 328 <Option compilerVar="CC" />
326 329 </Unit>
... ...
src/ssl/ctx_init.c
... ... @@ -1,182 +0,0 @@
1   -/*
2   - * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
3   - * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
4   - * aplicativos mainframe. Registro no INPI sob o nome G3270.
5   - *
6   - * Copyright (C) <2008> <Banco do Brasil S.A.>
7   - *
8   - * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
9   - * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
10   - * Free Software Foundation.
11   - *
12   - * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
13   - * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
14   - * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
15   - * obter mais detalhes.
16   - *
17   - * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
18   - * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
19   - * St, Fifth Floor, Boston, MA 02110-1301 USA
20   - *
21   - * Este programa está nomeado como - e possui - linhas de código.
22   - *
23   - * Contatos:
24   - *
25   - * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
26   - * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
27   - *
28   - *
29   - * References:
30   - *
31   - * http://www.openssl.org/docs/ssl/
32   - * https://stackoverflow.com/questions/4389954/does-openssl-automatically-handle-crls-certificate-revocation-lists-now
33   - *
34   - */
35   -
36   -/**
37   - * @brief OpenSSL initialization for linux.
38   - *
39   - */
40   -
41   -#include <config.h>
42   -#if defined(HAVE_LIBSSL)
43   -
44   -#include <openssl/ssl.h>
45   -#include <openssl/err.h>
46   -#include <openssl/x509_vfy.h>
47   -
48   -#ifndef SSL_ST_OK
49   - #define SSL_ST_OK 3
50   -#endif // !SSL_ST_OK
51   -
52   -#include <lib3270-internals.h>
53   -#include <errno.h>
54   -#include <lib3270.h>
55   -#include <lib3270/internals.h>
56   -#include <lib3270/trace.h>
57   -#include <lib3270/log.h>
58   -#include "trace_dsc.h"
59   -
60   -#ifdef SSL_ENABLE_CRL_CHECK
61   - #include <openssl/x509.h>
62   -#endif // SSL_ENABLE_CRL_CHECK
63   -
64   -/*--[ Implement ]------------------------------------------------------------------------------------*/
65   -
66   -/*
67   -#pragma GCC diagnostic push
68   -#pragma GCC diagnostic ignored "-Wsequence-point"
69   -
70   -// https://stackoverflow.com/questions/10975542/asn1-time-to-time-t-conversion
71   -static time_t ASN1_GetTimeT(const ASN1_TIME* time)
72   -{
73   - struct tm t;
74   - const char* str = (const char*) time->data;
75   - size_t i = 0;
76   -
77   - memset(&t, 0, sizeof(t));
78   -
79   - if (time->type == V_ASN1_UTCTIME) // two digit year
80   - {
81   - t.tm_year = (str[i++] - '0') * 10 + (str[++i] - '0');
82   - if (t.tm_year < 70)
83   - t.tm_year += 100;
84   - }
85   - else if (time->type == V_ASN1_GENERALIZEDTIME) // four digit year
86   - {
87   - t.tm_year = (str[i++] - '0') * 1000 + (str[++i] - '0') * 100 + (str[++i] - '0') * 10 + (str[++i] - '0');
88   - t.tm_year -= 1900;
89   - }
90   - t.tm_mon = ((str[i++] - '0') * 10 + (str[++i] - '0')) - 1; // -1 since January is 0 not 1.
91   - t.tm_mday = (str[i++] - '0') * 10 + (str[++i] - '0');
92   - t.tm_hour = (str[i++] - '0') * 10 + (str[++i] - '0');
93   - t.tm_min = (str[i++] - '0') * 10 + (str[++i] - '0');
94   - t.tm_sec = (str[i++] - '0') * 10 + (str[++i] - '0');
95   -
96   - // Note: we did not adjust the time based on time zone information
97   - return mktime(&t);
98   -}
99   -#pragma GCC diagnostic pop
100   -*/
101   -
102   -/**
103   - * @brief Initialize openssl library.
104   - *
105   - * @return 0 if ok, non zero if fails.
106   - *
107   - */
108   -int ssl_ctx_init(H3270 *hSession, SSL_ERROR_MESSAGE * message)
109   -{
110   - debug("%s ssl_ctx=%p",__FUNCTION__,ssl_ctx);
111   -
112   - if(ssl_ctx)
113   - return 0;
114   -
115   - trace_ssl(hSession,"Initializing SSL context.\n");
116   -
117   - SSL_load_error_strings();
118   - SSL_library_init();
119   -
120   - ssl_ctx = SSL_CTX_new(SSLv23_method());
121   - if(ssl_ctx == NULL)
122   - {
123   - message->error = hSession->ssl.error = ERR_get_error();
124   - message->title = N_( "Security error" );
125   - message->text = N_( "Cant initialize the SSL context." );
126   - return -1;
127   - }
128   -
129   - SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL);
130   - SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
131   -
132   - SSL_CTX_set_default_verify_paths(ssl_ctx);
133   -
134   -#ifdef _WIN32
135   - {
136   - lib3270_autoptr(char) appdir = lib3270_get_installation_path();
137   - lib3270_autoptr(char) certpath = lib3270_strdup_printf("%s\\certs",appdir);
138   -
139   - debug("Searching certs from \"%s\".", certpath);
140   -
141   - if(SSL_CTX_load_verify_locations(ssl_ctx,NULL,certpath))
142   - {
143   - trace_ssl(hSession,"Searching certs from \"%s\".\n", certpath);
144   - }
145   - else
146   - {
147   - int ssl_error = ERR_get_error();
148   -
149   - lib3270_autoptr(char) message = lib3270_strdup_printf( _( "Can't read SSL certificates from \"%s\"" ), certpath);
150   -
151   - lib3270_popup_dialog(
152   - hSession,
153   - LIB3270_NOTIFY_ERROR,
154   - N_( "Security error" ),
155   - message,
156   - "%s", ERR_lib_error_string(ssl_error)
157   - );
158   -
159   - }
160   -
161   - }
162   -#endif // _WIN32
163   -
164   - ssl_3270_ex_index = SSL_get_ex_new_index(0,NULL,NULL,NULL,NULL);
165   -
166   -#ifdef SSL_ENABLE_CRL_CHECK
167   -
168   - // Enable CRL check
169   - X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
170   - X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
171   - X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
172   - X509_STORE_set1_param(store, param);
173   - X509_VERIFY_PARAM_free(param);
174   - trace_ssl(hSession,"CRL CHECK was enabled\n");
175   -
176   -#endif // SSL_ENABLE_CRL_CHECK
177   -
178   - return 0;
179   -
180   -}
181   -
182   -#endif // HAVE_LIBSSL
src/ssl/linux/init.c 0 → 100644
... ... @@ -0,0 +1,117 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + *
  29 + * References:
  30 + *
  31 + * http://www.openssl.org/docs/ssl/
  32 + * https://stackoverflow.com/questions/4389954/does-openssl-automatically-handle-crls-certificate-revocation-lists-now
  33 + *
  34 + */
  35 +
  36 +/**
  37 + * @brief OpenSSL initialization for linux.
  38 + *
  39 + */
  40 +
  41 +#include <config.h>
  42 +
  43 +#if defined(HAVE_LIBSSL)
  44 +
  45 +#include <openssl/ssl.h>
  46 +#include <openssl/err.h>
  47 +#include <openssl/x509_vfy.h>
  48 +
  49 +#ifndef SSL_ST_OK
  50 + #define SSL_ST_OK 3
  51 +#endif // !SSL_ST_OK
  52 +
  53 +#include <lib3270-internals.h>
  54 +#include <errno.h>
  55 +#include <lib3270.h>
  56 +#include <lib3270/internals.h>
  57 +#include <lib3270/trace.h>
  58 +#include <lib3270/log.h>
  59 +#include "trace_dsc.h"
  60 +
  61 +#ifdef SSL_ENABLE_CRL_CHECK
  62 + #include <openssl/x509.h>
  63 +#endif // SSL_ENABLE_CRL_CHECK
  64 +
  65 +/*--[ Implement ]------------------------------------------------------------------------------------*/
  66 +
  67 +/**
  68 + * @brief Initialize openssl library.
  69 + *
  70 + * @return 0 if ok, non zero if fails.
  71 + *
  72 + */
  73 +int ssl_ctx_init(H3270 *hSession, SSL_ERROR_MESSAGE * message)
  74 +{
  75 + debug("%s ssl_ctx=%p",__FUNCTION__,ssl_ctx);
  76 +
  77 + if(ssl_ctx)
  78 + return 0;
  79 +
  80 + trace_ssl(hSession,"Initializing SSL context.\n");
  81 +
  82 + SSL_load_error_strings();
  83 + SSL_library_init();
  84 +
  85 + ssl_ctx = SSL_CTX_new(SSLv23_method());
  86 + if(ssl_ctx == NULL)
  87 + {
  88 + message->error = hSession->ssl.error = ERR_get_error();
  89 + message->title = N_( "Security error" );
  90 + message->text = N_( "Cant initialize the SSL context." );
  91 + return -1;
  92 + }
  93 +
  94 + SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL);
  95 + SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
  96 +
  97 + SSL_CTX_set_default_verify_paths(ssl_ctx);
  98 +
  99 + ssl_3270_ex_index = SSL_get_ex_new_index(0,NULL,NULL,NULL,NULL);
  100 +
  101 +#ifdef SSL_ENABLE_CRL_CHECK
  102 +
  103 + // Enable CRL check
  104 + X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx);
  105 + X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
  106 + X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
  107 + X509_STORE_set1_param(store, param);
  108 + X509_VERIFY_PARAM_free(param);
  109 + trace_ssl(hSession,"CRL CHECK was enabled\n");
  110 +
  111 +#endif // SSL_ENABLE_CRL_CHECK
  112 +
  113 + return 0;
  114 +
  115 +}
  116 +
  117 +#endif // HAVE_LIBSSL
... ...
src/ssl/windows/init.c 0 → 100644
... ... @@ -0,0 +1,213 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + *
  29 + * References:
  30 + *
  31 + * http://www.openssl.org/docs/ssl/
  32 + * https://stackoverflow.com/questions/4389954/does-openssl-automatically-handle-crls-certificate-revocation-lists-now
  33 + *
  34 + */
  35 +
  36 +/**
  37 + * @brief OpenSSL initialization for linux.
  38 + *
  39 + */
  40 +
  41 +#include <config.h>
  42 +
  43 +#if defined(HAVE_LIBSSL)
  44 +
  45 +#include <openssl/ssl.h>
  46 +#include <openssl/err.h>
  47 +#include <openssl/x509_vfy.h>
  48 +
  49 +#ifndef SSL_ST_OK
  50 + #define SSL_ST_OK 3
  51 +#endif // !SSL_ST_OK
  52 +
  53 +#include <lib3270-internals.h>
  54 +#include <errno.h>
  55 +#include <lib3270.h>
  56 +#include <lib3270/internals.h>
  57 +#include <lib3270/trace.h>
  58 +#include <lib3270/log.h>
  59 +#include <dirent.h>
  60 +
  61 +#include "trace_dsc.h"
  62 +
  63 +#include <openssl/x509.h>
  64 +
  65 +/*--[ Implement ]------------------------------------------------------------------------------------*/
  66 +
  67 +/**
  68 + * @brief Initialize openssl library.
  69 + *
  70 + * @return 0 if ok, non zero if fails.
  71 + *
  72 + */
  73 +int ssl_ctx_init(H3270 *hSession, SSL_ERROR_MESSAGE * message)
  74 +{
  75 + debug("%s ssl_ctx=%p",__FUNCTION__,ssl_ctx);
  76 +
  77 + if(ssl_ctx)
  78 + return 0;
  79 +
  80 + trace_ssl(hSession,"Initializing SSL context.\n");
  81 +
  82 + SSL_load_error_strings();
  83 + SSL_library_init();
  84 +
  85 + ssl_ctx = SSL_CTX_new(SSLv23_method());
  86 + if(ssl_ctx == NULL)
  87 + {
  88 + message->error = hSession->ssl.error = ERR_get_error();
  89 + message->title = N_( "Security error" );
  90 + message->text = N_( "Cant initialize the SSL context." );
  91 + return -1;
  92 + }
  93 +
  94 + SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL);
  95 + SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
  96 +
  97 + // SSL_CTX_set_default_verify_paths(ssl_ctx);
  98 +
  99 + // Load certs
  100 + // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store
  101 + X509_STORE * store = SSL_CTX_get_cert_store(ssl_ctx);
  102 +
  103 + lib3270_autoptr(char) certpath = lib3270_build_data_filename("certs","*.der",NULL);
  104 +
  105 + WIN32_FIND_DATA ffd;
  106 + HANDLE hFind = FindFirstFile(certpath, &ffd);
  107 +
  108 + if(hFind == INVALID_HANDLE_VALUE)
  109 + {
  110 + lib3270_autoptr(char) message = lib3270_strdup_printf( _( "Can't read SSL certificates from \"%s\"" ), certpath);
  111 +
  112 + lib3270_popup_dialog(
  113 + hSession,
  114 + LIB3270_NOTIFY_ERROR,
  115 + N_( "Security error" ),
  116 + message,
  117 + _("The windows error code was %d"), (int) GetLastError()
  118 + );
  119 +
  120 + }
  121 + else
  122 + {
  123 + do
  124 + {
  125 + char * filename = lib3270_build_data_filename("certs",ffd.cFileName,NULL);
  126 +
  127 + debug("Loading \"%s\"",filename);
  128 +
  129 + FILE *fp = fopen(filename,"r");
  130 + if(!fp) {
  131 +
  132 + lib3270_autoptr(char) message = lib3270_strdup_printf( _( "Can't open \"%s\"" ), filename);
  133 +
  134 + lib3270_popup_dialog(
  135 + hSession,
  136 + LIB3270_NOTIFY_ERROR,
  137 + N_( "Security error" ),
  138 + message,
  139 + "%s", strerror(errno)
  140 + );
  141 +
  142 + }
  143 + else
  144 + {
  145 + X509 * cert = d2i_X509_fp(fp, NULL);
  146 +
  147 + if(!cert)
  148 + {
  149 + int ssl_error = ERR_get_error();
  150 +
  151 + lib3270_autoptr(char) message = lib3270_strdup_printf( _( "Can't read \"%s\"" ), filename);
  152 +
  153 + lib3270_popup_dialog(
  154 + hSession,
  155 + LIB3270_NOTIFY_ERROR,
  156 + N_( "Security error" ),
  157 + message,
  158 + "%s", ERR_lib_error_string(ssl_error)
  159 + );
  160 +
  161 + }
  162 + else
  163 + {
  164 + trace_ssl(hSession,"Loading %s\n",filename);
  165 +
  166 + if(X509_STORE_add_cert(store, cert) != 1)
  167 + {
  168 + int ssl_error = ERR_get_error();
  169 +
  170 + lib3270_autoptr(char) message = lib3270_strdup_printf( _( "Can't load \"%s\"" ), filename);
  171 +
  172 + lib3270_popup_dialog(
  173 + hSession,
  174 + LIB3270_NOTIFY_ERROR,
  175 + N_( "Security error" ),
  176 + message,
  177 + "%s", ERR_lib_error_string(ssl_error)
  178 + );
  179 + }
  180 +
  181 + X509_free(cert);
  182 + }
  183 +
  184 + fclose(fp);
  185 + }
  186 +
  187 + lib3270_free(filename);
  188 +
  189 + }
  190 + while (FindNextFile(hFind, &ffd) != 0);
  191 +
  192 + }
  193 +
  194 + // lib3270_build_lib3270_strdup_printf("%s\\certs",appdir);
  195 +
  196 + ssl_3270_ex_index = SSL_get_ex_new_index(0,NULL,NULL,NULL,NULL);
  197 +
  198 +#ifdef SSL_ENABLE_CRL_CHECK
  199 +
  200 + // Enable CRL check
  201 + X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
  202 + X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
  203 + X509_STORE_set1_param(store, param);
  204 + X509_VERIFY_PARAM_free(param);
  205 + trace_ssl(hSession,"CRL CHECK was enabled\n");
  206 +
  207 +#endif // SSL_ENABLE_CRL_CHECK
  208 +
  209 + return 0;
  210 +
  211 +}
  212 +
  213 +#endif // HAVE_LIBSSL
... ...
src/testprogram/testprogram.c
... ... @@ -90,7 +90,6 @@ int main(int argc, char *argv[])
90 90 }
91 91  
92 92  
93   - /*
94 93 if(lib3270_set_url(h,NULL))
95 94 lib3270_set_url(h,"tn3270://fandezhi.efglobe.com");
96 95  
... ... @@ -117,8 +116,6 @@ int main(int argc, char *argv[])
117 116  
118 117 }
119 118  
120   - */
121   -
122 119 lib3270_session_free(h);
123 120  
124 121 return 0;
... ...