2 * EAP-TNC - TNCC (IF-IMC and IF-TNCCS)
3 * Copyright (c) 2007, 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 #ifndef CONFIG_NATIVE_WINDOWS
12 #endif /* CONFIG_NATIVE_WINDOWS */
16 #include "common/tnc.h"
18 #include "eap_common/eap_tlv_common.h"
19 #include "eap_common/eap_defs.h"
29 #ifndef TNC_CONFIG_FILE
30 #define TNC_CONFIG_FILE "/etc/tnc_config"
31 #endif /* TNC_CONFIG_FILE */
32 #define TNC_WINREG_PATH TEXT("SOFTWARE\\Trusted Computing Group\\TNC\\IMCs")
33 #define IF_TNCCS_START \
34 "<?xml version=\"1.0\"?>\n" \
35 "<TNCCS-Batch BatchId=\"%d\" Recipient=\"TNCS\" " \
36 "xmlns=\"http://www.trustedcomputinggroup.org/IWG/TNC/1_0/IF_TNCCS#\" " \
37 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \
38 "xsi:schemaLocation=\"http://www.trustedcomputinggroup.org/IWG/TNC/1_0/" \
39 "IF_TNCCS# https://www.trustedcomputinggroup.org/XML/SCHEMA/TNCCS_1.0.xsd\">\n"
40 #define IF_TNCCS_END "\n</TNCCS-Batch>"
44 /* IF-TNCCS-SOH - SSoH and SSoHR Attributes */
46 SSOH_MS_MACHINE_INVENTORY = 1,
47 SSOH_MS_QUARANTINE_STATE = 2,
48 SSOH_MS_PACKET_INFO = 3,
49 SSOH_MS_SYSTEMGENERATED_IDS = 4,
50 SSOH_MS_MACHINENAME = 5,
51 SSOH_MS_CORRELATIONID = 6,
52 SSOH_MS_INSTALLED_SHVS = 7,
53 SSOH_MS_MACHINE_INVENTORY_EX = 8
57 struct tnc_if_imc *next;
60 void *dlhandle; /* from dlopen() */
62 TNC_ConnectionID connectionID;
63 TNC_MessageTypeList supported_types;
64 size_t num_supported_types;
68 /* Functions implemented by IMCs (with TNC_IMC_ prefix) */
69 TNC_Result (*Initialize)(
71 TNC_Version minVersion,
72 TNC_Version maxVersion,
73 TNC_Version *pOutActualVersion);
74 TNC_Result (*NotifyConnectionChange)(
76 TNC_ConnectionID connectionID,
77 TNC_ConnectionState newState);
78 TNC_Result (*BeginHandshake)(
80 TNC_ConnectionID connectionID);
81 TNC_Result (*ReceiveMessage)(
83 TNC_ConnectionID connectionID,
84 TNC_BufferReference messageBuffer,
85 TNC_UInt32 messageLength,
86 TNC_MessageType messageType);
87 TNC_Result (*BatchEnding)(
89 TNC_ConnectionID connectionID);
90 TNC_Result (*Terminate)(TNC_IMCID imcID);
91 TNC_Result (*ProvideBindFunction)(
93 TNC_TNCC_BindFunctionPointer bindFunction);
97 struct tnc_if_imc *imc;
98 unsigned int last_batchid;
101 #define TNC_MAX_IMC_ID 10
102 static struct tnc_if_imc *tnc_imc[TNC_MAX_IMC_ID] = { NULL };
105 /* TNCC functions that IMCs can call */
107 static TNC_Result TNC_TNCC_ReportMessageTypes(
109 TNC_MessageTypeList supportedTypes,
110 TNC_UInt32 typeCount)
113 struct tnc_if_imc *imc;
115 wpa_printf(MSG_DEBUG, "TNC: TNC_TNCC_ReportMessageTypes(imcID=%lu "
117 (unsigned long) imcID, (unsigned long) typeCount);
119 for (i = 0; i < typeCount; i++) {
120 wpa_printf(MSG_DEBUG, "TNC: supportedTypes[%lu] = %lu",
121 i, supportedTypes[i]);
124 if (imcID >= TNC_MAX_IMC_ID || tnc_imc[imcID] == NULL)
125 return TNC_RESULT_INVALID_PARAMETER;
127 imc = tnc_imc[imcID];
128 os_free(imc->supported_types);
129 imc->supported_types = os_memdup(supportedTypes,
130 typeCount * sizeof(TNC_MessageType));
131 if (imc->supported_types == NULL)
132 return TNC_RESULT_FATAL;
133 imc->num_supported_types = typeCount;
135 return TNC_RESULT_SUCCESS;
139 static TNC_Result TNC_TNCC_SendMessage(
141 TNC_ConnectionID connectionID,
142 TNC_BufferReference message,
143 TNC_UInt32 messageLength,
144 TNC_MessageType messageType)
146 struct tnc_if_imc *imc;
150 wpa_printf(MSG_DEBUG, "TNC: TNC_TNCC_SendMessage(imcID=%lu "
151 "connectionID=%lu messageType=%lu)",
152 imcID, connectionID, messageType);
153 wpa_hexdump_ascii(MSG_DEBUG, "TNC: TNC_TNCC_SendMessage",
154 message, messageLength);
156 if (imcID >= TNC_MAX_IMC_ID || tnc_imc[imcID] == NULL)
157 return TNC_RESULT_INVALID_PARAMETER;
159 b64 = base64_encode(message, messageLength, &b64len);
161 return TNC_RESULT_FATAL;
163 imc = tnc_imc[imcID];
164 os_free(imc->imc_send);
165 imc->imc_send_len = 0;
166 imc->imc_send = os_zalloc(b64len + 100);
167 if (imc->imc_send == NULL) {
169 return TNC_RESULT_OTHER;
173 os_snprintf((char *) imc->imc_send, b64len + 100,
174 "<IMC-IMV-Message><Type>%08X</Type>"
175 "<Base64>%s</Base64></IMC-IMV-Message>",
176 (unsigned int) messageType, b64);
180 return TNC_RESULT_SUCCESS;
184 static TNC_Result TNC_TNCC_RequestHandshakeRetry(
186 TNC_ConnectionID connectionID,
187 TNC_RetryReason reason)
189 wpa_printf(MSG_DEBUG, "TNC: TNC_TNCC_RequestHandshakeRetry");
191 if (imcID >= TNC_MAX_IMC_ID || tnc_imc[imcID] == NULL)
192 return TNC_RESULT_INVALID_PARAMETER;
195 * TODO: trigger a call to eapol_sm_request_reauth(). This would
196 * require that the IMC continues to be loaded in memory afer
200 return TNC_RESULT_SUCCESS;
204 static TNC_Result TNC_9048_LogMessage(TNC_IMCID imcID, TNC_UInt32 severity,
207 wpa_printf(MSG_DEBUG, "TNC: TNC_9048_LogMessage(imcID=%lu "
208 "severity==%lu message='%s')",
209 imcID, severity, message);
210 return TNC_RESULT_SUCCESS;
214 static TNC_Result TNC_9048_UserMessage(TNC_IMCID imcID,
215 TNC_ConnectionID connectionID,
218 wpa_printf(MSG_DEBUG, "TNC: TNC_9048_UserMessage(imcID=%lu "
219 "connectionID==%lu message='%s')",
220 imcID, connectionID, message);
221 return TNC_RESULT_SUCCESS;
225 static TNC_Result TNC_TNCC_BindFunction(
228 void **pOutfunctionPointer)
230 wpa_printf(MSG_DEBUG, "TNC: TNC_TNCC_BindFunction(imcID=%lu, "
231 "functionName='%s')", (unsigned long) imcID, functionName);
233 if (imcID >= TNC_MAX_IMC_ID || tnc_imc[imcID] == NULL)
234 return TNC_RESULT_INVALID_PARAMETER;
236 if (pOutfunctionPointer == NULL)
237 return TNC_RESULT_INVALID_PARAMETER;
239 if (os_strcmp(functionName, "TNC_TNCC_ReportMessageTypes") == 0)
240 *pOutfunctionPointer = TNC_TNCC_ReportMessageTypes;
241 else if (os_strcmp(functionName, "TNC_TNCC_SendMessage") == 0)
242 *pOutfunctionPointer = TNC_TNCC_SendMessage;
243 else if (os_strcmp(functionName, "TNC_TNCC_RequestHandshakeRetry") ==
245 *pOutfunctionPointer = TNC_TNCC_RequestHandshakeRetry;
246 else if (os_strcmp(functionName, "TNC_9048_LogMessage") == 0)
247 *pOutfunctionPointer = TNC_9048_LogMessage;
248 else if (os_strcmp(functionName, "TNC_9048_UserMessage") == 0)
249 *pOutfunctionPointer = TNC_9048_UserMessage;
251 *pOutfunctionPointer = NULL;
253 return TNC_RESULT_SUCCESS;
257 static void * tncc_get_sym(void *handle, char *func)
261 #ifdef CONFIG_NATIVE_WINDOWS
263 fptr = GetProcAddressA(handle, func);
264 #else /* _WIN32_WCE */
265 fptr = GetProcAddress(handle, func);
266 #endif /* _WIN32_WCE */
267 #else /* CONFIG_NATIVE_WINDOWS */
268 fptr = dlsym(handle, func);
269 #endif /* CONFIG_NATIVE_WINDOWS */
275 static int tncc_imc_resolve_funcs(struct tnc_if_imc *imc)
277 void *handle = imc->dlhandle;
279 /* Mandatory IMC functions */
280 imc->Initialize = tncc_get_sym(handle, "TNC_IMC_Initialize");
281 if (imc->Initialize == NULL) {
282 wpa_printf(MSG_ERROR, "TNC: IMC does not export "
283 "TNC_IMC_Initialize");
287 imc->BeginHandshake = tncc_get_sym(handle, "TNC_IMC_BeginHandshake");
288 if (imc->BeginHandshake == NULL) {
289 wpa_printf(MSG_ERROR, "TNC: IMC does not export "
290 "TNC_IMC_BeginHandshake");
294 imc->ProvideBindFunction =
295 tncc_get_sym(handle, "TNC_IMC_ProvideBindFunction");
296 if (imc->ProvideBindFunction == NULL) {
297 wpa_printf(MSG_ERROR, "TNC: IMC does not export "
298 "TNC_IMC_ProvideBindFunction");
302 /* Optional IMC functions */
303 imc->NotifyConnectionChange =
304 tncc_get_sym(handle, "TNC_IMC_NotifyConnectionChange");
305 imc->ReceiveMessage = tncc_get_sym(handle, "TNC_IMC_ReceiveMessage");
306 imc->BatchEnding = tncc_get_sym(handle, "TNC_IMC_BatchEnding");
307 imc->Terminate = tncc_get_sym(handle, "TNC_IMC_Terminate");
313 static int tncc_imc_initialize(struct tnc_if_imc *imc)
318 wpa_printf(MSG_DEBUG, "TNC: Calling TNC_IMC_Initialize for IMC '%s'",
320 res = imc->Initialize(imc->imcID, TNC_IFIMC_VERSION_1,
321 TNC_IFIMC_VERSION_1, &imc_ver);
322 wpa_printf(MSG_DEBUG, "TNC: TNC_IMC_Initialize: res=%lu imc_ver=%lu",
323 (unsigned long) res, (unsigned long) imc_ver);
325 return res == TNC_RESULT_SUCCESS ? 0 : -1;
329 static int tncc_imc_terminate(struct tnc_if_imc *imc)
333 if (imc->Terminate == NULL)
336 wpa_printf(MSG_DEBUG, "TNC: Calling TNC_IMC_Terminate for IMC '%s'",
338 res = imc->Terminate(imc->imcID);
339 wpa_printf(MSG_DEBUG, "TNC: TNC_IMC_Terminate: %lu",
340 (unsigned long) res);
342 return res == TNC_RESULT_SUCCESS ? 0 : -1;
346 static int tncc_imc_provide_bind_function(struct tnc_if_imc *imc)
350 wpa_printf(MSG_DEBUG, "TNC: Calling TNC_IMC_ProvideBindFunction for "
351 "IMC '%s'", imc->name);
352 res = imc->ProvideBindFunction(imc->imcID, TNC_TNCC_BindFunction);
353 wpa_printf(MSG_DEBUG, "TNC: TNC_IMC_ProvideBindFunction: res=%lu",
354 (unsigned long) res);
356 return res == TNC_RESULT_SUCCESS ? 0 : -1;
360 static int tncc_imc_notify_connection_change(struct tnc_if_imc *imc,
361 TNC_ConnectionState state)
365 if (imc->NotifyConnectionChange == NULL)
368 wpa_printf(MSG_DEBUG, "TNC: Calling TNC_IMC_NotifyConnectionChange(%d)"
369 " for IMC '%s'", (int) state, imc->name);
370 res = imc->NotifyConnectionChange(imc->imcID, imc->connectionID,
372 wpa_printf(MSG_DEBUG, "TNC: TNC_IMC_NotifyConnectionChange: %lu",
373 (unsigned long) res);
375 return res == TNC_RESULT_SUCCESS ? 0 : -1;
379 static int tncc_imc_begin_handshake(struct tnc_if_imc *imc)
383 wpa_printf(MSG_DEBUG, "TNC: Calling TNC_IMC_BeginHandshake for IMC "
385 res = imc->BeginHandshake(imc->imcID, imc->connectionID);
386 wpa_printf(MSG_DEBUG, "TNC: TNC_IMC_BeginHandshake: %lu",
387 (unsigned long) res);
389 return res == TNC_RESULT_SUCCESS ? 0 : -1;
393 static int tncc_load_imc(struct tnc_if_imc *imc)
395 if (imc->path == NULL) {
396 wpa_printf(MSG_DEBUG, "TNC: No IMC configured");
400 wpa_printf(MSG_DEBUG, "TNC: Opening IMC: %s (%s)",
401 imc->name, imc->path);
402 #ifdef CONFIG_NATIVE_WINDOWS
405 TCHAR *lib = wpa_strdup_tchar(imc->path);
408 imc->dlhandle = LoadLibrary(lib);
412 imc->dlhandle = LoadLibrary(imc->path);
414 if (imc->dlhandle == NULL) {
415 wpa_printf(MSG_ERROR, "TNC: Failed to open IMC '%s' (%s): %d",
416 imc->name, imc->path, (int) GetLastError());
419 #else /* CONFIG_NATIVE_WINDOWS */
420 imc->dlhandle = dlopen(imc->path, RTLD_LAZY);
421 if (imc->dlhandle == NULL) {
422 wpa_printf(MSG_ERROR, "TNC: Failed to open IMC '%s' (%s): %s",
423 imc->name, imc->path, dlerror());
426 #endif /* CONFIG_NATIVE_WINDOWS */
428 if (tncc_imc_resolve_funcs(imc) < 0) {
429 wpa_printf(MSG_ERROR, "TNC: Failed to resolve IMC functions");
433 if (tncc_imc_initialize(imc) < 0 ||
434 tncc_imc_provide_bind_function(imc) < 0) {
435 wpa_printf(MSG_ERROR, "TNC: Failed to initialize IMC");
443 static void tncc_unload_imc(struct tnc_if_imc *imc)
445 tncc_imc_terminate(imc);
446 tnc_imc[imc->imcID] = NULL;
449 #ifdef CONFIG_NATIVE_WINDOWS
450 FreeLibrary(imc->dlhandle);
451 #else /* CONFIG_NATIVE_WINDOWS */
452 dlclose(imc->dlhandle);
453 #endif /* CONFIG_NATIVE_WINDOWS */
457 os_free(imc->supported_types);
458 os_free(imc->imc_send);
462 static int tncc_supported_type(struct tnc_if_imc *imc, unsigned int type)
465 unsigned int vendor, subtype;
467 if (imc == NULL || imc->supported_types == NULL)
471 subtype = type & 0xff;
473 for (i = 0; i < imc->num_supported_types; i++) {
474 unsigned int svendor, ssubtype;
475 svendor = imc->supported_types[i] >> 8;
476 ssubtype = imc->supported_types[i] & 0xff;
477 if ((vendor == svendor || svendor == TNC_VENDORID_ANY) &&
478 (subtype == ssubtype || ssubtype == TNC_SUBTYPE_ANY))
486 static void tncc_send_to_imcs(struct tncc_data *tncc, unsigned int type,
487 const u8 *msg, size_t len)
489 struct tnc_if_imc *imc;
492 wpa_hexdump_ascii(MSG_MSGDUMP, "TNC: Message to IMC(s)", msg, len);
494 for (imc = tncc->imc; imc; imc = imc->next) {
495 if (imc->ReceiveMessage == NULL ||
496 !tncc_supported_type(imc, type))
499 wpa_printf(MSG_DEBUG, "TNC: Call ReceiveMessage for IMC '%s'",
501 res = imc->ReceiveMessage(imc->imcID, imc->connectionID,
502 (TNC_BufferReference) msg, len,
504 wpa_printf(MSG_DEBUG, "TNC: ReceiveMessage: %lu",
505 (unsigned long) res);
510 void tncc_init_connection(struct tncc_data *tncc)
512 struct tnc_if_imc *imc;
514 for (imc = tncc->imc; imc; imc = imc->next) {
515 tncc_imc_notify_connection_change(
516 imc, TNC_CONNECTION_STATE_CREATE);
517 tncc_imc_notify_connection_change(
518 imc, TNC_CONNECTION_STATE_HANDSHAKE);
520 os_free(imc->imc_send);
521 imc->imc_send = NULL;
522 imc->imc_send_len = 0;
524 tncc_imc_begin_handshake(imc);
529 size_t tncc_total_send_len(struct tncc_data *tncc)
531 struct tnc_if_imc *imc;
534 for (imc = tncc->imc; imc; imc = imc->next)
535 len += imc->imc_send_len;
540 u8 * tncc_copy_send_buf(struct tncc_data *tncc, u8 *pos)
542 struct tnc_if_imc *imc;
544 for (imc = tncc->imc; imc; imc = imc->next) {
545 if (imc->imc_send == NULL)
548 os_memcpy(pos, imc->imc_send, imc->imc_send_len);
549 pos += imc->imc_send_len;
550 os_free(imc->imc_send);
551 imc->imc_send = NULL;
552 imc->imc_send_len = 0;
559 char * tncc_if_tnccs_start(struct tncc_data *tncc)
561 char *buf = os_malloc(1000);
564 tncc->last_batchid++;
565 os_snprintf(buf, 1000, IF_TNCCS_START, tncc->last_batchid);
570 char * tncc_if_tnccs_end(void)
572 char *buf = os_malloc(100);
575 os_snprintf(buf, 100, IF_TNCCS_END);
580 static void tncc_notify_recommendation(struct tncc_data *tncc,
581 enum tncc_process_res res)
583 TNC_ConnectionState state;
584 struct tnc_if_imc *imc;
587 case TNCCS_RECOMMENDATION_ALLOW:
588 state = TNC_CONNECTION_STATE_ACCESS_ALLOWED;
590 case TNCCS_RECOMMENDATION_NONE:
591 state = TNC_CONNECTION_STATE_ACCESS_NONE;
593 case TNCCS_RECOMMENDATION_ISOLATE:
594 state = TNC_CONNECTION_STATE_ACCESS_ISOLATED;
597 state = TNC_CONNECTION_STATE_ACCESS_NONE;
601 for (imc = tncc->imc; imc; imc = imc->next)
602 tncc_imc_notify_connection_change(imc, state);
606 static int tncc_get_type(char *start, unsigned int *type)
608 char *pos = os_strstr(start, "<Type>");
612 *type = strtoul(pos, NULL, 16);
617 static unsigned char * tncc_get_base64(char *start, size_t *decoded_len)
620 unsigned char *decoded;
622 pos = os_strstr(start, "<Base64>");
627 pos2 = os_strstr(pos, "</Base64>");
632 decoded = base64_decode((unsigned char *) pos, os_strlen(pos),
635 if (decoded == NULL) {
636 wpa_printf(MSG_DEBUG, "TNC: Failed to decode Base64 data");
643 static enum tncc_process_res tncc_get_recommendation(char *start)
645 char *pos, *pos2, saved;
648 pos = os_strstr(start, "<TNCCS-Recommendation ");
650 return TNCCS_RECOMMENDATION_ERROR;
653 pos = os_strstr(pos, " type=");
655 return TNCCS_RECOMMENDATION_ERROR;
662 while (*pos2 != '\0' && *pos2 != '"' && *pos2 != '>')
666 return TNCCS_RECOMMENDATION_ERROR;
670 wpa_printf(MSG_DEBUG, "TNC: TNCCS-Recommendation: '%s'", pos);
672 recom = TNCCS_RECOMMENDATION_ERROR;
673 if (os_strcmp(pos, "allow") == 0)
674 recom = TNCCS_RECOMMENDATION_ALLOW;
675 else if (os_strcmp(pos, "none") == 0)
676 recom = TNCCS_RECOMMENDATION_NONE;
677 else if (os_strcmp(pos, "isolate") == 0)
678 recom = TNCCS_RECOMMENDATION_ISOLATE;
686 enum tncc_process_res tncc_process_if_tnccs(struct tncc_data *tncc,
687 const u8 *msg, size_t len)
689 char *buf, *start, *end, *pos, *pos2, *payload;
690 unsigned int batch_id;
691 unsigned char *decoded;
693 enum tncc_process_res res = TNCCS_PROCESS_OK_NO_RECOMMENDATION;
694 int recommendation_msg = 0;
696 wpa_hexdump_ascii(MSG_MSGDUMP, "TNC: Received IF-TNCCS message",
698 buf = dup_binstr(msg, len);
700 return TNCCS_PROCESS_ERROR;
702 start = os_strstr(buf, "<TNCCS-Batch ");
703 end = os_strstr(buf, "</TNCCS-Batch>");
704 if (start == NULL || end == NULL || start > end) {
706 return TNCCS_PROCESS_ERROR;
710 while (*start == ' ')
714 pos = os_strstr(start, "BatchId=");
717 return TNCCS_PROCESS_ERROR;
723 batch_id = atoi(pos);
724 wpa_printf(MSG_DEBUG, "TNC: Received IF-TNCCS BatchId=%u",
726 if (batch_id != tncc->last_batchid + 1) {
727 wpa_printf(MSG_DEBUG, "TNC: Unexpected IF-TNCCS BatchId "
729 batch_id, tncc->last_batchid + 1);
731 return TNCCS_PROCESS_ERROR;
733 tncc->last_batchid = batch_id;
735 while (*pos != '\0' && *pos != '>')
739 return TNCCS_PROCESS_ERROR;
746 * <Type>01234567</Type>
747 * <Base64>foo==</Base64>
755 pos = os_strstr(start, "<IMC-IMV-Message>");
759 end = os_strstr(start, "</IMC-IMV-Message>");
766 if (tncc_get_type(start, &type) < 0) {
771 wpa_printf(MSG_DEBUG, "TNC: IMC-IMV-Message Type 0x%x", type);
773 decoded = tncc_get_base64(start, &decoded_len);
774 if (decoded == NULL) {
780 tncc_send_to_imcs(tncc, type, decoded, decoded_len);
788 * <TNCC-TNCS-Message>
789 * <Type>01234567</Type>
790 * <XML><TNCCS-Foo type="foo"></TNCCS-Foo></XML>
791 * <Base64>foo==</Base64>
792 * </TNCC-TNCS-Message>
798 char *xml, *xmlend, *endpos;
800 pos = os_strstr(start, "<TNCC-TNCS-Message>");
804 end = os_strstr(start, "</TNCC-TNCS-Message>");
811 if (tncc_get_type(start, &type) < 0) {
816 wpa_printf(MSG_DEBUG, "TNC: TNCC-TNCS-Message Type 0x%x",
823 pos = os_strstr(start, "<XML>");
826 pos2 = os_strstr(pos, "</XML>");
835 decoded = tncc_get_base64(start, &decoded_len);
836 if (decoded == NULL) {
844 wpa_hexdump_ascii(MSG_MSGDUMP,
845 "TNC: TNCC-TNCS-Message Base64",
846 decoded, decoded_len);
851 wpa_hexdump_ascii(MSG_MSGDUMP,
852 "TNC: TNCC-TNCS-Message XML",
853 (unsigned char *) xml,
857 if (type == TNC_TNCCS_RECOMMENDATION && xml) {
859 * <TNCCS-Recommendation type="allow">
860 * </TNCCS-Recommendation>
863 res = tncc_get_recommendation(xml);
865 recommendation_msg = 1;
873 if (recommendation_msg)
874 tncc_notify_recommendation(tncc, res);
880 #ifdef CONFIG_NATIVE_WINDOWS
881 static int tncc_read_config_reg(struct tncc_data *tncc, HKEY hive)
886 struct tnc_if_imc *imc, *last;
890 while (last && last->next)
893 ret = RegOpenKeyEx(hive, TNC_WINREG_PATH, 0, KEY_ENUMERATE_SUB_KEYS,
895 if (ret != ERROR_SUCCESS)
899 TCHAR name[255], *val;
900 DWORD namelen, buflen;
903 ret = RegEnumKeyEx(hk, i, name, &namelen, NULL, NULL, NULL,
906 if (ret == ERROR_NO_MORE_ITEMS)
909 if (ret != ERROR_SUCCESS) {
910 wpa_printf(MSG_DEBUG, "TNC: RegEnumKeyEx failed: 0x%x",
917 name[namelen] = '\0';
919 wpa_printf(MSG_DEBUG, "TNC: IMC '" TSTR "'", name);
921 ret = RegOpenKeyEx(hk, name, 0, KEY_QUERY_VALUE, &hk2);
922 if (ret != ERROR_SUCCESS) {
923 wpa_printf(MSG_DEBUG, "Could not open IMC key '" TSTR
928 ret = RegQueryValueEx(hk2, TEXT("Path"), NULL, NULL, NULL,
930 if (ret != ERROR_SUCCESS) {
931 wpa_printf(MSG_DEBUG, "TNC: Could not read Path from "
932 "IMC key '" TSTR "'", name);
937 val = os_malloc(buflen);
943 ret = RegQueryValueEx(hk2, TEXT("Path"), NULL, NULL,
944 (LPBYTE) val, &buflen);
945 if (ret != ERROR_SUCCESS) {
953 wpa_unicode2ascii_inplace(val);
954 wpa_printf(MSG_DEBUG, "TNC: IMC Path '%s'", (char *) val);
956 for (j = 0; j < TNC_MAX_IMC_ID; j++) {
957 if (tnc_imc[j] == NULL)
960 if (j >= TNC_MAX_IMC_ID) {
961 wpa_printf(MSG_DEBUG, "TNC: Too many IMCs");
966 imc = os_zalloc(sizeof(*imc));
974 wpa_unicode2ascii_inplace(name);
975 imc->name = os_strdup((char *) name);
976 imc->path = os_strdup((char *) val);
986 tnc_imc[imc->imcID] = imc;
995 static int tncc_read_config(struct tncc_data *tncc)
997 if (tncc_read_config_reg(tncc, HKEY_LOCAL_MACHINE) < 0 ||
998 tncc_read_config_reg(tncc, HKEY_CURRENT_USER) < 0)
1003 #else /* CONFIG_NATIVE_WINDOWS */
1005 static struct tnc_if_imc * tncc_parse_imc(char *start, char *end, int *error)
1007 struct tnc_if_imc *imc;
1011 for (i = 0; i < TNC_MAX_IMC_ID; i++) {
1012 if (tnc_imc[i] == NULL)
1015 if (i >= TNC_MAX_IMC_ID) {
1016 wpa_printf(MSG_DEBUG, "TNC: Too many IMCs");
1020 imc = os_zalloc(sizeof(*imc));
1029 wpa_printf(MSG_DEBUG, "TNC: Configured IMC: %s", pos);
1030 if (pos + 1 >= end || *pos != '"') {
1031 wpa_printf(MSG_ERROR, "TNC: Ignoring invalid IMC line '%s' "
1032 "(no starting quotation mark)", start);
1039 while (pos2 < end && *pos2 != '"')
1042 wpa_printf(MSG_ERROR, "TNC: Ignoring invalid IMC line '%s' "
1043 "(no ending quotation mark)", start);
1048 wpa_printf(MSG_DEBUG, "TNC: Name: '%s'", pos);
1049 imc->name = os_strdup(pos);
1052 if (pos >= end || *pos != ' ') {
1053 wpa_printf(MSG_ERROR, "TNC: Ignoring invalid IMC line '%s' "
1054 "(no space after name)", start);
1061 wpa_printf(MSG_DEBUG, "TNC: IMC file: '%s'", pos);
1062 imc->path = os_strdup(pos);
1063 tnc_imc[imc->imcID] = imc;
1069 static int tncc_read_config(struct tncc_data *tncc)
1071 char *config, *end, *pos, *line_end;
1073 struct tnc_if_imc *imc, *last;
1077 config = os_readfile(TNC_CONFIG_FILE, &config_len);
1078 if (config == NULL) {
1079 wpa_printf(MSG_ERROR, "TNC: Could not open TNC configuration "
1080 "file '%s'", TNC_CONFIG_FILE);
1084 end = config + config_len;
1085 for (pos = config; pos < end; pos = line_end + 1) {
1087 while (*line_end != '\n' && *line_end != '\r' &&
1092 if (os_strncmp(pos, "IMC ", 4) == 0) {
1095 imc = tncc_parse_imc(pos + 4, line_end, &error);
1115 #endif /* CONFIG_NATIVE_WINDOWS */
1118 struct tncc_data * tncc_init(void)
1120 struct tncc_data *tncc;
1121 struct tnc_if_imc *imc;
1123 tncc = os_zalloc(sizeof(*tncc));
1128 * move loading and Initialize() to a location that is not
1129 * re-initialized for every EAP-TNC session (?)
1132 if (tncc_read_config(tncc) < 0) {
1133 wpa_printf(MSG_ERROR, "TNC: Failed to read TNC configuration");
1137 for (imc = tncc->imc; imc; imc = imc->next) {
1138 if (tncc_load_imc(imc)) {
1139 wpa_printf(MSG_ERROR, "TNC: Failed to load IMC '%s'",
1153 void tncc_deinit(struct tncc_data *tncc)
1155 struct tnc_if_imc *imc, *prev;
1159 tncc_unload_imc(imc);
1170 static struct wpabuf * tncc_build_soh(int ver)
1173 u8 *tlv_len, *tlv_len2, *outer_len, *inner_len, *ssoh_len, *end;
1174 u8 correlation_id[24];
1175 /* TODO: get correct name */
1176 char *machinename = "wpa_supplicant@w1.fi";
1178 if (os_get_random(correlation_id, sizeof(correlation_id)))
1180 wpa_hexdump(MSG_DEBUG, "TNC: SoH Correlation ID",
1181 correlation_id, sizeof(correlation_id));
1183 buf = wpabuf_alloc(200);
1187 /* Vendor-Specific TLV (Microsoft) - SoH */
1188 wpabuf_put_be16(buf, EAP_TLV_VENDOR_SPECIFIC_TLV); /* TLV Type */
1189 tlv_len = wpabuf_put(buf, 2); /* Length */
1190 wpabuf_put_be32(buf, EAP_VENDOR_MICROSOFT); /* Vendor_Id */
1191 wpabuf_put_be16(buf, 0x01); /* TLV Type - SoH TLV */
1192 tlv_len2 = wpabuf_put(buf, 2); /* Length */
1195 wpabuf_put_be16(buf, EAP_TLV_VENDOR_SPECIFIC_TLV); /* Outer Type */
1196 outer_len = wpabuf_put(buf, 2);
1197 wpabuf_put_be32(buf, EAP_VENDOR_MICROSOFT); /* IANA SMI Code */
1198 wpabuf_put_be16(buf, ver); /* Inner Type */
1199 inner_len = wpabuf_put(buf, 2);
1202 /* SoH Mode Sub-Header */
1204 wpabuf_put_be16(buf, EAP_TLV_VENDOR_SPECIFIC_TLV);
1205 wpabuf_put_be16(buf, 4 + 24 + 1 + 1); /* Length */
1206 wpabuf_put_be32(buf, EAP_VENDOR_MICROSOFT); /* IANA SMI Code */
1208 wpabuf_put_data(buf, correlation_id, sizeof(correlation_id));
1209 wpabuf_put_u8(buf, 0x01); /* Intent Flag - Request */
1210 wpabuf_put_u8(buf, 0x00); /* Content-Type Flag */
1214 /* System-Health-Id */
1215 wpabuf_put_be16(buf, 0x0002); /* Type */
1216 wpabuf_put_be16(buf, 4); /* Length */
1217 wpabuf_put_be32(buf, 79616);
1218 /* Vendor-Specific Attribute */
1219 wpabuf_put_be16(buf, EAP_TLV_VENDOR_SPECIFIC_TLV);
1220 ssoh_len = wpabuf_put(buf, 2);
1221 wpabuf_put_be32(buf, EAP_VENDOR_MICROSOFT); /* IANA SMI Code */
1223 /* MS-Packet-Info */
1224 wpabuf_put_u8(buf, SSOH_MS_PACKET_INFO);
1225 /* Note: IF-TNCCS-SOH v1.0 r8 claims this field to be:
1226 * Reserved(4 bits) r(1 bit) Vers(3 bits), but Windows XP
1227 * SP3 seems to be sending 0x11 for SSoH, i.e., r(request/response) bit
1228 * would not be in the specified location.
1229 * [MS-SOH] 4.0.2: Reserved(3 bits) r(1 bit) Vers(4 bits)
1231 wpabuf_put_u8(buf, 0x11); /* r=request, vers=1 */
1233 /* MS-Machine-Inventory */
1234 /* TODO: get correct values; 0 = not applicable for OS */
1235 wpabuf_put_u8(buf, SSOH_MS_MACHINE_INVENTORY);
1236 wpabuf_put_be32(buf, 0); /* osVersionMajor */
1237 wpabuf_put_be32(buf, 0); /* osVersionMinor */
1238 wpabuf_put_be32(buf, 0); /* osVersionBuild */
1239 wpabuf_put_be16(buf, 0); /* spVersionMajor */
1240 wpabuf_put_be16(buf, 0); /* spVersionMinor */
1241 wpabuf_put_be16(buf, 0); /* procArch */
1243 /* MS-MachineName */
1244 wpabuf_put_u8(buf, SSOH_MS_MACHINENAME);
1245 wpabuf_put_be16(buf, os_strlen(machinename) + 1);
1246 wpabuf_put_data(buf, machinename, os_strlen(machinename) + 1);
1248 /* MS-CorrelationId */
1249 wpabuf_put_u8(buf, SSOH_MS_CORRELATIONID);
1250 wpabuf_put_data(buf, correlation_id, sizeof(correlation_id));
1252 /* MS-Quarantine-State */
1253 wpabuf_put_u8(buf, SSOH_MS_QUARANTINE_STATE);
1254 wpabuf_put_be16(buf, 1); /* Flags: ExtState=0, f=0, qState=1 */
1255 wpabuf_put_be32(buf, 0xffffffff); /* ProbTime (hi) */
1256 wpabuf_put_be32(buf, 0xffffffff); /* ProbTime (lo) */
1257 wpabuf_put_be16(buf, 1); /* urlLenInBytes */
1258 wpabuf_put_u8(buf, 0); /* null termination for the url */
1260 /* MS-Machine-Inventory-Ex */
1261 wpabuf_put_u8(buf, SSOH_MS_MACHINE_INVENTORY_EX);
1262 wpabuf_put_be32(buf, 0); /* Reserved
1263 * (note: Windows XP SP3 uses 0xdecafbad) */
1264 wpabuf_put_u8(buf, 1); /* ProductType: Client */
1266 /* Update SSoH Length */
1267 end = wpabuf_put(buf, 0);
1268 WPA_PUT_BE16(ssoh_len, end - ssoh_len - 2);
1270 /* TODO: SoHReportEntry TLV (zero or more) */
1272 /* Update length fields */
1273 end = wpabuf_put(buf, 0);
1274 WPA_PUT_BE16(tlv_len, end - tlv_len - 2);
1275 WPA_PUT_BE16(tlv_len2, end - tlv_len2 - 2);
1276 WPA_PUT_BE16(outer_len, end - outer_len - 2);
1277 WPA_PUT_BE16(inner_len, end - inner_len - 2);
1283 struct wpabuf * tncc_process_soh_request(int ver, const u8 *data, size_t len)
1287 wpa_hexdump(MSG_DEBUG, "TNC: SoH Request", data, len);
1296 if (WPA_GET_BE16(pos) != EAP_TLV_VENDOR_SPECIFIC_TLV)
1301 if (WPA_GET_BE16(pos) < 8)
1306 if (WPA_GET_BE32(pos) != EAP_VENDOR_MICROSOFT)
1311 if (WPA_GET_BE16(pos) != 0x02 /* SoH request TLV */)
1314 wpa_printf(MSG_DEBUG, "TNC: SoH Request TLV received");
1316 return tncc_build_soh(2);