1 23 24 package com.sun.enterprise.appclient.jws; 25 26 import com.sun.enterprise.deployment.backend.DeploymentLogger; 27 import com.sun.enterprise.security.SSLUtils; 28 import com.sun.enterprise.util.i18n.StringManager; 29 import java.io.File ; 30 import java.net.URI ; 31 import java.security.AccessControlException ; 32 import java.security.KeyStore ; 33 import java.security.KeyStoreException ; 34 import java.security.Permission ; 35 import java.util.ArrayList ; 36 import java.util.logging.Logger ; 37 import sun.security.tools.JarSigner; 38 39 47 public class SignedStaticContent extends StaticContent { 48 49 50 private static final String SIGNEDJAR_OPTION = "-signedjar"; 51 52 53 private static final String KEYSTORE_OPTION = "-keystore"; 54 55 56 private static final String STOREPASS_OPTION = "-storepass"; 57 58 59 private File unsignedJar; 60 61 62 private File signedJar; 63 64 65 private URI installRootURI; 66 67 68 private String keystoreAbsolutePath = null; 69 70 71 private String KEYSTORE_PATH_PROPERTYNAME = "javax.net.ssl.keyStore"; 72 73 74 private String USER_SPECIFIED_ALIAS_PROPERTYNAME = "com.sun.aas.jws.signing.alias"; 75 76 77 private String DEFAULT_ALIAS_VALUE = "s1as"; 78 79 private Logger logger = DeploymentLogger.get(); 80 81 private StringManager localStrings; 82 83 94 95 public SignedStaticContent( 96 ContentOrigin origin, 97 String contentKey, 98 String path, 99 File signedJar, 100 File unsignedJar, 101 URI installRootURI, 102 StringManager localStrings, 103 boolean isMain) throws Exception { 104 super(origin, contentKey, path, signedJar, installRootURI, isMain); 105 106 109 this.installRootURI = installRootURI; 110 this.unsignedJar = unsignedJar; 111 this.signedJar = signedJar; 112 this.localStrings = localStrings; 113 } 114 115 121 public URI getRelativeURI() { 122 try { 123 ensureSignedFileUpToDate(); 124 return installRootURI.relativize(signedJar.toURI()); 125 } catch (Throwable t) { 126 throw new RuntimeException (t); 127 } 128 } 129 130 137 private void ensureSignedFileUpToDate() throws KeyStoreException , IllegalArgumentException , Exception { 138 141 if ( ! unsignedJar.exists()) { 142 throw new IllegalArgumentException ( 143 localStrings.getString("jws.sign.noUnsignedJar", unsignedJar.getAbsolutePath())); 144 } 145 if ( ! signedJar.exists() || (signedJar.lastModified() < unsignedJar.lastModified())) { 146 signJar(); 147 } 148 } 149 150 154 private void signJar() throws KeyStoreException , Exception { 155 170 171 ArrayList <String > args = new ArrayList <String >(); 172 args.add(SIGNEDJAR_OPTION); 173 args.add(signedJar.getAbsolutePath()); 174 175 args.add(KEYSTORE_OPTION); 176 args.add(getKeystoreAbsolutePath()); 177 178 args.add(STOREPASS_OPTION); 179 int passwordSlot = args.size(); 180 args.add(getKeystorePassword()); 181 182 args.add(unsignedJar.getAbsolutePath()); 183 184 args.add(getAlias()); 185 long startTime = System.currentTimeMillis(); 186 189 SecurityManager mgr = System.getSecurityManager(); 190 191 try { 192 198 NoExitSecurityManager noExitMgr = new NoExitSecurityManager(mgr); 199 System.setSecurityManager(noExitMgr); 200 201 204 JarSigner.main(args.toArray(new String [args.size()])); 205 } catch (Throwable t) { 206 210 signedJar.delete(); 211 212 216 throw new Exception (localStrings.getString("jws.sign.errorSigning", signedJar.getAbsolutePath()), t); 217 } finally { 218 221 System.setSecurityManager(mgr); 222 223 226 args.set(passwordSlot, null); 227 228 long duration = System.currentTimeMillis() - startTime; 229 logger.fine("Signing " + unsignedJar.getAbsolutePath() + " took " + duration + " ms"); 230 } 231 } 232 233 237 private String getKeystoreAbsolutePath() { 238 if (keystoreAbsolutePath == null) { 239 keystoreAbsolutePath = System.getProperty(KEYSTORE_PATH_PROPERTYNAME); 240 } 241 return keystoreAbsolutePath; 242 } 243 244 248 private String getKeystorePassword() { 249 return SSLUtils.getKeyStorePass(); 250 } 251 252 257 private String getAlias() throws KeyStoreException , Exception { 258 264 KeyStore keystore = SSLUtils.getKeyStore(); 265 266 String alias = System.getProperty(USER_SPECIFIED_ALIAS_PROPERTYNAME); 267 if (alias == null || ! checkUserAlias(keystore, alias)) { 268 273 checkDefaultAlias(keystore); alias = DEFAULT_ALIAS_VALUE; 275 } 276 return alias; 277 } 278 279 286 private void checkDefaultAlias(KeyStore keystore) throws KeyStoreException { 287 if ( ! keystore.containsAlias(DEFAULT_ALIAS_VALUE)) { 288 throw new IllegalStateException (localStrings.getString("jws.sign.defaultAliasAbsent", DEFAULT_ALIAS_VALUE)); 289 } 290 } 291 292 300 private boolean checkUserAlias(KeyStore keystore, String candidateAlias) throws KeyStoreException { 301 boolean result; 302 if ( ! (result = keystore.containsAlias(candidateAlias)) ) { 303 logger.warning(localStrings.getString("jws.sign.userAliasAbsent", candidateAlias)); 304 } 305 return result; 306 } 307 308 311 private class NoExitSecurityManager extends SecurityManager { 312 313 private SecurityManager originalManager; 314 315 public NoExitSecurityManager(SecurityManager originalManager) { 316 this.originalManager = originalManager; 317 } 318 319 public void checkExit(int status) { 320 323 throw new AccessControlException ("System.exit"); 324 } 325 326 public void checkPermission(Permission p) { 327 330 if (originalManager != null) { 331 originalManager.checkPermission(p); 332 } 333 } 334 } 335 } 336 | Popular Tags |