Blame lasso/key.c

Packit 228f82
/*
Packit 228f82
 * Lasso - A free implementation of the Liberty Alliance specifications.
Packit 228f82
 *
Packit 228f82
 * Copyright (C) 2004-2011 Entr'ouvert
Packit 228f82
 * http://lasso.entrouvert.org
Packit 228f82
 *
Packit 228f82
 * Authors: See AUTHORS file in top-level directory.
Packit 228f82
 *
Packit 228f82
 * This program is free software; you can redistribute it and/or modify
Packit 228f82
 * it under the terms of the GNU General Public License as published by
Packit 228f82
 * the Free Software Foundation; either version 2 of the License, or
Packit 228f82
 * (at your option) any later version.
Packit 228f82
 *
Packit 228f82
 * This program is distributed in the hope that it will be useful,
Packit 228f82
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 228f82
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 228f82
 * GNU General Public License for more details.
Packit 228f82
 *
Packit 228f82
 * You should have received a copy of the GNU General Public License
Packit 228f82
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 228f82
 */
Packit 228f82
Packit 228f82
#include "key.h"
Packit 228f82
#include "keyprivate.h"
Packit 228f82
#include "xml/private.h"
Packit 228f82
#include "xmlsec/xmltree.h"
Packit 228f82
Packit 228f82
/*****************************************************************************/
Packit 228f82
/* private methods                                                           */
Packit 228f82
/*****************************************************************************/
Packit 228f82
Packit 228f82
struct _LassoKeyPrivate {
Packit 228f82
	enum _LassoKeyType type;
Packit 228f82
	union {
Packit 228f82
		LassoSignatureContext signature;
Packit 228f82
	} context;
Packit 228f82
};
Packit 228f82
Packit 228f82
#define LASSO_KEY_GET_PRIVATE(o) \
Packit 228f82
	   (G_TYPE_INSTANCE_GET_PRIVATE ((o), LASSO_TYPE_KEY, LassoKeyPrivate))
