Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/classpath/tools/jarsigner/JarSigner.java


1   /* JarSigner.java -- The signing handler of the gjarsigner tool
2      Copyright (C) 2006 Free Software Foundation, Inc.
3   
4   This file is part of GNU Classpath.
5   
6   GNU Classpath is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10  
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20  
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25  
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37  
38  
39  package gnu.classpath.tools.jarsigner;
40  
41  import gnu.classpath.SystemProperties;
42  import gnu.java.util.jar.JarUtils;
43  
44  import java.io.File;
45  import java.io.FileOutputStream;
46  import java.io.IOException;
47  import java.io.InputStream;
48  import java.util.Enumeration;
49  import java.util.jar.JarEntry;
50  import java.util.jar.JarFile;
51  import java.util.jar.JarOutputStream;
52  import java.util.logging.Logger;
53  
54  /**
55   * The JAR signing handler of the <code>gjarsigner</code> tool.
56   */
57  public class JarSigner
58  {
59    private static final Logger log = Logger.getLogger(JarSigner.class.getName());
60    /** The owner tool of this handler. */
61    private Main main;
62  
63    JarSigner(Main main)
64    {
65      super();
66  
67      this.main = main;
68    }
69  
70    void start() throws Exception
71    {
72      log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
73  
74      JarFile jarFile = new JarFile(main.getJarFileName());
75      SFHelper sfHelper = new SFHelper(jarFile);
76  
77      sfHelper.startSigning();
78  
79      // 1. compute the digests
80      for (Enumeration e = jarFile.entries(); e.hasMoreElements(); )
81        {
82          JarEntry je = (JarEntry) e.nextElement();
83          String jeName = je.getName();
84          if (jeName.equals(JarFile.MANIFEST_NAME)
85              || jeName.endsWith(File.separator))
86            continue;
87  
88          sfHelper.updateEntry(je);
89          if (main.isVerbose())
90            System.out.println(Messages.getString("JarSigner.1") + jeName); //$NON-NLS-1$
91        }
92  
93      sfHelper.finishSigning(main.isSectionsOnly());
94      if (main.isVerbose())
95        System.out.println(Messages.getString("JarSigner.2") + JarFile.MANIFEST_NAME); //$NON-NLS-1$
96  
97      // 2. write jar entries and manifest
98      File signedJarFile = File.createTempFile("gcp-", ".jar"); //$NON-NLS-1$ //$NON-NLS-2$
99      FileOutputStream fos = new FileOutputStream(signedJarFile);
100     JarOutputStream outSignedJarFile = new JarOutputStream(fos,
101                                                            sfHelper.getManifest());
102     for (Enumeration e = jarFile.entries(); e.hasMoreElements(); )
103       {
104         JarEntry je = (JarEntry) e.nextElement();
105         String jeName = je.getName();
106         if (jeName.equals(JarFile.MANIFEST_NAME)
107             || jeName.endsWith(File.separator))
108           continue;
109 
110         log.finest("Processing " + jeName); //$NON-NLS-1$
111         JarEntry newEntry = new JarEntry(jeName);
112         newEntry.setTime(je.getTime());
113         outSignedJarFile.putNextEntry(newEntry);
114         InputStream jeis = jarFile.getInputStream(je);
115         copyFromTo(jeis, outSignedJarFile);
116       }
117 
118     // 3. create the .SF file
119     String signaturesFileName = main.getSigFileName();
120     String sfFileName = JarUtils.META_INF + signaturesFileName
121                         + JarUtils.SF_SUFFIX;
122     log.finest("Processing " + sfFileName); //$NON-NLS-1$
123     JarEntry sfEntry = new JarEntry(sfFileName);
124     sfEntry.setTime(System.currentTimeMillis());
125     outSignedJarFile.putNextEntry(sfEntry);
126     sfHelper.writeSF(outSignedJarFile);
127     log.finer("Created .SF file"); //$NON-NLS-1$
128     if (main.isVerbose())
129       System.out.println(Messages.getString("JarSigner.8") + sfFileName); //$NON-NLS-1$
130 
131     // 4. create the .DSA file
132     String dsaFileName = JarUtils.META_INF + signaturesFileName
133                          + JarUtils.DSA_SUFFIX;
134     log.finest("Processing " + dsaFileName); //$NON-NLS-1$
135     JarEntry dsaEntry = new JarEntry(dsaFileName);
136     dsaEntry.setTime(System.currentTimeMillis());
137     outSignedJarFile.putNextEntry(dsaEntry);
138     sfHelper.writeDSA(outSignedJarFile,
139                       main.getSignerPrivateKey(),
140                       main.getSignerCertificateChain(),
141                       main.isInternalSF());
142     log.finer("Created .DSA file"); //$NON-NLS-1$
143     if (main.isVerbose())
144       System.out.println(Messages.getString("JarSigner.11") + dsaFileName); //$NON-NLS-1$
145 
146     // cleanup
147     outSignedJarFile.close();
148     fos.close();
149     signedJarFile.renameTo(new File(main.getSignedJarFileName()));
150     log.finer("Renamed signed JAR file"); //$NON-NLS-1$
151     if (main.isVerbose())
152       System.out.println(SystemProperties.getProperty("line.separator") //$NON-NLS-1$
153                          + Messages.getString("JarSigner.14")); //$NON-NLS-1$
154 
155     log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
156   }
157 
158   private void copyFromTo(InputStream in, JarOutputStream out)
159     throws IOException
160   {
161     byte[] buffer = new byte[8192];
162     int n;
163     while ((n = in.read(buffer)) != -1)
164       if (n > 0)
165         out.write(buffer, 0, n);
166   }
167 }