001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.crypto.keymanager;
019    
020    import java.util.Arrays;
021    import java.util.Date;
022    
023    /**
024     * {@link CryptoCache}-entry wrapping a secret key used for symmetric en-/decryption of actual data.
025     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
026     */
027    public class CryptoCacheKeyEntry
028    {
029            /**
030             * Create a new instance.
031             * @param keyID identifier of the key to be cached; must be &gt;= 0.
032             * @param keyData actual key data (raw). Warning: This byte array will be overwritten with 0 by the {@link #finalize()} method!
033             */
034            protected CryptoCacheKeyEntry(long keyID, byte[] keyData)
035            {
036                    if (keyID < 0)
037                            throw new IllegalArgumentException("keyID < 0");
038    
039                    if (keyData == null)
040                            throw new IllegalArgumentException("keyData == null");
041    
042                    this.keyID = keyID;
043                    this.keyData = keyData.clone(); // necessary, because we overwrite the keyData in the finalize() method.
044            }
045    
046            private long keyID = -1;
047    
048            private byte[] keyData;
049    
050            private Date lastUsageTimestamp = new Date();
051    
052            /**
053             * Get the identifier of the key being cached.
054             * @return the identifier of the key being cached.
055             */
056            public long getKeyID() {
057                    return keyID;
058            }
059    
060            /**
061             * Get the actual raw key data.
062             * @return the actual raw key data.
063             */
064            public byte[] getKeyData() {
065                    return keyData;
066            }
067    
068            /**
069             * Get the timestamp when the key was used the last time.
070             * @return the timestamp when the key was used the last time.
071             */
072            public Date getLastUsageTimestamp() {
073                    return lastUsageTimestamp;
074            }
075    
076            @Override
077            protected void finalize() throws Throwable
078            {
079                    Arrays.fill(keyData, (byte)0);
080                    super.finalize();
081            }
082    }