Return to tcg_pts_attr_req_file_meas.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libimcv / tcg / pts |
1.1 misho 1: /* 2: * Copyright (C) 2011-2012 Sansar Choinyambuu 3: * Copyright (C) 2011-2014 Andreas Steffen 4: * HSR Hochschule fuer Technik Rapperswil 5: * 6: * This program is free software; you can redistribute it and/or modify it 7: * under the terms of the GNU General Public License as published by the 8: * Free Software Foundation; either version 2 of the License, or (at your 9: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. 10: * 11: * This program is distributed in the hope that it will be useful, but 12: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14: * for more details. 15: */ 16: 17: #define _GNU_SOURCE /* for stdndup() */ 18: #include <string.h> 19: 20: #include "tcg_pts_attr_req_file_meas.h" 21: 22: #include <pa_tnc/pa_tnc_msg.h> 23: #include <bio/bio_writer.h> 24: #include <bio/bio_reader.h> 25: #include <utils/debug.h> 26: 27: typedef struct private_tcg_pts_attr_req_file_meas_t private_tcg_pts_attr_req_file_meas_t; 28: 29: /** 30: * Request File Measurement 31: * see section 3.19.1 of PTS Protocol: Binding to TNC IF-M Specification 32: * 33: * 1 2 3 34: * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 35: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36: * | Flags | Reserved | Request ID | 37: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 38: * | Delimiter | 39: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 40: * ~ Fully Qualified File Pathname (Variable Length) ~ 41: * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 42: */ 43: 44: #define PTS_REQ_FILE_MEAS_SIZE 8 45: #define PTS_REQ_FILE_MEAS_RESERVED 0x00 46: #define PTS_REQ_FILE_MEAS_NO_FLAGS 0x00 47: 48: #define DIRECTORY_CONTENTS_FLAG (1<<7) 49: 50: /** 51: * Private data of an tcg_pts_attr_req_file_meas_t object. 52: */ 53: struct private_tcg_pts_attr_req_file_meas_t { 54: 55: /** 56: * Public members of tcg_pts_attr_req_file_meas_t 57: */ 58: tcg_pts_attr_req_file_meas_t public; 59: 60: /** 61: * Vendor-specific attribute type 62: */ 63: pen_type_t type; 64: 65: /** 66: * Length of attribute value 67: */ 68: size_t length; 69: 70: /** 71: * Attribute value or segment 72: */ 73: chunk_t value; 74: 75: /** 76: * Noskip flag 77: */ 78: bool noskip_flag; 79: 80: /** 81: * Directory Contents flag 82: */ 83: bool directory_flag; 84: 85: /** 86: * Request ID 87: */ 88: uint16_t request_id; 89: 90: /** 91: * UTF8 Encoding of Delimiter Character 92: */ 93: uint32_t delimiter; 94: 95: /** 96: * Fully Qualified File Pathname 97: */ 98: char *pathname; 99: 100: /** 101: * Reference count 102: */ 103: refcount_t ref; 104: }; 105: 106: METHOD(pa_tnc_attr_t, get_type, pen_type_t, 107: private_tcg_pts_attr_req_file_meas_t *this) 108: { 109: return this->type; 110: } 111: 112: METHOD(pa_tnc_attr_t, get_value, chunk_t, 113: private_tcg_pts_attr_req_file_meas_t *this) 114: { 115: return this->value; 116: } 117: 118: METHOD(pa_tnc_attr_t, get_noskip_flag, bool, 119: private_tcg_pts_attr_req_file_meas_t *this) 120: { 121: return this->noskip_flag; 122: } 123: 124: METHOD(pa_tnc_attr_t, set_noskip_flag,void, 125: private_tcg_pts_attr_req_file_meas_t *this, bool noskip) 126: { 127: this->noskip_flag = noskip; 128: } 129: 130: METHOD(pa_tnc_attr_t, build, void, 131: private_tcg_pts_attr_req_file_meas_t *this) 132: { 133: uint8_t flags = PTS_REQ_FILE_MEAS_NO_FLAGS; 134: chunk_t pathname; 135: bio_writer_t *writer; 136: 137: if (this->value.ptr) 138: { 139: return; 140: } 141: if (this->directory_flag) 142: { 143: flags |= DIRECTORY_CONTENTS_FLAG; 144: } 145: pathname = chunk_create(this->pathname, strlen(this->pathname)); 146: 147: writer = bio_writer_create(PTS_REQ_FILE_MEAS_SIZE); 148: writer->write_uint8 (writer, flags); 149: writer->write_uint8 (writer, PTS_REQ_FILE_MEAS_RESERVED); 150: writer->write_uint16(writer, this->request_id); 151: writer->write_uint32(writer, this->delimiter); 152: writer->write_data (writer, pathname); 153: this->value = writer->extract_buf(writer); 154: this->length = this->value.len; 155: writer->destroy(writer); 156: } 157: 158: METHOD(pa_tnc_attr_t, process, status_t, 159: private_tcg_pts_attr_req_file_meas_t *this, uint32_t *offset) 160: { 161: bio_reader_t *reader; 162: uint8_t flags; 163: uint8_t reserved; 164: chunk_t pathname; 165: 166: *offset = 0; 167: 168: if (this->value.len < this->length) 169: { 170: return NEED_MORE; 171: } 172: if (this->value.len < PTS_REQ_FILE_MEAS_SIZE) 173: { 174: DBG1(DBG_TNC, "insufficient data for Request File Measurement"); 175: return FAILED; 176: } 177: 178: reader = bio_reader_create(this->value); 179: reader->read_uint8 (reader, &flags); 180: reader->read_uint8 (reader, &reserved); 181: reader->read_uint16(reader, &this->request_id); 182: reader->read_uint32(reader, &this->delimiter); 183: reader->read_data (reader, reader->remaining(reader), &pathname); 184: 185: this->directory_flag = (flags & DIRECTORY_CONTENTS_FLAG) != 186: PTS_REQ_FILE_MEAS_NO_FLAGS; 187: this->pathname = strndup(pathname.ptr, pathname.len); 188: 189: reader->destroy(reader); 190: return SUCCESS; 191: } 192: 193: METHOD(pa_tnc_attr_t, add_segment, void, 194: private_tcg_pts_attr_req_file_meas_t *this, chunk_t segment) 195: { 196: this->value = chunk_cat("mc", this->value, segment); 197: } 198: 199: METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*, 200: private_tcg_pts_attr_req_file_meas_t *this) 201: { 202: ref_get(&this->ref); 203: return &this->public.pa_tnc_attribute; 204: } 205: 206: METHOD(pa_tnc_attr_t, destroy, void, 207: private_tcg_pts_attr_req_file_meas_t *this) 208: { 209: if (ref_put(&this->ref)) 210: { 211: free(this->pathname); 212: free(this->value.ptr); 213: free(this); 214: } 215: } 216: 217: METHOD(tcg_pts_attr_req_file_meas_t, get_directory_flag, bool, 218: private_tcg_pts_attr_req_file_meas_t *this) 219: { 220: return this->directory_flag; 221: } 222: 223: METHOD(tcg_pts_attr_req_file_meas_t, get_request_id, uint16_t, 224: private_tcg_pts_attr_req_file_meas_t *this) 225: { 226: return this->request_id; 227: } 228: 229: METHOD(tcg_pts_attr_req_file_meas_t, get_delimiter, uint32_t, 230: private_tcg_pts_attr_req_file_meas_t *this) 231: { 232: return this->delimiter; 233: } 234: 235: METHOD(tcg_pts_attr_req_file_meas_t, get_pathname, char*, 236: private_tcg_pts_attr_req_file_meas_t *this) 237: { 238: return this->pathname; 239: } 240: 241: /** 242: * Described in header. 243: */ 244: pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create(bool directory_flag, 245: uint16_t request_id, 246: uint32_t delimiter, 247: char *pathname) 248: { 249: private_tcg_pts_attr_req_file_meas_t *this; 250: 251: INIT(this, 252: .public = { 253: .pa_tnc_attribute = { 254: .get_type = _get_type, 255: .get_value = _get_value, 256: .get_noskip_flag = _get_noskip_flag, 257: .set_noskip_flag = _set_noskip_flag, 258: .build = _build, 259: .process = _process, 260: .add_segment = _add_segment, 261: .get_ref = _get_ref, 262: .destroy = _destroy, 263: }, 264: .get_directory_flag = _get_directory_flag, 265: .get_request_id = _get_request_id, 266: .get_delimiter = _get_delimiter, 267: .get_pathname = _get_pathname, 268: }, 269: .type = { PEN_TCG, TCG_PTS_REQ_FILE_MEAS }, 270: .directory_flag = directory_flag, 271: .request_id = request_id, 272: .delimiter = delimiter, 273: .pathname = strdup(pathname), 274: .ref = 1, 275: ); 276: 277: return &this->public.pa_tnc_attribute; 278: } 279: 280: 281: /** 282: * Described in header. 283: */ 284: pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create_from_data(size_t length, 285: chunk_t data) 286: { 287: private_tcg_pts_attr_req_file_meas_t *this; 288: 289: INIT(this, 290: .public = { 291: .pa_tnc_attribute = { 292: .get_type = _get_type, 293: .get_value = _get_value, 294: .get_noskip_flag = _get_noskip_flag, 295: .set_noskip_flag = _set_noskip_flag, 296: .build = _build, 297: .process = _process, 298: .add_segment = _add_segment, 299: .get_ref = _get_ref, 300: .destroy = _destroy, 301: }, 302: .get_directory_flag = _get_directory_flag, 303: .get_request_id = _get_request_id, 304: .get_delimiter = _get_delimiter, 305: .get_pathname = _get_pathname, 306: }, 307: .type = { PEN_TCG, TCG_PTS_REQ_FILE_MEAS }, 308: .length = length, 309: .value = chunk_clone(data), 310: .ref = 1, 311: ); 312: 313: return &this->public.pa_tnc_attribute; 314: }