PageRenderTime 59ms CodeModel.GetById 29ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bind9/lib/dns/rdata/generic/sig_24.c

https://bitbucket.org/freebsd/freebsd-head/
C | 582 lines | 321 code | 103 blank | 158 comment | 59 complexity | e072b96291f6fcc384477ac891f5b010 MD5 | raw file
  1/*
  2 * Copyright (C) 2004, 2005, 2007, 2009, 2012  Internet Systems Consortium, Inc. ("ISC")
  3 * Copyright (C) 1999-2003  Internet Software Consortium.
  4 *
  5 * Permission to use, copy, modify, and/or 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.
  8 *
  9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 15 * PERFORMANCE OF THIS SOFTWARE.
 16 */
 17
 18/* $Id$ */
 19
 20/* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
 21
 22/* RFC2535 */
 23
 24#ifndef RDATA_GENERIC_SIG_24_C
 25#define RDATA_GENERIC_SIG_24_C
 26
 27#define RRTYPE_SIG_ATTRIBUTES (0)
 28
 29static inline isc_result_t
 30fromtext_sig(ARGS_FROMTEXT) {
 31	isc_token_t token;
 32	unsigned char c;
 33	long i;
 34	dns_rdatatype_t covered;
 35	char *e;
 36	isc_result_t result;
 37	dns_name_t name;
 38	isc_buffer_t buffer;
 39	isc_uint32_t time_signed, time_expire;
 40
 41	REQUIRE(type == 24);
 42
 43	UNUSED(type);
 44	UNUSED(rdclass);
 45	UNUSED(callbacks);
 46
 47	/*
 48	 * Type covered.
 49	 */
 50	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
 51				      ISC_FALSE));
 52	result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
 53	if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
 54		i = strtol(DNS_AS_STR(token), &e, 10);
 55		if (i < 0 || i > 65535)
 56			RETTOK(ISC_R_RANGE);
 57		if (*e != 0)
 58			RETTOK(result);
 59		covered = (dns_rdatatype_t)i;
 60	}
 61	RETERR(uint16_tobuffer(covered, target));
 62
 63	/*
 64	 * Algorithm.
 65	 */
 66	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
 67				      ISC_FALSE));
 68	RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
 69	RETERR(mem_tobuffer(target, &c, 1));
 70
 71	/*
 72	 * Labels.
 73	 */
 74	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
 75				      ISC_FALSE));
 76	if (token.value.as_ulong > 0xffU)
 77		RETTOK(ISC_R_RANGE);
 78	c = (unsigned char)token.value.as_ulong;
 79	RETERR(mem_tobuffer(target, &c, 1));
 80
 81	/*
 82	 * Original ttl.
 83	 */
 84	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
 85				      ISC_FALSE));
 86	RETERR(uint32_tobuffer(token.value.as_ulong, target));
 87
 88	/*
 89	 * Signature expiration.
 90	 */
 91	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
 92				      ISC_FALSE));
 93	RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
 94	RETERR(uint32_tobuffer(time_expire, target));
 95
 96	/*
 97	 * Time signed.
 98	 */
 99	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
