| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 37 | 0 | public static final String KEY_ENTRY = "-keyentry"; |
| 38 | ||
| 39 | public static final String CERTIFICATE_ENTRY = "-certificateentry"; | |
| 40 | ||
| 41 | 0 | private static final transient Logger logger = Logger |
| 42 | 0 | .getLogger(AsymmetricKeyRing.class.getName()); |
| 43 | ||
| 44 | /** The asymmetric key ring owner.* */ | |
| 45 | private final User owner; | |
| 46 | ||
| 47 | /** | |
| 48 | * The default constructor. | |
| 49 | */ | |
| 50 | 0 | public AsymmetricKeyRing(User owner) { |
| 51 | 0 | this.owner = owner; |
| 52 | 0 | } |
| 53 | ||
| 54 | /** | |
| 55 | * @return Returns the owner. | |
| 56 | */ | |
| 57 | public User getOwner() { | |
| 58 | 0 | return owner; |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * This method adds a keypair to the collection. | |
| 63 | * | |
| 64 | * @param keyPair | |
| 65 | * The keypair to be added. | |
| 66 | 0 | */ |
| 67 | 0 | public void addKeyPair(KeyPair keyPair) { |
| 68 | 0 | super.add(keyPair.getId(), keyPair); |
| 69 | 0 | } |
| 70 | ||
| 71 | /** | |
| 72 | * This method removes a keypair from the collection. | |
| 73 | * | |
| 74 | 0 | * @param keyPairId |
| 75 | 0 | * The keypair identifier associated to the keypair that must be |
| 76 | * removed from the collection. | |
| 77 | */ | |
| 78 | public void removeKeyPair(Integer keyPairId) { | |
| 79 | 0 | super.remove(keyPairId); |
| 80 | 0 | } |
| 81 | 0 | |
| 82 | 0 | /** |
| 83 | * This method removes all the keypairs contained in the collection. | |
| 84 | */ | |
| 85 | public void removeAllKeyPair() { | |
| 86 | 0 | super.removeAll(); |
| 87 | 0 | } |
| 88 | ||
| 89 | /** | |
| 90 | 0 | * 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 | */ | |
| 98 | 0 | public KeyPair getKeyPair(Integer keyPairId) { |
| 99 | 0 | 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. | |
| 106 | 0 | */ |
| 107 | public KeyPair[] getAllKeyPair() { | |
| 108 | 0 | return super.getAll().toArray(new KeyPair[0]); |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * This method computes a new Id for a KeyPair. | |
| 113 | 0 | * |
| 114 | * @return A new Id for a KeyPair. | |
| 115 | 0 | */ |
| 116 | 0 | protected Integer nextKeyPairId() { |
| 117 | 0 | return super.nextElementId(); |
| 118 | } | |
| 119 | 0 | |
| 120 | /* | |
| 121 | 0 | * (non-Javadoc) |
| 122 | 0 | * |
| 123 | 0 | * @see base.jdbs.cryptography.KeyRing#store(java.io.File, java.lang.String) |
| 124 | 0 | */ |
| 125 | 0 | public void store(File location, String password) { |
| 126 | 0 | logger.debug("Storing KeyRing to : " + location + " with password: " |
| 127 | 0 | + password); |
| 128 | try { | |
| 129 | 0 | KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 130 | 0 | keyStore.load(null, password.toCharArray()); |
| 131 | 0 | |
| 132 | 0 | /* |
| 133 | 0 | * If the keystore already exists on the file system, try to load |
| 134 | 0 | * its content... otherwise create a new one adding fresh data... |
| 135 | 0 | */ |
| 136 | 0 | if (location.exists()) { |
| 137 | 0 | try { |
| 138 | 0 | FileInputStream fileInputStream = new FileInputStream( |
| 139 | location); | |
| 140 | 0 | keyStore.load(fileInputStream, password.toCharArray()); |
| 141 | 0 | fileInputStream.close(); |
| 142 | 0 | } catch (Exception ex) { |
| 143 | 0 | logger.error(ex.getMessage()); |
| 144 | 0 | ex.printStackTrace(); |
| 145 | 0 | } |
| 146 | 0 | } |
| 147 | ||
| 148 | 0 | KeyPair[] keyPair = this.getAllKeyPair(); |
| 149 | 0 | for (int i = 0; i < keyPair.length; i++) { |
| 150 | 0 | if (keyStore.containsAlias(keyPair[i].alias() + KEY_ENTRY)) |
| 151 | 0 | logger.debug("The key store already contains the alias: " |
| 152 | 0 | + keyPair[i].alias() + KEY_ENTRY |
| 153 | + " it will be overwritten."); | |
| 154 | 0 | if (keyStore.containsAlias(keyPair[i].alias() |
| 155 | + CERTIFICATE_ENTRY)) | |
| 156 | 0 | logger.debug("The key store already contains the alias: " |
| 157 | + keyPair[i].alias() + CERTIFICATE_ENTRY | |
| 158 | 0 | + " it will be overwritten."); |
| 159 | ||
| 160 | 0 | keyStore.setKeyEntry(keyPair[i].alias() + KEY_ENTRY, keyPair[i] |
| 161 | 0 | .getPrivateKey().getValue(), password.toCharArray(), |
| 162 | 0 | new X509Certificate[] { keyPair[i].getCertificate() }); |
| 163 | 0 | keyStore.setCertificateEntry(keyPair[i].alias() |
| 164 | + CERTIFICATE_ENTRY, keyPair[i].getCertificate()); | |
| 165 | } | |
| 166 | 0 | |
| 167 | 0 | // Save the new keystore contents |
| 168 | 0 | logger.debug("Writing the keystore on file system..."); |
| 169 | 0 | FileOutputStream out = new FileOutputStream(location); |
| 170 | 0 | keyStore.store(out, password.toCharArray()); |
| 171 | 0 | out.close(); |
| 172 | 0 | logger.debug("Keystore successfully wrote..."); |
| 173 | 0 | |
| 174 | 0 | } catch (Exception ex) { |
| 175 | 0 | logger.error(ex.getMessage()); |
| 176 | 0 | ex.printStackTrace(); |
| 177 | 0 | } |
| 178 | 0 | } |
| 179 | 0 | |
| 180 | 0 | /* |
| 181 | * (non-Javadoc) | |
| 182 | * | |
| 183 | 0 | * @see base.jdbs.cryptography.KeyRing#retrieve(java.io.File, |
| 184 | * java.lang.String) | |
| 185 | 0 | */ |
| 186 | 0 | public KeyStore retrieve(File location, String password) { |
| 187 | 0 | KeyStore keyStore = null; |
| 188 | 0 | try { |
| 189 | 0 | keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 190 | 0 | FileInputStream fileInputStream = new FileInputStream(location); |
| 191 | 0 | keyStore.load(fileInputStream, password.toCharArray()); |
| 192 | 0 | fileInputStream.close(); |
| 193 | ||
| 194 | // For each key entry we must retrieve the private key and its | |
| 195 | // certificate. | |
| 196 | 0 | Enumeration aliasEnumerationOnKey = keyStore.aliases(); |
| 197 | 0 | while (aliasEnumerationOnKey.hasMoreElements()) { |
| 198 | 0 | String keyAlias = aliasEnumerationOnKey.nextElement() |
| 199 | .toString(); | |
| 200 | 0 | if (keyStore.isKeyEntry(keyAlias)) { |
| 201 | 0 | Key key = keyStore.getKey(keyAlias, password.toCharArray()); |
| 202 | 0 | String certAlias = keyAlias.replaceAll(KEY_ENTRY, |
| 203 | CERTIFICATE_ENTRY); | |
| 204 | 0 | if (keyStore.isCertificateEntry(certAlias)) { |
| 205 | 0 | X509Certificate certificate = (X509Certificate) keyStore |
| 206 | .getCertificate(certAlias); | |
| 207 | 0 | logger.debug("This is the certificate: \n" |
| 208 | + certificate); | |
| 209 | 0 | base.jdbs.cryptography.asymmetric.PublicKey publicKey = new base.jdbs.cryptography.asymmetric.PublicKey( |
| 210 | certificate.getPublicKey()); | |
| 211 | 0 | logger.debug("This is the publicKey: \n" |
| 212 | + publicKey.getValue()); | |
| 213 | 0 | base.jdbs.cryptography.asymmetric.PrivateKey privateKey = new base.jdbs.cryptography.asymmetric.PrivateKey( |
| 214 | (java.security.PrivateKey) key); | |
| 215 | 0 | logger.debug("This is the privateKey: \n" |
| 216 | + privateKey.getValue()); | |
| 217 | 0 | this.addKeyPair(new KeyPair(this.nextKeyPairId(), this |
| 218 | .getOwner().getNickname() | |
| 219 | + "-KeyPair", publicKey, privateKey, | |
| 220 | certificate)); | |
| 221 | 0 | } else |
| 222 | 0 | logger.error(certAlias |
| 223 | + " isn't a certificate entry!!!"); | |
| 224 | ||
| 225 | } | |
| 226 | 0 | } |
| 227 | ||
| 228 | 0 | } catch (Exception ex) { |
| 229 | 0 | logger.error(ex.getMessage()); |
| 230 | 0 | ex.printStackTrace(); |
| 231 | 0 | } |
| 232 | 0 | return keyStore; |
| 233 | } | |
| 234 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |