Import bind-9.3.4
[dragonfly.git] / contrib / bind-9.3 / lib / bind / nameser / ns_sign.c
blob7b742f1f5f1791b4a6080307974953686346a24b
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1999 by Internet Software Consortium, Inc.
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.
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.
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_sign.c,v 1.1.2.2.4.2 2006/03/10 00:17:21 marka Exp $";
20 #endif
22 /* Import. */
24 #include "port_before.h"
25 #include "fd_setsize.h"
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
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>
43 #include <isc/dst.h>
44 #include <isc/assertions.h>
46 #include "port_after.h"
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)
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
70 * Errors:
71 * - bad input data (-1)
72 * - bad key / sign failed (-BADKEY)
73 * - not enough space (NS_TSIG_ERROR_NO_SPACE)
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)
80 return(ns_sign2(msg, msglen, msgsize, error, k,
81 querysig, querysiglen, sig, siglen,
82 in_timesigned, NULL, NULL));
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)
90 HEADER *hp = (HEADER *)msg;
91 DST_KEY *key = (DST_KEY *)k;
92 u_char *cp, *eob;
93 u_char *lenp;
94 u_char *alg;
95 int n;
96 time_t timesigned;
97 u_char name[NS_MAXCDNAME];
99 dst_init();
100 if (msg == NULL || msglen == NULL || sig == NULL || siglen == NULL)
101 return (-1);
103 cp = msg + *msglen;
104 eob = msg + msgsize;
106 /* Name. */
107 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
108 n = ns_name_pton(key->dk_key_name, name, sizeof name);
109 if (n != -1)
110 n = ns_name_pack(name, cp, eob - cp,
111 (const u_char **)dnptrs,
112 (const u_char **)lastdnptr);
114 } else {
115 n = ns_name_pton("", name, sizeof name);
116 if (n != -1)
117 n = ns_name_pack(name, cp, eob - cp, NULL, NULL);
119 if (n < 0)
120 return (NS_TSIG_ERROR_NO_SPACE);
121 cp += n;
123 /* Type, class, ttl, length (not filled in yet). */
124 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
125 PUTSHORT(ns_t_tsig, cp);
126 PUTSHORT(ns_c_any, cp);
127 PUTLONG(0, cp); /* TTL */
128 lenp = cp;
129 cp += 2;
131 /* Alg. */
132 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
133 if (key->dk_alg != KEY_HMAC_MD5)
134 return (-ns_r_badkey);
135 n = dn_comp(NS_TSIG_ALG_HMAC_MD5, cp, eob - cp, NULL, NULL);
137 else
138 n = dn_comp("", cp, eob - cp, NULL, NULL);
139 if (n < 0)
140 return (NS_TSIG_ERROR_NO_SPACE);
141 alg = cp;
142 cp += n;
144 /* Time. */
145 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
146 PUTSHORT(0, cp);
147 timesigned = time(NULL);
148 if (error != ns_r_badtime)
149 PUTLONG(timesigned, cp);
150 else
151 PUTLONG(in_timesigned, cp);
152 PUTSHORT(NS_TSIG_FUDGE, cp);
154 /* Compute the signature. */
155 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
156 void *ctx;
157 u_char buf[NS_MAXCDNAME], *cp2;
158 int n;
160 dst_sign_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
162 /* Digest the query signature, if this is a response. */
163 if (querysiglen > 0 && querysig != NULL) {
164 u_int16_t len_n = htons(querysiglen);
165 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
166 (u_char *)&len_n, INT16SZ, NULL, 0);
167 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
168 querysig, querysiglen, NULL, 0);
171 /* Digest the message. */
172 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, msg, *msglen,
173 NULL, 0);
175 /* Digest the key name. */
176 n = ns_name_ntol(name, buf, sizeof(buf));
177 INSIST(n > 0);
178 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
180 /* Digest the class and TTL. */
181 cp2 = buf;
182 PUTSHORT(ns_c_any, cp2);
183 PUTLONG(0, cp2);
184 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
185 NULL, 0);
187 /* Digest the algorithm. */
188 n = ns_name_ntol(alg, buf, sizeof(buf));
189 INSIST(n > 0);
190 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
192 /* Digest the time signed, fudge, error, and other data */
193 cp2 = buf;
194 PUTSHORT(0, cp2); /* Top 16 bits of time */
195 if (error != ns_r_badtime)
196 PUTLONG(timesigned, cp2);
197 else
198 PUTLONG(in_timesigned, cp2);
199 PUTSHORT(NS_TSIG_FUDGE, cp2);
200 PUTSHORT(error, cp2); /* Error */
201 if (error != ns_r_badtime)
202 PUTSHORT(0, cp2); /* Other data length */
203 else {
204 PUTSHORT(INT16SZ+INT32SZ, cp2); /* Other data length */
205 PUTSHORT(0, cp2); /* Top 16 bits of time */
206 PUTLONG(timesigned, cp2);
208 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
209 NULL, 0);
211 n = dst_sign_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
212 sig, *siglen);
213 if (n < 0)
214 return (-ns_r_badkey);
215 *siglen = n;
216 } else
217 *siglen = 0;
219 /* Add the signature. */
220 BOUNDS_CHECK(cp, INT16SZ + (*siglen));
221 PUTSHORT(*siglen, cp);
222 memcpy(cp, sig, *siglen);
223 cp += (*siglen);
225 /* The original message ID & error. */
226 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
227 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
228 PUTSHORT(error, cp);
230 /* Other data. */
231 BOUNDS_CHECK(cp, INT16SZ);
232 if (error != ns_r_badtime)
233 PUTSHORT(0, cp); /* Other data length */
234 else {
235 PUTSHORT(INT16SZ+INT32SZ, cp); /* Other data length */
236 BOUNDS_CHECK(cp, INT32SZ+INT16SZ);
237 PUTSHORT(0, cp); /* Top 16 bits of time */
238 PUTLONG(timesigned, cp);
241 /* Go back and fill in the length. */
242 PUTSHORT(cp - lenp - INT16SZ, lenp);
244 hp->arcount = htons(ntohs(hp->arcount) + 1);
245 *msglen = (cp - msg);
246 return (0);
250 ns_sign_tcp_init(void *k, const u_char *querysig, int querysiglen,
251 ns_tcp_tsig_state *state)
253 dst_init();
254 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
255 return (-1);
256 state->counter = -1;
257 state->key = k;
258 if (state->key->dk_alg != KEY_HMAC_MD5)
259 return (-ns_r_badkey);
260 if (querysiglen > (int)sizeof(state->sig))
261 return (-1);
262 memcpy(state->sig, querysig, querysiglen);
263 state->siglen = querysiglen;
264 return (0);
268 ns_sign_tcp(u_char *msg, int *msglen, int msgsize, int error,
269 ns_tcp_tsig_state *state, int done)
271 return (ns_sign_tcp2(msg, msglen, msgsize, error, state,
272 done, NULL, NULL));
276 ns_sign_tcp2(u_char *msg, int *msglen, int msgsize, int error,
277 ns_tcp_tsig_state *state, int done,
278 u_char **dnptrs, u_char **lastdnptr)
280 u_char *cp, *eob, *lenp;
281 u_char buf[MAXDNAME], *cp2;
282 HEADER *hp = (HEADER *)msg;
283 time_t timesigned;
284 int n;
286 if (msg == NULL || msglen == NULL || state == NULL)
287 return (-1);
289 state->counter++;
290 if (state->counter == 0)
291 return (ns_sign2(msg, msglen, msgsize, error, state->key,
292 state->sig, state->siglen,
293 state->sig, &state->siglen, 0,
294 dnptrs, lastdnptr));
296 if (state->siglen > 0) {
297 u_int16_t siglen_n = htons(state->siglen);
298 dst_sign_data(SIG_MODE_INIT, state->key, &state->ctx,
299 NULL, 0, NULL, 0);
300 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
301 (u_char *)&siglen_n, INT16SZ, NULL, 0);
302 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
303 state->sig, state->siglen, NULL, 0);
304 state->siglen = 0;
307 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen,
308 NULL, 0);
310 if (done == 0 && (state->counter % 100 != 0))
311 return (0);
313 cp = msg + *msglen;
314 eob = msg + msgsize;
316 /* Name. */
317 n = dn_comp(state->key->dk_key_name, cp, eob - cp, dnptrs, lastdnptr);
318 if (n < 0)
319 return (NS_TSIG_ERROR_NO_SPACE);
320 cp += n;
322 /* Type, class, ttl, length (not filled in yet). */
323 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
324 PUTSHORT(ns_t_tsig, cp);
325 PUTSHORT(ns_c_any, cp);
326 PUTLONG(0, cp); /* TTL */
327 lenp = cp;
328 cp += 2;
330 /* Alg. */
331 n = dn_comp(NS_TSIG_ALG_HMAC_MD5, cp, eob - cp, NULL, NULL);
332 if (n < 0)
333 return (NS_TSIG_ERROR_NO_SPACE);
334 cp += n;
336 /* Time. */
337 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
338 PUTSHORT(0, cp);
339 timesigned = time(NULL);
340 PUTLONG(timesigned, cp);
341 PUTSHORT(NS_TSIG_FUDGE, cp);
344 * Compute the signature.
347 /* Digest the time signed and fudge. */
348 cp2 = buf;
349 PUTSHORT(0, cp2); /* Top 16 bits of time */
350 PUTLONG(timesigned, cp2);
351 PUTSHORT(NS_TSIG_FUDGE, cp2);
353 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
354 buf, cp2 - buf, NULL, 0);
356 n = dst_sign_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
357 state->sig, sizeof(state->sig));
358 if (n < 0)
359 return (-ns_r_badkey);
360 state->siglen = n;
362 /* Add the signature. */
363 BOUNDS_CHECK(cp, INT16SZ + state->siglen);
364 PUTSHORT(state->siglen, cp);
365 memcpy(cp, state->sig, state->siglen);
366 cp += state->siglen;
368 /* The original message ID & error. */
369 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
370 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
371 PUTSHORT(error, cp);
373 /* Other data. */
374 BOUNDS_CHECK(cp, INT16SZ);
375 PUTSHORT(0, cp);
377 /* Go back and fill in the length. */
378 PUTSHORT(cp - lenp - INT16SZ, lenp);
380 hp->arcount = htons(ntohs(hp->arcount) + 1);
381 *msglen = (cp - msg);
382 return (0);