~ubuntu-branches/ubuntu/utopic/lasso/utopic-proposed

« back to all changes in this revision

Viewing changes to lasso/protocols/artifact.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2004-09-13 09:26:34 UTC
  • Revision ID: james.westby@ubuntu.com-20040913092634-01vdfl8j9cp94exa
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: artifact.c,v 1.16 2004/09/01 09:59:53 fpeters Exp $ 
 
2
 *
 
3
 * Lasso - A free implementation of the Liberty Alliance specifications.
 
4
 *
 
5
 * Copyright (C) 2004 Entr'ouvert
 
6
 * http://lasso.entrouvert.org
 
7
 * 
 
8
 * Authors: Valery Febvre   <vfebvre@easter-eggs.com>
 
9
 *          Nicolas Clapies <nclapies@entrouvert.com>
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2 of the License, or
 
14
 * (at your option) any later version.
 
15
 * 
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 * 
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
 */
 
25
 
 
26
#include <lasso/xml/errors.h>
 
27
#include <lasso/protocols/artifact.h>
 
28
#include <xmlsec/base64.h>
 
29
 
 
30
/*****************************************************************************/
 
31
/* functions                                                                 */
 
32
/*****************************************************************************/
 
33
 
 
34
static gint
 
35
lasso_artifact_split_samlArt(gchar *b64_samlArt,
 
36
                             gchar *byteCode,
 
37
                             gchar *identityProviderSuccinctID,
 
38
                             gchar *assertionHandle)
 
39
{
 
40
  xmlSecByte samlArt[42+1];
 
41
  gint i, j, byte_code = 0;
 
42
 
 
43
  /* decode samlArt */
 
44
  i = xmlSecBase64Decode((const xmlChar *)b64_samlArt, samlArt, 42+1);
 
45
  if (i < 0 || i > 42) {
 
46
    return -1;
 
47
  }
 
48
  /* extract ByteCode, IdentityProviderSuccinctID and AssertionHandle */
 
49
  for(j=0; j<42; j++) {
 
50
    if (j < 2) {
 
51
      byte_code += (guint)samlArt[j];
 
52
    }
 
53
    else if (j >= 2 && j < 22) {
 
54
      identityProviderSuccinctID[j-2] = samlArt[j];
 
55
    }
 
56
    else if (j >= 22) {
 
57
      assertionHandle[j-22] = samlArt[j];
 
58
    }
 
59
  }
 
60
  sprintf(byteCode, "%d", byte_code);
 
61
 
 
62
  return 0;
 
63
}
 
64
 
 
65
/*****************************************************************************/
 
66
/* public methods                                                            */
 
67
/*****************************************************************************/
 
68
 
 
69
xmlChar*
 
70
lasso_artifact_get_assertionHandle(LassoArtifact  *artifact,
 
71
                                   GError        **err)
 
72
{
 
73
  xmlChar *assertionHandle;
 
74
  GError *tmp_err = NULL;
 
75
 
 
76
  if (err != NULL && *err != NULL) {
 
77
    g_set_error(err, g_quark_from_string("Lasso"),
 
78
                LASSO_PARAM_ERROR_CHECK_FAILED,
 
79
                lasso_strerror(LASSO_PARAM_ERROR_CHECK_FAILED));
 
80
    g_return_val_if_fail (err == NULL || *err == NULL, NULL);
 
81
  }
 
82
  if (LASSO_IS_ARTIFACT(artifact) == FALSE) {
 
83
    g_set_error(err, g_quark_from_string("Lasso"),
 
84
                LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ,
 
85
                lasso_strerror(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ));
 
86
    g_return_val_if_fail(LASSO_IS_ARTIFACT(artifact), NULL);
 
87
  }
 
88
 
 
89
  assertionHandle = lasso_node_get_child_content(LASSO_NODE(artifact),
 
90
                                                 "AssertionHandle",
 
91
                                                 NULL, &tmp_err);
 
92
  if (assertionHandle == NULL) {
 
93
    g_propagate_error (err, tmp_err);
 
94
  }
 
95
 
 
96
  return assertionHandle;
 
97
}
 
98
 
 
99
/**
 
100
 * lasso_artifact_get_byteCode:
 
101
 * @artifact: an LassoArtifact
 
102
 * @err: return location for an allocated GError, or NULL to ignore errors
 
103
 * 
 
104
 * Gets the ByteCode of the artifact
 
105
 * 
 
106
 * Return value: the ByteCode or a negative integer if an error occurs.
 
107
 **/
 
