Branch data Line data Source code
1 : : /* server.c --- SAML20 mechanism, server side.
2 : : * Copyright (C) 2010-2012 Simon Josefsson
3 : : *
4 : : * This file is part of GNU SASL Library.
5 : : *
6 : : * GNU SASL Library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public License
8 : : * as published by the Free Software Foundation; either version 2.1 of
9 : : * the License, or (at your option) any later version.
10 : : *
11 : : * GNU SASL Library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with GNU SASL Library; if not, write to the Free
18 : : * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 : : * Boston, MA 02110-1301, USA.
20 : : *
21 : : */
22 : :
23 : : #ifdef HAVE_CONFIG_H
24 : : #include "config.h"
25 : : #endif
26 : :
27 : : /* Get specification. */
28 : : #include "saml20.h"
29 : :
30 : : /* Get strdup, strlen. */
31 : : #include <string.h>
32 : :
33 : : /* Get free. */
34 : : #include <stdlib.h>
35 : :
36 : : /* Get _gsasl_parse_gs2_header. */
37 : : #include "mechtools.h"
38 : :
39 : : struct saml20_server_state
40 : : {
41 : : int step;
42 : : };
43 : :
44 : : int
45 : 9 : _gsasl_saml20_server_start (Gsasl_session * sctx, void **mech_data)
46 : : {
47 : : struct saml20_server_state *state;
48 : :
49 : 9 : state = (struct saml20_server_state *) calloc (sizeof (*state), 1);
50 [ - + ]: 9 : if (state == NULL)
51 : 0 : return GSASL_MALLOC_ERROR;
52 : :
53 : 9 : *mech_data = state;
54 : :
55 : 9 : return GSASL_OK;
56 : : }
57 : :
58 : : int
59 : 7 : _gsasl_saml20_server_step (Gsasl_session * sctx,
60 : : void *mech_data,
61 : : const char *input, size_t input_len,
62 : : char **output, size_t * output_len)
63 : : {
64 : 7 : struct saml20_server_state *state = mech_data;
65 : 7 : int res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
66 : :
67 : 7 : *output_len = 0;
68 : 7 : *output = NULL;
69 : :
70 [ + + - ]: 7 : switch (state->step)
71 : : {
72 : : case 0:
73 : : {
74 : : const char *p;
75 : : char *authzid;
76 : : size_t headerlen;
77 : :
78 [ + + ]: 6 : if (input_len == 0)
79 : 1 : return GSASL_NEEDS_MORE;
80 : :
81 : 5 : res = _gsasl_parse_gs2_header (input, input_len,
82 : : &authzid, &headerlen);
83 [ + + ]: 5 : if (res != GSASL_OK)
84 : 4 : return res;
85 : :
86 [ - + ]: 1 : if (authzid)
87 : : {
88 : 0 : gsasl_property_set (sctx, GSASL_AUTHZID, authzid);
89 : 0 : free (authzid);
90 : : }
91 : :
92 : 1 : input += headerlen;
93 : 1 : input_len -= headerlen;
94 : :
95 : 1 : gsasl_property_set_raw (sctx, GSASL_SAML20_IDP_IDENTIFIER,
96 : : input, input_len);
97 : :
98 : 1 : p = gsasl_property_get (sctx, GSASL_SAML20_REDIRECT_URL);
99 [ + - ][ - + ]: 1 : if (!p || !*p)
100 : 0 : return GSASL_NO_SAML20_REDIRECT_URL;
101 : :
102 : 1 : *output_len = strlen (p);
103 : 1 : *output = malloc (*output_len);
104 [ - + ]: 1 : if (!*output)
105 : 0 : return GSASL_MALLOC_ERROR;
106 : :
107 : 1 : memcpy (*output, p, *output_len);
108 : :
109 : 1 : res = GSASL_NEEDS_MORE;
110 : 1 : state->step++;
111 : 1 : break;
112 : : }
113 : :
114 : : case 1:
115 : : {
116 [ + - ][ - + ]: 1 : if (!(input_len == 1 && *input == '='))
117 : 0 : return GSASL_MECHANISM_PARSE_ERROR;
118 : :
119 : 1 : res = gsasl_callback (NULL, sctx, GSASL_VALIDATE_SAML20);
120 [ - + ]: 1 : if (res != GSASL_OK)
121 : 0 : return res;
122 : :
123 : 1 : *output = NULL;
124 : 1 : *output_len = 0;
125 : :
126 : 1 : res = GSASL_OK;
127 : 1 : state->step++;
128 : 1 : break;
129 : : }
130 : :
131 : : default:
132 : 0 : break;
133 : : }
134 : :
135 : 7 : return res;
136 : : }
137 : :
138 : : void
139 : 9 : _gsasl_saml20_server_finish (Gsasl_session * sctx, void *mech_data)
140 : : {
141 : 9 : struct saml20_server_state *state = mech_data;
142 : :
143 [ - + ]: 9 : if (!state)
144 : 9 : return;
145 : :
146 : 9 : free (state);
147 : : }
|