Annotation of embedaddon/strongswan/src/libimcv/tcg/pts/tcg_pts_attr_req_file_meta.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2011-2012 Sansar Choinyambuu
                      3:  * Copyright (C) 2011-2018 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_meta.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_meta_t private_tcg_pts_attr_req_file_meta_t;
                     28: 
                     29: /**
                     30:  * Request File Metadata
                     31:  * see section 3.17.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   |   Delimiter  |                 Reserved                                      |
                     37:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     38:  *  ~     Fully Qualified File Pathname (Variable Length)                      ~
                     39:  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                     40:  */
                     41: 
                     42: #define PTS_REQ_FILE_META_SIZE                 4
                     43: #define PTS_REQ_FILE_META_RESERVED             0x00
                     44: #define PTS_REQ_FILE_META_NO_FLAGS             0x00
                     45: 
                     46: #define DIRECTORY_CONTENTS_FLAG                        (1<<7)
                     47: 
                     48: /**
                     49:  * Private data of an tcg_pts_attr_req_file_meta_t object.
                     50:  */
                     51: struct private_tcg_pts_attr_req_file_meta_t {
                     52: 
                     53:        /**
                     54:         * Public members of tcg_pts_attr_req_file_meta_t
                     55:         */
                     56:        tcg_pts_attr_req_file_meta_t public;
                     57: 
                     58:        /**
                     59:         * Vendor-specific attribute type
                     60:         */
                     61:        pen_type_t type;
                     62: 
                     63:        /**
                     64:         * Length of attribute value
                     65:         */
                     66:        size_t length;
                     67: 
                     68:        /**
                     69:         * Attribute value or segment
                     70:         */
                     71:        chunk_t value;
                     72: 
                     73:        /**
                     74:         * Noskip flag
                     75:         */
                     76:        bool noskip_flag;
                     77: 
                     78:        /**
                     79:         * Directory Contents flag
                     80:         */
                     81:        bool directory_flag;
                     82: 
                     83:        /**
                     84:         * UTF8 Encoding of Delimiter Character
                     85:         */
                     86:        uint8_t delimiter;
                     87: 
                     88:        /**
                     89:         * Fully Qualified File Pathname
                     90:         */
                     91:        char *pathname;
                     92: 
                     93:        /**
                     94:         * Reference count
                     95:         */
                     96:        refcount_t ref;
                     97: };
                     98: 
                     99: METHOD(pa_tnc_attr_t, get_type, pen_type_t,
                    100:        private_tcg_pts_attr_req_file_meta_t *this)
                    101: {
                    102:        return this->type;
                    103: }
                    104: 
                    105: METHOD(pa_tnc_attr_t, get_value, chunk_t,
                    106:        private_tcg_pts_attr_req_file_meta_t *this)
                    107: {
                    108:        return this->value;
                    109: }
                    110: 
                    111: METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
                    112:        private_tcg_pts_attr_req_file_meta_t *this)
                    113: {
                    114:        return this->noskip_flag;
                    115: }
                    116: 
                    117: METHOD(pa_tnc_attr_t, set_noskip_flag,void,
                    118:        private_tcg_pts_attr_req_file_meta_t *this, bool noskip)
                    119: {
                    120:        this->noskip_flag = noskip;
                    121: }
                    122: 
                    123: METHOD(pa_tnc_attr_t, build, void,
                    124:        private_tcg_pts_attr_req_file_meta_t *this)
                    125: {
                    126:        uint8_t flags = PTS_REQ_FILE_META_NO_FLAGS;
                    127:        chunk_t pathname;
                    128:        bio_writer_t *writer;
                    129: 
                    130:        if (this->value.ptr)
                    131:        {
                    132:                return;
                    133:        }
                    134:        if (this->directory_flag)
                    135:        {
                    136:                flags |= DIRECTORY_CONTENTS_FLAG;
                    137:        }
                    138:        pathname = chunk_create(this->pathname, strlen(this->pathname));
                    139: 
                    140:        writer = bio_writer_create(PTS_REQ_FILE_META_SIZE);
                    141:        writer->write_uint8 (writer, flags);
                    142:        writer->write_uint8 (writer, this->delimiter);
                    143:        writer->write_uint16(writer, PTS_REQ_FILE_META_RESERVED);
                    144: 
                    145:        writer->write_data  (writer, pathname);
                    146:        this->value = writer->extract_buf(writer);
                    147:        this->length = this->value.len;
                    148:        writer->destroy(writer);
                    149: }
                    150: 
                    151: METHOD(pa_tnc_attr_t, process, status_t,
                    152:        private_tcg_pts_attr_req_file_meta_t *this, uint32_t *offset)
                    153: {
                    154:        bio_reader_t *reader;
                    155:        uint8_t flags;
                    156:        uint16_t reserved;
                    157:        chunk_t pathname;
                    158: 
                    159:        *offset = 0;
                    160: 
                    161:        if (this->value.len < this->length)
                    162:        {
                    163:                return NEED_MORE;
                    164:        }
                    165:        if (this->value.len < PTS_REQ_FILE_META_SIZE)
                    166:        {
                    167:                DBG1(DBG_TNC, "insufficient data for Request File Metadata");
                    168:                return FAILED;
                    169:        }
                    170: 
                    171:        reader = bio_reader_create(this->value);
                    172:        reader->read_uint8 (reader, &flags);
                    173:        reader->read_uint8 (reader, &this->delimiter);
                    174:        reader->read_uint16(reader, &reserved);
                    175: 
                    176:        reader->read_data  (reader, reader->remaining(reader), &pathname);
                    177: 
                    178:        this->directory_flag = (flags & DIRECTORY_CONTENTS_FLAG) !=
                    179:                                                        PTS_REQ_FILE_META_NO_FLAGS;
                    180:        this->pathname = strndup(pathname.ptr, pathname.len);
                    181: 
                    182:        reader->destroy(reader);
                    183:        return SUCCESS;
                    184: }
                    185: 
                    186: METHOD(pa_tnc_attr_t, add_segment, void,
                    187:        private_tcg_pts_attr_req_file_meta_t *this, chunk_t segment)
                    188: {
                    189:        this->value = chunk_cat("mc", this->value, segment);
                    190: }
                    191: 
                    192: METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
                    193:        private_tcg_pts_attr_req_file_meta_t *this)
                    194: {
                    195:        ref_get(&this->ref);
                    196:        return &this->public.pa_tnc_attribute;
                    197: }
                    198: 
                    199: METHOD(pa_tnc_attr_t, destroy, void,
                    200:        private_tcg_pts_attr_req_file_meta_t *this)
                    201: {
                    202:        if (ref_put(&this->ref))
                    203:        {
                    204:                free(this->pathname);
                    205:                free(this->value.ptr);
                    206:                free(this);
                    207:        }
                    208: }
                    209: 
                    210: METHOD(tcg_pts_attr_req_file_meta_t, get_directory_flag, bool,
                    211:        private_tcg_pts_attr_req_file_meta_t *this)
                    212: {
                    213:        return this->directory_flag;
                    214: }
                    215: 
                    216: METHOD(tcg_pts_attr_req_file_meta_t, get_delimiter, uint8_t,
                    217:        private_tcg_pts_attr_req_file_meta_t *this)
                    218: {
                    219:        return this->delimiter;
                    220: }
                    221: 
                    222: METHOD(tcg_pts_attr_req_file_meta_t, get_pathname, char*,
                    223:        private_tcg_pts_attr_req_file_meta_t *this)
                    224: {
                    225:        return this->pathname;
                    226: }
                    227: 
                    228: /**
                    229:  * Described in header.
                    230:  */
                    231: pa_tnc_attr_t *tcg_pts_attr_req_file_meta_create(bool directory_flag,
                    232:                                                                                                 uint8_t delimiter,
                    233:                                                                                                 char *pathname)
                    234: {
                    235:        private_tcg_pts_attr_req_file_meta_t *this;
                    236: 
                    237:        INIT(this,
                    238:                .public = {
                    239:                        .pa_tnc_attribute = {
                    240:                                .get_type = _get_type,
                    241:                                .get_value = _get_value,
                    242:                                .get_noskip_flag = _get_noskip_flag,
                    243:                                .set_noskip_flag = _set_noskip_flag,
                    244:                                .build = _build,
                    245:                                .process = _process,
                    246:                                .add_segment = _add_segment,
                    247:                                .get_ref = _get_ref,
                    248:                                .destroy = _destroy,
                    249:                        },
                    250:                        .get_directory_flag = _get_directory_flag,
                    251:                        .get_delimiter = _get_delimiter,
                    252:                        .get_pathname = _get_pathname,
                    253:                },
                    254:                .type = { PEN_TCG, TCG_PTS_REQ_FILE_META },
                    255:                .directory_flag = directory_flag,
                    256:                .delimiter = delimiter,
                    257:                .pathname = strdup(pathname),
                    258:                .ref = 1,
                    259:        );
                    260: 
                    261:        return &this->public.pa_tnc_attribute;
                    262: }
                    263: 
                    264: 
                    265: /**
                    266:  * Described in header.
                    267:  */
                    268: pa_tnc_attr_t *tcg_pts_attr_req_file_meta_create_from_data(size_t length,
                    269:                                                                                                                   chunk_t data)
                    270: {
                    271:        private_tcg_pts_attr_req_file_meta_t *this;
                    272: 
                    273:        INIT(this,
                    274:                .public = {
                    275:                        .pa_tnc_attribute = {
                    276:                                .get_type = _get_type,
                    277:                                .get_value = _get_value,
                    278:                                .get_noskip_flag = _get_noskip_flag,
                    279:                                .set_noskip_flag = _set_noskip_flag,
                    280:                                .build = _build,
                    281:                                .process = _process,
                    282:                                .add_segment = _add_segment,
                    283:                                .get_ref = _get_ref,
                    284:                                .destroy = _destroy,
                    285:                        },
                    286:                        .get_directory_flag = _get_directory_flag,
                    287:                        .get_delimiter = _get_delimiter,
                    288:                        .get_pathname = _get_pathname,
                    289:                },
                    290:                .type = { PEN_TCG, TCG_PTS_REQ_FILE_META },
                    291:                .length = length,
                    292:                .value = chunk_clone(data),
                    293:                .ref = 1,
                    294:        );
                    295: 
                    296:        return &this->public.pa_tnc_attribute;
                    297: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>