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