1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.util.env;
18
19 import java.util.Properties;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.core.io.Resource;
25
26 import ch.elca.el4j.core.context.ModuleApplicationContext;
27 import ch.elca.el4j.util.encryption.AbstractPropertyEncryptor;
28 import ch.elca.el4j.util.encryption.PasswordSource;
29
30
31
32
33
34
35
36
37
38
39 public class PropertyEncryptionUtil {
40
41
42
43
44 private static Logger s_logger = LoggerFactory.getLogger(ModuleApplicationContext.EL4J_DEBUGGING_LOGGER);
45
46
47
48
49 protected String m_defaultConfigFile = "classpath:cryptor.properties";
50
51
52
53
54 private ClassLoader m_cl;
55
56
57
58
59 @SuppressWarnings("unchecked")
60 private Class m_c;
61
62
63
64
65
66
67
68 private enum CryptStatus {
69
70
71 UNINITED,
72
73
74 EXTERNAL,
75
76
77 ACTIVE,
78
79
80 DEACTIVATED
81 };
82
83
84
85
86 private CryptStatus m_status;
87
88
89
90
91 private AbstractPropertyEncryptor m_cryptor;
92
93
94
95
96 private PasswordSource m_source;
97
98
99
100
101 private boolean m_useSource = false;
102
103
104
105
106
107 public PropertyEncryptionUtil() {
108 m_status = CryptStatus.UNINITED;
109 }
110
111
112
113
114 public boolean isInited() {
115 return !m_status.equals(CryptStatus.UNINITED);
116 }
117
118
119
120
121 public boolean isActive() {
122 return m_status.equals(CryptStatus.ACTIVE);
123 }
124
125
126
127
128 public void deactivate() {
129 m_status = CryptStatus.DEACTIVATED;
130 }
131
132
133
134
135
136 public void init(ApplicationContext ctx) {
137 init(ctx, m_defaultConfigFile);
138 }
139
140
141
142
143
144 public void init(ApplicationContext ctx, String configFile) {
145
146 if (m_status != CryptStatus.UNINITED) {
147 throw new RuntimeException(
148 "You cannot call init() more than once.");
149 }
150
151
152 m_cl = Thread.currentThread().getContextClassLoader();
153 try {
154 m_c = m_cl
155 .loadClass("ch.elca.el4j.services.encryption.PropertyEncryptor");
156 m_cryptor = (AbstractPropertyEncryptor) m_c.newInstance();
157 } catch (ClassNotFoundException e) {
158
159 m_status = CryptStatus.EXTERNAL;
160 return;
161 } catch (Exception e) {
162 throw new RuntimeException("Error initializing cryptor.");
163 }
164
165
166
167
168
169
170 m_status = CryptStatus.ACTIVE;
171
172 s_logger.info("Trying to read cryptor config file: " + configFile);
173
174 Properties p = new Properties();
175 Resource res = ctx.getResource(configFile);
176 if (!res.exists()) {
177 s_logger
178 .error("The config file " + configFile + " does not exist.");
179 }
180
181 try {
182
183 p.load(res.getInputStream());
184 } catch (Exception e) {
185 s_logger.error("Config file " + configFile + " is not accessible.");
186 e.printStackTrace();
187 }
188
189 if (p.containsKey("cryptor.passwordSource")
190 && p.containsKey("cryptor.customPassword")) {
191 String source = p.getProperty("cryptor.passwordSource");
192 String custom = p.getProperty("cryptor.customPassword");
193
194 try {
195 if (source.equals("mixed")) {
196 String key = m_cryptor.decrypt(custom);
197 m_cryptor.deriveKey(key);
198 } else if (source.equals("custom")) {
199 if (m_useSource) {
200 m_cryptor.deriveKey(this.m_source.getPassword());
201 } else {
202
203
204 s_logger.error("Internal password mode set to custom"
205 + "but no passwordSource defined.");
206 }
207 }
208 s_logger.info("Success reading file.");
209 } catch (Exception e) {
210 s_logger.error("Error reading config file.");
211 }
212 } else {
213 s_logger.error("Config file must contain entries "
214 + "cryptor.passwordSource and " + "cryptor.customPassword.");
215 }
216 }
217
218
219
220
221
222 public AbstractPropertyEncryptor getCryptor() {
223 if (!isActive()) {
224 throw new RuntimeException("You can only get a cryptor in the "
225 + "ACTIVE state.");
226 }
227 return m_cryptor;
228 }
229
230
231
232
233 public void setSource(PasswordSource source) {
234
235
236 this.m_source = source;
237 m_useSource = true;
238 }
239
240 }