aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/main/java/org/opendaylight/aaa/authn/mdsal/store/DataEncrypter.java
blob: ca0a74be9b0c8ca7ad66d08291750d3c9fe1b1e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.authn.mdsal.store;

import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author - Sharon Aicler (saichler@cisco.com)
 **/
public class DataEncrypter {

    final protected SecretKey k;
    private static final Logger LOG = LoggerFactory.getLogger(DataEncrypter.class);
    private static final byte[] iv = { 0, 5, 0, 0, 7, 81, 0, 3, 0, 0, 0, 0, 0, 43, 0, 1 };
    private static final IvParameterSpec ivspec = new IvParameterSpec(iv);
    public static final String ENCRYPTED_TAG = "Encrypted:";

    public DataEncrypter(final String ckey) {
        SecretKey tmp = null;
        if (ckey != null && !ckey.isEmpty()) {

            try {
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
                KeySpec spec = new PBEKeySpec(ckey.toCharArray(), iv, 32768, 128);
                tmp = keyFactory.generateSecret(spec);
            } catch (Exception e) {
                LOG.error("Couldn't initialize key factory", e);
            }
            if (tmp != null) {
                k = new SecretKeySpec(tmp.getEncoded(), "AES");
            } else {
                throw new RuntimeException("Couldn't initalize encryption key");
            }
        } else {
            k = null;
            LOG.warn("Void crypto key passed! AuthN Store Encryption disabled");
        }

    }

    protected String encrypt(String token) {

        if (k == null) {
            return token;
        }

        String cryptostring = null;
        try {
            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
            c.init(Cipher.ENCRYPT_MODE, k, ivspec);
            byte[] cryptobytes = c.doFinal(token.getBytes());
            cryptostring = DatatypeConverter.printBase64Binary(cryptobytes);
            return ENCRYPTED_TAG + cryptostring;
        } catch (Exception e) {
            LOG.error("Couldn't encrypt token", e);
            return null;
        }
    }

    protected String decrypt(String eToken) {
        if (k == null) {
            return eToken;
        }

        if (eToken == null || eToken.length() == 0) {
            return null;
        }

        if (!eToken.startsWith(ENCRYPTED_TAG)) {
            return eToken;
        }

        try {
            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
            c.init(Cipher.DECRYPT_MODE, k, ivspec);

            byte[] cryptobytes = DatatypeConverter.parseBase64Binary(eToken.substring(ENCRYPTED_TAG.length()));
            byte[] clearbytes = c.doFinal(cryptobytes);
            return DatatypeConverter.printBase64Binary(clearbytes);

        } catch (Exception e) {
            LOG.error("Couldn't decrypt token", e);
            return null;
        }
    }
}