| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Licensed Materials - Property of IBM | 2 * Licensed Materials - Property of IBM |
| 4 * | 3 * |
| 5 * trousers - An open source TCG Software Stack | 4 * trousers - An open source TCG Software Stack |
| 6 * | 5 * |
| 7 * (C) Copyright International Business Machines Corp. 2004 | 6 * (C) Copyright International Business Machines Corp. 2004 |
| 8 * | 7 * |
| 9 */ | 8 */ |
| 10 | 9 |
| 11 | 10 |
| 12 #include <stdlib.h> | 11 #include <stdlib.h> |
| 13 #include <stdio.h> | 12 #include <stdio.h> |
| 14 #include <unistd.h> | 13 #include <unistd.h> |
| 15 #include <sys/types.h> | 14 #include <sys/types.h> |
| 16 #include <sys/stat.h> | 15 #include <sys/stat.h> |
| 16 #if defined(HAVE_BYTEORDER_H) |
| 17 #include <sys/byteorder.h> |
| 18 #elif defined(HTOLE_DEFINED) |
| 19 #include <endian.h> |
| 20 #define LE_16 htole16 |
| 21 #define LE_32 htole32 |
| 22 #define LE_64 htole64 |
| 23 #else |
| 24 #define LE_16(x) (x) |
| 25 #define LE_32(x) (x) |
| 26 #define LE_64(x) (x) |
| 27 #endif |
| 17 #include <fcntl.h> | 28 #include <fcntl.h> |
| 18 #include <string.h> | 29 #include <string.h> |
| 19 #include <limits.h> | 30 #include <limits.h> |
| 20 #include <assert.h> | 31 #include <assert.h> |
| 21 #include <errno.h> | 32 #include <errno.h> |
| 22 | 33 |
| 23 #include "trousers/tss.h" | 34 #include "trousers/tss.h" |
| 24 #include "trousers_types.h" | 35 #include "trousers_types.h" |
| 25 #include "tcs_int_literals.h" | 36 #include "tcs_int_literals.h" |
| 26 #include "tcsps.h" | 37 #include "tcsps.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 137 |
| 127 /* go to NUM_KEYS */ | 138 /* go to NUM_KEYS */ |
| 128 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); | 139 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); |
| 129 if (rc == ((off_t) - 1)) { | 140 if (rc == ((off_t) - 1)) { |
| 130 LogError("lseek: %s", strerror(errno)); | 141 LogError("lseek: %s", strerror(errno)); |
| 131 return -1; | 142 return -1; |
| 132 } | 143 } |
| 133 | 144 |
| 134 /* read the number of keys */ | 145 /* read the number of keys */ |
| 135 rc = read(fd, &num_keys, sizeof(UINT32)); | 146 rc = read(fd, &num_keys, sizeof(UINT32)); |
| 147 num_keys = LE_32(num_keys); |
| 136 if (rc == -1) { | 148 if (rc == -1) { |
| 137 LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno
)); | 149 LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno
)); |
| 138 return -1; | 150 return -1; |
| 139 } else if (rc == 0) { | 151 } else if (rc == 0) { |
| 140 /* This is the first key being written */ | 152 /* This is the first key being written */ |
| 141 num_keys = 1; | 153 num_keys = 1; |
| 142 version = 1; | 154 version = 1; |
| 143 | 155 |
| 144 /* seek to the PS version */ | 156 /* seek to the PS version */ |
| 145 rc = lseek(fd, TSSPS_VERSION_OFFSET, SEEK_SET); | 157 rc = lseek(fd, TSSPS_VERSION_OFFSET, SEEK_SET); |
| 146 if (rc == ((off_t) - 1)) { | 158 if (rc == ((off_t) - 1)) { |
| 147 LogError("lseek: %s", strerror(errno)); | 159 LogError("lseek: %s", strerror(errno)); |
| 148 return -1; | 160 return -1; |
| 149 } | 161 } |
| 150 | 162 |
| 151 /* write out the version info byte */ | 163 /* write out the version info byte */ |
| 152 if ((rc = write_data(fd, &version, sizeof(BYTE)))) { | 164 if ((rc = write_data(fd, &version, sizeof(BYTE)))) { |
| 153 LogError("%s", __FUNCTION__); | 165 LogError("%s", __FUNCTION__); |
| 154 return rc; | 166 return rc; |
| 155 } | 167 } |
| 156 | 168 |
| 157 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); | 169 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); |
| 158 if (rc == ((off_t) - 1)) { | 170 if (rc == ((off_t) - 1)) { |
| 159 LogError("lseek: %s", strerror(errno)); | 171 LogError("lseek: %s", strerror(errno)); |
| 160 return -1; | 172 return -1; |
| 161 } | 173 } |
| 162 | 174 |
| 175 num_keys = LE_32(num_keys); |
| 163 if ((rc = write_data(fd, &num_keys, sizeof(UINT32)))) { | 176 if ((rc = write_data(fd, &num_keys, sizeof(UINT32)))) { |
| 164 LogError("%s", __FUNCTION__); | 177 LogError("%s", __FUNCTION__); |
| 165 return rc; | 178 return rc; |
| 166 } | 179 } |
| 167 | 180 |
| 168 /* return the offset */ | 181 /* return the offset */ |
| 169 return (TSSPS_NUM_KEYS_OFFSET + sizeof(UINT32)); | 182 return (TSSPS_NUM_KEYS_OFFSET + sizeof(UINT32)); |
| 170 } | 183 } |
| 171 | 184 |
| 172 /* if there is a hole in the file we can write to, find it */ | 185 /* if there is a hole in the file we can write to, find it */ |
| 173 offset = find_write_offset(pub_data_size, blob_size, vendor_data_size); | 186 offset = find_write_offset(pub_data_size, blob_size, vendor_data_size); |
| 174 | 187 |
| 175 if (offset != -1) { | 188 if (offset != -1) { |
| 176 /* we found a hole, seek to it and don't increment the # of keys
on disk */ | 189 /* we found a hole, seek to it and don't increment the # of keys
on disk */ |
| 177 rc = lseek(fd, offset, SEEK_SET); | 190 rc = lseek(fd, offset, SEEK_SET); |
| 178 } else { | 191 } else { |
| 179 /* we didn't find a hole, increment the number of keys on disk a
nd seek | 192 /* we didn't find a hole, increment the number of keys on disk a
nd seek |
| 180 * to the end of the file | 193 * to the end of the file |
| 181 */ | 194 */ |
| 182 num_keys++; | 195 num_keys++; |
| 183 | 196 |
| 184 /* go to the beginning */ | 197 /* go to the beginning */ |
| 185 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); | 198 rc = lseek(fd, TSSPS_NUM_KEYS_OFFSET, SEEK_SET); |
| 186 if (rc == ((off_t) - 1)) { | 199 if (rc == ((off_t) - 1)) { |
| 187 LogError("lseek: %s", strerror(errno)); | 200 LogError("lseek: %s", strerror(errno)); |
| 188 return -1; | 201 return -1; |
| 189 } | 202 } |
| 190 | 203 num_keys = LE_32(num_keys); |
| 191 if ((rc = write_data(fd, &num_keys, sizeof(UINT32)))) { | 204 if ((rc = write_data(fd, &num_keys, sizeof(UINT32)))) { |
| 192 LogError("%s", __FUNCTION__); | 205 LogError("%s", __FUNCTION__); |
| 193 return rc; | 206 return rc; |
| 194 } | 207 } |
| 195 | 208 |
| 196 rc = lseek(fd, 0, SEEK_END); | 209 rc = lseek(fd, 0, SEEK_END); |
| 197 } | 210 } |
| 198 if (rc == ((off_t) - 1)) { | 211 if (rc == ((off_t) - 1)) { |
| 199 LogError("lseek: %s", strerror(errno)); | 212 LogError("lseek: %s", strerror(errno)); |
| 200 return -1; | 213 return -1; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 return 0; | 280 return 0; |
| 268 } | 281 } |
| 269 | 282 |
| 270 rc = read(fd, &num_keys, sizeof(UINT32)); | 283 rc = read(fd, &num_keys, sizeof(UINT32)); |
| 271 if (rc < 0) { | 284 if (rc < 0) { |
| 272 LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno
)); | 285 LogError("read of %zd bytes: %s", sizeof(UINT32), strerror(errno
)); |
| 273 return 0; | 286 return 0; |
| 274 } else if ((unsigned)rc < sizeof(UINT32)) { | 287 } else if ((unsigned)rc < sizeof(UINT32)) { |
| 275 num_keys = 0; | 288 num_keys = 0; |
| 276 } | 289 } |
| 290 num_keys = LE_32(num_keys); |
| 277 | 291 |
| 278 return num_keys; | 292 return num_keys; |
| 279 } | 293 } |
| 280 | 294 |
| 281 /* | 295 /* |
| 282 * count the number of valid keys in the cache | 296 * count the number of valid keys in the cache |
| 283 */ | 297 */ |
| 284 int | 298 int |
| 285 get_num_keys() | 299 get_num_keys() |
| 286 { | 300 { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 if ((rc = read_data(fd, (void *)&tmp->parent_uuid, sizeof(TSS_UU
ID)))) { | 410 if ((rc = read_data(fd, (void *)&tmp->parent_uuid, sizeof(TSS_UU
ID)))) { |
| 397 LogError("%s", __FUNCTION__); | 411 LogError("%s", __FUNCTION__); |
| 398 goto err_exit; | 412 goto err_exit; |
| 399 } | 413 } |
| 400 | 414 |
| 401 /* pub data size */ | 415 /* pub data size */ |
| 402 if ((rc = read_data(fd, &tmp->pub_data_size, sizeof(UINT16)))) { | 416 if ((rc = read_data(fd, &tmp->pub_data_size, sizeof(UINT16)))) { |
| 403 LogError("%s", __FUNCTION__); | 417 LogError("%s", __FUNCTION__); |
| 404 goto err_exit; | 418 goto err_exit; |
| 405 } | 419 } |
| 420 tmp->pub_data_size = LE_16(tmp->pub_data_size); |
| 406 | 421 |
| 407 » » DBG_ASSERT(tmp->pub_data_size <= 2048 && tmp->pub_data_size > 0)
; | 422 » » //DBG_ASSERT(tmp->pub_data_size <= 2048 && tmp->pub_data_size >
0); |
| 408 | 423 |
| 409 /* blob size */ | 424 /* blob size */ |
| 410 if ((rc = read_data(fd, &tmp->blob_size, sizeof(UINT16)))) { | 425 if ((rc = read_data(fd, &tmp->blob_size, sizeof(UINT16)))) { |
| 411 LogError("%s", __FUNCTION__); | 426 LogError("%s", __FUNCTION__); |
| 412 goto err_exit; | 427 goto err_exit; |
| 413 } | 428 } |
| 414 | 429 tmp->blob_size = LE_16(tmp->blob_size); |
| 415 DBG_ASSERT(tmp->blob_size <= 4096 && tmp->blob_size > 0); | 430 DBG_ASSERT(tmp->blob_size <= 4096 && tmp->blob_size > 0); |
| 416 | 431 |
| 417 /* vendor data size */ | 432 /* vendor data size */ |
| 418 if ((rc = read_data(fd, &tmp->vendor_data_size, sizeof(UINT32)))
) { | 433 if ((rc = read_data(fd, &tmp->vendor_data_size, sizeof(UINT32)))
) { |
| 419 LogError("%s", __FUNCTION__); | 434 LogError("%s", __FUNCTION__); |
| 420 goto err_exit; | 435 goto err_exit; |
| 421 } | 436 } |
| 437 tmp->vendor_data_size = LE_32(tmp->vendor_data_size); |
| 422 | 438 |
| 423 /* cache flags */ | 439 /* cache flags */ |
| 424 if ((rc = read_data(fd, &tmp->flags, sizeof(UINT16)))) { | 440 if ((rc = read_data(fd, &tmp->flags, sizeof(UINT16)))) { |
| 425 LogError("%s", __FUNCTION__); | 441 LogError("%s", __FUNCTION__); |
| 426 goto err_exit; | 442 goto err_exit; |
| 427 } | 443 } |
| 444 tmp->flags = LE_16(tmp->flags); |
| 428 | 445 |
| 429 #ifdef TSS_DEBUG | 446 #ifdef TSS_DEBUG |
| 430 if (tmp->flags & CACHE_FLAG_VALID) | 447 if (tmp->flags & CACHE_FLAG_VALID) |
| 431 valid_keys++; | 448 valid_keys++; |
| 432 #endif | 449 #endif |
| 433 /* fast forward over the pub key */ | 450 /* fast forward over the pub key */ |
| 434 offset = lseek(fd, tmp->pub_data_size, SEEK_CUR); | 451 offset = lseek(fd, tmp->pub_data_size, SEEK_CUR); |
| 435 if (offset == ((off_t) - 1)) { | 452 if (offset == ((off_t) - 1)) { |
| 436 LogError("lseek: %s", strerror(errno)); | 453 LogError("lseek: %s", strerror(errno)); |
| 437 rc = -1; | 454 rc = -1; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 do { | 529 do { |
| 513 tmp_next = tmp->next; | 530 tmp_next = tmp->next; |
| 514 free(tmp); | 531 free(tmp); |
| 515 tmp = tmp_next; | 532 tmp = tmp_next; |
| 516 } while (tmp); | 533 } while (tmp); |
| 517 | 534 |
| 518 MUTEX_UNLOCK(disk_cache_lock); | 535 MUTEX_UNLOCK(disk_cache_lock); |
| 519 | 536 |
| 520 return 0; | 537 return 0; |
| 521 } | 538 } |
| OLD | NEW |