~ubuntu-branches/ubuntu/lucid/dnsjava/lucid

« back to all changes in this revision

Viewing changes to org/xbill/DNS/KEYBase.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
 
2
 
 
3
package org.xbill.DNS;
 
4
 
 
5
import java.io.*;
 
6
import org.xbill.DNS.utils.*;
 
7
 
 
8
/**
 
9
 * The base class for KEY/DNSKEY records, which have identical formats 
 
10
 *
 
11
 * @author Brian Wellington
 
12
 */
 
13
 
 
14
abstract class KEYBase extends Record {
 
15
 
 
16
protected int flags, proto, alg;
 
17
protected byte [] key;
 
18
protected int footprint = -1;
 
19
 
 
20
protected
 
21
KEYBase() {}
 
22
 
 
23
public
 
24
KEYBase(Name name, int type, int dclass, long ttl, int flags, int proto,
 
25
        int alg, byte [] key)
 
26
{
 
27
        super(name, type, dclass, ttl);
 
28
        this.flags = checkU16("flags", flags);
 
29
        this.proto = checkU8("proto", proto);
 
30
        this.alg = checkU8("alg", alg);
 
31
        this.key = key;
 
32
}
 
33
 
 
34
void
 
35
rrFromWire(DNSInput in) throws IOException {
 
36
        flags = in.readU16();
 
37
        proto = in.readU8();
 
38
        alg = in.readU8();
 
39
        if (in.remaining() > 0)
 
40
                key = in.readByteArray();
 
41
}
 
42
 
 
43
/** Converts the DNSKEY/KEY Record to a String */
 
44
String
 
45
rrToString() {
 
46
        StringBuffer sb = new StringBuffer();
 
47
        sb.append(flags);
 
48
        sb.append(" ");
 
49
        sb.append(proto);
 
50
        sb.append(" ");
 
51
        sb.append(alg);
 
52
        if (key != null) {
 
53
                if (Options.check("multiline")) {
 
54
                        sb.append(" (\n");
 
55
                        sb.append(base64.formatString(key, 64, "\t", true));
 
56
                        sb.append(" ; key_tag = ");
 
57
                        sb.append(getFootprint());
 
58
                } else {
 
59
                        sb.append(" ");
 
60
                        sb.append(base64.toString(key));
 
61
                }
 
62
        }
 
63
        return sb.toString();
 
64
}
 
65
 
 
66
/**
 
67
 * Returns the flags describing the key's properties
 
68
 */
 
69
public int
 
70
getFlags() {
 
71
        return flags;
 
72
}
 
73
 
 
74
/**
 
75
 * Returns the protocol that the key was created for
 
76
 */
 
77
public int
 
78
getProtocol() {
 
79
        return proto;
 
80
}
 
81
 
 
82
/**
 
83
 * Returns the key's algorithm
 
84
 */
 
85
public int
 
86
getAlgorithm() {
 
87
        return alg;
 
88
}
 
89
 
 
90
/**
 
91
 * Returns the binary data representing the key
 
92
 */
 
93
public byte []
 
94
getKey() {
 
95
        return key;
 
96
}
 
97
 
 
98
/**
 
99
 * Returns the key's footprint (after computing it)
 
100
 */
 
101
public int
 
102
getFootprint() {
 
103
        if (footprint >= 0)
 
104
                return footprint;
 
105
 
 
106
        int foot = 0;
 
107
 
 
108
        DNSOutput out = new DNSOutput();
 
109
        rrToWire(out, null, false);
 
110
        byte [] rdata = out.toByteArray();
 
111
 
 
112
        if (alg == DNSSEC.Algorithm.RSAMD5) {
 
113
                int d1 = rdata[rdata.length - 3] & 0xFF;
 
114
                int d2 = rdata[rdata.length - 2] & 0xFF;
 
115
                foot = (d1 << 8) + d2;
 
116
        }
 
117
        else {
 
118
                int i; 
 
119
                for (i = 0; i < rdata.length - 1; i += 2) {
 
120
                        int d1 = rdata[i] & 0xFF;
 
121
                        int d2 = rdata[i + 1] & 0xFF;
 
122
                        foot += ((d1 << 8) + d2);
 
123
                }
 
124
                if (i < rdata.length) {
 
125
                        int d1 = rdata[i] & 0xFF;
 
126
                        foot += (d1 << 8);
 
127
                }
 
128
                foot += ((foot >> 16) & 0xFFFF);
 
129
        }
 
130
        footprint = (foot & 0xFFFF);
 
131
        return footprint;
 
132
}
 
133
 
 
134
void
 
135
rrToWire(DNSOutput out, Compression c, boolean canonical) {
 
136
        out.writeU16(flags);
 
137
        out.writeU8(proto);
 
138
        out.writeU8(alg);
 
139
        if (key != null)
 
140
                out.writeByteArray(key);
 
141
}
 
142
 
 
143
}