1 package org.cacert.gigi.dbObjects;
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;
11 import java.util.Properties;
13 import org.cacert.gigi.database.DatabaseConnection;
14 import org.cacert.gigi.database.GigiPreparedStatement;
15 import org.cacert.gigi.database.GigiResultSet;
17 public class CertificateProfile implements IdCachable {
21 private final String keyName;
23 private final String visibleName;
25 private static final HashMap<String, CertificateProfile> byName = new HashMap<>();
27 private static final HashMap<Integer, CertificateProfile> byId = new HashMap<>();
29 private final Map<String, PropertyTemplate> pt;
31 private final List<String> req;
33 private CertificateProfile(int id, String keyName, String visibleName, String requires, String include) {
35 this.keyName = keyName;
36 this.visibleName = visibleName;
37 req = parseConditions(requires);
38 pt = parsePropertyTemplates(include);
41 public static class PropertyTemplate implements Comparable<PropertyTemplate> {
43 private boolean required = true;
45 private boolean multiple = false;
49 public PropertyTemplate(String inc) {
50 if (inc.endsWith("?") || inc.endsWith("*") || inc.endsWith("+")) {
51 char sfx = inc.charAt(inc.length() - 1);
54 } else if (sfx == '*') {
60 inc = inc.substring(0, inc.length() - 1);
65 public String getBase() {
69 public boolean isMultiple() {
73 public boolean isRequired() {
78 public int hashCode() {
81 result = prime * result + ((base == null) ? 0 : base.hashCode());
82 result = prime * result + (multiple ? 1231 : 1237);
83 result = prime * result + (required ? 1231 : 1237);
88 public boolean equals(Object obj) {
95 if (getClass() != obj.getClass()) {
98 PropertyTemplate other = (PropertyTemplate) obj;
100 if (other.base != null) {
103 } else if ( !base.equals(other.base)) {
106 if (multiple != other.multiple) {
109 if (required != other.required) {
116 public String toString() {
117 return base + (multiple ? (required ? "+" : "*") : (required ? "" : "?"));
121 public int compareTo(PropertyTemplate o) {
122 return toString().compareTo(o.toString());
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]);
134 pt = parsePropertyTemplates(p.getProperty("include"));
135 req = parseConditions(p.getProperty("requires", ""));
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];
143 return Collections.unmodifiableList(Arrays.asList(split2));
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++) {
151 PropertyTemplate value = new PropertyTemplate(split[i]);
153 map.put(value.getBase(), value);
155 return Collections.unmodifiableMap(map);
162 public String getKeyName() {
166 public String getVisibleName() {
170 public Map<String, PropertyTemplate> getTemplates() {
174 public List<String> getReqireds() {
179 for (File f : new File("config/profiles").listFiles()) {
180 Properties p = new Properties();
182 p.load(new FileInputStream(f));
183 } catch (IOException e) {
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();
192 if ( !rs.getString("keyname").equals(parts[1])) {
193 throw new Error("Config error. Certificate Profile mismatch");
195 if ( !rs.getString("include").equals(p.getProperty("include"))) {
196 throw new Error("Config error. Certificate Profile mismatch");
198 if ( !rs.getString("requires").equals(p.getProperty("requires", ""))) {
199 throw new Error("Config error. Certificate Profile mismatch");
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]));
212 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name, requires, include FROM `profiles`");
213 GigiResultSet rs = ps.executeQuery();
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);
222 public static CertificateProfile getById(int id) {
226 public static CertificateProfile getByName(String name) {
227 return byName.get(name);
230 public static CertificateProfile[] getAll() {
231 return byId.values().toArray(new CertificateProfile[byId.size()]);
234 public boolean canBeIssuedBy(User u) {
235 for (String s : req) {
236 if (s.equals("points>=50")) {
237 if (u.getAssurancePoints() < 50) {
240 } else if (s.equals("points>=100")) {
241 if (u.getAssurancePoints() < 100) {
244 } else if (s.equals("codesign")) {
245 if (u.isInGroup(Group.CODESIGNING)) {