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 #include "port_before.h"
19 #include "fd_setsize.h"
21 #include <sys/types.h>
22 #include <sys/param.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
38 #include <isc/assertions.h>
40 #include "port_after.h"
42 #define BOUNDS_CHECK(ptr, count) \
44 if ((ptr) + (count) > eob) { \
46 return(NS_TSIG_ERROR_NO_SPACE); \
54 *\li msg message to be sent
55 *\li msglen input - length of message
56 * output - length of signed message
57 *\li msgsize length of buffer containing message
58 *\li error value to put in the error field
59 *\li key tsig key used for signing
60 *\li querysig (response), the signature in the query
61 *\li querysiglen (response), the length of the signature in the query
62 *\li sig a buffer to hold the generated signature
63 *\li siglen input - length of signature buffer
64 * output - length of signature
67 *\li - bad input data (-1)
68 *\li - bad key / sign failed (-BADKEY)
69 *\li - not enough space (NS_TSIG_ERROR_NO_SPACE)
72 ns_sign(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
73 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
76 return(ns_sign2(msg
, msglen
, msgsize
, error
, k
,
77 querysig
, querysiglen
, sig
, siglen
,
78 in_timesigned
, NULL
, NULL
));
82 ns_sign2(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
83 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
84 time_t in_timesigned
, u_char
**dnptrs
, u_char
**lastdnptr
)
86 HEADER
*hp
= (HEADER
*)msg
;
87 DST_KEY
*key
= (DST_KEY
*)k
;
93 u_char name
[NS_MAXCDNAME
];
96 if (msg
== NULL
|| msglen
== NULL
|| sig
== NULL
|| siglen
== NULL
)
103 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
104 n
= ns_name_pton(key
->dk_key_name
, name
, sizeof name
);
106 n
= ns_name_pack(name
, cp
, eob
- cp
,
107 (const u_char
**)dnptrs
,
108 (const u_char
**)lastdnptr
);
111 n
= ns_name_pton("", name
, sizeof name
);
113 n
= ns_name_pack(name
, cp
, eob
- cp
, NULL
, NULL
);
116 return (NS_TSIG_ERROR_NO_SPACE
);
119 /* Type, class, ttl, length (not filled in yet). */
120 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
121 PUTSHORT(ns_t_tsig
, cp
);
122 PUTSHORT(ns_c_any
, cp
);
123 PUTLONG(0, cp
); /*%< TTL */
128 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
129 if (key
->dk_alg
!= KEY_HMAC_MD5
)
130 return (-ns_r_badkey
);
131 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
134 n
= dn_comp("", cp
, eob
- cp
, NULL
, NULL
);
136 return (NS_TSIG_ERROR_NO_SPACE
);
141 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
143 timesigned
= time(NULL
);
144 if (error
!= ns_r_badtime
)
145 PUTLONG(timesigned
, cp
);
147 PUTLONG(in_timesigned
, cp
);
148 PUTSHORT(NS_TSIG_FUDGE
, cp
);
150 /* Compute the signature. */
151 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
153 u_char buf
[NS_MAXCDNAME
], *cp2
;
156 dst_sign_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
158 /* Digest the query signature, if this is a response. */
159 if (querysiglen
> 0 && querysig
!= NULL
) {
160 u_int16_t len_n
= htons(querysiglen
);
161 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
162 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
163 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
164 querysig
, querysiglen
, NULL
, 0);
167 /* Digest the message. */
168 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, *msglen
,
171 /* Digest the key name. */
172 n
= ns_name_ntol(name
, buf
, sizeof(buf
));
174 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
176 /* Digest the class and TTL. */
178 PUTSHORT(ns_c_any
, cp2
);
180 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
183 /* Digest the algorithm. */
184 n
= ns_name_ntol(alg
, buf
, sizeof(buf
));
186 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
188 /* Digest the time signed, fudge, error, and other data */
190 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
191 if (error
!= ns_r_badtime
)
192 PUTLONG(timesigned
, cp2
);
194 PUTLONG(in_timesigned
, cp2
);
195 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
196 PUTSHORT(error
, cp2
); /*%< Error */
197 if (error
!= ns_r_badtime
)
198 PUTSHORT(0, cp2
); /*%< Other data length */
200 PUTSHORT(INT16SZ
+INT32SZ
, cp2
); /*%< Other data length */
201 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
202 PUTLONG(timesigned
, cp2
);
204 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
207 n
= dst_sign_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
210 return (-ns_r_badkey
);
215 /* Add the signature. */
216 BOUNDS_CHECK(cp
, INT16SZ
+ (*siglen
));
217 PUTSHORT(*siglen
, cp
);
218 memcpy(cp
, sig
, *siglen
);
221 /* The original message ID & error. */
222 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
223 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
227 BOUNDS_CHECK(cp
, INT16SZ
);
228 if (error
!= ns_r_badtime
)
229 PUTSHORT(0, cp
); /*%< Other data length */
231 PUTSHORT(INT16SZ
+INT32SZ
, cp
); /*%< Other data length */
232 BOUNDS_CHECK(cp
, INT32SZ
+INT16SZ
);
233 PUTSHORT(0, cp
); /*%< Top 16 bits of time */
234 PUTLONG(timesigned
, cp
);
237 /* Go back and fill in the length. */
238 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
240 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
241 *msglen
= (cp
- msg
);
246 ns_sign_tcp_init(void *k
, const u_char
*querysig
, int querysiglen
,
247 ns_tcp_tsig_state
*state
)
250 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
254 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
255 return (-ns_r_badkey
);
256 if (querysiglen
> (int)sizeof(state
->sig
))
258 memcpy(state
->sig
, querysig
, querysiglen
);
259 state
->siglen
= querysiglen
;
264 ns_sign_tcp(u_char
*msg
, int *msglen
, int msgsize
, int error
,
265 ns_tcp_tsig_state
*state
, int done
)
267 return (ns_sign_tcp2(msg
, msglen
, msgsize
, error
, state
,
272 ns_sign_tcp2(u_char
*msg
, int *msglen
, int msgsize
, int error
,
273 ns_tcp_tsig_state
*state
, int done
,
274 u_char
**dnptrs
, u_char
**lastdnptr
)
276 u_char
*cp
, *eob
, *lenp
;
277 u_char buf
[MAXDNAME
], *cp2
;
278 HEADER
*hp
= (HEADER
*)msg
;
282 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
286 if (state
->counter
== 0)
287 return (ns_sign2(msg
, msglen
, msgsize
, error
, state
->key
,
288 state
->sig
, state
->siglen
,
289 state
->sig
, &state
->siglen
, 0,
292 if (state
->siglen
> 0) {
293 u_int16_t siglen_n
= htons(state
->siglen
);
294 dst_sign_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
296 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
297 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
298 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
299 state
->sig
, state
->siglen
, NULL
, 0);
303 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
, msg
, *msglen
,
306 if (done
== 0 && (state
->counter
% 100 != 0))
313 n
= dn_comp(state
->key
->dk_key_name
, cp
, eob
- cp
, dnptrs
, lastdnptr
);
315 return (NS_TSIG_ERROR_NO_SPACE
);
318 /* Type, class, ttl, length (not filled in yet). */
319 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
320 PUTSHORT(ns_t_tsig
, cp
);
321 PUTSHORT(ns_c_any
, cp
);
322 PUTLONG(0, cp
); /*%< TTL */
327 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
329 return (NS_TSIG_ERROR_NO_SPACE
);
333 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
335 timesigned
= time(NULL
);
336 PUTLONG(timesigned
, cp
);
337 PUTSHORT(NS_TSIG_FUDGE
, cp
);
340 * Compute the signature.
343 /* Digest the time signed and fudge. */
345 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
346 PUTLONG(timesigned
, cp2
);
347 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
349 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
350 buf
, cp2
- buf
, NULL
, 0);
352 n
= dst_sign_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
353 state
->sig
, sizeof(state
->sig
));
355 return (-ns_r_badkey
);
358 /* Add the signature. */
359 BOUNDS_CHECK(cp
, INT16SZ
+ state
->siglen
);
360 PUTSHORT(state
->siglen
, cp
);
361 memcpy(cp
, state
->sig
, state
->siglen
);
364 /* The original message ID & error. */
365 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
366 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
370 BOUNDS_CHECK(cp
, INT16SZ
);
373 /* Go back and fill in the length. */
374 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
376 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
377 *msglen
= (cp
- msg
);