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 #if defined(SONAME_LIBGNUTLS) && !defined(HAVE_SECURITY_SECURITY_H)
41 WINE_DEFAULT_DEBUG_CHANNEL(secur32
);
42 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
44 /* Not present in gnutls version < 2.9.10. */
45 static int (*pgnutls_cipher_get_block_size
)(gnutls_cipher_algorithm_t algorithm
);
47 static void *libgnutls_handle
;
48 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
49 MAKE_FUNCPTR(gnutls_alert_get
);
50 MAKE_FUNCPTR(gnutls_alert_get_name
);
51 MAKE_FUNCPTR(gnutls_certificate_allocate_credentials
);
52 MAKE_FUNCPTR(gnutls_certificate_free_credentials
);
53 MAKE_FUNCPTR(gnutls_certificate_get_peers
);
54 MAKE_FUNCPTR(gnutls_cipher_get
);
55 MAKE_FUNCPTR(gnutls_cipher_get_key_size
);
56 MAKE_FUNCPTR(gnutls_credentials_set
);
57 MAKE_FUNCPTR(gnutls_deinit
);
58 MAKE_FUNCPTR(gnutls_global_deinit
);
59 MAKE_FUNCPTR(gnutls_global_init
);
60 MAKE_FUNCPTR(gnutls_global_set_log_function
);
61 MAKE_FUNCPTR(gnutls_global_set_log_level
);
62 MAKE_FUNCPTR(gnutls_handshake
);
63 MAKE_FUNCPTR(gnutls_init
);
64 MAKE_FUNCPTR(gnutls_kx_get
);
65 MAKE_FUNCPTR(gnutls_mac_get
);
66 MAKE_FUNCPTR(gnutls_mac_get_key_size
);
67 MAKE_FUNCPTR(gnutls_perror
);
68 MAKE_FUNCPTR(gnutls_protocol_get_version
);
69 MAKE_FUNCPTR(gnutls_priority_set_direct
);
70 MAKE_FUNCPTR(gnutls_record_get_max_size
);
71 MAKE_FUNCPTR(gnutls_record_recv
);
72 MAKE_FUNCPTR(gnutls_record_send
);
73 MAKE_FUNCPTR(gnutls_server_name_set
);
74 MAKE_FUNCPTR(gnutls_transport_get_ptr
);
75 MAKE_FUNCPTR(gnutls_transport_set_errno
);
76 MAKE_FUNCPTR(gnutls_transport_set_ptr
);
77 MAKE_FUNCPTR(gnutls_transport_set_pull_function
);
78 MAKE_FUNCPTR(gnutls_transport_set_push_function
);
81 #if GNUTLS_VERSION_MAJOR < 3
82 #define GNUTLS_CIPHER_AES_192_CBC 92
83 #define GNUTLS_CIPHER_AES_128_GCM 93
84 #define GNUTLS_CIPHER_AES_256_GCM 94
86 #define GNUTLS_MAC_AEAD 200
88 #define GNUTLS_KX_ANON_ECDH 11
89 #define GNUTLS_KX_ECDHE_RSA 12
90 #define GNUTLS_KX_ECDHE_ECDSA 13
91 #define GNUTLS_KX_ECDHE_PSK 14
94 static int compat_cipher_get_block_size(gnutls_cipher_algorithm_t cipher
)
97 case GNUTLS_CIPHER_3DES_CBC
:
99 case GNUTLS_CIPHER_AES_128_CBC
:
100 case GNUTLS_CIPHER_AES_256_CBC
:
102 case GNUTLS_CIPHER_ARCFOUR_128
:
103 case GNUTLS_CIPHER_ARCFOUR_40
:
105 case GNUTLS_CIPHER_DES_CBC
:
107 case GNUTLS_CIPHER_NULL
:
109 case GNUTLS_CIPHER_RC2_40_CBC
:
112 FIXME("Unknown cipher %#x, returning 1\n", cipher
);
117 static ssize_t
schan_pull_adapter(gnutls_transport_ptr_t transport
,
118 void *buff
, size_t buff_len
)
120 struct schan_transport
*t
= (struct schan_transport
*)transport
;
121 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
123 int ret
= schan_pull(transport
, buff
, &buff_len
);
126 pgnutls_transport_set_errno(s
, ret
);
133 static ssize_t
schan_push_adapter(gnutls_transport_ptr_t transport
,
134 const void *buff
, size_t buff_len
)
136 struct schan_transport
*t
= (struct schan_transport
*)transport
;
137 gnutls_session_t s
= (gnutls_session_t
)schan_session_for_transport(t
);
139 int ret
= schan_push(transport
, buff
, &buff_len
);
142 pgnutls_transport_set_errno(s
, ret
);
149 static const struct {
151 const char *gnutls_flag
;
152 } protocol_priority_flags
[] = {
153 {SP_PROT_TLS1_2_CLIENT
, "VERS-TLS1.2"},
154 {SP_PROT_TLS1_1_CLIENT
, "VERS-TLS1.1"},
155 {SP_PROT_TLS1_0_CLIENT
, "VERS-TLS1.0"},
156 {SP_PROT_SSL3_CLIENT
, "VERS-SSL3.0"}
157 /* {SP_PROT_SSL2_CLIENT} is not supported by GnuTLS */
160 DWORD
schan_imp_enabled_protocols(void)
162 /* NOTE: No support for SSL 2.0 */
163 return SP_PROT_SSL3_CLIENT
| SP_PROT_TLS1_0_CLIENT
| SP_PROT_TLS1_1_CLIENT
| SP_PROT_TLS1_2_CLIENT
;
166 BOOL
schan_imp_create_session(schan_imp_session
*session
, schan_credentials
*cred
)
168 gnutls_session_t
*s
= (gnutls_session_t
*)session
;
169 char priority
[128] = "NORMAL:%LATEST_RECORD_VERSION", *p
;
172 int err
= pgnutls_init(s
, cred
->credential_use
== SECPKG_CRED_INBOUND
? GNUTLS_SERVER
: GNUTLS_CLIENT
);
173 if (err
!= GNUTLS_E_SUCCESS
)
179 p
= priority
+ strlen(priority
);
180 for(i
=0; i
< sizeof(protocol_priority_flags
)/sizeof(*protocol_priority_flags
); i
++) {
182 *p
++ = (cred
->enabled_protocols
& protocol_priority_flags
[i
].enable_flag
) ? '+' : '-';
183 strcpy(p
, protocol_priority_flags
[i
].gnutls_flag
);
187 TRACE("Using %s priority\n", debugstr_a(priority
));
188 err
= pgnutls_priority_set_direct(*s
, priority
, NULL
);
189 if (err
!= GNUTLS_E_SUCCESS
)
196 err
= pgnutls_credentials_set(*s
, GNUTLS_CRD_CERTIFICATE
,
197 (gnutls_certificate_credentials_t
)cred
->credentials
);
198 if (err
!= GNUTLS_E_SUCCESS
)
205 pgnutls_transport_set_pull_function(*s
, schan_pull_adapter
);
206 pgnutls_transport_set_push_function(*s
, schan_push_adapter
);
211 void schan_imp_dispose_session(schan_imp_session session
)
213 gnutls_session_t s
= (gnutls_session_t
)session
;
217 void schan_imp_set_session_transport(schan_imp_session session
,
218 struct schan_transport
*t
)
220 gnutls_session_t s
= (gnutls_session_t
)session
;
221 pgnutls_transport_set_ptr(s
, (gnutls_transport_ptr_t
)t
);
224 void schan_imp_set_session_target(schan_imp_session session
, const char *target
)
226 gnutls_session_t s
= (gnutls_session_t
)session
;
228 pgnutls_server_name_set( s
, GNUTLS_NAME_DNS
, target
, strlen(target
) );
231 SECURITY_STATUS
schan_imp_handshake(schan_imp_session session
)
233 gnutls_session_t s
= (gnutls_session_t
)session
;
237 err
= pgnutls_handshake(s
);
239 case GNUTLS_E_SUCCESS
:
240 TRACE("Handshake completed\n");
244 TRACE("Continue...\n");
245 return SEC_I_CONTINUE_NEEDED
;
247 case GNUTLS_E_WARNING_ALERT_RECEIVED
:
249 gnutls_alert_description_t alert
= pgnutls_alert_get(s
);
251 WARN("WARNING ALERT: %d %s\n", alert
, pgnutls_alert_get_name(alert
));
254 case GNUTLS_A_UNRECOGNIZED_NAME
:
258 return SEC_E_INTERNAL_ERROR
;
262 case GNUTLS_E_FATAL_ALERT_RECEIVED
:
264 gnutls_alert_description_t alert
= pgnutls_alert_get(s
);
265 WARN("FATAL ALERT: %d %s\n", alert
, pgnutls_alert_get_name(alert
));
266 return SEC_E_INTERNAL_ERROR
;
271 return SEC_E_INTERNAL_ERROR
;
279 static DWORD
schannel_get_protocol(gnutls_protocol_t proto
)
281 /* FIXME: currently schannel only implements client connections, but
282 * there's no reason it couldn't be used for servers as well. The
283 * context doesn't tell us which it is, so assume client for now.
287 case GNUTLS_SSL3
: return SP_PROT_SSL3_CLIENT
;
288 case GNUTLS_TLS1_0
: return SP_PROT_TLS1_0_CLIENT
;
289 case GNUTLS_TLS1_1
: return SP_PROT_TLS1_1_CLIENT
;
290 case GNUTLS_TLS1_2
: return SP_PROT_TLS1_2_CLIENT
;
292 FIXME("unknown protocol %d\n", proto
);
297 static ALG_ID
schannel_get_cipher_algid(gnutls_cipher_algorithm_t cipher
)
301 case GNUTLS_CIPHER_UNKNOWN
:
302 case GNUTLS_CIPHER_NULL
: return 0;
303 case GNUTLS_CIPHER_ARCFOUR_40
:
304 case GNUTLS_CIPHER_ARCFOUR_128
: return CALG_RC4
;
305 case GNUTLS_CIPHER_DES_CBC
: return CALG_DES
;
306 case GNUTLS_CIPHER_3DES_CBC
: return CALG_3DES
;
307 case GNUTLS_CIPHER_AES_128_CBC
:
308 case GNUTLS_CIPHER_AES_128_GCM
: return CALG_AES_128
;
309 case GNUTLS_CIPHER_AES_192_CBC
: return CALG_AES_192
;
310 case GNUTLS_CIPHER_AES_256_GCM
:
311 case GNUTLS_CIPHER_AES_256_CBC
: return CALG_AES_256
;
312 case GNUTLS_CIPHER_RC2_40_CBC
: return CALG_RC2
;
314 FIXME("unknown algorithm %d\n", cipher
);
319 static ALG_ID
schannel_get_mac_algid(gnutls_mac_algorithm_t mac
, gnutls_cipher_algorithm_t cipher
)
323 case GNUTLS_MAC_UNKNOWN
:
324 case GNUTLS_MAC_NULL
: return 0;
325 case GNUTLS_MAC_MD2
: return CALG_MD2
;
326 case GNUTLS_MAC_MD5
: return CALG_MD5
;
327 case GNUTLS_MAC_SHA1
: return CALG_SHA1
;
328 case GNUTLS_MAC_SHA256
: return CALG_SHA_256
;
329 case GNUTLS_MAC_SHA384
: return CALG_SHA_384
;
330 case GNUTLS_MAC_SHA512
: return CALG_SHA_512
;
331 case GNUTLS_MAC_AEAD
:
332 /* When using AEAD (such as GCM), we return PRF algorithm instead
333 which is defined in RFC 5289. */
336 case GNUTLS_CIPHER_AES_128_GCM
: return CALG_SHA_256
;
337 case GNUTLS_CIPHER_AES_256_GCM
: return CALG_SHA_384
;
343 FIXME("unknown algorithm %d, cipher %d\n", mac
, cipher
);
348 static ALG_ID
schannel_get_kx_algid(int kx
)
352 case GNUTLS_KX_UNKNOWN
: return 0;
354 case GNUTLS_KX_RSA_EXPORT
: return CALG_RSA_KEYX
;
355 case GNUTLS_KX_DHE_PSK
:
356 case GNUTLS_KX_DHE_DSS
:
357 case GNUTLS_KX_DHE_RSA
: return CALG_DH_EPHEM
;
358 case GNUTLS_KX_ANON_ECDH
: return CALG_ECDH
;
359 case GNUTLS_KX_ECDHE_RSA
:
360 case GNUTLS_KX_ECDHE_PSK
:
361 case GNUTLS_KX_ECDHE_ECDSA
: return CALG_ECDH_EPHEM
;
363 FIXME("unknown algorithm %d\n", kx
);
368 unsigned int schan_imp_get_session_cipher_block_size(schan_imp_session session
)
370 gnutls_session_t s
= (gnutls_session_t
)session
;
371 return pgnutls_cipher_get_block_size(pgnutls_cipher_get(s
));
374 unsigned int schan_imp_get_max_message_size(schan_imp_session session
)
376 return pgnutls_record_get_max_size((gnutls_session_t
)session
);
379 SECURITY_STATUS
schan_imp_get_connection_info(schan_imp_session session
,
380 SecPkgContext_ConnectionInfo
*info
)
382 gnutls_session_t s
= (gnutls_session_t
)session
;
383 gnutls_protocol_t proto
= pgnutls_protocol_get_version(s
);
384 gnutls_cipher_algorithm_t alg
= pgnutls_cipher_get(s
);
385 gnutls_mac_algorithm_t mac
= pgnutls_mac_get(s
);
386 gnutls_kx_algorithm_t kx
= pgnutls_kx_get(s
);
388 info
->dwProtocol
= schannel_get_protocol(proto
);
389 info
->aiCipher
= schannel_get_cipher_algid(alg
);
390 info
->dwCipherStrength
= pgnutls_cipher_get_key_size(alg
) * 8;
391 info
->aiHash
= schannel_get_mac_algid(mac
, alg
);
392 info
->dwHashStrength
= pgnutls_mac_get_key_size(mac
) * 8;
393 info
->aiExch
= schannel_get_kx_algid(kx
);
394 /* FIXME: info->dwExchStrength? */
395 info
->dwExchStrength
= 0;
399 ALG_ID
schan_imp_get_key_signature_algorithm(schan_imp_session session
)
401 gnutls_session_t s
= (gnutls_session_t
)session
;
402 gnutls_kx_algorithm_t kx
= pgnutls_kx_get(s
);
404 TRACE("(%p)\n", session
);
408 case GNUTLS_KX_UNKNOWN
: return 0;
410 case GNUTLS_KX_RSA_EXPORT
:
411 case GNUTLS_KX_DHE_RSA
:
412 case GNUTLS_KX_ECDHE_RSA
: return CALG_RSA_SIGN
;
413 case GNUTLS_KX_ECDHE_ECDSA
: return CALG_ECDSA
;
415 FIXME("unknown algorithm %d\n", kx
);
420 SECURITY_STATUS
schan_imp_get_session_peer_certificate(schan_imp_session session
, HCERTSTORE store
,
423 gnutls_session_t s
= (gnutls_session_t
)session
;
424 PCCERT_CONTEXT cert
= NULL
;
425 const gnutls_datum_t
*datum
;
426 unsigned list_size
, i
;
429 datum
= pgnutls_certificate_get_peers(s
, &list_size
);
431 return SEC_E_INTERNAL_ERROR
;
433 for(i
= 0; i
< list_size
; i
++) {
434 res
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
, datum
[i
].data
, datum
[i
].size
,
435 CERT_STORE_ADD_REPLACE_EXISTING
, i
? NULL
: &cert
);
438 CertFreeCertificateContext(cert
);
439 return GetLastError();
447 SECURITY_STATUS
schan_imp_send(schan_imp_session session
, const void *buffer
,
450 gnutls_session_t s
= (gnutls_session_t
)session
;
451 SSIZE_T ret
, total
= 0;
455 ret
= pgnutls_record_send(s
, (const char *)buffer
+ total
, *length
- total
);
459 TRACE( "sent %ld now %ld/%ld\n", ret
, total
, *length
);
460 if (total
== *length
) return SEC_E_OK
;
462 else if (ret
== GNUTLS_E_AGAIN
)
464 struct schan_transport
*t
= (struct schan_transport
*)pgnutls_transport_get_ptr(s
);
467 if (schan_get_buffer(t
, &t
->out
, &count
)) continue;
468 return SEC_I_CONTINUE_NEEDED
;
473 return SEC_E_INTERNAL_ERROR
;
478 SECURITY_STATUS
schan_imp_recv(schan_imp_session session
, void *buffer
,
481 gnutls_session_t s
= (gnutls_session_t
)session
;
485 ret
= pgnutls_record_recv(s
, buffer
, *length
);
489 else if (ret
== GNUTLS_E_AGAIN
)
491 struct schan_transport
*t
= (struct schan_transport
*)pgnutls_transport_get_ptr(s
);
494 if (schan_get_buffer(t
, &t
->in
, &count
))
497 return SEC_I_CONTINUE_NEEDED
;
502 return SEC_E_INTERNAL_ERROR
;
508 BOOL
schan_imp_allocate_certificate_credentials(schan_credentials
*c
)
510 int ret
= pgnutls_certificate_allocate_credentials((gnutls_certificate_credentials_t
*)&c
->credentials
);
511 if (ret
!= GNUTLS_E_SUCCESS
)
513 return (ret
== GNUTLS_E_SUCCESS
);
516 void schan_imp_free_certificate_credentials(schan_credentials
*c
)
518 pgnutls_certificate_free_credentials(c
->credentials
);
521 static void schan_gnutls_log(int level
, const char *msg
)
523 TRACE("<%d> %s", level
, msg
);
526 BOOL
schan_imp_init(void)
530 libgnutls_handle
= wine_dlopen(SONAME_LIBGNUTLS
, RTLD_NOW
, NULL
, 0);
531 if (!libgnutls_handle
)
533 ERR_(winediag
)("Failed to load libgnutls, secure connections will not be available.\n");
537 #define LOAD_FUNCPTR(f) \
538 if (!(p##f = wine_dlsym(libgnutls_handle, #f, NULL, 0))) \
540 ERR("Failed to load %s\n", #f); \
544 LOAD_FUNCPTR(gnutls_alert_get
)
545 LOAD_FUNCPTR(gnutls_alert_get_name
)
546 LOAD_FUNCPTR(gnutls_certificate_allocate_credentials
)
547 LOAD_FUNCPTR(gnutls_certificate_free_credentials
)
548 LOAD_FUNCPTR(gnutls_certificate_get_peers
)
549 LOAD_FUNCPTR(gnutls_cipher_get
)
550 LOAD_FUNCPTR(gnutls_cipher_get_key_size
)
551 LOAD_FUNCPTR(gnutls_credentials_set
)
552 LOAD_FUNCPTR(gnutls_deinit
)
553 LOAD_FUNCPTR(gnutls_global_deinit
)
554 LOAD_FUNCPTR(gnutls_global_init
)
555 LOAD_FUNCPTR(gnutls_global_set_log_function
)
556 LOAD_FUNCPTR(gnutls_global_set_log_level
)
557 LOAD_FUNCPTR(gnutls_handshake
)
558 LOAD_FUNCPTR(gnutls_init
)
559 LOAD_FUNCPTR(gnutls_kx_get
)
560 LOAD_FUNCPTR(gnutls_mac_get
)
561 LOAD_FUNCPTR(gnutls_mac_get_key_size
)
562 LOAD_FUNCPTR(gnutls_perror
)
563 LOAD_FUNCPTR(gnutls_protocol_get_version
)
564 LOAD_FUNCPTR(gnutls_priority_set_direct
)
565 LOAD_FUNCPTR(gnutls_record_get_max_size
);
566 LOAD_FUNCPTR(gnutls_record_recv
);
567 LOAD_FUNCPTR(gnutls_record_send
);
568 LOAD_FUNCPTR(gnutls_server_name_set
)
569 LOAD_FUNCPTR(gnutls_transport_get_ptr
)
570 LOAD_FUNCPTR(gnutls_transport_set_errno
)
571 LOAD_FUNCPTR(gnutls_transport_set_ptr
)
572 LOAD_FUNCPTR(gnutls_transport_set_pull_function
)
573 LOAD_FUNCPTR(gnutls_transport_set_push_function
)
576 if (!(pgnutls_cipher_get_block_size
= wine_dlsym(libgnutls_handle
, "gnutls_cipher_get_block_size", NULL
, 0)))
578 WARN("gnutls_cipher_get_block_size not found\n");
579 pgnutls_cipher_get_block_size
= compat_cipher_get_block_size
;
582 ret
= pgnutls_global_init();
583 if (ret
!= GNUTLS_E_SUCCESS
)
589 if (TRACE_ON(secur32
))
591 pgnutls_global_set_log_level(4);
592 pgnutls_global_set_log_function(schan_gnutls_log
);
598 wine_dlclose(libgnutls_handle
, NULL
, 0);
599 libgnutls_handle
= NULL
;
603 void schan_imp_deinit(void)
605 pgnutls_global_deinit();
606 wine_dlclose(libgnutls_handle
, NULL
, 0);
607 libgnutls_handle
= NULL
;
610 #endif /* SONAME_LIBGNUTLS && !HAVE_SECURITY_SECURITY_H */