]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_tpm.c
tpm: Check that parse_byte_string() has data to parse
[karo-tx-uboot.git] / common / cmd_tpm.c
index 46fae1877520796ec57869484d74785c4a78d08c..e9c661821ce747e41ec099ba081fc32697035da6 100644 (file)
@@ -1,32 +1,24 @@
 /*
  * Copyright (c) 2013 The Chromium OS Authors.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <command.h>
+#include <dm.h>
 #include <malloc.h>
 #include <tpm.h>
 #include <asm/unaligned.h>
 #include <linux/string.h>
 
+/* Useful constants */
+enum {
+       DIGEST_LENGTH           = 20,
+       /* max lengths, valid for RSA keys <= 2048 bits */
+       TPM_PUBKEY_MAX_LENGTH   = 288,
+};
+
 /**
  * Print a byte string in hexdecimal format, 16-bytes per line.
  *
@@ -66,6 +58,8 @@ static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
        size_t count, length;
        int i;
 
+       if (!bytes)
+               return NULL;
        length = strlen(bytes);
        count = length / 2;
 
@@ -88,17 +82,19 @@ static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
 }
 
 /**
- * Convert TPM command return code to U-Boot command error codes.
+ * report_return_code() - Report any error and return failure or success
  *
  * @param return_code  TPM command return code
  * @return value of enum command_ret_t
  */
-static int convert_return_code(uint32_t return_code)
+static int report_return_code(int return_code)
 {
-       if (return_code)
+       if (return_code) {
+               printf("Error: %d\n", return_code);
                return CMD_RET_FAILURE;
-       else
+       } else {
                return CMD_RET_SUCCESS;
+       }
 }
 
 /**
@@ -260,7 +256,7 @@ static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_FAILURE;
        }
 
-       return convert_return_code(tpm_startup(mode));
+       return report_return_code(tpm_startup(mode));
 }
 
 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
@@ -274,7 +270,7 @@ static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
        perm = simple_strtoul(argv[2], NULL, 0);
        size = simple_strtoul(argv[3], NULL, 0);
 
-       return convert_return_code(tpm_nv_define_space(index, perm, size));
+       return report_return_code(tpm_nv_define_space(index, perm, size));
 }
 
 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
@@ -295,7 +291,7 @@ static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
@@ -317,7 +313,7 @@ static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
        rc = tpm_nv_write_value(index, data, count);
        free(data);
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
@@ -340,7 +336,7 @@ static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(out_digest, sizeof(out_digest));
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
@@ -361,7 +357,7 @@ static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
@@ -373,7 +369,7 @@ static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_USAGE;
        presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
 
-       return convert_return_code(tpm_tsc_physical_presence(presence));
+       return report_return_code(tpm_tsc_physical_presence(presence));
 }
 
 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
@@ -393,7 +389,7 @@ static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
@@ -405,7 +401,7 @@ static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_USAGE;
        state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
 
-       return convert_return_code(tpm_physical_set_deactivated(state));
+       return report_return_code(tpm_physical_set_deactivated(state));
 }
 
 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
@@ -428,7 +424,7 @@ static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(cap, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 #define TPM_COMMAND_NO_ARG(cmd)                                \
@@ -437,7 +433,7 @@ static int do_##cmd(cmd_tbl_t *cmdtp, int flag,             \
 {                                                      \
        if (argc != 1)                                  \
                return CMD_RET_USAGE;                   \
-       return convert_return_code(cmd());              \
+       return report_return_code(cmd());               \
 }
 
 TPM_COMMAND_NO_ARG(tpm_init)
@@ -447,6 +443,21 @@ TPM_COMMAND_NO_ARG(tpm_force_clear)
 TPM_COMMAND_NO_ARG(tpm_physical_enable)
 TPM_COMMAND_NO_ARG(tpm_physical_disable)
 
+#ifdef CONFIG_DM_TPM
+static int get_tpm(struct udevice **devp)
+{
+       int rc;
+
+       rc = uclass_first_device(UCLASS_TPM, devp);
+       if (rc) {
+               printf("Could not find TPM (ret=%d)\n", rc);
+               return CMD_RET_FAILURE;
+       }
+
+       return 0;
+}
+#endif
+
 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
                int argc, char * const argv[])
 {
@@ -461,14 +472,24 @@ static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_FAILURE;
        }
 
+#ifdef CONFIG_DM_TPM
+       struct udevice *dev;
+
+       rc = get_tpm(&dev);
+       if (rc)
+               return rc;
+
+       rc = tpm_xfer(dev, command, count, response, &response_length);
+#else
        rc = tis_sendrecv(command, count, response, &response_length);
+#endif
        free(command);
        if (!rc) {
                puts("tpm response:\n");
                print_byte_string(response, response_length);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
@@ -486,7 +507,7 @@ static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
        index = simple_strtoul(argv[2], NULL, 0);
        perm = simple_strtoul(argv[3], NULL, 0);
 
-       return convert_return_code(tpm_nv_define_space(index, perm, size));
+       return report_return_code(tpm_nv_define_space(index, perm, size));
 }
 
 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
@@ -515,7 +536,7 @@ static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
        }
        free(data);
 
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
@@ -543,9 +564,75 @@ static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
        err = tpm_nv_write_value(index, data, count);
        free(data);
 
-       return convert_return_code(err);
+       return report_return_code(err);
+}
+
+#ifdef CONFIG_TPM_AUTH_SESSIONS
+
+static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
+               int argc, char * const argv[])
+{
+       uint32_t auth_handle, err;
+
+       err = tpm_oiap(&auth_handle);
+
+       return report_return_code(err);
 }
 
+static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
+               int argc, char * const argv[])
+{
+       uint32_t parent_handle, key_len, key_handle, err;
+       uint8_t usage_auth[DIGEST_LENGTH];
+       void *key;
+
+       if (argc < 5)
+               return CMD_RET_USAGE;
+
+       parent_handle = simple_strtoul(argv[1], NULL, 0);
+       key = (void *)simple_strtoul(argv[2], NULL, 0);
+       key_len = simple_strtoul(argv[3], NULL, 0);
+       if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
+               return CMD_RET_FAILURE;
+       parse_byte_string(argv[4], usage_auth, NULL);
+
+       err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
+                       &key_handle);
+       if (!err)
+               printf("Key handle is 0x%x\n", key_handle);
+
+       return report_return_code(err);
+}
+
+static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
+               int argc, char * const argv[])
+{
+       uint32_t key_handle, err;
+       uint8_t usage_auth[DIGEST_LENGTH];
+       uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
+       size_t pub_key_len = sizeof(pub_key_buffer);
+
+       if (argc < 3)
+               return CMD_RET_USAGE;
+
+       key_handle = simple_strtoul(argv[1], NULL, 0);
+       if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
+               return CMD_RET_FAILURE;
+       parse_byte_string(argv[2], usage_auth, NULL);
+
+       err = tpm_get_pub_key_oiap(key_handle, usage_auth,
+                       pub_key_buffer, &pub_key_len);
+       if (!err) {
+               printf("dump of received pub key structure:\n");
+               print_byte_string(pub_key_buffer, pub_key_len);
+       }
+       return report_return_code(err);
+}
+
+TPM_COMMAND_NO_ARG(tpm_end_oiap)
+
+#endif /* CONFIG_TPM_AUTH_SESSIONS */
+
 #define MAKE_TPM_CMD_ENTRY(cmd) \
        U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
 
