Merge from vendor branch GPERF:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / nameser / ns_sign.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1999 by Internet Software Consortium, Inc.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_sign.c,v 1.1.2.3 2004/03/09 09:17:37 marka Exp $";
20 #endif
21
22 /* Import. */
23
24 #include "port_before.h"
25 #include "fd_setsize.h"
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
33
34 #include <errno.h>
35 #include <netdb.h>
36 #include <resolv.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <unistd.h>
42
43 #include <isc/dst.h>
44 #include <isc/assertions.h>
45
46 #include "port_after.h"
47
48 #define BOUNDS_CHECK(ptr, count) \
49         do { \
50                 if ((ptr) + (count) > eob) { \
51                         errno = EMSGSIZE; \
52                         return(NS_TSIG_ERROR_NO_SPACE); \
53                 } \
54         } while (0)
55
56 /* ns_sign
57  * Parameters:
58  *      msg             message to be sent
59  *      msglen          input - length of message
60  *                      output - length of signed message
61  *      msgsize         length of buffer containing message
62  *      error           value to put in the error field
63  *      key             tsig key used for signing
64  *      querysig        (response), the signature in the query
65  *      querysiglen     (response), the length of the signature in the query
66  *      sig             a buffer to hold the generated signature
67  *      siglen          input - length of signature buffer
68  *                      output - length of signature
69  *
70  * Errors:
71  *      - bad input data (-1)
72  *      - bad key / sign failed (-BADKEY)
73  *      - not enough space (NS_TSIG_ERROR_NO_SPACE)
74  */
75 int
76 ns_sign(u_char *msg, int *msglen, int msgsize, int error, void *k,
77         const u_char *querysig, int querysiglen, u_char *sig, int *siglen,
78         time_t in_timesigned)
79 {
80         return(ns_sign2(msg, msglen, msgsize, error, k,
81                         querysig, querysiglen, sig, siglen,
82                         in_timesigned, NULL, NULL));
83 }
84
85 int
86 ns_sign2(u_char *msg, int *msglen, int msgsize, int error, void *k,
87          const u_char *querysig, int querysiglen, u_char *sig, int *siglen,
88          time_t in_timesigned, u_char **dnptrs, u_char **lastdnptr)
89 {
90         HEADER *hp = (HEADER *)msg;
91         DST_KEY *key = (DST_KEY *)k;
92         u_char *cp = msg + *msglen, *eob = msg + msgsize;
93         u_char *lenp;
94         u_char *alg;
95         int n;
96         time_t timesigned;
97         u_char name[NS_MAXCDNAME];
98
99         dst_init();
100         if (msg == NULL || msglen == NULL || sig == NULL || siglen == NULL)
101                 return (-1);
102
103         /* Name. */
104         if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
105                 n = ns_name_pton(key->dk_key_name, name, sizeof name);
106                 if (n != -1)
107                         n = ns_name_pack(name, cp, eob - cp,
108                                          (const u_char **)dnptrs,
109                                          (const u_char **)lastdnptr);
110
111         } else {
112                 n = ns_name_pton("", name, sizeof name);
113                 if (n != -1)
114                         n = ns_name_pack(name, cp, eob - cp, NULL, NULL);
115         }
116         if (n < 0)
117                 return (NS_TSIG_ERROR_NO_SPACE);
118         cp += n;
119
120         /* Type, class, ttl, length (not filled in yet). */
121         BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
122         PUTSHORT(ns_t_tsig, cp);
123         PUTSHORT(ns_c_any, cp);
124         PUTLONG(0, cp);         /* TTL */
125         lenp = cp;
126         cp += 2;
127
128         /* Alg. */
129         if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
130                 if (key->dk_alg != KEY_HMAC_MD5)
131                         return (-ns_r_badkey);
132                 n = dn_comp(NS_TSIG_ALG_HMAC_MD5, cp, eob - cp, NULL, NULL);
133         }
134         else
135                 n = dn_comp("", cp, eob - cp, NULL, NULL);
136         if (n < 0)
137                 return (NS_TSIG_ERROR_NO_SPACE);
138         alg = cp;
139         cp += n;
140         
141         /* Time. */
142         BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
143         PUTSHORT(0, cp);
144         timesigned = time(NULL);
145         if (error != ns_r_badtime)
146                 PUTLONG(timesigned, cp);
147         else
148                 PUTLONG(in_timesigned, cp);
149         PUTSHORT(NS_TSIG_FUDGE, cp);
150
151         /* Compute the signature. */
152         if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
153                 void *ctx;
154                 u_char buf[NS_MAXCDNAME], *cp2;
155                 int n;
156
157                 dst_sign_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
158
159                 /* Digest the query signature, if this is a response. */
160                 if (querysiglen > 0 && querysig != NULL) {
161                         u_int16_t len_n = htons(querysiglen);
162                         dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
163                                       (u_char *)&len_n, INT16SZ, NULL, 0);
164                         dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
165                                       querysig, querysiglen, NULL, 0);
166                 }
167
168                 /* Digest the message. */
169                 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, msg, *msglen,
170                               NULL, 0);
171
172                 /* Digest the key name. */
173                 n = ns_name_ntol(name, buf, sizeof(buf));
174                 INSIST(n > 0);
175                 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
176
177                 /* Digest the class and TTL. */
178                 cp2 = buf;
179                 PUTSHORT(ns_c_any, cp2);
180                 PUTLONG(0, cp2);
181                 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
182                               NULL, 0);
183
184                 /* Digest the algorithm. */
185                 n = ns_name_ntol(alg, buf, sizeof(buf));
186                 INSIST(n > 0);
187                 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
188
189                 /* Digest the time signed, fudge, error, and other data */
190                 cp2 = buf;
191                 PUTSHORT(0, cp2);       /* Top 16 bits of time */
192                 if (error != ns_r_badtime)
193                         PUTLONG(timesigned, cp2);
194                 else
195                         PUTLONG(in_timesigned, cp2);
196                 PUTSHORT(NS_TSIG_FUDGE, cp2);
197                 PUTSHORT(error, cp2);   /* Error */
198                 if (error != ns_r_badtime)
199                         PUTSHORT(0, cp2);       /* Other data length */
200                 else {
201                         PUTSHORT(INT16SZ+INT32SZ, cp2); /* Other data length */
202                         PUTSHORT(0, cp2);       /* Top 16 bits of time */
203                         PUTLONG(timesigned, cp2);
204                 }
205                 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
206                               NULL, 0);
207
208                 n = dst_sign_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
209                                   sig, *siglen);
210                 if (n < 0)
211                         return (-ns_r_badkey);
212                 *siglen = n;
213         } else
214                 *siglen = 0;
215
216         /* Add the signature. */
217         BOUNDS_CHECK(cp, INT16SZ + (*siglen));
218         PUTSHORT(*siglen, cp);
219         memcpy(cp, sig, *siglen);
220         cp += (*siglen);
221
222         /* The original message ID & error. */
223         BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
224         PUTSHORT(ntohs(hp->id), cp);    /* already in network order */
225         PUTSHORT(error, cp);
226
227         /* Other data. */
228         BOUNDS_CHECK(cp, INT16SZ);
229         if (error != ns_r_badtime)
230                 PUTSHORT(0, cp);        /* Other data length */
231         else {
232                 PUTSHORT(INT16SZ+INT32SZ, cp);  /* Other data length */
233                 BOUNDS_CHECK(cp, INT32SZ+INT16SZ);
234                 PUTSHORT(0, cp);        /* Top 16 bits of time */
235                 PUTLONG(timesigned, cp);
236         }
237
238         /* Go back and fill in the length. */
239         PUTSHORT(cp - lenp - INT16SZ, lenp);
240
241         hp->arcount = htons(ntohs(hp->arcount) + 1);
242         *msglen = (cp - msg);
243         return (0);
244 }
245
246 int
247 ns_sign_tcp_init(void *k, const u_char *querysig, int querysiglen,
248                  ns_tcp_tsig_state *state)
249 {
250         dst_init();
251         if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
252                 return (-1);
253         state->counter = -1;
254         state->key = k;
255         if (state->key->dk_alg != KEY_HMAC_MD5)
256                 return (-ns_r_badkey);
257         if (querysiglen > (int)sizeof(state->sig))
258                 return (-1);
259         memcpy(state->sig, querysig, querysiglen);
260         state->siglen = querysiglen;
261         return (0);
262 }
263
264 int
265 ns_sign_tcp(u_char *msg, int *msglen, int msgsize, int error,
266             ns_tcp_tsig_state *state, int done)
267 {
268         return (ns_sign_tcp2(msg, msglen, msgsize, error, state,
269                              done, NULL, NULL));
270 }
271
272 int
273 ns_sign_tcp2(u_char *msg, int *msglen, int msgsize, int error,
274              ns_tcp_tsig_state *state, int done,
275              u_char **dnptrs, u_char **lastdnptr)
276 {
277         u_char *cp, *eob, *lenp;
278         u_char buf[MAXDNAME], *cp2;
279         HEADER *hp = (HEADER *)msg;
280         time_t timesigned;
281         int n;
282
283         if (msg == NULL || msglen == NULL || state == NULL)
284                 return (-1);
285
286         state->counter++;
287         if (state->counter == 0)
288                 return (ns_sign2(msg, msglen, msgsize, error, state->key,
289                                  state->sig, state->siglen,
290                                  state->sig, &state->siglen, 0,
291                                  dnptrs, lastdnptr));
292
293         if (state->siglen > 0) {
294                 u_int16_t siglen_n = htons(state->siglen);
295                 dst_sign_data(SIG_MODE_INIT, state->key, &state->ctx,
296                               NULL, 0, NULL, 0);
297                 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
298                               (u_char *)&siglen_n, INT16SZ, NULL, 0);
299                 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
300                               state->sig, state->siglen, NULL, 0);
301                 state->siglen = 0;
302         }
303
304         dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen,
305                       NULL, 0);
306
307         if (done == 0 && (state->counter % 100 != 0))
308                 return (0);
309
310         cp = msg + *msglen;
311         eob = msg + msgsize;
312
313         /* Name. */
314         n = dn_comp(state->key->dk_key_name, cp, eob - cp, dnptrs, lastdnptr);
315         if (n < 0)
316                 return (NS_TSIG_ERROR_NO_SPACE);
317         cp += n;
318
319         /* Type, class, ttl, length (not filled in yet). */
320         BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
321         PUTSHORT(ns_t_tsig, cp);
322         PUTSHORT(ns_c_any, cp);
323         PUTLONG(0, cp);         /* TTL */
324         lenp = cp;
325         cp += 2;
326
327         /* Alg. */
328         n = dn_comp(NS_TSIG_ALG_HMAC_MD5, cp, eob - cp, NULL, NULL);
329         if (n < 0)
330                 return (NS_TSIG_ERROR_NO_SPACE);
331         cp += n;
332         
333         /* Time. */
334         BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
335         PUTSHORT(0, cp);
336         timesigned = time(NULL);
337         PUTLONG(timesigned, cp);
338         PUTSHORT(NS_TSIG_FUDGE, cp);
339
340         /*
341          * Compute the signature.
342          */
343
344         /* Digest the time signed and fudge. */
345         cp2 = buf;
346         PUTSHORT(0, cp2);       /* Top 16 bits of time */
347         PUTLONG(timesigned, cp2);
348         PUTSHORT(NS_TSIG_FUDGE, cp2);
349
350         dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
351                       buf, cp2 - buf, NULL, 0);
352
353         n = dst_sign_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
354                           state->sig, sizeof(state->sig));
355         if (n < 0)
356                 return (-ns_r_badkey);
357         state->siglen = n;
358
359         /* Add the signature. */
360         BOUNDS_CHECK(cp, INT16SZ + state->siglen);
361         PUTSHORT(state->siglen, cp);
362         memcpy(cp, state->sig, state->siglen);
363         cp += state->siglen;
364
365         /* The original message ID & error. */
366         BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
367         PUTSHORT(ntohs(hp->id), cp);    /* already in network order */
368         PUTSHORT(error, cp);
369
370         /* Other data. */
371         BOUNDS_CHECK(cp, INT16SZ);
372         PUTSHORT(0, cp);
373
374         /* Go back and fill in the length. */
375         PUTSHORT(cp - lenp - INT16SZ, lenp);
376
377         hp->arcount = htons(ntohs(hp->arcount) + 1);
378         *msglen = (cp - msg);
379         return (0);
380 }