gsasl  1.10.0
About: GNU SASL is an implementation of the Simple Authentication and Security Layer (SASL). Development version.
  Fossies Dox: gsasl-1.10.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

client.c
Go to the documentation of this file.
1 /* client.c --- SASL mechanism SECURID from RFC 2808, client side.
2  * Copyright (C) 2002-2021 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 "securid.h"
29 
30 /* Get malloc, free. */
31 #include <stdlib.h>
32 
33 /* Get strdup, strlen. */
34 #include <string.h>
35 
36 #define PASSCODE "passcode"
37 #define PIN "pin"
38 
39 int
41  void **mech_data)
42 {
43  int *step;
44 
45  step = (int *) malloc (sizeof (*step));
46  if (step == NULL)
47  return GSASL_MALLOC_ERROR;
48 
49  *step = 0;
50 
51  *mech_data = step;
52 
53  return GSASL_OK;
54 }
55 
56 int
58  void *mech_data,
59  const char *input,
60  size_t input_len,
61  char **output, size_t *output_len)
62 {
63  int *step = mech_data;
64  const char *authzid = NULL, *authid = NULL, *passcode = NULL, *pin = NULL;
65  size_t authzidlen, authidlen, passcodelen, pinlen = 0;
66  int do_pin = 0;
67  int res;
68 
69  switch (*step)
70  {
71  case 1:
72  if (input_len == strlen (PASSCODE) &&
73  memcmp (input, PASSCODE, strlen (PASSCODE)) == 0)
74  {
75  *step = 0;
76  }
77  else if (input_len >= strlen (PIN) &&
78  memcmp (input, PIN, strlen (PIN)) == 0)
79  {
80  do_pin = 1;
81  *step = 0;
82  }
83  else
84  {
85  *output_len = 0;
86  res = GSASL_OK;
87  break;
88  }
89  /* fall through */
90 
91  case 0:
92  authzid = gsasl_property_get (sctx, GSASL_AUTHZID);
93  if (authzid)
94  authzidlen = strlen (authzid);
95  else
96  authzidlen = 0;
97 
98  authid = gsasl_property_get (sctx, GSASL_AUTHID);
99  if (!authid)
100  return GSASL_NO_AUTHID;
101  authidlen = strlen (authid);
102 
103  passcode = gsasl_property_get (sctx, GSASL_PASSCODE);
104  if (!passcode)
105  return GSASL_NO_PASSCODE;
106  passcodelen = strlen (passcode);
107 
108  if (do_pin)
109  {
110  if (input_len > strlen (PIN))
112  &input[strlen (PIN)],
113  input_len - strlen (PIN));
114 
115  pin = gsasl_property_get (sctx, GSASL_PIN);
116  if (!pin)
117  return GSASL_NO_PIN;
118  pinlen = strlen (pin);
119  }
120 
121  *output_len = authzidlen + 1 + authidlen + 1 + passcodelen + 1;
122  if (do_pin)
123  *output_len += pinlen + 1;
124  *output = malloc (*output_len);
125  if (*output == NULL)
126  return GSASL_MALLOC_ERROR;
127 
128  if (authzid)
129  memcpy (*output, authzid, authzidlen);
130  (*output)[authzidlen] = '\0';
131  memcpy (*output + authzidlen + 1, authid, authidlen);
132  (*output)[authzidlen + 1 + authidlen] = '\0';
133  memcpy (*output + authzidlen + 1 + authidlen + 1, passcode,
134  passcodelen);
135  (*output)[authzidlen + 1 + authidlen + 1 + passcodelen] = '\0';
136  if (do_pin)
137  {
138  memcpy (*output + authzidlen + 1 + authidlen + 1 + passcodelen + 1,
139  pin, pinlen);
140  (*output)[authzidlen + 1 + authidlen + 1 + passcodelen + 1 +
141  pinlen] = '\0';
142  }
143 
144  (*step)++;
145  res = GSASL_OK;
146  break;
147 
148  case 2:
149  *output_len = 0;
150  *output = NULL;
151  (*step)++;
152  res = GSASL_OK;
153  break;
154 
155  default:
157  break;
158  }
159 
160  return res;
161 }
162 
163 void
165  void *mech_data)
166 {
167  int *step = mech_data;
168 
169  free (step);
170 }
#define NULL
Definition: stddef.in.h:72
const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:263
@ GSASL_OK
Definition: gsasl.h:171
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:175
@ GSASL_NO_PASSCODE
Definition: gsasl.h:189
@ GSASL_MECHANISM_CALLED_TOO_MANY_TIMES
Definition: gsasl.h:174
@ GSASL_NO_AUTHID
Definition: gsasl.h:186
@ GSASL_NO_PIN
Definition: gsasl.h:190
void gsasl_property_set_raw(Gsasl_session *sctx, Gsasl_property prop, const char *data, size_t len)
Definition: property.c:184
@ GSASL_AUTHZID
Definition: gsasl.h:336
@ GSASL_PASSCODE
Definition: gsasl.h:342
@ GSASL_AUTHID
Definition: gsasl.h:335
@ GSASL_PIN
Definition: gsasl.h:344
@ GSASL_SUGGESTED_PIN
Definition: gsasl.h:343
int res
Definition: mbrtowc-impl.h:45
int _gsasl_securid_client_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
Definition: client.c:40
int _gsasl_securid_client_step(Gsasl_session *sctx, void *mech_data, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: client.c:57
void _gsasl_securid_client_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
Definition: client.c:164
#define PASSCODE
Definition: client.c:36
#define PIN
Definition: client.c:37