108
gint
 
109
lasso_artifact_get_byteCode(LassoArtifact  *artifact,
 
110
                            GError        **err)
 
111
{
 
112
  xmlChar *byteCode;
 
113
  gint ret;
 
114
  GError *tmp_err = NULL;
 
115
 
 
116
  if (err != NULL && *err != NULL) {
 
117
    g_set_error(err, g_quark_from_string("Lasso"),
 
118
                LASSO_PARAM_ERROR_CHECK_FAILED,
 
119
                lasso_strerror(LASSO_PARAM_ERROR_CHECK_FAILED));
 
120
    g_return_val_if_fail (err == NULL || *err == NULL,
 
121
                          LASSO_PARAM_ERROR_CHECK_FAILED);
 
122
  }
 
123
  if (LASSO_IS_ARTIFACT(artifact) == FALSE) {
 
124
    g_set_error(err, g_quark_from_string("Lasso"),
 
125
                LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ,
 
126
                lasso_strerror(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ));
 
127
    g_return_val_if_fail(LASSO_IS_ARTIFACT(artifact),
 
128
                         LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ);
 
129
  }
 
130
 
 
131
  byteCode = lasso_node_get_child_content(LASSO_NODE(artifact),
 
132
                                          "ByteCode", NULL, &tmp_err);
 
133
  if (byteCode == NULL) {
 
134
    g_propagate_error (err, tmp_err);
 
135
    return -1;
 
136
  }
 
137
 
 
138
  ret = (gint)g_strtod(byteCode, NULL);
 
139
  xmlFree(byteCode);
 
140
 
 
141
  return ret;
 
142
}
 
143
 
 
144
xmlChar*
 
145
lasso_artifact_get_b64IdentityProviderSuccinctID(LassoArtifact  *artifact,
 
146
                                                 GError        **err)
 
147
{
 
148
  xmlChar *b64_identityProviderSuccinctID;
 
149
  GError *tmp_err = NULL;
 
150
 
 
151
  if (err != NULL && *err != NULL) {
 
152
    g_set_error(err, g_quark_from_string("Lasso"),
 
153
                LASSO_PARAM_ERROR_CHECK_FAILED,
 
154
                lasso_strerror(LASSO_PARAM_ERROR_CHECK_FAILED));
 
155
    g_return_val_if_fail (err == NULL || *err == NULL, NULL);
 
156
  }
 
157
  if (LASSO_IS_ARTIFACT(artifact) == FALSE) {
 
158
    g_set_error(err, g_quark_from_string("Lasso"),
 
159
                LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ,
 
160
                lasso_strerror(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ));
 
161
    g_return_val_if_fail(LASSO_IS_ARTIFACT(artifact), NULL);
 
162
  }
 
163
 
 
164
  b64_identityProviderSuccinctID = lasso_node_get_child_content(LASSO_NODE(artifact),
 
165
                                                                "B64IdentityProviderSuccinctID",
 
166
                                                                NULL, &tmp_err);
 
167
  if (b64_identityProviderSuccinctID == NULL) {
 
168
    g_propagate_error (err, tmp_err);
 
169
  }
 
170
 
 
171
  return b64_identityProviderSuccinctID;
 
172
}
 
173
 
 
174
xmlChar*
 
175
lasso_artifact_get_relayState(LassoArtifact  *artifact,
 
176
                              GError        **err)
 
177
{
 
178
  xmlChar *relayState;
 
179
  GError *tmp_err = NULL;
 
180
 
 
181
  if (err != NULL && *err != NULL) {
 
182
    g_set_error(err, g_quark_from_string("Lasso"),
 
183
                LASSO_PARAM_ERROR_CHECK_FAILED,
 
184
                lasso_strerror(LASSO_PARAM_ERROR_CHECK_FAILED));
 
185
    g_return_val_if_fail (err == NULL || *err == NULL, NULL);
 
186
  }
 
187
  if (LASSO_IS_ARTIFACT(artifact) == FALSE) {
 
188
    g_set_error(err, g_quark_from_string("Lasso"),
 
189
                LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ,
 
190
                lasso_strerror(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ));
 
191
    g_return_val_if_fail(LASSO_IS_ARTIFACT(artifact), NULL);
 
192
  }
 
193
 
 
194
  relayState = lasso_node_get_child_content(LASSO_NODE(artifact),
 
195
                                            "RelayState", NULL, &tmp_err);
 
196
  if (relayState == NULL) {
 
197
    g_propagate_error (err, tmp_err);
 
198
  }
 
199
 
 
200
  return relayState;
 
201
}
 
