1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (c) 2015 - 2018 Intel Corporation
4 * All rights reserved.
5 */
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <inttypes.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #ifndef _WIN32
17 #include <unistd.h>
18 #endif
19
20 #include "tss2_tpm2_types.h"
21 #include "tss2_mu.h"
22
23 #include "tcti-common.h"
24 #define LOGMODULE tcti
25 #include "util/log.h"
26
27 TSS2_TCTI_COMMON_CONTEXT*
tcti_common_context_cast(TSS2_TCTI_CONTEXT * ctx)28 tcti_common_context_cast (TSS2_TCTI_CONTEXT *ctx)
29 {
30 return (TSS2_TCTI_COMMON_CONTEXT*)ctx;
31 }
32
33 TSS2_TCTI_CONTEXT*
tcti_common_down_cast(TSS2_TCTI_COMMON_CONTEXT * ctx)34 tcti_common_down_cast (TSS2_TCTI_COMMON_CONTEXT *ctx)
35 {
36 return (TSS2_TCTI_CONTEXT*)&ctx->v2;
37 }
38
39 TSS2_RC
tcti_common_cancel_checks(TSS2_TCTI_COMMON_CONTEXT * tcti_common)40 tcti_common_cancel_checks (
41 TSS2_TCTI_COMMON_CONTEXT *tcti_common)
42 {
43 if (tcti_common->state != TCTI_STATE_RECEIVE) {
44 return TSS2_TCTI_RC_BAD_SEQUENCE;
45 }
46 return TSS2_RC_SUCCESS;
47 }
48
49 TSS2_RC
tcti_common_transmit_checks(TSS2_TCTI_COMMON_CONTEXT * tcti_common,const uint8_t * command_buffer)50 tcti_common_transmit_checks (
51 TSS2_TCTI_COMMON_CONTEXT *tcti_common,
52 const uint8_t *command_buffer)
53 {
54 if (tcti_common->state != TCTI_STATE_TRANSMIT) {
55 return TSS2_TCTI_RC_BAD_SEQUENCE;
56 }
57 if (command_buffer == NULL) {
58 return TSS2_TCTI_RC_BAD_REFERENCE;
59 }
60
61 return TSS2_RC_SUCCESS;
62 }
63
64 TSS2_RC
tcti_common_receive_checks(TSS2_TCTI_COMMON_CONTEXT * tcti_common,size_t * response_size)65 tcti_common_receive_checks (
66 TSS2_TCTI_COMMON_CONTEXT *tcti_common,
67 size_t *response_size)
68 {
69 if (tcti_common->state != TCTI_STATE_RECEIVE) {
70 return TSS2_TCTI_RC_BAD_SEQUENCE;
71 }
72 if (response_size == NULL) {
73 return TSS2_TCTI_RC_BAD_REFERENCE;
74 }
75
76 return TSS2_RC_SUCCESS;
77 }
78
79 TSS2_RC
tcti_common_set_locality_checks(TSS2_TCTI_COMMON_CONTEXT * tcti_common)80 tcti_common_set_locality_checks (
81 TSS2_TCTI_COMMON_CONTEXT *tcti_common)
82 {
83 if (tcti_common->state != TCTI_STATE_TRANSMIT) {
84 return TSS2_TCTI_RC_BAD_SEQUENCE;
85 }
86 return TSS2_RC_SUCCESS;
87 }
88
89 TSS2_RC
tcti_make_sticky_not_implemented(TSS2_TCTI_CONTEXT * tctiContext,TPM2_HANDLE * handle,uint8_t sticky)90 tcti_make_sticky_not_implemented (
91 TSS2_TCTI_CONTEXT *tctiContext,
92 TPM2_HANDLE *handle,
93 uint8_t sticky)
94 {
95 (void)(tctiContext);
96 (void)(handle);
97 (void)(sticky);
98 return TSS2_TCTI_RC_NOT_IMPLEMENTED;
99 }
100
101 TSS2_RC
header_unmarshal(const uint8_t * buf,tpm_header_t * header)102 header_unmarshal (
103 const uint8_t *buf,
104 tpm_header_t *header)
105 {
106 TSS2_RC rc;
107 size_t offset = 0;
108
109 LOG_TRACE ("Parsing header from buffer: 0x%" PRIxPTR, (uintptr_t)buf);
110 rc = Tss2_MU_TPM2_ST_Unmarshal (buf,
111 TPM_HEADER_SIZE,
112 &offset,
113 &header->tag);
114 if (rc != TSS2_RC_SUCCESS) {
115 LOG_ERROR ("Failed to unmarshal tag.");
116 return rc;
117 }
118 rc = Tss2_MU_UINT32_Unmarshal (buf,
119 TPM_HEADER_SIZE,
120 &offset,
121 &header->size);
122 if (rc != TSS2_RC_SUCCESS) {
123 LOG_ERROR ("Failed to unmarshal command size.");
124 return rc;
125 }
126 rc = Tss2_MU_UINT32_Unmarshal (buf,
127 TPM_HEADER_SIZE,
128 &offset,
129 &header->code);
130 if (rc != TSS2_RC_SUCCESS) {
131 LOG_ERROR ("Failed to unmarshal command code.");
132 }
133 return rc;
134 }
135
136 TSS2_RC
header_marshal(const tpm_header_t * header,uint8_t * buf)137 header_marshal (
138 const tpm_header_t *header,
139 uint8_t *buf)
140 {
141 TSS2_RC rc;
142 size_t offset = 0;
143
144 LOG_TRACE ("Parsing header from buffer: 0x%" PRIxPTR, (uintptr_t)buf);
145 rc = Tss2_MU_TPM2_ST_Marshal (header->tag,
146 buf,
147 TPM_HEADER_SIZE,
148 &offset);
149 if (rc != TSS2_RC_SUCCESS) {
150 LOG_ERROR ("Failed to marshal tag.");
151 return rc;
152 }
153 rc = Tss2_MU_UINT32_Marshal (header->size,
154 buf,
155 TPM_HEADER_SIZE,
156 &offset);
157 if (rc != TSS2_RC_SUCCESS) {
158 LOG_ERROR ("Failed to marshal command size.");
159 return rc;
160 }
161 rc = Tss2_MU_UINT32_Marshal (header->code,
162 buf,
163 TPM_HEADER_SIZE,
164 &offset);
165 if (rc != TSS2_RC_SUCCESS) {
166 LOG_ERROR ("Failed to marshal command code.");
167 }
168 return rc;
169 }
170