Clean implementation of liveconnect.

r=edburns
author=Nikolay
This commit is contained in:
edburns%acm.org 2001-07-18 20:48:17 +00:00
parent 830f617bd4
commit 8d465ac384
5 changed files with 650 additions and 2 deletions

View File

@ -15,7 +15,7 @@
# Portions created by Sun Microsystems Inc are Copyright (C) 2001
# All Rights Reserved.
#
# $Id: FileListExt.mk,v 1.2 2001/07/12 19:57:37 edburns%acm.org Exp $
# $Id: FileListExt.mk,v 1.3 2001/07/18 20:48:15 edburns%acm.org Exp $
#
# Contributor(s):
#
@ -26,4 +26,9 @@ FILES_java =\
sun/jvmp/mozilla/MozillaAppletPeer.java \
sun/jvmp/mozilla/MozillaHostObjectPeer.java \
sun/jvmp/mozilla/MozillaPeerFactory.java \
sun/jvmp/mozilla/MozillaSecurityManager.java
sun/jvmp/mozilla/MozillaSecurityManager.java \
sun/jvmp/mozilla/JSObject.java \
sun/jvmp/mozilla/SecureInvocation.java \
sun/jvmp/mozilla/JavaScriptProtectionDomain.java \
sun/jvmp/mozilla/JSPermission.java

View File

