1 /* $Id$
2  *
3  * Lasso - A free implementation of the Liberty Alliance specifications.
4  *
5  * Copyright (C) 2004-2007 Entr'ouvert
6  * http://lasso.entrouvert.org
7  *
8  * Authors: See AUTHORS file in top-level directory.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 
25 #include "../../utils.h"
26 #include "../private.h"
27 #include <xmlsec/xmldsig.h>
28 #include <xmlsec/templates.h>
29 #include <xmlsec/xmltree.h>
30 
31 #include "saml2_assertion.h"
32 
33 /**
34  * SECTION:saml2_assertion
35  * @short_description: &lt;saml2:Assertion&gt;
36  *
37  * <figure><title>Schema fragment for saml2:Assertion</title>
38  * <programlisting><![CDATA[
39  *
40  * <complexType name="AssertionType">
41  *   <sequence>
42  *     <element ref="saml:Issuer"/>
43  *     <element ref="ds:Signature" minOccurs="0"/>
44  *     <element ref="saml:Subject" minOccurs="0"/>
45  *     <element ref="saml:Conditions" minOccurs="0"/>
46  *     <element ref="saml:Advice" minOccurs="0"/>
47  *     <choice minOccurs="0" maxOccurs="unbounded">
48  *       <element ref="saml:Statement"/>
49  *       <element ref="saml:AuthnStatement"/>
50  *       <element ref="saml:AuthzDecisionStatement"/>
51  *       <element ref="saml:AttributeStatement"/>
52  *     </choice>
53  *   </sequence>
54  *   <attribute name="Version" type="string" use="required"/>
55  *   <attribute name="ID" type="ID" use="required"/>
56  *   <attribute name="IssueInstant" type="dateTime" use="required"/>
57  * </complexType>
58  * ]]></programlisting>
59  * </figure>
60  */
61 
62 /*****************************************************************************/
63 /* private methods                                                           */
64 /*****************************************************************************/
65 
66 
67 static struct XmlSnippet schema_snippets[] = {
68 	{ "Issuer", SNIPPET_NODE,
69 		G_STRUCT_OFFSET(LassoSaml2Assertion, Issuer),
70 		"LassoSaml2NameID", NULL, NULL},
71 	{ "Signature", SNIPPET_SIGNATURE,
72 		G_STRUCT_OFFSET(LassoSaml2Assertion, ID), NULL, LASSO_DS_PREFIX, LASSO_DS_HREF},
73 	{ "Subject", SNIPPET_NODE,
74 		G_STRUCT_OFFSET(LassoSaml2Assertion, Subject), NULL, NULL, NULL},
75 	{ "Conditions", SNIPPET_NODE,
76 		G_STRUCT_OFFSET(LassoSaml2Assertion, Conditions), NULL, NULL, NULL},
77 	{ "Advice", SNIPPET_NODE,
78 		G_STRUCT_OFFSET(LassoSaml2Assertion, Advice), NULL, NULL, NULL},
79 	{ "Statement", SNIPPET_LIST_NODES,
80 		G_STRUCT_OFFSET(LassoSaml2Assertion, Statement), NULL, NULL, NULL},
81 	{ "AuthnStatement", SNIPPET_LIST_NODES | SNIPPET_JUMP_ON_MATCH | SNIPPET_BACK_1,
82 		G_STRUCT_OFFSET(LassoSaml2Assertion, AuthnStatement), NULL, NULL, NULL},
83 	{ "AuthzDecisionStatement", SNIPPET_LIST_NODES | SNIPPET_JUMP_ON_MATCH | SNIPPET_BACK_2,
84 		G_STRUCT_OFFSET(LassoSaml2Assertion, AuthzDecisionStatement), NULL, NULL, NULL},
85 	{ "AttributeStatement", SNIPPET_LIST_NODES | SNIPPET_JUMP_ON_MATCH | SNIPPET_BACK_3,
86 		G_STRUCT_OFFSET(LassoSaml2Assertion, AttributeStatement), NULL, NULL, NULL},
87 	{ "Version", SNIPPET_ATTRIBUTE,
88 		G_STRUCT_OFFSET(LassoSaml2Assertion, Version), NULL, NULL, NULL},
89 	{ "ID", SNIPPET_ATTRIBUTE,
90 		G_STRUCT_OFFSET(LassoSaml2Assertion, ID), NULL, NULL, NULL},
91 	{ "IssueInstant", SNIPPET_ATTRIBUTE,
92 		G_STRUCT_OFFSET(LassoSaml2Assertion, IssueInstant), NULL, NULL, NULL},
93 
94 	/* hidden fields; used in lasso dumps */
95 	{ "SignType", SNIPPET_ATTRIBUTE | SNIPPET_INTEGER | SNIPPET_LASSO_DUMP,
96 		G_STRUCT_OFFSET(LassoSaml2Assertion, sign_type), NULL, NULL, NULL},
97 	{ "SignMethod", SNIPPET_ATTRIBUTE | SNIPPET_INTEGER | SNIPPET_LASSO_DUMP,
98 		G_STRUCT_OFFSET(LassoSaml2Assertion, sign_method), NULL, NULL, NULL},
99 	{ "PrivateKeyFile", SNIPPET_CONTENT | SNIPPET_LASSO_DUMP,
100 		G_STRUCT_OFFSET(LassoSaml2Assertion, private_key_file), NULL, NULL, NULL},
101 	{ "CertificateFile", SNIPPET_CONTENT | SNIPPET_LASSO_DUMP,
102 		G_STRUCT_OFFSET(LassoSaml2Assertion, certificate_file), NULL, NULL, NULL},
103 	{ "EncryptionActivated", SNIPPET_ATTRIBUTE | SNIPPET_BOOLEAN | SNIPPET_LASSO_DUMP,
104 		G_STRUCT_OFFSET(LassoSaml2Assertion, encryption_activated), NULL, NULL, NULL},
105 	{ "EncryptionPublicKeyStr", SNIPPET_CONTENT | SNIPPET_LASSO_DUMP,
106 		G_STRUCT_OFFSET(LassoSaml2Assertion, encryption_public_key_str), NULL, NULL, NULL},
107 	{ "EncryptionSymKeyType", SNIPPET_ATTRIBUTE | SNIPPET_INTEGER | SNIPPET_LASSO_DUMP,
108 		G_STRUCT_OFFSET(LassoSaml2Assertion, encryption_sym_key_type), NULL, NULL, NULL},
109 
110 	{NULL, 0, 0, NULL, NULL, NULL}
111 };
112 
113 static LassoNodeClass *parent_class = NULL;
114 
115 static xmlNode*
get_xmlNode(LassoNode * node,gboolean lasso_dump)116 get_xmlNode(LassoNode *node, gboolean lasso_dump)
117 {
118 	xmlNode *xmlnode, *original_xmlnode;
119 
120 	/* If assertion has been deserialized and contain a signature, dump it from the
121 	 * original xmlnode. */
122 	original_xmlnode = lasso_node_get_original_xmlnode(node);
123 	while (original_xmlnode) {
124 		xmlNode *signature, *signature_value;
125 
126 		signature = xmlSecFindChild(original_xmlnode, xmlSecNodeSignature, xmlSecDSigNs);
127 		if (! signature)
128 			break;
129 		signature_value = xmlSecFindNode(signature, xmlSecNodeSignatureValue, xmlSecDSigNs);
130 		if (signature_value && signature_value->children) {
131 			return xmlCopyNode(original_xmlnode, 1);
132 		}
133 		break;
134 	}
135 
136 	xmlnode = parent_class->get_xmlNode(node, lasso_dump);
137 
138 	return xmlnode;
139 }
140 
141 
142 /*****************************************************************************/
143 /* instance and class init functions                                         */
144 /*****************************************************************************/
145 
146 static void
instance_init(LassoSaml2Assertion * node)147 instance_init(LassoSaml2Assertion *node)
148 {
149 	node->sign_type = LASSO_SIGNATURE_TYPE_NONE;
150 	node->encryption_sym_key_type = LASSO_ENCRYPTION_SYM_KEY_TYPE_DEFAULT;
151 }
152 
153 static void
class_init(LassoSaml2AssertionClass * klass,void * unused G_GNUC_UNUSED)154 class_init(LassoSaml2AssertionClass *klass, void *unused G_GNUC_UNUSED)
155 {
156 	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
157 
158 	parent_class = g_type_class_peek_parent(klass);
159 	nclass->get_xmlNode = get_xmlNode;
160 	nclass->node_data = g_new0(LassoNodeClassData, 1);
161 	lasso_node_class_set_nodename(nclass, "Assertion");
162 	lasso_node_class_set_ns(nclass,LASSO_SAML2_ASSERTION_HREF, LASSO_SAML2_ASSERTION_PREFIX);
163 	lasso_node_class_add_snippets(nclass, schema_snippets);
164 
165 	nclass->node_data->id_attribute_name = "ID";
166 	nclass->node_data->id_attribute_offset = G_STRUCT_OFFSET(LassoSaml2Assertion, ID);
167 	nclass->node_data->sign_type_offset = G_STRUCT_OFFSET(
168 			LassoSaml2Assertion, sign_type);
169 	nclass->node_data->sign_method_offset = G_STRUCT_OFFSET(
170 			LassoSaml2Assertion, sign_method);
171 	nclass->node_data->private_key_file_offset = G_STRUCT_OFFSET(LassoSaml2Assertion,
172 			private_key_file);
173 	nclass->node_data->certificate_file_offset = G_STRUCT_OFFSET(LassoSaml2Assertion,
174 			certificate_file);
175 	nclass->node_data->keep_xmlnode = TRUE;
176 }
177 
178 GType
lasso_saml2_assertion_get_type()179 lasso_saml2_assertion_get_type()
180 {
181 	static GType this_type = 0;
182 
183 	if (!this_type) {
184 		static const GTypeInfo this_info = {
185 			sizeof (LassoSaml2AssertionClass),
186 			NULL,
187 			NULL,
188 			(GClassInitFunc) class_init,
189 			NULL,
190 			NULL,
191 			sizeof(LassoSaml2Assertion),
192 			0,
193 			(GInstanceInitFunc) instance_init,
194 			NULL
195 		};
196 
197 		this_type = g_type_register_static(LASSO_TYPE_NODE,
198 				"LassoSaml2Assertion", &this_info, 0);
199 	}
200 	return this_type;
201 }
202 
203 /**
204  * lasso_saml2_assertion_new:
205  *
206  * Creates a new #LassoSaml2Assertion object.
207  *
208  * Return value: a newly created #LassoSaml2Assertion object
209  **/
210 LassoNode*
lasso_saml2_assertion_new()211 lasso_saml2_assertion_new()
212 {
213 	return g_object_new(LASSO_TYPE_SAML2_ASSERTION, NULL);
214 }
215