]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/security/JcrKeyring.java
Better clean up unit tests
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / security / JcrKeyring.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.security;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.CharArrayReader;
20 import java.io.InputStream;
21 import java.io.Reader;
22 import java.security.Provider;
23 import java.security.SecureRandom;
24
25 import javax.crypto.Cipher;
26 import javax.crypto.CipherInputStream;
27 import javax.crypto.SecretKey;
28 import javax.crypto.spec.IvParameterSpec;
29 import javax.jcr.Binary;
30 import javax.jcr.Node;
31 import javax.jcr.Property;
32 import javax.jcr.RepositoryException;
33 import javax.jcr.Session;
34
35 import org.apache.commons.io.IOUtils;
36 import org.argeo.jcr.ArgeoJcrException;
37 import org.argeo.jcr.JcrUtils;
38 import org.argeo.node.ArgeoNames;
39 import org.argeo.node.ArgeoTypes;
40 import org.argeo.node.NodeUtils;
41 import org.argeo.node.security.PBEKeySpecCallback;
42
43 /** JCR based implementation of a keyring */
44 public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
45 /**
46 * Stronger with 256, but causes problem with Oracle JVM, force 128 in this
47 * case
48 */
49 public final static Long DEFAULT_SECRETE_KEY_LENGTH = 256l;
50 public final static String DEFAULT_SECRETE_KEY_FACTORY = "PBKDF2WithHmacSHA1";
51 public final static String DEFAULT_SECRETE_KEY_ENCRYPTION = "AES";
52 public final static String DEFAULT_CIPHER_NAME = "AES/CBC/PKCS5Padding";
53
54 private Integer iterationCountFactor = 200;
55 private Long secreteKeyLength = DEFAULT_SECRETE_KEY_LENGTH;
56 private String secreteKeyFactoryName = DEFAULT_SECRETE_KEY_FACTORY;
57 private String secreteKeyEncryption = DEFAULT_SECRETE_KEY_ENCRYPTION;
58 private String cipherName = DEFAULT_CIPHER_NAME;
59
60 private Session session;
61
62 /**
63 * When setup is called the session has not yet been saved and we don't want
64 * to save it since there maybe other data which would be inconsistent. So
65 * we keep a reference to this node which will then be used (an reset to
66 * null) when handling the PBE callback. We keep one per thread in case
67 * multiple users are accessing the same instance of a keyring.
68 */
69 private ThreadLocal<Node> notYetSavedKeyring = new ThreadLocal<Node>() {
70
71 @Override
72 protected Node initialValue() {
73 return null;
74 }
75 };
76
77 @Override
78 protected Boolean isSetup() {
79 try {
80 if (notYetSavedKeyring.get() != null)
81 return true;
82
83 Node userHome = NodeUtils.getUserHome(session);
84 return userHome.hasNode(ARGEO_KEYRING);
85 } catch (RepositoryException e) {
86 throw new ArgeoJcrException("Cannot check whether keyring is setup", e);
87 }
88 }
89
90 @Override
91 protected void setup(char[] password) {
92 Binary binary = null;
93 InputStream in = null;
94 try {
95 Node userHome = NodeUtils.getUserHome(session);
96 if (userHome.hasNode(ARGEO_KEYRING))
97 throw new ArgeoJcrException("Keyring already setup");
98 Node keyring = userHome.addNode(ARGEO_KEYRING);
99 keyring.addMixin(ArgeoTypes.ARGEO_PBE_SPEC);
100
101 // deterministic salt and iteration count based on username
102 String username = session.getUserID();
103 byte[] salt = new byte[8];
104 byte[] usernameBytes = username.getBytes();
105 for (int i = 0; i < salt.length; i++) {
106 if (i < usernameBytes.length)
107 salt[i] = usernameBytes[i];
108 else
109 salt[i] = 0;
110 }
111 in = new ByteArrayInputStream(salt);
112 binary = session.getValueFactory().createBinary(in);
113 keyring.setProperty(ARGEO_SALT, binary);
114
115 Integer iterationCount = username.length() * iterationCountFactor;
116 keyring.setProperty(ARGEO_ITERATION_COUNT, iterationCount);
117
118 // default algo
119 // TODO check if algo and key length are available, use DES if not
120 keyring.setProperty(ARGEO_SECRET_KEY_FACTORY, secreteKeyFactoryName);
121 keyring.setProperty(ARGEO_KEY_LENGTH, secreteKeyLength);
122 keyring.setProperty(ARGEO_SECRET_KEY_ENCRYPTION, secreteKeyEncryption);
123 keyring.setProperty(ARGEO_CIPHER, cipherName);
124
125 // keyring.getSession().save();
126
127 // encrypted password hash
128 // IOUtils.closeQuietly(in);
129 // JcrUtils.closeQuietly(binary);
130 // byte[] btPass = hash(password, salt, iterationCount);
131 // in = new ByteArrayInputStream(btPass);
132 // binary = session.getValueFactory().createBinary(in);
133 // keyring.setProperty(ARGEO_PASSWORD, binary);
134
135 notYetSavedKeyring.set(keyring);
136 } catch (Exception e) {
137 throw new ArgeoJcrException("Cannot setup keyring", e);
138 } finally {
139 JcrUtils.closeQuietly(binary);
140 IOUtils.closeQuietly(in);
141 // JcrUtils.discardQuietly(session);
142 }
143 }
144
145 @Override
146 protected void handleKeySpecCallback(PBEKeySpecCallback pbeCallback) {
147 try {
148 Node userHome = NodeUtils.getUserHome(session);
149 Node keyring;
150 if (userHome.hasNode(ARGEO_KEYRING))
151 keyring = userHome.getNode(ARGEO_KEYRING);
152 else if (notYetSavedKeyring.get() != null)
153 keyring = notYetSavedKeyring.get();
154 else
155 throw new ArgeoJcrException("Keyring not setup");
156
157 pbeCallback.set(keyring.getProperty(ARGEO_SECRET_KEY_FACTORY).getString(),
158 JcrUtils.getBinaryAsBytes(keyring.getProperty(ARGEO_SALT)),
159 (int) keyring.getProperty(ARGEO_ITERATION_COUNT).getLong(),
160 (int) keyring.getProperty(ARGEO_KEY_LENGTH).getLong(),
161 keyring.getProperty(ARGEO_SECRET_KEY_ENCRYPTION).getString());
162
163 if (notYetSavedKeyring.get() != null)
164 notYetSavedKeyring.remove();
165 } catch (RepositoryException e) {
166 throw new ArgeoJcrException("Cannot handle key spec callback", e);
167 }
168 }
169
170 /** The parent node must already exist at this path. */
171 @Override
172 protected synchronized void encrypt(String path, InputStream unencrypted) {
173 // should be called first for lazy initialization
174 SecretKey secretKey = getSecretKey();
175
176 Binary binary = null;
177 InputStream in = null;
178 try {
179 Cipher cipher = createCipher();
180 Node node;
181 if (!session.nodeExists(path)) {
182 String parentPath = JcrUtils.parentPath(path);
183 if (!session.nodeExists(parentPath))
184 throw new ArgeoJcrException("No parent node of " + path);
185 Node parentNode = session.getNode(parentPath);
186 node = parentNode.addNode(JcrUtils.nodeNameFromPath(path));
187 } else {
188 node = session.getNode(path);
189 }
190 node.addMixin(ArgeoTypes.ARGEO_ENCRYPTED);
191 SecureRandom random = new SecureRandom();
192 byte[] iv = new byte[16];
193 random.nextBytes(iv);
194 cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
195 JcrUtils.setBinaryAsBytes(node, ARGEO_IV, iv);
196
197 in = new CipherInputStream(unencrypted, cipher);
198 binary = session.getValueFactory().createBinary(in);
199 node.setProperty(Property.JCR_DATA, binary);
200 session.save();
201 } catch (Exception e) {
202 throw new ArgeoJcrException("Cannot encrypt", e);
203 } finally {
204 IOUtils.closeQuietly(unencrypted);
205 IOUtils.closeQuietly(in);
206 JcrUtils.closeQuietly(binary);
207 }
208 }
209
210 @Override
211 protected synchronized InputStream decrypt(String path) {
212 Binary binary = null;
213 InputStream encrypted = null;
214 Reader reader = null;
215 try {
216 if (!session.nodeExists(path)) {
217 char[] password = ask();
218 reader = new CharArrayReader(password);
219 return new ByteArrayInputStream(IOUtils.toByteArray(reader));
220 } else {
221 // should be called first for lazy initialisation
222 SecretKey secretKey = getSecretKey();
223
224 Cipher cipher = createCipher();
225
226 Node node = session.getNode(path);
227 if (node.hasProperty(ARGEO_IV)) {
228 byte[] iv = JcrUtils.getBinaryAsBytes(node.getProperty(ARGEO_IV));
229 cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
230 } else {
231 cipher.init(Cipher.DECRYPT_MODE, secretKey);
232 }
233
234 binary = node.getProperty(Property.JCR_DATA).getBinary();
235 encrypted = binary.getStream();
236 return new CipherInputStream(encrypted, cipher);
237 }
238 } catch (Exception e) {
239 throw new ArgeoJcrException("Cannot decrypt", e);
240 } finally {
241 IOUtils.closeQuietly(encrypted);
242 IOUtils.closeQuietly(reader);
243 JcrUtils.closeQuietly(binary);
244 }
245 }
246
247 protected Cipher createCipher() {
248 try {
249 Node userHome = NodeUtils.getUserHome(session);
250 if (!userHome.hasNode(ARGEO_KEYRING))
251 throw new ArgeoJcrException("Keyring not setup");
252 Node keyring = userHome.getNode(ARGEO_KEYRING);
253 String cipherName = keyring.getProperty(ARGEO_CIPHER).getString();
254 Provider securityProvider = getSecurityProvider();
255 Cipher cipher;
256 if (securityProvider == null)// TODO use BC?
257 cipher = Cipher.getInstance(cipherName);
258 else
259 cipher = Cipher.getInstance(cipherName, securityProvider);
260 return cipher;
261 } catch (Exception e) {
262 throw new ArgeoJcrException("Cannot get cipher", e);
263 }
264 }
265
266 public synchronized void changePassword(char[] oldPassword, char[] newPassword) {
267 // TODO decrypt with old pw / encrypt with new pw all argeo:encrypted
268 }
269
270 public synchronized void setSession(Session session) {
271 this.session = session;
272 }
273
274 public void setIterationCountFactor(Integer iterationCountFactor) {
275 this.iterationCountFactor = iterationCountFactor;
276 }
277
278 public void setSecreteKeyLength(Long keyLength) {
279 this.secreteKeyLength = keyLength;
280 }
281
282 public void setSecreteKeyFactoryName(String secreteKeyFactoryName) {
283 this.secreteKeyFactoryName = secreteKeyFactoryName;
284 }
285
286 public void setSecreteKeyEncryption(String secreteKeyEncryption) {
287 this.secreteKeyEncryption = secreteKeyEncryption;
288 }
289
290 public void setCipherName(String cipherName) {
291 this.cipherName = cipherName;
292 }
293
294 }