]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CertificateProfile.java
add: let a user revoke his certificates.
[gigi.git] / src / org / cacert / gigi / dbObjects / CertificateProfile.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12
13 import org.cacert.gigi.database.DatabaseConnection;
14 import org.cacert.gigi.database.GigiPreparedStatement;
15 import org.cacert.gigi.database.GigiResultSet;
16
17 public class CertificateProfile implements IdCachable {
18
19     private final int id;
20
21     private final String keyName;
22
23     private final String visibleName;
24
25     private static HashMap<String, CertificateProfile> byName = new HashMap<>();
26
27     private static HashMap<Integer, CertificateProfile> byId = new HashMap<>();
28
29     private final Map<String, PropertyTemplate> pt;
30
31     private final List<String> req;
32
33     private CertificateProfile(int id, String keyName, String visibleName, String requires, String include) {
34         this.id = id;
35         this.keyName = keyName;
36         this.visibleName = visibleName;
37         req = parseConditions(requires);
38         pt = parsePropertyTemplates(include);
39     }
40
41     public static class PropertyTemplate implements Comparable<PropertyTemplate> {
42
43         private boolean required = true;
44
45         private boolean multiple = false;
46
47         private String base;
48
49         public PropertyTemplate(String inc) {
50             if (inc.endsWith("?") || inc.endsWith("*") || inc.endsWith("+")) {
51                 char sfx = inc.charAt(inc.length() - 1);
52                 if (sfx == '?') {
53                     required = false;
54                 } else if (sfx == '*') {
55                     multiple = true;
56                     required = false;
57                 } else {
58                     multiple = true;
59                 }
60                 inc = inc.substring(0, inc.length() - 1);
61             }
62             this.base = inc;
63         }
64
65         public String getBase() {
66             return base;
67         }
68
69         public boolean isMultiple() {
70             return multiple;
71         }
72
73         public boolean isRequired() {
74             return required;
75         }
76
77         @Override
78         public int hashCode() {
79             final int prime = 31;
80             int result = 1;
81             result = prime * result + ((base == null) ? 0 : base.hashCode());
82             result = prime * result + (multiple ? 1231 : 1237);
83             result = prime * result + (required ? 1231 : 1237);
84             return result;
85         }
86
87         @Override
88         public boolean equals(Object obj) {
89             if (this == obj) {
90                 return true;
91             }
92             if (obj == null) {
93                 return false;
94             }
95             if (getClass() != obj.getClass()) {
96                 return false;
97             }
98             PropertyTemplate other = (PropertyTemplate) obj;
99             if (base == null) {
100                 if (other.base != null) {
101                     return false;
102                 }
103             } else if ( !base.equals(other.base)) {
104                 return false;
105             }
106             if (multiple != other.multiple) {
107                 return false;
108             }
109             if (required != other.required) {
110                 return false;
111             }
112             return true;
113         }
114
115         @Override
116         public String toString() {
117             return base + (multiple ? (required ? "+" : "*") : (required ? "" : "?"));
118         }
119
120         @Override
121         public int compareTo(PropertyTemplate o) {
122             return toString().compareTo(o.toString());
123         }
124
125     }
126
127     private CertificateProfile(File f) throws IOException {
128         Properties p = new Properties();
129         p.load(new FileInputStream(f));
130         String[] parts = f.getName().split("\\.")[0].split("-", 2);
131         id = Integer.parseInt(parts[0]);
132         keyName = parts[1];
133         visibleName = "";
134         pt = parsePropertyTemplates(p.getProperty("include"));
135         req = parseConditions(p.getProperty("requires", ""));
136     }
137
138     private List<String> parseConditions(String property) {
139         String[] split2 = property.split(",");
140         if (split2.length == 1 && split2[0].equals("")) {
141             split2 = new String[0];
142         }
143         return Collections.unmodifiableList(Arrays.asList(split2));
144     }
145
146     private Map<String, PropertyTemplate> parsePropertyTemplates(String property) {
147         String[] split = property.split(",");
148         HashMap<String, PropertyTemplate> map = new HashMap<>(split.length);
149         for (int i = 0; i < split.length; i++) {
150
151             PropertyTemplate value = new PropertyTemplate(split[i]);
152
153             map.put(value.getBase(), value);
154         }
155         return Collections.unmodifiableMap(map);
156     }
157
158     public int getId() {
159         return id;
160     }
161
162     public String getKeyName() {
163         return keyName;
164     }
165
166     public String getVisibleName() {
167         return visibleName;
168     }
169
170     public Map<String, PropertyTemplate> getTemplates() {
171         return pt;
172     }
173
174     public List<String> getReqireds() {
175         return req;
176     }
177
178     static {
179         for (File f : new File("config/profiles").listFiles()) {
180             Properties p = new Properties();
181             try {
182                 p.load(new FileInputStream(f));
183             } catch (IOException e) {
184                 e.printStackTrace();
185             }
186             String[] parts = f.getName().split("\\.")[0].split("-", 2);
187             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT keyname, include, requires, name FROM `profiles` WHERE id=?");
188             ps.setInt(1, Integer.parseInt(parts[0]));
189             GigiResultSet rs = ps.executeQuery();
190
191             if (rs.next()) {
192                 if ( !rs.getString("keyname").equals(parts[1])) {
193                     throw new Error("Config error. Certificate Profile mismatch");
194                 }
195                 if ( !rs.getString("include").equals(p.getProperty("include"))) {
196                     throw new Error("Config error. Certificate Profile mismatch");
197                 }
198                 if ( !rs.getString("requires").equals(p.getProperty("requires", ""))) {
199                     throw new Error("Config error. Certificate Profile mismatch");
200                 }
201             } else {
202                 GigiPreparedStatement insert = DatabaseConnection.getInstance().prepare("INSERT INTO `profiles` SET keyname=?, include=?, requires=?, name=?, id=?");
203                 insert.setString(1, parts[1]);
204                 insert.setString(2, p.getProperty("include"));
205                 insert.setString(3, p.getProperty("requires", ""));
206                 insert.setString(4, p.getProperty("name"));
207                 insert.setInt(5, Integer.parseInt(parts[0]));
208                 insert.execute();
209             }
210
211         }
212         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name, requires, include FROM `profiles`");
213         GigiResultSet rs = ps.executeQuery();
214         while (rs.next()) {
215             CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"), rs.getString("requires"), rs.getString("include"));
216             byId.put(cp.getId(), cp);
217             byName.put(cp.getKeyName(), cp);
218         }
219
220     }
221
222     public static CertificateProfile getById(int id) {
223         return byId.get(id);
224     }
225
226     public static CertificateProfile getByName(String name) {
227         return byName.get(name);
228     }
229
230     public static CertificateProfile[] getAll() {
231         return byId.values().toArray(new CertificateProfile[byId.size()]);
232     }
233
234     public boolean canBeIssuedBy(User u) {
235         for (String s : req) {
236             if (s.equals("points>=50")) {
237                 if (u.getAssurancePoints() < 50) {
238                     return false;
239                 }
240             } else if (s.equals("points>=100")) {
241                 if (u.getAssurancePoints() < 100) {
242                     return false;
243                 }
244             } else if (s.equals("codesign")) {
245                 if (u.isInGroup(Group.CODESIGNING)) {
246                     return false;
247                 }
248             } else {
249                 return false;
250             }
251
252         }
253         return true;
254     }
255 }