@@ -590,6 +677,16 @@ static cmd_tbl_t tpm_commands[] = {
                        do_tpm_nv_read, "", ""),
        U_BOOT_CMD_MKENT(nv_write, 0, 1,
                        do_tpm_nv_write, "", ""),
+#ifdef CONFIG_TPM_AUTH_SESSIONS
+       U_BOOT_CMD_MKENT(oiap, 0, 1,
+                        do_tpm_oiap, "", ""),
+       U_BOOT_CMD_MKENT(end_oiap, 0, 1,
+                        do_tpm_end_oiap, "", ""),
+       U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
+                        do_tpm_load_key2_oiap, "", ""),
+       U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
+                        do_tpm_get_pub_key_oiap, "", ""),
+#endif /* CONFIG_TPM_AUTH_SESSIONS */
 };
 
 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
@@ -638,6 +735,16 @@ U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
 "  get_capability cap_area sub_cap addr count\n"
 "    - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
 "      <sub_cap> to memory address <addr>.\n"
+#ifdef CONFIG_TPM_AUTH_SESSIONS
+"Storage functions\n"
+"  loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
+"    - loads a key data from memory address <key_addr>, <key_len> bytes\n"
+"      into TPM using the parent key <parent_handle> with authorization\n"
+"      <usage_auth> (20 bytes hex string).\n"
+"  get_pub_key_oiap key_handle usage_auth\n"
+"    - get the public key portion of a loaded key <key_handle> using\n"
+"      authorization <usage auth> (20 bytes hex string)\n"
+#endif /* CONFIG_TPM_AUTH_SESSIONS */
 "Endorsement Key Handling Commands:\n"
 "  read_pubek addr count\n"
 "    - Read <count> bytes of the public endorsement key to memory\n"
@@ -648,6 +755,13 @@ U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
 "      <digest_hex_string>\n"
 "  pcr_read index addr count\n"
 "    - Read <count> bytes from PCR <index> to memory address <addr>.\n"
+#ifdef CONFIG_TPM_AUTH_SESSIONS
+"Authorization Sessions\n"
+"  oiap\n"
+"    - setup an OIAP session\n"
+"  end_oiap\n"
+"    - terminates an active OIAP session\n"
+#endif /* CONFIG_TPM_AUTH_SESSIONS */
 "Non-volatile Storage Commands:\n"
 "  nv_define_space index permission size\n"
 "    - Establish a space at index <index> with <permission> of <size> bytes.\n"