This patch applies some modifications in the code, in order to make TrouSerS easier to port for the windows architecture: - tcs_tsp.h and trousers_types.h have been patched in order to permit the code to be cross-compiled with MinGW and Visual Studio; - tcsem.h, tcsi_evlog.c have been modified because the field 'close' of the 'ext_log_source' structure causes a trouble when a #define with the same name is used to wrap the close() function; - in the file tcs_evlog_imaem.c the 'uint' declaration has been replaced with the more portable 'unsigned int'; - in the file tcs_aik.c, the mmap() function has been replaced with the read() to make this code portable to windows; - in svrside.c, the 'unsigned' declaration has been replaced with 'socklen_t' to avoid errors when cross-compiling; - in tcsd_conf.c the function rindex() has been replaced with the more portable strrchr(); - obj_context.c has been patched because the strlen() call inside a declaration is incompatible with the Visual Studio compiler; - in the file ps_convert.c the macro OUT has been redefined because it conficts with a same definition in the include file rpcdce.h.
Signed-off-by: Roberto Sassu <[email protected]> --- src/include/tcs_tsp.h | 4 ++++ src/include/tcsem.h | 2 +- src/include/trousers_types.h | 6 +++++- src/tcs/tcs_aik.c | 17 ++++------------- src/tcs/tcs_evlog_imaem.c | 4 ++-- src/tcs/tcsi_evlog.c | 16 ++++++++-------- src/tcsd/svrside.c | 2 +- src/tcsd/tcsd_conf.c | 18 +++++++++--------- src/tspi/obj_context.c | 38 ++++++++++++++++++++++---------------- tools/ps_convert.c | 22 +++++++++++----------- 10 files changed, 67 insertions(+), 62 deletions(-) diff --git a/src/include/tcs_tsp.h b/src/include/tcs_tsp.h index bba3258..74a0450 100644 --- a/src/include/tcs_tsp.h +++ b/src/include/tcs_tsp.h @@ -79,7 +79,11 @@ struct key_disk_cache /* XXX Get rid of this, there's no reason to set an arbitrary limit */ #define MAX_KEY_CHILDREN 10 +#ifdef __GNUC__ #define STRUCTURE_PACKING_ATTRIBUTE __attribute__((packed)) +#else +#define STRUCTURE_PACKING_ATTRIBUTE +#endif #ifdef TSS_DEBUG #define DBG_ASSERT(x) assert(x) diff --git a/src/include/tcsem.h b/src/include/tcsem.h index 4b1f215..13ec782 100644 --- a/src/include/tcsem.h +++ b/src/include/tcsem.h @@ -15,7 +15,7 @@ struct ext_log_source { int (*open)(void *, FILE **); TSS_RESULT (*get_entries_by_pcr)(FILE *, UINT32, UINT32, UINT32 *, TSS_PCR_EVENT **); TSS_RESULT (*get_entry)(FILE *, UINT32, UINT32 *, TSS_PCR_EVENT **); - int (*close)(FILE *); + int (*file_close)(FILE *); }; struct event_wrapper { diff --git a/src/include/trousers_types.h b/src/include/trousers_types.h index 1c1d79f..68b9c32 100644 --- a/src/include/trousers_types.h +++ b/src/include/trousers_types.h @@ -100,7 +100,11 @@ typedef struct tdTSS_KEY11_HDR { typedef struct tdTSS_KEY12_HDR { TPM_STRUCTURE_TAG tag; UINT16 fill; -} __attribute__((packed)) TSS_KEY12_HDR; +} +#ifdef __GNUC__ + __attribute__((packed)) +#endif + TSS_KEY12_HDR; typedef struct tdTSS_KEY { union { diff --git a/src/tcs/tcs_aik.c b/src/tcs/tcs_aik.c index 1d22506..16fcddc 100644 --- a/src/tcs/tcs_aik.c +++ b/src/tcs/tcs_aik.c @@ -85,7 +85,6 @@ get_credential(UINT32 type, UINT32 *size, BYTE **cred) { int rc, fd; char *path = NULL; - void *file = NULL; struct stat stat_buf; size_t file_size; @@ -121,23 +120,15 @@ get_credential(UINT32 type, UINT32 *size, BYTE **cred) LogDebugFn("%s, (%zd bytes)", path, file_size); - file = mmap(0, file_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (file == MAP_FAILED) { - LogError("Error reading credential: %s: %s", path, strerror(errno)); - close(fd); - goto done; - } - close(fd); - - if ((*cred = malloc(file_size)) == NULL) { + if ((*cred = malloc(file_size + 1)) == NULL) { LogError("malloc of %zd bytes failed.", file_size); - munmap(file, file_size); goto done; } - memcpy(*cred, file, file_size); + memset(*cred, 0, file_size + 1); + read(fd, *cred, file_size); + close(fd); *size = file_size; - munmap(file, file_size); return; done: diff --git a/src/tcs/tcs_evlog_imaem.c b/src/tcs/tcs_evlog_imaem.c index fbdd29a..c480f13 100644 --- a/src/tcs/tcs_evlog_imaem.c +++ b/src/tcs/tcs_evlog_imaem.c @@ -84,7 +84,7 @@ ima_get_entries_by_pcr(FILE *handle, UINT32 pcr_index, UINT32 first, struct event_wrapper *cur = list; TSS_RESULT result = TCSERR(TSS_E_INTERNAL_ERROR); FILE *fp = (FILE *) handle; - uint len; + unsigned int len; char name[255]; if (list == NULL) { @@ -226,7 +226,7 @@ TSS_RESULT ima_get_entry(FILE *handle, UINT32 pcr_index, UINT32 *num, TSS_PCR_EVENT **ppEvent) { int pcr_value, ptr = 0; - uint len; + unsigned int len; char page[IMA_READ_SIZE]; UINT32 seen_indices = 0; TSS_RESULT result = TCSERR(TSS_E_INTERNAL_ERROR); diff --git a/src/tcs/tcsi_evlog.c b/src/tcs/tcsi_evlog.c index 355f30a..2892cdd 100644 --- a/src/tcs/tcsi_evlog.c +++ b/src/tcs/tcsi_evlog.c @@ -77,11 +77,11 @@ TCS_GetExternalPcrEvent(UINT32 PcrIndex, /* in */ if (tcs_event_log->kernel_source->get_entry(log_handle, PcrIndex, pNumber, ppEvent)) { - tcs_event_log->kernel_source->close(log_handle); + tcs_event_log->kernel_source->file_close(log_handle); return TCSERR(TSS_E_INTERNAL_ERROR); } - tcs_event_log->kernel_source->close(log_handle); + tcs_event_log->kernel_source->file_close(log_handle); } else { LogError("No source for externel kernel events was compiled in, but " "the tcsd is configured to use one! (see %s)", @@ -97,11 +97,11 @@ TCS_GetExternalPcrEvent(UINT32 PcrIndex, /* in */ if (tcs_event_log->firmware_source->get_entry(log_handle, PcrIndex, pNumber, ppEvent)) { - tcs_event_log->firmware_source->close(log_handle); + tcs_event_log->firmware_source->file_close(log_handle); return TCSERR(TSS_E_INTERNAL_ERROR); } - tcs_event_log->firmware_source->close(log_handle); + tcs_event_log->firmware_source->file_close(log_handle); } else { LogError("No source for externel firmware events was compiled in, but " "the tcsd is configured to use one! (see %s)", @@ -192,11 +192,11 @@ TCS_GetExternalPcrEventsByPcr(UINT32 PcrIndex, /* in */ if (tcs_event_log->kernel_source->get_entries_by_pcr(log_handle, PcrIndex, FirstEvent, pEventCount, ppEvents)) { - tcs_event_log->kernel_source->close(log_handle); + tcs_event_log->kernel_source->file_close(log_handle); return TCSERR(TSS_E_INTERNAL_ERROR); } - tcs_event_log->kernel_source->close(log_handle); + tcs_event_log->kernel_source->file_close(log_handle); } else { LogError("No source for externel kernel events was compiled in, but " "the tcsd is configured to use one! (see %s)", @@ -212,11 +212,11 @@ TCS_GetExternalPcrEventsByPcr(UINT32 PcrIndex, /* in */ if (tcs_event_log->firmware_source->get_entries_by_pcr(log_handle, PcrIndex, FirstEvent, pEventCount, ppEvents)) { - tcs_event_log->firmware_source->close(log_handle); + tcs_event_log->firmware_source->file_close(log_handle); return TCSERR(TSS_E_INTERNAL_ERROR); } - tcs_event_log->firmware_source->close(log_handle); + tcs_event_log->firmware_source->file_close(log_handle); } else { LogError("No source for externel firmware events was compiled in, but " "the tcsd is configured to use one! (see %s)", diff --git a/src/tcsd/svrside.c b/src/tcsd/svrside.c index 04cb9e5..6641800 100644 --- a/src/tcsd/svrside.c +++ b/src/tcsd/svrside.c @@ -216,7 +216,7 @@ main(int argc, char **argv) struct sockaddr_in serv_addr, client_addr; TSS_RESULT result; int newsd, c, option_index = 0; - unsigned client_len; + socklen_t client_len; char *hostname = NULL; struct passwd *pwd; struct hostent *client_hostent = NULL; diff --git a/src/tcsd/tcsd_conf.c b/src/tcsd/tcsd_conf.c index fbb6d1a..b4a01a4 100644 --- a/src/tcsd/tcsd_conf.c +++ b/src/tcsd/tcsd_conf.c @@ -307,7 +307,7 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) case opt_firmware_pcrs: conf->unset &= ~TCSD_OPTION_FIRMWARE_PCRS; while (1) { - comma = rindex(arg, ','); + comma = strrchr(arg, ','); if (comma == NULL) { if (!isdigit(*arg)) @@ -336,7 +336,7 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) case opt_kernel_pcrs: conf->unset &= ~TCSD_OPTION_KERNEL_PCRS; while (1) { - comma = rindex(arg, ','); + comma = strrchr(arg, ','); if (comma == NULL) { if (!isdigit(*arg)) @@ -388,7 +388,7 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) free(conf->system_ps_dir); /* break out the system ps directory from the file path */ - dir_ptr = rindex(tmp_ptr, '/'); + dir_ptr = strrchr(tmp_ptr, '/'); *dir_ptr = '\0'; if (strlen(tmp_ptr) == 0) conf->system_ps_dir = strdup("/"); @@ -537,10 +537,10 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) break; case opt_remote_ops: conf->unset &= ~TCSD_OPTION_REMOTE_OPS; - comma = rindex(arg, '\n'); + comma = strrchr(arg, '\n'); *comma = '\0'; while (1) { - comma = rindex(arg, ','); + comma = strrchr(arg, ','); if (comma == NULL) { comma = arg; @@ -576,10 +576,10 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) case opt_host_platform_class: /* append the host class on the list */ conf->unset &= ~TCSD_OPTION_HOST_PLATFORM_CLASS; - comma = rindex(arg,'\n'); + comma = strrchr(arg,'\n'); *comma = '\0'; - comma = rindex(arg,','); + comma = strrchr(arg,','); /* At least one comma: error - more than one host class defined */ if (comma != NULL) { LogError("Config option \"host_platform_class\" error: more than one " @@ -597,10 +597,10 @@ read_conf_line(char *buf, int line_num, struct tcsd_config *conf) break; case opt_all_platform_classes: /* append each of the comma separated values on the list */ - comma = rindex(arg, '\n'); + comma = strrchr(arg, '\n'); *comma = '\0'; while (1) { - comma = rindex(arg, ','); + comma = strrchr(arg, ','); if (comma == NULL) { comma = arg; diff --git a/src/tspi/obj_context.c b/src/tspi/obj_context.c index cb2091e..a1d0262 100644 --- a/src/tspi/obj_context.c +++ b/src/tspi/obj_context.c @@ -989,8 +989,8 @@ do_transport_encryption(TPM_TRANSPORT_PUBLIC *transPub, TSS_RESULT result; UINT32 i, encLen; UINT32 seedLen, ivLen; - BYTE *enc; - BYTE seed[(2 * sizeof(TPM_NONCE)) + strlen("in") + TPM_SHA1_160_HASH_LEN]; + BYTE *enc = NULL; + BYTE *seed = NULL; /* allocate the most data anyone below might need */ encLen = ((inLen / TSS_MAX_SYM_BLOCK_SIZE) + 1) * TSS_MAX_SYM_BLOCK_SIZE; @@ -999,6 +999,13 @@ do_transport_encryption(TPM_TRANSPORT_PUBLIC *transPub, return TSPERR(TSS_E_OUTOFMEMORY); } + seedLen = (2 * sizeof(TPM_NONCE)) + strlen("in") + TPM_SHA1_160_HASH_LEN; + if ((seed = malloc(seedLen + 1)) == NULL) { + LogError("malloc of %u bytes failed", seedLen); + free(enc); + return TSPERR(TSS_E_OUTOFMEMORY); + } + /* set the common 3 initial values of 'seed', which is used to generate either the IV or * mask */ memcpy(seed, pTransAuth->NonceEven.nonce, sizeof(TPM_NONCE)); @@ -1014,10 +1021,8 @@ do_transport_encryption(TPM_TRANSPORT_PUBLIC *transPub, /* add the secret data to the seed for MGF1 */ memcpy(&seed[2 * sizeof(TPM_NONCE) + strlen("in")], secret, TPM_SHA1_160_HASH_LEN); - if ((result = Trspi_MGF1(TSS_HASH_SHA1, seedLen, seed, encLen, enc))) { - free(enc); - return result; - } + if ((result = Trspi_MGF1(TSS_HASH_SHA1, seedLen, seed, encLen, enc))) + goto fail; for (i = 0; i < inLen; i++) enc[i] ^= in[i]; @@ -1030,30 +1035,31 @@ do_transport_encryption(TPM_TRANSPORT_PUBLIC *transPub, ivLen = TSS_MAX_SYM_BLOCK_SIZE; seedLen = (2 * sizeof(TPM_NONCE)) + strlen("in"); - if ((result = Trspi_MGF1(TSS_HASH_SHA1, seedLen, seed, ivLen, iv))) { - free(enc); - return result; - } + if ((result = Trspi_MGF1(TSS_HASH_SHA1, seedLen, seed, ivLen, iv))) + goto fail; /* use the secret data as the key for AES */ if ((result = Trspi_SymEncrypt(transPub->algId, transPub->encScheme, secret, iv, in, - inLen, enc, &encLen))) { - free(enc); - return result; - } + inLen, enc, &encLen))) + goto fail; break; } default: LogDebug("Unknown algorithm for encrypted transport session: 0x%x", transPub->algId); - free(enc); - return TSPERR(TSS_E_INTERNAL_ERROR); + result = TSPERR(TSS_E_INTERNAL_ERROR); + goto fail; } *out = enc; *outLen = encLen; + return TSS_SUCCESS; +fail: + free(enc); + free(seed); + return result; } diff --git a/tools/ps_convert.c b/tools/ps_convert.c index 2a2d85a..63caf4d 100644 --- a/tools/ps_convert.c +++ b/tools/ps_convert.c @@ -64,7 +64,7 @@ #define PRINTERR(...) fprintf(stderr, ##__VA_ARGS__) #define PRINT(...) printf("PS " __VA_ARGS__) -#define OUT(stream, buf, size) \ +#define OUTDATA(stream, buf, size) \ do { \ if (fwrite(buf, size, 1, stream) != 1) { \ PRINTERR("fwrite error: %s\n", strerror(errno)); \ @@ -107,17 +107,17 @@ convertkey_0(FILE *in, FILE *out) int members; /* output the key's UUID and parent UUID */ - OUT(out, buf, sizeof(TSS_UUID)); - OUT(out, &buf[sizeof(TSS_UUID)], sizeof(TSS_UUID)); + OUTDATA(out, buf, sizeof(TSS_UUID)); + OUTDATA(out, &buf[sizeof(TSS_UUID)], sizeof(TSS_UUID)); pub_data_size = *(UINT16 *)&buf[(2 * sizeof(TSS_UUID))]; blob_size = *(UINT16 *)&buf[(2 * sizeof(TSS_UUID)) + sizeof(UINT16)]; cache_flags = *(UINT16 *)&buf[2*sizeof(TSS_UUID) + 2*sizeof(UINT16)]; - OUT(out, &pub_data_size, sizeof(UINT16)); - OUT(out, &blob_size, sizeof(UINT16)); - OUT(out, &vendor_data_size, sizeof(UINT32)); - OUT(out, &cache_flags, sizeof(UINT16)); + OUTDATA(out, &pub_data_size, sizeof(UINT16)); + OUTDATA(out, &blob_size, sizeof(UINT16)); + OUTDATA(out, &vendor_data_size, sizeof(UINT32)); + OUTDATA(out, &cache_flags, sizeof(UINT16)); /* trash buf, we've got what we needed from it */ if ((members = fread(buf, pub_data_size + blob_size, @@ -126,8 +126,8 @@ convertkey_0(FILE *in, FILE *out) return -1; } - OUT(out, buf, pub_data_size); - OUT(out, &buf[pub_data_size], blob_size); + OUTDATA(out, buf, pub_data_size); + OUTDATA(out, &buf[pub_data_size], blob_size); return 0; } @@ -140,10 +140,10 @@ version_0_convert(FILE *in, FILE *out) UINT32 *u32 = (UINT32 *) &buf; /* output the PS version */ - OUT(out, "\1", 1); + OUTDATA(out, "\1", 1); /* number of keys */ - OUT(out, u32, sizeof(UINT32)); + OUTDATA(out, u32, sizeof(UINT32)); /* The +- 1's below account for the byte we read in to determine * if the PS file had a version byte at the beginning */ -- 1.7.2.2 ------------------------------------------------------------------------------ This SF.net Dev2Dev email is sponsored by: Show off your parallel programming skills. Enter the Intel(R) Threading Challenge 2010. http://p.sf.net/sfu/intel-thread-sfd _______________________________________________ TrouSerS-tech mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/trousers-tech
