1 /* Software-based Trusted Platform Module (TPM) Emulator
2  * Copyright (C) 2004-2010 Mario Strasser <mast@gmx.net>
3  *
4  * This module is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This module is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * $Id: tpm_handles.c 364 2010-02-11 10:24:45Z mast $
15  */
16 
17 #include "tpm_emulator.h"
18 #include "tpm_handles.h"
19 #include "tpm_data.h"
20 
tpm_get_key_slot(TPM_KEY_HANDLE handle)21 TPM_KEY_DATA *tpm_get_key_slot(TPM_KEY_HANDLE handle)
22 {
23   if (handle == TPM_INVALID_HANDLE) return NULL;
24   handle &= 0x00ffffff;
25   if (handle >= TPM_MAX_KEYS) return NULL;
26   return &tpmData.permanent.data.keys[handle];
27 }
28 
tpm_get_session_slot(TPM_HANDLE handle)29 TPM_SESSION_DATA *tpm_get_session_slot(TPM_HANDLE handle)
30 {
31   if (handle == TPM_INVALID_HANDLE) return NULL;
32   handle &= 0x00ffffff;
33   if (handle >= TPM_MAX_SESSIONS) return NULL;
34   return &tpmData.stany.data.sessions[handle];
35 }
36 
tpm_get_daa_slot(TPM_HANDLE handle)37 TPM_DAA_SESSION_DATA *tpm_get_daa_slot(TPM_HANDLE handle)
38 {
39   if (handle == TPM_INVALID_HANDLE) return NULL;
40   handle &= 0x00ffffff;
41   if (handle >= TPM_MAX_SESSIONS_DAA) return NULL;
42   return &tpmData.stany.data.sessionsDAA[handle];
43 }
44 
tpm_get_key(TPM_KEY_HANDLE handle)45 TPM_KEY_DATA *tpm_get_key(TPM_KEY_HANDLE handle)
46 {
47   /* handle reserved key handles */
48   switch (handle) {
49     case TPM_KH_EK:
50     case TPM_KH_OWNER:
51     case TPM_KH_REVOKE:
52     case TPM_KH_TRANSPORT:
53     case TPM_KH_OPERATOR:
54     case TPM_KH_ADMIN:
55       return NULL;
56     case TPM_KH_SRK:
57       debug("SRK valid? %d", tpmData.permanent.data.srk.payload);
58       return (tpmData.permanent.data.srk.payload) ?
59         &tpmData.permanent.data.srk : NULL;
60   }
61   if (handle == TPM_INVALID_HANDLE
62       || (handle >> 24) != TPM_RT_KEY) return NULL;
63   handle &= 0x00ffffff;
64   if (handle >= TPM_MAX_KEYS
65       || !tpmData.permanent.data.keys[handle].payload) return NULL;
66   return &tpmData.permanent.data.keys[handle];
67 }
68 
tpm_get_auth(TPM_AUTHHANDLE handle)69 TPM_SESSION_DATA *tpm_get_auth(TPM_AUTHHANDLE handle)
70 {
71   if (handle == TPM_INVALID_HANDLE
72       || (handle >> 24) != TPM_RT_AUTH) return NULL;
73   handle &= 0x00ffffff;
74   if (handle >= TPM_MAX_SESSIONS
75       || (tpmData.stany.data.sessions[handle].type != TPM_ST_OIAP
76           && tpmData.stany.data.sessions[handle].type != TPM_ST_OSAP
77           && tpmData.stany.data.sessions[handle].type != TPM_ST_DSAP)) return NULL;
78   return &tpmData.stany.data.sessions[handle];
79 }
80 
tpm_get_transport(TPM_TRANSHANDLE handle)81 TPM_SESSION_DATA *tpm_get_transport(TPM_TRANSHANDLE handle)
82 {
83   if (handle == TPM_INVALID_HANDLE
84       || (handle >> 24) != TPM_RT_TRANS) return NULL;
85   handle &= 0x00ffffff;
86   if (handle >= TPM_MAX_SESSIONS
87       || tpmData.stany.data.sessions[handle].type != TPM_ST_TRANSPORT) return NULL;
88   return &tpmData.stany.data.sessions[handle];
89 }
90 
tpm_get_counter(TPM_COUNT_ID handle)91 TPM_COUNTER_VALUE *tpm_get_counter(TPM_COUNT_ID handle)
92 {
93   if ((handle == TPM_INVALID_HANDLE) || ((handle >> 24) != TPM_RT_COUNTER))
94     return NULL;
95   handle &= 0x00ffffff;
96   if ((handle >= TPM_MAX_COUNTERS)
97     || !tpmData.permanent.data.counters[handle].valid) return NULL;
98   return &tpmData.permanent.data.counters[handle];
99 }
100 
tpm_get_daa(TPM_DAAHANDLE handle)101 TPM_DAA_SESSION_DATA *tpm_get_daa(TPM_DAAHANDLE handle)
102 {
103   if ((handle == TPM_INVALID_HANDLE) || ((handle >> 24) != TPM_RT_DAA_TPM))
104     return NULL;
105   handle &= 0x00ffffff;
106   if ((handle >= TPM_MAX_SESSIONS_DAA)
107     || (tpmData.stany.data.sessionsDAA[handle].type != TPM_ST_DAA)) return NULL;
108   return &tpmData.stany.data.sessionsDAA[handle];
109 }
110