2 * Copyright (c) 1999-2001 by Internet Software Consortium, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 static const char rcsid[] = "$Id: ns_sign.c,v 1.4.2.1 2001/05/17 20:47:33 mellon Exp $";
23 #define time(x) trace_mr_time (x)
24 time_t trace_mr_time (time_t *);
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
44 #include "minires/minires.h"
45 #include "arpa/nameser.h"
47 #include <isc-dhcp/dst.h>
49 #define BOUNDS_CHECK(ptr, count) \
51 if ((ptr) + (count) > eob) { \
52 return ISC_R_NOSPACE; \
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, unsigned *msglen, unsigned msgsize, int error, void *k,
77 const u_char *querysig, unsigned querysiglen, u_char *sig,
78 unsigned *siglen, time_t in_timesigned)
80 HEADER *hp = (HEADER *)msg;
81 DST_KEY *key = (DST_KEY *)k;
82 u_char *cp = msg + *msglen, *eob = msg + msgsize;
89 if (msg == NULL || msglen == NULL || sig == NULL || siglen == NULL)
93 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey)
94 n = dn_comp(key->dk_key_name,
95 cp, (unsigned)(eob - cp), NULL, NULL);
97 n = dn_comp("", cp, (unsigned)(eob - cp), NULL, NULL);
103 /* Type, class, ttl, length (not filled in yet). */
104 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
105 PUTSHORT(ns_t_tsig, cp);
106 PUTSHORT(ns_c_any, cp);
107 PUTLONG(0, cp); /* TTL */
112 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
113 if (key->dk_alg != KEY_HMAC_MD5)
115 n = dn_comp(NS_TSIG_ALG_HMAC_MD5,
116 cp, (unsigned)(eob - cp), NULL, NULL);
119 n = dn_comp("", cp, (unsigned)(eob - cp), NULL, NULL);
126 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
128 timesigned = time(NULL);
129 if (error != ns_r_badtime)
130 PUTLONG(timesigned, cp);
132 PUTLONG(in_timesigned, cp);
133 PUTSHORT(NS_TSIG_FUDGE, cp);
135 /* Compute the signature. */
136 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
138 u_char buf[MAXDNAME], *cp2;
141 dst_sign_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
143 /* Digest the query signature, if this is a response. */
144 if (querysiglen > 0 && querysig != NULL) {
145 u_int16_t len_n = htons(querysiglen);
146 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
147 (u_char *)&len_n, INT16SZ, NULL, 0);
148 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
149 querysig, querysiglen, NULL, 0);
152 /* Digest the message. */
153 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, msg, *msglen,
156 /* Digest the key name. */
157 n = ns_name_ntol(name, buf, sizeof(buf));
158 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
160 /* Digest the class and TTL. */
162 PUTSHORT(ns_c_any, cp2);
164 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
165 buf, (unsigned)(cp2-buf), NULL, 0);
167 /* Digest the algorithm. */
168 n = ns_name_ntol(alg, buf, sizeof(buf));
169 dst_sign_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
171 /* Digest the time signed, fudge, error, and other data */
173 PUTSHORT(0, cp2); /* Top 16 bits of time */
174 if (error != ns_r_badtime)
175 PUTLONG(timesigned, cp2);
177 PUTLONG(in_timesigned, cp2);
178 PUTSHORT(NS_TSIG_FUDGE, cp2);
179 PUTSHORT(error, cp2); /* Error */
180 if (error != ns_r_badtime)
181 PUTSHORT(0, cp2); /* Other data length */
183 PUTSHORT(INT16SZ+INT32SZ, cp2); /* Other data length */
184 PUTSHORT(0, cp2); /* Top 16 bits of time */
185 PUTLONG(timesigned, cp2);
187 dst_sign_data(SIG_MODE_UPDATE, key, &ctx,
188 buf, (unsigned)(cp2-buf), NULL, 0);
190 n = dst_sign_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
198 /* Add the signature. */
199 BOUNDS_CHECK(cp, INT16SZ + (*siglen));
200 PUTSHORT(*siglen, cp);
201 memcpy(cp, sig, *siglen);
204 /* The original message ID & error. */
205 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
206 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
210 BOUNDS_CHECK(cp, INT16SZ);
211 if (error != ns_r_badtime)
212 PUTSHORT(0, cp); /* Other data length */
214 PUTSHORT(INT16SZ+INT32SZ, cp); /* Other data length */
215 BOUNDS_CHECK(cp, INT32SZ+INT16SZ);
216 PUTSHORT(0, cp); /* Top 16 bits of time */
217 PUTLONG(timesigned, cp);
220 /* Go back and fill in the length. */
221 PUTSHORT(cp - lenp - INT16SZ, lenp);
223 hp->arcount = htons(ntohs(hp->arcount) + 1);
224 *msglen = (cp - msg);
225 return ISC_R_SUCCESS;
230 ns_sign_tcp_init(void *k, const u_char *querysig, unsigned querysiglen,
231 ns_tcp_tsig_state *state)
234 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
235 return ISC_R_INVALIDARG;
238 if (state->key->dk_alg != KEY_HMAC_MD5)
240 if (querysiglen > sizeof(state->sig))
241 return ISC_R_NOSPACE;
242 memcpy(state->sig, querysig, querysiglen);
243 state->siglen = querysiglen;
244 return ISC_R_SUCCESS;
248 ns_sign_tcp(u_char *msg, unsigned *msglen, unsigned msgsize, int error,
249 ns_tcp_tsig_state *state, int done)
251 u_char *cp, *eob, *lenp;
252 u_char buf[MAXDNAME], *cp2;
253 HEADER *hp = (HEADER *)msg;
257 if (msg == NULL || msglen == NULL || state == NULL)
258 return ISC_R_INVALIDARG;
261 if (state->counter == 0)
262 return ns_sign(msg, msglen, msgsize, error, state->key,
263 state->sig, state->siglen,
264 state->sig, &state->siglen, 0);
266 if (state->siglen > 0) {
267 u_int16_t siglen_n = htons(state->siglen);
268 dst_sign_data(SIG_MODE_INIT, state->key, &state->ctx,
270 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
271 (u_char *)&siglen_n, INT16SZ, NULL, 0);
272 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
273 state->sig, state->siglen, NULL, 0);
277 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen,
280 if (done == 0 && (state->counter % 100 != 0))
281 return ISC_R_SUCCESS;
287 n = dn_comp(state->key->dk_key_name,
288 cp, (unsigned)(eob - cp), NULL, NULL);
290 return ISC_R_NOSPACE;
293 /* Type, class, ttl, length (not filled in yet). */
294 BOUNDS_CHECK(cp, INT16SZ + INT16SZ + INT32SZ + INT16SZ);
295 PUTSHORT(ns_t_tsig, cp);
296 PUTSHORT(ns_c_any, cp);
297 PUTLONG(0, cp); /* TTL */
302 n = dn_comp(NS_TSIG_ALG_HMAC_MD5,
303 cp, (unsigned)(eob - cp), NULL, NULL);
305 return ISC_R_NOSPACE;
309 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
311 timesigned = time(NULL);
312 PUTLONG(timesigned, cp);
313 PUTSHORT(NS_TSIG_FUDGE, cp);
316 * Compute the signature.
319 /* Digest the time signed and fudge. */
321 PUTSHORT(0, cp2); /* Top 16 bits of time */
322 PUTLONG(timesigned, cp2);
323 PUTSHORT(NS_TSIG_FUDGE, cp2);
325 dst_sign_data(SIG_MODE_UPDATE, state->key, &state->ctx,
326 buf, (unsigned)(cp2 - buf), NULL, 0);
328 n = dst_sign_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
329 state->sig, sizeof(state->sig));
334 /* Add the signature. */
335 BOUNDS_CHECK(cp, INT16SZ + state->siglen);
336 PUTSHORT(state->siglen, cp);
337 memcpy(cp, state->sig, state->siglen);
340 /* The original message ID & error. */
341 BOUNDS_CHECK(cp, INT16SZ + INT16SZ);
342 PUTSHORT(ntohs(hp->id), cp); /* already in network order */
346 BOUNDS_CHECK(cp, INT16SZ);
349 /* Go back and fill in the length. */
350 PUTSHORT(cp - lenp - INT16SZ, lenp);
352 hp->arcount = htons(ntohs(hp->arcount) + 1);
353 *msglen = (cp - msg);
354 return ISC_R_SUCCESS;