1 /********************************************************************************/
2 /*										*/
3 /*		Common TPM 1.2 and TPM 2.0 TSS Authorization 			*/
4 /*			     Written by Ken Goldman				*/
5 /*		       IBM Thomas J. Watson Research Center			*/
6 /*										*/
7 /* (c) Copyright IBM Corporation 2015 - 2019.					*/
8 /*										*/
9 /* All rights reserved.								*/
10 /* 										*/
11 /* Redistribution and use in source and binary forms, with or without		*/
12 /* modification, are permitted provided that the following conditions are	*/
13 /* met:										*/
14 /* 										*/
15 /* Redistributions of source code must retain the above copyright notice,	*/
16 /* this list of conditions and the following disclaimer.			*/
17 /* 										*/
18 /* Redistributions in binary form must reproduce the above copyright		*/
19 /* notice, this list of conditions and the following disclaimer in the		*/
20 /* documentation and/or other materials provided with the distribution.		*/
21 /* 										*/
22 /* Neither the names of the IBM Corporation nor the names of its		*/
23 /* contributors may be used to endorse or promote products derived from		*/
24 /* this software without specific prior written permission.			*/
25 /* 										*/
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS		*/
27 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT		*/
28 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR	*/
29 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT		*/
30 /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,	*/
31 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT		*/
32 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,	*/
33 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY	*/
34 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT		*/
35 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE	*/
36 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		*/
37 /********************************************************************************/
38 
39 /* This layer handles command and response packet authorization parameters. */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdarg.h>
46 
47 #ifdef TPM_POSIX
48 #include <netinet/in.h>
49 #endif
50 #ifdef TPM_WINDOWS
51 #include <winsock2.h>
52 #endif
53 
54 #include <ibmtss/tsserror.h>
55 #include <ibmtss/tssprint.h>
56 #include <ibmtss/tssutils.h>
57 #include <ibmtss/tssmarshal.h>
58 #include <ibmtss/Unmarshal_fp.h>
59 #include <ibmtss/tsstransmit.h>
60 #include "tssproperties.h"
61 #include <ibmtss/tssresponsecode.h>
62 
63 #include "tssauth.h"
64 
65 extern int tssVerbose;
66 extern int tssVverbose;
67 
68 /* TSS_AuthCreate() allocates and initializes a TSS_AUTH_CONTEXT */
69 
TSS_AuthCreate(TSS_AUTH_CONTEXT ** tssAuthContext)70 TPM_RC TSS_AuthCreate(TSS_AUTH_CONTEXT **tssAuthContext)
71 {
72     TPM_RC rc = 0;
73     if (rc == 0) {
74         rc = TSS_Malloc((uint8_t **)tssAuthContext, sizeof(TSS_AUTH_CONTEXT));
75    }
76     if (rc == 0) {
77 	TSS_InitAuthContext(*tssAuthContext);
78     }
79     return rc;
80 }
81 
82 /* TSS_InitAuthContext() sets initial values for an allocated TSS_AUTH_CONTEXT */
83 
TSS_InitAuthContext(TSS_AUTH_CONTEXT * tssAuthContext)84 void TSS_InitAuthContext(TSS_AUTH_CONTEXT *tssAuthContext)
85 {
86     memset(tssAuthContext->commandBuffer, 0, sizeof(tssAuthContext->commandBuffer));
87     memset(tssAuthContext->responseBuffer, 0, sizeof(tssAuthContext->responseBuffer));
88     tssAuthContext->commandText = NULL;
89     tssAuthContext->commandCode = 0;
90     tssAuthContext->responseCode = 0;
91     tssAuthContext->commandHandleCount = 0;
92     tssAuthContext->responseHandleCount = 0;
93     tssAuthContext->authCount = 0;
94     tssAuthContext->commandSize = 0;
95     tssAuthContext->cpBufferSize = 0;
96     tssAuthContext->cpBuffer = NULL;
97     tssAuthContext->responseSize = 0;
98     tssAuthContext->marshalInFunction = NULL;
99     tssAuthContext->unmarshalOutFunction = NULL;
100 #ifndef TPM_TSS_NOCMDCHECK
101     tssAuthContext->unmarshalInFunction = NULL;
102 #endif
103 #ifdef TPM_TPM12
104     tssAuthContext->sessionNumber = 0xffff;	/* no encrypt sessions */
105     tssAuthContext->encAuthOffset0 = 0;
106     tssAuthContext->encAuthOffset1 = 0;
107 #endif
108     return;
109 }
110 
111 /* TSS_AuthDelete() re-initializes and then frees an allocated TSS_AUTH_CONTEXT */
112 
TSS_AuthDelete(TSS_AUTH_CONTEXT * tssAuthContext)113 TPM_RC TSS_AuthDelete(TSS_AUTH_CONTEXT *tssAuthContext)
114 {
115     if (tssAuthContext != NULL) {
116 	TSS_InitAuthContext(tssAuthContext);
117 	free(tssAuthContext);
118     }
119     return 0;
120 }
121 
TSS_GetCommandCode(TSS_AUTH_CONTEXT * tssAuthContext)122 TPM_CC TSS_GetCommandCode(TSS_AUTH_CONTEXT *tssAuthContext)
123 {
124     TPM_CC commandCode = tssAuthContext->commandCode;
125     return commandCode;
126 }
127 
TSS_GetCpBuffer(TSS_AUTH_CONTEXT * tssAuthContext,uint32_t * cpBufferSize,uint8_t ** cpBuffer)128 TPM_RC TSS_GetCpBuffer(TSS_AUTH_CONTEXT *tssAuthContext,
129 		       uint32_t *cpBufferSize,
130 		       uint8_t **cpBuffer)
131 {
132     *cpBufferSize = tssAuthContext->cpBufferSize;
133     *cpBuffer = tssAuthContext->cpBuffer;
134     return 0;
135 }
136 
137 /* TSS_GetCommandHandleCount() returns the number of handles in the command area */
138 
TSS_GetCommandHandleCount(TSS_AUTH_CONTEXT * tssAuthContext,size_t * commandHandleCount)139 TPM_RC TSS_GetCommandHandleCount(TSS_AUTH_CONTEXT *tssAuthContext,
140 				 size_t *commandHandleCount)
141 {
142     *commandHandleCount = tssAuthContext->commandHandleCount;
143     return 0;
144 }
145 
TSS_AuthExecute(TSS_CONTEXT * tssContext)146 TPM_RC TSS_AuthExecute(TSS_CONTEXT *tssContext)
147 {
148     TPM_RC rc = 0;
149     if (tssVverbose) printf("TSS_AuthExecute: Executing %s\n",
150 			    tssContext->tssAuthContext->commandText);
151     /* transmit the command and receive the response.  Normally returns the TPM response code. */
152     if (rc == 0) {
153 	rc = TSS_Transmit(tssContext,
154 			  tssContext->tssAuthContext->responseBuffer,
155 			  &tssContext->tssAuthContext->responseSize,
156 			  tssContext->tssAuthContext->commandBuffer,
157 			  tssContext->tssAuthContext->commandSize,
158 			  tssContext->tssAuthContext->commandText);
159     }
160     return rc;
161 }
162