~ubuntu-branches/ubuntu/hardy/trousers/hardy-proposed

« back to all changes in this revision

Viewing changes to src/tcs/tcs_aik.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-01-23 22:03:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123220300-fhtqja3c0oq0gp6z
Tags: 0.3.1-4
* Added patch from Aaron M. Ucko <ucko@debian.org> to allow trousers to
  build successfully on amd64, and presumably also other 64-bit
  architectures (Closes: #457400).
* Including udev rule for /dev/tpm from William Lima
  <wlima.amadeus@gmail.com> as suggested by David Smith <dds@google.com>
  (Closes: #459682).
* Added lintian overrides.

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. 2004-2006
 
8
 *
 
9
 */
 
10
 
 
11
 
 
12
#include <stdlib.h>
 
13
#include <stdio.h>
 
14
#include <string.h>
 
15
#include <unistd.h>
 
16
#include <sys/types.h>
 
17
#include <sys/stat.h>
 
18
#include <sys/mman.h>
 
19
#include <fcntl.h>
 
20
#include <errno.h>
 
21
 
 
22
#include "trousers/tss.h"
 
23
#include "trousers_types.h"
 
24
#include "trousers_types.h"
 
25
#include "tcs_tsp.h"
 
26
#include "tcs_utils.h"
 
27
#include "tcs_int_literals.h"
 
28
#include "tcsps.h"
 
29
#include "tcslog.h"
 
30
#include "tcsd_wrap.h"
 
31
#include "tcsd.h"
 
32
#include "tcs_aik.h"
 
33
 
 
34
void
 
35
LoadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
 
36
{
 
37
        LoadBlob_UINT32(offset, key->algId, blob);
 
38
        LoadBlob_UINT16(offset, key->encScheme, blob);
 
39
        LoadBlob_UINT16(offset, key->size, blob);
 
40
 
 
41
        if (key->size > 0) {
 
42
                LoadBlob(offset, key->size, blob, key->data);
 
43
        } else {
 
44
                key->data = NULL;
 
45
        }
 
46
}
 
47
 
 
48
TSS_RESULT
 
49
UnloadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
 
50
{
 
51
        if (!key) {
 
52
                UINT16 size;
 
53
 
 
54
                UnloadBlob_UINT32(offset, NULL, blob);
 
55
                UnloadBlob_UINT16(offset, NULL, blob);
 
56
                UnloadBlob_UINT16(offset, &size, blob);
 
57
 
 
58
                if (size > 0)
 
59
                        UnloadBlob(offset, size, blob, NULL);
 
60
 
 
61
                return TSS_SUCCESS;
 
62
        }
 
63
 
 
64
        UnloadBlob_UINT32(offset, &key->algId, blob);
 
65
        UnloadBlob_UINT16(offset, &key->encScheme, blob);
 
66
        UnloadBlob_UINT16(offset, &key->size, blob);
 
67
 
 
68
        if (key->size > 0) {
 
69
                key->data = (BYTE *)malloc(key->size);
 
70
                if (key->data == NULL) {
 
71
                        LogError("malloc of %hu bytes failed.", key->size);
 
72
                        key->size = 0;
 
73
                        return TCSERR(TSS_E_OUTOFMEMORY);
 
74
                }
 
75
                UnloadBlob(offset, key->size, blob, key->data);
 
76
        } else {
 
77
                key->data = NULL;
 
78
        }
 
79
 
 
80
        return TSS_SUCCESS;
 
81
}
 
82
 
 
83
void
 
84
get_credential(UINT32 type, UINT32 *size, BYTE **cred)
 
85
{
 
86
        int rc, fd;
 
87
        char *path = NULL;
 
88
        void *file = NULL;
 
89
        struct stat stat_buf;
 
90
        size_t file_size;
 
91
 
 
92
        switch (type) {
 
93
                case TSS_TCS_CREDENTIAL_PLATFORMCERT:
 
94
                        path = tcsd_options.platform_cred;
 
95
                        break;
 
96
                case TSS_TCS_CREDENTIAL_TPM_CC:
 
97
                        path = tcsd_options.conformance_cred;
 
98
                        break;
 
99
                case TSS_TCS_CREDENTIAL_EKCERT:
 
100
                        path = tcsd_options.endorsement_cred;
 
101
                        break;
 
102
                default:
 
103
                        LogDebugFn("Bad credential type");
 
104
                        break;
 
105
        }
 
106
 
 
107
        if (path == NULL)
 
108
                goto done;
 
109
 
 
110
        if ((fd = open(path, O_RDONLY)) < 0) {
 
111
                LogError("open(%s): %s", path, strerror(errno));
 
112
                goto done;
 
113
        }
 
114
 
 
115
        if ((rc = fstat(fd, &stat_buf)) == -1) {
 
116
                LogError("Error stating credential: %s: %s", path, strerror(errno));
 
117
                goto done;
 
118
        }
 
119
 
 
120
        file_size = (size_t)stat_buf.st_size;
 
121
 
 
122
        LogDebugFn("%s, (%zd bytes)", path, file_size);
 
123
 
 
124
        file = mmap(0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
 
125
        if (file == MAP_FAILED) {
 
126
                LogError("Error reading credential: %s: %s", path, strerror(errno));
 
127
                close(fd);
 
128
                goto done;
 
129
        }
 
130
        close(fd);
 
131
 
 
132
        if ((*cred = malloc(file_size)) == NULL) {
 
133
                LogError("malloc of %zd bytes failed.", file_size);
 
134
                munmap(file, file_size);
 
135
                goto done;
 
136
        }
 
137
 
 
138
        memcpy(*cred, file, file_size);
 
139
        *size = file_size;
 
140
        munmap(file, file_size);
 
141
 
 
142
        return;
 
143
done:
 
144
        *cred = NULL;
 
145
        *size = 0;
 
146
}