rpc_aik.c revision 1.1
1 2/* 3 * Licensed Materials - Property of IBM 4 * 5 * trousers - An open source TCG Software Stack 6 * 7 * (C) Copyright International Business Machines Corp. 2004-2006 8 * 9 */ 10 11#include <stdlib.h> 12#include <stdio.h> 13#include <syslog.h> 14#include <string.h> 15#include <netdb.h> 16 17#include "trousers/tss.h" 18#include "trousers_types.h" 19#include "tcs_tsp.h" 20#include "tcs_utils.h" 21#include "tcs_int_literals.h" 22#include "capabilities.h" 23#include "tcslog.h" 24#include "tcsd_wrap.h" 25#include "tcsd.h" 26#include "tcs_utils.h" 27#include "rpc_tcstp_tcs.h" 28 29 30TSS_RESULT 31tcs_wrap_MakeIdentity(struct tcsd_thread_data *data) 32{ 33 TCS_CONTEXT_HANDLE hContext; 34 TCPA_ENCAUTH identityAuth; 35 TCPA_CHOSENID_HASH privCAHash; 36 UINT32 idKeyInfoSize; 37 BYTE *idKeyInfo = NULL; 38 39 TPM_AUTH auth1, auth2; 40 TPM_AUTH *pSRKAuth, *pOwnerAuth; 41 42 UINT32 idKeySize; 43 BYTE *idKey = NULL; 44 UINT32 pcIDBindSize; 45 BYTE *prgbIDBind = NULL; 46 UINT32 pcECSize; 47 BYTE *prgbEC = NULL; 48 UINT32 pcPlatCredSize; 49 BYTE *prgbPlatCred = NULL; 50 UINT32 pcConfCredSize; 51 BYTE *prgbConfCred = NULL; 52 TSS_RESULT result; 53 54 int i; 55 56 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 57 return TCSERR(TSS_E_INTERNAL_ERROR); 58 59 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 60 61 if (getData(TCSD_PACKET_TYPE_ENCAUTH, 1, &identityAuth, 0, &data->comm)) 62 return TCSERR(TSS_E_INTERNAL_ERROR); 63 if (getData(TCSD_PACKET_TYPE_DIGEST, 2, &privCAHash, 0, &data->comm)) 64 return TCSERR(TSS_E_INTERNAL_ERROR); 65 66 if (getData(TCSD_PACKET_TYPE_UINT32, 3, &idKeyInfoSize, 0, &data->comm)) 67 return TCSERR(TSS_E_INTERNAL_ERROR); 68 idKeyInfo = (BYTE *) calloc(1, idKeyInfoSize); 69 if (idKeyInfo == NULL) { 70 LogError("malloc of %d bytes failed.", idKeyInfoSize); 71 return TCSERR(TSS_E_OUTOFMEMORY); 72 } 73 if (getData(TCSD_PACKET_TYPE_PBYTE, 4, idKeyInfo, idKeyInfoSize, &data->comm)) { 74 free(idKeyInfo); 75 return TCSERR(TSS_E_INTERNAL_ERROR); 76 } 77 if (getData(TCSD_PACKET_TYPE_AUTH, 5, &auth1, 0, &data->comm)) { 78 free(idKeyInfo); 79 return TCSERR(TSS_E_INTERNAL_ERROR); 80 } 81 82 result = getData(TCSD_PACKET_TYPE_AUTH, 6, &auth2, 0, &data->comm); 83 if (result == TSS_TCP_RPC_BAD_PACKET_TYPE) { 84 pOwnerAuth = &auth1; 85 pSRKAuth = NULL; 86 } else if (result) { 87 free(idKeyInfo); 88 return result; 89 } else { 90 pOwnerAuth = &auth2; 91 pSRKAuth = &auth1; 92 } 93 94 MUTEX_LOCK(tcsp_lock); 95 96 result = TCSP_MakeIdentity_Internal(hContext, identityAuth, privCAHash, 97 idKeyInfoSize, idKeyInfo, pSRKAuth, 98 pOwnerAuth, &idKeySize, &idKey, 99 &pcIDBindSize, &prgbIDBind, &pcECSize, 100 &prgbEC, &pcPlatCredSize, &prgbPlatCred, 101 &pcConfCredSize, &prgbConfCred); 102 103 MUTEX_UNLOCK(tcsp_lock); 104 free(idKeyInfo); 105 106 if (result == TSS_SUCCESS) { 107 i = 0; 108 initData(&data->comm, 12); 109 if (pSRKAuth) { 110 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pSRKAuth, 0, &data->comm)) 111 goto internal_error; 112 } 113 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pOwnerAuth, 0, &data->comm)) 114 goto internal_error; 115 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &idKeySize, 0, &data->comm)) 116 goto internal_error; 117 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, idKey, idKeySize, &data->comm)) 118 goto internal_error; 119 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcIDBindSize, 0, &data->comm)) 120 goto internal_error; 121 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbIDBind, pcIDBindSize, &data->comm)) 122 goto internal_error; 123 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcECSize, 0, &data->comm)) 124 goto internal_error; 125 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbEC, pcECSize, &data->comm)) 126 goto internal_error; 127 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcPlatCredSize, 0, &data->comm)) 128 goto internal_error; 129 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbPlatCred, pcPlatCredSize, &data->comm)) 130 goto internal_error; 131 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &pcConfCredSize, 0, &data->comm)) 132 goto internal_error; 133 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, prgbConfCred, pcConfCredSize, &data->comm)) 134 goto internal_error; 135 136 free(idKey); 137 free(prgbIDBind); 138 free(prgbEC); 139 free(prgbPlatCred); 140 free(prgbConfCred); 141 } else 142 initData(&data->comm, 0); 143 144 data->comm.hdr.u.result = result; 145 146 return TSS_SUCCESS; 147 148internal_error: 149 free(idKey); 150 free(prgbIDBind); 151 free(prgbEC); 152 free(prgbPlatCred); 153 free(prgbConfCred); 154 return TCSERR(TSS_E_INTERNAL_ERROR); 155} 156 157TSS_RESULT 158tcs_wrap_ActivateIdentity(struct tcsd_thread_data *data) 159{ 160 TCS_CONTEXT_HANDLE hContext; 161 TCS_KEY_HANDLE idKeyHandle; 162 TPM_AUTH *pIdKeyAuth = NULL, *pOwnerAuth = NULL, auth1, auth2; 163 UINT32 SymmetricKeySize, blobSize; 164 BYTE *SymmetricKey, *blob; 165 TSS_RESULT result; 166 UINT32 i; 167 168 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 169 return TCSERR(TSS_E_INTERNAL_ERROR); 170 171 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 172 173 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idKeyHandle, 0, &data->comm)) 174 return TCSERR(TSS_E_INTERNAL_ERROR); 175 if (getData(TCSD_PACKET_TYPE_UINT32, 2, &blobSize, 0, &data->comm)) 176 return TCSERR(TSS_E_INTERNAL_ERROR); 177 178 if ((blob = malloc(blobSize)) == NULL) 179 return TCSERR(TSS_E_OUTOFMEMORY); 180 181 if (getData(TCSD_PACKET_TYPE_PBYTE, 3, blob, blobSize, &data->comm)) { 182 free(blob); 183 return TCSERR(TSS_E_INTERNAL_ERROR); 184 } 185 186 if (getData(TCSD_PACKET_TYPE_AUTH, 4, &auth1, 0, &data->comm)) { 187 free(blob); 188 return TCSERR(TSS_E_INTERNAL_ERROR); 189 } 190 191 result = getData(TCSD_PACKET_TYPE_AUTH, 5, &auth2, 0, &data->comm); 192 if (result == TSS_TCP_RPC_BAD_PACKET_TYPE) 193 pOwnerAuth = &auth1; 194 else if (result) { 195 free(blob); 196 return result; 197 } else { 198 pIdKeyAuth = &auth1; 199 pOwnerAuth = &auth2; 200 } 201 202 MUTEX_LOCK(tcsp_lock); 203 204 result = TCSP_ActivateTPMIdentity_Internal(hContext, idKeyHandle, blobSize, 205 blob, pIdKeyAuth, pOwnerAuth, 206 &SymmetricKeySize, 207 &SymmetricKey); 208 209 MUTEX_UNLOCK(tcsp_lock); 210 free(blob); 211 212 if (result == TSS_SUCCESS) { 213 i = 0; 214 initData(&data->comm, 4); 215 if (pIdKeyAuth) { 216 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pIdKeyAuth, 0, &data->comm)) { 217 free(SymmetricKey); 218 return TCSERR(TSS_E_INTERNAL_ERROR); 219 } 220 } 221 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pOwnerAuth, 0, &data->comm)) { 222 free(SymmetricKey); 223 return TCSERR(TSS_E_INTERNAL_ERROR); 224 } 225 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &SymmetricKeySize, 0, &data->comm)) { 226 free(SymmetricKey); 227 return TCSERR(TSS_E_INTERNAL_ERROR); 228 } 229 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, SymmetricKey, SymmetricKeySize, &data->comm)) { 230 free(SymmetricKey); 231 return TCSERR(TSS_E_INTERNAL_ERROR); 232 } 233 free(SymmetricKey); 234 } else 235 initData(&data->comm, 0); 236 237 data->comm.hdr.u.result = result; 238 239 return TSS_SUCCESS; 240} 241 242#ifdef TSS_BUILD_TSS12 243TSS_RESULT 244tcs_wrap_GetCredential(struct tcsd_thread_data *data) 245{ 246 TCS_CONTEXT_HANDLE hContext; 247 UINT32 CredType; 248 UINT32 CredAccessMode; 249 UINT32 CredSize; 250 BYTE *CredData = NULL; 251 TSS_RESULT result; 252 253 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 254 return TCSERR(TSS_E_INTERNAL_ERROR); 255 256 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &CredType, 0, &data->comm)) 257 return TCSERR(TSS_E_INTERNAL_ERROR); 258 259 if (getData(TCSD_PACKET_TYPE_UINT32, 2, &CredAccessMode, 0, &data->comm)) 260 return TCSERR(TSS_E_INTERNAL_ERROR); 261 262 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 263 264 result = TCS_GetCredential_Internal(hContext, CredType, CredAccessMode, 265 &CredSize, &CredData); 266 267 if (result == TSS_SUCCESS) { 268 initData(&data->comm, 2); 269 if (setData(TCSD_PACKET_TYPE_UINT32, 0, &CredSize, 0, &data->comm)) 270 goto internal_error; 271 if (setData(TCSD_PACKET_TYPE_PBYTE, 1, CredData, CredSize, &data->comm)) 272 goto internal_error; 273 274 free(CredData); 275 } else 276 initData(&data->comm, 0); 277 278 data->comm.hdr.u.result = result; 279 return TSS_SUCCESS; 280 281internal_error: 282 free(CredData); 283 return TCSERR(TSS_E_INTERNAL_ERROR); 284} 285#endif 286