uclient-utils.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * uclient - ustream based protocol client library
  3. *
  4. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <libubox/md5.h>
  23. #include <libubox/utils.h>
  24. #include "uclient-utils.h"
  25. static const char *b64 =
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  27. void base64_encode(const void *inbuf, unsigned int len, void *outbuf)
  28. {
  29. unsigned char *out = outbuf;
  30. const uint8_t *in = inbuf;
  31. unsigned int i;
  32. int pad = len % 3;
  33. for (i = 0; i < len - pad; i += 3) {
  34. uint32_t in3 = (in[0] << 16) | (in[1] << 8) | in[2];
  35. int k;
  36. for (k = 3; k >= 0; k--) {
  37. out[k] = b64[in3 & 0x3f];
  38. in3 >>= 6;
  39. }
  40. in += 3;
  41. out += 4;
  42. }
  43. if (pad) {
  44. uint32_t in2 = in[0] << (16 - 6);
  45. out[3] = '=';
  46. if (pad > 1) {
  47. in2 |= in[1] << (8 - 6);
  48. out[2] = b64[in2 & 0x3f];
  49. } else {
  50. out[2] = '=';
  51. }
  52. in2 >>= 6;
  53. out[1] = b64[in2 & 0x3f];
  54. in2 >>= 6;
  55. out[0] = b64[in2 & 0x3f];
  56. out += 4;
  57. }
  58. *out = '\0';
  59. }
  60. int uclient_urldecode(const char *in, char *out, bool decode_plus)
  61. {
  62. static char dec[3];
  63. int ret = 0;
  64. char c;
  65. while ((c = *(in++))) {
  66. if (c == '%') {
  67. if (!isxdigit(in[0]) || !isxdigit(in[1]))
  68. return -1;
  69. dec[0] = in[0];
  70. dec[1] = in[1];
  71. c = strtol(dec, NULL, 16);
  72. in += 2;
  73. } else if (decode_plus && c == '+') {
  74. c = ' ';
  75. }
  76. *(out++) = c;
  77. ret++;
  78. }
  79. *out = 0;
  80. return ret;
  81. }
  82. static char hex_digit(char val)
  83. {
  84. val += val > 9 ? 'a' - 10 : '0';
  85. return val;
  86. }
  87. void bin_to_hex(char *dest, const void *buf, int len)
  88. {
  89. const uint8_t *data = buf;
  90. int i;
  91. for (i = 0; i < len; i++) {
  92. *(dest++) = hex_digit(data[i] >> 4);
  93. *(dest++) = hex_digit(data[i] & 0xf);
  94. }
  95. *dest = 0;
  96. }
  97. static void http_create_hash(char *dest, const char * const * str, int n_str)
  98. {
  99. uint32_t hash[4];
  100. md5_ctx_t md5;
  101. int i;
  102. md5_begin(&md5);
  103. for (i = 0; i < n_str; i++) {
  104. if (i)
  105. md5_hash(":", 1, &md5);
  106. md5_hash(str[i], strlen(str[i]), &md5);
  107. }
  108. md5_end(hash, &md5);
  109. bin_to_hex(dest, &hash, sizeof(hash));
  110. }
  111. void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password)
  112. {
  113. const char *hash_str[] = {
  114. user,
  115. realm,
  116. password
  117. };
  118. http_create_hash(dest, hash_str, ARRAY_SIZE(hash_str));
  119. }
  120. void http_digest_calculate_response(char *dest, const struct http_digest_data *data)
  121. {
  122. const char *h_a2_strings[] = {
  123. data->method,
  124. data->uri,
  125. };
  126. const char *resp_strings[] = {
  127. data->auth_hash,
  128. data->nonce,
  129. data->nc,
  130. data->cnonce,
  131. data->qop,
  132. dest, /* initialized to H(A2) first */
  133. };
  134. http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
  135. http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
  136. }