1d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org) 2d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 3d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpackage org.xbill.DNS; 4d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 5d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.io.*; 6d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport java.util.*; 7d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.xbill.DNS.utils.*; 8d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 9d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** 10d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * The base class for SIG/RRSIG records, which have identical formats 11d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * 12d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @author Brian Wellington 13d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 14d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 15d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenabstract class SIGBase extends Record { 16d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 17d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprivate static final long serialVersionUID = -3738444391533812369L; 18d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 19d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected int covered; 20d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected int alg, labels; 21d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected long origttl; 22d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected Date expire, timeSigned; 23d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected int footprint; 24d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected Name signer; 25d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected byte [] signature; 26d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 27d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprotected 28d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenSIGBase() {} 29d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 30d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic 31d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenSIGBase(Name name, int type, int dclass, long ttl, int covered, int alg, 32d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen long origttl, Date expire, Date timeSigned, int footprint, Name signer, 33d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen byte [] signature) 34d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen{ 35d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen super(name, type, dclass, ttl); 36d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen Type.check(covered); 37d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen TTL.check(origttl); 38d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.covered = covered; 39d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.alg = checkU8("alg", alg); 40d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.labels = name.labels() - 1; 41d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (name.isWild()) 42d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.labels--; 43d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.origttl = origttl; 44d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.expire = expire; 45d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.timeSigned = timeSigned; 46d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.footprint = checkU16("footprint", footprint); 47d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.signer = checkName("signer", signer); 48d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.signature = signature; 49d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 50d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 51d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenvoid 52d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenrrFromWire(DNSInput in) throws IOException { 53d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen covered = in.readU16(); 54d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen alg = in.readU8(); 55d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen labels = in.readU8(); 56d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen origttl = in.readU32(); 57d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen expire = new Date(1000 * in.readU32()); 58d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen timeSigned = new Date(1000 * in.readU32()); 59d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen footprint = in.readU16(); 60d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen signer = new Name(in); 61d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen signature = in.readByteArray(); 62d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 63d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 64d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenvoid 65d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenrdataFromString(Tokenizer st, Name origin) throws IOException { 66d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen String typeString = st.getString(); 67d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen covered = Type.value(typeString); 68d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (covered < 0) 69d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen throw st.exception("Invalid type: " + typeString); 70d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen String algString = st.getString(); 71d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen alg = DNSSEC.Algorithm.value(algString); 72d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (alg < 0) 73d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen throw st.exception("Invalid algorithm: " + algString); 74d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen labels = st.getUInt8(); 75d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen origttl = st.getTTL(); 76d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen expire = FormattedTime.parse(st.getString()); 77d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen timeSigned = FormattedTime.parse(st.getString()); 78d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen footprint = st.getUInt16(); 79d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen signer = st.getName(origin); 80d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen signature = st.getBase64(); 81d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 82d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 83d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Converts the RRSIG/SIG Record to a String */ 84d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenString 85d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenrrToString() { 86d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen StringBuffer sb = new StringBuffer(); 87d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (Type.string(covered)); 88d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 89d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (alg); 90d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 91d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (labels); 92d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 93d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (origttl); 94d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 95d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (Options.check("multiline")) 96d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append ("(\n\t"); 97d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (FormattedTime.format(expire)); 98d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 99d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (FormattedTime.format(timeSigned)); 100d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 101d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (footprint); 102d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 103d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (signer); 104d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen if (Options.check("multiline")) { 105d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append("\n"); 106d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append(base64.formatString(signature, 64, "\t", 107d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen true)); 108d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } else { 109d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append (" "); 110d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen sb.append(base64.toString(signature)); 111d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen } 112d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return sb.toString(); 113d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 114d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 115d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the RRset type covered by this signature */ 116d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic int 117d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetTypeCovered() { 118d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return covered; 119d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 120d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 121d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** 122d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the cryptographic algorithm of the key that generated the signature 123d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 124d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic int 125d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetAlgorithm() { 126d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return alg; 127d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 128d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 129d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** 130d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Returns the number of labels in the signed domain name. This may be 131d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * different than the record's domain name if the record is a wildcard 132d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * record. 133d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */ 134d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic int 135d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetLabels() { 136d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return labels; 137d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 138d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 139d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the original TTL of the RRset */ 140d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic long 141d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetOrigTTL() { 142d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return origttl; 143d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 144d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 145d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the time at which the signature expires */ 146d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic Date 147d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetExpire() { 148d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return expire; 149d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 150d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 151d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the time at which this signature was generated */ 152d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic Date 153d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetTimeSigned() { 154d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return timeSigned; 155d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 156d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 157d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns The footprint/key id of the signing key. */ 158d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic int 159d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetFootprint() { 160d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return footprint; 161d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 162d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 163d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the owner of the signing key */ 164d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic Name 165d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetSigner() { 166d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return signer; 167d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 168d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 169d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/** Returns the binary data representing the signature */ 170d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic byte [] 171d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChengetSignature() { 172d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen return signature; 173d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 174d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 175d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenvoid 176d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChensetSignature(byte [] signature) { 177d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen this.signature = signature; 178d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 179d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 180d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenvoid 181d7955ce24d294fb2014c59d11fca184471056f44Shuyi ChenrrToWire(DNSOutput out, Compression c, boolean canonical) { 182d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU16(covered); 183d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU8(alg); 184d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU8(labels); 185d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU32(origttl); 186d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU32(expire.getTime() / 1000); 187d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU32(timeSigned.getTime() / 1000); 188d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeU16(footprint); 189d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen signer.toWire(out, null, canonical); 190d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen out.writeByteArray(signature); 191d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 192d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen 193d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen} 194