~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/vtpm_manager/migration/vtpm_migratorc.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ===================================================================
 
2
//
 
3
// Copyright (c) 2005, Intel Corp.
 
4
// All rights reserved.
 
5
//
 
6
// Redistribution and use in source and binary forms, with or without
 
7
// modification, are permitted provided that the following conditions
 
8
// are met:
 
9
//
 
10
//   * Redistributions of source code must retain the above copyright
 
11
//     notice, this list of conditions and the following disclaimer.
 
12
//   * Redistributions in binary form must reproduce the above
 
13
//     copyright notice, this list of conditions and the following
 
14
//     disclaimer in the documentation and/or other materials provided
 
15
//     with the distribution.
 
16
//   * Neither the name of Intel Corporation nor the names of its
 
17
//     contributors may be used to endorse or promote products derived
 
18
//     from this software without specific prior written permission.
 
19
//
 
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
23
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
24
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
25
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
26
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
27
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
28
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
29
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
30
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 
31
// OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
// ===================================================================
 
33
 
 
34
#include <stdio.h>
 
35
#include <string.h>
 
36
 
 
37
#include "tcg.h"
 
38
#include "log.h"
 
39
#include "bsg.h"
 
40
#include "buffer.h"
 
41
#include "vtpm_migrator.h"
 
42
#include "vtpm_manager.h"
 
43
 
 
44
TPM_RESULT handle_vtpm_mig_step2(char *server_addr, 
 
45
                                 char *name, 
 
46
                                 UINT32 instance) {
 
47
  TPM_RESULT status, cmd_status;
 
48
  buffer_t out_param_buf=NULL_BUF, mig_key_buf=NULL_BUF, empty_buf=NULL_BUF;
 
49
  UINT32 offset; 
 
50
  struct pack_buf_t addr_data32;
 
51
 
 
52
  //===== Get Destination's Public Migration Key ======
 
53
  TPMTRYRETURN( vtpm_migratord_open(server_addr) );
 
54
 
 
55
  TPMTRYRETURN( vtpm_migratord_command(VTPM_MORD_MIG_STEP2,
 
56
                                     &out_param_buf,
 
57
                                     &cmd_status, 
 
58
                                     &mig_key_buf) ); 
 
59
  vtpm_migratord_close();
 
60
 
 
61
  TPMTRYRETURN(cmd_status);
 
62
 
 
63
  //===== Load migration key into vtpm_manager ========
 
64
 
 
65
  addr_data32.data = (BYTE *)server_addr;
 
66
  addr_data32.size = strlen(server_addr) + 1; // Include the null
 
67
 
 
68
  TPMTRYRETURN ( buffer_init ( &out_param_buf, 
 
69
                               sizeof(UINT32) + addr_data32.size +buffer_len(&mig_key_buf),
 
70
                               NULL ) ) ;
 
71
 
 
72
  offset =  BSG_PackList(out_param_buf.bytes, 1,
 
73
               BSG_TPM_SIZE32_DATA, &addr_data32);
 
74
 
 
75
  memcpy(out_param_buf.bytes + offset , mig_key_buf.bytes, buffer_len(&mig_key_buf) );
 
76
 
 
77
  TPMTRYRETURN ( vtpm_manager_open() );
 
78
 
 
79
  TPMTRYRETURN ( vtpm_manager_command(VTPM_ORD_LOAD_MIG_KEY,
 
80
                                      &out_param_buf,
 
81
                                      &cmd_status,
 
82
                                      &empty_buf) );
 
83
 
 
84
  vtpm_manager_close();
 
85
 
 
86
  TPMTRYRETURN(cmd_status);
 
87
 
 
88
  goto egress;
 
89
 
 
90
 abort_egress:
 
91
 egress:
 
92
 
 
93
  buffer_free(&mig_key_buf);
 
94
  buffer_free(&out_param_buf);
 
95
 
 
96
  return status;
 
97
}
 