100				      ISC_FALSE));
101	RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
102	RETERR(uint32_tobuffer(time_signed, target));
103
104	/*
105	 * Key footprint.
106	 */
107	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
108				      ISC_FALSE));
109	RETERR(uint16_tobuffer(token.value.as_ulong, target));
110
111	/*
112	 * Signer.
113	 */
114	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
115				      ISC_FALSE));
116	dns_name_init(&name, NULL);
117	buffer_fromregion(&buffer, &token.value.as_region);
118	origin = (origin != NULL) ? origin : dns_rootname;
119	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
120
121	/*
122	 * Sig.
123	 */
124	return (isc_base64_tobuffer(lexer, target, -1));
125}
126
127static inline isc_result_t
128totext_sig(ARGS_TOTEXT) {
129	isc_region_t sr;
130	char buf[sizeof("4294967295")];
131	dns_rdatatype_t covered;
132	unsigned long ttl;
133	unsigned long when;
134	unsigned long exp;
135	unsigned long foot;
136	dns_name_t name;
137	dns_name_t prefix;
138	isc_boolean_t sub;
139
140	REQUIRE(rdata->type == 24);
141	REQUIRE(rdata->length != 0);
142
143	dns_rdata_toregion(rdata, &sr);
144
145	/*
146	 * Type covered.
147	 */
148	covered = uint16_fromregion(&sr);
149	isc_region_consume(&sr, 2);
150	/*
151	 * XXXAG We should have something like dns_rdatatype_isknown()
152	 * that does the right thing with type 0.
153	 */
154	if (dns_rdatatype_isknown(covered) && covered != 0) {
155		RETERR(dns_rdatatype_totext(covered, target));
156	} else {
157		char buf[sizeof("65535")];
158		sprintf(buf, "%u", covered);
159		RETERR(str_totext(buf, target));
160	}
161	RETERR(str_totext(" ", target));
162
163	/*
164	 * Algorithm.
165	 */
166	sprintf(buf, "%u", sr.base[0]);
167	isc_region_consume(&sr, 1);
168	RETERR(str_totext(buf, target));
169	RETERR(str_totext(" ", target));
170
171	/*
172	 * Labels.
173	 */
174	sprintf(buf, "%u", sr.base[0]);
175	isc_region_consume(&sr, 1);
176	RETERR(str_totext(buf, target));
177	RETERR(str_totext(" ", target));
178
179	/*
180	 * Ttl.
181	 */
182	ttl = uint32_fromregion(&sr);
183	isc_region_consume(&sr, 4);
184	sprintf(buf, "%lu", ttl);
185	RETERR(str_totext(buf, target));
186	RETERR(str_totext(" ", target));
187
188	/*
189	 * Sig exp.
190	 */
191	exp = uint32_fromregion(&sr);
192	isc_region_consume(&sr, 4);
193	RETERR(dns_time32_totext(exp, target));
194
195	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
196		RETERR(str_totext(" (", target));
197	RETERR(str_totext(tctx->linebreak, target));
198
199	/*
200	 * Time signed.
201	 */
202	when = uint32_fromregion(&sr);
203	isc_region_consume(&sr, 4);
204	RETERR(dns_time32_totext(when, target));
205	RETERR(str_totext(" ", target));
206
207	/*
208	 * Footprint.
209	 */
210	foot = uint16_fromregion(&sr);
211	isc_region_consume(&sr, 2);
212	sprintf(buf, "%lu", foot);
213	RETERR(str_totext(buf, target));
214	RETERR(str_totext(" ", target));
215
216	/*
217	 * Signer.
218	 */
219	dns_name_init(&name, NULL);
220	dns_name_init(&prefix, NULL);
221	dns_name_fromregion(&name, &sr);
222	isc_region_consume(&sr, name_length(&name));
223	sub = name_prefix(&name, tctx->origin, &prefix);
224	RETERR(dns_name_totext(&prefix, sub, target));
225
226	/*
227	 * Sig.
228	 */
229	RETERR(str_totext(tctx->linebreak, target));
230	RETERR(isc_base64_totext(&sr, tctx->width - 2,
231				    tctx->linebreak, target));
232	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
233		RETERR(str_totext(" )", target));
234
235	return (ISC_R_SUCCESS);
236}
237
238static inline isc_result_t
239fromwire_sig(ARGS_FROMWIRE) {
240	isc_region_t sr;
241	dns_name_t name;
242
243	REQUIRE(type == 24);
244
245	UNUSED(type);
246	UNUSED(rdclass);
247
248	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
249
250	isc_buffer_activeregion(source, &sr);
251	/*
252	 * type covered: 2
253	 * algorithm: 1
254	 * labels: 1
255	 * original ttl: 4
256	 * signature expiration: 4
257	 * time signed: 4
258	 * key footprint: 2
259	 */
260	if (sr.length < 18)
261		return (ISC_R_UNEXPECTEDEND);
262
263	isc_buffer_forward(source, 18);
264	RETERR(mem_tobuffer(target, sr.base, 18));
265
266	/*
267	 * Signer.
268	 */
269	dns_name_init(&name, NULL);
270	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
271
272	/*
273	 * Sig.
274	 */
275	isc_buffer_activeregion(source, &sr);
276	isc_buffer_forward(source, sr.length);
277	return (mem_tobuffer(target, sr.base, sr.length));
278}
279
280static inline isc_result_t
281towire_sig(ARGS_TOWIRE) {
282	isc_region_t sr;
283	dns_name_t name;
284	dns_offsets_t offsets;
285
286	REQUIRE(rdata->type == 24);
287	REQUIRE(rdata->length != 0);
288
289	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
290	dns_rdata_toregion(rdata, &sr);
291	/*
292	 * type covered: 2
293	 * algorithm: 1
294	 * labels: 1
295	 * original ttl: 4
296	 * signature expiration: 4
297	 * time signed: 4
298	 * key footprint: 2
299	 */
300	RETERR(mem_tobuffer(target, sr.base, 18));
301	isc_region_consume(&sr, 18);
302
303	/*
304	 * Signer.
305	 */
306	dns_name_init(&name, offsets);
307	dns_name_fromregion(&name, &sr);
308	isc_region_consume(&sr, name_length(&name));
309	RETERR(dns_name_towire(&name, cctx, target));
310
311	/*
312	 * Signature.
313	 */
314	return (mem_tobuffer(target, sr.base, sr.length));
315}
316
317static inline int
318compare_sig(ARGS_COMPARE) {
319	isc_region_t r1;
320	isc_region_t r2;
321	dns_name_t name1;
322	dns_name_t name2;
323	int order;
324
325	REQUIRE(rdata1->type == rdata2->type);
326	REQUIRE(rdata1->rdclass == rdata2->rdclass);
327	REQUIRE(rdata1->type == 24);
328	REQUIRE(rdata1->length != 0);
329	REQUIRE(rdata2->length != 0);
330
331	dns_rdata_toregion(rdata1, &r1);
332	dns_rdata_toregion(rdata2, &r2);
333
334	INSIST(r1.length > 18);
335	INSIST(r2.length > 18);
336	r1.length = 18;
337	r2.length = 18;
338	order = isc_region_compare(&r1, &r2);
339	if (order != 0)
340		return (order);
341
342	dns_name_init(&name1, NULL);
343	dns_name_init(&name2, NULL);
344	dns_rdata_toregion(rdata1, &r1);
345	dns_rdata_toregion(rdata2, &r2);
346	isc_region_consume(&r1, 18);
347	isc_region_consume(&r2, 18);
348	dns_name_fromregion(&name1, &r1);
349	dns_name_fromregion(&name2, &r2);
350	order = dns_name_rdatacompare(&name1, &name2);
351	if (order != 0)
352		return (order);
353
354	isc_region_consume(&r1, name_length(&name1));
355	isc_region_consume(&r2, name_length(&name2));
356
357	return (isc_region_compare(&r1, &r2));
358}
359
360static inline isc_result_t
361fromstruct_sig(ARGS_FROMSTRUCT) {
362	dns_rdata_sig_t *sig = source;
363
364	REQUIRE(type == 24);
365	REQUIRE(source != NULL);
366	REQUIRE(sig->common.rdtype == type);
367	REQUIRE(sig->common.rdclass == rdclass);
368	REQUIRE(sig->signature != NULL || sig->siglen == 0);
369
370	UNUSED(type);
371	UNUSED(rdclass);
372
373	/*
374	 * Type covered.
375	 */
376	RETERR(uint16_tobuffer(sig->covered, target));
377
378	/*
379	 * Algorithm.
380	 */
381	RETERR(uint8_tobuffer(sig->algorithm, target));
382
383	/*
384	 * Labels.
385	 */
386	RETERR(uint8_tobuffer(sig->labels, target));
387
388	/*
389	 * Original TTL.
390	 */
391	RETERR(uint32_tobuffer(sig->originalttl, target));
392
393	/*
394	 * Expire time.
395	 */
396	RETERR(uint32_tobuffer(sig->timeexpire, target));
397
398	/*
399	 * Time signed.
400	 */
401	RETERR(uint32_tobuffer(sig->timesigned, target));
402
403	/*
404	 * Key ID.
405	 */
406	RETERR(uint16_tobuffer(sig->keyid, target));
407
408	/*
409	 * Signer name.
410	 */
411	RETERR(name_tobuffer(&sig->signer, target));
412
413	/*
414	 * Signature.
415	 */
416	return (mem_tobuffer(target, sig->signature, sig->siglen));
417}
418
419static inline isc_result_t
420tostruct_sig(ARGS_TOSTRUCT) {
421	isc_region_t sr;
422	dns_rdata_sig_t *sig = target;
423	dns_name_t signer;
424
425	REQUIRE(rdata->type == 24);
426	REQUIRE(target != NULL);
427	REQUIRE(rdata->length != 0);
428
429	sig->common.rdclass = rdata->rdclass;
430	sig->common.rdtype = rdata->type;
431	ISC_LINK_INIT(&sig->common, link);
432
433	dns_rdata_toregion(rdata, &sr);
434
435	/*
436	 * Type covered.
437	 */
438	sig->covered = uint16_fromregion(&sr);
439	isc_region_consume(&sr, 2);
440
441	/*
442	 * Algorithm.
443	 */
444	sig->algorithm = uint8_fromregion(&sr);
445	isc_region_consume(&sr, 1);
446
447	/*
448	 * Labels.
449	 */
450	sig->labels = uint8_fromregion(&sr);
451	isc_region_consume(&sr, 1);
452
453	/*
454	 * Original TTL.
455	 */
456	sig->originalttl = uint32_fromregion(&sr);
457	isc_region_consume(&sr, 4);
458
459	/*
460	 * Expire time.
461	 */
462	sig->timeexpire = uint32_fromregion(&sr);
463	isc_region_consume(&sr, 4);
464
465	/*
466	 * Time signed.
467	 */
468	sig->timesigned = uint32_fromregion(&sr);
469	isc_region_consume(&sr, 4);
470
471	/*
472	 * Key ID.
473	 */
474	sig->keyid = uint16_fromregion(&sr);
475	isc_region_consume(&sr, 2);
476
477	dns_name_init(&signer, NULL);
478	dns_name_fromregion(&signer, &sr);
479	dns_name_init(&sig->signer, NULL);
480	RETERR(name_duporclone(&signer, mctx, &sig->signer));
481	isc_region_consume(&sr, name_length(&sig->signer));
482
483	/*
484	 * Signature.
485	 */
486	sig->siglen = sr.length;
487	sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
488	if (sig->signature == NULL)
489		goto cleanup;
490
491
492	sig->mctx = mctx;
493	return (ISC_R_SUCCESS);
494
495 cleanup:
496	if (mctx != NULL)
497		dns_name_free(&sig->signer, mctx);
498	return (ISC_R_NOMEMORY);
499}
500
501static inline void
502freestruct_sig(ARGS_FREESTRUCT) {
503	dns_rdata_sig_t *sig = (dns_rdata_sig_t *) source;
504
505	REQUIRE(source != NULL);
506	REQUIRE(sig->common.rdtype == 24);
507
508	if (sig->mctx == NULL)
509		return;
510
511	dns_name_free(&sig->signer, sig->mctx);
512	if (sig->signature != NULL)
513		isc_mem_free(sig->mctx, sig->signature);
514	sig->mctx = NULL;
515}
516
517static inline isc_result_t
518additionaldata_sig(ARGS_ADDLDATA) {
519	REQUIRE(rdata->type == 24);
520
521	UNUSED(rdata);
522	UNUSED(add);
523	UNUSED(arg);
524
525	return (ISC_R_SUCCESS);
526}
527
528static inline isc_result_t
529digest_sig(ARGS_DIGEST) {
530
531	REQUIRE(rdata->type == 24);
532
533	UNUSED(rdata);
534	UNUSED(digest);
535	UNUSED(arg);
536
537	return (ISC_R_NOTIMPLEMENTED);
538}
539
540static inline dns_rdatatype_t
541covers_sig(dns_rdata_t *rdata) {
542	dns_rdatatype_t type;
543	isc_region_t r;
544
545	REQUIRE(rdata->type == 24);
546
547	dns_rdata_toregion(rdata, &r);
548	type = uint16_fromregion(&r);
549
550	return (type);
551}
552
553static inline isc_boolean_t
554checkowner_sig(ARGS_CHECKOWNER) {
555
556	REQUIRE(type == 24);
557
558	UNUSED(name);
559	UNUSED(type);
560	UNUSED(rdclass);
561	UNUSED(wildcard);
562
563	return (ISC_TRUE);
564}
565
566static inline isc_boolean_t
567checknames_sig(ARGS_CHECKNAMES) {
568
569	REQUIRE(rdata->type == 24);
570
571	UNUSED(rdata);
572	UNUSED(owner);
573	UNUSED(bad);
574
575	return (ISC_TRUE);
576}
577
578static inline int
579casecompare_sig(ARGS_COMPARE) {
580	return (compare_sig(rdata1, rdata2));
581}
582#endif	/* RDATA_GENERIC_SIG_24_C */