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.6 2006/03/09 23:57:56 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); \
60 *\li msg message to be sent
61 *\li msglen input - length of message
62 * output - length of signed message
63 *\li msgsize length of buffer containing message
64 *\li error value to put in the error field
65 *\li key tsig key used for signing
66 *\li querysig (response), the signature in the query
67 *\li querysiglen (response), the length of the signature in the query
68 *\li sig a buffer to hold the generated signature
69 *\li siglen input - length of signature buffer
70 * output - length of signature
73 *\li - bad input data (-1)
74 *\li - bad key / sign failed (-BADKEY)
75 *\li - not enough space (NS_TSIG_ERROR_NO_SPACE)
78 ns_sign(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
79 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
82 return(ns_sign2(msg
, msglen
, msgsize
, error
, k
,
83 querysig
, querysiglen
, sig
, siglen
,
84 in_timesigned
, NULL
, NULL
));
88 ns_sign2(u_char
*msg
, int *msglen
, int msgsize
, int error
, void *k
,
89 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
90 time_t in_timesigned
, u_char
**dnptrs
, u_char
**lastdnptr
)
92 HEADER
*hp
= (HEADER
*)msg
;
93 DST_KEY
*key
= (DST_KEY
*)k
;
99 u_char name
[NS_MAXCDNAME
];
102 if (msg
== NULL
|| msglen
== NULL
|| sig
== NULL
|| siglen
== NULL
)
109 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
110 n
= ns_name_pton(key
->dk_key_name
, name
, sizeof name
);
112 n
= ns_name_pack(name
, cp
, eob
- cp
,
113 (const u_char
**)dnptrs
,
114 (const u_char
**)lastdnptr
);
117 n
= ns_name_pton("", name
, sizeof name
);
119 n
= ns_name_pack(name
, cp
, eob
- cp
, NULL
, NULL
);
122 return (NS_TSIG_ERROR_NO_SPACE
);
125 /* Type, class, ttl, length (not filled in yet). */
126 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
127 PUTSHORT(ns_t_tsig
, cp
);
128 PUTSHORT(ns_c_any
, cp
);
129 PUTLONG(0, cp
); /*%< TTL */
134 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
135 if (key
->dk_alg
!= KEY_HMAC_MD5
)
136 return (-ns_r_badkey
);
137 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
140 n
= dn_comp("", cp
, eob
- cp
, NULL
, NULL
);
142 return (NS_TSIG_ERROR_NO_SPACE
);
147 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
149 timesigned
= time(NULL
);
150 if (error
!= ns_r_badtime
)
151 PUTLONG(timesigned
, cp
);
153 PUTLONG(in_timesigned
, cp
);
154 PUTSHORT(NS_TSIG_FUDGE
, cp
);
156 /* Compute the signature. */
157 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
159 u_char buf
[NS_MAXCDNAME
], *cp2
;
162 dst_sign_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
164 /* Digest the query signature, if this is a response. */
165 if (querysiglen
> 0 && querysig
!= NULL
) {
166 u_int16_t len_n
= htons(querysiglen
);
167 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
168 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
169 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
,
170 querysig
, querysiglen
, NULL
, 0);
173 /* Digest the message. */
174 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, *msglen
,
177 /* Digest the key name. */
178 n
= ns_name_ntol(name
, buf
, sizeof(buf
));
180 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
182 /* Digest the class and TTL. */
184 PUTSHORT(ns_c_any
, cp2
);
186 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
189 /* Digest the algorithm. */
190 n
= ns_name_ntol(alg
, buf
, sizeof(buf
));
192 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
194 /* Digest the time signed, fudge, error, and other data */
196 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
197 if (error
!= ns_r_badtime
)
198 PUTLONG(timesigned
, cp2
);
200 PUTLONG(in_timesigned
, cp2
);
201 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
202 PUTSHORT(error
, cp2
); /*%< Error */
203 if (error
!= ns_r_badtime
)
204 PUTSHORT(0, cp2
); /*%< Other data length */
206 PUTSHORT(INT16SZ
+INT32SZ
, cp2
); /*%< Other data length */
207 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
208 PUTLONG(timesigned
, cp2
);
210 dst_sign_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, cp2
-buf
,
213 n
= dst_sign_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
216 return (-ns_r_badkey
);
221 /* Add the signature. */
222 BOUNDS_CHECK(cp
, INT16SZ
+ (*siglen
));
223 PUTSHORT(*siglen
, cp
);
224 memcpy(cp
, sig
, *siglen
);
227 /* The original message ID & error. */
228 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
229 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
233 BOUNDS_CHECK(cp
, INT16SZ
);
234 if (error
!= ns_r_badtime
)
235 PUTSHORT(0, cp
); /*%< Other data length */
237 PUTSHORT(INT16SZ
+INT32SZ
, cp
); /*%< Other data length */
238 BOUNDS_CHECK(cp
, INT32SZ
+INT16SZ
);
239 PUTSHORT(0, cp
); /*%< Top 16 bits of time */
240 PUTLONG(timesigned
, cp
);
243 /* Go back and fill in the length. */
244 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
246 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
247 *msglen
= (cp
- msg
);
252 ns_sign_tcp_init(void *k
, const u_char
*querysig
, int querysiglen
,
253 ns_tcp_tsig_state
*state
)
256 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
260 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
261 return (-ns_r_badkey
);
262 if (querysiglen
> (int)sizeof(state
->sig
))
264 memcpy(state
->sig
, querysig
, querysiglen
);
265 state
->siglen
= querysiglen
;
270 ns_sign_tcp(u_char
*msg
, int *msglen
, int msgsize
, int error
,
271 ns_tcp_tsig_state
*state
, int done
)
273 return (ns_sign_tcp2(msg
, msglen
, msgsize
, error
, state
,
278 ns_sign_tcp2(u_char
*msg
, int *msglen
, int msgsize
, int error
,
279 ns_tcp_tsig_state
*state
, int done
,
280 u_char
**dnptrs
, u_char
**lastdnptr
)
282 u_char
*cp
, *eob
, *lenp
;
283 u_char buf
[MAXDNAME
], *cp2
;
284 HEADER
*hp
= (HEADER
*)msg
;
288 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
292 if (state
->counter
== 0)
293 return (ns_sign2(msg
, msglen
, msgsize
, error
, state
->key
,
294 state
->sig
, state
->siglen
,
295 state
->sig
, &state
->siglen
, 0,
298 if (state
->siglen
> 0) {
299 u_int16_t siglen_n
= htons(state
->siglen
);
300 dst_sign_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
302 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
303 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
304 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
305 state
->sig
, state
->siglen
, NULL
, 0);
309 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
, msg
, *msglen
,
312 if (done
== 0 && (state
->counter
% 100 != 0))
319 n
= dn_comp(state
->key
->dk_key_name
, cp
, eob
- cp
, dnptrs
, lastdnptr
);
321 return (NS_TSIG_ERROR_NO_SPACE
);
324 /* Type, class, ttl, length (not filled in yet). */
325 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
+ INT32SZ
+ INT16SZ
);
326 PUTSHORT(ns_t_tsig
, cp
);
327 PUTSHORT(ns_c_any
, cp
);
328 PUTLONG(0, cp
); /*%< TTL */
333 n
= dn_comp(NS_TSIG_ALG_HMAC_MD5
, cp
, eob
- cp
, NULL
, NULL
);
335 return (NS_TSIG_ERROR_NO_SPACE
);
339 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
341 timesigned
= time(NULL
);
342 PUTLONG(timesigned
, cp
);
343 PUTSHORT(NS_TSIG_FUDGE
, cp
);
346 * Compute the signature.
349 /* Digest the time signed and fudge. */
351 PUTSHORT(0, cp2
); /*%< Top 16 bits of time */
352 PUTLONG(timesigned
, cp2
);
353 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
355 dst_sign_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
356 buf
, cp2
- buf
, NULL
, 0);
358 n
= dst_sign_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
359 state
->sig
, sizeof(state
->sig
));
361 return (-ns_r_badkey
);
364 /* Add the signature. */
365 BOUNDS_CHECK(cp
, INT16SZ
+ state
->siglen
);
366 PUTSHORT(state
->siglen
, cp
);
367 memcpy(cp
, state
->sig
, state
->siglen
);
370 /* The original message ID & error. */
371 BOUNDS_CHECK(cp
, INT16SZ
+ INT16SZ
);
372 PUTSHORT(ntohs(hp
->id
), cp
); /*%< already in network order */
376 BOUNDS_CHECK(cp
, INT16SZ
);
379 /* Go back and fill in the length. */
380 PUTSHORT(cp
- lenp
- INT16SZ
, lenp
);
382 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
383 *msglen
= (cp
- msg
);