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

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.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  
420public class RSAKeyPairFactory {
430 
440    private static final transient Logger logger = Logger
450            .getLogger(RSAKeyPairFactory.class.getName());
46  
47     public static base.jdbs.cryptography.asymmetric.KeyPair newKeyPair(
480            AsymmetricKeyRing asymmetricKeyRing, String description)
490            throws NoSuchAlgorithmException, NoSuchProviderException,
500            InvalidKeyException, SecurityException, SignatureException {
510        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
520        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
530        keyPairGenerator.initialize(1024, secureRandom);
540        KeyPair keyPair = keyPairGenerator.generateKeyPair();
55  
560        PrivateKey privateKey = keyPair.getPrivate();
570        logger.debug("PrivateKey:\n" + privateKey);
58  
590        PublicKey publicKey = keyPair.getPublic();
600        logger.debug("PublicKey:\n" + publicKey);
610 
620        Security.addProvider(new BouncyCastleProvider());
630        org.bouncycastle.jce.X509V3CertificateGenerator generator = new org.bouncycastle.jce.X509V3CertificateGenerator();
640        generator.setSignatureAlgorithm("SHA1WITHRSA");
65  
66         /* X509V3CertificateGenerator PARAMETER SETTING */
670 
68         /* Setup the SerialNumber attribute */
690        generator.setSerialNumber(GUIDGenerator.getInstance().getNumericKey());
700 
710        /* COMMON PARAMETERS IN SELF-CERTIFICATE */
720        String CN = "CN=" + asymmetricKeyRing.getOwner().getName();
730        String OU = "OU=JDBS-OU";
740        String O = "O=JDBS-O";
750        String L = "L=" + asymmetricKeyRing.getOwner().getSurname();
760        String C = "C=SP";
770 
78         /* Setup the IssuerDomainName attribute */
790        generator.setIssuerDN(new X509Principal(CN + "," + OU + "," + O + ","
800                + L + "," + C));
810 
820        /* Setup the NotBefore attribute */
830        long currentTime = System.currentTimeMillis();
840        GregorianCalendar notBefore = new GregorianCalendar();
850        notBefore.setTimeInMillis(currentTime);
860        generator.setNotBefore(notBefore.getTime());
870 
880        /* Setup the NotAfter attribute */
890        GregorianCalendar notAfter = new GregorianCalendar();
900        notAfter.setTimeInMillis(currentTime);
910        notAfter.set(Calendar.YEAR, notBefore.get(Calendar.YEAR) + 1);// The
920        // certificate
93         // will
94         // be
950        // valid
96         // for 1
970        // Year
98         // from
990        // now.
1000        generator.setNotAfter(notAfter.getTime());
1010 
102         /* Setup the Subject Domain Name */
1030        generator.setSubjectDN(new X509Principal(CN + "," + OU + "," + O + ","
104                 + L + "," + C));
105  
106         /* Setup the PublikKey attribute */
1070        generator.setPublicKey(publicKey);
108  
1090        X509Certificate certificate = generator
110                 .generateX509Certificate(privateKey);
111  
1120        logger.debug("Certificate:\n" + certificate);
113  
1140        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.
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.