summaryrefslogtreecommitdiffstats
path: root/lasso/key.c
blob: 747492c6d84cf5e717b6bdd349993e91296587d3 (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004-2011 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: See AUTHORS file in top-level directory.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "key.h"
#include "keyprivate.h"
#include "xml/private.h"
#include "xmlsec/xmltree.h"

/*****************************************************************************/
/* private methods                                                           */
/*****************************************************************************/

struct _LassoKeyPrivate {
	enum _LassoKeyType type;
	union {
		LassoSignatureContext signature;
	} context;
};

#define LASSO_KEY_GET_PRIVATE(o) \
	   (G_TYPE_INSTANCE_GET_PRIVATE ((o), LASSO_TYPE_KEY, LassoKeyPrivate))

static struct XmlSnippet schema_snippets[] = {
	{NULL, 0, 0, NULL, NULL, NULL}
};

static LassoNodeClass *parent_class = NULL;


/*****************************************************************************/
/* instance and class init functions                                         */
/*****************************************************************************/

static void
instance_init(LassoKey *key)
{
	key->private_data = LASSO_KEY_GET_PRIVATE(key);
}

static void
dispose(GObject *g_object)
{
	LassoKey *key = (LassoKey*)g_object;

	if (key->private_data) {
		switch (key->private_data->type) {
			case LASSO_KEY_TYPE_FOR_SIGNATURE:
				lasso_assign_new_signature_context(
						key->private_data->context.signature,
						LASSO_SIGNATURE_CONTEXT_NONE);
				break;
		}
	}

	G_OBJECT_CLASS(parent_class)->dispose(G_OBJECT(key));
}

static void
class_init(LassoKeyClass *klass)
{
	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);

	parent_class = g_type_class_peek_parent(klass);
	nclass->node_data = g_new0(LassoNodeClassData, 1);
	lasso_node_class_set_nodename(nclass, "Key");
	lasso_node_class_set_ns(nclass, LASSO_LASSO_HREF, LASSO_LASSO_PREFIX);
	lasso_node_class_add_snippets(nclass, schema_snippets);
	g_type_class_add_private(klass, sizeof(LassoKeyPrivate));
	G_OBJECT_CLASS(klass)->dispose = dispose;
}

GType
lasso_key_get_type()
{
	static GType this_type = 0;

	if (!this_type) {
		static const GTypeInfo this_info = {
			sizeof (LassoKeyClass),
			NULL,
			NULL,
			(GClassInitFunc) class_init,
			NULL,
			NULL,
			sizeof(LassoKey),
			0,
			(GInstanceInitFunc) instance_init,
			NULL
		};

		this_type = g_type_register_static(LASSO_TYPE_NODE,
				"LassoKey", &this_info, 0);
	}
	return this_type;
}

static LassoKey*
lasso_key_new()
{
	return g_object_new(LASSO_TYPE_KEY, NULL);
}

static LassoKey*
lasso_key_new_for_signature_from_context(LassoSignatureContext context) {
	LassoKey *key = lasso_key_new();

	key->private_data->type = LASSO_KEY_TYPE_FOR_SIGNATURE;
	lasso_assign_new_signature_context(
			key->private_data->context.signature, context);
	if (! lasso_validate_signature_context(key->private_data->context.signature)) {
		lasso_release_gobject(key);
	}
	return key;
}

/**
 * lasso_key_new_for_signature_from_file:
 * @filename_or_buffer: a file path of a string containing the key PEM or Base64 encoded
 * @password: an eventual password to decoded the private key contained in @buffer
 * @signature_method: the signature method to associate to this key
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
 *
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
 * provider.
 *
 * Return value:(transfer full): a newly allocated #LassoKey object
 */
LassoKey*
lasso_key_new_for_signature_from_file(char *filename_or_buffer,
		char *password,
		LassoSignatureMethod signature_method,
		char *certificate) {
	return lasso_key_new_for_signature_from_context(
			lasso_make_signature_context_from_path_or_string(filename_or_buffer,
				password,
				signature_method,
				certificate));
}

/**
 * lasso_key_new_for_signature_from_memory:
 * @buffer: a byte buffer of size @size
 * @size: the size of @buffer
 * @password: an eventual password to decoded the private key contained in @buffer
 * @signature_method: the signature method to associate to this key
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
 *
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
 * provider.
 *
 * Return value:(transfer full): a newly allocated #LassoKey object
 */
LassoKey*
lasso_key_new_for_signature_from_memory(const void *buffer,
		size_t size,
		char *password,
		LassoSignatureMethod signature_method,
		char *certificate)
{
	return lasso_key_new_for_signature_from_context(
			lasso_make_signature_context_from_buffer(buffer,
				size,
				password,
				signature_method,
				certificate));
}

/**
 * lasso_key_new_for_signature_from_base64_string:
 * @base64_string: a NULL-terminated string containing a base64 encode representation of the key
 * @password: an eventual password to decoded the private key contained in @buffer
 * @signature_method: the signature method to associate to this key
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
 *
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
 * provider.
 *
 * Return value:(transfer full): a newly allocated #LassoKey object
 */
