4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1999 by Internet Software Consortium, Inc.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static const char rcsid
[] = "Id: ns_sign.c,v 1.6 2006/03/09 23:57:56 marka Exp";
26 #include "port_before.h"
27 #include "fd_setsize.h"
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #include <arpa/inet.h>
46 #include <isc/assertions.h>
48 #include "port_after.h"
50 #define BOUNDS_CHECK(ptr, count) \
52 if ((ptr) + (count) > eob) { \
54 return(NS_TSIG_ERROR_NO_SPACE); \
62 *\li msg message to be sent
63 *\li msglen input - length of message
64 * output - length of signed message
65 *\li msgsize length of buffer containing message
66 *\li error value to put in the error field
67 *\li key tsig key used for signing
68 *\li querysig (response), the signature in the query
69 *\li querysiglen (response), the length of the signature in the query
70 *\li sig a buffer to hold the generated signature
71 *\li siglen input - length of signature buffer
72 * output - length of signature
75 *\li - bad input data (-1)
76 *\li - bad key / sign failed (-BADKEY)
77 *\li - not enough space (NS_TSIG_ERROR_NO_SPACE)
80 ns_sign(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
81 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
84 return(ns_sign2(msg
, msglen
, msgsize
, error
, k
,
85 querysig
, querysiglen
, sig
, siglen
,
86 in_timesigned
, NULL
, NULL
));
90 ns_sign2(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
91 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
92 time_t in_timesigned
, u_char
**dnptrs
, u_char
**lastdnptr
)
94 HEADER
*hp
= (HEADER
*)msg
;
95 DST_KEY
*key
= (DST_KEY
*)k
;
101 u_char name
[NS_MAXCDNAME
];
104 if (msg
== NULL
|| msglen
== NULL
|| sig
== NULL
|| siglen
== NULL
)
111 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
112 n
= ns_name_pton(key
->dk_key_name
, name
, sizeof name
);
114 n
= ns_name_pack(name
, cp
, eob
- cp
,
115 (const u_char
**)dnptrs
,
116 (const u_char
**)lastdnptr
);
119 n
= ns_name_pton("", name
, sizeof name
);
121 n
= ns_name_pack(name
, cp
, eob
- cp
, NULL
, NULL
);
124 return (NS_TSIG_ERROR_NO_SPACE
);
127 /* Type, class, ttl, length (not filled in yet). */
128 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
129 PUTSHORT(ns_t_tsig
, cp
);
130 PUTSHORT(ns_c_any
, cp
);
131 PUTLONG(0, cp
); /*%< TTL */
136 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
137 if (key
->dk_alg
!= KEY_HMAC_MD5
)
138 return (-ns_r_badkey
);
139 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
142 n
= dn_comp("", cp
, eob
- cp
, NULL
, NULL
);
144 return (NS_TSIG_ERROR_NO_SPACE
);
149 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
151 timesigned
= time(NULL
);
152 if (error
!= ns_r_badtime
)
153 PUTLONG(timesigned
, cp
);
155 PUTLONG(in_timesigned
, cp
);
156 PUTSHORT(NS_TSIG_FUDGE
, cp
);
158 /* Compute the signature. */
159 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
161 u_char buf
[NS_MAXCDNAME
], *cp2
;
164 dst_sign_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
166 /* Digest the query signature, if this is a response. */
167 if (querysiglen
> 0 && querysig
!= NULL
) {
168 u_int16_t len_n
= htons(querysiglen
);
169 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
170 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
171 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
172 querysig
, querysiglen
, NULL
, 0);
175 /* Digest the message. */
176 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, *msglen
,
179 /* Digest the key name. */
180 n
= ns_name_ntol(name
, buf
, sizeof(buf
));
182 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
184 /* Digest the class and TTL. */
186 PUTSHORT(ns_c_any
, cp2
);
188 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
191 /* Digest the algorithm. */
192 n
= ns_name_ntol(alg
, buf
, sizeof(buf
));
194 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
196 /* Digest the time signed, fudge, error, and other data */
198 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
199 if (error
!= ns_r_badtime
)
200 PUTLONG(timesigned
, cp2
);
202 PUTLONG(in_timesigned
, cp2
);
203 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
204 PUTSHORT(error
, cp2
); /*%< Error */
205 if (error
!= ns_r_badtime
)
206 PUTSHORT(0, cp2
); /*%< Other data length */
208 PUTSHORT(INT16SZ
+INT32SZ
, cp2
); /*%< Other data length */
209 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
210 PUTLONG(timesigned
, cp2
);
212 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
215 n
= dst_sign_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
218 return (-ns_r_badkey
);
223 /* Add the signature. */
224 BOUNDS_CHECK(cp
, INT16SZ
+ (*siglen
));
225 PUTSHORT(*siglen
, cp
);
226 memcpy(cp
, sig
, *siglen
);
229 /* The original message ID & error. */
230 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
231 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
235 BOUNDS_CHECK(cp
, INT16SZ
);
236 if (error
!= ns_r_badtime
)
237 PUTSHORT(0, cp
); /*%< Other data length */
239 PUTSHORT(INT16SZ
+INT32SZ
, cp
); /*%< Other data length */
240 BOUNDS_CHECK(cp
, INT32SZ
+INT16SZ
);
241 PUTSHORT(0, cp
); /*%< Top 16 bits of time */
242 PUTLONG(timesigned
, cp
);
245 /* Go back and fill in the length. */
246 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
248 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
249 *msglen
= (cp
- msg
);
254 ns_sign_tcp_init(void *k
, const u_char
*querysig
, int querysiglen
,
255 ns_tcp_tsig_state
*state
)
258 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
262 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
263 return (-ns_r_badkey
);
264 if (querysiglen
> (int)sizeof(state
->sig
))
266 memcpy(state
->sig
, querysig
, querysiglen
);
267 state
->siglen
= querysiglen
;
272 ns_sign_tcp(u_char
*msg
, int *msglen
, int msgsize
, int error
,
273 ns_tcp_tsig_state
*state
, int done
)
275 return (ns_sign_tcp2(msg
, msglen
, msgsize
, error
, state
,
280 ns_sign_tcp2(u_char
*msg
, int *msglen
, int msgsize
, int error
,
281 ns_tcp_tsig_state
*state
, int done
,
282 u_char
**dnptrs
, u_char
**lastdnptr
)
284 u_char
*cp
, *eob
, *lenp
;
285 u_char buf
[MAXDNAME
], *cp2
;
286 HEADER
*hp
= (HEADER
*)msg
;
290 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
294 if (state
->counter
== 0)
295 return (ns_sign2(msg
, msglen
, msgsize
, error
, state
->key
,
296 state
->sig
, state
->siglen
,
297 state
->sig
, &state
->siglen
, 0,
300 if (state
->siglen
> 0) {
301 u_int16_t siglen_n
= htons(state
->siglen
);
302 dst_sign_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
304 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
305 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
306 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
307 state
->sig
, state
->siglen
, NULL
, 0);
311 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
, msg
, *msglen
,
314 if (done
== 0 && (state
->counter
% 100 != 0))
321 n
= dn_comp(state
->key
->dk_key_name
, cp
, eob
- cp
, dnptrs
, lastdnptr
);
323 return (NS_TSIG_ERROR_NO_SPACE
);
326 /* Type, class, ttl, length (not filled in yet). */
327 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
328 PUTSHORT(ns_t_tsig
, cp
);
329 PUTSHORT(ns_c_any
, cp
);
330 PUTLONG(0, cp
); /*%< TTL */
335 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
337 return (NS_TSIG_ERROR_NO_SPACE
);
341 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
343 timesigned
= time(NULL
);
344 PUTLONG(timesigned
, cp
);
345 PUTSHORT(NS_TSIG_FUDGE
, cp
);
348 * Compute the signature.
351 /* Digest the time signed and fudge. */
353 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
354 PUTLONG(timesigned
, cp2
);
355 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
357 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
358 buf
, cp2
- buf
, NULL
, 0);
360 n
= dst_sign_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
361 state
->sig
, sizeof(state
->sig
));
363 return (-ns_r_badkey
);
366 /* Add the signature. */
367 BOUNDS_CHECK(cp
, INT16SZ
+ state
->siglen
);
368 PUTSHORT(state
->siglen
, cp
);
369 memcpy(cp
, state
->sig
, state
->siglen
);
372 /* The original message ID & error. */
373 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
374 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
378 BOUNDS_CHECK(cp
, INT16SZ
);
381 /* Go back and fill in the length. */
382 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
384 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
385 *msglen
= (cp
- msg
);