202
 
 
203
xmlChar*
 
204
lasso_artifact_get_samlArt(LassoArtifact  *artifact,
 
205
                           GError        **err)
 
206
{
 
207
  xmlChar *samlArt;
 
208
  GError *tmp_err = NULL;
 
209
 
 
210
  if (err != NULL && *err != NULL) {
 
211
    g_set_error(err, g_quark_from_string("Lasso"),
 
212
                LASSO_PARAM_ERROR_CHECK_FAILED,
 
213
                lasso_strerror(LASSO_PARAM_ERROR_CHECK_FAILED));
 
214
    g_return_val_if_fail (err == NULL || *err == NULL, NULL);
 
215
  }
 
216
  if (LASSO_IS_ARTIFACT(artifact) == FALSE) {
 
217
    g_set_error(err, g_quark_from_string("Lasso"),
 
218
                LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ,
 
219
                lasso_strerror(LASSO_PARAM_ERROR_BAD_TYPE_OR_NULL_OBJ));
 
220
    g_return_val_if_fail(LASSO_IS_ARTIFACT(artifact), NULL);
 
221
  }
 
222
 
 
223
  samlArt = lasso_node_get_child_content(LASSO_NODE(artifact),
 
224
                                         "SAMLart", NULL, &tmp_err);
 
225
  if (samlArt == NULL) {
 
226
    g_propagate_error (err, tmp_err);
 
227
  }
 
228
 
 
229
  return samlArt;
 
230
}
 
231
 
 
232
/*****************************************************************************/
 
233
/* instance and class init functions                                         */
 
234
/*****************************************************************************/
 
235
 
 
236
static void
 
237
lasso_artifact_instance_init(LassoArtifact *artifact)
 
238
{
 
239
  LassoNodeClass *class = LASSO_NODE_GET_CLASS(LASSO_NODE(artifact));
 
240
 
 
241
  class->set_name(LASSO_NODE(artifact), "Artifact");
 
242
}
 
243
 
 
244
static void
 
245
lasso_artifact_class_init(LassoArtifactClass *class)
 
246
{
 
247
}
 
248
 
 
249
GType lasso_artifact_get_type() {
 
250
  static GType this_type = 0;
 
251
 
 
252
  if (!this_type) {
 
253
    static const GTypeInfo this_info = {
 
254
      sizeof (LassoArtifactClass),
 
255
      NULL,
 
256
      NULL,
 
257
      (GClassInitFunc) lasso_artifact_class_init,
 
258
      NULL,
 
259
      NULL,
 
260
      sizeof(LassoArtifact),
 
261
      0,
 
262
      (GInstanceInitFunc) lasso_artifact_instance_init,
 
263
    };
 
264
    
 
265
    this_type = g_type_register_static(LASSO_TYPE_NODE,
 
266
                                       "LassoArtifact",
 
267
                                       &this_info, 0);
 
268
  }
 
269
  return this_type;
 
270
}
 
271
 
 
272
LassoNode*
 
273
lasso_artifact_new(gchar *samlArt,
 
274
                   gchar *byteCode,
 
275
                   gchar *identityProviderSuccinctID,
 
276
                   gchar *assertionHandle,
 
277
                   gchar *relayState)
 
278
{
 
279
  LassoNode *artifact;
 
280
  LassoNodeClass *class;
 
281
  xmlChar *b64_identityProviderSuccinctID;
 
282
 
 
283
  g_return_val_if_fail(byteCode != NULL, NULL);
 
284
  g_return_val_if_fail(identityProviderSuccinctID != NULL, NULL);
 
285
  g_return_val_if_fail(assertionHandle != NULL, NULL);
 
286
 
 
287
  artifact = LASSO_NODE(g_object_new(LASSO_TYPE_ARTIFACT, NULL));
 
288
 
 
289
  class = LASSO_NODE_GET_CLASS(artifact);
 
290
  class->new_child(artifact, "SAMLart", samlArt, FALSE);
 
291
  class->new_child(artifact, "ByteCode", byteCode, FALSE);
 
292
  b64_identityProviderSuccinctID = xmlSecBase64Encode((const xmlSecByte *)identityProviderSuccinctID,
 
293
                                                      20, 0);
 
294
  class->new_child(artifact, "B64IdentityProviderSuccinctID",
 
295
                   b64_identityProviderSuccinctID, FALSE);
 
296
  xmlFree(b64_identityProviderSuccinctID);
 
297
  class->new_child(artifact, "AssertionHandle", assertionHandle, FALSE);
 
298
  if (relayState != NULL) {
 
299
    class->new_child(artifact, "RelayState", relayState, FALSE);
 
300
  }
 
301
 
 
302
  return artifact;
 
303
}
 
