a635f5c70a38421c4270b4289263c5c233878d35
[captive-validator.git] / src / com / versign / tat / dnssec / TrustAnchorStore.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 /**
32  *
33  */
34 public class TrustAnchorStore {
35     private Map<String, SRRset> mMap;
36
37     public TrustAnchorStore() {
38         mMap = null;
39     }
40
41     private String key(Name n, int dclass) {
42         return "T" + dclass + "/" + Util.nameToString(n);
43     }
44
45     public void store(SRRset rrset) {
46         if (mMap == null) {
47             mMap = new HashMap<String, SRRset>();
48         }
49
50         String k = key(rrset.getName(), rrset.getDClass());
51         rrset.setSecurityStatus(SecurityStatus.SECURE);
52
53         mMap.put(k, rrset);
54     }
55
56     private SRRset lookup(String key) {
57         if (mMap == null) {
58             return null;
59         }
60
61         return mMap.get(key);
62     }
63
64     public SRRset find(Name n, int dclass) {
65         if (mMap == null) {
66             return null;
67         }
68
69         while (n.labels() > 0) {
70             String k = key(n, dclass);
71             SRRset r = lookup(k);
72
73             if (r != null) {
74                 return r;
75             }
76
77             n = new Name(n, 1);
78         }
79
80         return null;
81     }
82
83     public boolean isBelowTrustAnchor(Name n, int dclass) {
84         return find(n, dclass) != null;
85     }
86
87     public List<String> listTrustAnchors() {
88         List<String> res = new ArrayList<String>();
89
90         for (Map.Entry<String, SRRset> entry : mMap.entrySet()) {
91             for (Iterator<Record> i = entry.getValue().rrs(); i.hasNext();) {
92                 DNSKEYRecord r        = (DNSKEYRecord) i.next();
93                 String       key_desc = r.getName().toString() + "/" +
94                     DNSSEC.Algorithm.string(r.getAlgorithm()) + "/" +
95                     r.getFootprint();
96                 res.add(key_desc);
97             }
98         }
99
100         return res;
101     }
102 }