98
 
 
99
 
 
100
TPM_RESULT handle_vtpm_mig_step3(char *server_addr, 
 
101
                                 char *name, 
 
102
                                 UINT32 instance) {
 
103
  TPM_RESULT status, cmd_status;
 
104
  buffer_t out_param_buf=NULL_BUF, state_buf=NULL_BUF, empty_buf=NULL_BUF;
 
105
  struct pack_buf_t addr_data32, name_data32, state_data32;
 
106
 
 
107
  //===== Get vtpm state from vtpm_manager ========
 
108
  addr_data32.data = (BYTE *)server_addr;
 
109
  addr_data32.size = strlen(server_addr) + 1; // Include the null
 
110
 
 
111
  TPMTRYRETURN ( buffer_init ( &out_param_buf,
 
112
                               (2 * sizeof(UINT32)) + addr_data32.size,
 
113
                               NULL ) ) ;
 
114
 
 
115
  BSG_PackList(out_param_buf.bytes, 2,
 
116
                 BSG_TYPE_UINT32, &instance, 
 
117
                 BSG_TPM_SIZE32_DATA, &addr_data32);
 
118
 
 
119
  TPMTRYRETURN ( vtpm_manager_open() );
 
120
 
 
121
  TPMTRYRETURN ( vtpm_manager_command(VTPM_ORD_MIGRATE_OUT,
 
122
                                      &out_param_buf,
 
123
                                      &cmd_status,
 
124
                                      &state_buf) );
 
125
 
 
126
  vtpm_manager_close();
 
127
 
 
128
  TPMTRYRETURN(cmd_status);
 
129
 
 
130
  TPMTRYRETURN( buffer_free( &out_param_buf ) );
 
131
 
 
132
  //===== Send vtpm state to destination ======
 
133
  name_data32.data = (BYTE *)name;
 
134
  name_data32.size = strlen(name) + 1; // Include the null
 
135
  state_data32.data = state_buf.bytes;
 
136
  state_data32.size = buffer_len(&state_buf);
 
137
 
 
138
  TPMTRYRETURN( buffer_init( &out_param_buf,
 
139
                             2 * sizeof(UINT32) + name_data32.size + state_data32.size,
 
140
                             NULL ) ) ;
 
141
                             
 
142
  BSG_PackList(out_param_buf.bytes, 2,
 
143
                 BSG_TPM_SIZE32_DATA, &name_data32,
 
144
                 BSG_TPM_SIZE32_DATA, &state_data32);
 
145
 
 
146
  TPMTRYRETURN( vtpm_migratord_open(server_addr) );
 
147
 
 
148
  TPMTRYRETURN( vtpm_migratord_command(VTPM_MORD_MIG_STEP3,
 
149
                                     &out_param_buf,
 
150
                                     &cmd_status, 
 
151
                                     &empty_buf) ); 
 
152
  vtpm_migratord_close();
 
153
 
 
154
  TPMTRYRETURN(cmd_status);
 
155
 
 
156
  goto egress;
 
157
 
 
158
 abort_egress:
 
159
 egress:
 
160
 
 
161
  buffer_free( &out_param_buf);
 
162
  buffer_free( &state_buf);
 
163
  buffer_free( &empty_buf);
 
164
 
 
165
  return status;
 
166
}
 
167
 
 
168
 
 
169
// Usage vtpm_migrator addr domain_name instance step
 
170
 
 
171
int main(int argc, char **argv) {
 
172
 
 
173
    /* variables for processing of command */
 
174
    TPM_RESULT status = TPM_FAIL;
 
175
    char *server_addr, *name;
 
176
    UINT32 instance, step;
 
177
 
 
178
    if (argc != 5) {
 
179
      vtpmlogerror(VTPM_LOG_VTPM, "Usage: vtpm_migrator addr vm_name instance step\n");
 
180
      vtpmlogerror(VTPM_LOG_VTPM, "       params given %d\n", argc);
 
181
      status= TPM_BAD_PARAMETER;
 
182
      goto abort_egress;
 
183
    }
 
184
 
 
185
    server_addr = argv[1];
 
186
    name = argv[2];
 
187
    instance = atoi( argv[3] );
 
188
    step = atoi( argv[4] );    
 
189
 
 
190
    switch (step) {
 
191
    case VTPM_MORD_MIG_STEP2:
 
192
      status = handle_vtpm_mig_step2(server_addr, name, instance);
 
193
      break;
 
194
 
 
195
    case VTPM_MORD_MIG_STEP3:
 
196
      status = handle_vtpm_mig_step3(server_addr, name, instance);
 
197
      break;
 
198
 
 
199
    default:
 
200
      status = TPM_BAD_PARAMETER;
 
201
      goto abort_egress;
 
202
      break;
 
203
    }
 
204
 
 
205
    goto egress;
 
206
 abort_egress:
 
207
 egress:
 
208
 
 
209
    return status;
 
210
}
 
211