blob: ab9f6831aba7cab86912035e961e26b0a538f356 [file] [log] [blame]
Peter Huewed5a36f62018-06-12 00:59:26 +02001/* SPDX-License-Identifier: BSD-2 */
Philip Tricca21fa3212018-02-19 06:43:56 -08002/*
3 * Copyright (c) 2015 - 2018 Intel Corporation
Philip Triccaa003d762017-10-17 13:25:42 -07004 * All rights reserved.
Philip Tricca21fa3212018-02-19 06:43:56 -08005 */
wcarthureedecd62015-11-20 16:59:45 -05006
Philip Triccaa003ae62018-02-19 10:32:46 -08007#include <inttypes.h>
wcarthureedecd62015-11-20 16:59:45 -05008#include <stdio.h>
Philip Triccaa003d762017-10-17 13:25:42 -07009#include <stdlib.h>
Philip Tricca910f17c2018-03-15 12:38:37 -070010#include <string.h>
David J. Maria06db5752018-06-19 14:52:37 -070011
12#ifndef _WIN32
Philip Triccaa003ae62018-02-19 10:32:46 -080013#include <unistd.h>
David J. Maria06db5752018-06-19 14:52:37 -070014#endif
wcarthureedecd62015-11-20 16:59:45 -050015
Philip Tricca910f17c2018-03-15 12:38:37 -070016#include "tss2_tpm2_types.h"
17#include "tss2_mu.h"
18
Philip Tricca850bb592018-04-03 09:29:22 -070019#include "tcti-common.h"
Philip Triccaa003ae62018-02-19 10:32:46 -080020#define LOGMODULE tcti
Philip Triccaa7c51ce2018-03-10 18:28:25 -080021#include "util/log.h"
wcarthureedecd62015-11-20 16:59:45 -050022
Philip Tricca850bb592018-04-03 09:29:22 -070023TSS2_TCTI_COMMON_CONTEXT*
24tcti_common_context_cast (TSS2_TCTI_CONTEXT *ctx)
wcarthureedecd62015-11-20 16:59:45 -050025{
Philip Tricca850bb592018-04-03 09:29:22 -070026 return (TSS2_TCTI_COMMON_CONTEXT*)ctx;
27}
Philip Triccadfa41a52016-07-20 17:43:57 -070028
Philip Tricca850bb592018-04-03 09:29:22 -070029TSS2_TCTI_CONTEXT*
30tcti_common_down_cast (TSS2_TCTI_COMMON_CONTEXT *ctx)
31{
32 return (TSS2_TCTI_CONTEXT*)&ctx->v2;
33}
34
35TSS2_RC
36tcti_common_cancel_checks (
37 TSS2_TCTI_COMMON_CONTEXT *tcti_common)
38{
39 if (tcti_common->state != TCTI_STATE_RECEIVE) {
40 return TSS2_TCTI_RC_BAD_SEQUENCE;
41 }
Tadeusz Struk32aadee2017-06-14 13:23:42 -070042 return TSS2_RC_SUCCESS;
wcarthureedecd62015-11-20 16:59:45 -050043}
44
Philip Triccacac4c3a2018-03-05 18:21:41 -080045TSS2_RC
Philip Tricca850bb592018-04-03 09:29:22 -070046tcti_common_transmit_checks (
47 TSS2_TCTI_COMMON_CONTEXT *tcti_common,
Philip Triccacac4c3a2018-03-05 18:21:41 -080048 const uint8_t *command_buffer)
Philip Tricca3f1828c2017-10-18 18:44:13 -070049{
Philip Tricca850bb592018-04-03 09:29:22 -070050 if (tcti_common->state != TCTI_STATE_TRANSMIT) {
Philip Triccafa14abe2018-03-05 21:14:54 -080051 return TSS2_TCTI_RC_BAD_SEQUENCE;
52 }
Philip Tricca3f1828c2017-10-18 18:44:13 -070053 if (command_buffer == NULL) {
54 return TSS2_TCTI_RC_BAD_REFERENCE;
55 }
Philip Tricca3f1828c2017-10-18 18:44:13 -070056
57 return TSS2_RC_SUCCESS;
58}
wcarthureedecd62015-11-20 16:59:45 -050059
Philip Triccacac4c3a2018-03-05 18:21:41 -080060TSS2_RC
Philip Tricca850bb592018-04-03 09:29:22 -070061tcti_common_receive_checks (
62 TSS2_TCTI_COMMON_CONTEXT *tcti_common,
Andreas Fuchsbaeb2be2018-03-29 15:39:50 +020063 size_t *response_size)
wcarthureedecd62015-11-20 16:59:45 -050064{
Philip Tricca850bb592018-04-03 09:29:22 -070065 if (tcti_common->state != TCTI_STATE_RECEIVE) {
Philip Triccafa14abe2018-03-05 21:14:54 -080066 return TSS2_TCTI_RC_BAD_SEQUENCE;
67 }
Philip Tricca25789b22018-03-07 15:23:47 -080068 if (response_size == NULL) {
Tadeusz Struk32aadee2017-06-14 13:23:42 -070069 return TSS2_TCTI_RC_BAD_REFERENCE;
Philip Triccadfa41a52016-07-20 17:43:57 -070070 }
Philip Triccadfa41a52016-07-20 17:43:57 -070071
Tadeusz Struk32aadee2017-06-14 13:23:42 -070072 return TSS2_RC_SUCCESS;
Philip Tricca4ea417c2016-01-24 23:10:03 +000073}
Philip Triccaa003ae62018-02-19 10:32:46 -080074
Philip Tricca850bb592018-04-03 09:29:22 -070075TSS2_RC
76tcti_common_set_locality_checks (
77 TSS2_TCTI_COMMON_CONTEXT *tcti_common)
78{
79 if (tcti_common->state != TCTI_STATE_TRANSMIT) {
80 return TSS2_TCTI_RC_BAD_SEQUENCE;
81 }
82 return TSS2_RC_SUCCESS;
83}
Philip Triccaee411152018-02-23 19:16:04 -080084
Philip Triccacac4c3a2018-03-05 18:21:41 -080085TSS2_RC
86tcti_make_sticky_not_implemented (
Philip Triccaee411152018-02-23 19:16:04 -080087 TSS2_TCTI_CONTEXT *tctiContext,
88 TPM2_HANDLE *handle,
89 uint8_t sticky)
90{
Andreas Fuchsbaeb2be2018-03-29 15:39:50 +020091 (void)(tctiContext);
92 (void)(handle);
93 (void)(sticky);
Philip Triccaee411152018-02-23 19:16:04 -080094 return TSS2_TCTI_RC_NOT_IMPLEMENTED;
95}
Philip Tricca7a10ee32018-03-06 12:48:39 -080096
97TSS2_RC
Philip Tricca9d4e41e2018-03-27 08:22:54 -070098header_unmarshal (
Philip Tricca7a10ee32018-03-06 12:48:39 -080099 const uint8_t *buf,
100 tpm_header_t *header)
101{
102 TSS2_RC rc;
103 size_t offset = 0;
104
105 LOG_TRACE ("Parsing header from buffer: 0x%" PRIxPTR, (uintptr_t)buf);
106 rc = Tss2_MU_TPM2_ST_Unmarshal (buf,
107 TPM_HEADER_SIZE,
108 &offset,
109 &header->tag);
110 if (rc != TSS2_RC_SUCCESS) {
111 LOG_ERROR ("Failed to unmarshal tag.");
112 return rc;
113 }
114 rc = Tss2_MU_UINT32_Unmarshal (buf,
115 TPM_HEADER_SIZE,
116 &offset,
117 &header->size);
118 if (rc != TSS2_RC_SUCCESS) {
119 LOG_ERROR ("Failed to unmarshal command size.");
120 return rc;
121 }
122 rc = Tss2_MU_UINT32_Unmarshal (buf,
123 TPM_HEADER_SIZE,
124 &offset,
125 &header->code);
126 if (rc != TSS2_RC_SUCCESS) {
127 LOG_ERROR ("Failed to unmarshal command code.");
128 }
129 return rc;
130}
Philip Tricca0b870442018-03-26 14:27:28 -0700131
132TSS2_RC
133header_marshal (
134 const tpm_header_t *header,
135 uint8_t *buf)
136{
137 TSS2_RC rc;
138 size_t offset = 0;
139
140 LOG_TRACE ("Parsing header from buffer: 0x%" PRIxPTR, (uintptr_t)buf);
141 rc = Tss2_MU_TPM2_ST_Marshal (header->tag,
142 buf,
143 TPM_HEADER_SIZE,
144 &offset);
145 if (rc != TSS2_RC_SUCCESS) {
146 LOG_ERROR ("Failed to marshal tag.");
147 return rc;
148 }
149 rc = Tss2_MU_UINT32_Marshal (header->size,
150 buf,
151 TPM_HEADER_SIZE,
152 &offset);
153 if (rc != TSS2_RC_SUCCESS) {
154 LOG_ERROR ("Failed to marshal command size.");
155 return rc;
156 }
157 rc = Tss2_MU_UINT32_Marshal (header->code,
158 buf,
159 TPM_HEADER_SIZE,
160 &offset);
161 if (rc != TSS2_RC_SUCCESS) {
162 LOG_ERROR ("Failed to marshal command code.");
163 }
164 return rc;
165}