1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*
* commands.c: handlers for commands
*
* Copyright (c) 2006-2008, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <zlib.h>
#include <openssl/evp.h>
#include <safe_lib.h>
#define PRINT printf
#include "../include/config.h"
#include "../include/hash.h"
#include "../include/uuid.h"
#include "../include/lcp2.h"
#include "../include/tb_error.h"
#include "../include/tb_policy.h"
#include "tb_polgen.h"
extern tb_policy_t *g_policy;
static bool hash_file(const char *filename, bool unzip, tb_hash_t *hash, uint16_t hash_alg)
{
FILE *f;
static char buf[1024];
int read_cnt;
if ( unzip )
f = (FILE *)gzopen(filename, "rb");
else
f = fopen(filename, "rb");
if ( f == NULL ) {
error_msg("File %s does not exist\n", filename);
return false;
}
const EVP_MD *md;
uint8_t* hash_out;
EVP_MD_CTX *ctx = EVP_MD_CTX_create();
switch (hash_alg) {
case TB_HALG_SHA1:
md = EVP_sha1();
hash_out = hash->sha1;
break;
case TB_HALG_SHA256:
md = EVP_sha256();
hash_out = hash->sha256;
break;
case TB_HALG_SHA384:
md = EVP_sha384();
hash_out = hash->sha384;
break;
case TB_HALG_SHA512:
md = EVP_sha512();
hash_out = hash->sha512;
break;
default:
error_msg("unsupported hash alg (%d)\n", hash_alg);
EVP_MD_CTX_destroy(ctx);
if ( unzip ) {
gzclose((gzFile)f);
} else {
fclose(f);
}
return false;
}
EVP_DigestInit(ctx, md);
do {
if ( unzip )
read_cnt = gzread((gzFile)f, buf, sizeof(buf));
else
read_cnt = fread(buf, 1, sizeof(buf), f);
if ( read_cnt == 0 )
break;
EVP_DigestUpdate(ctx, buf, read_cnt);
} while ( true );
EVP_DigestFinal(ctx, hash_out, NULL);
if ( unzip )
gzclose((gzFile)f);
else
fclose(f);
EVP_MD_CTX_destroy(ctx);
return true;
}
bool do_show(const param_data_t *params)
{
/* read the policy file */
if ( !read_policy_file(params->policy_file, NULL) ) {
error_msg("Error reading policy file %s\n", params->policy_file);
return false;
}
/* this also displays it */
verify_policy(g_policy, calc_policy_size(g_policy), true);
return true;
}
bool do_create(const param_data_t *params)
{
bool existing_policy = false;
/* read the policy file, if it exists */
info_msg("reading existing policy file %s...\n", params->policy_file);
if ( !read_policy_file(params->policy_file, &existing_policy) ) {
/* this means there was an error reading an existing file */
if ( existing_policy ) {
error_msg("Error reading policy file %s\n", params->policy_file);
return false;
}
}
/* policy_type must be specified for new policy */
if ( !existing_policy && params->policy_type == -1 ) {
error_msg("Must specify --policy_type for new policy\n");
return false;
}
/* if file does not exist then create empty policy */
if ( !existing_policy )
new_policy(params->policy_type, params->policy_control, params->hash_alg);
else {
error_msg("warning: modifying existing policy file, hash algorithm "
"will not be changed\n");
modify_policy(params->policy_type, params->policy_control);
}
info_msg("writing new policy file...\n");
if ( !write_policy_file(params->policy_file) )
return false;
return true;
}
bool do_add(const param_data_t *params)
{
/* read the policy file, if it exists */
info_msg("reading existing policy file %s...\n", params->policy_file);
if ( !read_policy_file(params->policy_file, NULL) ) {
error_msg("Error reading policy file %s\n", params->policy_file);
return false;
}
/* see if there is already an entry for this module */
tb_policy_entry_t *pol_entry = find_policy_entry(g_policy,
params->mod_num);
if ( pol_entry == NULL || pol_entry->mod_num != params->mod_num ) {
/* since existing entry whose mod_num is TB_POL_MOD_NUM_ANY will */
/* match, exclude it unless that is what is being added */
pol_entry = add_pol_entry(params->mod_num, params->pcr,
params->hash_type);
if ( pol_entry == NULL ) {
error_msg("cannot add another entry\n");
return false;
}
}
else
modify_pol_entry(pol_entry, params->pcr, params->hash_type);
/* hash command line and files */
if ( params->hash_type == TB_HTYPE_IMAGE ) {
tb_hash_t final_hash, hash;
/* hash command line */
info_msg("hashing command line \"%s\"...\n", params->cmdline);
hash_buffer((unsigned char *)params->cmdline,
strnlen_s(params->cmdline, sizeof(params->cmdline)),
&final_hash, g_policy->hash_alg);
if ( verbose ) {
info_msg("hash is...");
print_hash(&final_hash, g_policy->hash_alg);
}
/* hash file */
info_msg("hashing image file %s...\n", params->image_file);
if ( !hash_file(params->image_file, true, &hash, g_policy->hash_alg) ) {
return false;
}
if ( verbose ) {
info_msg("hash is...");
print_hash(&hash, g_policy->hash_alg);
}
if ( !extend_hash(&final_hash, &hash, g_policy->hash_alg) ){
return false;
}
if ( verbose ) {
info_msg("cumulative hash is...");
print_hash(&final_hash, g_policy->hash_alg);
}
if ( !add_hash(pol_entry, &final_hash) ) {
error_msg("cannot add another hash\n");
return false;
}
}
info_msg("writing new policy file...\n");
if ( !write_policy_file(params->policy_file) )
return false;
return true;
}
bool do_del(const param_data_t *params)
{
/* read the policy file, if it exists */
info_msg("reading existing policy file %s...\n", params->policy_file);
if ( !read_policy_file(params->policy_file, NULL) ) {
error_msg("Error reading policy file %s\n", params->policy_file);
return false;
}
/* see if there is an entry for this module */
tb_policy_entry_t *pol_entry = find_policy_entry(g_policy,
params->mod_num);
if ( pol_entry == NULL ) {
error_msg("specified mod_num does not exist\n");
return false;
}
/* if pos was specified, find it */
if ( params->pos != -1 ) {
if ( params->pos >= pol_entry->num_hashes ) {
error_msg("specified pos does not exist\n");
return false;
}
/* if entry only has 1 hash, then delete the entire entry */
if ( pol_entry->num_hashes == 1 ) {
if ( !del_entry(pol_entry) ) {
error_msg("failed to delete entry\n");
return false;
}
}
else {
if ( !del_hash(pol_entry, params->pos) ) {
error_msg("failed to delete hash\n");
return false;
}
}
}
else {
if ( !del_entry(pol_entry) ) {
error_msg("failed to delete entry\n");
return false;
}
}
info_msg("writing new policy file...\n");
if ( !write_policy_file(params->policy_file) )
return false;
return true;
}
bool do_unwrap(const param_data_t *params)
{
bool ret = false;
/* read the elt file */
info_msg("reading existing elt file %s...\n", params->elt_file);
size_t file_len;
void *file = read_elt_file(params->elt_file, &file_len);
if ( file == NULL ) {
error_msg("Error reading elt file %s\n", params->elt_file);
return false;
}
if ( sizeof(lcp_policy_element_t) > file_len ) {
error_msg("data is too small\n");
goto exit;
}
lcp_policy_element_t *elt = (lcp_policy_element_t *)file;
if ( file_len != elt->size ) {
error_msg("data is too small\n");
goto exit;
}
if ( elt->type != LCP_POLELT_TYPE_CUSTOM ) {
error_msg("Bad element type %u (i.e. non-custom)\n", elt->type);
goto exit;
}
lcp_custom_element_t *custom = (lcp_custom_element_t *)&elt->data;
tb_policy_t *pol = (tb_policy_t *)&custom->data;
memcpy_s(g_policy, MAX_TB_POLICY_SIZE, pol, calc_policy_size(pol));
info_msg("writing/overwriting policy file...\n");
if ( !write_policy_file(params->policy_file) )
goto exit;
ret = true;
exit:
free(file);
file = NULL;
return ret;
}
/*
* Local variables:
* mode: C
* c-set-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|