2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.security
;
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
;
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
;
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
;
43 /** JCR based implementation of a keyring */
44 public class JcrKeyring
extends AbstractKeyring
implements ArgeoNames
{
46 * Stronger with 256, but causes problem with Oracle JVM, force 128 in this
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";
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
;
60 private Session session
;
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.
69 private ThreadLocal
<Node
> notYetSavedKeyring
= new ThreadLocal
<Node
>() {
72 protected Node
initialValue() {
78 protected Boolean
isSetup() {
80 if (notYetSavedKeyring
.get() != null)
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
);
91 protected void setup(char[] password
) {
93 InputStream in
= null;
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
);
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
];
111 in
= new ByteArrayInputStream(salt
);
112 binary
= session
.getValueFactory().createBinary(in
);
113 keyring
.setProperty(ARGEO_SALT
, binary
);
115 Integer iterationCount
= username
.length() * iterationCountFactor
;
116 keyring
.setProperty(ARGEO_ITERATION_COUNT
, iterationCount
);
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
);
125 // keyring.getSession().save();
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);
135 notYetSavedKeyring
.set(keyring
);
136 } catch (Exception e
) {
137 throw new ArgeoJcrException("Cannot setup keyring", e
);
139 JcrUtils
.closeQuietly(binary
);
140 IOUtils
.closeQuietly(in
);
141 // JcrUtils.discardQuietly(session);
146 protected void handleKeySpecCallback(PBEKeySpecCallback pbeCallback
) {
148 Node userHome
= NodeUtils
.getUserHome(session
);
150 if (userHome
.hasNode(ARGEO_KEYRING
))
151 keyring
= userHome
.getNode(ARGEO_KEYRING
);
152 else if (notYetSavedKeyring
.get() != null)
153 keyring
= notYetSavedKeyring
.get();
155 throw new ArgeoJcrException("Keyring not setup");
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());
163 if (notYetSavedKeyring
.get() != null)
164 notYetSavedKeyring
.remove();
165 } catch (RepositoryException e
) {
166 throw new ArgeoJcrException("Cannot handle key spec callback", e
);
170 /** The parent node must already exist at this path. */
172 protected synchronized void encrypt(String path
, InputStream unencrypted
) {
173 // should be called first for lazy initialization
174 SecretKey secretKey
= getSecretKey();
176 Binary binary
= null;
177 InputStream in
= null;
179 Cipher cipher
= createCipher();
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
));
188 node
= session
.getNode(path
);
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
);
197 in
= new CipherInputStream(unencrypted
, cipher
);
198 binary
= session
.getValueFactory().createBinary(in
);
199 node
.setProperty(Property
.JCR_DATA
, binary
);
201 } catch (Exception e
) {
202 throw new ArgeoJcrException("Cannot encrypt", e
);
204 IOUtils
.closeQuietly(unencrypted
);
205 IOUtils
.closeQuietly(in
);
206 JcrUtils
.closeQuietly(binary
);
211 protected synchronized InputStream
decrypt(String path
) {
212 Binary binary
= null;
213 InputStream encrypted
= null;
214 Reader reader
= null;
216 if (!session
.nodeExists(path
)) {
217 char[] password
= ask();
218 reader
= new CharArrayReader(password
);
219 return new ByteArrayInputStream(IOUtils
.toByteArray(reader
));
221 // should be called first for lazy initialisation
222 SecretKey secretKey
= getSecretKey();
224 Cipher cipher
= createCipher();
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
));
231 cipher
.init(Cipher
.DECRYPT_MODE
, secretKey
);
234 binary
= node
.getProperty(Property
.JCR_DATA
).getBinary();
235 encrypted
= binary
.getStream();
236 return new CipherInputStream(encrypted
, cipher
);
238 } catch (Exception e
) {
239 throw new ArgeoJcrException("Cannot decrypt", e
);
241 IOUtils
.closeQuietly(encrypted
);
242 IOUtils
.closeQuietly(reader
);
243 JcrUtils
.closeQuietly(binary
);
247 protected Cipher
createCipher() {
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();
256 if (securityProvider
== null)// TODO use BC?
257 cipher
= Cipher
.getInstance(cipherName
);
259 cipher
= Cipher
.getInstance(cipherName
, securityProvider
);
261 } catch (Exception e
) {
262 throw new ArgeoJcrException("Cannot get cipher", e
);
266 public synchronized void changePassword(char[] oldPassword
, char[] newPassword
) {
267 // TODO decrypt with old pw / encrypt with new pw all argeo:encrypted
270 public synchronized void setSession(Session session
) {
271 this.session
= session
;
274 public void setIterationCountFactor(Integer iterationCountFactor
) {
275 this.iterationCountFactor
= iterationCountFactor
;
278 public void setSecreteKeyLength(Long keyLength
) {
279 this.secreteKeyLength
= keyLength
;
282 public void setSecreteKeyFactoryName(String secreteKeyFactoryName
) {
283 this.secreteKeyFactoryName
= secreteKeyFactoryName
;
286 public void setSecreteKeyEncryption(String secreteKeyEncryption
) {
287 this.secreteKeyEncryption
= secreteKeyEncryption
;
290 public void setCipherName(String cipherName
) {
291 this.cipherName
= cipherName
;