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)  

server.c
Go to the documentation of this file.
1 /* server.c --- SASL CRAM-MD5 server side functions.
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 "cram-md5.h"
29 
30 /* Get malloc, free. */
31 #include <stdlib.h>
32 
33 /* Get memcpy, strdup, strlen. */
34 #include <string.h>
35 
36 /* Get cram_md5_challenge. */
37 #include "challenge.h"
38 
39 /* Get cram_md5_digest. */
40 #include "digest.h"
41 
42 #define MD5LEN 16
43 
44 int
46  void **mech_data)
47 {
48  char *challenge;
49  int rc;
50 
51  challenge = malloc (CRAM_MD5_CHALLENGE_LEN);
52  if (challenge == NULL)
53  return GSASL_MALLOC_ERROR;
54 
55  rc = cram_md5_challenge (challenge);
56  if (rc)
57  return GSASL_CRYPTO_ERROR;
58 
59  *mech_data = challenge;
60 
61  return GSASL_OK;
62 }
63 
64 int
66  void *mech_data,
67  const char *input, size_t input_len,
68  char **output, size_t *output_len)
69 {
70  char *challenge = mech_data;
71  char hash[CRAM_MD5_DIGEST_LEN];
72  const char *password;
73  char *username = NULL;
74  int res = GSASL_OK;
75  char *normkey;
76 
77  if (input_len == 0)
78  {
79  *output_len = strlen (challenge);
80  *output = strdup (challenge);
81 
82  return GSASL_NEEDS_MORE;
83  }
84 
85  if (input_len <= MD5LEN * 2)
87 
88  if (input[input_len - MD5LEN * 2 - 1] != ' ')
90 
91  username = calloc (1, input_len - MD5LEN * 2);
92  if (username == NULL)
93  return GSASL_MALLOC_ERROR;
94 
95  memcpy (username, input, input_len - MD5LEN * 2 - 1);
96 
97  gsasl_property_set (sctx, GSASL_AUTHID, username);
98 
99  free (username);
100 
101  password = gsasl_property_get (sctx, GSASL_PASSWORD);
102  if (!password)
103  return GSASL_NO_PASSWORD;
104 
105  /* FIXME: Use SASLprep here? Treat string as storage string?
106  Specification is unclear. */
107  res = gsasl_saslprep (password, 0, &normkey, NULL);
108  if (res != GSASL_OK)
109  return res;
110 
111  cram_md5_digest (challenge, strlen (challenge),
112  normkey, strlen (normkey), hash);
113 
114  free (normkey);
115 
116  if (memcmp (&input[input_len - MD5LEN * 2], hash, 2 * MD5LEN) == 0)
117  res = GSASL_OK;
118  else
120 
121  *output_len = 0;
122  *output = NULL;
123 
124  return res;
125 }
126 
127 void
129  void *mech_data)
130 {
131  char *challenge = mech_data;
132 
133  free (challenge);
134 }
int cram_md5_challenge(char challenge[35])
Definition: challenge.c:68
#define CRAM_MD5_CHALLENGE_LEN
Definition: challenge.h:26
#define MD5LEN
Definition: server.c:42
int _gsasl_cram_md5_server_step(Gsasl_session *sctx, void *mech_data, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: server.c:65
void _gsasl_cram_md5_server_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
Definition: server.c:128
int _gsasl_cram_md5_server_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
Definition: server.c:45
void cram_md5_digest(const char *challenge, size_t challengelen, const char *secret, size_t secretlen, char response[32])
Definition: digest.c:62
#define CRAM_MD5_DIGEST_LEN
Definition: digest.h:29
#define NULL
Definition: stddef.in.h:72
const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:263
void gsasl_property_set(Gsasl_session *sctx, Gsasl_property prop, const char *data)
Definition: property.c:158
int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags, char **out, int *stringpreprc)
@ GSASL_OK
Definition: gsasl.h:171
@ GSASL_AUTHENTICATION_ERROR
Definition: gsasl.h:180
@ GSASL_NEEDS_MORE
Definition: gsasl.h:172
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:175
@ GSASL_NO_PASSWORD
Definition: gsasl.h:188
@ GSASL_MECHANISM_PARSE_ERROR
Definition: gsasl.h:179
@ GSASL_CRYPTO_ERROR
Definition: gsasl.h:177
@ GSASL_PASSWORD
Definition: gsasl.h:337
@ GSASL_AUTHID
Definition: gsasl.h:335
int rc
Definition: error.c:42
int res
Definition: mbrtowc-impl.h:45
char * strdup(const char *s)
Definition: strdup.c:39