001: /*
002:
003: Derby - Class org.apache.derby.impl.services.reflect.JarFileJava2
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.reflect;
023:
024: import org.apache.derby.iapi.reference.MessageId;
025: import org.apache.derby.iapi.error.StandardException;
026: import org.apache.derby.iapi.services.i18n.MessageService;
027:
028: import java.util.zip.ZipEntry;
029: import java.util.zip.ZipInputStream;
030: import java.io.IOException;
031: import java.io.File;
032: import java.io.InputStream;
033:
034: // below are all Java2 imports.
035: import java.security.cert.Certificate;
036: import java.security.cert.X509Certificate;
037: import java.security.GeneralSecurityException;
038:
039: /**
040: Sub-class of JarFile for a Java2 environment that uses the
041: java.util.jar.* classes to be signature aware.
042: */
043:
044: final class JarFileJava2 extends JarFile {
045:
046: JarFileJava2() {
047: super ();
048: }
049:
050: JarFileJava2(String[] name) {
051: super (name);
052: }
053:
054: JarFile newJarFile(String[] name) {
055: return new JarFileJava2(name);
056: }
057:
058: void initialize(File jarFile) throws IOException {
059:
060: java.util.jar.JarFile jf = new java.util.jar.JarFile(jarFile);
061:
062: // determine if it is signed.
063: zip = jf;
064: }
065:
066: ZipEntry getEntry(String entryName) {
067: return ((java.util.jar.JarFile) zip).getJarEntry(entryName);
068: }
069:
070: ZipInputStream getZipOnStream(InputStream in) throws IOException {
071: return new java.util.jar.JarInputStream(in);
072: }
073:
074: ZipEntry getNextEntry(ZipInputStream in) throws IOException {
075: return ((java.util.jar.JarInputStream) in).getNextJarEntry();
076: }
077:
078: byte[] readData(ZipEntry ze, InputStream in, String className)
079: throws IOException {
080: try {
081: return super .readData(ze, in, className);
082: } catch (SecurityException se) {
083: throw handleException(se, className);
084: }
085: }
086:
087: Object[] getSigners(String className, ZipEntry ze)
088: throws IOException {
089: Exception e;
090:
091: try {
092: Certificate[] list = ((java.util.jar.JarEntry) ze)
093: .getCertificates();
094: if ((list == null) || (list.length == 0)) {
095: return null;
096: }
097:
098: for (int i = 0; i < list.length; i++) {
099: if (!(list[i] instanceof X509Certificate)) {
100: String msg = MessageService.getTextMessage(
101: MessageId.CM_UNKNOWN_CERTIFICATE,
102: className, getJarName());
103:
104: throw new SecurityException(msg);
105: }
106:
107: X509Certificate cert = (X509Certificate) list[i];
108:
109: cert.checkValidity();
110: }
111:
112: return list;
113:
114: } catch (GeneralSecurityException gse) {
115: // convert this into an unchecked security
116: // exception. Unchecked as eventually it has
117: // to pass through a method that's only throwing
118: // ClassNotFoundException
119: e = gse;
120: }
121: throw handleException(e, className);
122: }
123:
124: private SecurityException handleException(Exception e,
125: String className) {
126: String msg = MessageService.getTextMessage(
127: MessageId.CM_SECURITY_EXCEPTION, className,
128: getJarName(), e.getLocalizedMessage());
129: return new SecurityException(msg);
130: }
131: }
|