| 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.security.InvalidKeyException; | |
| 23 | import java.security.KeyPair; | |
| 24 | import java.security.KeyPairGenerator; | |
| 25 | import java.security.NoSuchAlgorithmException; | |
| 26 | import java.security.NoSuchProviderException; | |
| 27 | import java.security.PrivateKey; | |
| 28 | import java.security.PublicKey; | |
| 29 | import java.security.SecureRandom; | |
| 30 | import java.security.Security; | |
| 31 | import java.security.SignatureException; | |
| 32 | import java.security.cert.X509Certificate; | |
| 33 | import java.util.Calendar; | |
| 34 | import java.util.GregorianCalendar; | |
| 35 | ||
| 36 | import org.apache.log4j.Logger; | |
| 37 | import org.bouncycastle.jce.X509Principal; | |
| 38 | import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
| 39 | ||
| 40 | import base.jdbs.GUIDGenerator; | |
| 41 | ||
| 42 | 0 | public class RSAKeyPairFactory { |
| 43 | 0 | |
| 44 | 0 | private static final transient Logger logger = Logger |
| 45 | 0 | .getLogger(RSAKeyPairFactory.class.getName()); |
| 46 | ||
| 47 | public static base.jdbs.cryptography.asymmetric.KeyPair newKeyPair( | |
| 48 | 0 | AsymmetricKeyRing asymmetricKeyRing, String description) |
| 49 | 0 | throws NoSuchAlgorithmException, NoSuchProviderException, |
| 50 | 0 | InvalidKeyException, SecurityException, SignatureException { |
| 51 | 0 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); |
| 52 | 0 | SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN"); |
| 53 | 0 | keyPairGenerator.initialize(1024, secureRandom); |
| 54 | 0 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); |
| 55 | ||
| 56 | 0 | PrivateKey privateKey = keyPair.getPrivate(); |
| 57 | 0 | logger.debug("PrivateKey:\n" + privateKey); |
| 58 | ||
| 59 | 0 | PublicKey publicKey = keyPair.getPublic(); |
| 60 | 0 | logger.debug("PublicKey:\n" + publicKey); |
| 61 | 0 | |
| 62 | 0 | Security.addProvider(new BouncyCastleProvider()); |
| 63 | 0 | org.bouncycastle.jce.X509V3CertificateGenerator generator = new org.bouncycastle.jce.X509V3CertificateGenerator(); |
| 64 | 0 | generator.setSignatureAlgorithm("SHA1WITHRSA"); |
| 65 | ||
| 66 | /* X509V3CertificateGenerator PARAMETER SETTING */ | |
| 67 | 0 | |
| 68 | /* Setup the SerialNumber attribute */ | |
| 69 | 0 | generator.setSerialNumber(GUIDGenerator.getInstance().getNumericKey()); |
| 70 | 0 | |
| 71 | 0 | /* COMMON PARAMETERS IN SELF-CERTIFICATE */ |
| 72 | 0 | String CN = "CN=" + asymmetricKeyRing.getOwner().getName(); |
| 73 | 0 | String OU = "OU=JDBS-OU"; |
| 74 | 0 | String O = "O=JDBS-O"; |
| 75 | 0 | String L = "L=" + asymmetricKeyRing.getOwner().getSurname(); |
| 76 | 0 | String C = "C=SP"; |
| 77 | 0 | |
| 78 | /* Setup the IssuerDomainName attribute */ | |
| 79 | 0 | generator.setIssuerDN(new X509Principal(CN + "," + OU + "," + O + "," |
| 80 | 0 | + L + "," + C)); |
| 81 | 0 | |
| 82 | 0 | /* Setup the NotBefore attribute */ |
| 83 | 0 | long currentTime = System.currentTimeMillis(); |
| 84 | 0 | GregorianCalendar notBefore = new GregorianCalendar(); |
| 85 | 0 | notBefore.setTimeInMillis(currentTime); |
| 86 | 0 | generator.setNotBefore(notBefore.getTime()); |
| 87 | 0 | |
| 88 | 0 | /* Setup the NotAfter attribute */ |
| 89 | 0 | GregorianCalendar notAfter = new GregorianCalendar(); |
| 90 | 0 | notAfter.setTimeInMillis(currentTime); |
| 91 | 0 | notAfter.set(Calendar.YEAR, notBefore.get(Calendar.YEAR) + 1);// The |
| 92 | 0 | // certificate |
| 93 | // will | |
| 94 | // be | |
| 95 | 0 | // valid |
| 96 | // for 1 | |
| 97 | 0 | // Year |
| 98 | // from | |
| 99 | 0 | // now. |
| 100 | 0 | generator.setNotAfter(notAfter.getTime()); |
| 101 | 0 | |
| 102 | /* Setup the Subject Domain Name */ | |
| 103 | 0 | generator.setSubjectDN(new X509Principal(CN + "," + OU + "," + O + "," |
| 104 | + L + "," + C)); | |
| 105 | ||
| 106 | /* Setup the PublikKey attribute */ | |
| 107 | 0 | generator.setPublicKey(publicKey); |
| 108 | ||
| 109 | 0 | X509Certificate certificate = generator |
| 110 | .generateX509Certificate(privateKey); | |
| 111 | ||
| 112 | 0 | logger.debug("Certificate:\n" + certificate); |
| 113 | ||
| 114 | 0 | return new base.jdbs.cryptography.asymmetric.KeyPair(asymmetricKeyRing |
| 115 | .nextKeyPairId(), asymmetricKeyRing.getOwner().getNickname() | |
| 116 | + "-KeyPair", new base.jdbs.cryptography.asymmetric.PublicKey( | |
| 117 | publicKey), new base.jdbs.cryptography.asymmetric.PrivateKey( | |
| 118 | privateKey), certificate); | |
| 119 | } | |
| 120 | ||
| 121 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |