~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to org/xbill/DNS/DNSSEC.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.util.*;
 
6
 
 
7
/**
 
8
 * Constants and functions relating to DNSSEC (algorithm constants).
 
9
 * DNSSEC provides authentication for DNS information.  RRsets are
 
10
 * signed by an appropriate key, and a SIG record is added to the set.
 
11
 * A KEY record is obtained from DNS and used to validate the signature,
 
12
 * The KEY record must also be validated or implicitly trusted - to
 
13
 * validate a key requires a series of validations leading to a trusted
 
14
 * key.  The key must also be authorized to sign the data.
 
15
 * @see SIGRecord
 
16
 * @see KEYRecord
 
17
 * @see RRset
 
18
 *
 
19
 * @author Brian Wellington
 
20
 */
 
21
 
 
22
public class DNSSEC {
 
23
 
 
24
public static class Algorithm {
 
25
        private Algorithm() {}
 
26
 
 
27
        /** RSA/MD5 public key (deprecated) */
 
28
        public static final int RSAMD5 = 1;
 
29
 
 
30
        /** Diffie Hellman key */
 
31
        public static final int DH = 2;
 
32
 
 
33
        /** DSA public key */
 
34
        public static final int DSA = 3;
 
35
 
 
36
        /** Elliptic Curve key */
 
37
        public static final int ECC = 4;
 
38
 
 
39
        /** RSA/SHA1 public key */
 
40
        public static final int RSASHA1 = 5;
 
41
        
 
42
        /** Indirect keys; the actual key is elsewhere. */
 
43
        public static final int INDIRECT = 252;
 
44
 
 
45
        /** Private algorithm, specified by domain name */
 
46
        public static final int PRIVATEDNS = 253;
 
47
 
 
48
        /** Private algorithm, specified by OID */
 
49
        public static final int PRIVATEOID = 254;
 
50
 
 
51
        private static Mnemonic algs = new Mnemonic("DNSSEC algorithm",
 
52
                                                    Mnemonic.CASE_UPPER);
 
53
 
 
54
        static {
 
55
                algs.setMaximum(0xFF);
 
56
                algs.setNumericAllowed(true);
 
57
 
 
58
                algs.add(RSAMD5, "RSAMD5");
 
59
                algs.add(DH, "DH");
 
60
                algs.add(DSA, "DSA");
 
61
                algs.add(ECC, "ECC");
 
62
                algs.add(RSASHA1, "RSASHA1");
 
63
                algs.add(INDIRECT, "INDIRECT");
 
64
                algs.add(PRIVATEDNS, "PRIVATEDNS");
 
65
                algs.add(PRIVATEOID, "PRIVATEOID");
 
66
        }
 
67
 
 
68
        /**
 
69
         * Converts an algorithm into its textual representation
 
70
         */
 
71
        public static String
 
72
        string(int alg) {
 
73
                return algs.getText(alg);
 
74
        }
 
75
 
 
76
        /**
 
77
         * Converts a textual representation of an algorithm into its numeric
 
78
         * code.  Integers in the range 0..255 are also accepted.
 
79
         * @param s The textual representation of the algorithm
 
80
         * @return The algorithm code, or -1 on error.
 
81
         */
 
82
        public static int
 
83
        value(String s) {
 
84
                return algs.getValue(s);
 
85
        }
 
86
}
 
87
 
 
88
public static final int RSAMD5 = Algorithm.RSAMD5;
 
89
public static final int RSA = Algorithm.RSAMD5;
 
90
public static final int DH = Algorithm.DH;
 
91
public static final int DSA = Algorithm.DSA;
 
92
public static final int RSASHA1 = Algorithm.RSASHA1;
 
93
 
 
94
public static final int Failed = -1;
 
95
public static final int Insecure = 0;
 
96
public static final int Secure = 1;
 
97
 
 
98
private
 
99
DNSSEC() { }
 
100
 
 
101
private static void
 
102
digestSIG(DNSOutput out, SIGBase sig) {
 
103
        out.writeU16(sig.getTypeCovered());
 
104
        out.writeU8(sig.getAlgorithm());
 
105
        out.writeU8(sig.getLabels());
 
106
        out.writeU32(sig.getOrigTTL());
 
107
        out.writeU32(sig.getExpire().getTime() / 1000);
 
108
        out.writeU32(sig.getTimeSigned().getTime() / 1000);
 
109
        out.writeU16(sig.getFootprint());
 
110
        sig.getSigner().toWireCanonical(out);
 
111
}
 
112
 
 
113
/**
 
114
 * Creates a byte array containing the concatenation of the fields of the
 
115
 * SIG record and the RRsets to be signed/verified.  This does not perform
 
116
 * a cryptographic digest.
 
117
 * @param sig The SIG record used to sign/verify the rrset.
 
118
 * @param rrset The data to be signed/verified.
 
119
 * @return The data to be cryptographically signed or verified.
 
120
 */
 
121
public static byte []
 
122
digestRRset(RRSIGRecord sig, RRset rrset) {
 
123
        DNSOutput out = new DNSOutput();
 
124
        digestSIG(out, sig);
 
125
 
 
126
        int size = rrset.size();
 
127
        Record [] records = new Record[size];
 
128
 
 
129
        Iterator it = rrset.rrs();
 
130
        Name name = rrset.getName();
 
131
        Name wild = null;
 
132
        int sigLabels = sig.getLabels() + 1; // Add the root label back.
 
133
        if (name.labels() > sigLabels)
 
134
                wild = name.wild(name.labels() - sigLabels);
 
135
        while (it.hasNext()) {
 
136
                Record rec = (Record) it.next();
 
137
                if (wild != null)
 
138
                        rec = rec.withName(wild);
 
139
                records[--size] = rec;
 
140
        }
 
141
        Arrays.sort(records);
 
142
        for (int i = 0; i < records.length; i++)
 
143
                out.writeByteArray(records[i].toWireCanonical());
 
144
        return out.toByteArray();
 
145
}
 
146
 
 
147
/**
 
148
 * Creates a byte array containing the concatenation of the fields of the
 
149
 * SIG record and the message to be signed/verified.  This does not perform
 
150
 * a cryptographic digest.
 
151
 * @param sig The SIG record used to sign/verify the rrset.
 
152
 * @param msg The message to be signed/verified.
 
153
 * @param previous If this is a response, the signature from the query.
 
154
 * @return The data to be cryptographically signed or verified.
 
155
 */
 
156
public static byte []
 
157
digestMessage(SIGRecord sig, Message msg, byte [] previous) {
 
158
        DNSOutput out = new DNSOutput();
 
159
        digestSIG(out, sig);
 
160
 
 
161
        if (previous != null)
 
162
                out.writeByteArray(previous);
 
163
        
 
164
        msg.toWire(out);
 
165
        return out.toByteArray();
 
166
}
 
167
 
 
168
}