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 Map<String, CertificateProfile> byName;
27 private static final Map<Integer, CertificateProfile> byId;
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 final HashMap<String, CertificateProfile> myName = new HashMap<String, CertificateProfile>();
180 final HashMap<Integer, CertificateProfile> myId = new HashMap<Integer, CertificateProfile>();
182 for (File f : new File("config/profiles").listFiles()) {
183 Properties p = new Properties();
185 p.load(new FileInputStream(f));
186 } catch (IOException e) {
187 throw new Error("Unable to load profile from " + f.getName(), e);
190 String[] parts = f.getName().split("\\.")[0].split("-", 2);
191 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `keyname`, `include`, `requires`, `name` FROM `profiles` WHERE `id`=?");
192 ps.setInt(1, Integer.parseInt(parts[0]));
193 GigiResultSet rs = ps.executeQuery();
196 if ( !rs.getString("keyname").equals(parts[1])) {
197 throw new Error("Config error. Certificate Profile mismatch");
199 if ( !rs.getString("include").equals(p.getProperty("include"))) {
200 throw new Error("Config error. Certificate Profile mismatch");
202 if ( !rs.getString("requires").equals(p.getProperty("requires", ""))) {
203 throw new Error("Config error. Certificate Profile mismatch");
206 GigiPreparedStatement insert = DatabaseConnection.getInstance().prepare("INSERT INTO `profiles` SET `keyname`=?, `include`=?, `requires`=?, `name`=?, `id`=?");
207 insert.setString(1, parts[1]);
208 insert.setString(2, p.getProperty("include"));
209 insert.setString(3, p.getProperty("requires", ""));
210 insert.setString(4, p.getProperty("name"));
211 insert.setInt(5, Integer.parseInt(parts[0]));
216 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `keyname`, `name`, `requires`, `include` FROM `profiles`");
217 GigiResultSet rs = ps.executeQuery();
219 CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"), rs.getString("requires"), rs.getString("include"));
220 myId.put(cp.getId(), cp);
221 myName.put(cp.getKeyName(), cp);
224 byName = Collections.unmodifiableMap(myName);
225 byId = Collections.unmodifiableMap(myId);
228 public static CertificateProfile getById(int id) {
232 public static CertificateProfile getByName(String name) {
233 return byName.get(name);
236 public static CertificateProfile[] getAll() {
237 return byId.values().toArray(new CertificateProfile[byId.size()]);
240 public boolean canBeIssuedBy(User u) {
241 for (String s : req) {
242 if (s.equals("points>=50")) {
243 if (u.getAssurancePoints() < 50) {
246 } else if (s.equals("points>=100")) {
247 if (u.getAssurancePoints() < 100) {
250 } else if (s.equals("codesign")) {
251 if (u.isInGroup(Group.CODESIGNING)) {