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>
28 #include <gnutls/crypto.h>
35 #include "secur32_priv.h"
36 #include "wine/debug.h"
37 #include "wine/library.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(secur32
);
41 #if defined(SONAME_LIBGNUTLS) && !defined(HAVE_SECURITY_SECURITY_H)
43 static void *libgnutls_handle
;
44 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
45 MAKE_FUNCPTR(gnutls_alert_get
);
46 MAKE_FUNCPTR(gnutls_alert_get_name
);
47 MAKE_FUNCPTR(gnutls_certificate_allocate_credentials
);
48 MAKE_FUNCPTR(gnutls_certificate_free_credentials
);
49 MAKE_FUNCPTR(gnutls_certificate_get_peers
);
50 MAKE_FUNCPTR(gnutls_cipher_get
);
51 MAKE_FUNCPTR(gnutls_cipher_get_key_size
);
52 MAKE_FUNCPTR(gnutls_credentials_set
);
53 MAKE_FUNCPTR(gnutls_deinit
);
54 MAKE_FUNCPTR(gnutls_global_deinit
);
55 MAKE_FUNCPTR(gnutls_global_init
);
56 MAKE_FUNCPTR(gnutls_global_set_log_function
);
57 MAKE_FUNCPTR(gnutls_global_set_log_level
);
58 MAKE_FUNCPTR(gnutls_handshake
);
59 MAKE_FUNCPTR(gnutls_init
);
60 MAKE_FUNCPTR(gnutls_kx_get
);
61 MAKE_FUNCPTR(gnutls_mac_get
);
62 MAKE_FUNCPTR(gnutls_mac_get_key_size
);
63 MAKE_FUNCPTR(gnutls_perror
);
64 MAKE_FUNCPTR(gnutls_protocol_get_version
);
65 MAKE_FUNCPTR(gnutls_priority_set_direct
);
66 MAKE_FUNCPTR(gnutls_record_get_max_size
);
67 MAKE_FUNCPTR(gnutls_record_recv
);
68 MAKE_FUNCPTR(gnutls_record_send
);
69 MAKE_FUNCPTR(gnutls_server_name_set
);
70 MAKE_FUNCPTR(gnutls_transport_get_ptr
);
71 MAKE_FUNCPTR(gnutls_transport_set_errno
);
72 MAKE_FUNCPTR(gnutls_transport_set_ptr
);
73 MAKE_FUNCPTR(gnutls_transport_set_pull_function
);
74 MAKE_FUNCPTR(gnutls_transport_set_push_function
);
79 static ssize_t
schan_pull_adapter(gnutls_transport_ptr_t transport
,
80 void *buff
, size_t buff_len
)
82 struct schan_transport
*t
= (struct schan_transport
*)transport
;
83 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
85 int ret
= schan_pull(transport
, buff
, &buff_len
);
88 pgnutls_transport_set_errno(s
, ret
);
95 static ssize_t
schan_push_adapter(gnutls_transport_ptr_t transport
,
96 const void *buff
, size_t buff_len
)
98 struct schan_transport
*t
= (struct schan_transport
*)transport
;
99 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
101 int ret
= schan_push(transport
, buff
, &buff_len
);
104 pgnutls_transport_set_errno(s
, ret
);
111 static const struct {
113 const char *gnutls_flag
;
114 } protocol_priority_flags
[] = {
115 {SP_PROT_TLS1_2_CLIENT
, "VERS-TLS1.2"},
116 {SP_PROT_TLS1_1_CLIENT
, "VERS-TLS1.1"},
117 {SP_PROT_TLS1_0_CLIENT
, "VERS-TLS1.0"},
118 {SP_PROT_SSL3_CLIENT
, "VERS-SSL3.0"}
119 /* {SP_PROT_SSL2_CLIENT} is not supported by GnuTLS */
122 DWORD
schan_imp_enabled_protocols(void)
124 /* NOTE: No support for SSL 2.0 */
125 return SP_PROT_SSL3_CLIENT
| SP_PROT_TLS1_0_CLIENT
| SP_PROT_TLS1_1_CLIENT
| SP_PROT_TLS1_2_CLIENT
;
128 BOOL
schan_imp_create_session(schan_imp_session
*session
, schan_credentials
*cred
)
130 gnutls_session_t
*s
= (gnutls_session_t
*)session
;
131 char priority
[64] = "NORMAL", *p
;
134 int err
= pgnutls_init(s
, cred
->credential_use
== SECPKG_CRED_INBOUND
? GNUTLS_SERVER
: GNUTLS_CLIENT
);
135 if (err
!= GNUTLS_E_SUCCESS
)
141 p
= priority
+ strlen(priority
);
142 for(i
=0; i
< sizeof(protocol_priority_flags
)/sizeof(*protocol_priority_flags
); i
++) {
144 *p
++ = (cred
->enabled_protocols
& protocol_priority_flags
[i
].enable_flag
) ? '+' : '-';
145 strcpy(p
, protocol_priority_flags
[i
].gnutls_flag
);
149 TRACE("Using %s priority\n", debugstr_a(priority
));
150 err
= pgnutls_priority_set_direct(*s
, priority
, NULL
);
151 if (err
!= GNUTLS_E_SUCCESS
)
158 err
= pgnutls_credentials_set(*s
, GNUTLS_CRD_CERTIFICATE
,
159 (gnutls_certificate_credentials_t
)cred
->credentials
);
160 if (err
!= GNUTLS_E_SUCCESS
)
167 pgnutls_transport_set_pull_function(*s
, schan_pull_adapter
);
168 pgnutls_transport_set_push_function(*s
, schan_push_adapter
);
173 void schan_imp_dispose_session(schan_imp_session session
)
175 gnutls_session_t s
= (gnutls_session_t
)session
;
179 void schan_imp_set_session_transport(schan_imp_session session
,
180 struct schan_transport
*t
)
182 gnutls_session_t s
= (gnutls_session_t
)session
;
183 pgnutls_transport_set_ptr(s
, (gnutls_transport_ptr_t
)t
);
186 void schan_imp_set_session_target(schan_imp_session session
, const char *target
)
188 gnutls_session_t s
= (gnutls_session_t
)session
;
190 pgnutls_server_name_set( s
, GNUTLS_NAME_DNS
, target
, strlen(target
) );
193 SECURITY_STATUS
schan_imp_handshake(schan_imp_session session
)
195 gnutls_session_t s
= (gnutls_session_t
)session
;
196 int err
= pgnutls_handshake(s
);
199 case GNUTLS_E_SUCCESS
:
200 TRACE("Handshake completed\n");
204 TRACE("Continue...\n");
205 return SEC_I_CONTINUE_NEEDED
;
207 case GNUTLS_E_WARNING_ALERT_RECEIVED
:
208 case GNUTLS_E_FATAL_ALERT_RECEIVED
:
210 gnutls_alert_description_t alert
= pgnutls_alert_get(s
);
211 const char *alert_name
= pgnutls_alert_get_name(alert
);
212 WARN("ALERT: %d %s\n", alert
, alert_name
);
213 return SEC_E_INTERNAL_ERROR
;
218 return SEC_E_INTERNAL_ERROR
;
225 static unsigned int schannel_get_cipher_block_size(gnutls_cipher_algorithm_t cipher
)
229 gnutls_cipher_algorithm_t cipher
;
230 unsigned int block_size
;
234 {GNUTLS_CIPHER_3DES_CBC
, 8},
235 {GNUTLS_CIPHER_AES_128_CBC
, 16},
236 {GNUTLS_CIPHER_AES_256_CBC
, 16},
237 {GNUTLS_CIPHER_ARCFOUR_128
, 1},
238 {GNUTLS_CIPHER_ARCFOUR_40
, 1},
239 {GNUTLS_CIPHER_DES_CBC
, 8},
240 {GNUTLS_CIPHER_NULL
, 1},
241 {GNUTLS_CIPHER_RC2_40_CBC
, 8},
245 for (i
= 0; i
< sizeof(algorithms
) / sizeof(*algorithms
); ++i
)
247 if (algorithms
[i
].cipher
== cipher
)
248 return algorithms
[i
].block_size
;
251 FIXME("Unknown cipher %#x, returning 1\n", cipher
);
256 static DWORD
schannel_get_protocol(gnutls_protocol_t proto
)
258 /* FIXME: currently schannel only implements client connections, but
259 * there's no reason it couldn't be used for servers as well. The
260 * context doesn't tell us which it is, so assume client for now.
264 case GNUTLS_SSL3
: return SP_PROT_SSL3_CLIENT
;
265 case GNUTLS_TLS1_0
: return SP_PROT_TLS1_0_CLIENT
;
266 case GNUTLS_TLS1_1
: return SP_PROT_TLS1_1_CLIENT
;
267 case GNUTLS_TLS1_2
: return SP_PROT_TLS1_2_CLIENT
;
269 FIXME("unknown protocol %d\n", proto
);
274 static ALG_ID
schannel_get_cipher_algid(gnutls_cipher_algorithm_t cipher
)
278 case GNUTLS_CIPHER_UNKNOWN
:
279 case GNUTLS_CIPHER_NULL
: return 0;
280 case GNUTLS_CIPHER_ARCFOUR_40
:
281 case GNUTLS_CIPHER_ARCFOUR_128
: return CALG_RC4
;
282 case GNUTLS_CIPHER_DES_CBC
:
283 case GNUTLS_CIPHER_3DES_CBC
: return CALG_DES
;
284 case GNUTLS_CIPHER_AES_128_CBC
:
285 case GNUTLS_CIPHER_AES_256_CBC
: return CALG_AES
;
286 case GNUTLS_CIPHER_RC2_40_CBC
: return CALG_RC2
;
288 FIXME("unknown algorithm %d\n", cipher
);
293 static ALG_ID
schannel_get_mac_algid(gnutls_mac_algorithm_t mac
)
297 case GNUTLS_MAC_UNKNOWN
:
298 case GNUTLS_MAC_NULL
: return 0;
299 case GNUTLS_MAC_MD5
: return CALG_MD5
;
300 case GNUTLS_MAC_SHA1
:
301 case GNUTLS_MAC_SHA256
:
302 case GNUTLS_MAC_SHA384
:
303 case GNUTLS_MAC_SHA512
: return CALG_SHA
;
305 FIXME("unknown algorithm %d\n", mac
);
310 static ALG_ID
schannel_get_kx_algid(gnutls_kx_algorithm_t kx
)
314 case GNUTLS_KX_RSA
: return CALG_RSA_KEYX
;
315 case GNUTLS_KX_DHE_DSS
:
316 case GNUTLS_KX_DHE_RSA
: return CALG_DH_EPHEM
;
318 FIXME("unknown algorithm %d\n", kx
);
323 unsigned int schan_imp_get_session_cipher_block_size(schan_imp_session session
)
325 gnutls_session_t s
= (gnutls_session_t
)session
;
326 gnutls_cipher_algorithm_t cipher
= pgnutls_cipher_get(s
);
327 return schannel_get_cipher_block_size(cipher
);
330 unsigned int schan_imp_get_max_message_size(schan_imp_session session
)
332 return pgnutls_record_get_max_size((gnutls_session_t
)session
);
335 SECURITY_STATUS
schan_imp_get_connection_info(schan_imp_session session
,
336 SecPkgContext_ConnectionInfo
*info
)
338 gnutls_session_t s
= (gnutls_session_t
)session
;
339 gnutls_protocol_t proto
= pgnutls_protocol_get_version(s
);
340 gnutls_cipher_algorithm_t alg
= pgnutls_cipher_get(s
);
341 gnutls_mac_algorithm_t mac
= pgnutls_mac_get(s
);
342 gnutls_kx_algorithm_t kx
= pgnutls_kx_get(s
);
344 info
->dwProtocol
= schannel_get_protocol(proto
);
345 info
->aiCipher
= schannel_get_cipher_algid(alg
);
346 info
->dwCipherStrength
= pgnutls_cipher_get_key_size(alg
) * 8;
347 info
->aiHash
= schannel_get_mac_algid(mac
);
348 info
->dwHashStrength
= pgnutls_mac_get_key_size(mac
) * 8;
349 info
->aiExch
= schannel_get_kx_algid(kx
);
350 /* FIXME: info->dwExchStrength? */
351 info
->dwExchStrength
= 0;
355 SECURITY_STATUS
schan_imp_get_session_peer_certificate(schan_imp_session session
, HCERTSTORE store
,
358 gnutls_session_t s
= (gnutls_session_t
)session
;
359 PCCERT_CONTEXT cert
= NULL
;
360 const gnutls_datum_t
*datum
;
361 unsigned list_size
, i
;
364 datum
= pgnutls_certificate_get_peers(s
, &list_size
);
366 return SEC_E_INTERNAL_ERROR
;
368 for(i
= 0; i
< list_size
; i
++) {
369 res
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
, datum
[i
].data
, datum
[i
].size
,
370 CERT_STORE_ADD_REPLACE_EXISTING
, i
? NULL
: &cert
);
373 CertFreeCertificateContext(cert
);
374 return GetLastError();
382 SECURITY_STATUS
schan_imp_send(schan_imp_session session
, const void *buffer
,
385 gnutls_session_t s
= (gnutls_session_t
)session
;
389 ret
= pgnutls_record_send(s
, buffer
, *length
);
393 else if (ret
== GNUTLS_E_AGAIN
)
395 struct schan_transport
*t
= (struct schan_transport
*)pgnutls_transport_get_ptr(s
);
398 if (schan_get_buffer(t
, &t
->out
, &count
))
401 return SEC_I_CONTINUE_NEEDED
;
406 return SEC_E_INTERNAL_ERROR
;
412 SECURITY_STATUS
schan_imp_recv(schan_imp_session session
, void *buffer
,
415 gnutls_session_t s
= (gnutls_session_t
)session
;
419 ret
= pgnutls_record_recv(s
, buffer
, *length
);
423 else if (ret
== GNUTLS_E_AGAIN
)
425 struct schan_transport
*t
= (struct schan_transport
*)pgnutls_transport_get_ptr(s
);
428 if (schan_get_buffer(t
, &t
->in
, &count
))
431 return SEC_I_CONTINUE_NEEDED
;
436 return SEC_E_INTERNAL_ERROR
;
442 BOOL
schan_imp_allocate_certificate_credentials(schan_credentials
*c
)
444 int ret
= pgnutls_certificate_allocate_credentials((gnutls_certificate_credentials_t
*)&c
->credentials
);
445 if (ret
!= GNUTLS_E_SUCCESS
)
447 return (ret
== GNUTLS_E_SUCCESS
);
450 void schan_imp_free_certificate_credentials(schan_credentials
*c
)
452 pgnutls_certificate_free_credentials(c
->credentials
);
455 static void schan_gnutls_log(int level
, const char *msg
)
457 TRACE("<%d> %s", level
, msg
);
460 BOOL
schan_imp_init(void)
464 libgnutls_handle
= wine_dlopen(SONAME_LIBGNUTLS
, RTLD_NOW
, NULL
, 0);
465 if (!libgnutls_handle
)
467 WARN("Failed to load libgnutls.\n");
471 #define LOAD_FUNCPTR(f) \
472 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
474 ERR("Failed to load %s\n", #f); \
478 LOAD_FUNCPTR(gnutls_alert_get
)
479 LOAD_FUNCPTR(gnutls_alert_get_name
)
480 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials
)
481 LOAD_FUNCPTR(gnutls_certificate_free_credentials
)
482 LOAD_FUNCPTR(gnutls_certificate_get_peers
)
483 LOAD_FUNCPTR(gnutls_cipher_get
)
484 LOAD_FUNCPTR(gnutls_cipher_get_key_size
)
485 LOAD_FUNCPTR(gnutls_credentials_set
)
486 LOAD_FUNCPTR(gnutls_deinit
)
487 LOAD_FUNCPTR(gnutls_global_deinit
)
488 LOAD_FUNCPTR(gnutls_global_init
)
489 LOAD_FUNCPTR(gnutls_global_set_log_function
)
490 LOAD_FUNCPTR(gnutls_global_set_log_level
)
491 LOAD_FUNCPTR(gnutls_handshake
)
492 LOAD_FUNCPTR(gnutls_init
)
493 LOAD_FUNCPTR(gnutls_kx_get
)
494 LOAD_FUNCPTR(gnutls_mac_get
)
495 LOAD_FUNCPTR(gnutls_mac_get_key_size
)
496 LOAD_FUNCPTR(gnutls_perror
)
497 LOAD_FUNCPTR(gnutls_protocol_get_version
)
498 LOAD_FUNCPTR(gnutls_priority_set_direct
)
499 LOAD_FUNCPTR(gnutls_record_get_max_size
);
500 LOAD_FUNCPTR(gnutls_record_recv
);
501 LOAD_FUNCPTR(gnutls_record_send
);
502 LOAD_FUNCPTR(gnutls_server_name_set
)
503 LOAD_FUNCPTR(gnutls_transport_get_ptr
)
504 LOAD_FUNCPTR(gnutls_transport_set_errno
)
505 LOAD_FUNCPTR(gnutls_transport_set_ptr
)
506 LOAD_FUNCPTR(gnutls_transport_set_pull_function
)
507 LOAD_FUNCPTR(gnutls_transport_set_push_function
)
510 ret
= pgnutls_global_init();
511 if (ret
!= GNUTLS_E_SUCCESS
)
517 if (TRACE_ON(secur32
))
519 pgnutls_global_set_log_level(4);
520 pgnutls_global_set_log_function(schan_gnutls_log
);
526 wine_dlclose(libgnutls_handle
, NULL
, 0);
527 libgnutls_handle
= NULL
;
531 void schan_imp_deinit(void)
533 pgnutls_global_deinit();
534 wine_dlclose(libgnutls_handle
, NULL
, 0);
535 libgnutls_handle
= NULL
;
538 #endif /* SONAME_LIBGNUTLS && !HAVE_SECURITY_SECURITY_H */