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.5 2007/05/27 16:27:57 tls 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 "omapip/trace_mr.h"
52 #include "arpa/nameser.h"
54 #include <isc-dhcp/dst.h>
56 #define BOUNDS_CHECK(ptr, count) \
58 if ((ptr) + (count) > eob) { \
59 return ISC_R_NOSPACE; \
65 * msg message to be sent
66 * msglen input - length of message
67 * output - length of signed message
68 * msgsize length of buffer containing message
69 * error value to put in the error field
70 * key tsig key used for signing
71 * querysig (response), the signature in the query
72 * querysiglen (response), the length of the signature in the query
73 * sig a buffer to hold the generated signature
74 * siglen input - length of signature buffer
75 * output - length of signature
78 * - bad input data (-1)
79 * - bad key / sign failed (-BADKEY)
80 * - not enough space (NS_TSIG_ERROR_NO_SPACE)
83 ns_sign(u_char
*msg
, unsigned *msglen
, unsigned msgsize
, int error
, void *k
,
84 const u_char
*querysig
, unsigned querysiglen
, u_char
*sig
,
85 unsigned *siglen
, time_t in_timesigned
)
87 HEADER
*hp
= (HEADER
*)msg
;
88 DST_KEY
*key
= (DST_KEY
*)k
;
89 u_char
*cp
= msg
+ *msglen
, *eob
= msg
+ msgsize
;
96 if (msg
== NULL
|| msglen
== NULL
|| sig
== NULL
|| siglen
== NULL
)
97 return ISC_R_INVALIDARG
;
100 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
)
101 n
= dn_comp(key
->dk_key_name
,
102 cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
104 n
= dn_comp("", cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
106 return ISC_R_NOSPACE
;
110 /* Type, class, ttl, length (not filled in yet). */
111 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
112 PUTSHORT(ns_t_tsig
, cp
);
113 PUTSHORT(ns_c_any
, cp
);
114 PUTLONG(0, cp
); /* TTL */
119 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
120 if (key
->dk_alg
!= KEY_HMAC_MD5
)
122 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
,
123 cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
126 n
= dn_comp("", cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
128 return ISC_R_NOSPACE
;
133 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
135 timesigned
= time(NULL
);
136 if (error
!= ns_r_badtime
)
137 PUTLONG(timesigned
, cp
);
139 PUTLONG(in_timesigned
, cp
);
140 PUTSHORT(NS_TSIG_FUDGE
, cp
);
142 /* Compute the signature. */
143 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
145 u_char buf
[MAXDNAME
], *cp2
;
148 dst_sign_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
150 /* Digest the query signature, if this is a response. */
151 if (querysiglen
> 0 && querysig
!= NULL
) {
152 u_int16_t len_n
= htons(querysiglen
);
153 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
154 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
155 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
156 querysig
, querysiglen
, NULL
, 0);
159 /* Digest the message. */
160 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, *msglen
,
163 /* Digest the key name. */
164 n
= ns_name_ntol(name
, buf
, sizeof(buf
));
165 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
167 /* Digest the class and TTL. */
169 PUTSHORT(ns_c_any
, cp2
);
171 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
172 buf
, (unsigned)(cp2
-buf
), NULL
, 0);
174 /* Digest the algorithm. */
175 n
= ns_name_ntol(alg
, buf
, sizeof(buf
));
176 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
178 /* Digest the time signed, fudge, error, and other data */
180 PUTSHORT(0, cp2
); /* Top 16 bits of time */
181 if (error
!= ns_r_badtime
)
182 PUTLONG(timesigned
, cp2
);
184 PUTLONG(in_timesigned
, cp2
);
185 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
186 PUTSHORT(error
, cp2
); /* Error */
187 if (error
!= ns_r_badtime
)
188 PUTSHORT(0, cp2
); /* Other data length */
190 PUTSHORT(INT16SZ
+INT32SZ
, cp2
); /* Other data length */
191 PUTSHORT(0, cp2
); /* Top 16 bits of time */
192 PUTLONG(timesigned
, cp2
);
194 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
195 buf
, (unsigned)(cp2
-buf
), NULL
, 0);
197 n
= dst_sign_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
205 /* Add the signature. */
206 BOUNDS_CHECK(cp
, INT16SZ
+ (*siglen
));
207 PUTSHORT(*siglen
, cp
);
208 memcpy(cp
, sig
, *siglen
);
211 /* The original message ID & error. */
212 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
213 PUTSHORT(ntohs(hp
->id
), cp
); /* already in network order */
217 BOUNDS_CHECK(cp
, INT16SZ
);
218 if (error
!= ns_r_badtime
)
219 PUTSHORT(0, cp
); /* Other data length */
221 PUTSHORT(INT16SZ
+INT32SZ
, cp
); /* Other data length */
222 BOUNDS_CHECK(cp
, INT32SZ
+INT16SZ
);
223 PUTSHORT(0, cp
); /* Top 16 bits of time */
224 PUTLONG(timesigned
, cp
);
227 /* Go back and fill in the length. */
228 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
230 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
231 *msglen
= (cp
- msg
);
232 return ISC_R_SUCCESS
;
237 ns_sign_tcp_init(void *k
, const u_char
*querysig
, unsigned querysiglen
,
238 ns_tcp_tsig_state
*state
)
241 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
242 return ISC_R_INVALIDARG
;
245 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
247 if (querysiglen
> sizeof(state
->sig
))
248 return ISC_R_NOSPACE
;
249 memcpy(state
->sig
, querysig
, querysiglen
);
250 state
->siglen
= querysiglen
;
251 return ISC_R_SUCCESS
;
255 ns_sign_tcp(u_char
*msg
, unsigned *msglen
, unsigned msgsize
, int error
,
256 ns_tcp_tsig_state
*state
, int done
)
258 u_char
*cp
, *eob
, *lenp
;
259 u_char buf
[MAXDNAME
], *cp2
;
260 HEADER
*hp
= (HEADER
*)msg
;
264 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
265 return ISC_R_INVALIDARG
;
268 if (state
->counter
== 0)
269 return ns_sign(msg
, msglen
, msgsize
, error
, state
->key
,
270 state
->sig
, state
->siglen
,
271 state
->sig
, &state
->siglen
, 0);
273 if (state
->siglen
> 0) {
274 u_int16_t siglen_n
= htons(state
->siglen
);
275 dst_sign_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
277 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
278 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
279 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
280 state
->sig
, state
->siglen
, NULL
, 0);
284 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
, msg
, *msglen
,
287 if (done
== 0 && (state
->counter
% 100 != 0))
288 return ISC_R_SUCCESS
;
294 n
= dn_comp(state
->key
->dk_key_name
,
295 cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
297 return ISC_R_NOSPACE
;
300 /* Type, class, ttl, length (not filled in yet). */
301 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
302 PUTSHORT(ns_t_tsig
, cp
);
303 PUTSHORT(ns_c_any
, cp
);
304 PUTLONG(0, cp
); /* TTL */
309 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
,
310 cp
, (unsigned)(eob
- cp
), NULL
, NULL
);
312 return ISC_R_NOSPACE
;
316 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
318 timesigned
= time(NULL
);
319 PUTLONG(timesigned
, cp
);
320 PUTSHORT(NS_TSIG_FUDGE
, cp
);
323 * Compute the signature.
326 /* Digest the time signed and fudge. */
328 PUTSHORT(0, cp2
); /* Top 16 bits of time */
329 PUTLONG(timesigned
, cp2
);
330 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
332 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
333 buf
, (unsigned)(cp2
- buf
), NULL
, 0);
335 n
= dst_sign_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
336 state
->sig
, sizeof(state
->sig
));
341 /* Add the signature. */
342 BOUNDS_CHECK(cp
, INT16SZ
+ state
->siglen
);
343 PUTSHORT(state
->siglen
, cp
);
344 memcpy(cp
, state
->sig
, state
->siglen
);
347 /* The original message ID & error. */
348 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
349 PUTSHORT(ntohs(hp
->id
), cp
); /* already in network order */
353 BOUNDS_CHECK(cp
, INT16SZ
);
356 /* Go back and fill in the length. */
357 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
359 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
360 *msglen
= (cp
- msg
);
361 return ISC_R_SUCCESS
;