cleartext.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC4616 PLAIN authentication
  22. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "vauth/vauth.h"
  29. #include "curl_base64.h"
  30. #include "curl_md5.h"
  31. #include "warnless.h"
  32. #include "strtok.h"
  33. #include "sendf.h"
  34. #include "curl_printf.h"
  35. /* The last #include files should be: */
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /*
  39. * Curl_auth_create_plain_message()
  40. *
  41. * This is used to generate an already encoded PLAIN message ready
  42. * for sending to the recipient.
  43. *
  44. * Parameters:
  45. *
  46. * data [in] - The session handle.
  47. * userp [in] - The user name.
  48. * passdwp [in] - The user's password.
  49. * outptr [in/out] - The address where a pointer to newly allocated memory
  50. * holding the result will be stored upon completion.
  51. * outlen [out] - The length of the output message.
  52. *
  53. * Returns CURLE_OK on success.
  54. */
  55. CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
  56. const char *userp,
  57. const char *passwdp,
  58. char **outptr, size_t *outlen)
  59. {
  60. CURLcode result;
  61. char *plainauth;
  62. size_t ulen;
  63. size_t plen;
  64. size_t plainlen;
  65. *outlen = 0;
  66. *outptr = NULL;
  67. ulen = strlen(userp);
  68. plen = strlen(passwdp);
  69. /* Compute binary message length. Check for overflows. */
  70. if((ulen > SIZE_T_MAX/2) || (plen > (SIZE_T_MAX/2 - 2)))
  71. return CURLE_OUT_OF_MEMORY;
  72. plainlen = 2 * ulen + plen + 2;
  73. plainauth = malloc(plainlen);
  74. if(!plainauth)
  75. return CURLE_OUT_OF_MEMORY;
  76. /* Calculate the reply */
  77. memcpy(plainauth, userp, ulen);
  78. plainauth[ulen] = '\0';
  79. memcpy(plainauth + ulen + 1, userp, ulen);
  80. plainauth[2 * ulen + 1] = '\0';
  81. memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
  82. /* Base64 encode the reply */
  83. result = Curl_base64_encode(data, plainauth, plainlen, outptr, outlen);
  84. free(plainauth);
  85. return result;
  86. }
  87. /*
  88. * Curl_auth_create_login_message()
  89. *
  90. * This is used to generate an already encoded LOGIN message containing the
  91. * user name or password ready for sending to the recipient.
  92. *
  93. * Parameters:
  94. *
  95. * data [in] - The session handle.
  96. * valuep [in] - The user name or user's password.
  97. * outptr [in/out] - The address where a pointer to newly allocated memory
  98. * holding the result will be stored upon completion.
  99. * outlen [out] - The length of the output message.
  100. *
  101. * Returns CURLE_OK on success.
  102. */
  103. CURLcode Curl_auth_create_login_message(struct Curl_easy *data,
  104. const char *valuep, char **outptr,
  105. size_t *outlen)
  106. {
  107. size_t vlen = strlen(valuep);
  108. if(!vlen) {
  109. /* Calculate an empty reply */
  110. *outptr = strdup("=");
  111. if(*outptr) {
  112. *outlen = (size_t) 1;
  113. return CURLE_OK;
  114. }
  115. *outlen = 0;
  116. return CURLE_OUT_OF_MEMORY;
  117. }
  118. /* Base64 encode the value */
  119. return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
  120. }
  121. /*
  122. * Curl_auth_create_external_message()
  123. *
  124. * This is used to generate an already encoded EXTERNAL message containing
  125. * the user name ready for sending to the recipient.
  126. *
  127. * Parameters:
  128. *
  129. * data [in] - The session handle.
  130. * user [in] - The user name.
  131. * outptr [in/out] - The address where a pointer to newly allocated memory
  132. * holding the result will be stored upon completion.
  133. * outlen [out] - The length of the output message.
  134. *
  135. * Returns CURLE_OK on success.
  136. */
  137. CURLcode Curl_auth_create_external_message(struct Curl_easy *data,
  138. const char *user, char **outptr,
  139. size_t *outlen)
  140. {
  141. /* This is the same formatting as the login message */
  142. return Curl_auth_create_login_message(data, user, outptr, outlen);
  143. }