summaryrefslogtreecommitdiff
path: root/c_src/dnssec.c
blob: 6b9431d4911f89e3d27b00a8fbbbc971f7c97209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (c) 2016, NORDUnet A/S.
 * See LICENSE for licensing information.
 *
 * Invocation: dnssec <path-to-trust-anchor-file>
 *
 * Once running, read DNSSEC RR's from stdin, canonicalise RR's
 * (RFC4034 6.2), validate RR's (todo:ref) and write the result to
 * stdout.
 *
 * All length fields in the input and output denotes the length of the
 * piece of data to follow in number of octets.
 *
 * Input format:
 * - Length of data (4 octets)
 * - DNSSEC RR's as a DNSSEC_key_chain, specified in
 *   draft-zhang-trans-ct-dnssec-03 section 4.1 but without the TLS
 *   data structure encoding.
 *
 * Output format:
 * - Lenght of data (4 octets)
 * - Status code -- the getdns_return_t value in network byte order (2
 *   octets)
 * - (RR's)* -- if validation succeeded: the DS+RRSIG and the full
 *   chain up to and including the trust anchor; if validation failed:
 *   nothing
 *
 * (RR's)* denotes zero or more RR's.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <endian.h>
#include <getopt.h>
#include <getdns/getdns.h>
#include <getdns/getdns_extra.h>
#include "erlport.h"
#include "dnssec_test.h"

static int debug = 0;           /* DEBUG */

#if defined(TEST)
static char *testmode = NULL;
#endif

static void
print_tree(FILE *fp, const getdns_list *tree, const char *name)
{
  if (name)
    fprintf(fp, "* %s\n", name);

    char *s = getdns_pretty_print_list(tree);
    fputs(s, fp);
    free(s);
}

/* TODO: Replace read_file() and wire_rrs2list() with getdns_fp2rr_list()? */
size_t
read_file(FILE *infp, uint8_t **bufp_out, size_t size_hint)
{
#define CHUNKSIZE 4096
  size_t nread = 0;
  uint8_t *wirebuf = NULL;
  size_t chunksize = CHUNKSIZE;
  int chunks = 1;

  if (size_hint > 0)
    chunksize = size_hint;
  wirebuf = malloc(chunksize);

  if (wirebuf == NULL)
    goto out;

  while (1)
    {
      size_t n = fread(wirebuf + nread, 1, chunksize, infp);
      nread += n;
      if (n < chunksize)
        break;                  /* Done. */

      wirebuf = realloc(wirebuf, ++chunks * chunksize);
      if (wirebuf == NULL)
        break;
    }

 out:
  if (bufp_out != NULL)
    *bufp_out = wirebuf;
  return nread;
}

static getdns_return_t
wire_rrs2list(const uint8_t *buf, size_t buf_len, getdns_list **list_out)
{
  getdns_return_t r = GETDNS_RETURN_GOOD;
  getdns_list *list = getdns_list_create();
  getdns_dict *dict = NULL;
  size_t rr_count = 0;

  if (list == NULL)
    return GETDNS_RETURN_MEMORY_ERROR;
  while (buf_len > 0)
    {
      r = getdns_wire2rr_dict_scan(&buf, &buf_len, &dict);
      if (r)
        break;
      r = getdns_list_set_dict(list, rr_count, dict);
      getdns_dict_destroy(dict); /* The list has a copy. */
      if (r)
        break;
      rr_count++;
    }

  if (list_out)
    *list_out = list;
  return r;
}

static int
read_trust_anchors(const char *fname, getdns_list **list_out)
{
  if (debug)
    fprintf(stderr, "reading trust anchors from file %s\n", fname);

  FILE *fp = fopen(fname, "r");
  if (fp == NULL)
    return -errno;
  uint8_t *buf = NULL;
  size_t n = read_file(fp, &buf, 0);
  if (fclose(fp))
    fprintf(stderr, "unable to close trust anchors file: %d", errno);
  int r = wire_rrs2list(buf, n, list_out);
  free(buf);
  return r;
}

static getdns_return_t
list2wire(const getdns_list *list, uint8_t *out_buf, size_t *out_buf_len)
{
  size_t list_len;
  getdns_return_t r = getdns_list_get_length(list, &list_len);
  if (r)
    return r;

  getdns_dict *rr = NULL;
  uint8_t *buf = NULL;
  size_t buf_len = 0;
  *out_buf_len = 0;
  for (int i = 0; i < list_len; i++)
    {
      if ((r = getdns_list_get_dict(list, i , &rr)))
        return r;
      if ((r = getdns_rr_dict2wire(rr, &buf, &buf_len)))
        return r;               /* FIXME: Risk of leaking buf? */
      memcpy(out_buf, buf, buf_len);
      free(buf);
      out_buf += buf_len;
      *out_buf_len += buf_len;
    }

  return r;
}

#if !defined(TEST)
static getdns_return_t
validate(const uint8_t *buf, size_t buf_len,
         getdns_list *trust_anchors,
         time_t validation_time, uint32_t skew,
         uint8_t *out_buf, size_t *out_buf_len)
{
  getdns_return_t r = GETDNS_DNSSEC_INDETERMINATE;
  getdns_list *list = NULL;
  getdns_list *to_validate = getdns_list_create();
  getdns_list *support_records = getdns_list_create();

  if (to_validate == NULL || support_records == NULL)
    return GETDNS_RETURN_MEMORY_ERROR;

  /* Convert RR's in buf to dicts in a list. */
  if ((r = wire_rrs2list(buf, buf_len, &list)))
    goto out;

  /* First record MUST be the DS RR to validate. Second record MUST be
     an RRSIG covering the DS RR. Copy those to to_validate. */
  getdns_dict *ds_dict = NULL;
  getdns_dict *rrsig_ds_dict = NULL;
  uint32_t rrtype = 0;
  /* DS */
  if ((r = getdns_list_get_dict(list, 0, &ds_dict)))
    goto out;
  if ((r = getdns_dict_get_int(ds_dict, "type", &rrtype)))
    goto out;
  if (rrtype != GETDNS_RRTYPE_DS)
    {
      r = GETDNS_RETURN_INVALID_PARAMETER;
      goto out;
    }
  if ((r = getdns_list_set_dict(to_validate, 0, ds_dict)))
    goto out;
  /* RRSIG DS */
  if ((r = getdns_list_get_dict(list, 1, &rrsig_ds_dict)))
    goto out;
  if ((r = getdns_dict_get_int(rrsig_ds_dict, "type", &rrtype)))
    goto out;
  if (rrtype != GETDNS_RRTYPE_RRSIG)
    {
      r = GETDNS_RETURN_INVALID_PARAMETER;
      goto out;
    }
  if ((r = getdns_list_set_dict(to_validate, 1, rrsig_ds_dict)))
    goto out;

  /* The rest is "support records". Copy them to support_records. */
  size_t list_len;
  if ((r = getdns_list_get_length(list, &list_len)))
    goto out;
  for (int i = 2; i < list_len; i++)
    {
      getdns_dict *tmp_dict = NULL;
      if ((r = getdns_list_get_dict(list, i, &tmp_dict)))
        goto out;
      if ((r = getdns_list_set_dict(support_records, i - 2, tmp_dict)))
        goto out;
    }

  if (0 && debug)
    {
      print_tree(stderr, to_validate, "to_validate");
      print_tree(stderr, support_records, "support_records");
      print_tree(stderr, trust_anchors, "trust_anchors");
    }

  r = getdns_validate_dnssec2(to_validate,
                              support_records,
                              trust_anchors,
                              validation_time,
                              skew);

  if (out_buf && out_buf_len)
    {
      getdns_return_t r_save = r;
      size_t len = 0;
      *out_buf_len = 0;

      if ((r = list2wire(to_validate, out_buf, &len)))
        goto out;
      out_buf += len;
      *out_buf_len += len;

      if ((r = list2wire(support_records, out_buf, &len)))
        goto out;
      out_buf += len;
      *out_buf_len += len;

      if ((r = list2wire(trust_anchors, out_buf, &len)))
        goto out;
      out_buf += len;
      *out_buf_len += len;

      r = r_save;
    }

out:
  if (list)
    getdns_list_destroy(list);
  getdns_list_destroy(to_validate);
  getdns_list_destroy(support_records);
  return r;
}
#endif  /* !TEST */

#define DNSSEC_VALIDATION_SKEW 30 /* Seconds. */

static void
loop(getdns_list *trust_anchors)
{
  getdns_return_t r = GETDNS_RETURN_GENERIC_ERROR;
  unsigned char buf[64 * 1024]; /* FIXME */
  ssize_t len;

  while ((len = read_command(buf, sizeof(buf), 4)) > 0)
    {
      unsigned char reply[2 * 64 * 1024]; /* FIXME */
      size_t out_len = 0;

#if !defined(TEST)
      r = validate(buf, len, trust_anchors,
                   time(NULL), DNSSEC_VALIDATION_SKEW,
                   reply + 2, &out_len);
#else
      r = test_validate(buf, len, trust_anchors, testmode);
#endif

      if (debug)
        {
          int intr = r; /* GETDNS_DNSSEC_SECURE is not in enum getdns_return_t */
          switch (intr)
            {
            case GETDNS_DNSSEC_SECURE:
              fprintf(stderr, "validation successful\n");
              break;
            default:
              fprintf(stderr, "validation error %d (%s)\n",
                      r, getdns_get_errorstr_by_id(r));
            }
        }

      *((uint16_t *) reply) = htobe16(r);
      if (debug)
        fprintf(stderr, "writing %d octets of data, including status code %d\n",
                2 + out_len, r);
      if (write_reply(reply, 2 + out_len, 4))
        fprintf(stderr, "error writing reply\n");
    }
}


int
main(int argc, char *argv[])
{
  int c;
  getdns_list *trust_anchors = NULL;
  time_t trust_anchor_date;

  /* Parse command line. */
  while (1) {
    static struct option long_options[] = {
      {"testmode", required_argument, NULL, 't'},
      {0, 0, 0, 0}};

    c = getopt_long(argc, argv, "", long_options, NULL);
    if (c == -1)
      break;

    switch (c)
      {
#if defined(TEST)
      case 't':
        testmode = optarg;
        break;
#endif
      default:
        fprintf(stderr, "bad option: %s", argv[optind]);
        return -1;
      }
  }

  /* Read trust anchors file. */
  if (optind < argc)
    {
      int r = read_trust_anchors(argv[optind], &trust_anchors);
      if (r < 0)
        {
          perror("read trust anchors");
          return -r;
        }
      else if (r > 0)
        {
          fprintf(stderr,
                  "unable to read trust anchors file %s: %d (%s)",
                  argv[optind], r, getdns_get_errorstr_by_id(r));
          return r;
        }
    }
  else                         /* DEBUG: Using getdns trust anchor. */
    {
      trust_anchors = getdns_root_trust_anchor(&trust_anchor_date);
    }

  /* Eternal loop. */
  loop(trust_anchors);

  /* Not reached. */
  return 0;
}