2 * SSL/TLS interface functions for Microsoft Schannel
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 * FIX: Go through all SSPI functions and verify what needs to be freed
11 * FIX: session resumption
12 * TODO: add support for server cert chain validation
13 * TODO: add support for CA cert validation
14 * TODO: add support for EAP-TLS (client cert/key conf)
21 #define SECURITY_WIN32
31 PSecurityFunctionTable sspi;
32 HCERTSTORE my_cert_store;
35 struct tls_connection {
36 int established, start;
37 int failed, read_alerts, write_alerts;
39 SCHANNEL_CRED schannel_cred;
48 static int schannel_load_lib(struct tls_global *global)
50 INIT_SECURITY_INTERFACE pInitSecurityInterface;
52 global->hsecurity = LoadLibrary(TEXT("Secur32.dll"));
53 if (global->hsecurity == NULL) {
54 wpa_printf(MSG_ERROR, "%s: Could not load Secur32.dll - 0x%x",
55 __func__, (unsigned int) GetLastError());
59 pInitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress(
60 global->hsecurity, "InitSecurityInterfaceA");
61 if (pInitSecurityInterface == NULL) {
62 wpa_printf(MSG_ERROR, "%s: Could not find "
63 "InitSecurityInterfaceA from Secur32.dll",
65 FreeLibrary(global->hsecurity);
66 global->hsecurity = NULL;
70 global->sspi = pInitSecurityInterface();
71 if (global->sspi == NULL) {
72 wpa_printf(MSG_ERROR, "%s: Could not read security "
74 __func__, (unsigned int) GetLastError());
75 FreeLibrary(global->hsecurity);
76 global->hsecurity = NULL;
84 void * tls_init(const struct tls_config *conf)
86 struct tls_global *global;
88 global = os_zalloc(sizeof(*global));
91 if (schannel_load_lib(global)) {
99 void tls_deinit(void *ssl_ctx)
101 struct tls_global *global = ssl_ctx;
103 if (global->my_cert_store)
104 CertCloseStore(global->my_cert_store, 0);
105 FreeLibrary(global->hsecurity);
110 int tls_get_errors(void *ssl_ctx)
116 struct tls_connection * tls_connection_init(void *ssl_ctx)
118 struct tls_connection *conn;
120 conn = os_zalloc(sizeof(*conn));
129 void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
138 int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
140 return conn ? conn->established : 0;
144 int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
146 struct tls_global *global = ssl_ctx;
150 conn->eap_tls_prf_set = 0;
151 conn->established = conn->failed = 0;
152 conn->read_alerts = conn->write_alerts = 0;
153 global->sspi->DeleteSecurityContext(&conn->context);
154 /* FIX: what else needs to be reseted? */
160 int tls_global_set_params(void *tls_ctx,
161 const struct tls_connection_params *params)
167 int tls_global_set_verify(void *ssl_ctx, int check_crl)
173 int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
180 int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
181 struct tls_keys *keys)
183 /* Schannel does not export master secret or client/server random. */
188 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
189 const char *label, int server_random_first,
190 u8 *out, size_t out_len)
193 * Cannot get master_key from Schannel, but EapKeyBlock can be used to
194 * generate session keys for EAP-TLS and EAP-PEAPv0. EAP-PEAPv2 and
195 * EAP-TTLS cannot use this, though, since they are using different
196 * labels. The only option could be to implement TLSv1 completely here
197 * and just use Schannel or CryptoAPI for low-level crypto
201 if (conn == NULL || !conn->eap_tls_prf_set || server_random_first ||
202 os_strcmp(label, "client EAP encryption") != 0 ||
203 out_len > sizeof(conn->eap_tls_prf))
206 os_memcpy(out, conn->eap_tls_prf, out_len);
212 static struct wpabuf * tls_conn_hs_clienthello(struct tls_global *global,
213 struct tls_connection *conn)
215 DWORD sspi_flags, sspi_flags_out;
216 SecBufferDesc outbuf;
217 SecBuffer outbufs[1];
218 SECURITY_STATUS status;
221 sspi_flags = ISC_REQ_REPLAY_DETECT |
222 ISC_REQ_CONFIDENTIALITY |
223 ISC_RET_EXTENDED_ERROR |
224 ISC_REQ_ALLOCATE_MEMORY |
225 ISC_REQ_MANUAL_CRED_VALIDATION;
227 wpa_printf(MSG_DEBUG, "%s: Generating ClientHello", __func__);
229 outbufs[0].pvBuffer = NULL;
230 outbufs[0].BufferType = SECBUFFER_TOKEN;
231 outbufs[0].cbBuffer = 0;
234 outbuf.pBuffers = outbufs;
235 outbuf.ulVersion = SECBUFFER_VERSION;
238 status = global->sspi->InitializeSecurityContextW(
239 &conn->creds, NULL, NULL /* server name */, sspi_flags, 0,
240 SECURITY_NATIVE_DREP, NULL, 0, &conn->context,
241 &outbuf, &sspi_flags_out, &ts_expiry);
243 status = global->sspi->InitializeSecurityContextA(
244 &conn->creds, NULL, NULL /* server name */, sspi_flags, 0,
245 SECURITY_NATIVE_DREP, NULL, 0, &conn->context,
246 &outbuf, &sspi_flags_out, &ts_expiry);
248 if (status != SEC_I_CONTINUE_NEEDED) {
249 wpa_printf(MSG_ERROR, "%s: InitializeSecurityContextA "
251 __func__, (unsigned int) status);
255 if (outbufs[0].cbBuffer != 0 && outbufs[0].pvBuffer) {
257 wpa_hexdump(MSG_MSGDUMP, "SChannel - ClientHello",
258 outbufs[0].pvBuffer, outbufs[0].cbBuffer);
260 buf = wpabuf_alloc_copy(outbufs[0].pvBuffer,
261 outbufs[0].cbBuffer);
264 global->sspi->FreeContextBuffer(outbufs[0].pvBuffer);
268 wpa_printf(MSG_ERROR, "SChannel: Failed to generate ClientHello");
274 #ifndef SECPKG_ATTR_EAP_KEY_BLOCK
275 #define SECPKG_ATTR_EAP_KEY_BLOCK 0x5b
277 typedef struct _SecPkgContext_EapKeyBlock {
280 } SecPkgContext_EapKeyBlock, *PSecPkgContext_EapKeyBlock;
281 #endif /* !SECPKG_ATTR_EAP_KEY_BLOCK */
283 static int tls_get_eap(struct tls_global *global, struct tls_connection *conn)
285 SECURITY_STATUS status;
286 SecPkgContext_EapKeyBlock kb;
288 /* Note: Windows NT and Windows Me/98/95 do not support getting
291 status = global->sspi->QueryContextAttributes(
292 &conn->context, SECPKG_ATTR_EAP_KEY_BLOCK, &kb);
293 if (status != SEC_E_OK) {
294 wpa_printf(MSG_DEBUG, "%s: QueryContextAttributes("
295 "SECPKG_ATTR_EAP_KEY_BLOCK) failed (%d)",
296 __func__, (int) status);
300 wpa_hexdump_key(MSG_MSGDUMP, "Schannel - EapKeyBlock - rgbKeys",
301 kb.rgbKeys, sizeof(kb.rgbKeys));
302 wpa_hexdump_key(MSG_MSGDUMP, "Schannel - EapKeyBlock - rgbIVs",
303 kb.rgbIVs, sizeof(kb.rgbIVs));
305 os_memcpy(conn->eap_tls_prf, kb.rgbKeys, sizeof(kb.rgbKeys));
306 conn->eap_tls_prf_set = 1;
311 struct wpabuf * tls_connection_handshake(void *tls_ctx,
312 struct tls_connection *conn,
313 const struct wpabuf *in_data,
314 struct wpabuf **appl_data)
316 struct tls_global *global = tls_ctx;
317 DWORD sspi_flags, sspi_flags_out;
318 SecBufferDesc inbuf, outbuf;
319 SecBuffer inbufs[2], outbufs[1];
320 SECURITY_STATUS status;
322 struct wpabuf *out_buf = NULL;
328 return tls_conn_hs_clienthello(global, conn);
330 wpa_printf(MSG_DEBUG, "SChannel: %d bytes handshake data to process",
331 (int) wpabuf_len(in_data));
333 sspi_flags = ISC_REQ_REPLAY_DETECT |
334 ISC_REQ_CONFIDENTIALITY |
335 ISC_RET_EXTENDED_ERROR |
336 ISC_REQ_ALLOCATE_MEMORY |
337 ISC_REQ_MANUAL_CRED_VALIDATION;
339 /* Input buffer for Schannel */
340 inbufs[0].pvBuffer = (u8 *) wpabuf_head(in_data);
341 inbufs[0].cbBuffer = wpabuf_len(in_data);
342 inbufs[0].BufferType = SECBUFFER_TOKEN;
344 /* Place for leftover data from Schannel */
345 inbufs[1].pvBuffer = NULL;
346 inbufs[1].cbBuffer = 0;
347 inbufs[1].BufferType = SECBUFFER_EMPTY;
350 inbuf.pBuffers = inbufs;
351 inbuf.ulVersion = SECBUFFER_VERSION;
353 /* Output buffer for Schannel */
354 outbufs[0].pvBuffer = NULL;
355 outbufs[0].cbBuffer = 0;
356 outbufs[0].BufferType = SECBUFFER_TOKEN;
359 outbuf.pBuffers = outbufs;
360 outbuf.ulVersion = SECBUFFER_VERSION;
363 status = global->sspi->InitializeSecurityContextW(
364 &conn->creds, &conn->context, NULL, sspi_flags, 0,
365 SECURITY_NATIVE_DREP, &inbuf, 0, NULL,
366 &outbuf, &sspi_flags_out, &ts_expiry);
368 status = global->sspi->InitializeSecurityContextA(
369 &conn->creds, &conn->context, NULL, sspi_flags, 0,
370 SECURITY_NATIVE_DREP, &inbuf, 0, NULL,
371 &outbuf, &sspi_flags_out, &ts_expiry);
374 wpa_printf(MSG_MSGDUMP, "Schannel: InitializeSecurityContext -> "
375 "status=%d inlen[0]=%d intype[0]=%d inlen[1]=%d "
376 "intype[1]=%d outlen[0]=%d",
377 (int) status, (int) inbufs[0].cbBuffer,
378 (int) inbufs[0].BufferType, (int) inbufs[1].cbBuffer,
379 (int) inbufs[1].BufferType,
380 (int) outbufs[0].cbBuffer);
381 if (status == SEC_E_OK || status == SEC_I_CONTINUE_NEEDED ||
382 (FAILED(status) && (sspi_flags_out & ISC_RET_EXTENDED_ERROR))) {
383 if (outbufs[0].cbBuffer != 0 && outbufs[0].pvBuffer) {
384 wpa_hexdump(MSG_MSGDUMP, "SChannel - output",
385 outbufs[0].pvBuffer, outbufs[0].cbBuffer);
386 out_buf = wpabuf_alloc_copy(outbufs[0].pvBuffer,
387 outbufs[0].cbBuffer);
388 global->sspi->FreeContextBuffer(outbufs[0].pvBuffer);
389 outbufs[0].pvBuffer = NULL;
396 case SEC_E_INCOMPLETE_MESSAGE:
397 wpa_printf(MSG_DEBUG, "Schannel: SEC_E_INCOMPLETE_MESSAGE");
399 case SEC_I_CONTINUE_NEEDED:
400 wpa_printf(MSG_DEBUG, "Schannel: SEC_I_CONTINUE_NEEDED");
403 /* TODO: verify server certificate chain */
404 wpa_printf(MSG_DEBUG, "Schannel: SEC_E_OK - Handshake "
405 "completed successfully");
406 conn->established = 1;
407 tls_get_eap(global, conn);
409 /* Need to return something to get final TLS ACK. */
411 out_buf = wpabuf_alloc(0);
413 if (inbufs[1].BufferType == SECBUFFER_EXTRA) {
414 wpa_hexdump(MSG_MSGDUMP, "SChannel - Encrypted "
416 inbufs[1].pvBuffer, inbufs[1].cbBuffer);
418 *appl_data = wpabuf_alloc_copy(
420 outbufs[1].cbBuffer);
422 global->sspi->FreeContextBuffer(inbufs[1].pvBuffer);
423 inbufs[1].pvBuffer = NULL;
426 case SEC_I_INCOMPLETE_CREDENTIALS:
427 wpa_printf(MSG_DEBUG,
428 "Schannel: SEC_I_INCOMPLETE_CREDENTIALS");
430 case SEC_E_WRONG_PRINCIPAL:
431 wpa_printf(MSG_DEBUG, "Schannel: SEC_E_WRONG_PRINCIPAL");
433 case SEC_E_INTERNAL_ERROR:
434 wpa_printf(MSG_DEBUG, "Schannel: SEC_E_INTERNAL_ERROR");
438 if (FAILED(status)) {
439 wpa_printf(MSG_DEBUG, "Schannel: Handshake failed "
440 "(out_buf=%p)", out_buf);
442 global->sspi->DeleteSecurityContext(&conn->context);
446 if (inbufs[1].BufferType == SECBUFFER_EXTRA) {
447 /* TODO: Can this happen? What to do with this data? */
448 wpa_hexdump(MSG_MSGDUMP, "SChannel - Leftover data",
449 inbufs[1].pvBuffer, inbufs[1].cbBuffer);
450 global->sspi->FreeContextBuffer(inbufs[1].pvBuffer);
451 inbufs[1].pvBuffer = NULL;
458 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
459 struct tls_connection *conn,
460 const struct wpabuf *in_data,
461 struct wpabuf **appl_data)
467 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
468 struct tls_connection *conn,
469 const struct wpabuf *in_data)
471 struct tls_global *global = tls_ctx;
472 SECURITY_STATUS status;
475 SecPkgContext_StreamSizes sizes;
479 status = global->sspi->QueryContextAttributes(&conn->context,
480 SECPKG_ATTR_STREAM_SIZES,
482 if (status != SEC_E_OK) {
483 wpa_printf(MSG_DEBUG, "%s: QueryContextAttributes failed",
487 wpa_printf(MSG_DEBUG, "%s: Stream sizes: header=%u trailer=%u",
489 (unsigned int) sizes.cbHeader,
490 (unsigned int) sizes.cbTrailer);
492 out = wpabuf_alloc(sizes.cbHeader + wpabuf_len(in_data) +
495 os_memset(&bufs, 0, sizeof(bufs));
496 bufs[0].pvBuffer = wpabuf_put(out, sizes.cbHeader);
497 bufs[0].cbBuffer = sizes.cbHeader;
498 bufs[0].BufferType = SECBUFFER_STREAM_HEADER;
500 bufs[1].pvBuffer = wpabuf_put(out, 0);
501 wpabuf_put_buf(out, in_data);
502 bufs[1].cbBuffer = wpabuf_len(in_data);
503 bufs[1].BufferType = SECBUFFER_DATA;
505 bufs[2].pvBuffer = wpabuf_put(out, sizes.cbTrailer);
506 bufs[2].cbBuffer = sizes.cbTrailer;
507 bufs[2].BufferType = SECBUFFER_STREAM_TRAILER;
509 buf.ulVersion = SECBUFFER_VERSION;
513 status = global->sspi->EncryptMessage(&conn->context, 0, &buf, 0);
515 wpa_printf(MSG_MSGDUMP, "Schannel: EncryptMessage -> "
516 "status=%d len[0]=%d type[0]=%d len[1]=%d type[1]=%d "
517 "len[2]=%d type[2]=%d",
519 (int) bufs[0].cbBuffer, (int) bufs[0].BufferType,
520 (int) bufs[1].cbBuffer, (int) bufs[1].BufferType,
521 (int) bufs[2].cbBuffer, (int) bufs[2].BufferType);
522 wpa_printf(MSG_MSGDUMP, "Schannel: EncryptMessage pointers: "
523 "out_data=%p bufs %p %p %p",
524 wpabuf_head(out), bufs[0].pvBuffer, bufs[1].pvBuffer,
527 for (i = 0; i < 3; i++) {
528 if (bufs[i].pvBuffer && bufs[i].BufferType != SECBUFFER_EMPTY)
530 wpa_hexdump(MSG_MSGDUMP, "SChannel: bufs",
531 bufs[i].pvBuffer, bufs[i].cbBuffer);
535 if (status == SEC_E_OK) {
536 wpa_printf(MSG_DEBUG, "%s: SEC_E_OK", __func__);
537 wpa_hexdump_buf_key(MSG_MSGDUMP, "Schannel: Encrypted data "
538 "from EncryptMessage", out);
542 wpa_printf(MSG_DEBUG, "%s: Failed - status=%d",
543 __func__, (int) status);
549 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
550 struct tls_connection *conn,
551 const struct wpabuf *in_data)
553 struct tls_global *global = tls_ctx;
554 SECURITY_STATUS status;
558 struct wpabuf *out, *tmp;
560 wpa_hexdump_buf(MSG_MSGDUMP,
561 "Schannel: Encrypted data to DecryptMessage", in_data);
562 os_memset(&bufs, 0, sizeof(bufs));
563 tmp = wpabuf_dup(in_data);
566 bufs[0].pvBuffer = wpabuf_mhead(tmp);
567 bufs[0].cbBuffer = wpabuf_len(in_data);
568 bufs[0].BufferType = SECBUFFER_DATA;
570 bufs[1].BufferType = SECBUFFER_EMPTY;
571 bufs[2].BufferType = SECBUFFER_EMPTY;
572 bufs[3].BufferType = SECBUFFER_EMPTY;
574 buf.ulVersion = SECBUFFER_VERSION;
578 status = global->sspi->DecryptMessage(&conn->context, &buf, 0,
580 wpa_printf(MSG_MSGDUMP, "Schannel: DecryptMessage -> "
581 "status=%d len[0]=%d type[0]=%d len[1]=%d type[1]=%d "
582 "len[2]=%d type[2]=%d len[3]=%d type[3]=%d",
584 (int) bufs[0].cbBuffer, (int) bufs[0].BufferType,
585 (int) bufs[1].cbBuffer, (int) bufs[1].BufferType,
586 (int) bufs[2].cbBuffer, (int) bufs[2].BufferType,
587 (int) bufs[3].cbBuffer, (int) bufs[3].BufferType);
588 wpa_printf(MSG_MSGDUMP, "Schannel: DecryptMessage pointers: "
589 "out_data=%p bufs %p %p %p %p",
590 wpabuf_head(tmp), bufs[0].pvBuffer, bufs[1].pvBuffer,
591 bufs[2].pvBuffer, bufs[3].pvBuffer);
594 case SEC_E_INCOMPLETE_MESSAGE:
595 wpa_printf(MSG_DEBUG, "%s: SEC_E_INCOMPLETE_MESSAGE",
599 wpa_printf(MSG_DEBUG, "%s: SEC_E_OK", __func__);
600 for (i = 0; i < 4; i++) {
601 if (bufs[i].BufferType == SECBUFFER_DATA)
605 wpa_printf(MSG_DEBUG, "%s: No output data from "
606 "DecryptMessage", __func__);
610 wpa_hexdump_key(MSG_MSGDUMP, "Schannel: Decrypted data from "
612 bufs[i].pvBuffer, bufs[i].cbBuffer);
613 out = wpabuf_alloc_copy(bufs[i].pvBuffer, bufs[i].cbBuffer);
618 wpa_printf(MSG_DEBUG, "%s: Failed - status=%d",
619 __func__, (int) status);
625 int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
631 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
638 int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
639 char *buf, size_t buflen)
645 int tls_connection_enable_workaround(void *ssl_ctx,
646 struct tls_connection *conn)
652 int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
653 int ext_type, const u8 *data,
660 int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
668 int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
672 return conn->read_alerts;
676 int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
680 return conn->write_alerts;
684 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
685 const struct tls_connection_params *params)
687 struct tls_global *global = tls_ctx;
689 SECURITY_STATUS status;
695 if (global->my_cert_store == NULL &&
696 (global->my_cert_store = CertOpenSystemStore(0, TEXT("MY"))) ==
698 wpa_printf(MSG_ERROR, "%s: CertOpenSystemStore failed - 0x%x",
699 __func__, (unsigned int) GetLastError());
703 os_memset(&conn->schannel_cred, 0, sizeof(conn->schannel_cred));
704 conn->schannel_cred.dwVersion = SCHANNEL_CRED_VERSION;
705 conn->schannel_cred.grbitEnabledProtocols = SP_PROT_TLS1;
706 algs[0] = CALG_RSA_KEYX;
707 conn->schannel_cred.cSupportedAlgs = 1;
708 conn->schannel_cred.palgSupportedAlgs = algs;
709 conn->schannel_cred.dwFlags |= SCH_CRED_NO_DEFAULT_CREDS;
711 status = global->sspi->AcquireCredentialsHandleW(
712 NULL, UNISP_NAME_W, SECPKG_CRED_OUTBOUND, NULL,
713 &conn->schannel_cred, NULL, NULL, &conn->creds, &ts_expiry);
715 status = global->sspi->AcquireCredentialsHandleA(
716 NULL, UNISP_NAME_A, SECPKG_CRED_OUTBOUND, NULL,
717 &conn->schannel_cred, NULL, NULL, &conn->creds, &ts_expiry);
719 if (status != SEC_E_OK) {
720 wpa_printf(MSG_DEBUG, "%s: AcquireCredentialsHandleA failed - "
721 "0x%x", __func__, (unsigned int) status);
729 unsigned int tls_capabilities(void *tls_ctx)