@ -0,0 +1,373 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is The Waterfall Java Plugin Module
*
* The Initial Developer of the Original Code is Sun Microsystems Inc
* Portions created by Sun Microsystems Inc are Copyright (C) 2001
* All Rights Reserved.
*
* $Id: JSObject.java,v 1.1 2001/07/18 20:48:17 edburns%acm.org Exp $
*
* Contributor(s):
*
* Nikolay N. Igotti <nikolay.igotti@Sun.Com>
*/
package sun.jvmp.mozilla;
import sun.jvmp.PluggableJVM;
import sun.jvmp.applet.*;
import java.security.*;
import netscape.javascript.JSException;
import java.net.URL;
public class JSObject extends netscape.javascript.JSObject {
private int nativeJSObject = 0;
private int jsThreadID = 0;
private long m_params = 0;
/* this field used to perform static calls - it is inited from constructor
of MozillaPeerFactory with pointer to BrowserSupportWrapper */
static long m_evaluator = 0;
// just for reflection
JSObject(Long params) {
this(params.longValue());
}
JSObject(long params) {
this(JSGetThreadID(params), JSGetNativeJSObject(params));
m_params = params;
}
JSObject(int jsThreadID, int nativeJSObject) {
this.jsThreadID = jsThreadID;
this.nativeJSObject = nativeJSObject;
}
public Object call(String methodName,
Object args[])
throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
return JSObjectCall(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
methodName, args,
ctx.getAccessControlContext());
}
public Object eval(String s) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
return JSObjectEval(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
s,
ctx.getAccessControlContext());
}
public Object getMember(String name) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
return JSObjectGetMember(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
name,
ctx.getAccessControlContext());
}
public void setMember(String name, Object value) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
JSObjectSetMember(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
name,
value,
ctx.getAccessControlContext());
}
public void removeMember(String name) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
JSObjectRemoveMember(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
name,
ctx.getAccessControlContext());
}
public Object getSlot(int index) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
return JSObjectGetSlot(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
index,
ctx.getAccessControlContext());
}
public void setSlot(int index, Object value) throws JSException
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
JSObjectSetSlot(jsThreadID,
nativeJSObject,
m_params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
index,
value,
ctx.getAccessControlContext());
}
public String toString()
{
return JSObjectToString(jsThreadID, nativeJSObject, m_params);
}
public void finalize()
throws Throwable
{
JSFinalize(jsThreadID, nativeJSObject, m_params);
super.finalize();
}
private static int JSGetNativeJSObject(long params)
{
WFSecurityContext ctx = WFSecurityContext.getCurrentSecurityContext();
return JSGetNativeJSObject(params,
ctx.getURL(),
ctx.getCertChain(),
ctx.getCertLength(),
ctx.getNumOfCert(),
ctx.getAccessControlContext());
}
private static native int JSGetNativeJSObject(long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
AccessControlContext ctx);
private static native int JSGetThreadID(long params);
private static native void JSFinalize(int jsThreadID,
int nativeJSObject,
long params);
private static native Object JSObjectCall(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength, int numOfCerts,
String methodName,
Object args[],
AccessControlContext ctx)
throws JSException;
private static native Object JSObjectEval(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
String script,
AccessControlContext ctx)
throws JSException;
private static native Object JSObjectGetMember(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
String name,
AccessControlContext ctx)
throws JSException;
private static native void JSObjectSetMember(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
String name,
Object value,
AccessControlContext ctx)
throws JSException;
private static native void JSObjectRemoveMember(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
String name,
AccessControlContext ctx)
throws JSException;
private static native Object JSObjectGetSlot(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
int index,
AccessControlContext ctx)
throws JSException;
private static native void JSObjectSetSlot(int jsThreadID,
int nativeJSObject,
long params,
String url,
byte[][] chain,
int[] certLength,
int numOfCerts,
int index,
Object value,
AccessControlContext ctx)
throws JSException;
private static native String JSObjectToString(int jsThreadID,
int nativeJSObject,
long params);
}
class WFSecurityContext
{
private ProtectionDomain domain;
private AccessControlContext ctx;
WFSecurityContext(ProtectionDomain domain, AccessControlContext ctx)
{
this.domain = domain;
this.ctx = ctx;
}
String getURL()
{
if (domain != null)
{
CodeSource src = domain.getCodeSource();
URL u = src.getLocation();
if (u != null) return u.toString();
}
return null;
}
byte[][] getCertChain()
{
return null;
}
int[] getCertLength()
{
return null;
}
int getNumOfCert()
{
return 0;
}
AccessControlContext getAccessControlContext()
{
return ctx;
}
static WFSecurityContext getCurrentSecurityContext()
{
final AccessControlContext ctx = AccessController.getContext();
try {
return (WFSecurityContext)
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws PrivilegedActionException
{
SecurityManager sm = System.getSecurityManager();
// do anything only if installed SM is subclass
// of our applet security manager
if (sm != null && sm instanceof sun.jvmp.applet.WFAppletSecurityManager)
{
WFAppletSecurityManager m = (WFAppletSecurityManager)sm;
Class[] stack = m.getExecutionStackContext();
for (int i=0; i < stack.length; i++)
{
ClassLoader loader = stack[i].getClassLoader();
if (loader !=null &&
loader instanceof sun.jvmp.applet.WFAppletClassLoader)
return new WFSecurityContext(stack[i].getProtectionDomain(),
ctx);
}
}
return new WFSecurityContext(null, ctx);
}
});
} catch (PrivilegedActionException e) {
PluggableJVM.trace(e, PluggableJVM.LOG_WARNING);
return new WFSecurityContext(null, ctx);
}
}
}

View File

@ -0,0 +1,47 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is The Waterfall Java Plugin Module
*
* The Initial Developer of the Original Code is Sun Microsystems Inc
* Portions created by Sun Microsystems Inc are Copyright (C) 2001
* All Rights Reserved.
*
* $Id: JSPermission.java,v 1.1 2001/07/18 20:48:17 edburns%acm.org Exp $
*
* Contributor(s):
*
* Nikolay N. Igotti <nikolay.igotti@Sun.Com>
*/
package sun.jvmp.mozilla;
public final class JSPermission extends java.security.BasicPermission
{
public JSPermission(String name)
{
super(name);
}
public JSPermission(String name, String actions)
{
super(name, actions);
}
public static final String AllJavaPermission = "AllJavaPermission";
public static final String AllJavaScriptPermission = "AllJavaScriptPermission";
}

View File

