Ethereal-dev: [Ethereal-dev] Computing key id in DNS KEY RR

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: David Fort <david.fort@xxxxxxxx>
Date: Wed, 10 Dec 2003 19:11:52 +0100
With this little patch you can see the id of a key in a DNS KEY RR. This
is against the last ethereal snapshot.

--
Projet IDsA
IRISA-INRIA, Campus de Beaulieu, 35042 Rennes cedex, France
T�l: +33 (0) 2 99 84 71 00, Fax: +33 (0) 2 99 84 71 71


--- packet-dns.c.old	2003-11-27 16:02:34.000000000 -0500
+++ packet-dns.c	2003-12-10 18:50:13.803867120 -0500
@@ -888,6 +888,43 @@
 	  { 0,                   NULL }
 };
 
+/**
+ *   Compute the key id of a KEY RR depending of the algo used
+ *
+ *
+ *
+ */
+static u_int16_t compute_key_id( tvbuff_t *tvb, int offset, int size, const int algo ) 
+{
+  u_int32_t ac;
+  unsigned char c1, c2;
+
+  if( !tvb || size < 4 ) {
+    return 0;
+  }
+  
+  switch( algo ) {
+     case DNS_ALGO_RSAMD5:
+       return (tvb_get_guint8(tvb, offset + size - 3) << 8) + tvb_get_guint8( tvb, offset + size - 2 );
+     case DNS_ALGO_RSASHA1:
+       for (ac = 0; size > 1; size -= 2, offset += 2) {
+	 c1 = tvb_get_guint8( tvb, offset );
+	 c2 = tvb_get_guint8( tvb, offset + 1 );
+	 ac +=  (c1 << 8) + c2 ;
+       }
+       if (size > 0) {
+	 c1 = tvb_get_guint8( tvb, offset );
+	 ac += c1 << 8;
+       }
+       ac += (ac >> 16) & 0xffff;
+       return ((u_int16_t)(ac & 0xffff));
+     default:
+       printf("%s - %s: Can't compute key id for algo %u\n", , __FILE__, __func, algo );
+  }
+  return 0;
+}
+
+
 static int
 dissect_dns_answer(tvbuff_t *tvb, int offset, int dns_data_offset,
   column_info *cinfo, proto_tree *dns_tree, packet_info *pinfo)
@@ -1252,7 +1289,7 @@
 	cur_offset += 4;
 	rr_len -= 4;
 
-	proto_tree_add_text(rr_tree, tvb, cur_offset, 2, "Key footprint: 0x%04x",
+	proto_tree_add_text(rr_tree, tvb, cur_offset, 2, "Id of signing key(footprint): %u",
 		tvb_get_ntohs(tvb, cur_offset));
 	cur_offset += 2;
 	rr_len -= 2;
@@ -1274,6 +1311,7 @@
       guint16 flags;
       proto_item *tf;
       proto_tree *flags_tree;
+      int algo;
 
       if (dns_tree != NULL) {
         flags = tvb_get_ntohs(tvb, cur_offset);
@@ -1325,12 +1363,13 @@
 	cur_offset += 1;
 	rr_len -= 1;
 
+	algo = tvb_get_guint8(tvb, cur_offset);
 	proto_tree_add_text(rr_tree, tvb, cur_offset, 1, "Algorithm: %s",
-		val_to_str(tvb_get_guint8(tvb, cur_offset), algo_vals,
-	            "Unknown (0x%02X)"));
+		val_to_str(algo, algo_vals, "Unknown (0x%02X)"));
 	cur_offset += 1;
 		rr_len -= 1;
 
+	proto_tree_add_text(rr_tree, tvb, cur_offset-4, rr_len+4, "Key id: %u", compute_key_id(tvb, cur_offset-4, rr_len+4, algo) );
 	proto_tree_add_text(rr_tree, tvb, cur_offset, rr_len, "Public key");
       }
     }