25fff0c295c03debe456d01b6b81010d680370ba
[captive-validator.git] / src / com / verisign / tat / dnssec / SRRset.java
1 /***************************** -*- Java -*- ********************************\
2  *                                                                         *
3  *   Copyright (c) 2009 VeriSign, Inc. All rights reserved.                *
4  *                                                                         *
5  * This software is provided solely in connection with the terms of the    *
6  * license agreement.  Any other use without the prior express written     *
7  * permission of VeriSign is completely prohibited.  The software and      *
8  * documentation are "Commercial Items", as that term is defined in 48     *
9  * C.F.R.  section 2.101, consisting of "Commercial Computer Software" and *
10  * "Commercial Computer Software Documentation" as such terms are defined  *
11  * in 48 C.F.R. section 252.227-7014(a)(5) and 48 C.F.R. section           *
12  * 252.227-7014(a)(1), and used in 48 C.F.R. section 12.212 and 48 C.F.R.  *
13  * section 227.7202, as applicable.  Pursuant to the above and other       *
14  * relevant sections of the Code of Federal Regulations, as applicable,    *
15  * VeriSign's publications, commercial computer software, and commercial   *
16  * computer software documentation are distributed and licensed to United  *
17  * States Government end users with only those rights as granted to all    *
18  * other end users, according to the terms and conditions contained in the *
19  * license agreement(s) that accompany the products and software           *
20  * documentation.                                                          *
21  *                                                                         *
22 \***************************************************************************/
23
24 package com.verisign.tat.dnssec;
25
26 import org.xbill.DNS.*;
27
28 import java.util.*;
29
30 /**
31  * A version of the RRset class overrides the standard security status.
32  */
33 public class SRRset extends RRset {
34     private SecurityStatus mSecurityStatus;
35
36     /** Create a new, blank SRRset. */
37     public SRRset() {
38         super();
39         mSecurityStatus = new SecurityStatus();
40     }
41
42     /**
43      * Create a new SRRset from an existing RRset. This SRRset will contain that
44      * same internal Record objects as the original RRset.
45      */
46     @SuppressWarnings("unchecked")
47     public SRRset(RRset r) {
48         this();
49
50         for (Iterator i = r.rrs(); i.hasNext();) {
51             addRR((Record) i.next());
52         }
53
54         for (Iterator i = r.sigs(); i.hasNext();) {
55             addRR((Record) i.next());
56         }
57     }
58
59     /**
60      * Return the current security status (generally: UNCHECKED, BOGUS, or
61      * SECURE).
62      */
63     public int getSecurity() {
64         return getSecurityStatus();
65     }
66
67     /**
68      * Return the current security status (generally: UNCHECKED, BOGUS, or
69      * SECURE).
70      */
71     public byte getSecurityStatus() {
72         return mSecurityStatus.getStatus();
73     }
74
75     /**
76      * Set the current security status for this SRRset. This status will be
77      * shared amongst all copies of this SRRset (created with cloneSRRset())
78      */
79     public void setSecurityStatus(byte status) {
80         mSecurityStatus.setStatus(status);
81     }
82
83     @SuppressWarnings("unchecked")
84     public Iterator<Record> rrs() {
85         return (Iterator<Record>) super.rrs();
86     }
87
88     @SuppressWarnings("unchecked")
89     public Iterator<RRSIGRecord> sigs() {
90         return (Iterator<RRSIGRecord>) super.sigs();
91     }
92
93     public int totalSize() {
94         int num_sigs = 0;
95
96         for (Iterator<RRSIGRecord> i = sigs(); i.hasNext();) {
97             num_sigs++;
98             i.next();
99         }
100
101         return size() + num_sigs;
102     }
103
104     /**
105      * @return The total number of records (data + sigs) in the SRRset.
106      */
107     public int getNumRecords() {
108         return totalSize();
109     }
110
111     public RRSIGRecord firstSig() {
112         for (Iterator<RRSIGRecord> i = sigs(); i.hasNext();) {
113             return i.next();
114         }
115
116         return null;
117     }
118
119     /**
120      * @return true if this RRset has RRSIG records that cover data records.
121      *         (i.e., RRSIG SRRsets return false)
122      */
123     public boolean isSigned() {
124         if (getType() == Type.RRSIG) {
125             return false;
126         }
127
128         return firstSig() != null;
129     }
130
131     /**
132      * @return The "signer" name for this SRRset, if signed, or null if not.
133      */
134     public Name getSignerName() {
135         RRSIGRecord sig = (RRSIGRecord) firstSig();
136
137         if (sig == null) {
138             return null;
139         }
140
141         return sig.getSigner();
142     }
143 }