gsasl  2.2.2
cram-md5/server.c
Go to the documentation of this file.
1 /* server.c --- SASL CRAM-MD5 server side functions.
2  * Copyright (C) 2002-2025 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, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 
24 /* Get specification. */
25 #include "cram-md5.h"
26 
27 /* Get malloc, free. */
28 #include <stdlib.h>
29 
30 /* Get memcpy, strdup, strlen. */
31 #include <string.h>
32 
33 /* Get cram_md5_challenge. */
34 #include "challenge.h"
35 
36 /* Get cram_md5_digest. */
37 #include "digest.h"
38 
39 #define MD5LEN 16
40 
41 int
43  void **mech_data)
44 {
45  char *challenge;
46  int rc;
47 
48  challenge = malloc (CRAM_MD5_CHALLENGE_LEN);
49  if (challenge == NULL)
50  return GSASL_MALLOC_ERROR;
51 
52  rc = cram_md5_challenge (challenge);
53  if (rc)
54  {
55  free (challenge);
56  return GSASL_CRYPTO_ERROR;
57  }
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  res = gsasl_property_set (sctx, GSASL_AUTHID, username);
98  free (username);
99  if (res != GSASL_OK)
100  return res;
101 
102  password = gsasl_property_get (sctx, GSASL_PASSWORD);
103  if (!password)
104  return GSASL_NO_PASSWORD;
105 
106  /* FIXME: Use SASLprep here? Treat string as storage string?
107  Specification is unclear. */
108  res = gsasl_saslprep (password, 0, &normkey, NULL);
109  if (res != GSASL_OK)
110  return res;
111 
112  cram_md5_digest (challenge, strlen (challenge),
113  normkey, strlen (normkey), hash);
114 
115  free (normkey);
116 
117  if (memcmp (&input[input_len - MD5LEN * 2], hash, 2 * MD5LEN) == 0)
118  res = GSASL_OK;
119  else
121 
122  *output_len = 0;
123  *output = NULL;
124 
125  return res;
126 }
127 
128 void
130  void *mech_data)
131 {
132  char *challenge = mech_data;
133 
134  free (challenge);
135 }
int cram_md5_challenge(char challenge[CRAM_MD5_CHALLENGE_LEN])
Definition: challenge.c:65
#define CRAM_MD5_CHALLENGE_LEN
Definition: challenge.h:25
#define MD5LEN
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)
void _gsasl_cram_md5_server_finish(Gsasl_session *sctx _GL_UNUSED, void *mech_data)
int _gsasl_cram_md5_server_start(Gsasl_session *sctx _GL_UNUSED, void **mech_data)
void cram_md5_digest(const char *challenge, size_t challengelen, const char *secret, size_t secretlen, char response[CRAM_MD5_DIGEST_LEN])
Definition: digest.c:59
#define CRAM_MD5_DIGEST_LEN
Definition: digest.h:28
int rc
Definition: error.c:36
@ GSASL_OK
Definition: gsasl.h:128
@ GSASL_AUTHENTICATION_ERROR
Definition: gsasl.h:137
@ GSASL_NEEDS_MORE
Definition: gsasl.h:129
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:132
@ GSASL_NO_PASSWORD
Definition: gsasl.h:145
@ GSASL_MECHANISM_PARSE_ERROR
Definition: gsasl.h:136
@ GSASL_CRYPTO_ERROR
Definition: gsasl.h:134
_GSASL_API int gsasl_property_set(Gsasl_session *sctx, Gsasl_property prop, const char *data)
Definition: property.c:188
_GSASL_API const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:291
@ GSASL_PASSWORD
Definition: gsasl.h:225
@ GSASL_AUTHID
Definition: gsasl.h:223
_GSASL_API int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags, char **out, int *stringpreprc)