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