LassoKey*
lasso_key_new_for_signature_from_base64_string(char *base64_string,
		char *password,
		LassoSignatureMethod signature_method,
		char *certificate)
{
	LassoKey *key = NULL;
	char *buffer = NULL;
	int length = 0;

	if (lasso_base64_decode(base64_string, &buffer, &length)) {
		key = lasso_key_new_for_signature_from_context(
				lasso_make_signature_context_from_buffer(buffer,
					length,
					password,
					signature_method,
					certificate));
		lasso_release_string(buffer);
	}
	return key;
}

static xmlNode *
find_xmlnode_with_saml2_id(xmlNode *xmlnode, const char *id)
{
	xmlNode *found = NULL;
	xmlNode *t;

	if (! xmlnode)
		return NULL;

	if (xmlHasProp(xmlnode, BAD_CAST "ID")) {
		xmlChar *value;

		value = xmlGetProp(xmlnode, BAD_CAST "ID");
		if (lasso_strisequal((char*)value, id)) {
			found = xmlnode;
		}
		xmlFree(value);
	}
	if (found) {
		return found;
	}
	t = xmlSecGetNextElementNode(xmlnode->children);
	while (t) {
		found = find_xmlnode_with_saml2_id(t, id);
		if (found) {
			return found;
		}
		t = xmlSecGetNextElementNode(t->next);
	}
	return NULL;
}

/**
 * lasso_key_saml2_xml_verify:
 * @key: a #LassoKey object
 * @id: the value of the ID attribute of signed node
 * @document: the document containing the signed node
 *
 * Verify the first signature node child of the node with the given id. It follows from the profile
 * of XMLDsig used by the SAML 2.0 specification.
 *
 * Return value: 0 if the signature validate, an error code otherwise.
 */
lasso_error_t
lasso_key_saml2_xml_verify(LassoKey *key, char *id, xmlNode *document)
{
	xmlNode *signed_node;
	LassoSignatureContext signature_context;


	signed_node = find_xmlnode_with_saml2_id(document, id);
	if (! signed_node) {
		return LASSO_DS_ERROR_INVALID_REFERENCE_FOR_SAML;
	}
	signature_context = lasso_key_get_signature_context(key);
	return lasso_verify_signature(signed_node, signed_node->doc, "ID", NULL,
			signature_context.signature_key, NO_OPTION, NULL);
}

/**
 * lasso_key_saml2_xml_sign:
 * @key: a #LassoKey object
 * @id: the value of the ID attribute of signed node
 * @document: the document containing the signed node
 *
 * Sign the first signature node child of the node with the given id. It no signature node is found
 * a new one is added at the end of the children list of the signed node.
 *
 * The passed document node is modified in-place.
 *
 * Return value: The modified xmlNode object, or NULL if the signature failed.
 */
xmlNode*
lasso_key_saml2_xml_sign(LassoKey *key, const char *id, xmlNode *document)
{
	xmlNode *signed_node;
	LassoSignatureContext signature_context;

	signed_node = find_xmlnode_with_saml2_id(document, id);
	if (! signed_node) {
		return NULL;
	}
	signature_context = lasso_key_get_signature_context(key);
	lasso_xmlnode_add_saml2_signature_template(signed_node, signature_context, id);
	if (lasso_sign_node(signed_node, signature_context,
			"ID", id) == 0) {
		return document;
	} else {
		return NULL;
	}
}

/**
 * lasso_key_query_verify:
 * key: a #LassoKey object
 * query: a raw HTTP query string
 *
 * Check if this query string contains a proper SAML2 signature for this key.
 *
 * Return value: 0 if a valid signature was found, an error code otherwise.
 */
lasso_error_t
lasso_key_query_verify(LassoKey *key, const char *query)
{
	LassoSignatureContext signature_context;
	lasso_bad_param(KEY, key);

	signature_context = lasso_key_get_signature_context(key);
	if (! lasso_validate_signature_context(signature_context))
		return LASSO_ERROR_UNDEFINED;
	return lasso_saml2_query_verify_signature(query, signature_context.signature_key);
}

/**
 * lasso_key_query_verify:
 * key: a #LassoKey object
 * query: a raw HTTP query string
 *
 * Sign the given query string using the given key.
 *
 * Return value: the signed query string.
 */
char*
lasso_key_query_sign(LassoKey *key, const char *query)
{
	LassoSignatureContext signature_context;

	if (! LASSO_IS_KEY(key))
		return NULL;
	signature_context = lasso_key_get_signature_context(key);
	if (! lasso_validate_signature_context(signature_context))
		return NULL;
	return lasso_query_sign((char*)query, signature_context);
}

/**
 * lasso_key_get_signature_context:
 * @key: a #LassoKey object
 *
 * Private method to extract the signature context embedded in a LassoKey object.
 *
 * Return value: a #LassoSignatureContext structure value.
 */
LassoSignatureContext
lasso_key_get_signature_context(LassoKey *key) {
	if (key->private_data && key->private_data->type == LASSO_KEY_TYPE_FOR_SIGNATURE) {
		return key->private_data->context.signature;
	}
	return LASSO_SIGNATURE_CONTEXT_NONE;
}

/**
 * lasso_key_get_key_type:
 * @key: a #LassoKey object
 *
 * Return the type of key, i.e. which operation it supports.
 */
LassoKeyType
lasso_key_get_key_type(LassoKey *key) {
	lasso_return_val_if_fail(LASSO_IS_KEY(key),
			LASSO_KEY_TYPE_FOR_SIGNATURE);
	return key->private_data->type;
}