1 /* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "lib.h"
4 #include "base64.h"
5 #include "hex-binary.h"
6 #include "str.h"
7 #include "hash-method.h"
8 #include "hash-format.h"
9 
10 enum hash_encoding {
11 	HASH_ENCODING_HEX,
12 	HASH_ENCODING_HEX_SHORT,
13 	HASH_ENCODING_BASE64
14 };
15 
16 struct hash_format_list {
17 	struct hash_format_list *next;
18 
19 	const struct hash_method *method;
20 	void *context;
21 	unsigned int bits;
22 	enum hash_encoding encoding;
23 };
24 
25 struct hash_format {
26 	pool_t pool;
27 	const char *str;
28 
29 	struct hash_format_list *list, **pos;
30 	unsigned char *digest;
31 };
32 
33 static int
hash_format_parse(const char * str,unsigned int * idxp,const struct hash_method ** method_r,unsigned int * bits_r,const char ** error_r)34 hash_format_parse(const char *str, unsigned int *idxp,
35 		  const struct hash_method **method_r,
36 		  unsigned int *bits_r, const char **error_r)
37 {
38 	const char *name, *end, *bitsp;
39 	unsigned int bits, i = *idxp;
40 
41 	/* we should have "hash_name}" or "hash_name:bits}" */
42 	end = strchr(str+i, '}');
43 	if (end == NULL) {
44 		*error_r = "Missing '}'";
45 		return -1;
46 	}
47 	*idxp = end - str;
48 	name = t_strdup_until(str+i, end);
49 
50 	bitsp = strchr(name, ':');
51 	if (bitsp != NULL)
52 		name = t_strdup_until(name, bitsp++);
53 
54 	*method_r = hash_method_lookup(name);
55 	if (*method_r == NULL) {
56 		*error_r = t_strconcat("Unknown hash method: ", name, NULL);
57 		return -1;
58 	}
59 
60 	bits = (*method_r)->digest_size * 8;
61 	if (bitsp != NULL) {
62 		if (str_to_uint(bitsp, &bits) < 0 ||
63 		    bits == 0 || bits > (*method_r)->digest_size*8) {
64 			*error_r = t_strconcat("Invalid :bits number: ",
65 					       bitsp, NULL);
66 			return -1;
67 		}
68 		if ((bits % 8) != 0) {
69 			*error_r = t_strconcat(
70 				"Currently :bits must be divisible by 8: ",
71 				bitsp, NULL);
72 			return -1;
73 		}
74 	}
75 	*bits_r = bits;
76 	return 0;
77 }
78 
79 static int
hash_format_string_analyze(struct hash_format * format,const char * str,const char ** error_r)80 hash_format_string_analyze(struct hash_format *format, const char *str,
81 			   const char **error_r)
82 {
83 	struct hash_format_list *list;
84 	unsigned int i;
85 
86 	for (i = 0; str[i] != '\0'; i++) {
87 		if (str[i] != '%')
88 			continue;
89 		i++;
90 
91 		list = p_new(format->pool, struct hash_format_list, 1);
92 		list->encoding = HASH_ENCODING_HEX;
93 		*format->pos = list;
94 		format->pos = &list->next;
95 
96 		if (str[i] == 'B') {
97 			list->encoding = HASH_ENCODING_BASE64;
98 			i++;
99 		} else if (str[i] == 'X') {
100 			list->encoding = HASH_ENCODING_HEX_SHORT;
101 			i++;
102 		}
103 		if (str[i++] != '{') {
104 			*error_r = "No '{' after '%'";
105 			return -1;
106 		}
107 		if (hash_format_parse(str, &i, &list->method,
108 				      &list->bits, error_r) < 0)
109 			return -1;
110 		list->context = p_malloc(format->pool,
111 					 list->method->context_size);
112 		list->method->init(list->context);
113 	}
114 	return 0;
115 }
116 
hash_format_init(const char * format_string,struct hash_format ** format_r,const char ** error_r)117 int hash_format_init(const char *format_string, struct hash_format **format_r,
118 		     const char **error_r)
119 {
120 	struct hash_format *format;
121 	pool_t pool;
122 	int ret;
123 
124 	pool = pool_alloconly_create("hash format", 1024);
125 	format = p_new(pool, struct hash_format, 1);
126 	format->pool = pool;
127 	format->str = p_strdup(pool, format_string);
128 	format->pos = &format->list;
129 	T_BEGIN {
130 		ret = hash_format_string_analyze(format, format_string,
131 						 error_r);
132 		if (ret < 0)
133 			*error_r = p_strdup(format->pool, *error_r);
134 	} T_END;
135 	if (ret < 0) {
136 		*error_r = t_strdup(*error_r);
137 		pool_unref(&pool);
138 		return -1;
139 	}
140 	*format_r = format;
141 	return 0;
142 }
143 
hash_format_loop(struct hash_format * format,const void * data,size_t size)144 void hash_format_loop(struct hash_format *format,
145 		      const void *data, size_t size)
146 {
147 	struct hash_format_list *list;
148 
149 	for (list = format->list; list != NULL; list = list->next)
150 		list->method->loop(list->context, data, size);
151 }
152 
hash_format_reset(struct hash_format * format)153 void hash_format_reset(struct hash_format *format)
154 {
155 	struct hash_format_list *list;
156 
157 	for (list = format->list; list != NULL; list = list->next) {
158 		memset(list->context, 0, list->method->context_size);
159 		list->method->init(list->context);
160 	}
161 }
162 
163 static void
hash_format_digest(string_t * dest,const struct hash_format_list * list,const unsigned char * digest)164 hash_format_digest(string_t *dest, const struct hash_format_list *list,
165 		   const unsigned char *digest)
166 {
167 	unsigned int i, orig_len, size = list->bits / 8;
168 
169 	i_assert(list->bits % 8 == 0);
170 
171 	switch (list->encoding) {
172 	case HASH_ENCODING_HEX:
173 		binary_to_hex_append(dest, digest, size);
174 		break;
175 	case HASH_ENCODING_HEX_SHORT:
176 		orig_len = str_len(dest);
177 		binary_to_hex_append(dest, digest, size);
178 		/* drop leading zeros, except if it's the only one */
179 		for (i = orig_len; i < str_len(dest); i++) {
180 			if (str_data(dest)[i] != '0')
181 				break;
182 		}
183 		if (i == str_len(dest)) i--;
184 		str_delete(dest, orig_len, i-orig_len);
185 		break;
186 	case HASH_ENCODING_BASE64:
187 		orig_len = str_len(dest);
188 		base64_encode(digest, size, dest);
189 		/* drop trailing '=' chars */
190 		while (str_len(dest) > orig_len &&
191 		       str_data(dest)[str_len(dest)-1] == '=')
192 			str_truncate(dest, str_len(dest)-1);
193 		break;
194 	}
195 }
196 
hash_format_write(struct hash_format * format,string_t * dest)197 void hash_format_write(struct hash_format *format, string_t *dest)
198 {
199 	struct hash_format_list *list;
200 	const char *p;
201 	unsigned int i, max_digest_size = 0;
202 
203 	for (list = format->list; list != NULL; list = list->next) {
204 		if (max_digest_size < list->method->digest_size)
205 			max_digest_size = list->method->digest_size;
206 	}
207 	if (format->digest == NULL)
208 		format->digest = p_malloc(format->pool, max_digest_size);
209 
210 	list = format->list;
211 	for (i = 0; format->str[i] != '\0'; i++) {
212 		if (format->str[i] != '%') {
213 			str_append_c(dest, format->str[i]);
214 			continue;
215 		}
216 
217 		/* we already verified that the string is ok */
218 		i_assert(list != NULL);
219 		list->method->result(list->context, format->digest);
220 		hash_format_digest(dest, list, format->digest);
221 		list = list->next;
222 
223 		p = strchr(format->str+i, '}');
224 		i_assert(p != NULL);
225 		i = p - format->str;
226 	}
227 }
228 
hash_format_deinit(struct hash_format ** _format,string_t * dest)229 void hash_format_deinit(struct hash_format **_format, string_t *dest)
230 {
231 	struct hash_format *format = *_format;
232 
233 	*_format = NULL;
234 
235 	hash_format_write(format, dest);
236 	pool_unref(&format->pool);
237 }
238 
hash_format_deinit_free(struct hash_format ** _format)239 void hash_format_deinit_free(struct hash_format **_format)
240 {
241 	struct hash_format *format = *_format;
242 
243 	*_format = NULL;
244 	pool_unref(&format->pool);
245 }
246