~ubuntu-branches/ubuntu/gutsy/trousers/gutsy

« back to all changes in this revision

Viewing changes to src/tspi/obj_context.c

  • Committer: Bazaar Package Importer
  • Author(s): William Lima
  • Date: 2007-04-18 16:39:38 UTC
  • Revision ID: james.westby@ubuntu.com-20070418163938-opscl2mvvi76jiec
Tags: upstream-0.2.9.1
ImportĀ upstreamĀ versionĀ 0.2.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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. 2005, 2006
 
8
 *
 
9
 */
 
10
 
 
11
 
 
12
#include <stdlib.h>
 
13
#include <stdio.h>
 
14
#include <errno.h>
 
15
#include <string.h>
 
16
#include <pthread.h>
 
17
 
 
18
#include "trousers/tss.h"
 
19
#include "trousers/trousers.h"
 
20
#include "spi_internal_types.h"
 
21
#include "spi_utils.h"
 
22
#include "capabilities.h"
 
23
#include "tsplog.h"
 
24
#include "obj.h"
 
25
 
 
26
 
 
27
TSS_RESULT
 
28
obj_context_add(TSS_HOBJECT *phObject)
 
29
{
 
30
        TSS_RESULT result;
 
31
        struct tr_context_obj *context = calloc(1, sizeof(struct tr_context_obj));
 
32
        unsigned len = strlen(TSS_LOCALHOST_STRING) + 1;
 
33
 
 
34
        if (context == NULL) {
 
35
                LogError("malloc of %zd bytes failed.",
 
36
                                sizeof(struct tr_context_obj));
 
37
                return TSPERR(TSS_E_OUTOFMEMORY);
 
38
        }
 
39
 
 
40
#ifndef TSS_NO_GUI
 
41
        context->silentMode = TSS_TSPATTRIB_CONTEXT_NOT_SILENT;
 
42
#else
 
43
        context->silentMode = TSS_TSPATTRIB_CONTEXT_SILENT;
 
44
#endif
 
45
        if ((context->machineName = calloc(1, len)) == NULL) {
 
46
                LogError("malloc of %d bytes failed", len);
 
47
                free(context);
 
48
                return TSPERR(TSS_E_OUTOFMEMORY);
 
49
        }
 
50
        memcpy(context->machineName, TSS_LOCALHOST_STRING, len);
 
51
        context->machineNameLength = len;
 
52
 
 
53
#ifndef TSS_SPEC_COMPLIANCE
 
54
        context->hashMode = TSS_TSPATTRIB_HASH_MODE_NULL;
 
55
#endif
 
56
 
 
57
        if ((result = obj_list_add(&context_list, NULL_HCONTEXT, 0, context, phObject))) {
 
58
                free(context);
 
59
                return result;
 
60
        }
 
61
 
 
62
        if ((obj_policy_add(*phObject, TSS_POLICY_USAGE, &context->policy))) {
 
63
                obj_close_context(*phObject);
 
64
                return TSPERR(TSS_E_INTERNAL_ERROR);
 
65
        }
 
66
 
 
67
        return TSS_SUCCESS;
 
68
}
 
69
 
 
70
TSS_BOOL
 
71
obj_is_context(TSS_HOBJECT hObject)
 
72
{
 
73
        TSS_BOOL answer = FALSE;
 
74
 
 
75
        if ((obj_list_get_obj(&context_list, hObject))) {
 
76
                answer = TRUE;
 
77
                obj_list_put(&context_list);
 
78
        }
 
79
 
 
80
        return answer;
 
81
}
 
82
 
 
83
#if 0
 
84
TSS_RESULT
 
85
//obj_context_is_connected(TSS_HCONTEXT tspContext, TCS_CONTEXT_HANDLE *tcsContext)
 
86
obj_context_is_connected(TSS_HCONTEXT tspContext)
 
87
{
 
88
        struct tsp_object *obj;
 
89
        TSS_RESULT result = TSS_SUCCESS;
 
90
 
 
91
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
92
                return TSPERR(TSS_E_INVALID_HANDLE);
 
93
 
 
94
        if (obj->tcsContext == NULL_HCONTEXT)
 
95
                result = TSPERR(TSS_E_NO_CONNECTION);
 
96
 
 
97
        //*tcsContext = obj->tcsContext;
 
98
 
 
99
        obj_list_put(&context_list);
 
100
 
 
101
        return result;
 
102
}
 
103
#endif
 
104
 
 
105
TSS_RESULT
 
106
obj_context_get_policy(TSS_HCONTEXT tspContext, TSS_HPOLICY *phPolicy)
 
107
{
 
108
        struct tsp_object *obj;
 
109
        struct tr_context_obj *context;
 
110
 
 
111
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
112
                return TSPERR(TSS_E_INVALID_HANDLE);
 
113
 
 
114
        context = (struct tr_context_obj *)obj->data;
 
115
        *phPolicy = context->policy;
 
116
 
 
117
        obj_list_put(&context_list);
 
118
 
 
119
        return TSS_SUCCESS;
 
120
}
 
