2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1999-2003 by Internet Software Consortium
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.
17 * Internet Systems Consortium, Inc.
19 * Redwood City, CA 94063
25 static const char rcsid[] = "$Id: ns_sign.c,v 1.4.2.4 2004/06/10 17:59:42 dhankins Exp $";
29 #define time(x) trace_mr_time (x)
30 time_t trace_mr_time (time_t *);
35 #include <sys/types.h>
36 #include <sys/param.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <sys/socket.h>
50 #include "minires/minires.h"
51 #include "arpa/nameser.h"
53 #include <isc-dhcp/dst.h>
55 #define BOUNDS_CHECK(ptr, count) \
57 if ((ptr) + (count) > eob) { \
58 return ISC_R_NOSPACE; \
64 * msg message to be sent
65 * msglen input - length of message
66 * output - length of signed message
67 * msgsize length of buffer containing message
68 * error value to put in the error field
69 * key tsig key used for signing
70 * querysig (response), the signature in the query
71 * querysiglen (response), the length of the signature in the query
72 * sig a buffer to hold the generated signature
73 * siglen input - length of signature buffer
74 * output - length of signature
77 * - bad input data (-1)
78 * - bad key / sign failed (-BADKEY)
79 * - not enough space (NS_TSIG_ERROR_NO_SPACE)
82 ns_sign(u_char *msg, unsigned *msglen, unsigned msgsize, int error, void *k,
83 const u_char *querysig, unsigned querysiglen, u_char *sig,
84 unsigned *siglen, time_t in_timesigned)
86 HEADER *hp = (HEADER *)msg;
87 DST_KEY *key = (DST_KEY *)k;
88 u_char *cp = msg + *msglen, *eob = msg + msgsize;
95 if (msg == NULL || msglen == NULL || sig == NULL || siglen == NULL)
96 return ISC_R_INVALIDARG;
99 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey)
100 n = dn_comp(key->dk_key_name,
101 cp, (unsigned)(eob - cp), NULL, NULL);
103 n = dn_comp("", cp, (unsigned)(eob - cp), NULL, NULL);
105 return ISC_R_NOSPACE;
109 /* Type, class, ttl, length (not filled in yet). */
110 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
111 PUTSHORT(ns_t_tsig, cp);
112 PUTSHORT(ns_c_any, cp);
113 PUTLONG(0, cp); /* TTL */
118 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
119 if (key->dk_alg != KEY_HMAC_MD5)
121 n = dn_comp(NS_TSIG_ALG_HMAC_MD5,
122 cp, (unsigned)(eob - cp), NULL, NULL);
125 n = dn_comp("", cp, (unsigned)(eob - cp), NULL, NULL);
127 return ISC_R_NOSPACE;
132 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
134 timesigned = time(NULL);
135 if (error != ns_r_badtime)
136 PUTLONG(timesigned, cp);
138 PUTLONG(in_timesigned, cp);
139 PUTSHORT(NS_TSIG_FUDGE, cp);
141 /* Compute the signature. */
142 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
144 u_char buf[MAXDNAME], *cp2;
147 dst_sign_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
149 /* Digest the query signature, if this is a response. */
150 if (querysiglen > 0 && querysig != NULL) {
151 u_int16_t len_n = htons(querysiglen);
152 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
153 (u_char *)&len_n, INT16SZ, NULL, 0);
154 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
155 querysig, querysiglen, NULL, 0);
158 /* Digest the message. */
159 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, msg, *msglen,
162 /* Digest the key name. */
163 n = ns_name_ntol(name, buf, sizeof(buf));
164 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
166 /* Digest the class and TTL. */
168 PUTSHORT(ns_c_any, cp2);
170 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
171 buf, (unsigned)(cp2-buf), NULL, 0);
173 /* Digest the algorithm. */
174 n = ns_name_ntol(alg, buf, sizeof(buf));
175 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
177 /* Digest the time signed, fudge, error, and other data */
179 PUTSHORT(0, cp2); /* Top 16 bits of time */
180 if (error != ns_r_badtime)
181 PUTLONG(timesigned, cp2);
183 PUTLONG(in_timesigned, cp2);
184 PUTSHORT(NS_TSIG_FUDGE, cp2);
185 PUTSHORT(error, cp2); /* Error */
186 if (error != ns_r_badtime)
187 PUTSHORT(0, cp2); /* Other data length */
189 PUTSHORT(INT16SZ+INT32SZ, cp2); /* Other data length */
190 PUTSHORT(0, cp2); /* Top 16 bits of time */
191 PUTLONG(timesigned, cp2);
193 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
194 buf, (unsigned)(cp2-buf), NULL, 0);
196 n = dst_sign_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
204 /* Add the signature. */
205 BOUNDS_CHECK(cp, INT16SZ + (*siglen));
206 PUTSHORT(*siglen, cp);
207 memcpy(cp, sig, *siglen);
210 /* The original message ID & error. */
211 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
212 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
216 BOUNDS_CHECK(cp, INT16SZ);
217 if (error != ns_r_badtime)
218 PUTSHORT(0, cp); /* Other data length */
220 PUTSHORT(INT16SZ+INT32SZ, cp); /* Other data length */
221 BOUNDS_CHECK(cp, INT32SZ+INT16SZ);
222 PUTSHORT(0, cp); /* Top 16 bits of time */
223 PUTLONG(timesigned, cp);
226 /* Go back and fill in the length. */
227 PUTSHORT(cp - lenp - INT16SZ, lenp);
229 hp->arcount = htons(ntohs(hp->arcount) + 1);
230 *msglen = (cp - msg);
231 return ISC_R_SUCCESS;
236 ns_sign_tcp_init(void *k, const u_char *querysig, unsigned querysiglen,
237 ns_tcp_tsig_state *state)
240 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
241 return ISC_R_INVALIDARG;
244 if (state->key->dk_alg != KEY_HMAC_MD5)
246 if (querysiglen > sizeof(state->sig))
247 return ISC_R_NOSPACE;
248 memcpy(state->sig, querysig, querysiglen);
249 state->siglen = querysiglen;
250 return ISC_R_SUCCESS;
254 ns_sign_tcp(u_char *msg, unsigned *msglen, unsigned msgsize, int error,
255 ns_tcp_tsig_state *state, int done)
257 u_char *cp, *eob, *lenp;
258 u_char buf[MAXDNAME], *cp2;
259 HEADER *hp = (HEADER *)msg;
263 if (msg == NULL || msglen == NULL || state == NULL)
264 return ISC_R_INVALIDARG;
267 if (state->counter == 0)
268 return ns_sign(msg, msglen, msgsize, error, state->key,
269 state->sig, state->siglen,
270 state->sig, &state->siglen, 0);
272 if (state->siglen > 0) {
273 u_int16_t siglen_n = htons(state->siglen);
274 dst_sign_data(SIG_MODE_INIT, state->key, &state->ctx,
276 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
277 (u_char *)&siglen_n, INT16SZ, NULL, 0);
278 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
279 state->sig, state->siglen, NULL, 0);
283 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen,
286 if (done == 0 && (state->counter % 100 != 0))
287 return ISC_R_SUCCESS;
293 n = dn_comp(state->key->dk_key_name,
294 cp, (unsigned)(eob - cp), NULL, NULL);
296 return ISC_R_NOSPACE;
299 /* Type, class, ttl, length (not filled in yet). */
300 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
301 PUTSHORT(ns_t_tsig, cp);
302 PUTSHORT(ns_c_any, cp);
303 PUTLONG(0, cp); /* TTL */
308 n = dn_comp(NS_TSIG_ALG_HMAC_MD5,
309 cp, (unsigned)(eob - cp), NULL, NULL);
311 return ISC_R_NOSPACE;
315 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
317 timesigned = time(NULL);
318 PUTLONG(timesigned, cp);
319 PUTSHORT(NS_TSIG_FUDGE, cp);
322 * Compute the signature.
325 /* Digest the time signed and fudge. */
327 PUTSHORT(0, cp2); /* Top 16 bits of time */
328 PUTLONG(timesigned, cp2);
329 PUTSHORT(NS_TSIG_FUDGE, cp2);
331 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
332 buf, (unsigned)(cp2 - buf), NULL, 0);
334 n = dst_sign_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
335 state->sig, sizeof(state->sig));
340 /* Add the signature. */
341 BOUNDS_CHECK(cp, INT16SZ + state->siglen);
342 PUTSHORT(state->siglen, cp);
343 memcpy(cp, state->sig, state->siglen);
346 /* The original message ID & error. */
347 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
348 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
352 BOUNDS_CHECK(cp, INT16SZ);
355 /* Go back and fill in the length. */
356 PUTSHORT(cp - lenp - INT16SZ, lenp);
358 hp->arcount = htons(ntohs(hp->arcount) + 1);
359 *msglen = (cp - msg);
360 return ISC_R_SUCCESS;