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.
19 static const char rcsid
[] = "$Id: ns_sign.c,v 1.1.2.2.4.2 2006/03/10 00:17:21 marka Exp $";
24 #include "port_before.h"
25 #include "fd_setsize.h"
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
44 #include <isc/assertions.h>
46 #include "port_after.h"
48 #define BOUNDS_CHECK(ptr, count) \
50 if ((ptr) + (count) > eob) { \
52 return(NS_TSIG_ERROR_NO_SPACE); \
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
, int *msglen
, int msgsize
, int error
, void *k
,
77 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
80 return(ns_sign2(msg
, msglen
, msgsize
, error
, k
,
81 querysig
, querysiglen
, sig
, siglen
,
82 in_timesigned
, NULL
, NULL
));
86 ns_sign2(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
87 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
88 time_t in_timesigned
, u_char
**dnptrs
, u_char
**lastdnptr
)
90 HEADER
*hp
= (HEADER
*)msg
;
91 DST_KEY
*key
= (DST_KEY
*)k
;
97 u_char name
[NS_MAXCDNAME
];
100 if (msg
== NULL
|| msglen
== NULL
|| sig
== NULL
|| siglen
== NULL
)
107 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
108 n
= ns_name_pton(key
->dk_key_name
, name
, sizeof name
);
110 n
= ns_name_pack(name
, cp
, eob
- cp
,
111 (const u_char
**)dnptrs
,
112 (const u_char
**)lastdnptr
);
115 n
= ns_name_pton("", name
, sizeof name
);
117 n
= ns_name_pack(name
, cp
, eob
- cp
, NULL
, NULL
);
120 return (NS_TSIG_ERROR_NO_SPACE
);
123 /* Type, class, ttl, length (not filled in yet). */
124 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
125 PUTSHORT(ns_t_tsig
, cp
);
126 PUTSHORT(ns_c_any
, cp
);
127 PUTLONG(0, cp
); /* TTL */
132 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
133 if (key
->dk_alg
!= KEY_HMAC_MD5
)
134 return (-ns_r_badkey
);
135 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
138 n
= dn_comp("", cp
, eob
- cp
, NULL
, NULL
);
140 return (NS_TSIG_ERROR_NO_SPACE
);
145 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
147 timesigned
= time(NULL
);
148 if (error
!= ns_r_badtime
)
149 PUTLONG(timesigned
, cp
);
151 PUTLONG(in_timesigned
, cp
);
152 PUTSHORT(NS_TSIG_FUDGE
, cp
);
154 /* Compute the signature. */
155 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
157 u_char buf
[NS_MAXCDNAME
], *cp2
;
160 dst_sign_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
162 /* Digest the query signature, if this is a response. */
163 if (querysiglen
> 0 && querysig
!= NULL
) {
164 u_int16_t len_n
= htons(querysiglen
);
165 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
166 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
167 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
168 querysig
, querysiglen
, NULL
, 0);
171 /* Digest the message. */
172 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, *msglen
,
175 /* Digest the key name. */
176 n
= ns_name_ntol(name
, buf
, sizeof(buf
));
178 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
180 /* Digest the class and TTL. */
182 PUTSHORT(ns_c_any
, cp2
);
184 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
187 /* Digest the algorithm. */
188 n
= ns_name_ntol(alg
, buf
, sizeof(buf
));
190 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
192 /* Digest the time signed, fudge, error, and other data */
194 PUTSHORT(0, cp2
); /* Top 16 bits of time */
195 if (error
!= ns_r_badtime
)
196 PUTLONG(timesigned
, cp2
);
198 PUTLONG(in_timesigned
, cp2
);
199 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
200 PUTSHORT(error
, cp2
); /* Error */
201 if (error
!= ns_r_badtime
)
202 PUTSHORT(0, cp2
); /* Other data length */
204 PUTSHORT(INT16SZ
+INT32SZ
, cp2
); /* Other data length */
205 PUTSHORT(0, cp2
); /* Top 16 bits of time */
206 PUTLONG(timesigned
, cp2
);
208 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
211 n
= dst_sign_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
214 return (-ns_r_badkey
);
219 /* Add the signature. */
220 BOUNDS_CHECK(cp
, INT16SZ
+ (*siglen
));
221 PUTSHORT(*siglen
, cp
);
222 memcpy(cp
, sig
, *siglen
);
225 /* The original message ID & error. */
226 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
227 PUTSHORT(ntohs(hp
->id
), cp
); /* already in network order */
231 BOUNDS_CHECK(cp
, INT16SZ
);
232 if (error
!= ns_r_badtime
)
233 PUTSHORT(0, cp
); /* Other data length */
235 PUTSHORT(INT16SZ
+INT32SZ
, cp
); /* Other data length */
236 BOUNDS_CHECK(cp
, INT32SZ
+INT16SZ
);
237 PUTSHORT(0, cp
); /* Top 16 bits of time */
238 PUTLONG(timesigned
, cp
);
241 /* Go back and fill in the length. */
242 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
244 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
245 *msglen
= (cp
- msg
);
250 ns_sign_tcp_init(void *k
, const u_char
*querysig
, int querysiglen
,
251 ns_tcp_tsig_state
*state
)
254 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
258 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
259 return (-ns_r_badkey
);
260 if (querysiglen
> (int)sizeof(state
->sig
))
262 memcpy(state
->sig
, querysig
, querysiglen
);
263 state
->siglen
= querysiglen
;
268 ns_sign_tcp(u_char
*msg
, int *msglen
, int msgsize
, int error
,
269 ns_tcp_tsig_state
*state
, int done
)
271 return (ns_sign_tcp2(msg
, msglen
, msgsize
, error
, state
,
276 ns_sign_tcp2(u_char
*msg
, int *msglen
, int msgsize
, int error
,
277 ns_tcp_tsig_state
*state
, int done
,
278 u_char
**dnptrs
, u_char
**lastdnptr
)
280 u_char
*cp
, *eob
, *lenp
;
281 u_char buf
[MAXDNAME
], *cp2
;
282 HEADER
*hp
= (HEADER
*)msg
;
286 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
290 if (state
->counter
== 0)
291 return (ns_sign2(msg
, msglen
, msgsize
, error
, state
->key
,
292 state
->sig
, state
->siglen
,
293 state
->sig
, &state
->siglen
, 0,
296 if (state
->siglen
> 0) {
297 u_int16_t siglen_n
= htons(state
->siglen
);
298 dst_sign_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
300 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
301 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
302 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
303 state
->sig
, state
->siglen
, NULL
, 0);
307 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
, msg
, *msglen
,
310 if (done
== 0 && (state
->counter
% 100 != 0))
317 n
= dn_comp(state
->key
->dk_key_name
, cp
, eob
- cp
, dnptrs
, lastdnptr
);
319 return (NS_TSIG_ERROR_NO_SPACE
);
322 /* Type, class, ttl, length (not filled in yet). */
323 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
324 PUTSHORT(ns_t_tsig
, cp
);
325 PUTSHORT(ns_c_any
, cp
);
326 PUTLONG(0, cp
); /* TTL */
331 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
333 return (NS_TSIG_ERROR_NO_SPACE
);
337 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
339 timesigned
= time(NULL
);
340 PUTLONG(timesigned
, cp
);
341 PUTSHORT(NS_TSIG_FUDGE
, cp
);
344 * Compute the signature.
347 /* Digest the time signed and fudge. */
349 PUTSHORT(0, cp2
); /* Top 16 bits of time */
350 PUTLONG(timesigned
, cp2
);
351 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
353 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
354 buf
, cp2
- buf
, NULL
, 0);
356 n
= dst_sign_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
357 state
->sig
, sizeof(state
->sig
));
359 return (-ns_r_badkey
);
362 /* Add the signature. */
363 BOUNDS_CHECK(cp
, INT16SZ
+ state
->siglen
);
364 PUTSHORT(state
->siglen
, cp
);
365 memcpy(cp
, state
->sig
, state
->siglen
);
368 /* The original message ID & error. */
369 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
370 PUTSHORT(ntohs(hp
->id
), cp
); /* already in network order */
374 BOUNDS_CHECK(cp
, INT16SZ
);
377 /* Go back and fill in the length. */
378 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
380 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
381 *msglen
= (cp
- msg
);