121
 
 
122
TSS_RESULT
 
123
obj_context_get_machine_name(TSS_HCONTEXT tspContext, UINT32 *size, BYTE **data)
 
124
{
 
125
        struct tsp_object *obj;
 
126
        struct tr_context_obj *context;
 
127
        TSS_RESULT result = TSPERR(TSS_E_INVALID_HANDLE);
 
128
 
 
129
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
130
                return TSPERR(TSS_E_INVALID_HANDLE);
 
131
 
 
132
        context = (struct tr_context_obj *)obj->data;
 
133
 
 
134
        if (context->machineNameLength == 0) {
 
135
                *data = NULL;
 
136
                *size = 0;
 
137
        } else {
 
138
                *data = calloc_tspi(obj->tspContext, context->machineNameLength);
 
139
                if (*data == NULL) {
 
140
                        LogError("malloc of %u bytes failed.",
 
141
                                        context->machineNameLength);
 
142
                        result = TSPERR(TSS_E_OUTOFMEMORY);
 
143
                        goto done;
 
144
                }
 
145
                *size = context->machineNameLength;
 
146
                memcpy(*data, context->machineName, *size);
 
147
        }
 
148
 
 
149
        result = TSS_SUCCESS;
 
150
 
 
151
done:
 
152
        obj_list_put(&context_list);
 
153
 
 
154
        return result;
 
155
}
 
156
 
 
157
/* This function converts the machine name to a TSS_UNICODE string before
 
158
 * returning it, as Tspi_GetAttribData would like. We could do the conversion
 
159
 * in Tspi_GetAttribData, but we don't have access to the TSP context there */
 
160
TSS_RESULT
 
161
obj_context_get_machine_name_attrib(TSS_HCONTEXT tspContext, UINT32 *size,
 
162
                                    BYTE **data)
 
163
{
 
164
        struct tsp_object *obj;
 
165
        struct tr_context_obj *context;
 
166
        BYTE *utf_string;
 
167
        UINT32 utf_size;
 
168
        TSS_RESULT result = TSPERR(TSS_E_INVALID_HANDLE);
 
169
 
 
170
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
171
                return TSPERR(TSS_E_INVALID_HANDLE);
 
172
 
 
173
        context = (struct tr_context_obj *)obj->data;
 
174
 
 
175
        if (context->machineNameLength == 0) {
 
176
                *data = NULL;
 
177
                *size = 0;
 
178
        } else {
 
179
                utf_size = context->machineNameLength;
 
180
                utf_string = Trspi_Native_To_UNICODE(context->machineName,
 
181
                                                     &utf_size);
 
182
                if (utf_string == NULL) {
 
183
                        result = TSPERR(TSS_E_INTERNAL_ERROR);
 
184
                        goto done;
 
185
                }
 
186
 
 
187
                *data = calloc_tspi(obj->tspContext, utf_size);
 
188
                if (*data == NULL) {
 
189
                        free(utf_string);
 
190
                        LogError("malloc of %u bytes failed.", utf_size);
 
191
                        result = TSPERR(TSS_E_OUTOFMEMORY);
 
192
                        goto done;
 
193
                }
 
194
                *size = utf_size;
 
195
                memcpy(*data, utf_string, utf_size);
 
196
                free(utf_string);
 
197
        }
 
198
 
 
199
        result = TSS_SUCCESS;
 
200
 
 
201
done:
 
202
        obj_list_put(&context_list);
 
203
 
 
204
        return result;
 
205
}
 
206
 
 
207
TSS_RESULT
 
208
obj_context_set_machine_name(TSS_HCONTEXT tspContext, BYTE *name, UINT32 len)
 
209
{
 
210
        struct tsp_object *obj;
 
211
        struct tr_context_obj *context;
 
212
 
 
213
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
214
                return TSPERR(TSS_E_INVALID_HANDLE);
 
215
 
 
216
        context = (struct tr_context_obj *)obj->data;
 
217
 
 
218
        free(context->machineName);
 
219
        context->machineName = name;
 
220
        context->machineNameLength = len;
 
221
 
 
222
        obj_list_put(&context_list);
 
223
 
 
224
        return TSS_SUCCESS;
 
225
}
 
226
 
 
227
TSS_BOOL
 
228
obj_context_is_silent(TSS_HCONTEXT tspContext)
 
229
{
 
230
        struct tsp_object *obj;
 
231
        struct tr_context_obj *context;
 
232
        TSS_BOOL silent = FALSE;
 
233
 
 
234
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
235
                return FALSE;
 
236
 
 
237
        context = (struct tr_context_obj *)obj->data;
 
238
        if (context->silentMode == TSS_TSPATTRIB_CONTEXT_SILENT)
 
239
                silent = TRUE;
 
240
 
 
241
        obj_list_put(&context_list);
 
242
 
 
243
        return silent;
 
244
}
 
