]> git.ozlabs.org Git - ccan/blob - ccan/rune/coding.c
380a225c948319c66dbb5e004383e8bd07dc28f5
[ccan] / ccan / rune / coding.c
1 /* MIT (BSD) license - see LICENSE file for details */
2 /* Routines to encode / decode a rune */
3 #include <ccan/rune/rune.h>
4 #include <ccan/rune/internal.h>
5 #include <ccan/str/hex/hex.h>
6 #include <ccan/tal/str/str.h>
7 #include <ccan/base64/base64.h>
8 #include <ccan/endian/endian.h>
9 #include <errno.h>
10
11 /* From Python base64.urlsafe_b64encode:
12  *
13  * The alphabet uses '-' instead of '+' and '_' instead of '/'.
14  */
15 static const base64_maps_t base64_maps_urlsafe = {
16   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
17
18   "\xff\xff\xff\xff\xff" /* 0 */
19   "\xff\xff\xff\xff\xff" /* 5 */
20   "\xff\xff\xff\xff\xff" /* 10 */
21   "\xff\xff\xff\xff\xff" /* 15 */
22   "\xff\xff\xff\xff\xff" /* 20 */
23   "\xff\xff\xff\xff\xff" /* 25 */
24   "\xff\xff\xff\xff\xff" /* 30 */
25   "\xff\xff\xff\xff\xff" /* 35 */
26   "\xff\xff\xff\xff\xff" /* 40 */
27   "\x3e\xff\xff\x34\x35" /* 45 */
28   "\x36\x37\x38\x39\x3a" /* 50 */
29   "\x3b\x3c\x3d\xff\xff" /* 55 */
30   "\xff\xff\xff\xff\xff" /* 60 */
31   "\x00\x01\x02\x03\x04" /* 65 A */
32   "\x05\x06\x07\x08\x09" /* 70 */
33   "\x0a\x0b\x0c\x0d\x0e" /* 75 */
34   "\x0f\x10\x11\x12\x13" /* 80 */
35   "\x14\x15\x16\x17\x18" /* 85 */
36   "\x19\xff\xff\xff\xff" /* 90 */
37   "\x3f\xff\x1a\x1b\x1c" /* 95 */
38   "\x1d\x1e\x1f\x20\x21" /* 100 */
39   "\x22\x23\x24\x25\x26" /* 105 */
40   "\x27\x28\x29\x2a\x2b" /* 110 */
41   "\x2c\x2d\x2e\x2f\x30" /* 115 */
42   "\x31\x32\x33\xff\xff" /* 120 */
43   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 125 */
44   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 135 */
45   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 145 */
46   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 155 */
47   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 165 */
48   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 175 */
49   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 185 */
50   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 195 */
51   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 205 */
52   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 215 */
53   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 225 */
54   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 235 */
55   "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" /* 245 */
56 };
57
58 /* For encoding as a string */
59 struct wbuf {
60         size_t off, len;
61         char *buf;
62 };
63
64 static void to_wbuf(const char *s, size_t len, void *vwbuf)
65 {
66         struct wbuf *wbuf = vwbuf;
67
68         while (wbuf->off + len > wbuf->len)
69                 tal_resize(&wbuf->buf, wbuf->len *= 2);
70         memcpy(wbuf->buf + wbuf->off, s, len);
71         wbuf->off += len;
72 }
73
74 /* For adding to sha256 */
75 static void to_sha256(const char *s, size_t len, void *vshactx)
76 {
77         struct sha256_ctx *shactx = vshactx;
78         sha256_update(shactx, s, len);
79 }
80
81 static void rune_altern_encode(const struct rune_altern *altern,
82                                void (*cb)(const char *s, size_t len,
83                                           void *arg),
84                                void *arg)
85 {
86         char cond = altern->condition;
87         const char *p;
88
89         cb(altern->fieldname, strlen(altern->fieldname), arg);
90         cb(&cond, 1, arg);
91
92         p = altern->value;
93         for (;;) {
94                 char esc[2] = { '\\' };
95                 size_t len = strcspn(p, "\\|&");
96                 cb(p, len, arg);
97                 if (!p[len])
98                         break;
99                 esc[1] = p[len];
100                 cb(esc, 2, arg);
101                 p += len + 1;
102         }
103 }
104
105 static void rune_restr_encode(const struct rune_restr *restr,
106                               void (*cb)(const char *s, size_t len,
107                                          void *arg),
108                               void *arg)
109 {
110         for (size_t i = 0; i < tal_count(restr->alterns); i++) {
111                 if (i != 0)
112                         cb("|", 1, arg);
113                 rune_altern_encode(restr->alterns[i], cb, arg);
114         }
115 }
116
117 void rune_sha256_add_restr(struct sha256_ctx *shactx,
118                            struct rune_restr *restr)
119 {
120         rune_restr_encode(restr, to_sha256, shactx);
121         rune_sha256_endmarker(shactx);
122 }
123
124 const char *rune_is_derived(const struct rune *source, const struct rune *rune)
125 {
126         if (!runestr_eq(source->version, rune->version))
127                 return "Version mismatch";
128
129         return rune_is_derived_anyversion(source, rune);
130 }
131         
132 const char *rune_is_derived_anyversion(const struct rune *source,
133                                        const struct rune *rune)
134 {
135         struct sha256_ctx shactx;
136         size_t i;
137
138         if (tal_count(rune->restrs) < tal_count(source->restrs))
139                 return "Fewer restrictions than master";
140
141         /* If we add the same restrictions to source rune, do we match? */
142         shactx = source->shactx;
143         for (i = 0; i < tal_count(rune->restrs); i++) {
144                 /* First restrictions must be identical */
145                 if (i < tal_count(source->restrs)) {
146                         if (!rune_restr_eq(source->restrs[i], rune->restrs[i]))
147                                 return "Does not match master restrictions";
148                 } else
149                         rune_sha256_add_restr(&shactx, rune->restrs[i]);
150         }
151
152         if (memcmp(shactx.s, rune->shactx.s, sizeof(shactx.s)) != 0)
153                 return "Not derived from master";
154         return NULL;
155 }
156
157 static bool peek_char(const char *data, size_t len, char *c)
158 {
159         if (len == 0)
160                 return false;
161         *c = *data;
162         return true;
163 }
164
165 static void drop_char(const char **data, size_t *len)
166 {
167         (*data)++;
168         (*len)--;
169 }
170
171 static void pull_invalid(const char **data, size_t *len)
172 {
173         *data = NULL;
174         *len = 0;
175 }
176
177 static bool pull_char(const char **data, size_t *len, char *c)
178 {
179         if (!peek_char(*data, *len, c)) {
180                 pull_invalid(data, len);
181                 return false;
182         }
183         drop_char(data, len);
184         return true;
185 }
186
187 static bool is_valid_cond(enum rune_condition cond)
188 {
189         switch (cond) {
190         case RUNE_COND_IF_MISSING:
191         case RUNE_COND_EQUAL:
192         case RUNE_COND_NOT_EQUAL:
193         case RUNE_COND_BEGINS:
194         case RUNE_COND_ENDS:
195         case RUNE_COND_CONTAINS:
196         case RUNE_COND_INT_LESS:
197         case RUNE_COND_INT_GREATER:
198         case RUNE_COND_LEXO_BEFORE:
199         case RUNE_COND_LEXO_AFTER:
200         case RUNE_COND_COMMENT:
201                 return true;
202         }
203         return false;
204 }
205
206 /* Sets *more on success: true if another altern follows */
207 static struct rune_altern *rune_altern_decode(const tal_t *ctx,
208                                               const char **data, size_t *len,
209                                               bool *more)
210 {
211         struct rune_altern *alt = tal(ctx, struct rune_altern);
212         const char *strstart = *data;
213         char *value;
214         size_t strlen = 0;
215         char c;
216
217         /* Swallow field up to conditional */
218         for (;;) {
219                 if (!pull_char(data, len, &c))
220                         return tal_free(alt);
221                 if (cispunct(c))
222                         break;
223                 strlen++;
224         }
225
226         alt->fieldname = tal_strndup(alt, strstart, strlen);
227         if (!is_valid_cond(c)) {
228                 pull_invalid(data, len);
229                 return tal_free(alt);
230         }
231         alt->condition = c;
232
233         /* Assign worst case. */
234         value = tal_arr(alt, char, *len + 1);
235         strlen = 0;
236         *more = false;
237         while (*len && pull_char(data, len, &c)) {
238                 if (c == '|') {
239                         *more = true;
240                         break;
241                 }
242                 if (c == '&')
243                         break;
244
245                 if (c == '\\' && !pull_char(data, len, &c))
246                         return tal_free(alt);
247                 value[strlen++] = c;
248         }
249         value[strlen] = '\0';
250         tal_resize(&value, strlen + 1);
251         alt->value = value;
252         return alt;
253 }
254
255 static struct rune_restr *rune_restr_decode(const tal_t *ctx,
256                                             const char **data, size_t *len)
257 {
258         struct rune_restr *restr = tal(ctx, struct rune_restr);
259         size_t num_alts = 0;
260         bool more;
261
262         /* Must have at least one! */
263         restr->alterns = tal_arr(restr, struct rune_altern *, 0);
264         do {
265                 struct rune_altern *alt;
266
267                 alt = rune_altern_decode(restr, data, len, &more);
268                 if (!alt)
269                         return tal_free(restr);
270                 tal_resize(&restr->alterns, num_alts+1);
271                 restr->alterns[num_alts++] = alt;
272         } while (more);
273         return restr;
274 }
275
276 static struct rune *from_string(const tal_t *ctx,
277                                 const char *str,
278                                 const u8 *hash32)
279 {
280         size_t len = strlen(str);
281         struct rune *rune = tal(ctx, struct rune);
282
283         /* Now count up how many bytes we should have hashed: secret uses
284          * first block. */
285         rune->shactx.bytes = 64;
286
287         rune->restrs = tal_arr(rune, struct rune_restr *, 0);
288         rune->unique_id = NULL;
289         rune->version = NULL;
290
291         while (len) {
292                 struct rune_restr *restr;
293                 restr = rune_restr_decode(rune, &str, &len);
294                 if (!restr)
295                         return tal_free(rune);
296                 if (!rune_add_restr(rune, restr))
297                         return tal_free(rune);
298         }
299
300         /* Now we replace with canned hash state */
301         memcpy(rune->shactx.s, hash32, 32);
302         for (size_t i = 0; i < 8; i++)
303                 rune->shactx.s[i] = be32_to_cpu(rune->shactx.s[i]);
304
305         return rune;
306 }
307
308 struct rune_restr *rune_restr_from_string(const tal_t *ctx,
309                                           const char *str,
310                                           size_t len)
311 {
312         struct rune_restr *restr;
313
314         restr = rune_restr_decode(NULL, &str, &len);
315         /* Don't allow trailing chars */
316         if (restr && len != 0)
317                 restr = tal_free(restr);
318         return tal_steal(ctx, restr);
319 }
320
321 static void to_string(struct wbuf *wbuf, const struct rune *rune, u8 *hash32)
322 {
323         /* Copy hash in big-endian */
324         for (size_t i = 0; i < 8; i++) {
325                 be32 v = cpu_to_be32(rune->shactx.s[i]);
326                 memcpy(hash32 + i*4, &v, sizeof(v));
327         }
328
329         for (size_t i = 0; i < tal_count(rune->restrs); i++) {
330                 if (i != 0)
331                         to_wbuf("&", 1, wbuf);
332                 rune_restr_encode(rune->restrs[i], to_wbuf, wbuf);
333         }
334         to_wbuf("", 1, wbuf);
335 }
336
337 struct rune *rune_from_base64n(const tal_t *ctx, const char *str, size_t len)
338 {
339         size_t blen;
340         u8 *data;
341         struct rune *rune;
342
343         data = tal_arr(NULL, u8, base64_decoded_length(len) + 1);
344
345         blen = base64_decode_using_maps(&base64_maps_urlsafe,
346                                        (char *)data, tal_bytelen(data),
347                                        str, len);
348         if (blen == -1)
349                 goto fail;
350
351         if (blen < 32)
352                 goto fail;
353
354         data[blen] = '\0';
355         /* Sanity check that it's a valid string! */
356         if (strlen((char *)data + 32) != blen - 32)
357                 goto fail;
358
359         rune = from_string(ctx, (const char *)data + 32, data);
360         tal_free(data);
361         return rune;
362
363 fail:
364         tal_free(data);
365         return NULL;
366 }
367
368 struct rune *rune_from_base64(const tal_t *ctx, const char *str)
369 {
370         return rune_from_base64n(ctx, str, strlen(str));
371 }
372
373 char *rune_to_base64(const tal_t *ctx, const struct rune *rune)
374 {
375         u8 hash32[32];
376         char *ret;
377         size_t ret_len;
378         struct wbuf wbuf;
379
380         /* We're going to prepend hash */
381         wbuf.off = sizeof(hash32);
382         wbuf.len = 64;
383         wbuf.buf = tal_arr(NULL, char, wbuf.len);
384
385         to_string(&wbuf, rune, hash32);
386         /* Prepend hash */
387         memcpy(wbuf.buf, hash32, sizeof(hash32));
388
389         ret = tal_arr(ctx, char, base64_encoded_length(wbuf.off) + 1);
390         ret_len = base64_encode_using_maps(&base64_maps_urlsafe,
391                                            ret, tal_bytelen(ret),
392                                            wbuf.buf, wbuf.off - 1);
393         ret[ret_len] = '\0';
394         tal_free(wbuf.buf);
395         return ret;
396 }
397
398 struct rune *rune_from_string(const tal_t *ctx, const char *str)
399 {
400         u8 hash[32];
401         if (!hex_decode(str, 64, hash, sizeof(hash)))
402                 return NULL;
403         if (str[64] != ':')
404                 return NULL;
405         return from_string(ctx, str + 65, hash);
406 }
407
408 char *rune_to_string(const tal_t *ctx, const struct rune *rune)
409 {
410         u8 hash32[32];
411         struct wbuf wbuf;
412
413         /* We're going to prepend hash (in hex), plus colon */
414         wbuf.off = sizeof(hash32) * 2 + 1;
415         wbuf.len = 128;
416         wbuf.buf = tal_arr(ctx, char, wbuf.len);
417
418         to_string(&wbuf, rune, hash32);
419         hex_encode(hash32, sizeof(hash32), wbuf.buf, sizeof(hash32) * 2 + 1);
420         wbuf.buf[sizeof(hash32) * 2] = ':';
421         return wbuf.buf;
422 }