66a4f15fcf2a423d99b798c13ef3a6196a8a9763
[project/uclient.git] / uclient-utils.c
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #include <libubox/md5.h>
7 #include <libubox/utils.h>
8
9 #include "uclient-utils.h"
10
11 static const char *b64 =
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
14 void base64_encode(const void *inbuf, unsigned int len, void *outbuf)
15 {
16 unsigned char *out = outbuf;
17 const uint8_t *in = inbuf;
18 unsigned int i;
19 int pad = len % 3;
20
21 for (i = 0; i < len - pad; i += 3) {
22 uint32_t in3 = (in[0] << 16) | (in[1] << 8) | in[2];
23 int k;
24
25 for (k = 3; k >= 0; k--) {
26 out[k] = b64[in3 & 0x3f];
27 in3 >>= 6;
28 }
29 in += 3;
30 out += 4;
31 }
32
33 if (pad) {
34 uint32_t in2 = in[0] << (16 - 6);
35
36 out[3] = '=';
37
38 if (pad > 1) {
39 in2 |= in[1] << (8 - 6);
40 out[2] = b64[in2 & 0x3f];
41 } else {
42 out[2] = '=';
43 }
44
45 in2 >>= 6;
46 out[1] = b64[in2 & 0x3f];
47 in2 >>= 6;
48 out[0] = b64[in2 & 0x3f];
49
50 out += 4;
51 }
52
53 *out = '\0';
54 }
55
56 int uclient_urldecode(const char *in, char *out, bool decode_plus)
57 {
58 static char dec[3];
59 int ret = 0;
60 char c;
61
62 while ((c = *(in++))) {
63 if (c == '%') {
64 if (!isxdigit(in[0]) || !isxdigit(in[1]))
65 return -1;
66
67 dec[0] = in[0];
68 dec[1] = in[1];
69 c = strtol(dec, NULL, 16);
70 in += 2;
71 } else if (decode_plus && c == '+') {
72 c = ' ';
73 }
74
75 *(out++) = c;
76 ret++;
77 }
78
79 *out = 0;
80 return ret;
81 }
82
83 static char hex_digit(char val)
84 {
85 val += val > 9 ? 'a' - 10 : '0';
86 return val;
87 }
88
89 void bin_to_hex(char *dest, const void *buf, int len)
90 {
91 const uint8_t *data = buf;
92 int i;
93
94 for (i = 0; i < len; i++) {
95 *(dest++) = hex_digit(data[i] >> 4);
96 *(dest++) = hex_digit(data[i] & 0xf);
97 }
98 *dest = 0;
99 }
100
101 static void http_create_hash(char *dest, const char * const * str, int n_str)
102 {
103 uint32_t hash[4];
104 md5_ctx_t md5;
105 int i;
106
107 md5_begin(&md5);
108 for (i = 0; i < n_str; i++) {
109 if (i)
110 md5_hash(":", 1, &md5);
111 md5_hash(str[i], strlen(str[i]), &md5);
112 }
113 md5_end(hash, &md5);
114 bin_to_hex(dest, &hash, sizeof(hash));
115 }
116
117 void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password)
118 {
119 const char *hash_str[] = {
120 user,
121 realm,
122 password
123 };
124
125 http_create_hash(dest, hash_str, ARRAY_SIZE(hash_str));
126 }
127
128 void http_digest_calculate_response(char *dest, const struct http_digest_data *data)
129 {
130 const char *h_a2_strings[] = {
131 data->method,
132 data->uri,
133 };
134 const char *resp_strings[] = {
135 data->auth_hash,
136 data->nonce,
137 data->nc,
138 data->cnonce,
139 data->qop,
140 dest, /* initialized to H(A2) first */
141 };
142
143 http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings));
144 http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings));
145 }