245
 
 
246
TSS_RESULT
 
247
obj_context_set_policy(TSS_HCONTEXT tspContext, TSS_HPOLICY hPolicy)
 
248
{
 
249
        struct tsp_object *obj;
 
250
        struct tr_context_obj *context;
 
251
 
 
252
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
253
                return TSPERR(TSS_E_INVALID_HANDLE);
 
254
 
 
255
        context = (struct tr_context_obj *)obj->data;
 
256
        context->policy = hPolicy;
 
257
 
 
258
        obj_list_put(&context_list);
 
259
 
 
260
        return TSS_SUCCESS;
 
261
}
 
262
 
 
263
TSS_RESULT
 
264
obj_context_get_mode(TSS_HCONTEXT tspContext, UINT32 *mode)
 
265
{
 
266
        struct tsp_object *obj;
 
267
        struct tr_context_obj *context;
 
268
 
 
269
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
270
                return TSPERR(TSS_E_INVALID_HANDLE);
 
271
 
 
272
        context = (struct tr_context_obj *)obj->data;
 
273
        *mode = context->silentMode;
 
274
 
 
275
        obj_list_put(&context_list);
 
276
 
 
277
        return TSS_SUCCESS;
 
278
}
 
279
 
 
280
TSS_RESULT
 
281
obj_context_set_mode(TSS_HCONTEXT tspContext, UINT32 mode)
 
282
{
 
283
        struct tsp_object *obj;
 
284
        struct tr_context_obj *context;
 
285
 
 
286
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
287
                return TSPERR(TSS_E_INVALID_HANDLE);
 
288
 
 
289
        context = (struct tr_context_obj *)obj->data;
 
290
        context->silentMode = mode;
 
291
 
 
292
        obj_list_put(&context_list);
 
293
 
 
294
        return TSS_SUCCESS;
 
295
}
 
296
 
 
297
/* search the list of all policies bound to context @tspContext. If
 
298
 * one is found of type popup, return TRUE, else return FALSE. */
 
299
TSS_BOOL
 
300
obj_context_has_popups(TSS_HCONTEXT tspContext)
 
301
{
 
302
        struct tsp_object *obj;
 
303
        struct tr_policy_obj *policy;
 
304
        struct obj_list *list = &policy_list;
 
305
        TSS_BOOL ret = FALSE;
 
306
 
 
307
        pthread_mutex_lock(&list->lock);
 
308
 
 
309
        for (obj = list->head; obj; obj = obj->next) {
 
310
                if (obj->tspContext == tspContext) {
 
311
                        policy = (struct tr_policy_obj *)obj->data;
 
312
                        if (policy->SecretMode == TSS_SECRET_MODE_POPUP)
 
313
                                ret = TRUE;
 
314
                        break;
 
315
                }
 
316
        }
 
317
 
 
318
        pthread_mutex_unlock(&list->lock);
 
319
 
 
320
        return ret;
 
321
}
 
322
 
 
323
#ifndef TSS_SPEC_COMPLIANCE
 
324
TSS_RESULT
 
325
obj_context_get_hash_mode(TSS_HCONTEXT tspContext, UINT32 *mode)
 
326
{
 
327
        struct tsp_object *obj;
 
328
        struct tr_context_obj *context;
 
329
 
 
330
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
331
                return TSPERR(TSS_E_INVALID_HANDLE);
 
332
 
 
333
        context = (struct tr_context_obj *)obj->data;
 
334
        *mode = context->hashMode;
 
335
 
 
336
        obj_list_put(&context_list);
 
337
 
 
338
        return TSS_SUCCESS;
 
339
}
 
340
 
 
341
TSS_RESULT
 
342
obj_context_set_hash_mode(TSS_HCONTEXT tspContext, UINT32 mode)
 
343
{
 
344
        struct tsp_object *obj;
 
345
        struct tr_context_obj *context;
 
346
 
 
347
        switch (mode) {
 
348
                case TSS_TSPATTRIB_HASH_MODE_NULL:
 
349
                case TSS_TSPATTRIB_HASH_MODE_NOT_NULL:
 
350
                        break;
 
351
                default:
 
352
                        return TSPERR(TSS_E_INVALID_ATTRIB_DATA);
 
353
        }
 
354
 
 
355
        if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
 
356
                return TSPERR(TSS_E_INVALID_HANDLE);
 
357
 
 
358
        context = (struct tr_context_obj *)obj->data;
 
359
        context->hashMode = mode;
 
360
 
 
361
        obj_list_put(&context_list);
 
362
 
 
363
        return TSS_SUCCESS;
 
364
}
 
365
#endif