author | weijun |
Fri, 04 Sep 2009 14:58:01 +0800 | |
changeset 3716 | aa66143f7ad1 |
parent 3481 | 6ae7a2a6c956 |
child 3951 | e821908c953e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
2 |
* Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.tools; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.*; |
|
30 |
import java.util.zip.*; |
|
31 |
import java.util.jar.*; |
|
32 |
import java.math.BigInteger; |
|
33 |
import java.net.URI; |
|
34 |
import java.net.URISyntaxException; |
|
35 |
import java.text.Collator; |
|
36 |
import java.text.MessageFormat; |
|
37 |
import java.security.cert.Certificate; |
|
38 |
import java.security.cert.X509Certificate; |
|
39 |
import java.security.cert.CertificateException; |
|
40 |
import java.security.*; |
|
41 |
import java.lang.reflect.Constructor; |
|
42 |
||
43 |
import com.sun.jarsigner.ContentSigner; |
|
44 |
import com.sun.jarsigner.ContentSignerParameters; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
45 |
import java.net.SocketTimeoutException; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
46 |
import java.net.URL; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
47 |
import java.net.URLClassLoader; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
48 |
import java.security.cert.CertPath; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
49 |
import java.security.cert.CertPathValidator; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
50 |
import java.security.cert.CertificateExpiredException; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
51 |
import java.security.cert.CertificateFactory; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
52 |
import java.security.cert.CertificateNotYetValidException; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
53 |
import java.security.cert.PKIXParameters; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
54 |
import java.security.cert.TrustAnchor; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
55 |
import java.util.Map.Entry; |
2 | 56 |
import sun.security.x509.*; |
57 |
import sun.security.util.*; |
|
58 |
import sun.misc.BASE64Encoder; |
|
59 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
60 |
|
2 | 61 |
/** |
62 |
* <p>The jarsigner utility. |
|
63 |
* |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
64 |
* The exit codes for the main method are: |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
65 |
* |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
66 |
* 0: success |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
67 |
* 1: any error that the jar cannot be signed or verified, including: |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
68 |
* keystore loading error |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
69 |
* TSP communciation error |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
70 |
* jarsigner command line error... |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
71 |
* otherwise: error codes from -strict |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
72 |
* |
2 | 73 |
* @author Roland Schemers |
74 |
* @author Jan Luehe |
|
75 |
*/ |
|
76 |
||
77 |
public class JarSigner { |
|
78 |
||
79 |
// for i18n |
|
80 |
private static final java.util.ResourceBundle rb = |
|
81 |
java.util.ResourceBundle.getBundle |
|
82 |
("sun.security.tools.JarSignerResources"); |
|
83 |
private static final Collator collator = Collator.getInstance(); |
|
84 |
static { |
|
85 |
// this is for case insensitive string comparisions |
|
86 |
collator.setStrength(Collator.PRIMARY); |
|
87 |
} |
|
88 |
||
89 |
private static final String META_INF = "META-INF/"; |
|
90 |
||
91 |
// prefix for new signature-related files in META-INF directory |
|
92 |
private static final String SIG_PREFIX = META_INF + "SIG-"; |
|
93 |
||
94 |
private static final Class[] PARAM_STRING = { String.class }; |
|
95 |
||
96 |
private static final String NONE = "NONE"; |
|
97 |
private static final String P11KEYSTORE = "PKCS11"; |
|
98 |
||
99 |
private static final long SIX_MONTHS = 180*24*60*60*1000L; //milliseconds |
|
100 |
||
101 |
// Attention: |
|
102 |
// This is the entry that get launched by the security tool jarsigner. |
|
103 |
public static void main(String args[]) throws Exception { |
|
104 |
JarSigner js = new JarSigner(); |
|
105 |
js.run(args); |
|
106 |
} |
|
107 |
||
108 |
static final String VERSION = "1.0"; |
|
109 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
110 |
static final int IN_KEYSTORE = 0x01; // signer is in keystore |
2 | 111 |
static final int IN_SCOPE = 0x02; |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
112 |
static final int NOT_ALIAS = 0x04; // alias list is NOT empty and |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
113 |
// signer is not in alias list |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
114 |
static final int SIGNED_BY_ALIAS = 0x08; // signer is in alias list |
2 | 115 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
116 |
X509Certificate[] certChain; // signer's cert chain (when composing) |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
117 |
PrivateKey privateKey; // private key |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
118 |
KeyStore store; // the keystore specified by -keystore |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
119 |
// or the default keystore, never null |
2 | 120 |
|
121 |
IdentityScope scope; |
|
122 |
||
123 |
String keystore; // key store file |
|
124 |
boolean nullStream = false; // null keystore input stream (NONE) |
|
125 |
boolean token = false; // token-based keystore |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
126 |
String jarfile; // jar file to sign or verify |
2 | 127 |
String alias; // alias to sign jar with |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
128 |
List<String> ckaliases = new ArrayList<String>(); // aliases in -verify |
2 | 129 |
char[] storepass; // keystore password |
130 |
boolean protectedPath; // protected authentication path |
|
131 |
String storetype; // keystore type |
|
132 |
String providerName; // provider name |
|
133 |
Vector<String> providers = null; // list of providers |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
134 |
// arguments for provider constructors |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
135 |
HashMap<String,String> providerArgs = new HashMap<String, String>(); |
2 | 136 |
char[] keypass; // private key password |
137 |
String sigfile; // name of .SF file |
|
138 |
String sigalg; // name of signature algorithm |
|
3318
dade78e63c92
6561126: keytool should use larger default keysize for keypairs
weijun
parents:
3047
diff
changeset
|
139 |
String digestalg = "SHA-256"; // name of digest algorithm |
2 | 140 |
String signedjar; // output filename |
141 |
String tsaUrl; // location of the Timestamping Authority |
|
142 |
String tsaAlias; // alias for the Timestamping Authority's certificate |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
143 |
String altCertChain; // file to read alternative cert chain from |
2 | 144 |
boolean verify = false; // verify the jar |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
145 |
String verbose = null; // verbose output when signing/verifying |
2 | 146 |
boolean showcerts = false; // show certs when verifying |
147 |
boolean debug = false; // debug |
|
148 |
boolean signManifest = true; // "sign" the whole manifest |
|
149 |
boolean externalSF = true; // leave the .SF out of the PKCS7 block |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
150 |
boolean strict = false; // treat warnings as error |
2 | 151 |
|
152 |
// read zip entry raw bytes |
|
153 |
private ByteArrayOutputStream baos = new ByteArrayOutputStream(2048); |
|
154 |
private byte[] buffer = new byte[8192]; |
|
155 |
private ContentSigner signingMechanism = null; |
|
156 |
private String altSignerClass = null; |
|
157 |
private String altSignerClasspath = null; |
|
158 |
private ZipFile zipFile = null; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
159 |
|
2 | 160 |
private boolean hasExpiredCert = false; |
161 |
private boolean hasExpiringCert = false; |
|
162 |
private boolean notYetValidCert = false; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
163 |
private boolean chainNotValidated = false; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
164 |
private boolean notSignedByAlias = false; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
165 |
private boolean aliasNotInStore = false; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
166 |
private boolean hasUnsignedEntry = false; |
2 | 167 |
private boolean badKeyUsage = false; |
168 |
private boolean badExtendedKeyUsage = false; |
|
169 |
private boolean badNetscapeCertType = false; |
|
170 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
171 |
CertificateFactory certificateFactory; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
172 |
CertPathValidator validator; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
173 |
PKIXParameters pkixParameters; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
174 |
|
2 | 175 |
public void run(String args[]) { |
176 |
try { |
|
177 |
parseArgs(args); |
|
178 |
||
179 |
// Try to load and install the specified providers |
|
180 |
if (providers != null) { |
|
181 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
|
182 |
Enumeration<String> e = providers.elements(); |
|
183 |
while (e.hasMoreElements()) { |
|
184 |
String provName = e.nextElement(); |
|
185 |
Class<?> provClass; |
|
186 |
if (cl != null) { |
|
187 |
provClass = cl.loadClass(provName); |
|
188 |
} else { |
|
189 |
provClass = Class.forName(provName); |
|
190 |
} |
|
191 |
||
192 |
String provArg = providerArgs.get(provName); |
|
193 |
Object obj; |
|
194 |
if (provArg == null) { |
|
195 |
obj = provClass.newInstance(); |
|
196 |
} else { |
|
197 |
Constructor<?> c = |
|
198 |
provClass.getConstructor(PARAM_STRING); |
|
199 |
obj = c.newInstance(provArg); |
|
200 |
} |
|
201 |
||
202 |
if (!(obj instanceof Provider)) { |
|
203 |
MessageFormat form = new MessageFormat(rb.getString |
|
204 |
("provName not a provider")); |
|
205 |
Object[] source = {provName}; |
|
206 |
throw new Exception(form.format(source)); |
|
207 |
} |
|
208 |
Security.addProvider((Provider)obj); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
if (verify) { |
|
213 |
try { |
|
214 |
loadKeyStore(keystore, false); |
|
215 |
scope = IdentityScope.getSystemScope(); |
|
216 |
} catch (Exception e) { |
|
217 |
if ((keystore != null) || (storepass != null)) { |
|
218 |
System.out.println(rb.getString("jarsigner error: ") + |
|
219 |
e.getMessage()); |
|
220 |
System.exit(1); |
|
221 |
} |
|
222 |
} |
|
223 |
/* if (debug) { |
|
224 |
SignatureFileVerifier.setDebug(true); |
|
225 |
ManifestEntryVerifier.setDebug(true); |
|
226 |
} |
|
227 |
*/ |
|
228 |
verifyJar(jarfile); |
|
229 |
} else { |
|
230 |
loadKeyStore(keystore, true); |
|
231 |
getAliasInfo(alias); |
|
232 |
||
233 |
// load the alternative signing mechanism |
|
234 |
if (altSignerClass != null) { |
|
235 |
signingMechanism = loadSigningMechanism(altSignerClass, |
|
236 |
altSignerClasspath); |
|
237 |
} |
|
238 |
signJar(jarfile, alias, args); |
|
239 |
} |
|
240 |
} catch (Exception e) { |
|
241 |
System.out.println(rb.getString("jarsigner error: ") + e); |
|
242 |
if (debug) { |
|
243 |
e.printStackTrace(); |
|
244 |
} |
|
245 |
System.exit(1); |
|
246 |
} finally { |
|
247 |
// zero-out private key password |
|
248 |
if (keypass != null) { |
|
249 |
Arrays.fill(keypass, ' '); |
|
250 |
keypass = null; |
|
251 |
} |
|
252 |
// zero-out keystore password |
|
253 |
if (storepass != null) { |
|
254 |
Arrays.fill(storepass, ' '); |
|
255 |
storepass = null; |
|
256 |
} |
|
257 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
258 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
259 |
if (strict) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
260 |
int exitCode = 0; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
261 |
if (hasExpiringCert) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
262 |
exitCode |= 2; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
263 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
264 |
if (chainNotValidated) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
265 |
// hasExpiredCert and notYetValidCert included in this case |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
266 |
exitCode |= 4; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
267 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
268 |
if (badKeyUsage || badExtendedKeyUsage || badNetscapeCertType) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
269 |
exitCode |= 8; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
270 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
271 |
if (hasUnsignedEntry) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
272 |
exitCode |= 16; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
273 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
274 |
if (notSignedByAlias || aliasNotInStore) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
275 |
exitCode |= 32; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
276 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
277 |
if (exitCode != 0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
278 |
System.exit(exitCode); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
279 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
280 |
} |
2 | 281 |
} |
282 |
||
283 |
/* |
|
284 |
* Parse command line arguments. |
|
285 |
*/ |
|
286 |
void parseArgs(String args[]) { |
|
287 |
/* parse flags */ |
|
288 |
int n = 0; |
|
289 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
290 |
if (args.length == 0) fullusage(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
291 |
for (n=0; n < args.length; n++) { |
2 | 292 |
|
293 |
String flags = args[n]; |
|
294 |
||
295 |
if (collator.compare(flags, "-keystore") == 0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
296 |
if (++n == args.length) usageNoArg(); |
2 | 297 |
keystore = args[n]; |
298 |
} else if (collator.compare(flags, "-storepass") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
299 |
if (++n == args.length) usageNoArg(); |
2 | 300 |
storepass = args[n].toCharArray(); |
301 |
} else if (collator.compare(flags, "-storetype") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
302 |
if (++n == args.length) usageNoArg(); |
2 | 303 |
storetype = args[n]; |
304 |
} else if (collator.compare(flags, "-providerName") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
305 |
if (++n == args.length) usageNoArg(); |
2 | 306 |
providerName = args[n]; |
307 |
} else if ((collator.compare(flags, "-provider") == 0) || |
|
308 |
(collator.compare(flags, "-providerClass") == 0)) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
309 |
if (++n == args.length) usageNoArg(); |
2 | 310 |
if (providers == null) { |
311 |
providers = new Vector<String>(3); |
|
312 |
} |
|
313 |
providers.add(args[n]); |
|
314 |
||
315 |
if (args.length > (n+1)) { |
|
316 |
flags = args[n+1]; |
|
317 |
if (collator.compare(flags, "-providerArg") == 0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
318 |
if (args.length == (n+2)) usageNoArg(); |
2 | 319 |
providerArgs.put(args[n], args[n+2]); |
320 |
n += 2; |
|
321 |
} |
|
322 |
} |
|
323 |
} else if (collator.compare(flags, "-protected") ==0) { |
|
324 |
protectedPath = true; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
325 |
} else if (collator.compare(flags, "-certchain") ==0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
326 |
if (++n == args.length) usageNoArg(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
327 |
altCertChain = args[n]; |
2 | 328 |
} else if (collator.compare(flags, "-debug") ==0) { |
329 |
debug = true; |
|
330 |
} else if (collator.compare(flags, "-keypass") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
331 |
if (++n == args.length) usageNoArg(); |
2 | 332 |
keypass = args[n].toCharArray(); |
333 |
} else if (collator.compare(flags, "-sigfile") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
334 |
if (++n == args.length) usageNoArg(); |
2 | 335 |
sigfile = args[n]; |
336 |
} else if (collator.compare(flags, "-signedjar") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
337 |
if (++n == args.length) usageNoArg(); |
2 | 338 |
signedjar = args[n]; |
339 |
} else if (collator.compare(flags, "-tsa") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
340 |
if (++n == args.length) usageNoArg(); |
2 | 341 |
tsaUrl = args[n]; |
342 |
} else if (collator.compare(flags, "-tsacert") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
343 |
if (++n == args.length) usageNoArg(); |
2 | 344 |
tsaAlias = args[n]; |
345 |
} else if (collator.compare(flags, "-altsigner") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
346 |
if (++n == args.length) usageNoArg(); |
2 | 347 |
altSignerClass = args[n]; |
348 |
} else if (collator.compare(flags, "-altsignerpath") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
349 |
if (++n == args.length) usageNoArg(); |
2 | 350 |
altSignerClasspath = args[n]; |
351 |
} else if (collator.compare(flags, "-sectionsonly") ==0) { |
|
352 |
signManifest = false; |
|
353 |
} else if (collator.compare(flags, "-internalsf") ==0) { |
|
354 |
externalSF = false; |
|
355 |
} else if (collator.compare(flags, "-verify") ==0) { |
|
356 |
verify = true; |
|
357 |
} else if (collator.compare(flags, "-verbose") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
358 |
verbose = "all"; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
359 |
} else if (collator.compare(flags, "-verbose:all") ==0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
360 |
verbose = "all"; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
361 |
} else if (collator.compare(flags, "-verbose:summary") ==0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
362 |
verbose = "summary"; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
363 |
} else if (collator.compare(flags, "-verbose:grouped") ==0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
364 |
verbose = "grouped"; |
2 | 365 |
} else if (collator.compare(flags, "-sigalg") ==0) { |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
366 |
if (++n == args.length) usageNoArg(); |
2 | 367 |
sigalg = args[n]; |
368 |
} else if (collator.compare(flags, "-digestalg") ==0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
369 |
if (++n == args.length) usageNoArg(); |
2 | 370 |
digestalg = args[n]; |
371 |
} else if (collator.compare(flags, "-certs") ==0) { |
|
372 |
showcerts = true; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
373 |
} else if (collator.compare(flags, "-strict") ==0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
374 |
strict = true; |
2 | 375 |
} else if (collator.compare(flags, "-h") == 0 || |
376 |
collator.compare(flags, "-help") == 0) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
377 |
fullusage(); |
2 | 378 |
} else { |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
379 |
if (!flags.startsWith("-")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
380 |
if (jarfile == null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
381 |
jarfile = flags; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
382 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
383 |
alias = flags; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
384 |
ckaliases.add(alias); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
385 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
386 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
387 |
System.err.println( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
388 |
rb.getString("Illegal option: ") + flags); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
389 |
usage(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
390 |
} |
2 | 391 |
} |
392 |
} |
|
393 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
394 |
// -certs must always be specified with -verbose |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
395 |
if (verbose == null) showcerts = false; |
2 | 396 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
397 |
if (jarfile == null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
398 |
System.err.println(rb.getString("Please specify jarfile name")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
399 |
usage(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
400 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
401 |
if (!verify && alias == null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
402 |
System.err.println(rb.getString("Please specify alias name")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
403 |
usage(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
404 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
405 |
if (!verify && ckaliases.size() > 1) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
406 |
System.err.println(rb.getString("Only one alias can be specified")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
407 |
usage(); |
2 | 408 |
} |
409 |
||
410 |
if (storetype == null) { |
|
411 |
storetype = KeyStore.getDefaultType(); |
|
412 |
} |
|
413 |
storetype = KeyStoreUtil.niceStoreTypeName(storetype); |
|
414 |
||
3481
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
415 |
try { |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
416 |
if (signedjar != null && new File(signedjar).getCanonicalPath().equals( |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
417 |
new File(jarfile).getCanonicalPath())) { |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
418 |
signedjar = null; |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
419 |
} |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
420 |
} catch (IOException ioe) { |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
421 |
// File system error? |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
422 |
// Just ignore it. |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
423 |
} |
6ae7a2a6c956
6866479: libzip.so caused JVM to crash when running jarsigner
weijun
parents:
3318
diff
changeset
|
424 |
|
2 | 425 |
if (P11KEYSTORE.equalsIgnoreCase(storetype) || |
426 |
KeyStoreUtil.isWindowsKeyStore(storetype)) { |
|
427 |
token = true; |
|
428 |
if (keystore == null) { |
|
429 |
keystore = NONE; |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
if (NONE.equals(keystore)) { |
|
434 |
nullStream = true; |
|
435 |
} |
|
436 |
||
437 |
if (token && !nullStream) { |
|
438 |
System.err.println(MessageFormat.format(rb.getString |
|
439 |
("-keystore must be NONE if -storetype is {0}"), storetype)); |
|
440 |
usage(); |
|
441 |
} |
|
442 |
||
443 |
if (token && keypass != null) { |
|
444 |
System.err.println(MessageFormat.format(rb.getString |
|
445 |
("-keypass can not be specified " + |
|
446 |
"if -storetype is {0}"), storetype)); |
|
447 |
usage(); |
|
448 |
} |
|
449 |
||
450 |
if (protectedPath) { |
|
451 |
if (storepass != null || keypass != null) { |
|
452 |
System.err.println(rb.getString |
|
453 |
("If -protected is specified, " + |
|
454 |
"then -storepass and -keypass must not be specified")); |
|
455 |
usage(); |
|
456 |
} |
|
457 |
} |
|
458 |
if (KeyStoreUtil.isWindowsKeyStore(storetype)) { |
|
459 |
if (storepass != null || keypass != null) { |
|
460 |
System.err.println(rb.getString |
|
461 |
("If keystore is not password protected, " + |
|
462 |
"then -storepass and -keypass must not be specified")); |
|
463 |
usage(); |
|
464 |
} |
|
465 |
} |
|
466 |
} |
|
467 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
468 |
void usageNoArg() { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
469 |
System.out.println(rb.getString("Option lacks argument")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
470 |
usage(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
471 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
472 |
|
2 | 473 |
void usage() { |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
474 |
System.out.println(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
475 |
System.out.println(rb.getString("Please type jarsigner -help for usage")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
476 |
System.exit(1); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
477 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
478 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
479 |
void fullusage() { |
2 | 480 |
System.out.println(rb.getString |
481 |
("Usage: jarsigner [options] jar-file alias")); |
|
482 |
System.out.println(rb.getString |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
483 |
(" jarsigner -verify [options] jar-file [alias...]")); |
2 | 484 |
System.out.println(); |
485 |
System.out.println(rb.getString |
|
486 |
("[-keystore <url>] keystore location")); |
|
487 |
System.out.println(); |
|
488 |
System.out.println(rb.getString |
|
489 |
("[-storepass <password>] password for keystore integrity")); |
|
490 |
System.out.println(); |
|
491 |
System.out.println(rb.getString |
|
492 |
("[-storetype <type>] keystore type")); |
|
493 |
System.out.println(); |
|
494 |
System.out.println(rb.getString |
|
495 |
("[-keypass <password>] password for private key (if different)")); |
|
496 |
System.out.println(); |
|
497 |
System.out.println(rb.getString |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
498 |
("[-certchain <file>] name of alternative certchain file")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
499 |
System.out.println(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
500 |
System.out.println(rb.getString |
2 | 501 |
("[-sigfile <file>] name of .SF/.DSA file")); |
502 |
System.out.println(); |
|
503 |
System.out.println(rb.getString |
|
504 |
("[-signedjar <file>] name of signed JAR file")); |
|
505 |
System.out.println(); |
|
506 |
System.out.println(rb.getString |
|
507 |
("[-digestalg <algorithm>] name of digest algorithm")); |
|
508 |
System.out.println(); |
|
509 |
System.out.println(rb.getString |
|
510 |
("[-sigalg <algorithm>] name of signature algorithm")); |
|
511 |
System.out.println(); |
|
512 |
System.out.println(rb.getString |
|
513 |
("[-verify] verify a signed JAR file")); |
|
514 |
System.out.println(); |
|
515 |
System.out.println(rb.getString |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
516 |
("[-verbose[:suboptions]] verbose output when signing/verifying.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
517 |
System.out.println(rb.getString |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
518 |
(" suboptions can be all, grouped or summary")); |
2 | 519 |
System.out.println(); |
520 |
System.out.println(rb.getString |
|
521 |
("[-certs] display certificates when verbose and verifying")); |
|
522 |
System.out.println(); |
|
523 |
System.out.println(rb.getString |
|
524 |
("[-tsa <url>] location of the Timestamping Authority")); |
|
525 |
System.out.println(); |
|
526 |
System.out.println(rb.getString |
|
527 |
("[-tsacert <alias>] public key certificate for Timestamping Authority")); |
|
528 |
System.out.println(); |
|
529 |
System.out.println(rb.getString |
|
530 |
("[-altsigner <class>] class name of an alternative signing mechanism")); |
|
531 |
System.out.println(); |
|
532 |
System.out.println(rb.getString |
|
533 |
("[-altsignerpath <pathlist>] location of an alternative signing mechanism")); |
|
534 |
System.out.println(); |
|
535 |
System.out.println(rb.getString |
|
536 |
("[-internalsf] include the .SF file inside the signature block")); |
|
537 |
System.out.println(); |
|
538 |
System.out.println(rb.getString |
|
539 |
("[-sectionsonly] don't compute hash of entire manifest")); |
|
540 |
System.out.println(); |
|
541 |
System.out.println(rb.getString |
|
542 |
("[-protected] keystore has protected authentication path")); |
|
543 |
System.out.println(); |
|
544 |
System.out.println(rb.getString |
|
545 |
("[-providerName <name>] provider name")); |
|
546 |
System.out.println(); |
|
547 |
System.out.println(rb.getString |
|
548 |
("[-providerClass <class> name of cryptographic service provider's")); |
|
549 |
System.out.println(rb.getString |
|
550 |
(" [-providerArg <arg>]] ... master class file and constructor argument")); |
|
551 |
System.out.println(); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
552 |
System.out.println(rb.getString |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
553 |
("[-strict] treat warnings as errors")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
554 |
System.out.println(); |
2 | 555 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
556 |
System.exit(0); |
2 | 557 |
} |
558 |
||
559 |
void verifyJar(String jarName) |
|
560 |
throws Exception |
|
561 |
{ |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
562 |
boolean anySigned = false; // if there exists entry inside jar signed |
2 | 563 |
JarFile jf = null; |
564 |
||
565 |
try { |
|
566 |
jf = new JarFile(jarName, true); |
|
567 |
Vector<JarEntry> entriesVec = new Vector<JarEntry>(); |
|
568 |
byte[] buffer = new byte[8192]; |
|
569 |
||
570 |
Enumeration<JarEntry> entries = jf.entries(); |
|
571 |
while (entries.hasMoreElements()) { |
|
572 |
JarEntry je = entries.nextElement(); |
|
573 |
entriesVec.addElement(je); |
|
574 |
InputStream is = null; |
|
575 |
try { |
|
576 |
is = jf.getInputStream(je); |
|
577 |
int n; |
|
578 |
while ((n = is.read(buffer, 0, buffer.length)) != -1) { |
|
579 |
// we just read. this will throw a SecurityException |
|
580 |
// if a signature/digest check fails. |
|
581 |
} |
|
582 |
} finally { |
|
583 |
if (is != null) { |
|
584 |
is.close(); |
|
585 |
} |
|
586 |
} |
|
587 |
} |
|
588 |
||
589 |
Manifest man = jf.getManifest(); |
|
590 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
591 |
// The map to record display info, only used when -verbose provided |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
592 |
// key: signer info string |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
593 |
// value: the list of files with common key |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
594 |
Map<String,List<String>> output = |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
595 |
new LinkedHashMap<String,List<String>>(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
596 |
|
2 | 597 |
if (man != null) { |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
598 |
if (verbose != null) System.out.println(); |
2 | 599 |
Enumeration<JarEntry> e = entriesVec.elements(); |
600 |
||
601 |
long now = System.currentTimeMillis(); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
602 |
String tab = rb.getString(" "); |
2 | 603 |
|
604 |
while (e.hasMoreElements()) { |
|
605 |
JarEntry je = e.nextElement(); |
|
606 |
String name = je.getName(); |
|
607 |
CodeSigner[] signers = je.getCodeSigners(); |
|
608 |
boolean isSigned = (signers != null); |
|
609 |
anySigned |= isSigned; |
|
610 |
hasUnsignedEntry |= !je.isDirectory() && !isSigned |
|
611 |
&& !signatureRelated(name); |
|
612 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
613 |
int inStoreOrScope = inKeyStore(signers); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
614 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
615 |
boolean inStore = (inStoreOrScope & IN_KEYSTORE) != 0; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
616 |
boolean inScope = (inStoreOrScope & IN_SCOPE) != 0; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
617 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
618 |
notSignedByAlias |= (inStoreOrScope & NOT_ALIAS) != 0; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
619 |
aliasNotInStore |= isSigned && (!inStore && !inScope); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
620 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
621 |
// Only used when -verbose provided |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
622 |
StringBuffer sb = null; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
623 |
if (verbose != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
624 |
sb = new StringBuffer(); |
2 | 625 |
boolean inManifest = |
626 |
((man.getAttributes(name) != null) || |
|
627 |
(man.getAttributes("./"+name) != null) || |
|
628 |
(man.getAttributes("/"+name) != null)); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
629 |
sb.append( |
2 | 630 |
(isSigned ? rb.getString("s") : rb.getString(" ")) + |
631 |
(inManifest ? rb.getString("m") : rb.getString(" ")) + |
|
632 |
(inStore ? rb.getString("k") : rb.getString(" ")) + |
|
633 |
(inScope ? rb.getString("i") : rb.getString(" ")) + |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
634 |
((inStoreOrScope & NOT_ALIAS) != 0 ?"X":" ") + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
635 |
rb.getString(" ")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
636 |
sb.append("|"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
637 |
} |
2 | 638 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
639 |
// When -certs provided, display info has extra empty |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
640 |
// lines at the beginning and end. |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
641 |
if (isSigned) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
642 |
if (showcerts) sb.append('\n'); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
643 |
for (CodeSigner signer: signers) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
644 |
// signerInfo() must be called even if -verbose |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
645 |
// not provided. The method updates various |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
646 |
// warning flags. |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
647 |
String si = signerInfo(signer, tab, now); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
648 |
if (showcerts) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
649 |
sb.append(si); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
650 |
sb.append('\n'); |
2 | 651 |
} |
652 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
653 |
} else if (showcerts && !verbose.equals("all")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
654 |
// Print no info for unsigned entries when -verbose:all, |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
655 |
// to be consistent with old behavior. |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
656 |
if (signatureRelated(name)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
657 |
sb.append("\n" + tab + rb.getString( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
658 |
"(Signature related entries)") + "\n\n"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
659 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
660 |
sb.append("\n" + tab + rb.getString( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
661 |
"(Unsigned entries)") + "\n\n"); |
2 | 662 |
} |
663 |
} |
|
664 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
665 |
if (verbose != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
666 |
String label = sb.toString(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
667 |
if (signatureRelated(name)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
668 |
// Entries inside META-INF and other unsigned |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
669 |
// entries are grouped separately. |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
670 |
label = "-" + label.substring(1); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
671 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
672 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
673 |
// The label finally contains 2 parts separated by '|': |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
674 |
// The legend displayed before the entry names, and |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
675 |
// the cert info (if -certs specfied). |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
676 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
677 |
if (!output.containsKey(label)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
678 |
output.put(label, new ArrayList<String>()); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
679 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
680 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
681 |
StringBuffer fb = new StringBuffer(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
682 |
String s = Long.toString(je.getSize()); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
683 |
for (int i = 6 - s.length(); i > 0; --i) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
684 |
fb.append(' '); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
685 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
686 |
fb.append(s).append(' '). |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
687 |
append(new Date(je.getTime()).toString()); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
688 |
fb.append(' ').append(name); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
689 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
690 |
output.get(label).add(fb.toString()); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
691 |
} |
2 | 692 |
} |
693 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
694 |
if (verbose != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
695 |
for (Entry<String,List<String>> s: output.entrySet()) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
696 |
List<String> files = s.getValue(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
697 |
String key = s.getKey(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
698 |
if (key.charAt(0) == '-') { // the signature-related group |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
699 |
key = ' ' + key.substring(1); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
700 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
701 |
int pipe = key.indexOf('|'); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
702 |
if (verbose.equals("all")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
703 |
for (String f: files) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
704 |
System.out.println(key.substring(0, pipe) + f); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
705 |
System.out.printf(key.substring(pipe+1)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
706 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
707 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
708 |
if (verbose.equals("grouped")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
709 |
for (String f: files) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
710 |
System.out.println(key.substring(0, pipe) + f); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
711 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
712 |
} else if (verbose.equals("summary")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
713 |
System.out.print(key.substring(0, pipe)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
714 |
if (files.size() > 1) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
715 |
System.out.println(files.get(0) + " " + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
716 |
String.format(rb.getString( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
717 |
"(and %d more)"), files.size()-1)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
718 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
719 |
System.out.println(files.get(0)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
720 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
721 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
722 |
System.out.printf(key.substring(pipe+1)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
723 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
724 |
} |
2 | 725 |
System.out.println(); |
726 |
System.out.println(rb.getString( |
|
727 |
" s = signature was verified ")); |
|
728 |
System.out.println(rb.getString( |
|
729 |
" m = entry is listed in manifest")); |
|
730 |
System.out.println(rb.getString( |
|
731 |
" k = at least one certificate was found in keystore")); |
|
732 |
System.out.println(rb.getString( |
|
733 |
" i = at least one certificate was found in identity scope")); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
734 |
if (ckaliases.size() > 0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
735 |
System.out.println(( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
736 |
" X = not signed by specified alias(es)")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
737 |
} |
2 | 738 |
System.out.println(); |
739 |
} |
|
740 |
if (man == null) |
|
741 |
System.out.println(rb.getString("no manifest.")); |
|
742 |
||
743 |
if (!anySigned) { |
|
744 |
System.out.println(rb.getString( |
|
745 |
"jar is unsigned. (signatures missing or not parsable)")); |
|
746 |
} else { |
|
747 |
System.out.println(rb.getString("jar verified.")); |
|
748 |
if (hasUnsignedEntry || hasExpiredCert || hasExpiringCert || |
|
749 |
badKeyUsage || badExtendedKeyUsage || badNetscapeCertType || |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
750 |
notYetValidCert || chainNotValidated || |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
751 |
aliasNotInStore || notSignedByAlias) { |
2 | 752 |
|
753 |
System.out.println(); |
|
754 |
System.out.println(rb.getString("Warning: ")); |
|
755 |
if (badKeyUsage) { |
|
756 |
System.out.println( |
|
757 |
rb.getString("This jar contains entries whose signer certificate's KeyUsage extension doesn't allow code signing.")); |
|
758 |
} |
|
759 |
||
760 |
if (badExtendedKeyUsage) { |
|
761 |
System.out.println( |
|
762 |
rb.getString("This jar contains entries whose signer certificate's ExtendedKeyUsage extension doesn't allow code signing.")); |
|
763 |
} |
|
764 |
||
765 |
if (badNetscapeCertType) { |
|
766 |
System.out.println( |
|
767 |
rb.getString("This jar contains entries whose signer certificate's NetscapeCertType extension doesn't allow code signing.")); |
|
768 |
} |
|
769 |
||
770 |
if (hasUnsignedEntry) { |
|
771 |
System.out.println(rb.getString( |
|
772 |
"This jar contains unsigned entries which have not been integrity-checked. ")); |
|
773 |
} |
|
774 |
if (hasExpiredCert) { |
|
775 |
System.out.println(rb.getString( |
|
776 |
"This jar contains entries whose signer certificate has expired. ")); |
|
777 |
} |
|
778 |
if (hasExpiringCert) { |
|
779 |
System.out.println(rb.getString( |
|
780 |
"This jar contains entries whose signer certificate will expire within six months. ")); |
|
781 |
} |
|
782 |
if (notYetValidCert) { |
|
783 |
System.out.println(rb.getString( |
|
784 |
"This jar contains entries whose signer certificate is not yet valid. ")); |
|
785 |
} |
|
786 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
787 |
if (chainNotValidated) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
788 |
System.out.println( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
789 |
rb.getString("This jar contains entries whose certificate chain is not validated.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
790 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
791 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
792 |
if (notSignedByAlias) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
793 |
System.out.println( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
794 |
rb.getString("This jar contains signed entries which is not signed by the specified alias(es).")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
795 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
796 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
797 |
if (aliasNotInStore) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
798 |
System.out.println(rb.getString("This jar contains signed entries that's not signed by alias in this keystore.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
799 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
800 |
if (! (verbose != null && showcerts)) { |
2 | 801 |
System.out.println(); |
802 |
System.out.println(rb.getString( |
|
803 |
"Re-run with the -verbose and -certs options for more details.")); |
|
804 |
} |
|
805 |
} |
|
806 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
807 |
return; |
2 | 808 |
} catch (Exception e) { |
809 |
System.out.println(rb.getString("jarsigner: ") + e); |
|
810 |
if (debug) { |
|
811 |
e.printStackTrace(); |
|
812 |
} |
|
813 |
} finally { // close the resource |
|
814 |
if (jf != null) { |
|
815 |
jf.close(); |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
System.exit(1); |
|
820 |
} |
|
821 |
||
822 |
private static MessageFormat validityTimeForm = null; |
|
823 |
private static MessageFormat notYetTimeForm = null; |
|
824 |
private static MessageFormat expiredTimeForm = null; |
|
825 |
private static MessageFormat expiringTimeForm = null; |
|
826 |
||
827 |
/* |
|
828 |
* Display some details about a certificate: |
|
829 |
* |
|
830 |
* [<tab>] <cert-type> [", " <subject-DN>] [" (" <keystore-entry-alias> ")"] |
|
831 |
* [<validity-period> | <expiry-warning>] |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
832 |
* |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
833 |
* Note: no newline character at the end |
2 | 834 |
*/ |
835 |
String printCert(String tab, Certificate c, boolean checkValidityPeriod, |
|
836 |
long now) { |
|
837 |
||
838 |
StringBuilder certStr = new StringBuilder(); |
|
839 |
String space = rb.getString(" "); |
|
840 |
X509Certificate x509Cert = null; |
|
841 |
||
842 |
if (c instanceof X509Certificate) { |
|
843 |
x509Cert = (X509Certificate) c; |
|
844 |
certStr.append(tab).append(x509Cert.getType()) |
|
845 |
.append(rb.getString(", ")) |
|
846 |
.append(x509Cert.getSubjectDN().getName()); |
|
847 |
} else { |
|
848 |
certStr.append(tab).append(c.getType()); |
|
849 |
} |
|
850 |
||
851 |
String alias = storeHash.get(c); |
|
852 |
if (alias != null) { |
|
853 |
certStr.append(space).append(alias); |
|
854 |
} |
|
855 |
||
856 |
if (checkValidityPeriod && x509Cert != null) { |
|
857 |
||
858 |
certStr.append("\n").append(tab).append("["); |
|
859 |
Date notAfter = x509Cert.getNotAfter(); |
|
860 |
try { |
|
861 |
x509Cert.checkValidity(); |
|
862 |
// test if cert will expire within six months |
|
863 |
if (now == 0) { |
|
864 |
now = System.currentTimeMillis(); |
|
865 |
} |
|
866 |
if (notAfter.getTime() < now + SIX_MONTHS) { |
|
867 |
hasExpiringCert = true; |
|
868 |
||
869 |
if (expiringTimeForm == null) { |
|
870 |
expiringTimeForm = new MessageFormat( |
|
871 |
rb.getString("certificate will expire on")); |
|
872 |
} |
|
873 |
Object[] source = { notAfter }; |
|
874 |
certStr.append(expiringTimeForm.format(source)); |
|
875 |
||
876 |
} else { |
|
877 |
if (validityTimeForm == null) { |
|
878 |
validityTimeForm = new MessageFormat( |
|
879 |
rb.getString("certificate is valid from")); |
|
880 |
} |
|
881 |
Object[] source = { x509Cert.getNotBefore(), notAfter }; |
|
882 |
certStr.append(validityTimeForm.format(source)); |
|
883 |
} |
|
884 |
} catch (CertificateExpiredException cee) { |
|
885 |
hasExpiredCert = true; |
|
886 |
||
887 |
if (expiredTimeForm == null) { |
|
888 |
expiredTimeForm = new MessageFormat( |
|
889 |
rb.getString("certificate expired on")); |
|
890 |
} |
|
891 |
Object[] source = { notAfter }; |
|
892 |
certStr.append(expiredTimeForm.format(source)); |
|
893 |
||
894 |
} catch (CertificateNotYetValidException cnyve) { |
|
895 |
notYetValidCert = true; |
|
896 |
||
897 |
if (notYetTimeForm == null) { |
|
898 |
notYetTimeForm = new MessageFormat( |
|
899 |
rb.getString("certificate is not valid until")); |
|
900 |
} |
|
901 |
Object[] source = { x509Cert.getNotBefore() }; |
|
902 |
certStr.append(notYetTimeForm.format(source)); |
|
903 |
} |
|
904 |
certStr.append("]"); |
|
905 |
||
906 |
boolean[] bad = new boolean[3]; |
|
907 |
checkCertUsage(x509Cert, bad); |
|
908 |
if (bad[0] || bad[1] || bad[2]) { |
|
909 |
String x = ""; |
|
910 |
if (bad[0]) { |
|
911 |
x ="KeyUsage"; |
|
912 |
} |
|
913 |
if (bad[1]) { |
|
914 |
if (x.length() > 0) x = x + ", "; |
|
915 |
x = x + "ExtendedKeyUsage"; |
|
916 |
} |
|
917 |
if (bad[2]) { |
|
918 |
if (x.length() > 0) x = x + ", "; |
|
919 |
x = x + "NetscapeCertType"; |
|
920 |
} |
|
921 |
certStr.append("\n").append(tab) |
|
922 |
.append(MessageFormat.format(rb.getString( |
|
923 |
"[{0} extension does not support code signing]"), x)); |
|
924 |
} |
|
925 |
} |
|
926 |
return certStr.toString(); |
|
927 |
} |
|
928 |
||
929 |
private static MessageFormat signTimeForm = null; |
|
930 |
||
931 |
private String printTimestamp(String tab, Timestamp timestamp) { |
|
932 |
||
933 |
if (signTimeForm == null) { |
|
934 |
signTimeForm = |
|
935 |
new MessageFormat(rb.getString("entry was signed on")); |
|
936 |
} |
|
937 |
Object[] source = { timestamp.getTimestamp() }; |
|
938 |
||
939 |
return new StringBuilder().append(tab).append("[") |
|
940 |
.append(signTimeForm.format(source)).append("]").toString(); |
|
941 |
} |
|
942 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
943 |
private Map<CodeSigner,Integer> cacheForInKS = |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
944 |
new IdentityHashMap<CodeSigner,Integer>(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
945 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
946 |
private int inKeyStoreForOneSigner(CodeSigner signer) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
947 |
if (cacheForInKS.containsKey(signer)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
948 |
return cacheForInKS.get(signer); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
949 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
950 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
951 |
boolean found = false; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
952 |
int result = 0; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
953 |
List<? extends Certificate> certs = signer.getSignerCertPath().getCertificates(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
954 |
for (Certificate c : certs) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
955 |
String alias = storeHash.get(c); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
956 |
if (alias != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
957 |
if (alias.startsWith("(")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
958 |
result |= IN_KEYSTORE; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
959 |
} else if (alias.startsWith("[")) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
960 |
result |= IN_SCOPE; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
961 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
962 |
if (ckaliases.contains(alias.substring(1, alias.length() - 1))) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
963 |
result |= SIGNED_BY_ALIAS; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
964 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
965 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
966 |
if (store != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
967 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
968 |
alias = store.getCertificateAlias(c); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
969 |
} catch (KeyStoreException kse) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
970 |
// never happens, because keystore has been loaded |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
971 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
972 |
if (alias != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
973 |
storeHash.put(c, "(" + alias + ")"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
974 |
found = true; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
975 |
result |= IN_KEYSTORE; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
976 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
977 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
978 |
if (!found && (scope != null)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
979 |
Identity id = scope.getIdentity(c.getPublicKey()); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
980 |
if (id != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
981 |
result |= IN_SCOPE; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
982 |
storeHash.put(c, "[" + id.getName() + "]"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
983 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
984 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
985 |
if (ckaliases.contains(alias)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
986 |
result |= SIGNED_BY_ALIAS; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
987 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
988 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
989 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
990 |
cacheForInKS.put(signer, result); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
991 |
return result; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
992 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
993 |
|
2 | 994 |
Hashtable<Certificate, String> storeHash = |
995 |
new Hashtable<Certificate, String>(); |
|
996 |
||
997 |
int inKeyStore(CodeSigner[] signers) { |
|
998 |
||
999 |
if (signers == null) |
|
1000 |
return 0; |
|
1001 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1002 |
int output = 0; |
2 | 1003 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1004 |
for (CodeSigner signer: signers) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1005 |
int result = inKeyStoreForOneSigner(signer); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1006 |
output |= result; |
2 | 1007 |
} |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1008 |
if (ckaliases.size() > 0 && (output & SIGNED_BY_ALIAS) == 0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1009 |
output |= NOT_ALIAS; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1010 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1011 |
return output; |
2 | 1012 |
} |
1013 |
||
1014 |
void signJar(String jarName, String alias, String[] args) |
|
1015 |
throws Exception { |
|
1016 |
boolean aliasUsed = false; |
|
1017 |
X509Certificate tsaCert = null; |
|
1018 |
||
1019 |
if (sigfile == null) { |
|
1020 |
sigfile = alias; |
|
1021 |
aliasUsed = true; |
|
1022 |
} |
|
1023 |
||
1024 |
if (sigfile.length() > 8) { |
|
1025 |
sigfile = sigfile.substring(0, 8).toUpperCase(); |
|
1026 |
} else { |
|
1027 |
sigfile = sigfile.toUpperCase(); |
|
1028 |
} |
|
1029 |
||
1030 |
StringBuilder tmpSigFile = new StringBuilder(sigfile.length()); |
|
1031 |
for (int j = 0; j < sigfile.length(); j++) { |
|
1032 |
char c = sigfile.charAt(j); |
|
1033 |
if (! |
|
1034 |
((c>= 'A' && c<= 'Z') || |
|
1035 |
(c>= '0' && c<= '9') || |
|
1036 |
(c == '-') || |
|
1037 |
(c == '_'))) { |
|
1038 |
if (aliasUsed) { |
|
1039 |
// convert illegal characters from the alias to be _'s |
|
1040 |
c = '_'; |
|
1041 |
} else { |
|
1042 |
throw new |
|
1043 |
RuntimeException(rb.getString |
|
1044 |
("signature filename must consist of the following characters: A-Z, 0-9, _ or -")); |
|
1045 |
} |
|
1046 |
} |
|
1047 |
tmpSigFile.append(c); |
|
1048 |
} |
|
1049 |
||
1050 |
sigfile = tmpSigFile.toString(); |
|
1051 |
||
1052 |
String tmpJarName; |
|
1053 |
if (signedjar == null) tmpJarName = jarName+".sig"; |
|
1054 |
else tmpJarName = signedjar; |
|
1055 |
||
1056 |
File jarFile = new File(jarName); |
|
1057 |
File signedJarFile = new File(tmpJarName); |
|
1058 |
||
1059 |
// Open the jar (zip) file |
|
1060 |
try { |
|
1061 |
zipFile = new ZipFile(jarName); |
|
1062 |
} catch (IOException ioe) { |
|
1063 |
error(rb.getString("unable to open jar file: ")+jarName, ioe); |
|
1064 |
} |
|
1065 |
||
1066 |
FileOutputStream fos = null; |
|
1067 |
try { |
|
1068 |
fos = new FileOutputStream(signedJarFile); |
|
1069 |
} catch (IOException ioe) { |
|
1070 |
error(rb.getString("unable to create: ")+tmpJarName, ioe); |
|
1071 |
} |
|
1072 |
||
1073 |
PrintStream ps = new PrintStream(fos); |
|
1074 |
ZipOutputStream zos = new ZipOutputStream(ps); |
|
1075 |
||
1076 |
/* First guess at what they might be - we don't xclude RSA ones. */ |
|
1077 |
String sfFilename = (META_INF + sigfile + ".SF").toUpperCase(); |
|
1078 |
String bkFilename = (META_INF + sigfile + ".DSA").toUpperCase(); |
|
1079 |
||
1080 |
Manifest manifest = new Manifest(); |
|
1081 |
Map<String,Attributes> mfEntries = manifest.getEntries(); |
|
1082 |
||
1083 |
// The Attributes of manifest before updating |
|
1084 |
Attributes oldAttr = null; |
|
1085 |
||
1086 |
boolean mfModified = false; |
|
1087 |
boolean mfCreated = false; |
|
1088 |
byte[] mfRawBytes = null; |
|
1089 |
||
1090 |
try { |
|
1091 |
MessageDigest digests[] = { MessageDigest.getInstance(digestalg) }; |
|
1092 |
||
1093 |
// Check if manifest exists |
|
1094 |
ZipEntry mfFile; |
|
1095 |
if ((mfFile = getManifestFile(zipFile)) != null) { |
|
1096 |
// Manifest exists. Read its raw bytes. |
|
1097 |
mfRawBytes = getBytes(zipFile, mfFile); |
|
1098 |
manifest.read(new ByteArrayInputStream(mfRawBytes)); |
|
1099 |
oldAttr = (Attributes)(manifest.getMainAttributes().clone()); |
|
1100 |
} else { |
|
1101 |
// Create new manifest |
|
1102 |
Attributes mattr = manifest.getMainAttributes(); |
|
1103 |
mattr.putValue(Attributes.Name.MANIFEST_VERSION.toString(), |
|
1104 |
"1.0"); |
|
1105 |
String javaVendor = System.getProperty("java.vendor"); |
|
1106 |
String jdkVersion = System.getProperty("java.version"); |
|
1107 |
mattr.putValue("Created-By", jdkVersion + " (" +javaVendor |
|
1108 |
+ ")"); |
|
1109 |
mfFile = new ZipEntry(JarFile.MANIFEST_NAME); |
|
1110 |
mfCreated = true; |
|
1111 |
} |
|
1112 |
||
1113 |
/* |
|
1114 |
* For each entry in jar |
|
1115 |
* (except for signature-related META-INF entries), |
|
1116 |
* do the following: |
|
1117 |
* |
|
1118 |
* - if entry is not contained in manifest, add it to manifest; |
|
1119 |
* - if entry is contained in manifest, calculate its hash and |
|
1120 |
* compare it with the one in the manifest; if they are |
|
1121 |
* different, replace the hash in the manifest with the newly |
|
1122 |
* generated one. (This may invalidate existing signatures!) |
|
1123 |
*/ |
|
1124 |
BASE64Encoder encoder = new JarBASE64Encoder(); |
|
1125 |
Vector<ZipEntry> mfFiles = new Vector<ZipEntry>(); |
|
1126 |
||
1127 |
for (Enumeration<? extends ZipEntry> enum_=zipFile.entries(); |
|
1128 |
enum_.hasMoreElements();) { |
|
1129 |
ZipEntry ze = enum_.nextElement(); |
|
1130 |
||
1131 |
if (ze.getName().startsWith(META_INF)) { |
|
1132 |
// Store META-INF files in vector, so they can be written |
|
1133 |
// out first |
|
1134 |
mfFiles.addElement(ze); |
|
1135 |
||
1136 |
if (signatureRelated(ze.getName())) { |
|
1137 |
// ignore signature-related and manifest files |
|
1138 |
continue; |
|
1139 |
} |
|
1140 |
} |
|
1141 |
||
1142 |
if (manifest.getAttributes(ze.getName()) != null) { |
|
1143 |
// jar entry is contained in manifest, check and |
|
1144 |
// possibly update its digest attributes |
|
1145 |
if (updateDigests(ze, zipFile, digests, encoder, |
|
1146 |
manifest) == true) { |
|
1147 |
mfModified = true; |
|
1148 |
} |
|
1149 |
} else if (!ze.isDirectory()) { |
|
1150 |
// Add entry to manifest |
|
1151 |
Attributes attrs = getDigestAttributes(ze, zipFile, |
|
1152 |
digests, |
|
1153 |
encoder); |
|
1154 |
mfEntries.put(ze.getName(), attrs); |
|
1155 |
mfModified = true; |
|
1156 |
} |
|
1157 |
} |
|
1158 |
||
1159 |
// Recalculate the manifest raw bytes if necessary |
|
1160 |
if (mfModified) { |
|
1161 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
1162 |
manifest.write(baos); |
|
1163 |
byte[] newBytes = baos.toByteArray(); |
|
1164 |
if (mfRawBytes != null |
|
1165 |
&& oldAttr.equals(manifest.getMainAttributes())) { |
|
1166 |
||
1167 |
/* |
|
1168 |
* Note: |
|
1169 |
* |
|
1170 |
* The Attributes object is based on HashMap and can handle |
|
1171 |
* continuation columns. Therefore, even if the contents are |
|
1172 |
* not changed (in a Map view), the bytes that it write() |
|
1173 |
* may be different from the original bytes that it read() |
|
1174 |
* from. Since the signature on the main attributes is based |
|
1175 |
* on raw bytes, we must retain the exact bytes. |
|
1176 |
*/ |
|
1177 |
||
1178 |
int newPos = findHeaderEnd(newBytes); |
|
1179 |
int oldPos = findHeaderEnd(mfRawBytes); |
|
1180 |
||
1181 |
if (newPos == oldPos) { |
|
1182 |
System.arraycopy(mfRawBytes, 0, newBytes, 0, oldPos); |
|
1183 |
} else { |
|
1184 |
// cat oldHead newTail > newBytes |
|
1185 |
byte[] lastBytes = new byte[oldPos + |
|
1186 |
newBytes.length - newPos]; |
|
1187 |
System.arraycopy(mfRawBytes, 0, lastBytes, 0, oldPos); |
|
1188 |
System.arraycopy(newBytes, newPos, lastBytes, oldPos, |
|
1189 |
newBytes.length - newPos); |
|
1190 |
newBytes = lastBytes; |
|
1191 |
} |
|
1192 |
} |
|
1193 |
mfRawBytes = newBytes; |
|
1194 |
} |
|
1195 |
||
1196 |
// Write out the manifest |
|
1197 |
if (mfModified) { |
|
1198 |
// manifest file has new length |
|
1199 |
mfFile = new ZipEntry(JarFile.MANIFEST_NAME); |
|
1200 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1201 |
if (verbose != null) { |
2 | 1202 |
if (mfCreated) { |
1203 |
System.out.println(rb.getString(" adding: ") + |
|
1204 |
mfFile.getName()); |
|
1205 |
} else if (mfModified) { |
|
1206 |
System.out.println(rb.getString(" updating: ") + |
|
1207 |
mfFile.getName()); |
|
1208 |
} |
|
1209 |
} |
|
1210 |
zos.putNextEntry(mfFile); |
|
1211 |
zos.write(mfRawBytes); |
|
1212 |
||
1213 |
// Calculate SignatureFile (".SF") and SignatureBlockFile |
|
1214 |
ManifestDigester manDig = new ManifestDigester(mfRawBytes); |
|
1215 |
SignatureFile sf = new SignatureFile(digests, manifest, manDig, |
|
1216 |
sigfile, signManifest); |
|
1217 |
||
1218 |
if (tsaAlias != null) { |
|
1219 |
tsaCert = getTsaCert(tsaAlias); |
|
1220 |
} |
|
1221 |
||
1222 |
SignatureFile.Block block = null; |
|
1223 |
||
1224 |
try { |
|
1225 |
block = |
|
1226 |
sf.generateBlock(privateKey, sigalg, certChain, |
|
1227 |
externalSF, tsaUrl, tsaCert, signingMechanism, args, |
|
1228 |
zipFile); |
|
1229 |
} catch (SocketTimeoutException e) { |
|
1230 |
// Provide a helpful message when TSA is beyond a firewall |
|
1231 |
error(rb.getString("unable to sign jar: ") + |
|
1232 |
rb.getString("no response from the Timestamping Authority. ") + |
|
1233 |
rb.getString("When connecting from behind a firewall then an HTTP proxy may need to be specified. ") + |
|
1234 |
rb.getString("Supply the following options to jarsigner: ") + |
|
1235 |
"\n -J-Dhttp.proxyHost=<hostname> " + |
|
1236 |
"\n -J-Dhttp.proxyPort=<portnumber> ", e); |
|
1237 |
} |
|
1238 |
||
1239 |
sfFilename = sf.getMetaName(); |
|
1240 |
bkFilename = block.getMetaName(); |
|
1241 |
||
1242 |
ZipEntry sfFile = new ZipEntry(sfFilename); |
|
1243 |
ZipEntry bkFile = new ZipEntry(bkFilename); |
|
1244 |
||
1245 |
long time = System.currentTimeMillis(); |
|
1246 |
sfFile.setTime(time); |
|
1247 |
bkFile.setTime(time); |
|
1248 |
||
1249 |
// signature file |
|
1250 |
zos.putNextEntry(sfFile); |
|
1251 |
sf.write(zos); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1252 |
if (verbose != null) { |
2 | 1253 |
if (zipFile.getEntry(sfFilename) != null) { |
1254 |
System.out.println(rb.getString(" updating: ") + |
|
1255 |
sfFilename); |
|
1256 |
} else { |
|
1257 |
System.out.println(rb.getString(" adding: ") + |
|
1258 |
sfFilename); |
|
1259 |
} |
|
1260 |
} |
|
1261 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1262 |
if (verbose != null) { |
2 | 1263 |
if (tsaUrl != null || tsaCert != null) { |
1264 |
System.out.println( |
|
1265 |
rb.getString("requesting a signature timestamp")); |
|
1266 |
} |
|
1267 |
if (tsaUrl != null) { |
|
1268 |
System.out.println(rb.getString("TSA location: ") + tsaUrl); |
|
1269 |
} |
|
1270 |
if (tsaCert != null) { |
|
1271 |
String certUrl = |
|
1272 |
TimestampedSigner.getTimestampingUrl(tsaCert); |
|
1273 |
if (certUrl != null) { |
|
1274 |
System.out.println(rb.getString("TSA location: ") + |
|
1275 |
certUrl); |
|
1276 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1277 |
System.out.println(rb.getString("TSA certificate: ") + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1278 |
printCert("", tsaCert, false, 0)); |
2 | 1279 |
} |
1280 |
if (signingMechanism != null) { |
|
1281 |
System.out.println( |
|
1282 |
rb.getString("using an alternative signing mechanism")); |
|
1283 |
} |
|
1284 |
} |
|
1285 |
||
1286 |
// signature block file |
|
1287 |
zos.putNextEntry(bkFile); |
|
1288 |
block.write(zos); |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1289 |
if (verbose != null) { |
2 | 1290 |
if (zipFile.getEntry(bkFilename) != null) { |
1291 |
System.out.println(rb.getString(" updating: ") + |
|
1292 |
bkFilename); |
|
1293 |
} else { |
|
1294 |
System.out.println(rb.getString(" adding: ") + |
|
1295 |
bkFilename); |
|
1296 |
} |
|
1297 |
} |
|
1298 |
||
1299 |
// Write out all other META-INF files that we stored in the |
|
1300 |
// vector |
|
1301 |
for (int i=0; i<mfFiles.size(); i++) { |
|
1302 |
ZipEntry ze = mfFiles.elementAt(i); |
|
1303 |
if (!ze.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME) |
|
1304 |
&& !ze.getName().equalsIgnoreCase(sfFilename) |
|
1305 |
&& !ze.getName().equalsIgnoreCase(bkFilename)) { |
|
1306 |
writeEntry(zipFile, zos, ze); |
|
1307 |
} |
|
1308 |
} |
|
1309 |
||
1310 |
// Write out all other files |
|
1311 |
for (Enumeration<? extends ZipEntry> enum_=zipFile.entries(); |
|
1312 |
enum_.hasMoreElements();) { |
|
1313 |
ZipEntry ze = enum_.nextElement(); |
|
1314 |
||
1315 |
if (!ze.getName().startsWith(META_INF)) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1316 |
if (verbose != null) { |
2 | 1317 |
if (manifest.getAttributes(ze.getName()) != null) |
1318 |
System.out.println(rb.getString(" signing: ") + |
|
1319 |
ze.getName()); |
|
1320 |
else |
|
1321 |
System.out.println(rb.getString(" adding: ") + |
|
1322 |
ze.getName()); |
|
1323 |
} |
|
1324 |
writeEntry(zipFile, zos, ze); |
|
1325 |
} |
|
1326 |
} |
|
1327 |
} catch(IOException ioe) { |
|
1328 |
error(rb.getString("unable to sign jar: ")+ioe, ioe); |
|
1329 |
} finally { |
|
1330 |
// close the resouces |
|
1331 |
if (zipFile != null) { |
|
1332 |
zipFile.close(); |
|
1333 |
zipFile = null; |
|
1334 |
} |
|
1335 |
||
1336 |
if (zos != null) { |
|
1337 |
zos.close(); |
|
1338 |
} |
|
1339 |
} |
|
1340 |
||
1341 |
// no IOException thrown in the follow try clause, so disable |
|
1342 |
// the try clause. |
|
1343 |
// try { |
|
1344 |
if (signedjar == null) { |
|
1345 |
// attempt an atomic rename. If that fails, |
|
1346 |
// rename the original jar file, then the signed |
|
1347 |
// one, then delete the original. |
|
1348 |
if (!signedJarFile.renameTo(jarFile)) { |
|
1349 |
File origJar = new File(jarName+".orig"); |
|
1350 |
||
1351 |
if (jarFile.renameTo(origJar)) { |
|
1352 |
if (signedJarFile.renameTo(jarFile)) { |
|
1353 |
origJar.delete(); |
|
1354 |
} else { |
|
1355 |
MessageFormat form = new MessageFormat(rb.getString |
|
1356 |
("attempt to rename signedJarFile to jarFile failed")); |
|
1357 |
Object[] source = {signedJarFile, jarFile}; |
|
1358 |
error(form.format(source)); |
|
1359 |
} |
|
1360 |
} else { |
|
1361 |
MessageFormat form = new MessageFormat(rb.getString |
|
1362 |
("attempt to rename jarFile to origJar failed")); |
|
1363 |
Object[] source = {jarFile, origJar}; |
|
1364 |
error(form.format(source)); |
|
1365 |
} |
|
1366 |
} |
|
1367 |
} |
|
1368 |
||
1369 |
if (hasExpiredCert || hasExpiringCert || notYetValidCert |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1370 |
|| badKeyUsage || badExtendedKeyUsage |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1371 |
|| badNetscapeCertType || chainNotValidated) { |
2 | 1372 |
System.out.println(); |
1373 |
||
1374 |
System.out.println(rb.getString("Warning: ")); |
|
1375 |
if (badKeyUsage) { |
|
1376 |
System.out.println( |
|
1377 |
rb.getString("The signer certificate's KeyUsage extension doesn't allow code signing.")); |
|
1378 |
} |
|
1379 |
||
1380 |
if (badExtendedKeyUsage) { |
|
1381 |
System.out.println( |
|
1382 |
rb.getString("The signer certificate's ExtendedKeyUsage extension doesn't allow code signing.")); |
|
1383 |
} |
|
1384 |
||
1385 |
if (badNetscapeCertType) { |
|
1386 |
System.out.println( |
|
1387 |
rb.getString("The signer certificate's NetscapeCertType extension doesn't allow code signing.")); |
|
1388 |
} |
|
1389 |
||
1390 |
if (hasExpiredCert) { |
|
1391 |
System.out.println( |
|
1392 |
rb.getString("The signer certificate has expired.")); |
|
1393 |
} else if (hasExpiringCert) { |
|
1394 |
System.out.println( |
|
1395 |
rb.getString("The signer certificate will expire within six months.")); |
|
1396 |
} else if (notYetValidCert) { |
|
1397 |
System.out.println( |
|
1398 |
rb.getString("The signer certificate is not yet valid.")); |
|
1399 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1400 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1401 |
if (chainNotValidated) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1402 |
System.out.println( |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1403 |
rb.getString("The signer's certificate chain is not validated.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1404 |
} |
2 | 1405 |
} |
1406 |
||
1407 |
// no IOException thrown in the above try clause, so disable |
|
1408 |
// the catch clause. |
|
1409 |
// } catch(IOException ioe) { |
|
1410 |
// error(rb.getString("unable to sign jar: ")+ioe, ioe); |
|
1411 |
// } |
|
1412 |
} |
|
1413 |
||
1414 |
/** |
|
3047
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1415 |
* Find the position of an empty line inside bs |
2 | 1416 |
*/ |
1417 |
private int findHeaderEnd(byte[] bs) { |
|
3047
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1418 |
// An empty line can be at the beginning... |
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1419 |
if (bs.length > 1 && bs[0] == '\r' && bs[1] == '\n') { |
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1420 |
return 0; |
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1421 |
} |
30e88a671e72
6712755: jarsigner fails to sign itextasian.jar since 1.5.0_b14, it works with 1.5.0_13
weijun
parents:
2432
diff
changeset
|
1422 |
// ... or after another line |
2 | 1423 |
for (int i=0; i<bs.length-3; i++) { |
1424 |
if (bs[i] == '\r' && bs[i+1] == '\n' && |
|
1425 |
bs[i+2] == '\r' && bs[i+3] == '\n') { |
|
1426 |
return i; |
|
1427 |
} |
|
1428 |
} |
|
1429 |
// If header end is not found, return 0, |
|
1430 |
// which means no behavior change. |
|
1431 |
return 0; |
|
1432 |
} |
|
1433 |
||
1434 |
/** |
|
1435 |
* signature-related files include: |
|
1436 |
* . META-INF/MANIFEST.MF |
|
1437 |
* . META-INF/SIG-* |
|
1438 |
* . META-INF/*.SF |
|
1439 |
* . META-INF/*.DSA |
|
1440 |
* . META-INF/*.RSA |
|
1441 |
*/ |
|
1442 |
private boolean signatureRelated(String name) { |
|
1443 |
String ucName = name.toUpperCase(); |
|
1444 |
if (ucName.equals(JarFile.MANIFEST_NAME) || |
|
1445 |
ucName.equals(META_INF) || |
|
1446 |
(ucName.startsWith(SIG_PREFIX) && |
|
1447 |
ucName.indexOf("/") == ucName.lastIndexOf("/"))) { |
|
1448 |
return true; |
|
1449 |
} |
|
1450 |
||
1451 |
if (ucName.startsWith(META_INF) && |
|
1452 |
SignatureFileVerifier.isBlockOrSF(ucName)) { |
|
1453 |
// .SF/.DSA/.RSA files in META-INF subdirs |
|
1454 |
// are not considered signature-related |
|
1455 |
return (ucName.indexOf("/") == ucName.lastIndexOf("/")); |
|
1456 |
} |
|
1457 |
||
1458 |
return false; |
|
1459 |
} |
|
1460 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1461 |
Map<CodeSigner,String> cacheForSignerInfo = new IdentityHashMap<CodeSigner,String>(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1462 |
|
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1463 |
/** |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1464 |
* Returns a string of singer info, with a newline at the end |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1465 |
*/ |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1466 |
private String signerInfo(CodeSigner signer, String tab, long now) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1467 |
if (cacheForSignerInfo.containsKey(signer)) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1468 |
return cacheForSignerInfo.get(signer); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1469 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1470 |
StringBuffer s = new StringBuffer(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1471 |
List<? extends Certificate> certs = signer.getSignerCertPath().getCertificates(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1472 |
// display the signature timestamp, if present |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1473 |
Timestamp timestamp = signer.getTimestamp(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1474 |
if (timestamp != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1475 |
s.append(printTimestamp(tab, timestamp)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1476 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1477 |
// display the certificate(s) |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1478 |
for (Certificate c : certs) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1479 |
s.append(printCert(tab, c, true, now)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1480 |
s.append('\n'); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1481 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1482 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1483 |
CertPath cp = certificateFactory.generateCertPath(certs); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1484 |
validator.validate(cp, pkixParameters); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1485 |
} catch (Exception e) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1486 |
chainNotValidated = true; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1487 |
s.append(tab + rb.getString("[CertPath not validated: ") + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1488 |
e.getLocalizedMessage() + "]\n"); // TODO |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1489 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1490 |
String result = s.toString(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1491 |
cacheForSignerInfo.put(signer, result); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1492 |
return result; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1493 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1494 |
|
2 | 1495 |
private void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze) |
1496 |
throws IOException |
|
1497 |
{ |
|
1498 |
ZipEntry ze2 = new ZipEntry(ze.getName()); |
|
1499 |
ze2.setMethod(ze.getMethod()); |
|
1500 |
ze2.setTime(ze.getTime()); |
|
1501 |
ze2.setComment(ze.getComment()); |
|
1502 |
ze2.setExtra(ze.getExtra()); |
|
1503 |
if (ze.getMethod() == ZipEntry.STORED) { |
|
1504 |
ze2.setSize(ze.getSize()); |
|
1505 |
ze2.setCrc(ze.getCrc()); |
|
1506 |
} |
|
1507 |
os.putNextEntry(ze2); |
|
1508 |
writeBytes(zf, ze, os); |
|
1509 |
} |
|
1510 |
||
1511 |
/** |
|
1512 |
* Writes all the bytes for a given entry to the specified output stream. |
|
1513 |
*/ |
|
1514 |
private synchronized void writeBytes |
|
1515 |
(ZipFile zf, ZipEntry ze, ZipOutputStream os) throws IOException { |
|
1516 |
int n; |
|
1517 |
||
1518 |
InputStream is = null; |
|
1519 |
try { |
|
1520 |
is = zf.getInputStream(ze); |
|
1521 |
long left = ze.getSize(); |
|
1522 |
||
1523 |
while((left > 0) && (n = is.read(buffer, 0, buffer.length)) != -1) { |
|
1524 |
os.write(buffer, 0, n); |
|
1525 |
left -= n; |
|
1526 |
} |
|
1527 |
} finally { |
|
1528 |
if (is != null) { |
|
1529 |
is.close(); |
|
1530 |
} |
|
1531 |
} |
|
1532 |
} |
|
1533 |
||
1534 |
void loadKeyStore(String keyStoreName, boolean prompt) { |
|
1535 |
||
1536 |
if (!nullStream && keyStoreName == null) { |
|
1537 |
keyStoreName = System.getProperty("user.home") + File.separator |
|
1538 |
+ ".keystore"; |
|
1539 |
} |
|
1540 |
||
1541 |
try { |
|
1542 |
if (providerName == null) { |
|
1543 |
store = KeyStore.getInstance(storetype); |
|
1544 |
} else { |
|
1545 |
store = KeyStore.getInstance(storetype, providerName); |
|
1546 |
} |
|
1547 |
||
1548 |
// Get pass phrase |
|
1549 |
// XXX need to disable echo; on UNIX, call getpass(char *prompt)Z |
|
1550 |
// and on NT call ?? |
|
1551 |
if (token && storepass == null && !protectedPath |
|
1552 |
&& !KeyStoreUtil.isWindowsKeyStore(storetype)) { |
|
1553 |
storepass = getPass |
|
1554 |
(rb.getString("Enter Passphrase for keystore: ")); |
|
1555 |
} else if (!token && storepass == null && prompt) { |
|
1556 |
storepass = getPass |
|
1557 |
(rb.getString("Enter Passphrase for keystore: ")); |
|
1558 |
} |
|
1559 |
||
1560 |
if (nullStream) { |
|
1561 |
store.load(null, storepass); |
|
1562 |
} else { |
|
1563 |
keyStoreName = keyStoreName.replace(File.separatorChar, '/'); |
|
1564 |
URL url = null; |
|
1565 |
try { |
|
1566 |
url = new URL(keyStoreName); |
|
1567 |
} catch (java.net.MalformedURLException e) { |
|
1568 |
// try as file |
|
1569 |
url = new File(keyStoreName).toURI().toURL(); |
|
1570 |
} |
|
1571 |
InputStream is = null; |
|
1572 |
try { |
|
1573 |
is = url.openStream(); |
|
1574 |
store.load(is, storepass); |
|
1575 |
} finally { |
|
1576 |
if (is != null) { |
|
1577 |
is.close(); |
|
1578 |
} |
|
1579 |
} |
|
1580 |
} |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1581 |
Set<TrustAnchor> tas = new HashSet<TrustAnchor>(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1582 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1583 |
KeyStore caks = KeyTool.getCacertsKeyStore(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1584 |
if (caks != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1585 |
Enumeration<String> aliases = caks.aliases(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1586 |
while (aliases.hasMoreElements()) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1587 |
String a = aliases.nextElement(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1588 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1589 |
tas.add(new TrustAnchor((X509Certificate)caks.getCertificate(a), null)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1590 |
} catch (Exception e2) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1591 |
// ignore, when a SecretkeyEntry does not include a cert |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1592 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1593 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1594 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1595 |
} catch (Exception e) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1596 |
// Ignore, if cacerts cannot be loaded |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1597 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1598 |
if (store != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1599 |
Enumeration<String> aliases = store.aliases(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1600 |
while (aliases.hasMoreElements()) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1601 |
String a = aliases.nextElement(); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1602 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1603 |
X509Certificate c = (X509Certificate)store.getCertificate(a); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1604 |
// Only add TrustedCertificateEntry and self-signed |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1605 |
// PrivateKeyEntry |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1606 |
if (store.isCertificateEntry(a) || |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1607 |
c.getSubjectDN().equals(c.getIssuerDN())) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1608 |
tas.add(new TrustAnchor(c, null)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1609 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1610 |
} catch (Exception e2) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1611 |
// ignore, when a SecretkeyEntry does not include a cert |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1612 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1613 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1614 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1615 |
certificateFactory = CertificateFactory.getInstance("X.509"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1616 |
validator = CertPathValidator.getInstance("PKIX"); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1617 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1618 |
pkixParameters = new PKIXParameters(tas); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1619 |
pkixParameters.setRevocationEnabled(false); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1620 |
} catch (InvalidAlgorithmParameterException ex) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1621 |
// Only if tas is empty |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1622 |
} |
2 | 1623 |
} catch (IOException ioe) { |
1624 |
throw new RuntimeException(rb.getString("keystore load: ") + |
|
1625 |
ioe.getMessage()); |
|
1626 |
} catch (java.security.cert.CertificateException ce) { |
|
1627 |
throw new RuntimeException(rb.getString("certificate exception: ") + |
|
1628 |
ce.getMessage()); |
|
1629 |
} catch (NoSuchProviderException pe) { |
|
1630 |
throw new RuntimeException(rb.getString("keystore load: ") + |
|
1631 |
pe.getMessage()); |
|
1632 |
} catch (NoSuchAlgorithmException nsae) { |
|
1633 |
throw new RuntimeException(rb.getString("keystore load: ") + |
|
1634 |
nsae.getMessage()); |
|
1635 |
} catch (KeyStoreException kse) { |
|
1636 |
throw new RuntimeException |
|
1637 |
(rb.getString("unable to instantiate keystore class: ") + |
|
1638 |
kse.getMessage()); |
|
1639 |
} |
|
1640 |
} |
|
1641 |
||
1642 |
X509Certificate getTsaCert(String alias) { |
|
1643 |
||
1644 |
java.security.cert.Certificate cs = null; |
|
1645 |
||
1646 |
try { |
|
1647 |
cs = store.getCertificate(alias); |
|
1648 |
} catch (KeyStoreException kse) { |
|
1649 |
// this never happens, because keystore has been loaded |
|
1650 |
} |
|
1651 |
if (cs == null || (!(cs instanceof X509Certificate))) { |
|
1652 |
MessageFormat form = new MessageFormat(rb.getString |
|
1653 |
("Certificate not found for: alias. alias must reference a valid KeyStore entry containing an X.509 public key certificate for the Timestamping Authority.")); |
|
1654 |
Object[] source = {alias, alias}; |
|
1655 |
error(form.format(source)); |
|
1656 |
} |
|
1657 |
return (X509Certificate) cs; |
|
1658 |
} |
|
1659 |
||
1660 |
/** |
|
1661 |
* Check if userCert is designed to be a code signer |
|
1662 |
* @param userCert the certificate to be examined |
|
1663 |
* @param bad 3 booleans to show if the KeyUsage, ExtendedKeyUsage, |
|
1664 |
* NetscapeCertType has codeSigning flag turned on. |
|
1665 |
* If null, the class field badKeyUsage, badExtendedKeyUsage, |
|
1666 |
* badNetscapeCertType will be set. |
|
1667 |
*/ |
|
1668 |
void checkCertUsage(X509Certificate userCert, boolean[] bad) { |
|
1669 |
||
1670 |
// Can act as a signer? |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1671 |
// 1. if KeyUsage, then [0:digitalSignature] or |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1672 |
// [1:nonRepudiation] should be true |
2 | 1673 |
// 2. if ExtendedKeyUsage, then should contains ANY or CODE_SIGNING |
1674 |
// 3. if NetscapeCertType, then should contains OBJECT_SIGNING |
|
1675 |
// 1,2,3 must be true |
|
1676 |
||
1677 |
if (bad != null) { |
|
1678 |
bad[0] = bad[1] = bad[2] = false; |
|
1679 |
} |
|
1680 |
||
1681 |
boolean[] keyUsage = userCert.getKeyUsage(); |
|
1682 |
if (keyUsage != null) { |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1683 |
keyUsage = Arrays.copyOf(keyUsage, 9); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1684 |
if (!keyUsage[0] && !keyUsage[1]) { |
2 | 1685 |
if (bad != null) { |
1686 |
bad[0] = true; |
|
1687 |
badKeyUsage = true; |
|
1688 |
} |
|
1689 |
} |
|
1690 |
} |
|
1691 |
||
1692 |
try { |
|
1693 |
List<String> xKeyUsage = userCert.getExtendedKeyUsage(); |
|
1694 |
if (xKeyUsage != null) { |
|
1695 |
if (!xKeyUsage.contains("2.5.29.37.0") // anyExtendedKeyUsage |
|
1696 |
&& !xKeyUsage.contains("1.3.6.1.5.5.7.3.3")) { // codeSigning |
|
1697 |
if (bad != null) { |
|
1698 |
bad[1] = true; |
|
1699 |
badExtendedKeyUsage = true; |
|
1700 |
} |
|
1701 |
} |
|
1702 |
} |
|
1703 |
} catch (java.security.cert.CertificateParsingException e) { |
|
1704 |
// shouldn't happen |
|
1705 |
} |
|
1706 |
||
1707 |
try { |
|
1708 |
// OID_NETSCAPE_CERT_TYPE |
|
1709 |
byte[] netscapeEx = userCert.getExtensionValue |
|
1710 |
("2.16.840.1.113730.1.1"); |
|
1711 |
if (netscapeEx != null) { |
|
1712 |
DerInputStream in = new DerInputStream(netscapeEx); |
|
1713 |
byte[] encoded = in.getOctetString(); |
|
1714 |
encoded = new DerValue(encoded).getUnalignedBitString() |
|
1715 |
.toByteArray(); |
|
1716 |
||
1717 |
NetscapeCertTypeExtension extn = |
|
1718 |
new NetscapeCertTypeExtension(encoded); |
|
1719 |
||
1720 |
Boolean val = (Boolean)extn.get( |
|
1721 |
NetscapeCertTypeExtension.OBJECT_SIGNING); |
|
1722 |
if (!val) { |
|
1723 |
if (bad != null) { |
|
1724 |
bad[2] = true; |
|
1725 |
badNetscapeCertType = true; |
|
1726 |
} |
|
1727 |
} |
|
1728 |
} |
|
1729 |
} catch (IOException e) { |
|
1730 |
// |
|
1731 |
} |
|
1732 |
} |
|
1733 |
||
1734 |
void getAliasInfo(String alias) { |
|
1735 |
||
1736 |
Key key = null; |
|
1737 |
||
1738 |
try { |
|
1739 |
java.security.cert.Certificate[] cs = null; |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1740 |
if (altCertChain != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1741 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1742 |
cs = CertificateFactory.getInstance("X.509"). |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1743 |
generateCertificates(new FileInputStream(altCertChain)). |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1744 |
toArray(new Certificate[0]); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1745 |
} catch (CertificateException ex) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1746 |
error(rb.getString("Cannot restore certchain from file specified")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1747 |
} catch (FileNotFoundException ex) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1748 |
error(rb.getString("File specified by -certchain does not exist")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1749 |
} |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1750 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1751 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1752 |
cs = store.getCertificateChain(alias); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1753 |
} catch (KeyStoreException kse) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1754 |
// this never happens, because keystore has been loaded |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1755 |
} |
2 | 1756 |
} |
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1757 |
if (cs == null || cs.length == 0) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1758 |
if (altCertChain != null) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1759 |
error(rb.getString |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1760 |
("Certificate chain not found in the file specified.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1761 |
} else { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1762 |
MessageFormat form = new MessageFormat(rb.getString |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1763 |
("Certificate chain not found for: alias. alias must" + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1764 |
" reference a valid KeyStore key entry containing a" + |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1765 |
" private key and corresponding public key certificate chain.")); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1766 |
Object[] source = {alias, alias}; |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1767 |
error(form.format(source)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1768 |
} |
2 | 1769 |
} |
1770 |
||
1771 |
certChain = new X509Certificate[cs.length]; |
|
1772 |
for (int i=0; i<cs.length; i++) { |
|
1773 |
if (!(cs[i] instanceof X509Certificate)) { |
|
1774 |
error(rb.getString |
|
1775 |
("found non-X.509 certificate in signer's chain")); |
|
1776 |
} |
|
1777 |
certChain[i] = (X509Certificate)cs[i]; |
|
1778 |
} |
|
1779 |
||
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1780 |
// We don't meant to print anything, the next call |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1781 |
// checks validity and keyUsage etc |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1782 |
printCert("", certChain[0], true, 0); |
2 | 1783 |
|
2432
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1784 |
try { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1785 |
CertPath cp = certificateFactory.generateCertPath(Arrays.asList(certChain)); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1786 |
validator.validate(cp, pkixParameters); |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1787 |
} catch (Exception e) { |
dc17f417ef85
6802846: jarsigner needs enhanced cert validation(options)
weijun
parents:
2
diff
changeset
|
1788 |
chainNotValidated = true; |
2 | 1789 |
} |
1790 |
||
1791 |
try { |
|
1792 |
if (!token && keypass == null) |
|
1793 |
key = store.getKey(alias, storepass); |
|
1794 |
else |
|
1795 |
key = store.getKey(alias, keypass); |
|
1796 |
} catch (UnrecoverableKeyException e) { |
|
1797 |
if (token) { |
|
1798 |
throw e; |
|
1799 |
} else if (keypass == null) { |
|
1800 |
// Did not work out, so prompt user for key password |
|
1801 |
MessageFormat form = new MessageFormat(rb.getString |
|
1802 |
("Enter key password for alias: ")); |
|
1803 |
Object[] source = {alias}; |
|
1804 |
keypass = getPass(form.format(source)); |
|
1805 |
key = store.getKey(alias, keypass); |
|
1806 |
} |
|
1807 |
} |
|
1808 |
} catch (NoSuchAlgorithmException e) { |
|
1809 |
error(e.getMessage()); |
|
1810 |
} catch (UnrecoverableKeyException e) { |
|
1811 |
error(rb.getString("unable to recover key from keystore")); |
|
1812 |
} catch (KeyStoreException kse) { |
|
1813 |
// this never happens, because keystore has been loaded |
|
1814 |
} |
|
1815 |
||
1816 |
if (!(key instanceof PrivateKey)) { |
|
1817 |
MessageFormat form = new MessageFormat(rb.getString |
|
1818 |
("key associated with alias not a private key")); |
|
1819 |
Object[] source = {alias}; |
|
1820 |
error(form.format(source)); |
|
1821 |
} else { |
|
1822 |
privateKey = (PrivateKey)key; |
|
1823 |
} |
|
1824 |
} |
|
1825 |
||
1826 |
void error(String message) |
|
1827 |
{ |
|
1828 |
System.out.println(rb.getString("jarsigner: ")+message); |
|
1829 |
System.exit(1); |
|
1830 |
} |
|
1831 |
||
1832 |
||
1833 |
void error(String message, Exception e) |
|
1834 |
{ |
|
1835 |
System.out.println(rb.getString("jarsigner: ")+message); |
|
1836 |
if (debug) { |
|
1837 |
e.printStackTrace(); |
|
1838 |
} |
|
1839 |
System.exit(1); |
|
1840 |
} |
|
1841 |
||
1842 |
char[] getPass(String prompt) |
|
1843 |
{ |
|
1844 |
System.err.print(prompt); |
|
1845 |
System.err.flush(); |
|
1846 |
try { |
|
1847 |
char[] pass = Password.readPassword(System.in); |
|
1848 |
||
1849 |
if (pass == null) { |
|
1850 |
error(rb.getString("you must enter key password")); |
|
1851 |
} else { |
|
1852 |
return pass; |
|
1853 |
} |
|
1854 |
} catch (IOException ioe) { |
|
1855 |
error(rb.getString("unable to read password: ")+ioe.getMessage()); |
|
1856 |
} |
|
1857 |
// this shouldn't happen |
|
1858 |
return null; |
|
1859 |
} |
|
1860 |
||
1861 |
/* |
|
1862 |
* Reads all the bytes for a given zip entry. |
|
1863 |
*/ |
|
1864 |
private synchronized byte[] getBytes(ZipFile zf, |
|
1865 |
ZipEntry ze) throws IOException { |
|
1866 |
int n; |
|
1867 |
||
1868 |
InputStream is = null; |
|
1869 |
try { |
|
1870 |
is = zf.getInputStream(ze); |
|
1871 |
baos.reset(); |
|
1872 |
long left = ze.getSize(); |
|
1873 |
||
1874 |
while((left > 0) && (n = is.read(buffer, 0, buffer.length)) != -1) { |
|
1875 |
baos.write(buffer, 0, n); |
|
1876 |
left -= n; |
|
1877 |
} |
|
1878 |
} finally { |
|
1879 |
if (is != null) { |
|
1880 |
is.close(); |
|
1881 |
} |
|
1882 |
} |
|
1883 |
||
1884 |
return baos.toByteArray(); |
|
1885 |
} |
|
1886 |
||
1887 |
/* |
|
1888 |
* Returns manifest entry from given jar file, or null if given jar file |
|
1889 |
* does not have a manifest entry. |
|
1890 |
*/ |
|
1891 |
private ZipEntry getManifestFile(ZipFile zf) { |
|
1892 |
ZipEntry ze = zf.getEntry(JarFile.MANIFEST_NAME); |
|
1893 |
if (ze == null) { |
|
1894 |
// Check all entries for matching name |
|
1895 |
Enumeration<? extends ZipEntry> enum_ = zf.entries(); |
|
1896 |
while (enum_.hasMoreElements() && ze == null) { |
|
1897 |
ze = enum_.nextElement(); |
|
1898 |
if (!JarFile.MANIFEST_NAME.equalsIgnoreCase |
|
1899 |
(ze.getName())) { |
|
1900 |
ze = null; |
|
1901 |
} |
|
1902 |
} |
|
1903 |
} |
|
1904 |
return ze; |
|
1905 |
} |
|
1906 |
||
1907 |
/* |
|
1908 |
* Computes the digests of a zip entry, and returns them as an array |
|
1909 |
* of base64-encoded strings. |
|
1910 |
*/ |
|
1911 |
private synchronized String[] getDigests(ZipEntry ze, ZipFile zf, |
|
1912 |
MessageDigest[] digests, |
|
1913 |
BASE64Encoder encoder) |
|
1914 |
throws IOException { |
|
1915 |
||
1916 |
int n, i; |
|
1917 |
InputStream is = null; |
|
1918 |
try { |
|
1919 |
is = zf.getInputStream(ze); |
|
1920 |
long left = ze.getSize(); |
|
1921 |
while((left > 0) |
|
1922 |
&& (n = is.read(buffer, 0, buffer.length)) != -1) { |
|
1923 |
for (i=0; i<digests.length; i++) { |
|
1924 |
digests[i].update(buffer, 0, n); |
|
1925 |
} |
|
1926 |
left -= n; |
|
1927 |
} |
|
1928 |
} finally { |
|
1929 |
if (is != null) { |
|
1930 |
is.close(); |
|
1931 |
} |
|
1932 |
} |
|
1933 |
||
1934 |
// complete the digests |
|
1935 |
String[] base64Digests = new String[digests.length]; |
|
1936 |
for (i=0; i<digests.length; i++) { |
|
1937 |
base64Digests[i] = encoder.encode(digests[i].digest()); |
|
1938 |
} |
|
1939 |
return base64Digests; |
|
1940 |
} |
|
1941 |
||
1942 |
/* |
|
1943 |
* Computes the digests of a zip entry, and returns them as a list of |
|
1944 |
* attributes |
|
1945 |
*/ |
|
1946 |
private Attributes getDigestAttributes(ZipEntry ze, ZipFile zf, |
|
1947 |
MessageDigest[] digests, |
|
1948 |
BASE64Encoder encoder) |
|
1949 |
throws IOException { |
|
1950 |
||
1951 |
String[] base64Digests = getDigests(ze, zf, digests, encoder); |
|
1952 |
Attributes attrs = new Attributes(); |
|
1953 |
||
1954 |
for (int i=0; i<digests.length; i++) { |
|
1955 |
attrs.putValue(digests[i].getAlgorithm()+"-Digest", |
|
1956 |
base64Digests[i]); |
|
1957 |
} |
|
1958 |
return attrs; |
|
1959 |
} |
|
1960 |
||
1961 |
/* |
|
1962 |
* Updates the digest attributes of a manifest entry, by adding or |
|
1963 |
* replacing digest values. |
|
1964 |
* A digest value is added if the manifest entry does not contain a digest |
|
1965 |
* for that particular algorithm. |
|
1966 |
* A digest value is replaced if it is obsolete. |
|
1967 |
* |
|
1968 |
* Returns true if the manifest entry has been changed, and false |
|
1969 |
* otherwise. |
|
1970 |
*/ |
|
1971 |
private boolean updateDigests(ZipEntry ze, ZipFile zf, |
|
1972 |
MessageDigest[] digests, |
|
1973 |
BASE64Encoder encoder, |
|
1974 |
Manifest mf) throws IOException { |
|
1975 |
boolean update = false; |
|
1976 |
||
1977 |
Attributes attrs = mf.getAttributes(ze.getName()); |
|
1978 |
String[] base64Digests = getDigests(ze, zf, digests, encoder); |
|
1979 |
||
1980 |
for (int i=0; i<digests.length; i++) { |
|
3716
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1981 |
// The entry name to be written into attrs |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1982 |
String name = null; |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1983 |
try { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1984 |
// Find if the digest already exists |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1985 |
AlgorithmId aid = AlgorithmId.get(digests[i].getAlgorithm()); |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1986 |
for (Object key: attrs.keySet()) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1987 |
if (key instanceof Attributes.Name) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1988 |
String n = ((Attributes.Name)key).toString(); |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1989 |
if (n.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1990 |
String tmp = n.substring(0, n.length() - 7); |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1991 |
if (AlgorithmId.get(tmp).equals(aid)) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1992 |
name = n; |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1993 |
break; |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1994 |
} |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1995 |
} |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1996 |
} |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1997 |
} |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1998 |
} catch (NoSuchAlgorithmException nsae) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
1999 |
// Ignored. Writing new digest entry. |
2 | 2000 |
} |
3716
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
2001 |
|
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
2002 |
if (name == null) { |
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
2003 |
name = digests[i].getAlgorithm()+"-Digest"; |
2 | 2004 |
attrs.putValue(name, base64Digests[i]); |
2005 |
update=true; |
|
2006 |
} else { |
|
2007 |
// compare digests, and replace the one in the manifest |
|
2008 |
// if they are different |
|
3716
aa66143f7ad1
6876328: different names for the same digest algorithms breaks jarsigner
weijun
parents:
3481
diff
changeset
|
2009 |
String mfDigest = attrs.getValue(name); |
2 | 2010 |
if (!mfDigest.equalsIgnoreCase(base64Digests[i])) { |
2011 |
attrs.putValue(name, base64Digests[i]); |
|
2012 |
update=true; |
|
2013 |
} |
|
2014 |
} |
|
2015 |
} |
|
2016 |
return update; |
|
2017 |
} |
|
2018 |
||
2019 |
/* |
|
2020 |
* Try to load the specified signing mechanism. |
|
2021 |
* The URL class loader is used. |
|
2022 |
*/ |
|
2023 |
private ContentSigner loadSigningMechanism(String signerClassName, |
|
2024 |
String signerClassPath) throws Exception { |
|
2025 |
||
2026 |
// construct class loader |
|
2027 |
String cpString = null; // make sure env.class.path defaults to dot |
|
2028 |
||
2029 |
// do prepends to get correct ordering |
|
2030 |
cpString = PathList.appendPath(System.getProperty("env.class.path"), cpString); |
|
2031 |
cpString = PathList.appendPath(System.getProperty("java.class.path"), cpString); |
|
2032 |
cpString = PathList.appendPath(signerClassPath, cpString); |
|
2033 |
URL[] urls = PathList.pathToURLs(cpString); |
|
2034 |
ClassLoader appClassLoader = new URLClassLoader(urls); |
|
2035 |
||
2036 |
// attempt to find signer |
|
2037 |
Class signerClass = appClassLoader.loadClass(signerClassName); |
|
2038 |
||
2039 |
// Check that it implements ContentSigner |
|
2040 |
Object signer = signerClass.newInstance(); |
|
2041 |
if (!(signer instanceof ContentSigner)) { |
|
2042 |
MessageFormat form = new MessageFormat( |
|
2043 |
rb.getString("signerClass is not a signing mechanism")); |
|
2044 |
Object[] source = {signerClass.getName()}; |
|
2045 |
throw new IllegalArgumentException(form.format(source)); |
|
2046 |
} |
|
2047 |
return (ContentSigner)signer; |
|
2048 |
} |
|
2049 |
} |
|
2050 |
||
2051 |
/** |
|
2052 |
* This is a BASE64Encoder that does not insert a default newline at the end of |
|
2053 |
* every output line. This is necessary because java.util.jar does its own |
|
2054 |
* line management (see Manifest.make72Safe()). Inserting additional new lines |
|
2055 |
* can cause line-wrapping problems (see CR 6219522). |
|
2056 |
*/ |
|
2057 |
class JarBASE64Encoder extends BASE64Encoder { |
|
2058 |
/** |
|
2059 |
* Encode the suffix that ends every output line. |
|
2060 |
*/ |
|
2061 |
protected void encodeLineSuffix(OutputStream aStream) throws IOException { } |
|
2062 |
} |
|
2063 |
||
2064 |
class SignatureFile { |
|
2065 |
||
2066 |
/** SignatureFile */ |
|
2067 |
Manifest sf; |
|
2068 |
||
2069 |
/** .SF base name */ |
|
2070 |
String baseName; |
|
2071 |
||
2072 |
public SignatureFile(MessageDigest digests[], |
|
2073 |
Manifest mf, |
|
2074 |
ManifestDigester md, |
|
2075 |
String baseName, |
|
2076 |
boolean signManifest) |
|
2077 |
||
2078 |
{ |
|
2079 |
this.baseName = baseName; |
|
2080 |
||
2081 |
String version = System.getProperty("java.version"); |
|
2082 |
String javaVendor = System.getProperty("java.vendor"); |
|
2083 |
||
2084 |
sf = new Manifest(); |
|
2085 |
Attributes mattr = sf.getMainAttributes(); |
|
2086 |
BASE64Encoder encoder = new JarBASE64Encoder(); |
|
2087 |
||
2088 |
mattr.putValue(Attributes.Name.SIGNATURE_VERSION.toString(), "1.0"); |
|
2089 |
mattr.putValue("Created-By", version + " (" + javaVendor + ")"); |
|
2090 |
||
2091 |
if (signManifest) { |
|
2092 |
// sign the whole manifest |
|
2093 |
for (int i=0; i < digests.length; i++) { |
|
2094 |
mattr.putValue(digests[i].getAlgorithm()+"-Digest-Manifest", |
|
2095 |
encoder.encode(md.manifestDigest(digests[i]))); |
|
2096 |
} |
|
2097 |
} |
|
2098 |
||
2099 |
// create digest of the manifest main attributes |
|
2100 |
ManifestDigester.Entry mde = |
|
2101 |
md.get(ManifestDigester.MF_MAIN_ATTRS, false); |
|
2102 |
if (mde != null) { |
|
2103 |
for (int i=0; i < digests.length; i++) { |
|
2104 |
mattr.putValue(digests[i].getAlgorithm() + |
|
2105 |
"-Digest-" + ManifestDigester.MF_MAIN_ATTRS, |
|
2106 |
encoder.encode(mde.digest(digests[i]))); |
|
2107 |
} |
|
2108 |
} else { |
|
2109 |
throw new IllegalStateException |
|
2110 |
("ManifestDigester failed to create " + |
|
2111 |
"Manifest-Main-Attribute entry"); |
|
2112 |
} |
|
2113 |
||
2114 |
/* go through the manifest entries and create the digests */ |
|
2115 |
||
2116 |
Map<String,Attributes> entries = sf.getEntries(); |
|
2117 |
Iterator<Map.Entry<String,Attributes>> mit = |
|
2118 |
mf.getEntries().entrySet().iterator(); |
|
2119 |
while(mit.hasNext()) { |
|
2120 |
Map.Entry<String,Attributes> e = mit.next(); |
|
2121 |
String name = e.getKey(); |
|
2122 |
mde = md.get(name, false); |
|
2123 |
if (mde != null) { |
|
2124 |
Attributes attr = new Attributes(); |
|
2125 |
for (int i=0; i < digests.length; i++) { |
|
2126 |
attr.putValue(digests[i].getAlgorithm()+"-Digest", |
|
2127 |
encoder.encode(mde.digest(digests[i]))); |
|
2128 |
} |
|
2129 |
entries.put(name, attr); |
|
2130 |
} |
|
2131 |
} |
|
2132 |
} |
|
2133 |
||
2134 |
/** |
|
2135 |
* Writes the SignatureFile to the specified OutputStream. |
|
2136 |
* |
|
2137 |
* @param out the output stream |
|
2138 |
* @exception IOException if an I/O error has occurred |
|
2139 |
*/ |
|
2140 |
||
2141 |
public void write(OutputStream out) throws IOException |
|
2142 |
{ |
|
2143 |
sf.write(out); |
|
2144 |
} |
|
2145 |
||
2146 |
/** |
|
2147 |
* get .SF file name |
|
2148 |
*/ |
|
2149 |
public String getMetaName() |
|
2150 |
{ |
|
2151 |
return "META-INF/"+ baseName + ".SF"; |
|
2152 |
} |
|
2153 |
||
2154 |
/** |
|
2155 |
* get base file name |
|
2156 |
*/ |
|
2157 |
public String getBaseName() |
|
2158 |
{ |
|
2159 |
return baseName; |
|
2160 |
} |
|
2161 |
||
2162 |
/* |
|
2163 |
* Generate a signed data block. |
|
2164 |
* If a URL or a certificate (containing a URL) for a Timestamping |
|
2165 |
* Authority is supplied then a signature timestamp is generated and |
|
2166 |
* inserted into the signed data block. |
|
2167 |
* |
|
2168 |
* @param sigalg signature algorithm to use, or null to use default |
|
2169 |
* @param tsaUrl The location of the Timestamping Authority. If null |
|
2170 |
* then no timestamp is requested. |
|
2171 |
* @param tsaCert The certificate for the Timestamping Authority. If null |
|
2172 |
* then no timestamp is requested. |
|
2173 |
* @param signingMechanism The signing mechanism to use. |
|
2174 |
* @param args The command-line arguments to jarsigner. |
|
2175 |
* @param zipFile The original source Zip file. |
|
2176 |
*/ |
|
2177 |
public Block generateBlock(PrivateKey privateKey, |
|
2178 |
String sigalg, |
|
2179 |
X509Certificate[] certChain, |
|
2180 |
boolean externalSF, String tsaUrl, |
|
2181 |
X509Certificate tsaCert, |
|
2182 |
ContentSigner signingMechanism, |
|
2183 |
String[] args, ZipFile zipFile) |
|
2184 |
throws NoSuchAlgorithmException, InvalidKeyException, IOException, |
|
2185 |
SignatureException, CertificateException |
|
2186 |
{ |
|
2187 |
return new Block(this, privateKey, sigalg, certChain, externalSF, |
|
2188 |
tsaUrl, tsaCert, signingMechanism, args, zipFile); |
|
2189 |
} |
|
2190 |
||
2191 |
||
2192 |
public static class Block { |
|
2193 |
||
2194 |
private byte[] block; |
|
2195 |
private String blockFileName; |
|
2196 |
||
2197 |
/* |
|
2198 |
* Construct a new signature block. |
|
2199 |
*/ |
|
2200 |
Block(SignatureFile sfg, PrivateKey privateKey, String sigalg, |
|
2201 |
X509Certificate[] certChain, boolean externalSF, String tsaUrl, |
|
2202 |
X509Certificate tsaCert, ContentSigner signingMechanism, |
|
2203 |
String[] args, ZipFile zipFile) |
|
2204 |
throws NoSuchAlgorithmException, InvalidKeyException, IOException, |
|
2205 |
SignatureException, CertificateException { |
|
2206 |
||
2207 |
Principal issuerName = certChain[0].getIssuerDN(); |
|
2208 |
if (!(issuerName instanceof X500Name)) { |
|
2209 |
// must extract the original encoded form of DN for subsequent |
|
2210 |
// name comparison checks (converting to a String and back to |
|
2211 |
// an encoded DN could cause the types of String attribute |
|
2212 |
// values to be changed) |
|
2213 |
X509CertInfo tbsCert = new |
|
2214 |
X509CertInfo(certChain[0].getTBSCertificate()); |
|
2215 |
issuerName = (Principal) |
|
2216 |
tbsCert.get(CertificateIssuerName.NAME + "." + |
|
2217 |
CertificateIssuerName.DN_NAME); |
|
2218 |
} |
|
2219 |
BigInteger serial = certChain[0].getSerialNumber(); |
|
2220 |
||
2221 |
String digestAlgorithm; |
|
2222 |
String signatureAlgorithm; |
|
2223 |
String keyAlgorithm = privateKey.getAlgorithm(); |
|
2224 |
/* |
|
2225 |
* If no signature algorithm was specified, we choose a |
|
2226 |
* default that is compatible with the private key algorithm. |
|
2227 |
*/ |
|
2228 |
if (sigalg == null) { |
|
2229 |
||
2230 |
if (keyAlgorithm.equalsIgnoreCase("DSA")) |
|
2231 |
digestAlgorithm = "SHA1"; |
|
2232 |
else if (keyAlgorithm.equalsIgnoreCase("RSA")) |
|
3318
dade78e63c92
6561126: keytool should use larger default keysize for keypairs
weijun
parents:
3047
diff
changeset
|
2233 |
digestAlgorithm = "SHA256"; |
2 | 2234 |
else { |
2235 |
throw new RuntimeException("private key is not a DSA or " |
|
2236 |
+ "RSA key"); |
|
2237 |
} |
|
2238 |
signatureAlgorithm = digestAlgorithm + "with" + keyAlgorithm; |
|
2239 |
} else { |
|
2240 |
signatureAlgorithm = sigalg; |
|
2241 |
} |
|
2242 |
||
2243 |
// check common invalid key/signature algorithm combinations |
|
2244 |
String sigAlgUpperCase = signatureAlgorithm.toUpperCase(); |
|
2245 |
if ((sigAlgUpperCase.endsWith("WITHRSA") && |
|
2246 |
!keyAlgorithm.equalsIgnoreCase("RSA")) || |
|
2247 |
(sigAlgUpperCase.endsWith("WITHDSA") && |
|
2248 |
!keyAlgorithm.equalsIgnoreCase("DSA"))) { |
|
2249 |
throw new SignatureException |
|
2250 |
("private key algorithm is not compatible with signature algorithm"); |
|
2251 |
} |
|
2252 |
||
2253 |
blockFileName = "META-INF/"+sfg.getBaseName()+"."+keyAlgorithm; |
|
2254 |
||
2255 |
AlgorithmId sigAlg = AlgorithmId.get(signatureAlgorithm); |
|
2256 |
AlgorithmId digEncrAlg = AlgorithmId.get(keyAlgorithm); |
|
2257 |
||
2258 |
Signature sig = Signature.getInstance(signatureAlgorithm); |
|
2259 |
sig.initSign(privateKey); |
|
2260 |
||
2261 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
2262 |
sfg.write(baos); |
|
2263 |
||
2264 |
byte[] content = baos.toByteArray(); |
|
2265 |
||
2266 |
sig.update(content); |
|
2267 |
byte[] signature = sig.sign(); |
|
2268 |
||
2269 |
// Timestamp the signature and generate the signature block file |
|
2270 |
if (signingMechanism == null) { |
|
2271 |
signingMechanism = new TimestampedSigner(); |
|
2272 |
} |
|
2273 |
URI tsaUri = null; |
|
2274 |
try { |
|
2275 |
if (tsaUrl != null) { |
|
2276 |
tsaUri = new URI(tsaUrl); |
|
2277 |
} |
|
2278 |
} catch (URISyntaxException e) { |
|
2279 |
IOException ioe = new IOException(); |
|
2280 |
ioe.initCause(e); |
|
2281 |
throw ioe; |
|
2282 |
} |
|
2283 |
||
2284 |
// Assemble parameters for the signing mechanism |
|
2285 |
ContentSignerParameters params = |
|
2286 |
new JarSignerParameters(args, tsaUri, tsaCert, signature, |
|
2287 |
signatureAlgorithm, certChain, content, zipFile); |
|
2288 |
||
2289 |
// Generate the signature block |
|
2290 |
block = signingMechanism.generateSignedData( |
|
2291 |
params, externalSF, (tsaUrl != null || tsaCert != null)); |
|
2292 |
} |
|
2293 |
||
2294 |
/* |
|
2295 |
* get block file name. |
|
2296 |
*/ |
|
2297 |
public String getMetaName() |
|
2298 |
{ |
|
2299 |
return blockFileName; |
|
2300 |
} |
|
2301 |
||
2302 |
/** |
|
2303 |
* Writes the block file to the specified OutputStream. |
|
2304 |
* |
|
2305 |
* @param out the output stream |
|
2306 |
* @exception IOException if an I/O error has occurred |
|
2307 |
*/ |
|
2308 |
||
2309 |
public void write(OutputStream out) throws IOException |
|
2310 |
{ |
|
2311 |
out.write(block); |
|
2312 |
} |
|
2313 |
} |
|
2314 |
} |
|
2315 |
||
2316 |
||
2317 |
/* |
|
2318 |
* This object encapsulates the parameters used to perform content signing. |
|
2319 |
*/ |
|
2320 |
class JarSignerParameters implements ContentSignerParameters { |
|
2321 |
||
2322 |
private String[] args; |
|
2323 |
private URI tsa; |
|
2324 |
private X509Certificate tsaCertificate; |
|
2325 |
private byte[] signature; |
|
2326 |
private String signatureAlgorithm; |
|
2327 |
private X509Certificate[] signerCertificateChain; |
|
2328 |
private byte[] content; |
|
2329 |
private ZipFile source; |
|
2330 |
||
2331 |
/** |
|
2332 |
* Create a new object. |
|
2333 |
*/ |
|
2334 |
JarSignerParameters(String[] args, URI tsa, X509Certificate tsaCertificate, |
|
2335 |
byte[] signature, String signatureAlgorithm, |
|
2336 |
X509Certificate[] signerCertificateChain, byte[] content, |
|
2337 |
ZipFile source) { |
|
2338 |
||
2339 |
if (signature == null || signatureAlgorithm == null || |
|
2340 |
signerCertificateChain == null) { |
|
2341 |
throw new NullPointerException(); |
|
2342 |
} |
|
2343 |
this.args = args; |
|
2344 |
this.tsa = tsa; |
|
2345 |
this.tsaCertificate = tsaCertificate; |
|
2346 |
this.signature = signature; |
|
2347 |
this.signatureAlgorithm = signatureAlgorithm; |
|
2348 |
this.signerCertificateChain = signerCertificateChain; |
|
2349 |
this.content = content; |
|
2350 |
this.source = source; |
|
2351 |
} |
|
2352 |
||
2353 |
/** |
|
2354 |
* Retrieves the command-line arguments. |
|
2355 |
* |
|
2356 |
* @return The command-line arguments. May be null. |
|
2357 |
*/ |
|
2358 |
public String[] getCommandLine() { |
|
2359 |
return args; |
|
2360 |
} |
|
2361 |
||
2362 |
/** |
|
2363 |
* Retrieves the identifier for a Timestamping Authority (TSA). |
|
2364 |
* |
|
2365 |
* @return The TSA identifier. May be null. |
|
2366 |
*/ |
|
2367 |
public URI getTimestampingAuthority() { |
|
2368 |
return tsa; |
|
2369 |
} |
|
2370 |
||
2371 |
/** |
|
2372 |
* Retrieves the certificate for a Timestamping Authority (TSA). |
|
2373 |
* |
|
2374 |
* @return The TSA certificate. May be null. |
|
2375 |
*/ |
|
2376 |
public X509Certificate getTimestampingAuthorityCertificate() { |
|
2377 |
return tsaCertificate; |
|
2378 |
} |
|
2379 |
||
2380 |
/** |
|
2381 |
* Retrieves the signature. |
|
2382 |
* |
|
2383 |
* @return The non-null signature bytes. |
|
2384 |
*/ |
|
2385 |
public byte[] getSignature() { |
|
2386 |
return signature; |
|
2387 |
} |
|
2388 |
||
2389 |
/** |
|
2390 |
* Retrieves the name of the signature algorithm. |
|
2391 |
* |
|
2392 |
* @return The non-null string name of the signature algorithm. |
|
2393 |
*/ |
|
2394 |
public String getSignatureAlgorithm() { |
|
2395 |
return signatureAlgorithm; |
|
2396 |
} |
|
2397 |
||
2398 |
/** |
|
2399 |
* Retrieves the signer's X.509 certificate chain. |
|
2400 |
* |
|
2401 |
* @return The non-null array of X.509 public-key certificates. |
|
2402 |
*/ |
|
2403 |
public X509Certificate[] getSignerCertificateChain() { |
|
2404 |
return signerCertificateChain; |
|
2405 |
} |
|
2406 |
||
2407 |
/** |
|
2408 |
* Retrieves the content that was signed. |
|
2409 |
* |
|
2410 |
* @return The content bytes. May be null. |
|
2411 |
*/ |
|
2412 |
public byte[] getContent() { |
|
2413 |
return content; |
|
2414 |
} |
|
2415 |
||
2416 |
/** |
|
2417 |
* Retrieves the original source ZIP file before it was signed. |
|
2418 |
* |
|
2419 |
* @return The original ZIP file. May be null. |
|
2420 |
*/ |
|
2421 |
public ZipFile getSource() { |
|
2422 |
return source; |
|
2423 |
} |
|
2424 |
} |