| 1 |
/* |
| 2 |
* Copyright (c) 2013 .SE (The Internet Infrastructure Foundation) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
softhsm2-dump-file.cpp |
| 29 |
|
| 30 |
This program can be used for dumping SoftHSM v2 object files. |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
|
| 35 |
#include "common.h" |
| 36 |
|
| 37 |
// Attribute types on disk |
| 38 |
#define BOOLEAN_ATTR 0x1 |
| 39 |
#define ULONG_ATTR 0x2 |
| 40 |
#define BYTES_ATTR 0x3 |
| 41 |
#define ATTRMAP_ATTR 0x4 |
| 42 |
#define MECHSET_ATTR 0x5 |
| 43 |
|
| 44 |
// Maximum byte string length (1Gib) |
| 45 |
#define MAX_BYTES 0x3fffffff |
| 46 |
|
| 47 |
typedef AttributeTK<uint64_t, uint64_t, uint64_t> Attribute; |
| 48 |
|
| 49 |
// Attribute specialization |
| 50 |
template<> |
| 51 |
bool Attribute::isBoolean() const |
| 52 |
{ |
| 53 |
return kind == BOOLEAN_ATTR; |
| 54 |
} |
| 55 |
|
| 56 |
template<> |
| 57 |
bool Attribute::isInteger() const |
| 58 |
{ |
| 59 |
return kind == ULONG_ATTR; |
| 60 |
} |
| 61 |
|
| 62 |
template<> |
| 63 |
bool Attribute::isBinary() const |
| 64 |
{ |
| 65 |
return kind == BYTES_ATTR; |
| 66 |
} |
| 67 |
|
| 68 |
template<> |
| 69 |
bool Attribute::isMechSet() const |
| 70 |
{ |
| 71 |
return kind == MECHSET_ATTR; |
| 72 |
} |
| 73 |
|
| 74 |
template<> |
| 75 |
void Attribute::dumpType() const |
| 76 |
{ |
| 77 |
dumpULong(type, true); |
| 78 |
} |
| 79 |
|
| 80 |
template<> |
| 81 |
void Attribute::dumpKind() const |
| 82 |
{ |
| 83 |
dumpULong(kind, true); |
| 84 |
} |
| 85 |
|
| 86 |
template<> |
| 87 |
void Attribute::dumpBoolValue() const |
| 88 |
{ |
| 89 |
dumpBool(boolValue, true); |
| 90 |
} |
| 91 |
|
| 92 |
template<> |
| 93 |
void Attribute::dumpULongValue(uint64_t value) const |
| 94 |
{ |
| 95 |
dumpULong(value, true); |
| 96 |
} |
| 97 |
|
| 98 |
// dumpMap specialization |
| 99 |
typedef std::vector<Attribute> va_type; |
| 100 |
|
| 101 |
void dumpMap(const va_type& value) |
| 102 |
{ |
| 103 |
for (va_type::const_iterator attr = value.begin(); attr != value.end(); ++attr) |
| 104 |
attr->dump(); |
| 105 |
} |
| 106 |
|
| 107 |
// Read a boolean (in fact unsigned 8 bit long) value |
| 108 |
bool readBool(FILE* stream, uint8_t& value) |
| 109 |
{ |
| 110 |
value = 0; |
| 111 |
fpos_t pos; |
| 112 |
if (fgetpos(stream, &pos) != 0) |
| 113 |
{ |
| 114 |
return false; |
| 115 |
} |
| 116 |
uint8_t v; |
| 117 |
if (fread(&v, 1, 1, stream) != 1) |
| 118 |
{ |
| 119 |
(void) fsetpos(stream, &pos); |
| 120 |
return false; |
| 121 |
} |
| 122 |
value = v; |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
// Read an unsigned 64 bit long value |
| 127 |
bool readULong(FILE* stream, uint64_t& value) |
| 128 |
{ |
| 129 |
value = 0; |
| 130 |
fpos_t pos; |
| 131 |
if (fgetpos(stream, &pos) != 0) |
| 132 |
{ |
| 133 |
return false; |
| 134 |
} |
| 135 |
uint8_t v[8]; |
| 136 |
if (fread(v, 1, 8, stream) != 8) |
| 137 |
{ |
| 138 |
(void) fsetpos(stream, &pos); |
| 139 |
return false; |
| 140 |
} |
| 141 |
for (size_t i = 0; i < 8; i++) |
| 142 |
{ |
| 143 |
value <<= 8; |
| 144 |
value += v[i]; |
| 145 |
} |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// Read a byte string (aka uint8_t vector) value |
| 150 |
bool readBytes(FILE* stream, std::vector<uint8_t>& value) |
| 151 |
{ |
| 152 |
size_t len = value.size(); |
| 153 |
fpos_t pos; |
| 154 |
if (fgetpos(stream, &pos) != 0) |
| 155 |
{ |
| 156 |
return false; |
| 157 |
} |
| 158 |
if (fread(&value[0], 1, len, stream) != len) |
| 159 |
{ |
| 160 |
(void) fsetpos(stream, &pos); |
| 161 |
return false; |
| 162 |
} |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
// Read a map (aka Attribute vector) value |
| 167 |
bool readMap(FILE* stream, uint64_t len, std::vector<Attribute>& value) |
| 168 |
{ |
| 169 |
fpos_t pos; |
| 170 |
if (fgetpos(stream, &pos) != 0) |
| 171 |
{ |
| 172 |
return false; |
| 173 |
} |
| 174 |
while (len != 0) |
| 175 |
{ |
| 176 |
Attribute attr; |
| 177 |
|
| 178 |
if (len < 8) |
| 179 |
{ |
| 180 |
(void) fsetpos(stream, &pos); |
| 181 |
return false; |
| 182 |
} |
| 183 |
if (!readULong(stream, attr.type)) |
| 184 |
{ |
| 185 |
(void) fsetpos(stream, &pos); |
| 186 |
return false; |
| 187 |
} |
| 188 |
len -= 8; |
| 189 |
|
| 190 |
if (len < 8) |
| 191 |
{ |
| 192 |
(void) fsetpos(stream, &pos); |
| 193 |
return false; |
| 194 |
} |
| 195 |
if (!readULong(stream, attr.kind)) |
| 196 |
{ |
| 197 |
(void) fsetpos(stream, &pos); |
| 198 |
return false; |
| 199 |
} |
| 200 |
len -= 8; |
| 201 |
|
| 202 |
if (attr.kind == BOOLEAN_ATTR) |
| 203 |
{ |
| 204 |
if (len < 1) |
| 205 |
{ |
| 206 |
(void) fsetpos(stream, &pos); |
| 207 |
return false; |
| 208 |
} |
| 209 |
len -= 1; |
| 210 |
if (!readBool(stream, attr.boolValue)) |
| 211 |
{ |
| 212 |
(void) fsetpos(stream, &pos); |
| 213 |
return false; |
| 214 |
} |
| 215 |
} |
| 216 |
else if (attr.kind == ULONG_ATTR) |
| 217 |
{ |
| 218 |
if (len < 8) |
| 219 |
{ |
| 220 |
(void) fsetpos(stream, &pos); |
| 221 |
return false; |
| 222 |
} |
| 223 |
if (!readULong(stream, attr.ulongValue)) |
| 224 |
{ |
| 225 |
(void) fsetpos(stream, &pos); |
| 226 |
return false; |
| 227 |
} |
| 228 |
len -= 8; |
| 229 |
} |
| 230 |
else if (attr.kind == BYTES_ATTR) |
| 231 |
{ |
| 232 |
uint64_t size; |
| 233 |
if (len < 8) |
| 234 |
{ |
| 235 |
(void) fsetpos(stream, &pos); |
| 236 |
return false; |
| 237 |
} |
| 238 |
if (!readULong(stream, size)) |
| 239 |
{ |
| 240 |
(void) fsetpos(stream, &pos); |
| 241 |
return false; |
| 242 |
} |
| 243 |
len -= 8; |
| 244 |
|
| 245 |
if (len < size) |
| 246 |
{ |
| 247 |
(void) fsetpos(stream, &pos); |
| 248 |
return false; |
| 249 |
} |
| 250 |
attr.bytestrValue.resize((size_t)size); |
| 251 |
if (!readBytes(stream, attr.bytestrValue)) |
| 252 |
{ |
| 253 |
(void) fsetpos(stream, &pos); |
| 254 |
return false; |
| 255 |
} |
| 256 |
len -= size; |
| 257 |
} |
| 258 |
else if (attr.kind == MECHSET_ATTR) |
| 259 |
{ |
| 260 |
uint64_t size; |
| 261 |
if (len < 8) |
| 262 |
{ |
| 263 |
(void) fsetpos(stream, &pos); |
| 264 |
return false; |
| 265 |
} |
| 266 |
if (!readULong(stream, size)) |
| 267 |
{ |
| 268 |
(void) fsetpos(stream, &pos); |
| 269 |
return false; |
| 270 |
} |
| 271 |
len -= 8; |
| 272 |
|
| 273 |
if (len < size * 8) |
| 274 |
{ |
| 275 |
(void) fsetpos(stream, &pos); |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
for (unsigned long i = 0; i < size; i++) |
| 280 |
{ |
| 281 |
uint64_t mech; |
| 282 |
if (!readULong(stream, mech)) |
| 283 |
{ |
| 284 |
(void) fsetpos(stream, &pos); |
| 285 |
return false; |
| 286 |
} |
| 287 |
attr.mechSetValue.insert(mech); |
| 288 |
} |
| 289 |
len -= size * 8; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
(void) fsetpos(stream, &pos); |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
value.push_back(attr); |
| 298 |
} |
| 299 |
|
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
// Error case |
| 304 |
void corrupt(FILE* stream) |
| 305 |
{ |
| 306 |
uint8_t v; |
| 307 |
for (size_t i = 0; i < 8; i++) |
| 308 |
{ |
| 309 |
if (fread(&v, 1, 1, stream) != 1) |
| 310 |
{ |
| 311 |
if (ferror(stream)) |
| 312 |
{ |
| 313 |
printf("get an error...\n"); |
| 314 |
} |
| 315 |
return; |
| 316 |
} |
| 317 |
if (i != 0) |
| 318 |
{ |
| 319 |
printf(" "); |
| 320 |
} |
| 321 |
printf("%02hhx", v); |
| 322 |
} |
| 323 |
if (fread(&v, 1, 1, stream) != 1) |
| 324 |
{ |
| 325 |
if (ferror(stream)) |
| 326 |
{ |
| 327 |
printf("\nget an error...\n"); |
| 328 |
} |
| 329 |
return; |
| 330 |
} |
| 331 |
printf("...\n"); |
| 332 |
} |
| 333 |
|
| 334 |
// Core function |
| 335 |
void dump(FILE* stream) |
| 336 |
{ |
| 337 |
uint64_t gen; |
| 338 |
if (!readULong(stream, gen)) |
| 339 |
{ |
| 340 |
if (feof(stream)) |
| 341 |
{ |
| 342 |
printf("empty file\n"); |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
corrupt(stream); |
| 347 |
} |
| 348 |
return; |
| 349 |
} |
| 350 |
dumpULong(gen); |
| 351 |
printf("generation %lu\n", (unsigned long) gen); |
| 352 |
|
| 353 |
while (!feof(stream)) |
| 354 |
{ |
| 355 |
uint64_t p11type; |
| 356 |
if (!readULong(stream, p11type)) |
| 357 |
{ |
| 358 |
corrupt(stream); |
| 359 |
return; |
| 360 |
} |
| 361 |
dumpULong(p11type); |
| 362 |
if ((uint64_t)((uint32_t)p11type) != p11type) |
| 363 |
{ |
| 364 |
printf("overflow attribute type\n"); |
| 365 |
} |
| 366 |
else |
| 367 |
{ |
| 368 |
dumpCKA((unsigned long) p11type, 48); |
| 369 |
printf("\n"); |
| 370 |
} |
| 371 |
|
| 372 |
uint64_t disktype; |
| 373 |
if (!readULong(stream, disktype)) |
| 374 |
{ |
| 375 |
corrupt(stream); |
| 376 |
return; |
| 377 |
} |
| 378 |
dumpULong(disktype); |
| 379 |
switch (disktype) |
| 380 |
{ |
| 381 |
case BOOLEAN_ATTR: |
| 382 |
printf("boolean attribute\n"); |
| 383 |
break; |
| 384 |
case ULONG_ATTR: |
| 385 |
printf("unsigned long attribute\n"); |
| 386 |
break; |
| 387 |
case BYTES_ATTR: |
| 388 |
printf("byte string attribute\n"); |
| 389 |
break; |
| 390 |
case ATTRMAP_ATTR: |
| 391 |
printf("attribute map attribute\n"); |
| 392 |
break; |
| 393 |
case MECHSET_ATTR: |
| 394 |
printf("mechanism set attribute\n"); |
| 395 |
break; |
| 396 |
default: |
| 397 |
printf("unknown attribute format\n"); |
| 398 |
break; |
| 399 |
} |
| 400 |
|
| 401 |
if (disktype == BOOLEAN_ATTR) |
| 402 |
{ |
| 403 |
uint8_t value; |
| 404 |
if (!readBool(stream, value)) |
| 405 |
{ |
| 406 |
corrupt(stream); |
| 407 |
return; |
| 408 |
} |
| 409 |
dumpBool(value); |
| 410 |
printf("\n"); |
| 411 |
} |
| 412 |
else if (disktype == ULONG_ATTR) |
| 413 |
{ |
| 414 |
uint64_t value; |
| 415 |
if (!readULong(stream, value)) |
| 416 |
{ |
| 417 |
corrupt(stream); |
| 418 |
return; |
| 419 |
} |
| 420 |
dumpULong(value); |
| 421 |
dumpCKx(p11type, value, 48); |
| 422 |
printf("\n"); |
| 423 |
} |
| 424 |
else if (disktype == BYTES_ATTR) |
| 425 |
{ |
| 426 |
uint64_t len; |
| 427 |
if (!readULong(stream, len)) |
| 428 |
{ |
| 429 |
corrupt(stream); |
| 430 |
return; |
| 431 |
} |
| 432 |
dumpULong(len); |
| 433 |
if (len > MAX_BYTES) |
| 434 |
{ |
| 435 |
printf("overflow length...\n"); |
| 436 |
return; |
| 437 |
} |
| 438 |
printf("(length %lu)\n", (unsigned long) len); |
| 439 |
|
| 440 |
std::vector<uint8_t> value((size_t) len); |
| 441 |
if (!readBytes(stream, value)) |
| 442 |
{ |
| 443 |
corrupt(stream); |
| 444 |
return; |
| 445 |
} |
| 446 |
dumpBytes(value); |
| 447 |
} |
| 448 |
else if (disktype == ATTRMAP_ATTR) |
| 449 |
{ |
| 450 |
uint64_t len; |
| 451 |
if (!readULong(stream, len)) |
| 452 |
{ |
| 453 |
corrupt(stream); |
| 454 |
return; |
| 455 |
} |
| 456 |
dumpULong(len); |
| 457 |
if (len > MAX_BYTES) |
| 458 |
{ |
| 459 |
printf("overflow length...\n"); |
| 460 |
return; |
| 461 |
} |
| 462 |
printf("(length %lu)\n", (unsigned long) len); |
| 463 |
|
| 464 |
std::vector<Attribute> value; |
| 465 |
if (!readMap(stream, len, value)) |
| 466 |
{ |
| 467 |
corrupt(stream); |
| 468 |
return; |
| 469 |
} |
| 470 |
dumpMap(value); |
| 471 |
} |
| 472 |
else if (disktype == MECHSET_ATTR) |
| 473 |
{ |
| 474 |
uint64_t len; |
| 475 |
if (!readULong(stream, len)) |
| 476 |
{ |
| 477 |
corrupt(stream); |
| 478 |
return; |
| 479 |
} |
| 480 |
dumpULong(len); |
| 481 |
if (len > MAX_BYTES) |
| 482 |
{ |
| 483 |
printf("overflow length...\n"); |
| 484 |
return; |
| 485 |
} |
| 486 |
printf("(length %lu)\n", (unsigned long) len); |
| 487 |
|
| 488 |
for (unsigned long i = 0; i < len; i++) |
| 489 |
{ |
| 490 |
uint64_t mech; |
| 491 |
if (!readULong(stream, mech)) |
| 492 |
{ |
| 493 |
corrupt(stream); |
| 494 |
return; |
| 495 |
} |
| 496 |
dumpULong(mech); |
| 497 |
dumpCKM(mech, 48); |
| 498 |
printf("\n"); |
| 499 |
} |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
corrupt(stream); |
| 504 |
return; |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
// Display the usage |
| 510 |
void usage() |
| 511 |
{ |
| 512 |
printf("SoftHSM dump tool. From SoftHSM v2 object file.\n"); |
| 513 |
printf("Usage: softhsm2-dump-file path\n"); |
| 514 |
} |
| 515 |
|
| 516 |
// The main function |
| 517 |
int main(int argc, char* argv[]) |
| 518 |
{ |
| 519 |
FILE* stream; |
| 520 |
|
| 521 |
if (argc != 2) |
| 522 |
{ |
| 523 |
usage(); |
| 524 |
exit(0); |
| 525 |
} |
| 526 |
|
| 527 |
stream = fopen(argv[1], "r"); |
| 528 |
if (stream == NULL) |
| 529 |
{ |
| 530 |
fprintf(stderr, "can't open object file %s\n", argv[1]); |
| 531 |
exit(0); |
| 532 |
} |
| 533 |
|
| 534 |
printf("Dump of object file \"%s\"\n", argv[1]); |
| 535 |
dump(stream); |
| 536 |
exit(1); |
| 537 |
} |