summaryrefslogtreecommitdiff
path: root/src/libtncif/tncif_identity.c
blob: 7ee215c7796cf79dbecdf9bf4a2ac85313e66169 (plain)
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
/*
 * Copyright (C) 2013 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "tncif_identity.h"

#include <bio/bio_writer.h>
#include <bio/bio_reader.h>
#include <pen/pen.h>
#include <utils/debug.h>

typedef struct private_tncif_identity_t private_tncif_identity_t;

/**
 * TNC Identity List Attribute Format (TCG TNC IF-IMV 1.4 Draft)
 *
 *                      1                   2                   3
 *   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
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                        Identity Count                         |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |   RESERVED    |            Identity Type Vendor ID            |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                         Identity Type                         |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                     Identity Value Length                     |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                                                               |
 *  ~                         Identity Value                        ~
 *  |                                                               |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |   RESERVED    |            Subject Type Vendor ID             |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                         Subject Type                          |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |   RESERVED    |        Authentication Method Vendor ID        |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                     Authentication Method                     |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

/**
 * Private data of a tncif_identity_t object.
 *
 */
struct private_tncif_identity_t {

	/**
	 * Public tncif_identity_t interface.
	 */
	tncif_identity_t public;

	/**
	 * Identity Type
	 */
	pen_type_t identity_type;

	/**
	 * Identity Value
	 */
	chunk_t identity_value;

	/**
	 * Subject Type
	 */
	pen_type_t subject_type;

	/**
	 * Authentication Type
	 */
	pen_type_t auth_type;
};

METHOD(tncif_identity_t, get_identity_type, pen_type_t,
	private_tncif_identity_t *this)
{
	return this->identity_type;
}

METHOD(tncif_identity_t, get_identity_value, chunk_t,
	private_tncif_identity_t *this)
{
	return this->identity_value;
}

METHOD(tncif_identity_t, get_subject_type, pen_type_t,
	private_tncif_identity_t *this)
{
	return this->subject_type;
}

METHOD(tncif_identity_t, get_auth_type, pen_type_t,
	private_tncif_identity_t *this)
{
	return this->auth_type;
}

METHOD(tncif_identity_t, build, void,
	private_tncif_identity_t *this, bio_writer_t *writer)
{
	writer->write_uint32(writer, this->identity_type.vendor_id);
	writer->write_uint32(writer, this->identity_type.type);
	writer->write_data32(writer, this->identity_value);
	writer->write_uint32(writer, this->subject_type.vendor_id);
	writer->write_uint32(writer, this->subject_type.type);
	writer->write_uint32(writer, this->auth_type.vendor_id);
	writer->write_uint32(writer, this->auth_type.type);
}

METHOD(tncif_identity_t, process, bool,
	private_tncif_identity_t *this, bio_reader_t *reader)
{
	u_int8_t reserved;
	u_int32_t vendor_id, type;
	chunk_t identity_value;

	if (reader->remaining(reader) < TNCIF_IDENTITY_MIN_SIZE)
	{
		return FALSE;
	}
	reader->read_uint8 (reader, &reserved);
	reader->read_uint24(reader, &vendor_id);
	reader->read_uint32(reader, &type);
	this->identity_type = pen_type_create(vendor_id, type);

	if (!reader->read_data32(reader, &identity_value) ||
		 reader->remaining(reader) < 16)
	{
		return FALSE;
	}
	this->identity_value = chunk_clone(identity_value);

	reader->read_uint8 (reader, &reserved);
	reader->read_uint24(reader, &vendor_id);
	reader->read_uint32(reader, &type);
	this->subject_type = pen_type_create(vendor_id, type);		

	reader->read_uint8 (reader, &reserved);
	reader->read_uint24(reader, &vendor_id);
	reader->read_uint32(reader, &type);
	this->auth_type = pen_type_create(vendor_id, type);

	return TRUE;
}

METHOD(tncif_identity_t, destroy, void,
	private_tncif_identity_t *this)
{
	free(this->identity_value.ptr);
	free(this);
}


/**
 * See header
 */
tncif_identity_t *tncif_identity_create_empty(void)
{
	private_tncif_identity_t *this;

	INIT(this,
		.public = {
			.get_identity_type = _get_identity_type,
			.get_identity_value = _get_identity_value,
			.get_subject_type = _get_subject_type,
			.get_auth_type = _get_auth_type,
			.build = _build,
			.process = _process,
			.destroy = _destroy,
		},
	);

	return &this->public;
}

/**
 * See header
 */
tncif_identity_t *tncif_identity_create(pen_type_t identity_type,
										chunk_t identity_value,
										pen_type_t subject_type,
										pen_type_t auth_type)
{
	private_tncif_identity_t *this;

	this = (private_tncif_identity_t*)tncif_identity_create_empty();
	this->identity_type = identity_type;
	this->identity_value = identity_value;
	this->subject_type = subject_type;
	this->auth_type = auth_type;

	return &this->public;
}