This program signs a certificate, using the private key of another certificate in a keystore. : KeyStore « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Security » KeyStoreScreenshots 
This program signs a certificate, using the private key of another certificate in a keystore.
   

/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.Console;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import sun.security.x509.CertificateIssuerName;
import sun.security.x509.X500Name;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;

/**
 * This program signs a certificate, using the private key of another certificate in a keystore.
 @version 1.01 2007-10-07
 @author Cay Horstmann
 */
public class CertificateSigner
{
   public static void main(String[] args)
   {
      String ksname = null// the keystore name
      String alias = null// the private key alias
      String inname = null// the input file name
      String outname = null// the output file name
      for (int i = 0; i < args.length; i += 2)
      {
         if (args[i].equals("-keystore")) ksname = args[i + 1];
         else if (args[i].equals("-alias")) alias = args[i + 1];
         else if (args[i].equals("-infile")) inname = args[i + 1];
         else if (args[i].equals("-outfile")) outname = args[i + 1];
         else usage();
      }

      if (ksname == null || alias == null || inname == null || outname == nullusage();

      try
      {
         Console console = System.console();
         if (console == nullerror("No console");
         char[] password = console.readPassword("Keystore password: ");
         KeyStore store = KeyStore.getInstance("JKS""SUN");
         InputStream in = new FileInputStream(ksname);
         store.load(in, password);
         Arrays.fill(password, ' ');
         in.close();

         char[] keyPassword = console.readPassword("Key password for %s: ", alias);
         PrivateKey issuerPrivateKey = (PrivateKeystore.getKey(alias, keyPassword);
         Arrays.fill(keyPassword, ' ');

         if (issuerPrivateKey == nullerror("No such private key");

         in = new FileInputStream(inname);

         CertificateFactory factory = CertificateFactory.getInstance("X.509");

         X509Certificate inCert = (X509Certificatefactory.generateCertificate(in);
         in.close();
         byte[] inCertBytes = inCert.getTBSCertificate();

         X509Certificate issuerCert = (X509Certificatestore.getCertificate(alias);
         Principal issuer = issuerCert.getSubjectDN();
         String issuerSigAlg = issuerCert.getSigAlgName();

         FileOutputStream out = new FileOutputStream(outname);

         X509CertInfo info = new X509CertInfo(inCertBytes);
         info.set(X509CertInfo.ISSUER, new CertificateIssuerName((X500Nameissuer));

         X509CertImpl outCert = new X509CertImpl(info);
         outCert.sign(issuerPrivateKey, issuerSigAlg);
         outCert.derEncode(out);

         out.close();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   /**
    * Prints an error message and exits.
    @param message the error message
    */
   public static void error(String message)
   {
      System.out.println(message);
      System.exit(1);
   }

   /**
    * Prints a usage message and exits.
    */
   public static void usage()
   {
      System.out.println("Usage: java CertificateSigner"
            " -keystore keyStore -alias issuerKeyAlias"
            " -infile inputFile -outfile outputFile");
      System.exit(1);
   }
}

   
    
    
  
Related examples in the same category
1.Exporting a Certificate to a File
2.Listing the Aliases in a Key Store: A key store is a collection of keys and certificates.
3.Listing the Aliases in a Key Store using keytool:
4.Retrieving a Key Pair from a Key Store
5.Create a keystore with a self-signed certificate, using the keytool command
6.Import a key/certificate pair from a pkcs12 file into a regular JKS format keystore
7.This class imports a key and a certificate into a keystore
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.