2
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.crypto;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.net.*;
|
|
30 |
import java.security.*;
|
|
31 |
import java.util.*;
|
|
32 |
import java.util.jar.*;
|
|
33 |
import javax.crypto.CryptoPolicyParser.ParsingException;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* This class verifies JAR files (and any supporting JAR files), and
|
|
37 |
* determines whether they may be used in this implementation.
|
|
38 |
*
|
|
39 |
* The JCE in OpenJDK has an open cryptographic interface, meaning it
|
|
40 |
* does not restrict which providers can be used. Compliance with
|
|
41 |
* United States export controls and with local law governing the
|
|
42 |
* import/export of products incorporating the JCE in the OpenJDK is
|
|
43 |
* the responsibility of the licensee.
|
|
44 |
*
|
|
45 |
* @since 1.7
|
|
46 |
*/
|
|
47 |
final class JarVerifier {
|
|
48 |
|
|
49 |
// The URL for the JAR file we want to verify.
|
|
50 |
private URL jarURL;
|
|
51 |
private boolean savePerms;
|
|
52 |
private CryptoPermissions appPerms = null;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Creates a JarVerifier object to verify the given URL.
|
|
56 |
*
|
|
57 |
* @param jarURL the JAR file to be verified.
|
|
58 |
* @param savePerms if true, save the permissions allowed by the
|
|
59 |
* exemption mechanism
|
|
60 |
*/
|
|
61 |
JarVerifier(URL jarURL, boolean savePerms) {
|
|
62 |
this.jarURL = jarURL;
|
|
63 |
this.savePerms = savePerms;
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Verify the JAR file is signed by an entity which has a certificate
|
|
68 |
* issued by a trusted CA.
|
|
69 |
*
|
|
70 |
* In OpenJDK, we just need to examine the "cryptoperms" file to see
|
|
71 |
* if any permissions were bundled together with this jar file.
|
|
72 |
*/
|
|
73 |
void verify() throws JarException, IOException {
|
|
74 |
|
|
75 |
// Short-circuit. If we weren't asked to save any, we're done.
|
|
76 |
if (!savePerms) {
|
|
77 |
return;
|
|
78 |
}
|
|
79 |
|
|
80 |
// If the protocol of jarURL isn't "jar", we should
|
|
81 |
// construct a JAR URL so we can open a JarURLConnection
|
|
82 |
// for verifying this provider.
|
|
83 |
final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
|
|
84 |
jarURL : new URL("jar:" + jarURL.toString() + "!/");
|
|
85 |
|
|
86 |
JarFile jf = null;
|
|
87 |
try {
|
|
88 |
|
|
89 |
// Get a link to the Jarfile to search.
|
|
90 |
try {
|
|
91 |
jf = (JarFile)
|
|
92 |
AccessController.doPrivileged(
|
|
93 |
new PrivilegedExceptionAction() {
|
|
94 |
public Object run() throws Exception {
|
|
95 |
JarURLConnection conn =
|
|
96 |
(JarURLConnection) url.openConnection();
|
|
97 |
// You could do some caching here as
|
|
98 |
// an optimization.
|
|
99 |
conn.setUseCaches(false);
|
|
100 |
return conn.getJarFile();
|
|
101 |
}
|
|
102 |
});
|
|
103 |
} catch (java.security.PrivilegedActionException pae) {
|
|
104 |
SecurityException se = new SecurityException(
|
|
105 |
"Cannot load " + url.toString());
|
|
106 |
se.initCause(pae);
|
|
107 |
throw se;
|
|
108 |
}
|
|
109 |
|
|
110 |
if (jf != null) {
|
|
111 |
JarEntry je = jf.getJarEntry("cryptoPerms");
|
|
112 |
if (je == null) {
|
|
113 |
throw new JarException(
|
|
114 |
"Can not find cryptoPerms");
|
|
115 |
}
|
|
116 |
try {
|
|
117 |
appPerms = new CryptoPermissions();
|
|
118 |
appPerms.load(jf.getInputStream(je));
|
|
119 |
} catch (Exception ex) {
|
|
120 |
JarException jex =
|
|
121 |
new JarException("Cannot load/parse" +
|
|
122 |
jarURL.toString());
|
|
123 |
jex.initCause(ex);
|
|
124 |
throw jex;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
} finally {
|
|
128 |
// Only call close() when caching is not enabled.
|
|
129 |
// Otherwise, exceptions will be thrown for all
|
|
130 |
// subsequent accesses of this cached jar.
|
|
131 |
if (jf != null) {
|
|
132 |
jf.close();
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Verify that the provided JarEntry was indeed signed by the
|
|
139 |
* framework signing certificate.
|
|
140 |
*
|
|
141 |
* @param je the URL of the jar entry to be checked.
|
|
142 |
* @throws Exception if the jar entry was not signed by
|
|
143 |
* the proper certificate
|
|
144 |
*/
|
|
145 |
static void verifyFrameworkSigned(URL je) throws Exception {
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Verify that the provided certs include the
|
|
150 |
* framework signing certificate.
|
|
151 |
*
|
|
152 |
* @param certs the list of certs to be checked.
|
|
153 |
* @throws Exception if the list of certs did not contain
|
|
154 |
* the framework signing certificate
|
|
155 |
*/
|
|
156 |
static void verifyPolicySigned(java.security.cert.Certificate[] certs)
|
|
157 |
throws Exception {
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Returns the permissions which are bundled with the JAR file,
|
|
162 |
* aka the "cryptoperms" file.
|
|
163 |
*
|
|
164 |
* NOTE: if this JarVerifier instance is constructed with "savePerms"
|
|
165 |
* equal to false, then this method would always return null.
|
|
166 |
*/
|
|
167 |
CryptoPermissions getPermissions() {
|
|
168 |
return appPerms;
|
|
169 |
}
|
|
170 |
}
|