summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/pubkey/pubkey_cert.c
blob: 67240fe0cffab5f75aaa9fa856d85b1c837acea1 (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
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
/*
 * Copyright (C) 2008 Martin Willi
 * 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 "pubkey_cert.h"

#include <time.h>

#include <debug.h>

typedef struct private_pubkey_cert_t private_pubkey_cert_t;

/**
 * private data of pubkey_cert
 */
struct private_pubkey_cert_t {

	/**
	 * public functions
	 */
	pubkey_cert_t public;

	/**
	 * wrapped public key
	 */
	public_key_t *key;

	/**
	 * dummy issuer id, ID_ANY
	 */
	identification_t *issuer;

	/**
	 * subject, ID_KEY_ID of the public key
	 */
	identification_t *subject;

	/**
	 * key inception time
	 */
	time_t notBefore;

	/**
	 * key expiration time
	 */
	time_t notAfter;

	/**
	 * reference count
	 */
	refcount_t ref;
};

METHOD(certificate_t, get_type, certificate_type_t,
	private_pubkey_cert_t *this)
{
	return CERT_TRUSTED_PUBKEY;
}

METHOD(certificate_t, get_subject, identification_t*,
	private_pubkey_cert_t *this)
{
	return this->subject;
}

METHOD(certificate_t, get_issuer, identification_t*,
	private_pubkey_cert_t *this)
{
	return this->issuer;
}

METHOD(certificate_t, has_subject, id_match_t,
	private_pubkey_cert_t *this, identification_t *subject)
{
	if (subject->get_type(subject) == ID_KEY_ID)
	{
		cred_encoding_type_t type;
		chunk_t fingerprint;

		for (type = 0; type < CRED_ENCODING_MAX; type++)
		{
			if (this->key->get_fingerprint(this->key, type, &fingerprint) &&
				chunk_equals(fingerprint, subject->get_encoding(subject)))
			{
				return ID_MATCH_PERFECT;
			}
		}
	}

	return this->subject->matches(this->subject, subject);
}

METHOD(certificate_t, has_issuer, id_match_t,
	private_pubkey_cert_t *this, identification_t *issuer)
{
	return ID_MATCH_NONE;
}

METHOD(certificate_t, equals, bool,
	private_pubkey_cert_t *this, certificate_t *other)
{
	public_key_t *other_key;

	other_key = other->get_public_key(other);
	if (other_key)
	{
		if (public_key_equals(this->key, other_key))
		{
			other_key->destroy(other_key);
			return TRUE;
		}
		other_key->destroy(other_key);
	}
	return FALSE;
}

METHOD(certificate_t, issued_by, bool,
	private_pubkey_cert_t *this, certificate_t *issuer)
{
	return equals(this, issuer);
}

METHOD(certificate_t, get_public_key,  public_key_t*,
	private_pubkey_cert_t *this)
{
	this->key->get_ref(this->key);
	return this->key;
}

METHOD(certificate_t, get_validity, bool,
	private_pubkey_cert_t *this, time_t *when, time_t *not_before,
	time_t *not_after)
{
	time_t t = when ? *when : time(NULL);

	if (not_before)
	{
		*not_before = this->notBefore;
	}
	if (not_after)
	{
		*not_after = this->notAfter;
	}
	return ((this->notBefore == UNDEFINED_TIME || t >= this->notBefore) &&
			(this->notAfter  == UNDEFINED_TIME || t <= this->notAfter));
}

METHOD(certificate_t, get_encoding, bool,
	private_pubkey_cert_t *this, cred_encoding_type_t type, chunk_t *encoding)
{
	return this->key->get_encoding(this->key, type, encoding);
}

METHOD(certificate_t, get_ref, certificate_t*,
	private_pubkey_cert_t *this)
{
	ref_get(&this->ref);
	return &this->public.interface;
}

METHOD(certificate_t, destroy, void,
	private_pubkey_cert_t *this)
{
	if (ref_put(&this->ref))
	{
		this->subject->destroy(this->subject);
		this->issuer->destroy(this->issuer);
		this->key->destroy(this->key);
		free(this);
	}
}

/*
 * see header file
 */
static pubkey_cert_t *pubkey_cert_create(public_key_t *key,
										 time_t notBefore, time_t notAfter,
										 identification_t *subject)
{
	private_pubkey_cert_t *this;
	chunk_t fingerprint;

	INIT(this,
		.public = {
			.interface = {
				.get_type = _get_type,
				.get_subject = _get_subject,
				.get_issuer = _get_issuer,
				.has_subject = _has_subject,
				.has_issuer = _has_issuer,
				.issued_by = _issued_by,
				.get_public_key = _get_public_key,
				.get_validity = _get_validity,
				.get_encoding = _get_encoding,
				.equals = _equals,
				.get_ref = _get_ref,
				.destroy = _destroy,
			},
		},
		.ref = 1,
		.key = key,
		.notBefore = notBefore,
		.notAfter = notAfter,
		.issuer = identification_create_from_encoding(ID_ANY, chunk_empty),
	);

	if (subject)
	{
		this->subject = subject->clone(subject);
	}
	else if (key->get_fingerprint(key, KEYID_PUBKEY_INFO_SHA1, &fingerprint))
	{
		this->subject = identification_create_from_encoding(ID_KEY_ID, fingerprint);
	}
	else
	{
		this->subject = identification_create_from_encoding(ID_ANY, chunk_empty);
	}

	return &this->public;
}

/**
 * See header.
 */
pubkey_cert_t *pubkey_cert_wrap(certificate_type_t type, va_list args)
{
	public_key_t *key = NULL;
	chunk_t blob = chunk_empty;
	identification_t *subject = NULL;
	time_t notBefore = UNDEFINED_TIME, notAfter = UNDEFINED_TIME;

	while (TRUE)
	{
		switch (va_arg(args, builder_part_t))
		{
			case BUILD_BLOB_ASN1_DER:
				blob = va_arg(args, chunk_t);
				continue;
			case BUILD_PUBLIC_KEY:
				key = va_arg(args, public_key_t*);
				continue;
			case BUILD_NOT_BEFORE_TIME:
				notBefore = va_arg(args, time_t);
				continue;
			case BUILD_NOT_AFTER_TIME:
				notAfter = va_arg(args, time_t);
				continue;
			case BUILD_SUBJECT:
				subject = va_arg(args, identification_t*);
				continue;
			case BUILD_END:
				break;
			default:
				return NULL;
		}
		break;
	}
	if (key)
	{
		key->get_ref(key);
	}
	else if (blob.ptr)
	{
		key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
								 BUILD_BLOB_ASN1_DER, blob, BUILD_END);
	}
	if (key)
	{
		return pubkey_cert_create(key, notBefore, notAfter, subject);
	}
	return NULL;
}