1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
|
/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "files.h"
#include "log.h"
#include "pcr.h"
#include "tpm2.h"
#include "tpm2_alg_util.h"
#include "tpm2_attr_util.h"
#include "tpm2_errata.h"
#include "tpm2_hash.h"
#include "tpm2_policy.h"
typedef struct alg_pair alg_pair;
struct alg_pair {
const char *name;
TPM2_ALG_ID id;
tpm2_alg_util_flags flags;
tpm2_alg_util_flags _flags;
};
typedef enum alg_iter_res alg_iter_res;
enum alg_iter_res {
stop,
go,
found
};
typedef enum alg_parser_rc alg_parser_rc;
enum alg_parser_rc {
alg_parser_rc_error,
alg_parser_rc_continue,
alg_parser_rc_done
};
typedef alg_iter_res (*alg_iter)(TPM2_ALG_ID id, const char *name,
tpm2_alg_util_flags flags, void *userdata);
static void tpm2_alg_util_for_each_alg(alg_iter iterator, void *userdata) {
static const alg_pair algs[] = {
// Asymmetric
{ .name = "rsa", .id = TPM2_ALG_RSA, .flags = tpm2_alg_util_flags_asymmetric|tpm2_alg_util_flags_base },
{ .name = "ecc", .id = TPM2_ALG_ECC, .flags = tpm2_alg_util_flags_asymmetric|tpm2_alg_util_flags_base },
// Symmetric
{ .name = "tdes", .id = TPM2_ALG_TDES, .flags = tpm2_alg_util_flags_symmetric },
{ .name = "aes", .id = TPM2_ALG_AES, .flags = tpm2_alg_util_flags_symmetric },
{ .name = "camellia", .id = TPM2_ALG_CAMELLIA, .flags = tpm2_alg_util_flags_symmetric },
{ .name = "sm4", .id = TPM2_ALG_SM4, .flags = tpm2_alg_util_flags_symmetric },
// Hash
{ .name = "sha1", .id = TPM2_ALG_SHA1, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha256", .id = TPM2_ALG_SHA256, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha384", .id = TPM2_ALG_SHA384, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha512", .id = TPM2_ALG_SHA512, .flags = tpm2_alg_util_flags_hash },
{ .name = "sm3_256", .id = TPM2_ALG_SM3_256, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha3_256", .id = TPM2_ALG_SHA3_256, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha3_384", .id = TPM2_ALG_SHA3_384, .flags = tpm2_alg_util_flags_hash },
{ .name = "sha3_512", .id = TPM2_ALG_SHA3_512, .flags = tpm2_alg_util_flags_hash },
// Keyed hash
{ .name = "hmac", .id = TPM2_ALG_HMAC, tpm2_alg_util_flags_keyedhash | tpm2_alg_util_flags_sig },
{ .name = "xor", .id = TPM2_ALG_XOR, tpm2_alg_util_flags_keyedhash },
{ .name = "cmac", .id = TPM2_ALG_CMAC, .flags = tpm2_alg_util_flags_sig },
// Mask Generation Functions
{ .name = "mgf1", .id = TPM2_ALG_MGF1, .flags = tpm2_alg_util_flags_mgf },
// Signature Schemes
{ .name = "rsassa", .id = TPM2_ALG_RSASSA, .flags = tpm2_alg_util_flags_sig },
{ .name = "rsapss", .id = TPM2_ALG_RSAPSS, .flags = tpm2_alg_util_flags_sig },
{ .name = "ecdsa", .id = TPM2_ALG_ECDSA, .flags = tpm2_alg_util_flags_sig },
{ .name = "ecdaa", .id = TPM2_ALG_ECDAA, .flags = tpm2_alg_util_flags_sig },
{ .name = "ecschnorr", .id = TPM2_ALG_ECSCHNORR, .flags = tpm2_alg_util_flags_sig },
{ .name = "sm2", .id = TPM2_ALG_SM2, .flags = tpm2_alg_util_flags_sig },
// Asymmetric Encryption Scheme
{ .name = "oaep", .id = TPM2_ALG_OAEP, .flags = tpm2_alg_util_flags_enc_scheme | tpm2_alg_util_flags_rsa_scheme },
{ .name = "rsaes", .id = TPM2_ALG_RSAES, .flags = tpm2_alg_util_flags_enc_scheme | tpm2_alg_util_flags_rsa_scheme },
{ .name = "ecdh", .id = TPM2_ALG_ECDH, .flags = tpm2_alg_util_flags_enc_scheme },
// Key derivation functions
{ .name = "kdf1_sp800_56a", .id = TPM2_ALG_KDF1_SP800_56A, .flags = tpm2_alg_util_flags_kdf },
{ .name = "kdf2", .id = TPM2_ALG_KDF2, .flags = tpm2_alg_util_flags_kdf },
{ .name = "kdf1_sp800_108", .id = TPM2_ALG_KDF1_SP800_108, .flags = tpm2_alg_util_flags_kdf },
{ .name = "ecmqv", .id = TPM2_ALG_ECMQV, .flags = tpm2_alg_util_flags_kdf },
// Modes
{ .name = "ctr", .id = TPM2_ALG_CTR, .flags = tpm2_alg_util_flags_mode },
{ .name = "ofb", .id = TPM2_ALG_OFB, .flags = tpm2_alg_util_flags_mode },
{ .name = "cbc", .id = TPM2_ALG_CBC, .flags = tpm2_alg_util_flags_mode },
{ .name = "cfb", .id = TPM2_ALG_CFB, .flags = tpm2_alg_util_flags_mode },
{ .name = "ecb", .id = TPM2_ALG_ECB, .flags = tpm2_alg_util_flags_mode },
{ .name = "symcipher", .id = TPM2_ALG_SYMCIPHER, .flags = tpm2_alg_util_flags_base },
{ .name = "keyedhash", .id = TPM2_ALG_KEYEDHASH, .flags = tpm2_alg_util_flags_base },
// Misc
{ .name = "null", .id = TPM2_ALG_NULL, .flags = tpm2_alg_util_flags_misc | tpm2_alg_util_flags_rsa_scheme },
};
size_t i;
for (i = 0; i < ARRAY_LEN(algs); i++) {
const alg_pair *alg = &algs[i];
alg_iter_res result = iterator(alg->id, alg->name, alg->flags,
userdata);
if (result != go) {
return;
}
}
}
static alg_parser_rc handle_sym_common(const char *ext, TPMT_SYM_DEF_OBJECT *s, bool is_parent) {
if (ext == NULL || ext[0] == '\0') {
ext = "128";
}
if (!strncmp(ext, "128", 3)) {
s->keyBits.sym = 128;
} else if (!strncmp(ext, "192", 3)) {
s->keyBits.sym = 192;
} else if (!strncmp(ext, "256", 3)) {
s->keyBits.sym = 256;
} else {
return alg_parser_rc_error;
}
ext += 3;
if (*ext == '\0') {
ext = is_parent ? "cfb" : "null";
}
s->mode.sym = tpm2_alg_util_strtoalg(ext,
tpm2_alg_util_flags_mode | tpm2_alg_util_flags_misc);
if (s->mode.sym == TPM2_ALG_ERROR) {
return alg_parser_rc_error;
}
return alg_parser_rc_done;
}
/*
* Macro for redundant code collapse in handle_asym_scheme_common
* You cannot change all the variables in this, as they are dependent
* on names in that routine; this is for simplicity.
*/
#define do_scheme_halg(scheme, advance, alg) \
do { \
scheme += advance; \
s->scheme.scheme = alg; \
do_scheme_hash_alg = true; \
found = true; \
} while (0)
static alg_parser_rc handle_scheme_sign(const char *scheme,
TPM2B_PUBLIC *public) {
char buf[256];
if (!scheme || scheme[0] == '\0') {
scheme = "null";
}
int rc = snprintf(buf, sizeof(buf), "%s", scheme);
if (rc < 0 || (size_t) rc >= sizeof(buf)) {
return alg_parser_rc_error;
}
// Get the scheme and symetric details
TPMS_ASYM_PARMS *s = &public->publicArea.parameters.asymDetail;
if (!strcmp(scheme, "null")) {
public->publicArea.parameters.asymDetail.scheme.scheme = TPM2_ALG_NULL;
return alg_parser_rc_continue;
}
char *halg = NULL;
char *split = strchr(scheme, '-');
if (split) {
*split = '\0';
halg = split + 1;
}
bool found = false;
bool do_scheme_hash_alg = false;
if (public->publicArea.type == TPM2_ALG_ECC) {
if (!strncmp(scheme, "ecdsa", 5)) {
do_scheme_halg(scheme, 5, TPM2_ALG_ECDSA);
} else if (!strncmp(scheme, "ecdh", 4)) {
do_scheme_halg(scheme, 4, TPM2_ALG_ECDH);
} else if (!strncmp(scheme, "ecschnorr", 9)) {
do_scheme_halg(scheme, 9, TPM2_ALG_ECSCHNORR);
} else if (!strncmp(scheme, "sm2", 3)) {
do_scheme_halg(scheme, 3, TPM2_ALG_SM2);
} else if (!strncmp(scheme, "ecdaa", 5)) {
do_scheme_halg(scheme, 5, TPM2_ALG_ECDAA);
/*
* ECDAA has both a commit-counter value and hashing algorithm.
* The default commit-counter value is set to zero to use the first
* commit-id.
*/
if (scheme[0] == '\0') {
scheme = "0";
}
TPMS_SIG_SCHEME_ECDAA *e = &s->scheme.details.ecdaa;
bool res = tpm2_util_string_to_uint16(scheme, &e->count);
if (!res) {
return alg_parser_rc_error;
}
} else if (!strcmp("null", scheme)) {
s->scheme.scheme = TPM2_ALG_NULL;
}
} else {
if (!strcmp(scheme, "rsaes")) {
/*
* rsaes has no hash alg or details, so it MUST
* match exactly, notice strcmp and NOT strNcmp!
*/
s->scheme.scheme = TPM2_ALG_RSAES;
found = true;
} else if (!strcmp("null", scheme)) {
s->scheme.scheme = TPM2_ALG_NULL;
found = true;
} else if (!strncmp("rsapss", scheme, 6)) {
do_scheme_halg(scheme, 6, TPM2_ALG_RSAPSS);
} else if (!strncmp("rsassa", scheme, 6)) {
do_scheme_halg(scheme, 6, TPM2_ALG_RSASSA);
} else if (!strncmp(scheme, "oaep", 4)) {
do_scheme_halg(scheme, 4, TPM2_ALG_OAEP);
}
}
/* If we're not expecting a hash alg then halg should be NULL */
if ((!do_scheme_hash_alg && halg) || !found) {
return alg_parser_rc_error;
}
/* if we're expecting a hash alg and none provided default */
if (do_scheme_hash_alg && !halg) {
halg = "sha256";
}
/*
* If the scheme is set, both the encrypt and decrypt attributes cannot be set,
* check to see if this is the case, and turn down:
* - DECRYPT - If its a signing scheme.
* - ENCRYPT - If its an asymmetric enc scheme.
*/
if (s->scheme.scheme != TPM2_ALG_NULL) {
bool is_both_set = !!(public->publicArea.objectAttributes
& (TPMA_OBJECT_SIGN_ENCRYPT | TPMA_OBJECT_DECRYPT));
if (is_both_set) {
tpm2_alg_util_flags flags = tpm2_alg_util_algtoflags(
s->scheme.scheme);
TPMA_OBJECT turn_down_flags =
(flags & tpm2_alg_util_flags_sig) ?
TPMA_OBJECT_DECRYPT : TPMA_OBJECT_SIGN_ENCRYPT;
public->publicArea.objectAttributes &= ~turn_down_flags;
}
}
if (do_scheme_hash_alg) {
public->publicArea.parameters.asymDetail.scheme.details.anySig.hashAlg =
tpm2_alg_util_strtoalg(halg, tpm2_alg_util_flags_hash);
if (public->publicArea.parameters.asymDetail.scheme.details.anySig.hashAlg
== TPM2_ALG_ERROR) {
return alg_parser_rc_error;
}
}
return alg_parser_rc_continue;
}
static alg_parser_rc handle_rsa(const char *ext, TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_RSA;
TPMS_RSA_PARMS *r = &public->publicArea.parameters.rsaDetail;
r->exponent = 0;
size_t len = ext ? strlen(ext) : 0;
if (len == 0 || ext[0] == '\0') {
ext = "2048";
}
// Deal with bit size
if (!strncmp(ext, "1024", 4)) {
r->keyBits = 1024;
ext += 4;
} else if (!strncmp(ext, "2048", 4)) {
r->keyBits = 2048;
ext += 4;
} else if (!strncmp(ext, "4096", 4)) {
r->keyBits = 4096;
ext += 4;
} else if (!strncmp(ext, "3072", 4)) {
r->keyBits = 3072;
ext += 4;
} else {
r->keyBits = 2048;
}
/* rsa extension should be consumed at this point */
return ext[0] == '\0' ? alg_parser_rc_continue : alg_parser_rc_error;
}
static alg_parser_rc handle_ecc(const char *ext, TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_ECC;
TPMS_ECC_PARMS *e = &public->publicArea.parameters.eccDetail;
e->kdf.scheme = TPM2_ALG_NULL;
e->curveID = TPM2_ECC_NONE;
/* handle default ecc curve (NIST_P256) */
if (ext == NULL || ext[0] == '\0') {
e->curveID = TPM2_ECC_NIST_P256;
return alg_parser_rc_continue;
}
if (ext[0] == '_') {
/* skip separator */
ext++;
if (!strcmp(ext, "sm2_p256") || !strcmp(ext, "sm2")) {
e->curveID = TPM2_ECC_SM2_P256;
return alg_parser_rc_continue;
} else if (strncmp(ext, "nist_p", 6)) {
return alg_parser_rc_error;
}
ext += 6;
if (ext[0] == '\0') {
return alg_parser_rc_error;
}
/* fall through to NIST curves */
}
/* parse NIST curves */
if (!strncmp(ext, "192", 3)) {
e->curveID = TPM2_ECC_NIST_P192;
ext += 3;
} else if (!strncmp(ext, "224", 3)) {
e->curveID = TPM2_ECC_NIST_P224;
ext += 3;
} else if (!strncmp(ext, "256", 3)) {
e->curveID = TPM2_ECC_NIST_P256;
ext += 3;
} else if (!strncmp(ext, "384", 3)) {
e->curveID = TPM2_ECC_NIST_P384;
ext += 3;
} else if (!strncmp(ext, "521", 3)) {
e->curveID = TPM2_ECC_NIST_P521;
ext += 3;
}
/* ecc extension should be consumed at this point */
return ext[0] == '\0' ? alg_parser_rc_continue : alg_parser_rc_error;
}
static alg_parser_rc handle_aes(const char *ext, TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_SYMCIPHER;
tpm2_errata_fixup(SPEC_116_ERRATA_2_7,
&public->publicArea.objectAttributes);
TPMT_SYM_DEF_OBJECT *s = &public->publicArea.parameters.symDetail.sym;
s->algorithm = TPM2_ALG_AES;
return handle_sym_common(ext, s, false);
}
static alg_parser_rc handle_camellia(const char *ext, TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_SYMCIPHER;
TPMT_SYM_DEF_OBJECT *s = &public->publicArea.parameters.symDetail.sym;
s->algorithm = TPM2_ALG_CAMELLIA;
return handle_sym_common(ext, s, false);
}
static alg_parser_rc handle_sm4(const char *ext, TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_SYMCIPHER;
TPMT_SYM_DEF_OBJECT *s = &public->publicArea.parameters.symDetail.sym;
s->algorithm = TPM2_ALG_SM4;
return handle_sym_common(ext, s, false);
}
static alg_parser_rc handle_xor(TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_KEYEDHASH;
public->publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_XOR;
return alg_parser_rc_continue;
}
static alg_parser_rc handle_hmac(TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_KEYEDHASH;
public->publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_HMAC;
return alg_parser_rc_continue;
}
static alg_parser_rc handle_keyedhash(TPM2B_PUBLIC *public) {
public->publicArea.type = TPM2_ALG_KEYEDHASH;
public->publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_NULL;
return alg_parser_rc_done;
}
static alg_parser_rc handle_object(const char *object, TPM2B_PUBLIC *public) {
if (!strncmp(object, "rsa", 3)) {
object += 3;
return handle_rsa(object, public);
} else if (!strncmp(object, "ecc", 3)) {
object += 3;
return handle_ecc(object, public);
} else if (!strncmp(object, "aes", 3)) {
object += 3;
return handle_aes(object, public);
} else if (!strncmp(object, "camellia", 8)) {
object += 8;
return handle_camellia(object, public);
} else if (!strncmp(object, "sm4", 3)) {
object += (object[3] == '_') ? 4 : 3;
return handle_sm4(object, public);
} else if (!strcmp(object, "hmac")) {
return handle_hmac(public);
} else if (!strcmp(object, "xor")) {
return handle_xor(public);
} else if (!strcmp(object, "keyedhash")) {
return handle_keyedhash(public);
}
return alg_parser_rc_error;
}
static alg_parser_rc handle_scheme_keyedhash(const char *scheme,
TPM2B_PUBLIC *public) {
if (!scheme || scheme[0] == '\0') {
scheme = "sha256";
}
TPM2_ALG_ID alg = tpm2_alg_util_strtoalg(scheme, tpm2_alg_util_flags_hash);
if (alg == TPM2_ALG_ERROR) {
return alg_parser_rc_error;
}
switch (public->publicArea.parameters.keyedHashDetail.scheme.scheme) {
case TPM2_ALG_HMAC:
public->publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg =
alg;
break;
case TPM2_ALG_XOR:
public->publicArea.parameters.keyedHashDetail.scheme.details.exclusiveOr.kdf =
TPM2_ALG_KDF1_SP800_108;
public->publicArea.parameters.keyedHashDetail.scheme.details.exclusiveOr.hashAlg =
alg;
break;
default:
return alg_parser_rc_error;
}
return alg_parser_rc_done;
}
static alg_parser_rc handle_scheme(const char *scheme, TPM2B_PUBLIC *public) {
switch (public->publicArea.type) {
case TPM2_ALG_RSA:
case TPM2_ALG_ECC:
return handle_scheme_sign(scheme, public);
case TPM2_ALG_KEYEDHASH:
return handle_scheme_keyedhash(scheme, public);
default:
return alg_parser_rc_error;
}
return alg_parser_rc_error;
}
static alg_parser_rc handle_asym_detail(const char *detail,
TPM2B_PUBLIC *public) {
bool is_restricted = !!(public->publicArea.objectAttributes
& TPMA_OBJECT_RESTRICTED);
bool is_rsapps = public->publicArea.parameters.asymDetail.scheme.scheme
== TPM2_ALG_RSAPSS;
switch (public->publicArea.type) {
case TPM2_ALG_RSA:
case TPM2_ALG_ECC:
if (!detail || detail[0] == '\0') {
detail = is_restricted || is_rsapps ? "aes128cfb" : "null";
}
TPMT_SYM_DEF_OBJECT *s = &public->publicArea.parameters.symDetail.sym;
if (!strncmp(detail, "aes", 3)) {
s->algorithm = TPM2_ALG_AES;
return handle_sym_common(detail + 3, s, is_restricted);
} else if (!strncmp(detail, "camellia", 8)) {
s->algorithm = TPM2_ALG_CAMELLIA;
return handle_sym_common(detail + 8, s, is_restricted);
} else if (!strncmp(detail, "sm4", 3)) {
s->algorithm = TPM2_ALG_SM4;
detail += (detail[3] == '_') ? 4 : 3;
return handle_sym_common(detail, s, is_restricted);
} else if (!strcmp(detail, "null")) {
s->algorithm = TPM2_ALG_NULL;
return alg_parser_rc_done;
}
/* no default */
}
return alg_parser_rc_error;
}
bool tpm2_alg_util_handle_ext_alg(const char *alg_spec, TPM2B_PUBLIC *public) {
char buf[256];
if (!alg_spec) {
return false;
}
int rc = snprintf(buf, sizeof(buf), "%s", alg_spec);
if (rc < 0 || (size_t) rc >= sizeof(buf)) {
goto error;
}
char *object = NULL;
char *scheme = NULL;
char *symdetail = NULL;
char *b = buf;
char *tok = NULL;
char *saveptr = NULL;
unsigned i = 0;
while ((tok = strtok_r(b, ":", &saveptr))) {
b = NULL;
switch (i) {
case 0:
object = tok;
break;
case 1:
scheme = tok;
break;
case 2:
symdetail = tok;
break;
default:
goto error;
}
i++;
}
if (i == 0) {
goto error;
}
alg_parser_rc prc = handle_object(object, public);
if (prc == alg_parser_rc_done) {
/* we must have exhausted all the entries or it's an error */
return scheme || symdetail ? false : true;
}
if (prc == alg_parser_rc_error) {
return false;
}
/*
* at this point we either have scheme or asym detail, if it
* doesn't process as a scheme shuffle it to asym detail
*/
for (i = 0; i < 2; i++) {
prc = handle_scheme(scheme, public);
if (prc == alg_parser_rc_done) {
/* we must have exhausted all the entries or it's an error */
return symdetail ? false : true;
}
if (prc == alg_parser_rc_error) {
/*
* if symdetail is set scheme must be consumed
* unless scheme has been skipped by setting it
* to NULL
*/
if (symdetail && scheme) {
return false;
}
symdetail = scheme;
scheme = NULL;
continue;
}
/* success in processing scheme */
break;
}
/* handle asym detail */
prc = handle_asym_detail(symdetail, public);
if (prc != alg_parser_rc_done) {
goto error;
}
return true;
error:
LOG_ERR("Could not handle algorithm spec: \"%s\"", alg_spec);
return false;
}
tool_rc tpm2_alg_util_handle_rsa_ext_alg(const char *alg_spec,
TPM2B_PUBLIC *public) {
#define RSA_KEYBITS_STRLEN 6
char *ext_alg_str = calloc(1, strlen(alg_spec) + strlen("rsa") +
RSA_KEYBITS_STRLEN);
if (ext_alg_str == NULL) {
LOG_ERR("oom");
return tool_rc_general_error;
}
strcat(ext_alg_str, "rsa");
switch(public->publicArea.parameters.rsaDetail.keyBits) {
case 1024: strcat(ext_alg_str, "1024:");
break;
case 2048: strcat(ext_alg_str, "2048:");
break;
case 3072: strcat(ext_alg_str, "3072:");
break;
case 4096: strcat(ext_alg_str, "4096:");
break;
};
strcat(ext_alg_str, alg_spec);
bool result = tpm2_alg_util_handle_ext_alg(ext_alg_str, public);
free(ext_alg_str);
return result ? tool_rc_success : tool_rc_general_error;
}
static alg_iter_res find_match(TPM2_ALG_ID id, const char *name,
tpm2_alg_util_flags flags, void *userdata) {
alg_pair *search_data = (alg_pair *) userdata;
/*
* if name, then search on name, else
* search by id.
*/
if (search_data->name && !strcmp(search_data->name, name)) {
alg_iter_res res = search_data->flags & flags ? found : stop;
if (res == found) {
search_data->id = id;
search_data->_flags = flags;
}
return res;
} else if (search_data->id == id) {
alg_iter_res res = search_data->flags & flags ? found : stop;
if (res == found) {
search_data->name = name;
search_data->_flags = flags;
}
return res;
}
return go;
}
TPM2_ALG_ID tpm2_alg_util_strtoalg(const char *name, tpm2_alg_util_flags flags) {
alg_pair userdata = { .name = name, .id = TPM2_ALG_ERROR, .flags = flags };
if (name) {
tpm2_alg_util_for_each_alg(find_match, &userdata);
}
return userdata.id;
}
const char *tpm2_alg_util_algtostr(TPM2_ALG_ID id, tpm2_alg_util_flags flags) {
alg_pair userdata = { .name = NULL, .id = id, .flags = flags };
tpm2_alg_util_for_each_alg(find_match, &userdata);
return userdata.name;
}
const char *tpm2_alg_util_numtoalgstr(const char *str, tpm2_alg_util_flags flags) {
TPM2_ALG_ID alg_id;
if (tpm2_util_string_to_uint16(str, &alg_id)) {
return tpm2_alg_util_algtostr(alg_id, flags);
} else {
return str;
}
}
tpm2_alg_util_flags tpm2_alg_util_algtoflags(TPM2_ALG_ID id) {
alg_pair userdata = { .name = NULL, .id = id, .flags =
tpm2_alg_util_flags_any, ._flags = tpm2_alg_util_flags_none };
tpm2_alg_util_for_each_alg(find_match, &userdata);
return userdata._flags;
}
TPM2_ALG_ID tpm2_alg_util_from_optarg(const char *optarg,
tpm2_alg_util_flags flags) {
TPM2_ALG_ID halg;
bool res = tpm2_util_string_to_uint16(optarg, &halg);
if (!res) {
halg = tpm2_alg_util_strtoalg(optarg, flags);
} else {
if (!tpm2_alg_util_algtostr(halg, flags)) {
return TPM2_ALG_ERROR;
}
}
return halg;
}
UINT16 tpm2_alg_util_get_hash_size(TPMI_ALG_HASH id) {
switch (id) {
case TPM2_ALG_SHA1:
return TPM2_SHA1_DIGEST_SIZE;
case TPM2_ALG_SHA256:
return TPM2_SHA256_DIGEST_SIZE;
case TPM2_ALG_SHA384:
return TPM2_SHA384_DIGEST_SIZE;
case TPM2_ALG_SHA512:
return TPM2_SHA512_DIGEST_SIZE;
case TPM2_ALG_SM3_256:
return TPM2_SM3_256_DIGEST_SIZE;
/* no default */
}
return 0;
}
static const char *hex_to_byte_err(int rc) {
switch (rc) {
case -2:
return "String not even in length";
case -3:
return "Non hex digit found";
case -4:
return "Hex value too big for digest";
}
return "unknown";
}
bool pcr_parse_digest_list(char **argv, int len,
tpm2_pcr_digest_spec *digest_spec) {
/*
* int is chosen because of what is passed in from main, avoids
* sign differences.
* */
int i;
for (i = 0; i < len; i++) {
tpm2_pcr_digest_spec *dspec = &digest_spec[i];
UINT32 count = 0;
/*
* Split <pcr index>:<hash alg>=<hash value>,... on : and separate with null byte, ie:
* <pce index> '\0' <hash alg>'\0'<data>
*
* Start by splitting out the pcr index, and validating it.
*/
char *spec_str = argv[i];
char *pcr_index_str = spec_str;
char *digest_spec_str = strchr(spec_str, ':');
if (!digest_spec_str) {
LOG_ERR("Expecting : in digest spec, not found, got: \"%s\"",
spec_str);
return false;
}
*digest_spec_str = '\0';
digest_spec_str++;
bool result = pcr_get_id(pcr_index_str, &dspec->pcr_index);
if (!result) {
LOG_ERR("Got invalid PCR Index: \"%s\", in digest spec: \"%s\"",
pcr_index_str, spec_str);
return false;
}
/* now that the pcr_index is removed, parse the remaining <hash_name>=<hash_value>,.. */
char *digest_hash_tok;
char *save_ptr = NULL;
/* keep track of digests we have seen */
while ((digest_hash_tok = strtok_r(digest_spec_str, ",", &save_ptr))) {
digest_spec_str = NULL;
if (count >= ARRAY_LEN(dspec->digests.digests)) {
LOG_ERR("Specified too many digests per spec, max is: %zu",
ARRAY_LEN(dspec->digests.digests));
return false;
}
TPMT_HA *d = &dspec->digests.digests[count];
char *stralg = digest_hash_tok;
char *split = strchr(digest_hash_tok, '=');
if (!split) {
LOG_ERR("Expecting = in <hash alg>=<hash value> spec, got: "
"\"%s\"", digest_hash_tok);
return false;
}
*split = '\0';
split++;
char *data = split;
/*
* Convert and validate the hash algorithm. It should be a hash algorithm
*/
TPM2_ALG_ID alg = tpm2_alg_util_from_optarg(stralg,
tpm2_alg_util_flags_hash);
if (alg == TPM2_ALG_ERROR) {
LOG_ERR("Could not convert algorithm, got: \"%s\"", stralg);
return false;
}
d->hashAlg = alg;
/* fill up the TPMT_HA structure with algorithm and digest */
BYTE *digest_data = (BYTE *) &d->digest;
UINT16 expected_hash_size = tpm2_alg_util_get_hash_size(alg);
/* strip any preceding hex on the data as tpm2_util_hex_to_byte_structure doesn't support it */
bool is_hex = !strncmp("0x", data, 2);
if (is_hex) {
data += 2;
}
UINT16 size = expected_hash_size;
int rc = tpm2_util_hex_to_byte_structure(data, &size, digest_data);
if (rc) {
LOG_ERR("Error \"%s\" converting hex string as data, got:"
" \"%s\"", hex_to_byte_err(rc), data);
return false;
}
if (expected_hash_size != size) {
LOG_ERR("Algorithm \"%s\" expects a size of %u bytes, got: %u",
stralg, expected_hash_size, size);
return false;
}
count++;
}
if (!count) {
LOG_ERR("Missing or invalid <hash alg>=<hash value> spec for pcr:"
" \"%s\"", pcr_index_str);
return false;
}
/* assign count at the end, so count is 0 on error */
dspec->digests.count = count;
}
return true;
}
static tool_rc tpm2_public_to_scheme(ESYS_CONTEXT *ectx, ESYS_TR key, TPMI_ALG_PUBLIC *type,
TPMT_SIG_SCHEME *sigscheme) {
tool_rc rc = tool_rc_general_error;
TPM2B_PUBLIC *out_public = NULL;
rc = tpm2_readpublic(ectx, key, &out_public, NULL, NULL);
if (rc != tool_rc_success) {
return rc;
}
*type = out_public->publicArea.type;
TPMU_PUBLIC_PARMS *pp = &out_public->publicArea.parameters;
/*
* Symmetric ciphers do not have signature algorithms
*/
if (*type == TPM2_ALG_SYMCIPHER) {
LOG_ERR("Cannot convert symmetric cipher to signature algorithm");
goto out;
}
/*
* Now we are looking at specific algorithms that the keys
* are bound to, so we need to populate that in the scheme
*/
if ((*type == TPM2_ALG_RSA || *type == TPM2_ALG_ECC)) {
sigscheme->scheme = pp->asymDetail.scheme.scheme;
/* they all have a hash alg, and for most schemes' thats it */
sigscheme->details.any.hashAlg
= pp->asymDetail.scheme.details.anySig.hashAlg;
rc = tool_rc_success;
goto out;
}
/* keyed hash could be the only one left */
sigscheme->scheme = pp->keyedHashDetail.scheme.scheme;
sigscheme->details.hmac.hashAlg = pp->keyedHashDetail.scheme.details.hmac.hashAlg;
rc = tool_rc_success;
out:
Esys_Free(out_public);
return rc;
}
static bool is_null_alg(TPM2_ALG_ID alg) {
return !alg || alg == TPM2_ALG_NULL;
}
tool_rc tpm2_alg_util_get_signature_scheme(ESYS_CONTEXT *ectx,
ESYS_TR key_handle, TPMI_ALG_HASH *halg, TPMI_ALG_SIG_SCHEME sig_scheme,
TPMT_SIG_SCHEME *scheme) {
TPMI_ALG_PUBLIC type = TPM2_ALG_NULL;
TPMT_SIG_SCHEME object_sigscheme = { 0 };
tool_rc rc = tpm2_public_to_scheme(ectx, key_handle, &type, &object_sigscheme);
if (rc != tool_rc_success) {
return rc;
}
LOG_INFO("hashing alg in: 0x%x", *halg);
LOG_INFO("sig scheme in: 0x%x", sig_scheme);
/*
* if scheme is requested by the user, verify the scheme will work
*/
if (!is_null_alg(sig_scheme) && !is_null_alg(object_sigscheme.scheme)
&& object_sigscheme.scheme != sig_scheme) {
LOG_ERR("Requested sign scheme \"%s\" but object only supports sign scheme \%s\")",
tpm2_alg_util_algtostr(sig_scheme, tpm2_alg_util_flags_any),
tpm2_alg_util_algtostr(object_sigscheme.scheme, tpm2_alg_util_flags_any));
return tool_rc_general_error;
} else {
/*
* set a default for RSA, ECC or KEYEDHASH
* Not ECDAA, it wasn't chosen because it needs count as well,
* so any.hashAlg doesn't work. Aksim what would you pick for count?
* See bug #2071 to figure out what to do if an ECDAA scheme comes back.
*
* Assign scheme based on type:
* TPM2_ALG_RSA --> TPM2_ALG_RSSASSA
* TPM2_ALG_ECC --> TPM2_ALG_ECDSA
* TPM2_ALG_KEYEDHASH --> TPM2_ALG_HMAC
*/
if (is_null_alg(sig_scheme)) {
object_sigscheme.scheme = (type == TPM2_ALG_RSA) ? TPM2_ALG_RSASSA :
(type == TPM2_ALG_ECC) ? TPM2_ALG_ECDSA : TPM2_ALG_HMAC;
} else {
object_sigscheme.scheme = sig_scheme;
}
}
/* if hash alg is requested by the user, verify the hash alg will work */
if (!is_null_alg(*halg) && !is_null_alg(object_sigscheme.details.any.hashAlg)
&& object_sigscheme.details.any.hashAlg != *halg) {
LOG_ERR("Requested signature hash alg \"%s\" but object only supports signature hash alg \%s\")",
tpm2_alg_util_algtostr(*halg, tpm2_alg_util_flags_any),
tpm2_alg_util_algtostr(object_sigscheme.details.any.hashAlg, tpm2_alg_util_flags_any));
return tool_rc_general_error;
} else {
object_sigscheme.details.any.hashAlg = is_null_alg(*halg) ? TPM2_ALG_SHA256 :
*halg;
}
/* everything requested matches, or we got defaults move along */
*halg = object_sigscheme.details.any.hashAlg;
*scheme = object_sigscheme;
LOG_INFO("halg out: 0x%x", *halg);
LOG_INFO("sig scheme out: 0x%x", scheme->scheme);
return tool_rc_success;
}
tool_rc tpm2_alg_util_public_init(const char *alg_details, const char *name_halg,
char *attrs, char *auth_policy, TPMA_OBJECT def_attrs,
TPM2B_PUBLIC *public) {
memset(public, 0, sizeof(*public));
/* load a policy from a path if present */
if (auth_policy) {
tool_rc rc = tpm2_policy_set_digest(auth_policy,
&public->publicArea.authPolicy);
if (rc != tool_rc_success) {
return rc;
}
}
/* Set the hashing algorithm used for object name */
public->publicArea.nameAlg = name_halg ?
tpm2_alg_util_from_optarg(name_halg, tpm2_alg_util_flags_hash) :
TPM2_ALG_SHA256;
if (public->publicArea.nameAlg == TPM2_ALG_ERROR) {
LOG_ERR("Invalid name hashing algorithm, got\"%s\"", name_halg);
return tool_rc_unsupported;
}
/* Set specified attributes or use default */
if (attrs) {
bool res = tpm2_attr_util_obj_from_optarg(attrs,
&public->publicArea.objectAttributes);
if (!res) {
return tool_rc_unsupported;
}
} else {
public->publicArea.objectAttributes = def_attrs;
}
/*
* Some defaults may not be OK with the specified algorithms, if their defaults,
* tweak the Object Attributes, if specified by user, complain things will not
* work together and suggest attributes. This allows the user to verify what the
* want.
*/
TPM2B_PUBLIC tmp = *public;
bool res = tpm2_alg_util_handle_ext_alg(alg_details, &tmp);
if (!res) {
LOG_ERR("Could not handle algorithm: \"%s\"", alg_details);
return tool_rc_unsupported;
}
if (attrs && tmp.publicArea.objectAttributes !=
public->publicArea.objectAttributes) {
char *proposed_attrs = tpm2_attr_util_obj_attrtostr(
tmp.publicArea.objectAttributes);
LOG_ERR("Specified attributes \"%s\" and algorithm specifier \"%s\" do "
"not work together, try attributes: \"%s\"", attrs, alg_details,
proposed_attrs);
free(proposed_attrs);
return tool_rc_unsupported;
}
*public = tmp;
return tool_rc_success;
}
const char *tpm2_alg_util_ecc_to_str(TPM2_ECC_CURVE curve_id) {
switch (curve_id) {
case TPM2_ECC_NIST_P192:
return "NIST p192";
case TPM2_ECC_NIST_P224:
return "NIST p224";
case TPM2_ECC_NIST_P256:
return "NIST p256";
case TPM2_ECC_NIST_P384:
return "NIST p384";
case TPM2_ECC_NIST_P521:
return "NIST 521";
case TPM2_ECC_BN_P256:
return "BN P256";
case TPM2_ECC_BN_P638:
return "BN P638";
case TPM2_ECC_SM2_P256:
return "SM2 p256";
/* no default */
}
return NULL;
}
bool tpm2_alg_util_is_aes_size_valid(UINT16 size_in_bytes) {
switch (size_in_bytes) {
case 16:
case 24:
case 32:
return true;
default:
LOG_ERR("Invalid AES key size, got %u bytes, expected 16,24 or 32",
size_in_bytes);
return false;
}
}
bool tpm2_alg_util_is_sm4_size_valid(UINT16 size_in_bytes) {
switch (size_in_bytes) {
case 16:
return true;
default:
LOG_ERR("Invalid SM4 key size, got %u bytes, expected 16",
size_in_bytes);
return false;
}
}
TPM2_ALG_ID tpm2_alg_util_get_name_alg(ESYS_CONTEXT *ectx, ESYS_TR handle) {
TPM2B_NAME *name = NULL;
TSS2_RC rc = Esys_TR_GetName(ectx, handle, &name);
if (rc != TSS2_RC_SUCCESS) {
return TPM2_ALG_ERROR;
}
if (name->size < 2) {
Esys_Free(name);
return TPM2_ALG_ERROR;
}
UINT16 *big_endian_alg = (UINT16 *)name->name;
TPM2_ALG_ID name_alg = tpm2_util_ntoh_16(*big_endian_alg);
Esys_Free(name);
return name_alg;
}
|