2 * GnuTLS-based implementation of the schannel (SSL/TLS) provider.
4 * Copyright 2005 Juan Lang
5 * Copyright 2008 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
26 #ifdef SONAME_LIBGNUTLS
27 #include <gnutls/gnutls.h>
34 #include "secur32_priv.h"
35 #include "wine/debug.h"
36 #include "wine/library.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(secur32
);
40 #if defined(SONAME_LIBGNUTLS) && !defined(HAVE_SECURITY_SECURITY_H)
42 static void *libgnutls_handle
;
43 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
44 MAKE_FUNCPTR(gnutls_alert_get
);
45 MAKE_FUNCPTR(gnutls_alert_get_name
);
46 MAKE_FUNCPTR(gnutls_certificate_allocate_credentials
);
47 MAKE_FUNCPTR(gnutls_certificate_free_credentials
);
48 MAKE_FUNCPTR(gnutls_certificate_get_peers
);
49 MAKE_FUNCPTR(gnutls_cipher_get
);
50 MAKE_FUNCPTR(gnutls_cipher_get_key_size
);
51 MAKE_FUNCPTR(gnutls_credentials_set
);
52 MAKE_FUNCPTR(gnutls_deinit
);
53 MAKE_FUNCPTR(gnutls_global_deinit
);
54 MAKE_FUNCPTR(gnutls_global_init
);
55 MAKE_FUNCPTR(gnutls_global_set_log_function
);
56 MAKE_FUNCPTR(gnutls_global_set_log_level
);
57 MAKE_FUNCPTR(gnutls_handshake
);
58 MAKE_FUNCPTR(gnutls_init
);
59 MAKE_FUNCPTR(gnutls_kx_get
);
60 MAKE_FUNCPTR(gnutls_mac_get
);
61 MAKE_FUNCPTR(gnutls_mac_get_key_size
);
62 MAKE_FUNCPTR(gnutls_perror
);
63 MAKE_FUNCPTR(gnutls_protocol_get_version
);
64 MAKE_FUNCPTR(gnutls_set_default_priority
);
65 MAKE_FUNCPTR(gnutls_record_recv
);
66 MAKE_FUNCPTR(gnutls_record_send
);
67 MAKE_FUNCPTR(gnutls_transport_set_errno
);
68 MAKE_FUNCPTR(gnutls_transport_set_ptr
);
69 MAKE_FUNCPTR(gnutls_transport_set_pull_function
);
70 MAKE_FUNCPTR(gnutls_transport_set_push_function
);
75 static ssize_t
schan_pull_adapter(gnutls_transport_ptr_t transport
,
76 void *buff
, size_t buff_len
)
78 struct schan_transport
*t
= (struct schan_transport
*)transport
;
79 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
81 int ret
= schan_pull(transport
, buff
, &buff_len
);
84 pgnutls_transport_set_errno(s
, ret
);
91 static ssize_t
schan_push_adapter(gnutls_transport_ptr_t transport
,
92 const void *buff
, size_t buff_len
)
94 struct schan_transport
*t
= (struct schan_transport
*)transport
;
95 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
97 int ret
= schan_push(transport
, buff
, &buff_len
);
100 pgnutls_transport_set_errno(s
, ret
);
107 BOOL
schan_imp_create_session(schan_imp_session
*session
, BOOL is_server
,
108 schan_imp_certificate_credentials cred
)
110 gnutls_session_t
*s
= (gnutls_session_t
*)session
;
112 int err
= pgnutls_init(s
, is_server
? GNUTLS_SERVER
: GNUTLS_CLIENT
);
113 if (err
!= GNUTLS_E_SUCCESS
)
119 /* FIXME: We should be using the information from the credentials here. */
120 FIXME("Using hardcoded \"NORMAL\" priority\n");
121 err
= pgnutls_set_default_priority(*s
);
122 if (err
!= GNUTLS_E_SUCCESS
)
129 err
= pgnutls_credentials_set(*s
, GNUTLS_CRD_CERTIFICATE
,
130 (gnutls_certificate_credentials
)cred
);
131 if (err
!= GNUTLS_E_SUCCESS
)
138 pgnutls_transport_set_pull_function(*s
, schan_pull_adapter
);
139 pgnutls_transport_set_push_function(*s
, schan_push_adapter
);
144 void schan_imp_dispose_session(schan_imp_session session
)
146 gnutls_session_t s
= (gnutls_session_t
)session
;
150 void schan_imp_set_session_transport(schan_imp_session session
,
151 struct schan_transport
*t
)
153 gnutls_session_t s
= (gnutls_session_t
)session
;
154 pgnutls_transport_set_ptr(s
, (gnutls_transport_ptr_t
)t
);
157 SECURITY_STATUS
schan_imp_handshake(schan_imp_session session
)
159 gnutls_session_t s
= (gnutls_session_t
)session
;
160 int err
= pgnutls_handshake(s
);
163 case GNUTLS_E_SUCCESS
:
164 TRACE("Handshake completed\n");
168 TRACE("Continue...\n");
169 return SEC_I_CONTINUE_NEEDED
;
171 case GNUTLS_E_WARNING_ALERT_RECEIVED
:
172 case GNUTLS_E_FATAL_ALERT_RECEIVED
:
174 gnutls_alert_description_t alert
= pgnutls_alert_get(s
);
175 const char *alert_name
= pgnutls_alert_get_name(alert
);
176 WARN("ALERT: %d %s\n", alert
, alert_name
);
177 return SEC_E_INTERNAL_ERROR
;
182 return SEC_E_INTERNAL_ERROR
;
189 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher
)
193 gnutls_cipher_algorithm_t cipher
;
194 unsigned int block_size
;
198 {GNUTLS_CIPHER_3DES_CBC
, 8},
199 {GNUTLS_CIPHER_AES_128_CBC
, 16},
200 {GNUTLS_CIPHER_AES_256_CBC
, 16},
201 {GNUTLS_CIPHER_ARCFOUR_128
, 1},
202 {GNUTLS_CIPHER_ARCFOUR_40
, 1},
203 {GNUTLS_CIPHER_DES_CBC
, 8},
204 {GNUTLS_CIPHER_NULL
, 1},
205 {GNUTLS_CIPHER_RC2_40_CBC
, 8},
209 for (i
= 0; i
< sizeof(algorithms
) / sizeof(*algorithms
); ++i
)
211 if (algorithms
[i
].cipher
== cipher
)
212 return algorithms
[i
].block_size
;
215 FIXME("Unknown cipher %#x, returning 1\n", cipher
);
220 static DWORD
schannel_get_protocol(gnutls_protocol_t proto
)
222 /* FIXME: currently schannel only implements client connections, but
223 * there's no reason it couldn't be used for servers as well. The
224 * context doesn't tell us which it is, so assume client for now.
228 case GNUTLS_SSL3
: return SP_PROT_SSL3_CLIENT
;
229 case GNUTLS_TLS1_0
: return SP_PROT_TLS1_CLIENT
;
231 FIXME("unknown protocol %d\n", proto
);
236 static ALG_ID
schannel_get_cipher_algid(gnutls_cipher_algorithm_t cipher
)
240 case GNUTLS_CIPHER_UNKNOWN
:
241 case GNUTLS_CIPHER_NULL
: return 0;
242 case GNUTLS_CIPHER_ARCFOUR_40
:
243 case GNUTLS_CIPHER_ARCFOUR_128
: return CALG_RC4
;
244 case GNUTLS_CIPHER_DES_CBC
:
245 case GNUTLS_CIPHER_3DES_CBC
: return CALG_DES
;
246 case GNUTLS_CIPHER_AES_128_CBC
:
247 case GNUTLS_CIPHER_AES_256_CBC
: return CALG_AES
;
248 case GNUTLS_CIPHER_RC2_40_CBC
: return CALG_RC2
;
250 FIXME("unknown algorithm %d\n", cipher
);
255 static ALG_ID
schannel_get_mac_algid(gnutls_mac_algorithm_t mac
)
259 case GNUTLS_MAC_UNKNOWN
:
260 case GNUTLS_MAC_NULL
: return 0;
261 case GNUTLS_MAC_MD5
: return CALG_MD5
;
262 case GNUTLS_MAC_SHA1
:
263 case GNUTLS_MAC_SHA256
:
264 case GNUTLS_MAC_SHA384
:
265 case GNUTLS_MAC_SHA512
: return CALG_SHA
;
267 FIXME("unknown algorithm %d\n", mac
);
272 static ALG_ID
schannel_get_kx_algid(gnutls_kx_algorithm_t kx
)
276 case GNUTLS_KX_RSA
: return CALG_RSA_KEYX
;
277 case GNUTLS_KX_DHE_DSS
:
278 case GNUTLS_KX_DHE_RSA
: return CALG_DH_EPHEM
;
280 FIXME("unknown algorithm %d\n", kx
);
285 unsigned int schan_imp_get_session_cipher_block_size(schan_imp_session session
)
287 gnutls_session_t s
= (gnutls_session_t
)session
;
288 gnutls_cipher_algorithm_t cipher
= pgnutls_cipher_get(s
);
289 return schannel_get_cipher_block_size(cipher
);
292 SECURITY_STATUS
schan_imp_get_connection_info(schan_imp_session session
,
293 SecPkgContext_ConnectionInfo
*info
)
295 gnutls_session_t s
= (gnutls_session_t
)session
;
296 gnutls_protocol_t proto
= pgnutls_protocol_get_version(s
);
297 gnutls_cipher_algorithm_t alg
= pgnutls_cipher_get(s
);
298 gnutls_mac_algorithm_t mac
= pgnutls_mac_get(s
);
299 gnutls_kx_algorithm_t kx
= pgnutls_kx_get(s
);
301 info
->dwProtocol
= schannel_get_protocol(proto
);
302 info
->aiCipher
= schannel_get_cipher_algid(alg
);
303 info
->dwCipherStrength
= pgnutls_cipher_get_key_size(alg
);
304 info
->aiHash
= schannel_get_mac_algid(mac
);
305 info
->dwHashStrength
= pgnutls_mac_get_key_size(mac
);
306 info
->aiExch
= schannel_get_kx_algid(kx
);
307 /* FIXME: info->dwExchStrength? */
308 info
->dwExchStrength
= 0;
312 SECURITY_STATUS
schan_imp_get_session_peer_certificate(schan_imp_session session
,
313 PCCERT_CONTEXT
*cert
)
315 gnutls_session_t s
= (gnutls_session_t
)session
;
316 unsigned int list_size
;
317 const gnutls_datum_t
*datum
;
319 datum
= pgnutls_certificate_get_peers(s
, &list_size
);
322 *cert
= CertCreateCertificateContext(X509_ASN_ENCODING
, datum
->data
,
325 return GetLastError();
330 return SEC_E_INTERNAL_ERROR
;
333 SECURITY_STATUS
schan_imp_send(schan_imp_session session
, const void *buffer
,
336 gnutls_session_t s
= (gnutls_session_t
)session
;
337 ssize_t ret
= pgnutls_record_send(s
, buffer
, *length
);
340 else if (ret
== GNUTLS_E_AGAIN
)
341 return SEC_I_CONTINUE_NEEDED
;
345 return SEC_E_INTERNAL_ERROR
;
351 SECURITY_STATUS
schan_imp_recv(schan_imp_session session
, void *buffer
,
354 gnutls_session_t s
= (gnutls_session_t
)session
;
355 ssize_t ret
= pgnutls_record_recv(s
, buffer
, *length
);
358 else if (ret
== GNUTLS_E_AGAIN
)
359 return SEC_I_CONTINUE_NEEDED
;
363 return SEC_E_INTERNAL_ERROR
;
369 BOOL
schan_imp_allocate_certificate_credentials(schan_imp_certificate_credentials
*c
)
371 int ret
= pgnutls_certificate_allocate_credentials((gnutls_certificate_credentials
*)c
);
372 if (ret
!= GNUTLS_E_SUCCESS
)
374 return (ret
== GNUTLS_E_SUCCESS
);
377 void schan_imp_free_certificate_credentials(schan_imp_certificate_credentials c
)
379 pgnutls_certificate_free_credentials((gnutls_certificate_credentials
)c
);
382 static void schan_gnutls_log(int level
, const char *msg
)
384 TRACE("<%d> %s", level
, msg
);
387 BOOL
schan_imp_init(void)
391 libgnutls_handle
= wine_dlopen(SONAME_LIBGNUTLS
, RTLD_NOW
, NULL
, 0);
392 if (!libgnutls_handle
)
394 WARN("Failed to load libgnutls.\n");
398 #define LOAD_FUNCPTR(f) \
399 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
401 ERR("Failed to load %s\n", #f); \
405 LOAD_FUNCPTR(gnutls_alert_get
)
406 LOAD_FUNCPTR(gnutls_alert_get_name
)
407 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials
)
408 LOAD_FUNCPTR(gnutls_certificate_free_credentials
)
409 LOAD_FUNCPTR(gnutls_certificate_get_peers
)
410 LOAD_FUNCPTR(gnutls_cipher_get
)
411 LOAD_FUNCPTR(gnutls_cipher_get_key_size
)
412 LOAD_FUNCPTR(gnutls_credentials_set
)
413 LOAD_FUNCPTR(gnutls_deinit
)
414 LOAD_FUNCPTR(gnutls_global_deinit
)
415 LOAD_FUNCPTR(gnutls_global_init
)
416 LOAD_FUNCPTR(gnutls_global_set_log_function
)
417 LOAD_FUNCPTR(gnutls_global_set_log_level
)
418 LOAD_FUNCPTR(gnutls_handshake
)
419 LOAD_FUNCPTR(gnutls_init
)
420 LOAD_FUNCPTR(gnutls_kx_get
)
421 LOAD_FUNCPTR(gnutls_mac_get
)
422 LOAD_FUNCPTR(gnutls_mac_get_key_size
)
423 LOAD_FUNCPTR(gnutls_perror
)
424 LOAD_FUNCPTR(gnutls_protocol_get_version
)
425 LOAD_FUNCPTR(gnutls_set_default_priority
)
426 LOAD_FUNCPTR(gnutls_record_recv
);
427 LOAD_FUNCPTR(gnutls_record_send
);
428 LOAD_FUNCPTR(gnutls_transport_set_errno
)
429 LOAD_FUNCPTR(gnutls_transport_set_ptr
)
430 LOAD_FUNCPTR(gnutls_transport_set_pull_function
)
431 LOAD_FUNCPTR(gnutls_transport_set_push_function
)
434 ret
= pgnutls_global_init();
435 if (ret
!= GNUTLS_E_SUCCESS
)
441 if (TRACE_ON(secur32
))
443 pgnutls_global_set_log_level(4);
444 pgnutls_global_set_log_function(schan_gnutls_log
);
450 wine_dlclose(libgnutls_handle
, NULL
, 0);
451 libgnutls_handle
= NULL
;
455 void schan_imp_deinit(void)
457 pgnutls_global_deinit();
458 wine_dlclose(libgnutls_handle
, NULL
, 0);
459 libgnutls_handle
= NULL
;
462 #endif /* SONAME_LIBGNUTLS && !HAVE_SECURITY_SECURITY_H */