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