Coverage details for base.jdbs.cryptography.asymmetric.AsymmetricKeyRing

LineHitsSource
1 /*******************************************************************************
2  *InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package base.jdbs.cryptography.asymmetric;
21  
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.security.Key;
26 import java.security.KeyStore;
27 import java.security.cert.X509Certificate;
28 import java.util.Enumeration;
29  
30 import org.apache.log4j.Logger;
31  
32 import base.jdbs.cryptography.KeyRing;
33 import base.user.User;
34  
35 public class AsymmetricKeyRing extends KeyRing<KeyPair> {
36  
370    public static final String KEY_ENTRY = "-keyentry";
38  
39     public static final String CERTIFICATE_ENTRY = "-certificateentry";
40  
410    private static final transient Logger logger = Logger
420            .getLogger(AsymmetricKeyRing.class.getName());
43  
44     /** The asymmetric key ring owner.* */
45     private final User owner;
46  
47     /**
48      * The default constructor.
49      */
500    public AsymmetricKeyRing(User owner) {
510        this.owner = owner;
520    }
53  
54     /**
55      * @return Returns the owner.
56      */
57     public User getOwner() {
580        return owner;
59     }
60  
61     /**
62      * This method adds a keypair to the collection.
63      *
64      * @param keyPair
65      * The keypair to be added.
660     */
670    public void addKeyPair(KeyPair keyPair) {
680        super.add(keyPair.getId(), keyPair);
690    }
70  
71     /**
72      * This method removes a keypair from the collection.
73      *
740     * @param keyPairId
750     * The keypair identifier associated to the keypair that must be
76      * removed from the collection.
77      */
78     public void removeKeyPair(Integer keyPairId) {
790        super.remove(keyPairId);
800    }
810 
820    /**
83      * This method removes all the keypairs contained in the collection.
84      */
85     public void removeAllKeyPair() {
860        super.removeAll();
870    }
88  
89     /**
900     * This method retrieves a keypair givind its identifier.
91      *
92      * @param keyPairId
93      * The keypair identifier associated to the keypair that must be
94      * retrieved.
95      * @return The keypair whose identifier is keyPairId, null if the keyPairId
96      * is not associated to any keypair in the collection.
97      */
980    public KeyPair getKeyPair(Integer keyPairId) {
990        return super.get(keyPairId);
100     }
101  
102     /**
103      * This method returns all the keypairs contained in the collection.
104      *
105      * @return All the keypairs contained in the collection in array form.
1060     */
107     public KeyPair[] getAllKeyPair() {
1080        return super.getAll().toArray(new KeyPair[0]);
109     }
110  
111     /**
112      * This method computes a new Id for a KeyPair.
1130     *
114      * @return A new Id for a KeyPair.
1150     */
1160    protected Integer nextKeyPairId() {
1170        return super.nextElementId();
118     }
1190 
120     /*
1210     * (non-Javadoc)
1220     *
1230     * @see base.jdbs.cryptography.KeyRing#store(java.io.File, java.lang.String)
1240     */
1250    public void store(File location, String password) {
1260        logger.debug("Storing KeyRing to : " + location + " with password: "
1270                + password);
128         try {
1290            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
1300            keyStore.load(null, password.toCharArray());
1310 
1320            /*
1330             * If the keystore already exists on the file system, try to load
1340             * its content... otherwise create a new one adding fresh data...
1350             */
1360            if (location.exists()) {
1370                try {
1380                    FileInputStream fileInputStream = new FileInputStream(
139                             location);
1400                    keyStore.load(fileInputStream, password.toCharArray());
1410                    fileInputStream.close();
1420                } catch (Exception ex) {
1430                    logger.error(ex.getMessage());
1440                    ex.printStackTrace();
1450                }
1460            }
147  
1480            KeyPair[] keyPair = this.getAllKeyPair();
1490            for (int i = 0; i < keyPair.length; i++) {
1500                if (keyStore.containsAlias(keyPair[i].alias() + KEY_ENTRY))
1510                    logger.debug("The key store already contains the alias: "
1520                            + keyPair[i].alias() + KEY_ENTRY
153                             + " it will be overwritten.");
1540                if (keyStore.containsAlias(keyPair[i].alias()
155                         + CERTIFICATE_ENTRY))
1560                    logger.debug("The key store already contains the alias: "
157                             + keyPair[i].alias() + CERTIFICATE_ENTRY
1580                            + " it will be overwritten.");
159  
1600                keyStore.setKeyEntry(keyPair[i].alias() + KEY_ENTRY, keyPair[i]
1610                        .getPrivateKey().getValue(), password.toCharArray(),
1620                        new X509Certificate[] { keyPair[i].getCertificate() });
1630                keyStore.setCertificateEntry(keyPair[i].alias()
164                         + CERTIFICATE_ENTRY, keyPair[i].getCertificate());
165             }
1660 
1670            // Save the new keystore contents
1680            logger.debug("Writing the keystore on file system...");
1690            FileOutputStream out = new FileOutputStream(location);
1700            keyStore.store(out, password.toCharArray());
1710            out.close();
1720            logger.debug("Keystore successfully wrote...");
1730 
1740        } catch (Exception ex) {
1750            logger.error(ex.getMessage());
1760            ex.printStackTrace();
1770        }
1780    }
1790 
1800    /*
181      * (non-Javadoc)
182      *
1830     * @see base.jdbs.cryptography.KeyRing#retrieve(java.io.File,
184      * java.lang.String)
1850     */
1860    public KeyStore retrieve(File location, String password) {
1870        KeyStore keyStore = null;
1880        try {
1890            keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
1900            FileInputStream fileInputStream = new FileInputStream(location);
1910            keyStore.load(fileInputStream, password.toCharArray());
1920            fileInputStream.close();
193  
194             // For each key entry we must retrieve the private key and its
195             // certificate.
1960            Enumeration aliasEnumerationOnKey = keyStore.aliases();
1970            while (aliasEnumerationOnKey.hasMoreElements()) {
1980                String keyAlias = aliasEnumerationOnKey.nextElement()
199                         .toString();
2000                if (keyStore.isKeyEntry(keyAlias)) {
2010                    Key key = keyStore.getKey(keyAlias, password.toCharArray());
2020                    String certAlias = keyAlias.replaceAll(KEY_ENTRY,
203                             CERTIFICATE_ENTRY);
2040                    if (keyStore.isCertificateEntry(certAlias)) {
2050                        X509Certificate certificate = (X509Certificate) keyStore
206                                 .getCertificate(certAlias);
2070                        logger.debug("This is the certificate: \n"
208                                 + certificate);
2090                        base.jdbs.cryptography.asymmetric.PublicKey publicKey = new base.jdbs.cryptography.asymmetric.PublicKey(
210                                 certificate.getPublicKey());
2110                        logger.debug("This is the publicKey: \n"
212                                 + publicKey.getValue());
2130                        base.jdbs.cryptography.asymmetric.PrivateKey privateKey = new base.jdbs.cryptography.asymmetric.PrivateKey(
214                                 (java.security.PrivateKey) key);
2150                        logger.debug("This is the privateKey: \n"
216                                 + privateKey.getValue());
2170                        this.addKeyPair(new KeyPair(this.nextKeyPairId(), this
218                                 .getOwner().getNickname()
219                                 + "-KeyPair", publicKey, privateKey,
220                                 certificate));
2210                    } else
2220                        logger.error(certAlias
223                                 + " isn't a certificate entry!!!");
224  
225                 }
2260            }
227  
2280        } catch (Exception ex) {
2290            logger.error(ex.getMessage());
2300            ex.printStackTrace();
2310        }
2320        return keyStore;
233     }
234 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.