304
 
 
305
LassoNode*
 
306
lasso_artifact_new_from_query(const xmlChar *query)
 
307
{
 
308
  LassoNode *artifact = NULL;
 
309
  GData *gd;
 
310
  gchar *b64_samlArt, *relayState;
 
311
  gchar *byteCode, *identityProviderSuccinctID, *assertionHandle;
 
312
  gint ret;
 
313
 
 
314
  g_return_val_if_fail(query != NULL, NULL);
 
315
 
 
316
 
 
317
  gd = lasso_query_to_dict(query);
 
318
  b64_samlArt = g_strdup(lasso_g_ptr_array_index((GPtrArray *)g_datalist_get_data(&gd, "SAMLart"), 0));
 
319
  relayState  = g_strdup(lasso_g_ptr_array_index((GPtrArray *)g_datalist_get_data(&gd, "RelayState"), 0));
 
320
  g_datalist_clear(&gd);
 
321
 
 
322
  byteCode = (gchar *) g_new0(gchar, 5+1);
 
323
  identityProviderSuccinctID = (gchar *) g_new0(gchar, 21);
 
324
  assertionHandle = (gchar *) g_new0(gchar, 20+1);
 
325
  ret = lasso_artifact_split_samlArt((gchar*)b64_samlArt, byteCode,
 
326
                                     identityProviderSuccinctID,
 
327
                                     assertionHandle);
 
328
 
 
329
  /* printf("b64_samlArt = %s\n", b64_samlArt); */
 
330
  /* printf("relayState = %s\n", relayState); */
 
331
  /* printf("byteCode = %s\n", byteCode); */
 
332
  /* printf("identityProviderSuccinctID = %s\n", identityProviderSuccinctID); */
 
333
  /* printf("assertionHandle = %s\n", assertionHandle); */
 
334
 
 
335
  if (ret >= 0) {
 
336
    artifact = lasso_artifact_new(b64_samlArt,
 
337
                                  byteCode, identityProviderSuccinctID,
 
338
                                  assertionHandle,
 
339
                                  relayState);
 
340
  }
 
341
  g_free(byteCode);
 
342
  g_free(identityProviderSuccinctID);
 
343
  g_free(assertionHandle);
 
344
  g_free(b64_samlArt);
 
345
  g_free(relayState);
 
346
 
 
347
  return artifact;
 
348
}
 
349
 
 
350
LassoNode*
 
351
lasso_artifact_new_from_lares(const xmlChar *lares,
 
352
                              const xmlChar *relayState)
 
353
{
 
354
  LassoNode *artifact = NULL;
 
355
  gchar *byteCode, *identityProviderSuccinctID, *assertionHandle;
 
356
  gint ret;
 
357
 
 
358
  g_return_val_if_fail(lares != NULL, NULL);
 
359
 
 
360
  byteCode = (gchar *) g_new0(gchar, 5+1);
 
361
  identityProviderSuccinctID = (gchar *) g_new0(gchar, 20);
 
362
  assertionHandle = (gchar *) g_new0(gchar, 20+1);
 
363
  ret = lasso_artifact_split_samlArt((gchar*)lares, byteCode,
 
364
                                     identityProviderSuccinctID,
 
365
                                     assertionHandle);
 
366
  if (ret >= 0) {
 
367
    artifact = lasso_artifact_new((gchar*)lares,
 
368
                                  byteCode, identityProviderSuccinctID,
 
369
                                  assertionHandle,
 
370
                                  (gchar*)relayState);
 
371
  }
 
372
  g_free(byteCode);
 
373
  g_free(identityProviderSuccinctID);
 
374
  g_free(assertionHandle);
 
375
 
 
376
  return artifact;
 
377
}