1 // Copyright (c) 2002-2004 Brian Wellington (bwelling (at) xbill.org) 2 3 package org.xbill.DNS; 4 5 import java.io.*; 6 import org.xbill.DNS.utils.*; 7 8 /** 9 * DS - contains a Delegation Signer record, which acts as a 10 * placeholder for KEY records in the parent zone. 11 * @see DNSSEC 12 * 13 * @author David Blacka 14 * @author Brian Wellington 15 */ 16 17 public class DSRecord extends Record { 18 19 public static class Digest { 20 private Digest() {} 21 22 /** SHA-1 */ 23 public static final int SHA1 = 1; 24 25 /** SHA-256 */ 26 public static final int SHA256 = 2; 27 28 /** SHA-384 */ 29 public static final int SHA384 = 4; 30 } 31 32 public static final int SHA1_DIGEST_ID = Digest.SHA1; 33 public static final int SHA256_DIGEST_ID = Digest.SHA256; 34 public static final int SHA384_DIGEST_ID = Digest.SHA384; 35 36 private static final long serialVersionUID = -9001819329700081493L; 37 38 private int footprint; 39 private int alg; 40 private int digestid; 41 private byte [] digest; 42 43 DSRecord() {} 44 45 Record 46 getObject() { 47 return new DSRecord(); 48 } 49 50 /** 51 * Creates a DS Record from the given data 52 * @param footprint The original KEY record's footprint (keyid). 53 * @param alg The original key algorithm. 54 * @param digestid The digest id code. 55 * @param digest A hash of the original key. 56 */ 57 public 58 DSRecord(Name name, int dclass, long ttl, int footprint, int alg, 59 int digestid, byte [] digest) 60 { 61 super(name, Type.DS, dclass, ttl); 62 this.footprint = checkU16("footprint", footprint); 63 this.alg = checkU8("alg", alg); 64 this.digestid = checkU8("digestid", digestid); 65 this.digest = digest; 66 } 67 68 /** 69 * Creates a DS Record from the given data 70 * @param digestid The digest id code. 71 * @param key The key to digest 72 */ 73 public 74 DSRecord(Name name, int dclass, long ttl, int digestid, DNSKEYRecord key) 75 { 76 this(name, dclass, ttl, key.getFootprint(), key.getAlgorithm(), 77 digestid, DNSSEC.generateDSDigest(key, digestid)); 78 } 79 80 void 81 rrFromWire(DNSInput in) throws IOException { 82 footprint = in.readU16(); 83 alg = in.readU8(); 84 digestid = in.readU8(); 85 digest = in.readByteArray(); 86 } 87 88 void 89 rdataFromString(Tokenizer st, Name origin) throws IOException { 90 footprint = st.getUInt16(); 91 alg = st.getUInt8(); 92 digestid = st.getUInt8(); 93 digest = st.getHex(); 94 } 95 96 /** 97 * Converts rdata to a String 98 */ 99 String 100 rrToString() { 101 StringBuffer sb = new StringBuffer(); 102 sb.append(footprint); 103 sb.append(" "); 104 sb.append(alg); 105 sb.append(" "); 106 sb.append(digestid); 107 if (digest != null) { 108 sb.append(" "); 109 sb.append(base16.toString(digest)); 110 } 111 112 return sb.toString(); 113 } 114 115 /** 116 * Returns the key's algorithm. 117 */ 118 public int 119 getAlgorithm() { 120 return alg; 121 } 122 123 /** 124 * Returns the key's Digest ID. 125 */ 126 public int 127 getDigestID() 128 { 129 return digestid; 130 } 131 132 /** 133 * Returns the binary hash of the key. 134 */ 135 public byte [] 136 getDigest() { 137 return digest; 138 } 139 140 /** 141 * Returns the key's footprint. 142 */ 143 public int 144 getFootprint() { 145 return footprint; 146 } 147 148 void 149 rrToWire(DNSOutput out, Compression c, boolean canonical) { 150 out.writeU16(footprint); 151 out.writeU8(alg); 152 out.writeU8(digestid); 153 if (digest != null) 154 out.writeByteArray(digest); 155 } 156 157 } 158