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.
19 static const char rcsid[] = "$Id: ns_sign.c,v 1.1.2.3 2004/03/09 09:17:37 marka Exp $";
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>
44 #include <isc/assertions.h>
46 #include "port_after.h"
48 #define BOUNDS_CHECK(ptr, count) \
50 if ((ptr) + (count) > eob) { \
52 return(NS_TSIG_ERROR_NO_SPACE); \
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
71 * - bad input data (-1)
72 * - bad key / sign failed (-BADKEY)
73 * - not enough space (NS_TSIG_ERROR_NO_SPACE)
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,
80 return(ns_sign2(msg, msglen, msgsize, error, k,
81 querysig, querysiglen, sig, siglen,
82 in_timesigned, NULL, NULL));
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 = msg + *msglen, *eob = msg + msgsize;
97 u_char name[NS_MAXCDNAME];
100 if (msg == NULL || msglen == NULL || sig == NULL || siglen == NULL)
104 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
105 n = ns_name_pton(key->dk_key_name, name, sizeof name);
107 n = ns_name_pack(name, cp, eob - cp,
108 (const u_char **)dnptrs,
109 (const u_char **)lastdnptr);
112 n = ns_name_pton("", name, sizeof name);
114 n = ns_name_pack(name, cp, eob - cp, NULL, NULL);
117 return (NS_TSIG_ERROR_NO_SPACE);
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 */
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);
135 n = dn_comp("", cp, eob - cp, NULL, NULL);
137 return (NS_TSIG_ERROR_NO_SPACE);
142 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
144 timesigned = time(NULL);
145 if (error != ns_r_badtime)
146 PUTLONG(timesigned, cp);
148 PUTLONG(in_timesigned, cp);
149 PUTSHORT(NS_TSIG_FUDGE, cp);
151 /* Compute the signature. */
152 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
154 u_char buf[NS_MAXCDNAME], *cp2;
157 dst_sign_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
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);
168 /* Digest the message. */
169 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, msg, *msglen,
172 /* Digest the key name. */
173 n = ns_name_ntol(name, buf, sizeof(buf));
175 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
177 /* Digest the class and TTL. */
179 PUTSHORT(ns_c_any, cp2);
181 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
184 /* Digest the algorithm. */
185 n = ns_name_ntol(alg, buf, sizeof(buf));
187 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
189 /* Digest the time signed, fudge, error, and other data */
191 PUTSHORT(0, cp2); /* Top 16 bits of time */
192 if (error != ns_r_badtime)
193 PUTLONG(timesigned, cp2);
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 */
201 PUTSHORT(INT16SZ+INT32SZ, cp2); /* Other data length */
202 PUTSHORT(0, cp2); /* Top 16 bits of time */
203 PUTLONG(timesigned, cp2);
205 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, cp2-buf,
208 n = dst_sign_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
211 return (-ns_r_badkey);
216 /* Add the signature. */
217 BOUNDS_CHECK(cp, INT16SZ + (*siglen));
218 PUTSHORT(*siglen, cp);
219 memcpy(cp, sig, *siglen);
222 /* The original message ID & error. */
223 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
224 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
228 BOUNDS_CHECK(cp, INT16SZ);
229 if (error != ns_r_badtime)
230 PUTSHORT(0, cp); /* Other data length */
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);
238 /* Go back and fill in the length. */
239 PUTSHORT(cp - lenp - INT16SZ, lenp);
241 hp->arcount = htons(ntohs(hp->arcount) + 1);
242 *msglen = (cp - msg);
247 ns_sign_tcp_init(void *k, const u_char *querysig, int querysiglen,
248 ns_tcp_tsig_state *state)
251 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
255 if (state->key->dk_alg != KEY_HMAC_MD5)
256 return (-ns_r_badkey);
257 if (querysiglen > (int)sizeof(state->sig))
259 memcpy(state->sig, querysig, querysiglen);
260 state->siglen = querysiglen;
265 ns_sign_tcp(u_char *msg, int *msglen, int msgsize, int error,
266 ns_tcp_tsig_state *state, int done)
268 return (ns_sign_tcp2(msg, msglen, msgsize, error, state,
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)
277 u_char *cp, *eob, *lenp;
278 u_char buf[MAXDNAME], *cp2;
279 HEADER *hp = (HEADER *)msg;
283 if (msg == NULL || msglen == NULL || state == NULL)
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,
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,
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);
304 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen,
307 if (done == 0 && (state->counter % 100 != 0))
314 n = dn_comp(state->key->dk_key_name, cp, eob - cp, dnptrs, lastdnptr);
316 return (NS_TSIG_ERROR_NO_SPACE);
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 */
328 n = dn_comp(NS_TSIG_ALG_HMAC_MD5, cp, eob - cp, NULL, NULL);
330 return (NS_TSIG_ERROR_NO_SPACE);
334 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
336 timesigned = time(NULL);
337 PUTLONG(timesigned, cp);
338 PUTSHORT(NS_TSIG_FUDGE, cp);
341 * Compute the signature.
344 /* Digest the time signed and fudge. */
346 PUTSHORT(0, cp2); /* Top 16 bits of time */
347 PUTLONG(timesigned, cp2);
348 PUTSHORT(NS_TSIG_FUDGE, cp2);
350 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
351 buf, cp2 - buf, NULL, 0);
353 n = dst_sign_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
354 state->sig, sizeof(state->sig));
356 return (-ns_r_badkey);
359 /* Add the signature. */
360 BOUNDS_CHECK(cp, INT16SZ + state->siglen);
361 PUTSHORT(state->siglen, cp);
362 memcpy(cp, state->sig, state->siglen);
365 /* The original message ID & error. */
366 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
367 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
371 BOUNDS_CHECK(cp, INT16SZ);
374 /* Go back and fill in the length. */
375 PUTSHORT(cp - lenp - INT16SZ, lenp);
377 hp->arcount = htons(ntohs(hp->arcount) + 1);
378 *msglen = (cp - msg);