xref: /third_party/curl/lib/vauth/cleartext.c (revision 13498266)
113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * RFC4616 PLAIN authentication
2413498266Sopenharmony_ci * Draft   LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
2513498266Sopenharmony_ci *
2613498266Sopenharmony_ci ***************************************************************************/
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "curl_setup.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) ||       \
3113498266Sopenharmony_ci  !defined(CURL_DISABLE_POP3) || \
3213498266Sopenharmony_ci  (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include <curl/curl.h>
3513498266Sopenharmony_ci#include "urldata.h"
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#include "vauth/vauth.h"
3813498266Sopenharmony_ci#include "warnless.h"
3913498266Sopenharmony_ci#include "strtok.h"
4013498266Sopenharmony_ci#include "sendf.h"
4113498266Sopenharmony_ci#include "curl_printf.h"
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci/* The last #include files should be: */
4413498266Sopenharmony_ci#include "curl_memory.h"
4513498266Sopenharmony_ci#include "memdebug.h"
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci/*
4813498266Sopenharmony_ci * Curl_auth_create_plain_message()
4913498266Sopenharmony_ci *
5013498266Sopenharmony_ci * This is used to generate an already encoded PLAIN message ready
5113498266Sopenharmony_ci * for sending to the recipient.
5213498266Sopenharmony_ci *
5313498266Sopenharmony_ci * Parameters:
5413498266Sopenharmony_ci *
5513498266Sopenharmony_ci * authzid [in]     - The authorization identity.
5613498266Sopenharmony_ci * authcid [in]     - The authentication identity.
5713498266Sopenharmony_ci * passwd  [in]     - The password.
5813498266Sopenharmony_ci * out     [out]    - The result storage.
5913498266Sopenharmony_ci *
6013498266Sopenharmony_ci * Returns CURLE_OK on success.
6113498266Sopenharmony_ci */
6213498266Sopenharmony_ciCURLcode Curl_auth_create_plain_message(const char *authzid,
6313498266Sopenharmony_ci                                        const char *authcid,
6413498266Sopenharmony_ci                                        const char *passwd,
6513498266Sopenharmony_ci                                        struct bufref *out)
6613498266Sopenharmony_ci{
6713498266Sopenharmony_ci  char *plainauth;
6813498266Sopenharmony_ci  size_t plainlen;
6913498266Sopenharmony_ci  size_t zlen;
7013498266Sopenharmony_ci  size_t clen;
7113498266Sopenharmony_ci  size_t plen;
7213498266Sopenharmony_ci
7313498266Sopenharmony_ci  zlen = (authzid == NULL ? 0 : strlen(authzid));
7413498266Sopenharmony_ci  clen = strlen(authcid);
7513498266Sopenharmony_ci  plen = strlen(passwd);
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci  /* Compute binary message length. Check for overflows. */
7813498266Sopenharmony_ci  if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) ||
7913498266Sopenharmony_ci     (plen > (SIZE_T_MAX/2 - 2)))
8013498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
8113498266Sopenharmony_ci  plainlen = zlen + clen + plen + 2;
8213498266Sopenharmony_ci
8313498266Sopenharmony_ci  plainauth = malloc(plainlen + 1);
8413498266Sopenharmony_ci  if(!plainauth)
8513498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci  /* Calculate the reply */
8813498266Sopenharmony_ci  if(zlen)
8913498266Sopenharmony_ci    memcpy(plainauth, authzid, zlen);
9013498266Sopenharmony_ci  plainauth[zlen] = '\0';
9113498266Sopenharmony_ci  memcpy(plainauth + zlen + 1, authcid, clen);
9213498266Sopenharmony_ci  plainauth[zlen + clen + 1] = '\0';
9313498266Sopenharmony_ci  memcpy(plainauth + zlen + clen + 2, passwd, plen);
9413498266Sopenharmony_ci  plainauth[plainlen] = '\0';
9513498266Sopenharmony_ci  Curl_bufref_set(out, plainauth, plainlen, curl_free);
9613498266Sopenharmony_ci  return CURLE_OK;
9713498266Sopenharmony_ci}
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci/*
10013498266Sopenharmony_ci * Curl_auth_create_login_message()
10113498266Sopenharmony_ci *
10213498266Sopenharmony_ci * This is used to generate an already encoded LOGIN message containing the
10313498266Sopenharmony_ci * user name or password ready for sending to the recipient.
10413498266Sopenharmony_ci *
10513498266Sopenharmony_ci * Parameters:
10613498266Sopenharmony_ci *
10713498266Sopenharmony_ci * valuep  [in]     - The user name or user's password.
10813498266Sopenharmony_ci * out     [out]    - The result storage.
10913498266Sopenharmony_ci *
11013498266Sopenharmony_ci * Returns CURLE_OK on success.
11113498266Sopenharmony_ci */
11213498266Sopenharmony_ciCURLcode Curl_auth_create_login_message(const char *valuep, struct bufref *out)
11313498266Sopenharmony_ci{
11413498266Sopenharmony_ci  Curl_bufref_set(out, valuep, strlen(valuep), NULL);
11513498266Sopenharmony_ci  return CURLE_OK;
11613498266Sopenharmony_ci}
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci/*
11913498266Sopenharmony_ci * Curl_auth_create_external_message()
12013498266Sopenharmony_ci *
12113498266Sopenharmony_ci * This is used to generate an already encoded EXTERNAL message containing
12213498266Sopenharmony_ci * the user name ready for sending to the recipient.
12313498266Sopenharmony_ci *
12413498266Sopenharmony_ci * Parameters:
12513498266Sopenharmony_ci *
12613498266Sopenharmony_ci * user    [in]     - The user name.
12713498266Sopenharmony_ci * out     [out]    - The result storage.
12813498266Sopenharmony_ci *
12913498266Sopenharmony_ci * Returns CURLE_OK on success.
13013498266Sopenharmony_ci */
13113498266Sopenharmony_ciCURLcode Curl_auth_create_external_message(const char *user,
13213498266Sopenharmony_ci                                           struct bufref *out)
13313498266Sopenharmony_ci{
13413498266Sopenharmony_ci  /* This is the same formatting as the login message */
13513498266Sopenharmony_ci  return Curl_auth_create_login_message(user, out);
13613498266Sopenharmony_ci}
13713498266Sopenharmony_ci
13813498266Sopenharmony_ci#endif /* if no users */
139