As we said earlier, applets were what started the Java craze. In practice, people discovered that although they could write animated applets (like the famous “nervous text”), applets could not do a whole lot of useful stuff in the JDK 1.0 security model. For example, since applets under JDK 1.0 were so
closely supervised, they couldn’t do much good on a corporate intranet, even though relatively little risk attaches to executing an applet from your company’s secure intranet. It quickly became clear to Sun that for applets to become truly useful, users need to be able to assign different levels of security, depending on where the applet originated. If an applet comes from a trusted supplier and has not been tampered with, the user of that applet can decide whether to give the applet more privileges.
To give more trust to an applet, we need to know two things:
- Where did the applet come from?
- Was the code corrupted in transit?
In the past 50 years, mathematicians and computer scientists have developed sophisticated algorithms for ensuring the integrity of data and for creating electronic signatures. The java.security package contains implementations of many of these algorithms. Fortunately, you don’t need to understand the underlying mathematics to use the algorithms in the java.security package. In the next sections, we’ll show you how message digests can detect changes in data files and how digital signatures can prove the identity of the signer.
1. Message Digests
A message digest is a digital fingerprint of a block of data. For example, the so-called SHA-1 (Secure Hash Algorithm #1) condenses any data block, no matter how long, into a sequence of 160 bits (20 bytes). As with real fingerprints, one hopes that no two different messages have the same SHA-1 fingerprint. Of course, that cannot be true—there are only 2160 SHA-1 fingerprints, so there must be some messages with the same fingerprint. But 2160 is so large that the probability of a collision is negligible. How negligible? According to James Walsh in True Odds: How Risks Affect Your Everyday Life (Merritt Publishing, 1996), the chance that you will die from being struck by lightning is about one in 30,000. Now, think of nine other people—for example, your nine least favorite managers or professors. The chance that you and all of them will die from lightning strikes is higher than that of a forged message having the same SHA-1 fingerprint as the original. (Of course, more than ten people, none of whom you are likely to know, will die from lightning strikes. However, we are talking about the far slimmer chance that your particular choice of people will be wiped out.)
A message digest has two essential properties:
- If one bit or several bits of the data are changed, the message digest also changes.
- A forger who is in possession of a given message cannot construct a fake message that has the same message digest as the original.
The second property is, again, a matter of probabilities. Consider the following message by the billionaire father:
“Upon my death, my property shall be divided equally among my children; however, my son George shall receive nothing.”
That message (with a final newline) has an SHA-1 fingerprint of
12 5F 09 03 E7 31 30 19 2E A6 E7 E4 90 43 84 B4 38 99 8F 67
The distrustful father has deposited the message with one attorney and the fingerprint with another. Now, suppose George bribes the lawyer holding the message. He wants to change the message so that Bill gets nothing. Of course, that changes the fingerprint to a completely different bit pattern:
7D F6 AB 08 EB 40 EC CD AB 74 ED E9 86 F9 ED 99 D1 45 B1 57
Can George find some other wording that matches the fingerprint? If he had been the proud owner of a billion computers from the time the Earth was formed, each computing a million messages a second, he would not yet have found a message he could substitute.
A number of algorithms have been designed to compute such message digests. Among them are SHA-1, the secure hash algorithm developed by the National Institute of Standards and Technology, and MD5, an algorithm invented by Ronald Rivest of MIT. Both algorithms scramble the bits of a message in ingenious ways. For details about these algorithms, see, for example, Cryptography and Network Security, Seventh Edition, by William Stallings (Prentice Hall, 2017). However, subtle regularities have been discovered in both algorithms, and NIST recommends to switch to stronger alternatives. Java supports the SHA-2 and SHA-3 sets of algorithms.
The MessageDigest class is a factory for creating objects that encapsulate the fingerprinting algorithms. It has a static method, called getInstance, that returns an object of a class that extends the MessageDigest class. This means the MessageDigest class serves double duty:
- As a factory class
- As the superclass for all message digest algorithms
For example, here is how you obtain an object that can compute SHA fingerprints:
MessageDigest alg = MessageDigest.getInstance(“SHA-1”);
After you have obtained a MessageDigest object, feed it all the bytes in the message by repeatedly calling the update method. For example, the following code passes all bytes in a file to the atg object just created to do the fingerprinting:
InputStream in = …;
white ((ch = in.read()) != -1)
Alternatively, if you have the bytes in an array, you can update the entire array at once:
byte bytes = . . .;
When you are done, call the digest method. This method pads the input as required by the fingerprinting algorithm, does the computation, and returns the digest as an array of bytes.
byte hash = atg.digest();
The program in Listing 10.16 computes a message digest. You can specify the file and algorithm on the command line:
java hash.Digest hash/input.txt SHA-1
If you do not supply command-line arguments, you will be prompted for the file and algorithm name.
2. Message Signing
In the last section, you saw how to compute a message digest—a fingerprint for the original message. If the message is altered, the fingerprint of the altered message will not match the fingerprint of the original. If the message and its fingerprint are delivered separately, the recipient can check whether the message has been tampered with. However, if both the message and the fingerprint were intercepted, it is an easy matter to modify the message and then recompute the fingerprint. After all, the message digest algorithms are publicly known, and they don’t require secret keys. In that case, the recipient of the forged message and the recomputed fingerprint would never know that the message has been altered. Digital signatures solve this problem.
To help you understand how digital signatures work, we’ll explain a few concepts from the field called public key cryptography. Public key cryptography is based on the notion of a public key and private key. The idea is that you tell everyone in the world your public key. However, only you hold the private key, and it is important that you safeguard it and don’t release it to anyone else. The keys are matched by mathematical relationships, though the exact nature of these relationships is not important to us. (If you are interested, look it up in The Handbook of Applied Cryptography at www.cacr.math.uwatertoo.ca/hac.)
The keys are quite long and complex. For example, here is a matching pair of public and private Digital Signature Algorithm (DSA) keys.
p: fca682ce8e12caba26efccf7110e526db078b05edecbcd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e12 ed0899bcd132acd50d99151bdc43ee737592e17
g: 678471b27a9cf44ee91a49c5147db1a9aaf244f05a434d6486931d2d14271b9e35030b71fd73da179069b32e2 935630e1c2062354d0da20a6c416e50be794ca4
y: c0b6e67b4ac098eb1a32c5f8c4c1f0e7e6fb9d832532e27d0bdab9ca2d2a8123ce5a8018b8161a760480fadd0 40b927281ddb22cb9bc4df596d7de4d1b977d50
p: fca682ce8e12caba26efccf7110e526db078b05edecbcd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e12 ed0899bcd132acd50d99151bdc43ee737592e17
g: 678471b27a9cf44ee91a49c5147db1a9aaf244f05a434d6486931d2d14271b9e35030b71fd73da179069b32e2 935630e1c2062354d0da20a6c416e50be794ca4
It is believed to be practically impossible to compute one key from the other. That is, even though everyone knows your public key, they can’t, in your lifetime, compute your private key, no matter how many computing resources they have available.
It may be difficult to believe that the private key can’t be computed from the public key—but nobody has found an algorithm to do this for the encryption algorithms in common use today. If the keys are sufficiently long, brute force—simply trying all possible keys—would require more computers than can be built from all the atoms in the solar system, crunching away for thousands of years. Of course it is possible that someone could come up with algorithms for computing keys that are much more clever than brute force. For example, the RSA algorithm (the encryption algorithm invented by Rivest, Shamir, and Adleman) depends on the difficulty of factoring large numbers. For the last 20 years, many of the best mathematicians have tried to come up with good factoring algorithms, but so far with no success. For that reason, most cryptographers believe that keys with a “modulus” of 2,000 bits or more are currently completely safe from any attack. DSA is believed to be similarly secure.
Figure 10.9 illustrates how the process works in practice.
Suppose Alice wants to send Bob a message, and Bob wants to know this message came from Alice and not an impostor. Alice writes the message and signs the message digest with her private key. Bob gets a copy of her public key. Bob then applies the public key to verify the signature. If the verification passes, Bob can be assured of two facts:
- The original message has not been altered.
- The message was signed by Alice, the holder of the private key that matches the public key that Bob used for verification.
You can see why the security of private keys is so important. If someone steals Alice’s private key, or if a government can require her to turn it over, then she is in trouble. The thief or a government agent can now impersonate her by sending messages, such as money transfer instructions, that others will believe came from Alice.
3. Verifying a Signature
The JDK comes with the keytoot program, which is a command-line tool to generate and manage a set of certificates. We expect that ultimately the functionality of this tool will be embedded in other, more user-friendly programs. But right now, we’ll use keytoot to show how Alice can sign a document and send it to Bob, and how Bob can verify that the document really was signed by Alice and not an impostor.
The keytoot program manages keystores—databases of certificates and private/ public key pairs. Each entry in the keystore has an alias. Here is how Alice creates a keystore, atice.certs, and generates a key pair with alias atice:
keytoot -genkeypair -keystore atice.certs -alias atice
When creating or opening a keystore, you are prompted for a keystore password. For this example, just use secret. If you were to use the keytoot-generated keystore for any serious purpose, you would need to choose a good password and safeguard this file.
When generating a key, you are prompted for the following information:
Enter keystore password: secret
Reenter new password: secret
What is your first and last name?
[Unknown]: Alice Lee
What is the name of your organizational unit?
What is the name of your organization?
[Unknown]: ACME Software
What is the name of your City or Locality?
[Unknown]: San Francisco
What is the name of your State or Province?
What is the two-letter country code for this unit?
Is <CN=Alice Lee, OU=Engineering, O=ACME Software, L=San Francisco, ST=CA, C=US> correct? [no]: yes
The keytool uses names in the X.500 format, whose components are Common Name (CN), Organizational Unit (OU), Organization (O), Location (L), State (ST), and Country (C), to identify key owners and certificate issuers.
Finally, specify a key password, or press Enter to use the keystore password as the key password.
Suppose Alice wants to give her public key to Bob. She needs to export a certificate file:
keytool -exportcert -keystore alice.certs -alias alice -file alice.cer
Now Alice can send the certificate to Bob. When Bob receives the certificate, he can print it:
keytool -printcert -file alice.cer
The printout looks like this:
Owner: CN=Alice Lee, OU=Engineering, O=ACME Software, L=San Francisco, ST=CA, C=US
Issuer: CN=Alice Lee, OU=Engineering, O=ACME Software, L=San Francisco, ST=CA, C=US
Serial number: 470835ce
Valid from: Sat Oct 06 18:26:38 PDT 2007 until: Fri Jan 04 17:26:38 PST 2008
Signature algorithm name: SHA1withDSA
If Bob wants to check that he got the right certificate, he can call Alice and verify the certificate fingerprint over the phone.
Once Bob trusts the certificate, he can import it into his keystore. keytool -importcert -keystore bob,certs -alias alice -file alice,cer
Now Alice can start sending signed documents to Bob. The jarsigner tool signs and verifies JAR files. Alice simply adds the document to be signed into a JAR file.
jar cvf documentjar document,txt
She then uses the jarsigner tool to add the signature to the file. She needs to specify the keystore, the JAR file, and the alias of the key to use.
jarsigner -keystore alice,certs documentjar alice
When Bob receives the file, he uses the -verify option of the jarsigner program.
jarsigner -verify -keystore bob,certs document,jar
Bob does not need to specify the key alias. The jarsigner program finds the X.500 name of the key owner in the digital signature and looks for a matching certificate in the keystore.
If the JAR file is not corrupted and the signature matches, the jarsigner program prints
Otherwise, the program displays an error message.
4. The Authentication Problem
Suppose you get a message from your friend Alice, signed with her private key, using the method we just showed you. You might already have her public key, or you can easily get it by asking her for a copy or by getting it from her web page. Then, you can verify that the message was in fact authored by Alice and has not been tampered with. Now, suppose you get a message from a stranger who claims to represent a famous software company, urging you to run a program attached to the message. The stranger even sends you a copy of his public key so you can verify that he authored the message. You check that the signature is valid. This proves that the message was signed with the matching private key and has not been corrupted.
Be careful: You still have no idea who wrote the message. Anyone can generate a pair of public and private keys, sign the message with the private key, and send the signed message and the public key to you. The problem of determining the identity of the sender is called the authentication problem.
The usual way to solve the authentication problem is simple. Suppose the stranger and you have a common acquaintance you both trust. Suppose the stranger meets your acquaintance in person and hands over a disk with the public key. Your acquaintance later meets you, assures you that he met the stranger and that the stranger indeed works for the famous software company, and then gives you the disk (see Figure 10.10). That way, your acquaintance vouches for the authenticity of the stranger.
In fact, your acquaintance does not actually need to meet you. Instead, he can use his private key to sign the stranger’s public key file (see Figure 10.11).
When you get the public key file, you verify the signature of your friend, and because you trust him, you are confident that he did check the stranger’s credentials before applying his signature.
However, you might not have a common acquaintance. Some trust models assume that there is always a “chain of trust”—a chain of mutual acquaintances—so that you trust every member of that chain. In practice, of course, that isn’t always true. You might trust your friend, Alice, and you know that Alice trusts Bob, but you don’t know Bob and aren’t sure that you trust him. Other trust models assume that there is a benevolent big brother—a company in which we all trust. Companies with confidence-inspiring names such as DigiCert, GlobalSign, and Entrust provide verification services.
You will often encounter digital signatures signed by one or more entities who will vouch for the authenticity, and you will need to evaluate to what degree you trust the authenticators. You might place a great deal of trust in a particular certificate authority, perhaps because you saw their logo on many web pages or because you heard that they require multiple people with black attache cases to come together into a secure chamber whenever new master keys are to be minted.
However, you should have realistic expectations about what is actually being authenticated. You can get a “class 1” ID simply by filling out a web form and paying a small fee. The key is mailed to the e-mail address included in the certificate. Thus, you can be reasonably assured that the e-mail address is genuine, but the requestor could have filled in any name and organization. There are more stringent classes of IDs. For example, with a “class 3” ID, the certificate authority will require an individual requestor to appear before a notary public, and it will check the financial rating of a corporate requestor. Other authenticators will have different procedures. Thus, when you receive an authenticated message, it is important that you understand what, in fact, is being authenticated.
5. Certificate Signing
In Section 10.4.3, “Verifying a Signature,” on p. 589 you saw how Alice used a self-signed certificate to distribute a public key to Bob. However, Bob needed to ensure that the certificate was valid by verifying the fingerprint with Alice.
Suppose Alice wants to send her colleague Cindy a signed message, but Cindy doesn’t want to bother with verifying lots of signature fingerprints. Now suppose there is an entity that Cindy trusts to verify signatures. In this example, Cindy trusts the Information Resources Department at ACME Software.
That department operates a certificate authority (CA). Everyone at ACME has the CA’s public key in their keystore, installed by a system administrator who carefully checked the key fingerprint. The CA signs the keys of ACME employees. When they install each other’s keys, the keystore will trust them implicitly because they are signed by a trusted key.
Here is how you can simulate this process. Create a keystore acmesoft.certs. Generate a key pair and export the public key:
keytoot -genkeypair -keystore acmesoft.certs -alias acmeroot
keytoot -exportcert -keystore acmesoft.certs -alias acmeroot -file acmeroot.cer
The public key is exported into a “self-signed” certificate. Then, add it to every employee’s keystore:
keytoot -importcert -keystore cindy.certs -alias acmeroot -file acmeroot.cer
For Alice to send messages to Cindy and to everyone else at ACME Software, she needs to bring her certificate to the Information Resources Department and have it signed. Unfortunately, this functionality is missing in the keytoo! program. In the book’s companion code, we supply a CertificateSigner class to fill the gap. An authorized staff member at ACME Software would verify Alice’s identity and generate a signed certificate as follows:
java CertificateSigner -keystore acmesoft.certs -alias acmeroot \
-infile atice.cer -outfite atice_signedby_acmeroot.cer
The certificate signer program must have access to the ACME Software key- store, and the staff member must know the keystore password. Clearly, this is a sensitive operation.
Alice gives the file a!ice_signedby_acmeroot.cer to Cindy and to anyone else in ACME Software. Alternatively, ACME Software can simply store the file in a company directory. Remember, this file contains Alice’s public key and an assertion by ACME Software that this key really belongs to Alice.
Now Cindy imports the signed certificate into her keystore:
keytoot -importcert -keystore cindy.certs -alias atice -file a!ice_signedby_acmeroot.cer
The keystore verifies that the key was signed by a trusted root key that is already present in the keystore. Cindy is not asked to verify the certificate fingerprint.
Once Cindy has added the root certificate and the certificates of the people who regularly send her documents, she never has to worry about the keystore again.
6. Certificate Requests
In the preceding section, we simulated a CA with a keystore and the CertificateSigner tool. However, most CAs run more sophisticated software to manage certificates, and they use slightly different formats for certificates. This section shows the added steps required to interact with those software packages.
We will use the OpenSSL software package as an example. The software is preinstalled on many Linux systems and Mac OS X, and a Cygwin port for Windows is also available. You can download the software at www.opensst.org.
To create a CA, run the CA script. The exact location depends on your operating system. On Ubuntu, run
This script creates a subdirectory called demoCA in the current directory. The directory contains a root key pair and storage for certificates and certificate revocation lists.
You will want to import the public key into the Java keystores of all employees, but it is in the Privacy Enhanced Mail (PEM) format, not the DER format that the keystore accepts easily. Copy the file demoCA/cacert.pem to a file acmeroot.pem and open that file in a text editor. Remove everything before the line
…. BEGIN CERTIFICATE
and after the line
…. END CERTIFICATE
Now you can import acmeroot.pem into each keystore in the usual way:
keytool -importcert -keystore cindy.certs -alias alice -file acmeroot.pem
It seems quite incredible that the keytool cannot carry out this editing operation itself.
To sign Alice’s public key, start by generating a certificate request that contains the certificate in the PEM format:
keytool -certreq -keystore alice.store -alias alice -file alice.pem
To sign the certificate, run
openssl ca -in alice.pem -out alice_signedby_acmeroot.pem
As before, cut out everything outside the BEGIN CERTIFICATE/END CERTIFICATE markers from alice_signedby_acmeroot.pem. Then import it into the keystore:
keytool -importcert -keystore cindy.certs -alias alice -file alice_signedby_acmeroot.pem
You can use the same steps to have a certificate signed with a key that is issued by a certificate authority.
7. Code Signing
A common use of authentication technology is signing executable programs. If you download a program, you are naturally concerned about the damage it can do. For example, the program could have been infected by a virus. If you know where the code comes from and that it has not been tampered with since it left its origin, your comfort level will be a lot higher than without this knowledge.
In this section, we’ll show you how to sign JAR files, and how you can configure Java to verify the signature. This capability was designed for applets and Java Web Start applications. These are no longer commonly used technologies, but you may still need to support them in legacy products.
When Java was first released, applets ran in the “sandbox,” with limited permissions, as soon as they were loaded. If users wanted to use applets that can access the local file system, make network connections, and so on, they had to explicitly agree. To ensure that the applet code was not tampered with in transit, it had to be digitally signed.
Here is a specific example. Suppose that while surfing the Internet, you encounter a web site that offers to run an applet from an unfamiliar vendor, provided you grant it the permission to do so (see Figure 10.12). Such a program is signed with a software developer certificate issued by a certificate authority that the Java runtime trusts. The pop-up dialog box identifies the software developer and the certificate issuer. Now you need to decide whether to authorize the program.
What facts do you have at your disposal that might influence your decision? Here is what you know:
- Thawte sold a certificate to the software developer.
- The program really was signed with that certificate, and it hasn’t been modified in transit.
- That certificate really was signed by Thawte—it was verified by the public key in the local cacerts file.
Of course, none of this tells you whether the code is safe to run. Can you trust a vendor if all you know is the vendor’s name and the fact that Thawte sold them a software developer certificate? This approach never made much sense.
For intranet deployment, certificates are more plausible. Administrators can install policy files and certificates on local machines so that no user interaction is required for launching trusted code. Whenever the Java plug-in tool loads signed code, it consults the policy file for the permissions and the keystore for signatures.
For the remainder of this section, we will describe how you can build policy files that grant specific permissions to trusted code.
Suppose ACME Software wants its employees to run certain programs that require local file access, and it wants to deploy these programs through a browser as Web Start applications.
As you saw earlier in this chapter, ACME could identify the programs by their code base. But that means ACME would need to update the policy files each time the programs are moved to a different web server. Instead, ACME decides to sign the JAR files that contain the program code.
First, ACME generates a root certificate:
keytool -genkeypair -keystore acmesoft.certs -alias acmeroot
Of course, the keystore containing the private root key must be kept in a safe place. Therefore, we create a second keystore client.certs for the public certificates and add the public acmeroot certificate into it.
keytool -exportcert -keystore acmesoft.certs -alias acmeroot -file acmeroot.cer
keytool -importcert -keystore client.certs -alias acmeroot -file acmeroot.cer
A trusted person at ACME runs the jarsigner tool to sign any application that they wish to sign, specifying the JAR file and the alias of the private key:
jarsigner -keystore acmesoft.certs ACMEApp.jar acmeroot
The signed Web Start application is now ready to be deployed on a web server.
Next, let us turn to the client machine configuration. A policy file must be distributed to each client machine.
To reference a keystore, a policy file starts with the line
keystore “keystoreURL”, “keystoreType”;
The URL can be absolute or relative. Relative URLs are relative to the location of the policy file. The type is JKS if the keystore was generated by keytool. For example,
keystore “client.certs”, “JKS”;
Then grant clauses can have suffixes signedBy “alias”, such as this one:
grant signedBy “acmeroot“
Any signed code that can be verified with the public key associated with the alias is now granted the permissions inside the grant clause.
Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.