| ★ wanayoo — archive 1999 http://java.sun.com/security/signExample12/ | Nouvelle recherche | Portail wanayoo |
This example shows how a signed applet may be run in two different environments.
Note that the Plug-in is already bundled with the Windows version of JRE 1.2, i.e., there is no separate download for the Plug-in for Windows: When you install the Windows version of JRE 1.2, the Plug-in gets installed automatically.
On Solaris, you need to install the Plug-in separately, however.
Here's the source code for an applet that
wants to do just that. It tries to write to the file
/tmp/foo (or C:\tmpfoo, if you're on a
Windows system.) This is not a malicious applet, but it is trying to
do something that the Java Applet Security Manager prevents it from
doing.
Visit this URL:
http://java.sun.com/security/signExample12/signedPluginEx.htmlfrom your browser.
On the signedPluginEx.html page, the applet display should
read "writeFile: caught security exception." This is a good thing, and
the expected behavior. A security exception doesn't mean that a
security violation occurred - it means exactly the opposite. It means
that the system caught the applet trying to escape the restrictions of
the applet sandbox. To alert you to that fact, the system throws an
exception. This is programmer terminology for "the system set off a
warning bell." When you see a security exception on a Java system, you
know that the system prevented the code from violating security.
Note that a "Java Console" window should have popped up from your browser, and it should have displayed some output, including the location of the User home directory. This is important information that you should remember for the next step.
signedWriteFile.jar. If you get a
copy of Duke's certificate and import it into your system's keystore
(by which you declare Duke's public key to be trusted) and configure
your system policy to grant code signed by Duke and downloaded from
the Java Software website to write to the specified file, then the
applet will be allowed to create a file on your local filesystem.
Here are the steps you need to take to accomplish that.
Duke.x509
% keytool -import -alias Duke -file Duke.x509
The policy files were generated automatically using policytool.
http://java.sun.com/security/signExample12/signedPluginEx.html
You might wonder, what does the HTML tag look like, for a signed archive loaded by a plugin? The HTML is complicated, but, you don't need to write this HTML by hand. When you downloaded the Java Plug-In, you also could download a tool called the Java Plug-In HTML Converter. It's very easy to run. Once you've installed it, invoke the simple GUI tool:
% java HTMLConverterType in the name of the HTML file you need to convert so that it can be loaded by the Plug-In, and click on the "Convert" button.
That's all there is to it. For more details on the object HTML tag, see the Java Plug-in HTML Specification.
Once you've downloaded JDK 1.2 and installed the software on your client, we're ready to go. Make sure that the JDK tools are accessible from your search path. We'll need to use these tools:
keytool, the tool for generating keys
appletviewer, a stripped-down browser for viewing
the applet
Here's the source code for an applet that
wants to do just that. It tries to write to the file
/tmp/foo (or C:\tmpfoo, if you're on a
Windows system.) This is not a malicious applet, but it is trying to
do something that the Java Applet Security Manager prevents it from
doing.
You can see what happens when you try to run this applet by typing
this command in a shell window (or in an MS-DOS prompt window:)
% appletviewer http://java.sun.com/security/signExample12/signedWriteFile.html
You should see that the system throws a security exception. This is a
good thing, and the expected behavior. A security exception doesn't
mean that a security violation occurred - it means exactly the
opposite. It means that the system caught the applet trying to escape
the restrictions of the applet sandbox. To alert you to that fact,
the system threw an exception. This is programmer terminology for
"the system set off a warning bell." When you see a security
exception on a Java system, you know that the system prevented the
code from violating security.
signedWriteFile.jar. If you get a
copy of Duke's certificate and import it into your system's keystore
(by which you declare Duke's public key to be trusted) and configure
your system policy to grant code signed by Duke and downloaded from
the Java Software website to write to the specified file, then the
applet will be allowed to create a file on your local filesystem.
Here's the steps you need to take to accomplish that.
Duke.x509
% keytool -import -alias Duke -file Duke.x509
Both files were generated automatically using policytool.
% appletviewer http://java.sun.com/security/signExample12/signedWriteFile.html or % appletviewer -J-Djava.security.policy="policy_file_location" http://java.sun.com/security/signExample12/signedWriteFile.html
You'll notice that the applet can now create and write a file on your local filesystem.
You might wonder, what does the HTML tag look like, for a signed archive?
<applet code=writeFile.class archive="signedWriteFile.jar" width=500 height=50> </applet>
That's all there is to it. You simply tell the browser the name of
the archive, using the archive= attribute. For more
details on the applet HTML tag, see the JAR
Guide.
To create your own signed JAR file, you need to run these additional tools:
jar, the tool for creating an archive
jarsigner, the tool for signing archives
doit: A script that creates a Duke identity with a signature keypair and certificate, and then creates an archive and signs it. It prints out some diagnostic information, such as the contents of the signed archive, and the contents of the keystore.
For more information, refer to the documentation on our main security page for JDK 1.2, http://java.sun.com/products/jdk/1.2/docs/guide/security. In particular you might be interested in the Summary of JDK 1.2 Security Tools.
java-security@sun.com Last modified: Fri Feb 5 14:26:11 PST 1999