ca0a74be9b0c8ca7ad66d08291750d3c9fe1b1e8
[moon.git] /
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.authn.mdsal.store;
10
11 import java.security.spec.KeySpec;
12 import javax.crypto.Cipher;
13 import javax.crypto.SecretKey;
14 import javax.crypto.SecretKeyFactory;
15 import javax.crypto.spec.IvParameterSpec;
16 import javax.crypto.spec.PBEKeySpec;
17 import javax.crypto.spec.SecretKeySpec;
18 import javax.xml.bind.DatatypeConverter;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * @author - Sharon Aicler (saichler@cisco.com)
24  **/
25 public class DataEncrypter {
26
27     final protected SecretKey k;
28     private static final Logger LOG = LoggerFactory.getLogger(DataEncrypter.class);
29     private static final byte[] iv = { 0, 5, 0, 0, 7, 81, 0, 3, 0, 0, 0, 0, 0, 43, 0, 1 };
30     private static final IvParameterSpec ivspec = new IvParameterSpec(iv);
31     public static final String ENCRYPTED_TAG = "Encrypted:";
32
33     public DataEncrypter(final String ckey) {
34         SecretKey tmp = null;
35         if (ckey != null && !ckey.isEmpty()) {
36
37             try {
38                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
39                 KeySpec spec = new PBEKeySpec(ckey.toCharArray(), iv, 32768, 128);
40                 tmp = keyFactory.generateSecret(spec);
41             } catch (Exception e) {
42                 LOG.error("Couldn't initialize key factory", e);
43             }
44             if (tmp != null) {
45                 k = new SecretKeySpec(tmp.getEncoded(), "AES");
46             } else {
47                 throw new RuntimeException("Couldn't initalize encryption key");
48             }
49         } else {
50             k = null;
51             LOG.warn("Void crypto key passed! AuthN Store Encryption disabled");
52         }
53
54     }
55
56     protected String encrypt(String token) {
57
58         if (k == null) {
59             return token;
60         }
61
62         String cryptostring = null;
63         try {
64             Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
65             c.init(Cipher.ENCRYPT_MODE, k, ivspec);
66             byte[] cryptobytes = c.doFinal(token.getBytes());
67             cryptostring = DatatypeConverter.printBase64Binary(cryptobytes);
68             return ENCRYPTED_TAG + cryptostring;
69         } catch (Exception e) {
70             LOG.error("Couldn't encrypt token", e);
71             return null;
72         }
73     }
74
75     protected String decrypt(String eToken) {
76         if (k == null) {
77             return eToken;
78         }
79
80         if (eToken == null || eToken.length() == 0) {
81             return null;
82         }
83
84         if (!eToken.startsWith(ENCRYPTED_TAG)) {
85             return eToken;
86         }
87
88         try {
89             Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
90             c.init(Cipher.DECRYPT_MODE, k, ivspec);
91
92             byte[] cryptobytes = DatatypeConverter.parseBase64Binary(eToken.substring(ENCRYPTED_TAG.length()));
93             byte[] clearbytes = c.doFinal(cryptobytes);
94             return DatatypeConverter.printBase64Binary(clearbytes);
95
96         } catch (Exception e) {
97             LOG.error("Couldn't decrypt token", e);
98             return null;
99         }
100     }
101 }