@ -0,0 +1,95 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is The Waterfall Java Plugin Module
*
* The Initial Developer of the Original Code is Sun Microsystems Inc
* Portions created by Sun Microsystems Inc are Copyright (C) 2001
* All Rights Reserved.
*
* $Id: JavaScriptProtectionDomain.java,v 1.1 2001/07/18 20:48:17 edburns%acm.org Exp $
*
* Contributor(s):
*
* Nikolay N. Igotti <nikolay.igotti@Sun.Com>
*/
package sun.jvmp.mozilla;
import java.security.cert.Certificate;
import java.security.*;
import java.net.URL;
public class JavaScriptProtectionDomain extends ProtectionDomain
{
// just pointer to native JS security context wrapper
long securityContext = 0;
public JavaScriptProtectionDomain(long securityContext)
{
super(getCodeSource(securityContext), null);
this.securityContext = securityContext;
}
public boolean implies(Permission permission)
{
if (securityContext == 0) return false;
if (permission instanceof JSPermission)
return implies(securityContext,
permission.getActions(),
null);
else
return implies(securityContext,
JSPermission.AllJavaPermission,
null);
}
protected static CodeSource getCodeSource(long ctx)
{
URL u;
try {
u = new URL(getCodeBase(ctx));
} catch (Exception e) {
u = null;
//System.err.println("cannot create JS code source: "+e);
}
return new CodeSource(u, getCerts(ctx));
}
private static Certificate[] getCerts(long ctx)
{
return null; // for now
}
public void finalize() throws Throwable
{
// release proper native object
if (securityContext != 0) finalize(securityContext);
}
private native static String getCodeBase(long ctx);
private native static byte[][] getRawCerts(long ctx);
private native boolean implies(long securityContext,
String target,
String action);
private native void finalize(long securityContext);
}

View File

@ -0,0 +1,128 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is The Waterfall Java Plugin Module
*
* The Initial Developer of the Original Code is Sun Microsystems Inc
* Portions created by Sun Microsystems Inc are Copyright (C) 2001
* All Rights Reserved.
*
* $Id: SecureInvocation.java,v 1.1 2001/07/18 20:48:17 edburns%acm.org Exp $
*
* Contributor(s):
*
* Nikolay N. Igotti <nikolay.igotti@Sun.Com>
*/
package sun.jvmp.mozilla;
import java.security.*;
import java.lang.reflect.*;
import sun.jvmp.PluggableJVM;
public class SecureInvocation {
public static Object ConstructObject(final Constructor constructor,
final Object[] args,
long handle) throws Exception
{
ProtectionDomain[] d = new ProtectionDomain[1];
d[0] = new JavaScriptProtectionDomain(handle);
AccessControlContext context = new AccessControlContext(d);
try {
// Perform the object constructor.
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception
{
return constructor.newInstance(args);
}
});
} catch (PrivilegedActionException e) {
PluggableJVM.trace(e, PluggableJVM.LOG_WARNING);
throw e;
}
// never be here
}
public static Object CallMethod(final Object obj,
final Method method,
final Object[] args,
long handle) throws Exception
{
ProtectionDomain[] d = new ProtectionDomain[1];
d[0] = new JavaScriptProtectionDomain(handle);
AccessControlContext context = new AccessControlContext(d);
try {
// Perform the object constructor.
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception
{
return method.invoke(obj, args);
}
});
} catch (PrivilegedActionException e) {
PluggableJVM.trace(e, PluggableJVM.LOG_WARNING);
throw e;
}
// never be here
}
public static Object GetField(final Object obj,
final Field field,
long handle) throws Exception
{
ProtectionDomain[] d = new ProtectionDomain[1];
d[0] = new JavaScriptProtectionDomain(handle);
AccessControlContext context = new AccessControlContext(d);
try {
// Perform the object constructor.
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception
{
return field.get(obj);
}
});
} catch (PrivilegedActionException e) {
PluggableJVM.trace(e, PluggableJVM.LOG_WARNING);
throw e;
}
// never be here
}
public static void SetField(final Object obj,
final Field field,
final Object val,
long handle) throws Exception
{
ProtectionDomain[] d = new ProtectionDomain[1];
d[0] = new JavaScriptProtectionDomain(handle);
AccessControlContext context = new AccessControlContext(d);
try {
// Perform the object constructor.
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception
{
field.set(obj, val);
return null;
}
});
} catch (PrivilegedActionException e) {
PluggableJVM.trace(e, PluggableJVM.LOG_WARNING);
throw e;
}
// never be here
}
}