dnsmasq: Backport some security updates
[librecmc/librecmc.git] / package / network / services / dnsmasq / patches / 0104-Use-SHA-256-to-provide-security-against-DNS-cache-po.patch
1 From 2d765867c597db18be9d876c9c17e2c0fe1953cd Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Thu, 12 Nov 2020 22:06:07 +0000
4 Subject: Use SHA-256 to provide security against DNS cache poisoning.
5
6 Use the SHA-256 hash function to verify that DNS answers
7 received are for the questions originally asked. This replaces
8 the slightly insecure SHA-1 (when compiled with DNSSEC) or
9 the very insecure CRC32 (otherwise). Refer: CERT VU#434904.
10 ---
11  CHANGELOG            |   5 +
12  Makefile             |   3 +-
13  bld/Android.mk       |   2 +-
14  src/dnsmasq.h        |  11 +-
15  src/dnssec.c         |  31 -----
16  src/forward.c        |  43 ++-----
17  src/hash_questions.c | 281 +++++++++++++++++++++++++++++++++++++++++++
18  src/rfc1035.c        |  49 --------
19  8 files changed, 301 insertions(+), 124 deletions(-)
20  create mode 100644 src/hash_questions.c
21
22 --- a/CHANGELOG
23 +++ b/CHANGELOG
24 @@ -7,6 +7,11 @@
25         in the {query-ID, random-port} tuple as possible, help defeat
26         cache poisoning attacks. Refer: CERT VU#434904.
27  
28 +       Use the SHA-256 hash function to verify that DNS answers
29 +       received are for the questions originally asked. This replaces
30 +       the slightly insecure SHA-1 (when compiled with DNSSEC) or
31 +       the very insecure CRC32 (otherwise). Refer: CERT VU#434904.
32 +       
33  
34  version 2.81
35         Impove cache behaviour for TCP connections. For ease of
36 --- a/Makefile
37 +++ b/Makefile
38 @@ -77,7 +77,8 @@ objs = cache.o rfc1035.o util.o option.o
39         helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
40         dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
41         domain.o dnssec.o blockdata.o tables.o loop.o inotify.o \
42 -       poll.o rrfilter.o edns0.o arp.o crypto.o dump.o ubus.o metrics.o
43 +       poll.o rrfilter.o edns0.o arp.o crypto.o dump.o ubus.o \
44 +       metrics.o hash_questions.o
45  
46  hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
47         dns-protocol.h radv-protocol.h ip6addr.h metrics.h
48 --- a/bld/Android.mk
49 +++ b/bld/Android.mk
50 @@ -11,7 +11,7 @@ LOCAL_SRC_FILES :=  bpf.c cache.c dbus.c
51                     radv.c slaac.c auth.c ipset.c domain.c \
52                     dnssec.c dnssec-openssl.c blockdata.c tables.c \
53                     loop.c inotify.c poll.c rrfilter.c edns0.c arp.c \
54 -                   crypto.c dump.c ubus.c
55 +                   crypto.c dump.c ubus.c metrics.c hash_questions.c
56  
57  LOCAL_MODULE := dnsmasq
58  
59 --- a/src/dnsmasq.h
60 +++ b/src/dnsmasq.h
61 @@ -644,11 +644,7 @@ struct hostsfile {
62  #define FREC_TEST_PKTSZ       256
63  #define FREC_HAS_EXTRADATA    512        
64  
65 -#ifdef HAVE_DNSSEC
66 -#define HASH_SIZE 20 /* SHA-1 digest size */
67 -#else
68 -#define HASH_SIZE sizeof(int)
69 -#endif
70 +#define HASH_SIZE 32 /* SHA-256 digest size */
71  
72  struct frec {
73    union mysockaddr source;
74 @@ -1199,7 +1195,6 @@ int check_for_bogus_wildcard(struct dns_
75                              struct bogus_addr *baddr, time_t now);
76  int check_for_ignored_address(struct dns_header *header, size_t qlen, struct bogus_addr *baddr);
77  int check_for_local_domain(char *name, time_t now);
78 -unsigned int questions_crc(struct dns_header *header, size_t plen, char *name);
79  size_t resize_packet(struct dns_header *header, size_t plen, 
80                   unsigned char *pheader, size_t hlen);
81  int add_resource_record(struct dns_header *header, char *limit, int *truncp,
82 @@ -1227,9 +1222,11 @@ int dnssec_validate_reply(time_t now, st
83                           int check_unsigned, int *neganswer, int *nons);
84  int dnskey_keytag(int alg, int flags, unsigned char *key, int keylen);
85  size_t filter_rrsigs(struct dns_header *header, size_t plen);
86 -unsigned char* hash_questions(struct dns_header *header, size_t plen, char *name);
87  int setup_timestamp(void);
88  
89 +/* hash_questions.c */
90 +unsigned char *hash_questions(struct dns_header *header, size_t plen, char *name);
91 +
92  /* crypto.c */
93  const struct nettle_hash *hash_find(char *name);
94  int hash_init(const struct nettle_hash *hash, void **ctxp, unsigned char **digestp);
95 --- a/src/dnssec.c
96 +++ b/src/dnssec.c
97 @@ -2082,35 +2082,4 @@ size_t dnssec_generate_query(struct dns_
98    return ret;
99  }
100  
101 -unsigned char* hash_questions(struct dns_header *header, size_t plen, char *name)
102 -{
103 -  int q;
104 -  unsigned int len;
105 -  unsigned char *p = (unsigned char *)(header+1);
106 -  const struct nettle_hash *hash;
107 -  void *ctx;
108 -  unsigned char *digest;
109 -  
110 -  if (!(hash = hash_find("sha1")) || !hash_init(hash, &ctx, &digest))
111 -    return NULL;
112 -  
113 -  for (q = ntohs(header->qdcount); q != 0; q--) 
114 -    {
115 -      if (!extract_name(header, plen, &p, name, 1, 4))
116 -       break; /* bad packet */
117 -      
118 -      len = to_wire(name);
119 -      hash->update(ctx, len, (unsigned char *)name);
120 -      /* CRC the class and type as well */
121 -      hash->update(ctx, 4, p);
122 -
123 -      p += 4;
124 -      if (!CHECK_LEN(header, p, plen, 0))
125 -       break; /* bad packet */
126 -    }
127 -  
128 -  hash->digest(ctx, hash->digest_size, digest);
129 -  return digest;
130 -}
131 -
132  #endif /* HAVE_DNSSEC */
133 --- a/src/forward.c
134 +++ b/src/forward.c
135 @@ -248,19 +248,16 @@ static int forward_query(int udpfd, unio
136    union all_addr *addrp = NULL;
137    unsigned int flags = 0;
138    struct server *start = NULL;
139 -#ifdef HAVE_DNSSEC
140    void *hash = hash_questions(header, plen, daemon->namebuff);
141 +#ifdef HAVE_DNSSEC
142    int do_dnssec = 0;
143 -#else
144 -  unsigned int crc = questions_crc(header, plen, daemon->namebuff);
145 -  void *hash = &crc;
146  #endif
147    unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
148    unsigned char *oph = find_pseudoheader(header, plen, NULL, NULL, NULL, NULL);
149    (void)do_bit;
150  
151    /* may be no servers available. */
152 -  if (forward || (hash && (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash))))
153 +  if (forward || (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash)))
154      {
155        /* If we didn't get an answer advertising a maximal packet in EDNS,
156          fall back to 1280, which should work everywhere on IPv6.
157 @@ -761,9 +758,6 @@ void reply_query(int fd, int family, tim
158    size_t nn;
159    struct server *server;
160    void *hash;
161 -#ifndef HAVE_DNSSEC
162 -  unsigned int crc;
163 -#endif
164  
165    /* packet buffer overwritten */
166    daemon->srv_save = NULL;
167 @@ -790,12 +784,7 @@ void reply_query(int fd, int family, tim
168    if (difftime(now, server->pktsz_reduced) > UDP_TEST_TIME)
169      server->edns_pktsz = daemon->edns_pktsz;
170  
171 -#ifdef HAVE_DNSSEC
172    hash = hash_questions(header, n, daemon->namebuff);
173 -#else
174 -  hash = &crc;
175 -  crc = questions_crc(header, n, daemon->namebuff);
176 -#endif
177    
178    if (!(forward = lookup_frec(ntohs(header->id), fd, family, hash)))
179      return;
180 @@ -1100,8 +1089,7 @@ void reply_query(int fd, int family, tim
181                         log_query(F_NOEXTRA | F_DNSSEC | F_IPV6, daemon->keyname, (union all_addr *)&(server->addr.in6.sin6_addr),
182                                   querystr("dnssec-query", querytype));
183    
184 -                     if ((hash = hash_questions(header, nn, daemon->namebuff)))
185 -                       memcpy(new->hash, hash, HASH_SIZE);
186 +                     memcpy(new->hash, hash_questions(header, nn, daemon->namebuff), HASH_SIZE);
187                       new->new_id = get_id();
188                       header->id = htons(new->new_id);
189                       /* Save query for retransmission */
190 @@ -1937,15 +1925,9 @@ unsigned char *tcp_request(int confd, ti
191               if (!flags && last_server)
192                 {
193                   struct server *firstsendto = NULL;
194 -#ifdef HAVE_DNSSEC
195 -                 unsigned char *newhash, hash[HASH_SIZE];
196 -                 if ((newhash = hash_questions(header, (unsigned int)size, daemon->namebuff)))
197 -                   memcpy(hash, newhash, HASH_SIZE);
198 -                 else
199 -                   memset(hash, 0, HASH_SIZE);
200 -#else
201 -                 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
202 -#endif           
203 +                 unsigned char hash[HASH_SIZE];
204 +                 memcpy(hash, hash_questions(header, (unsigned int)size, daemon->namebuff), HASH_SIZE);
205 +
206                   /* Loop round available servers until we succeed in connecting to one.
207                      Note that this code subtly ensures that consecutive queries on this connection
208                      which can go to the same server, do so. */
209 @@ -2068,20 +2050,11 @@ unsigned char *tcp_request(int confd, ti
210                       /* If the crc of the question section doesn't match the crc we sent, then
211                          someone might be attempting to insert bogus values into the cache by 
212                          sending replies containing questions and bogus answers. */
213 -#ifdef HAVE_DNSSEC
214 -                     newhash = hash_questions(header, (unsigned int)m, daemon->namebuff);
215 -                     if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0)
216 +                     if (memcmp(hash, hash_questions(header, (unsigned int)m, daemon->namebuff), HASH_SIZE) != 0)
217                         { 
218                           m = 0;
219                           break;
220                         }
221 -#else                    
222 -                     if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff))
223 -                       {
224 -                         m = 0;
225 -                         break;
226 -                       }
227 -#endif
228  
229                       m = process_reply(header, now, last_server, (unsigned int)m, 
230                                         option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, cache_secure, bogusanswer,
231 @@ -2295,7 +2268,7 @@ static struct frec *lookup_frec(unsigned
232  
233    for(f = daemon->frec_list; f; f = f->next)
234      if (f->sentto && f->new_id == id && 
235 -       (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0))
236 +       (memcmp(hash, f->hash, HASH_SIZE) == 0))
237        {
238         /* sent from random port */
239         if (family == AF_INET && f->rfd4 && f->rfd4->fd == fd)
240 --- /dev/null
241 +++ b/src/hash_questions.c
242 @@ -0,0 +1,281 @@
243 +/* Copyright (c) 2012-2020 Simon Kelley
244 +
245 +   This program is free software; you can redistribute it and/or modify
246 +   it under the terms of the GNU General Public License as published by
247 +   the Free Software Foundation; version 2 dated June, 1991, or
248 +   (at your option) version 3 dated 29 June, 2007.
249 +
250 +   This program is distributed in the hope that it will be useful,
251 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
252 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
253 +   GNU General Public License for more details.
254 +
255 +   You should have received a copy of the GNU General Public License
256 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.
257 +*/
258 +
259 +
260 +/* Hash the question section. This is used to safely detect query 
261 +   retransmission and to detect answers to questions we didn't ask, which 
262 +   might be poisoning attacks. Note that we decode the name rather 
263 +   than CRC the raw bytes, since replies might be compressed differently. 
264 +   We ignore case in the names for the same reason. 
265 +
266 +   The hash used is SHA-256. If we're building with DNSSEC support,
267 +   we use the Nettle cypto library. If not, we prefer not to
268 +   add a dependency on Nettle, and use a stand-alone implementaion. 
269 +*/
270 +
271 +#include "dnsmasq.h"
272 +
273 +#ifdef HAVE_DNSSEC
274 +unsigned char *hash_questions(struct dns_header *header, size_t plen, char *name)
275 +{
276 +  int q;
277 +  unsigned char *p = (unsigned char *)(header+1);
278 +  const struct nettle_hash *hash;
279 +  void *ctx;
280 +  unsigned char *digest;
281 +  
282 +  if (!(hash = hash_find("sha256")) || !hash_init(hash, &ctx, &digest))
283 +    {
284 +      /* don't think this can ever happen. */
285 +      static unsigned char dummy[HASH_SIZE];
286 +      static int warned = 0;
287 +
288 +      if (warned)
289 +       my_syslog(LOG_ERR, _("Failed to create SHA-256 hash object"));
290 +      warned = 1;
291 +     
292 +      return dummy;
293 +    }
294 +  
295 +  for (q = ntohs(header->qdcount); q != 0; q--) 
296 +    {
297 +      char *cp, c;
298 +
299 +      if (!extract_name(header, plen, &p, name, 1, 4))
300 +       break; /* bad packet */
301 +
302 +      for (cp = name; (c = *cp); cp++)
303 +        if (c >= 'A' && c <= 'Z')
304 +          *cp += 'a' - 'A';
305 +
306 +      hash->update(ctx, cp - name, (unsigned char *)name);
307 +      /* CRC the class and type as well */
308 +      hash->update(ctx, 4, p);
309 +
310 +      p += 4;
311 +      if (!CHECK_LEN(header, p, plen, 0))
312 +       break; /* bad packet */
313 +    }
314 +  
315 +  hash->digest(ctx, hash->digest_size, digest);
316 +  return digest;
317 +}
318 +
319 +#else /* HAVE_DNSSEC */
320 +
321 +#define SHA256_BLOCK_SIZE 32            // SHA256 outputs a 32 byte digest
322 +typedef unsigned char BYTE;             // 8-bit byte
323 +typedef unsigned int  WORD;             // 32-bit word, change to "long" for 16-bit machines
324 +
325 +typedef struct {
326 +  BYTE data[64];
327 +  WORD datalen;
328 +  unsigned long long bitlen;
329 +  WORD state[8];
330 +} SHA256_CTX;
331 +
332 +static void sha256_init(SHA256_CTX *ctx);
333 +static void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
334 +static void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
335 +
336 +
337 +unsigned char *hash_questions(struct dns_header *header, size_t plen, char *name)
338 +{
339 +  int q;
340 +  unsigned char *p = (unsigned char *)(header+1);
341 +  SHA256_CTX ctx;
342 +  static BYTE digest[SHA256_BLOCK_SIZE];
343 +  
344 +  sha256_init(&ctx);
345 +    
346 +  for (q = ntohs(header->qdcount); q != 0; q--) 
347 +    {
348 +      char *cp, c;
349 +
350 +      if (!extract_name(header, plen, &p, name, 1, 4))
351 +       break; /* bad packet */
352 +
353 +      for (cp = name; (c = *cp); cp++)
354 +        if (c >= 'A' && c <= 'Z')
355 +          *cp += 'a' - 'A';
356 +
357 +      sha256_update(&ctx, (BYTE *)name, cp - name);
358 +      /* CRC the class and type as well */
359 +      sha256_update(&ctx, (BYTE *)p, 4);
360 +
361 +      p += 4;
362 +      if (!CHECK_LEN(header, p, plen, 0))
363 +       break; /* bad packet */
364 +    }
365 +  
366 +  sha256_final(&ctx, digest);
367 +  return (unsigned char *)digest;
368 +}
369 +
370 +/* Code from here onwards comes from https://github.com/B-Con/crypto-algorithms
371 +   and was written by Brad Conte (brad@bradconte.com), to whom all credit is given.
372 +
373 +   This code is in the public domain, and the copyright notice at the head of this 
374 +   file does not apply to it.
375 +*/
376 +
377 +
378 +/****************************** MACROS ******************************/
379 +#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
380 +#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
381 +
382 +#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
383 +#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
384 +#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
385 +#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
386 +#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
387 +#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
388 +
389 +/**************************** VARIABLES *****************************/
390 +static const WORD k[64] = {
391 +                          0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
392 +                          0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
393 +                          0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
394 +                          0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
395 +                          0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
396 +                          0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
397 +                          0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
398 +                          0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
399 +};
400 +
401 +/*********************** FUNCTION DEFINITIONS ***********************/
402 +static void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
403 +{
404 +  WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
405 +  
406 +  for (i = 0, j = 0; i < 16; ++i, j += 4)
407 +    m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
408 +  for ( ; i < 64; ++i)
409 +    m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
410 +
411 +  a = ctx->state[0];
412 +  b = ctx->state[1];
413 +  c = ctx->state[2];
414 +  d = ctx->state[3];
415 +  e = ctx->state[4];
416 +  f = ctx->state[5];
417 +  g = ctx->state[6];
418 +  h = ctx->state[7];
419 +
420 +  for (i = 0; i < 64; ++i)
421 +    {
422 +      t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
423 +      t2 = EP0(a) + MAJ(a,b,c);
424 +      h = g;
425 +      g = f;
426 +      f = e;
427 +      e = d + t1;
428 +      d = c;
429 +      c = b;
430 +      b = a;
431 +      a = t1 + t2;
432 +    }
433 +  
434 +  ctx->state[0] += a;
435 +  ctx->state[1] += b;
436 +  ctx->state[2] += c;
437 +  ctx->state[3] += d;
438 +  ctx->state[4] += e;
439 +  ctx->state[5] += f;
440 +  ctx->state[6] += g;
441 +  ctx->state[7] += h;
442 +}
443 +
444 +static void sha256_init(SHA256_CTX *ctx)
445 +{
446 +  ctx->datalen = 0;
447 +  ctx->bitlen = 0;
448 +  ctx->state[0] = 0x6a09e667;
449 +  ctx->state[1] = 0xbb67ae85;
450 +  ctx->state[2] = 0x3c6ef372;
451 +  ctx->state[3] = 0xa54ff53a;
452 +  ctx->state[4] = 0x510e527f;
453 +  ctx->state[5] = 0x9b05688c;
454 +  ctx->state[6] = 0x1f83d9ab;
455 +  ctx->state[7] = 0x5be0cd19;
456 +}
457 +
458 +static void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
459 +{
460 +  WORD i;
461 +  
462 +  for (i = 0; i < len; ++i)
463 +    {
464 +      ctx->data[ctx->datalen] = data[i];
465 +      ctx->datalen++;
466 +      if (ctx->datalen == 64) {
467 +       sha256_transform(ctx, ctx->data);
468 +       ctx->bitlen += 512;
469 +       ctx->datalen = 0;
470 +      }
471 +    }
472 +}
473 +
474 +static void sha256_final(SHA256_CTX *ctx, BYTE hash[])
475 +{
476 +  WORD i;
477 +  
478 +  i = ctx->datalen;
479 +
480 +  // Pad whatever data is left in the buffer.
481 +  if (ctx->datalen < 56)
482 +    {
483 +      ctx->data[i++] = 0x80;
484 +      while (i < 56)
485 +       ctx->data[i++] = 0x00;
486 +    }
487 +  else
488 +    {
489 +      ctx->data[i++] = 0x80;
490 +      while (i < 64)
491 +       ctx->data[i++] = 0x00;
492 +      sha256_transform(ctx, ctx->data);
493 +      memset(ctx->data, 0, 56);
494 +    }
495 +  
496 +  // Append to the padding the total message's length in bits and transform.
497 +  ctx->bitlen += ctx->datalen * 8;
498 +  ctx->data[63] = ctx->bitlen;
499 +  ctx->data[62] = ctx->bitlen >> 8;
500 +  ctx->data[61] = ctx->bitlen >> 16;
501 +  ctx->data[60] = ctx->bitlen >> 24;
502 +  ctx->data[59] = ctx->bitlen >> 32;
503 +  ctx->data[58] = ctx->bitlen >> 40;
504 +  ctx->data[57] = ctx->bitlen >> 48;
505 +  ctx->data[56] = ctx->bitlen >> 56;
506 +  sha256_transform(ctx, ctx->data);
507 +  
508 +  // Since this implementation uses little endian byte ordering and SHA uses big endian,
509 +  // reverse all the bytes when copying the final state to the output hash.
510 +  for (i = 0; i < 4; ++i)
511 +    {
512 +      hash[i]      = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
513 +      hash[i + 4]  = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
514 +      hash[i + 8]  = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
515 +      hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
516 +      hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
517 +      hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
518 +      hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
519 +      hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
520 +    }
521 +}
522 +
523 +#endif
524 --- a/src/rfc1035.c
525 +++ b/src/rfc1035.c
526 @@ -333,55 +333,6 @@ unsigned char *skip_section(unsigned cha
527    return ansp;
528  }
529  
530 -/* CRC the question section. This is used to safely detect query 
531 -   retransmission and to detect answers to questions we didn't ask, which 
532 -   might be poisoning attacks. Note that we decode the name rather 
533 -   than CRC the raw bytes, since replies might be compressed differently. 
534 -   We ignore case in the names for the same reason. Return all-ones
535 -   if there is not question section. */
536 -#ifndef HAVE_DNSSEC
537 -unsigned int questions_crc(struct dns_header *header, size_t plen, char *name)
538 -{
539 -  int q;
540 -  unsigned int crc = 0xffffffff;
541 -  unsigned char *p1, *p = (unsigned char *)(header+1);
542 -
543 -  for (q = ntohs(header->qdcount); q != 0; q--) 
544 -    {
545 -      if (!extract_name(header, plen, &p, name, 1, 4))
546 -       return crc; /* bad packet */
547 -      
548 -      for (p1 = (unsigned char *)name; *p1; p1++)
549 -       {
550 -         int i = 8;
551 -         char c = *p1;
552 -
553 -         if (c >= 'A' && c <= 'Z')
554 -           c += 'a' - 'A';
555 -
556 -         crc ^= c << 24;
557 -         while (i--)
558 -           crc = crc & 0x80000000 ? (crc << 1) ^ 0x04c11db7 : crc << 1;
559 -       }
560 -      
561 -      /* CRC the class and type as well */
562 -      for (p1 = p; p1 < p+4; p1++)
563 -       {
564 -         int i = 8;
565 -         crc ^= *p1 << 24;
566 -         while (i--)
567 -           crc = crc & 0x80000000 ? (crc << 1) ^ 0x04c11db7 : crc << 1;
568 -       }
569 -
570 -      p += 4;
571 -      if (!CHECK_LEN(header, p, plen, 0))
572 -       return crc; /* bad packet */
573 -    }
574 -
575 -  return crc;
576 -}
577 -#endif
578 -
579  size_t resize_packet(struct dns_header *header, size_t plen, unsigned char *pheader, size_t hlen)
580  {
581    unsigned char *ansp = skip_questions(header, plen);