Packit 228f82
Packit 228f82
static struct XmlSnippet schema_snippets[] = {
Packit 228f82
	{NULL, 0, 0, NULL, NULL, NULL}
Packit 228f82
};
Packit 228f82
Packit 228f82
static LassoNodeClass *parent_class = NULL;
Packit 228f82
Packit 228f82
Packit 228f82
/*****************************************************************************/
Packit 228f82
/* instance and class init functions                                         */
Packit 228f82
/*****************************************************************************/
Packit 228f82
Packit 228f82
static void
Packit 228f82
instance_init(LassoKey *key)
Packit 228f82
{
Packit 228f82
	key->private_data = LASSO_KEY_GET_PRIVATE(key);
Packit 228f82
}
Packit 228f82
Packit 228f82
static void
Packit 228f82
dispose(GObject *g_object)
Packit 228f82
{
Packit 228f82
	LassoKey *key = (LassoKey*)g_object;
Packit 228f82
Packit 228f82
	if (key->private_data) {
Packit 228f82
		switch (key->private_data->type) {
Packit 228f82
			case LASSO_KEY_TYPE_FOR_SIGNATURE:
Packit 228f82
				lasso_assign_new_signature_context(
Packit 228f82
						key->private_data->context.signature,
Packit 228f82
						LASSO_SIGNATURE_CONTEXT_NONE);
Packit 228f82
				break;
Packit 228f82
		}
Packit 228f82
	}
Packit 228f82
Packit 228f82
	G_OBJECT_CLASS(parent_class)->dispose(G_OBJECT(key));
Packit 228f82
}
Packit 228f82
Packit 228f82
static void
Packit 228f82
class_init(LassoKeyClass *klass)
Packit 228f82
{
Packit 228f82
	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
Packit 228f82
Packit 228f82
	parent_class = g_type_class_peek_parent(klass);
Packit 228f82
	nclass->node_data = g_new0(LassoNodeClassData, 1);
Packit 228f82
	lasso_node_class_set_nodename(nclass, "Key");
Packit 228f82
	lasso_node_class_set_ns(nclass, LASSO_LASSO_HREF, LASSO_LASSO_PREFIX);
Packit 228f82
	lasso_node_class_add_snippets(nclass, schema_snippets);
Packit 228f82
	g_type_class_add_private(klass, sizeof(LassoKeyPrivate));
Packit 228f82
	G_OBJECT_CLASS(klass)->dispose = dispose;
Packit 228f82
}
Packit 228f82
Packit 228f82
GType
Packit 228f82
lasso_key_get_type()
Packit 228f82
{
Packit 228f82
	static GType this_type = 0;
Packit 228f82
Packit 228f82
	if (!this_type) {
Packit 228f82
		static const GTypeInfo this_info = {
Packit 228f82
			sizeof (LassoKeyClass),
Packit 228f82
			NULL,
Packit 228f82
			NULL,
Packit 228f82
			(GClassInitFunc) class_init,
Packit 228f82
			NULL,
Packit 228f82
			NULL,
Packit 228f82
			sizeof(LassoKey),
Packit 228f82
			0,
Packit 228f82
			(GInstanceInitFunc) instance_init,
Packit 228f82
			NULL
Packit 228f82
		};
Packit 228f82
Packit 228f82
		this_type = g_type_register_static(LASSO_TYPE_NODE,
Packit 228f82
				"LassoKey", &this_info, 0);
Packit 228f82
	}
Packit 228f82
	return this_type;
Packit 228f82
}
Packit 228f82
Packit 228f82
static LassoKey*
Packit 228f82
lasso_key_new()
Packit 228f82
{
Packit 228f82
	return g_object_new(LASSO_TYPE_KEY, NULL);
Packit 228f82
}
Packit 228f82
Packit 228f82
static LassoKey*
Packit 228f82
lasso_key_new_for_signature_from_context(LassoSignatureContext context) {
Packit 228f82
	LassoKey *key = lasso_key_new();
Packit 228f82
Packit 228f82
	key->private_data->type = LASSO_KEY_TYPE_FOR_SIGNATURE;
Packit 228f82
	lasso_assign_new_signature_context(
Packit 228f82
			key->private_data->context.signature, context);
Packit 228f82
	if (! lasso_validate_signature_context(key->private_data->context.signature)) {
Packit 228f82
		lasso_release_gobject(key);
Packit 228f82
	}
Packit 228f82
	return key;
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_new_for_signature_from_file:
Packit 228f82
 * @filename_or_buffer: a file path of a string containing the key PEM or Base64 encoded
Packit 228f82
 * @password: an eventual password to decoded the private key contained in @buffer
Packit 228f82
 * @signature_method: the signature method to associate to this key
Packit 228f82
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
Packit 228f82
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
Packit 228f82
 *
Packit 228f82
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
Packit 228f82
 * provider.
Packit 228f82
 *
Packit 228f82
 * Return value:(transfer full): a newly allocated #LassoKey object
Packit 228f82
 */
Packit 228f82
LassoKey*
Packit 228f82
lasso_key_new_for_signature_from_file(char *filename_or_buffer,
Packit 228f82
		char *password,
Packit 228f82
		LassoSignatureMethod signature_method,
Packit 228f82
		char *certificate) {
Packit 228f82
	return lasso_key_new_for_signature_from_context(
Packit 228f82
			lasso_make_signature_context_from_path_or_string(filename_or_buffer,
Packit 228f82
				password,
Packit 228f82
				signature_method,
Packit 228f82
				certificate));
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_new_for_signature_from_memory:
Packit 228f82
 * @buffer: a byte buffer of size @size
Packit 228f82
 * @size: the size of @buffer
Packit 228f82
 * @password: an eventual password to decoded the private key contained in @buffer
Packit 228f82
 * @signature_method: the signature method to associate to this key
Packit 228f82
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
Packit 228f82
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
Packit 228f82
 *
Packit 228f82
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
Packit 228f82
 * provider.
Packit 228f82
 *
Packit 228f82
 * Return value:(transfer full): a newly allocated #LassoKey object
Packit 228f82
 */
Packit 228f82
LassoKey*
Packit 228f82
lasso_key_new_for_signature_from_memory(const void *buffer,
Packit 228f82
		size_t size,
Packit 228f82
		char *password,
Packit 228f82
		LassoSignatureMethod signature_method,
Packit 228f82
		char *certificate)
Packit 228f82
{
Packit 228f82
	return lasso_key_new_for_signature_from_context(
Packit 228f82
			lasso_make_signature_context_from_buffer(buffer,
Packit 228f82
				size,
Packit 228f82
				password,
Packit 228f82
				signature_method,
Packit 228f82
				certificate));
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_new_for_signature_from_base64_string:
Packit 228f82
 * @base64_string: a NULL-terminated string containing a base64 encode representation of the key
Packit 228f82
 * @password: an eventual password to decoded the private key contained in @buffer
Packit 228f82
 * @signature_method: the signature method to associate to this key
Packit 228f82
 * @certificate: a certificate as a file path or PEM encoded in a NULL-terminated string, to
Packit 228f82
 * associate with the key, it will be used to fill the KeyInfo node in an eventual signature.
Packit 228f82
 *
Packit 228f82
 * Create a new #LassoKey object, you can use it to sign XML message or to specify the key of a
Packit 228f82
 * provider.
Packit 228f82
 *
Packit 228f82
 * Return value:(transfer full): a newly allocated #LassoKey object
Packit 228f82
 */
Packit 228f82
LassoKey*
Packit 228f82
lasso_key_new_for_signature_from_base64_string(char *base64_string,
Packit 228f82
		char *password,
Packit 228f82
		LassoSignatureMethod signature_method,
Packit 228f82
		char *certificate)
Packit 228f82
{
Packit 228f82
	LassoKey *key = NULL;
Packit 228f82
	char *buffer = NULL;
Packit 228f82
	int length = 0;
Packit 228f82
Packit 228f82
	if (lasso_base64_decode(base64_string, &buffer, &length)) {
Packit 228f82
		key = lasso_key_new_for_signature_from_context(
Packit 228f82
				lasso_make_signature_context_from_buffer(buffer,
Packit 228f82
					length,
Packit 228f82
					password,
Packit 228f82
					signature_method,
Packit 228f82
					certificate));
Packit 228f82
		lasso_release_string(buffer);
Packit 228f82
	}
Packit 228f82
	return key;
Packit 228f82
}
Packit 228f82
Packit 228f82
static xmlNode *
Packit 228f82
find_xmlnode_with_saml2_id(xmlNode *xmlnode, const char *id)
Packit 228f82
{
Packit 228f82
	xmlNode *found = NULL;
Packit 228f82
	xmlNode *t;
Packit 228f82
Packit 228f82
	if (! xmlnode)
Packit 228f82
		return NULL;
Packit 228f82
Packit 228f82
	if (xmlHasProp(xmlnode, BAD_CAST "ID")) {
Packit 228f82
		xmlChar *value;
Packit 228f82
Packit 228f82
		value = xmlGetProp(xmlnode, BAD_CAST "ID");
Packit 228f82
		if (lasso_strisequal((char*)value, id)) {
Packit 228f82
			found = xmlnode;
Packit 228f82
		}
Packit 228f82
		xmlFree(value);
Packit 228f82
	}
Packit 228f82
	if (found) {
Packit 228f82
		return found;
Packit 228f82
	}
Packit 228f82
	t = xmlSecGetNextElementNode(xmlnode->children);
Packit 228f82
	while (t) {
Packit 228f82
		found = find_xmlnode_with_saml2_id(t, id);
Packit 228f82
		if (found) {
Packit 228f82
			return found;
Packit 228f82
		}
Packit 228f82
		t = xmlSecGetNextElementNode(t->next);
Packit 228f82
	}
Packit 228f82
	return NULL;
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_saml2_xml_verify:
Packit 228f82
 * @key: a #LassoKey object
Packit 228f82
 * @id: the value of the ID attribute of signed node
Packit 228f82
 * @document: the document containing the signed node
Packit 228f82
 *
Packit 228f82
 * Verify the first signature node child of the node with the given id. It follows from the profile
Packit 228f82
 * of XMLDsig used by the SAML 2.0 specification.
Packit 228f82
 *
Packit 228f82
 * Return value: 0 if the signature validate, an error code otherwise.
Packit 228f82
 */
Packit 228f82
lasso_error_t
Packit 228f82
lasso_key_saml2_xml_verify(LassoKey *key, char *id, xmlNode *document)
Packit 228f82
{
Packit 228f82
	xmlNode *signed_node;
Packit 228f82
	LassoSignatureContext signature_context;
Packit 228f82
Packit 228f82
Packit 228f82
	signed_node = find_xmlnode_with_saml2_id(document, id);
Packit 228f82
	if (! signed_node) {
Packit 228f82
		return LASSO_DS_ERROR_INVALID_REFERENCE_FOR_SAML;
Packit 228f82
	}
Packit 228f82
	signature_context = lasso_key_get_signature_context(key);
Packit 228f82
	return lasso_verify_signature(signed_node, signed_node->doc, "ID", NULL,
Packit 228f82
			signature_context.signature_key, NO_OPTION, NULL);
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_saml2_xml_sign:
Packit 228f82
 * @key: a #LassoKey object
Packit 228f82
 * @id: the value of the ID attribute of signed node
Packit 228f82
 * @document: the document containing the signed node
Packit 228f82
 *
Packit 228f82
 * Sign the first signature node child of the node with the given id. It no signature node is found
Packit 228f82
 * a new one is added at the end of the children list of the signed node.
Packit 228f82
 *
Packit 228f82
 * The passed document node is modified in-place.
Packit 228f82
 *
Packit 228f82
 * Return value: The modified xmlNode object, or NULL if the signature failed.
Packit 228f82
 */
Packit 228f82
xmlNode*
Packit 228f82
lasso_key_saml2_xml_sign(LassoKey *key, const char *id, xmlNode *document)
Packit 228f82
{
Packit 228f82
	xmlNode *signed_node;
Packit 228f82
	LassoSignatureContext signature_context;
Packit 228f82
Packit 228f82
	signed_node = find_xmlnode_with_saml2_id(document, id);
Packit 228f82
	if (! signed_node) {
Packit 228f82
		return NULL;
Packit 228f82
	}
Packit 228f82
	signature_context = lasso_key_get_signature_context(key);
Packit 228f82
	lasso_xmlnode_add_saml2_signature_template(signed_node, signature_context, id);
Packit 228f82
	if (lasso_sign_node(signed_node, signature_context,
Packit 228f82
			"ID", id) == 0) {
Packit 228f82
		return document;
Packit 228f82
	} else {
Packit 228f82
		return NULL;
Packit 228f82
	}
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_query_verify:
Packit 228f82
 * key: a #LassoKey object
Packit 228f82
 * query: a raw HTTP query string
Packit 228f82
 *
Packit 228f82
 * Check if this query string contains a proper SAML2 signature for this key.
Packit 228f82
 *
Packit 228f82
 * Return value: 0 if a valid signature was found, an error code otherwise.
Packit 228f82
 */
Packit 228f82
lasso_error_t
Packit 228f82
lasso_key_query_verify(LassoKey *key, const char *query)
Packit 228f82
{
Packit 228f82
	LassoSignatureContext signature_context;
Packit 228f82
	lasso_bad_param(KEY, key);
Packit 228f82
Packit 228f82
	signature_context = lasso_key_get_signature_context(key);
Packit 228f82
	if (! lasso_validate_signature_context(signature_context))
Packit 228f82
		return LASSO_ERROR_UNDEFINED;
Packit 228f82
	return lasso_saml2_query_verify_signature(query, signature_context.signature_key);
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_query_verify:
Packit 228f82
 * key: a #LassoKey object
Packit 228f82
 * query: a raw HTTP query string
Packit 228f82
 *
Packit 228f82
 * Sign the given query string using the given key.
Packit 228f82
 *
Packit 228f82
 * Return value: the signed query string.
Packit 228f82
 */
Packit 228f82
char*
Packit 228f82
lasso_key_query_sign(LassoKey *key, const char *query)
Packit 228f82
{
Packit 228f82
	LassoSignatureContext signature_context;
Packit 228f82
Packit 228f82
	if (! LASSO_IS_KEY(key))
Packit 228f82
		return NULL;
Packit 228f82
	signature_context = lasso_key_get_signature_context(key);
Packit 228f82
	if (! lasso_validate_signature_context(signature_context))
Packit 228f82
		return NULL;
Packit 228f82
	return lasso_query_sign((char*)query, signature_context);
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_get_signature_context:
Packit 228f82
 * @key: a #LassoKey object
Packit 228f82
 *
Packit 228f82
 * Private method to extract the signature context embedded in a LassoKey object.
Packit 228f82
 *
Packit 228f82
 * Return value: a #LassoSignatureContext structure value.
Packit 228f82
 */
Packit 228f82
LassoSignatureContext
Packit 228f82
lasso_key_get_signature_context(LassoKey *key) {
Packit 228f82
	if (key->private_data && key->private_data->type == LASSO_KEY_TYPE_FOR_SIGNATURE) {
Packit 228f82
		return key->private_data->context.signature;
Packit 228f82
	}
Packit 228f82
	return LASSO_SIGNATURE_CONTEXT_NONE;
Packit 228f82
}
Packit 228f82
Packit 228f82
/**
Packit 228f82
 * lasso_key_get_key_type:
Packit 228f82
 * @key: a #LassoKey object
Packit 228f82
 *
Packit 228f82
 * Return the type of key, i.e. which operation it supports.
Packit 228f82
 */
Packit 228f82
LassoKeyType
Packit 228f82
lasso_key_get_key_type(LassoKey *key) {
Packit 228f82
	lasso_return_val_if_fail(LASSO_IS_KEY(key),
Packit 228f82
			LASSO_KEY_TYPE_FOR_SIGNATURE);
Packit 228f82
	return key->private_data->type;
Packit 228f82
}