gsasl  2.2.1
cram-md5/client.c
Go to the documentation of this file.
1 /* client.c --- SASL CRAM-MD5 client side functions.
2  * Copyright (C) 2002-2024 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 #include <config.h>
24 
25 /* Get specification. */
26 #include "cram-md5.h"
27 
28 /* Get malloc, free. */
29 #include <stdlib.h>
30 
31 /* Get memcpy, strlen. */
32 #include <string.h>
33 
34 /* Get cram_md5_digest. */
35 #include "digest.h"
36 
37 int
39  void *mech_data _GL_UNUSED,
40  const char *input, size_t input_len,
41  char **output, size_t *output_len)
42 {
43  char response[CRAM_MD5_DIGEST_LEN];
44  const char *p;
45  size_t len;
46  char *tmp;
47  char *authid;
48  int rc;
49 
50  if (input_len == 0)
51  {
52  *output_len = 0;
53  *output = NULL;
54  return GSASL_NEEDS_MORE;
55  }
56 
57  p = gsasl_property_get (sctx, GSASL_AUTHID);
58  if (!p)
59  return GSASL_NO_AUTHID;
60 
61  /* XXX Use query strings here? Specification is unclear. */
62  rc = gsasl_saslprep (p, GSASL_ALLOW_UNASSIGNED, &authid, NULL);
63  if (rc != GSASL_OK)
64  return rc;
65 
67  if (!p)
68  {
69  free (authid);
70  return GSASL_NO_PASSWORD;
71  }
72 
73  /* XXX Use query strings here? Specification is unclear. */
74  rc = gsasl_saslprep (p, GSASL_ALLOW_UNASSIGNED, &tmp, NULL);
75  if (rc != GSASL_OK)
76  {
77  free (authid);
78  return rc;
79  }
80 
81  cram_md5_digest (input, input_len, tmp, strlen (tmp), response);
82 
83  free (tmp);
84 
85  len = strlen (authid);
86 
87  *output_len = len + strlen (" ") + CRAM_MD5_DIGEST_LEN;
88  *output = malloc (*output_len);
89  if (!*output)
90  {
91  free (authid);
92  return GSASL_MALLOC_ERROR;
93  }
94 
95  memcpy (*output, authid, len);
96  (*output)[len++] = ' ';
97  memcpy (*output + len, response, CRAM_MD5_DIGEST_LEN);
98 
99  free (authid);
100 
101  return GSASL_OK;
102 }
int _gsasl_cram_md5_client_step(Gsasl_session *sctx, void *mech_data _GL_UNUSED, const char *input, size_t input_len, char **output, size_t *output_len)
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:60
#define CRAM_MD5_DIGEST_LEN
Definition: digest.h:29
int rc
Definition: error.c:37
@ GSASL_ALLOW_UNASSIGNED
Definition: gsasl.h:332
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_NEEDS_MORE
Definition: gsasl.h:130
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
@ GSASL_NO_PASSWORD
Definition: gsasl.h:146
@ GSASL_NO_AUTHID
Definition: gsasl.h:144
_GSASL_API const char * gsasl_property_get(Gsasl_session *sctx, Gsasl_property prop)
Definition: property.c:292
@ GSASL_PASSWORD
Definition: gsasl.h:226
@ GSASL_AUTHID
Definition: gsasl.h:224
_GSASL_API int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags, char **out, int *stringpreprc)