Tuesday Sep 03, 2013

Code signing: Understanding who and when

Code signatures have become more commonplace for applications in recent years and especially so in Java 7 update 21 (April 16, 2013). The security notifications for Java help provide your users with an understanding of who wrote the application so that they can decide whether to run it or not. Through the default settings of the Java security slider, end-users are blocked from running unsigned or self-signed applets and prompted for running signed applets.

These cryptographic signatures are designed to provide a means of authentication, proving to users that they are indeed running the same application that you wrote and that it has not been modified in any way. Within information security threat modeling terms, this is a means of repudiation.

Developers must sign any browser applet or Web Start application that they produce. Signatures on other application types (such as back-end server applications) are optional.

System Administrators may be impacted and have a choice for what to do:

  1. For managed users, the system administrator can add a Deployment Rule Set to client systems and indicate which applets may run.
  2. For unmanaged or bring-your-own-device users, the system administrator may sign jars with their own certificate.

Several steps are involved to sign code:

  1. Determine who in your organization is responsible for code signing
  2. Purchase a signing certificate from a public Certificate Authority.
  3. System Administrators: sign a jar using normal commands
  4. Developers: integrate code signing into your build chain

Determine who in your organization is responsible for code signing

In corporate enterprises, there is often a technology group responsible for these signatures. This group is usually separate from most system administrators in order to keep the private key a secret. The goal is to decrease the number of people with access to the private key.

If you are in a corporate enterprise, please look internally for a code signing group and ask them for their signing process instead.

Purchase a signing certificate

If you or your group is identified as responsible for code signing, then the next step is to obtain a code signing certificate. If a different group provides signatures (as in many large organizations), follow their process instead.

All users (system administrators and developers) that intend to sign code must purchase a signing certificate from a known authority. Certificate authorities help manage trust across millions of systems and prevent identity spoofing, or cases where someone else generates their own certificate pretending to be you. Signing certificates are available from companies like GoDaddy, Symantec, or others. Each certificate authority provides instructions for generating a certificate signing request with the keytool command.

Code signing certificates are different from domain certificates normally used for HTTPS websites. Signing certificates must be purchased from a Certificate Authority -- Oracle is not a Certificate Authority.

System Administrators: sign a jar using normal commands

After receiving the signing certificate, system administrators can sign files using the following jarsigner command:

jarsigner -verbose -keystore "c:\Users\ecostlow\javakeystore_keepsecret.jks" -tsa http://timestamp.verisign.com -signedjar FILENAME.jar FILENAME.jar your-certificate-name
Enter Passphrase for keystore:
 updating: META-INF/MANIFEST.MF
   adding: META-INF/COSTLOW.SF
   adding: META-INF/COSTLOW.RSA
  signing: ruleset.xml

Developers: integrate code signing into your build chain

In an ideal environment, members of the development team would not have access to the private key and its password. The fewer people that have this private key, the better.

For build automation, you can create stubs that allow other members of the team to substitute the real private key and password, without exposing this sensitive information.

Please note that Oracle only maintains the default jarsigner command (described above for System Administrators) and does not manage or control build tools.

Ant

The ant build system provides a SignJar task to mimic the jarsigner command. When using this task, just be sure to call the ant sign target.

<property file="devAndBuildHaveDifferentValues.properties" />
<target name="sign">
	<signjar
		alias="${sign.alias}" keystore="${sign.keystore}"
		storepass="${sign.storepass}" tsaurl="http://timestamp.verisign.com">
	  <path>
		<fileset dir="dist" includes="**/*.jar" />
	  </path>
	</signjar>
</target>

Then, both the development team and build team would have different entries for devAndBuildHaveDifferentValues.properties

 Dev would have this for testing
Build (continuous integration) would have this, keeping it secret
sign.alias=mykey
sign.keystore=testcerts.jks
sign.storepass=changeit
sign.alias=mykey
sign.keystore=/build/keepSecretFromEveryone.jks
sign.storepass=N0bodyE1se5houldKnowThis

By taking these actions, developers can automate their build to sign jar files without exposing the private key to the entire development team.

Maven

Maven provides a maven-jarsigner plugin to mimic the jarsigner command.

<build>
	<plugins>
		...
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jarsigner-plugin</artifactId>
			<version>1.2</version>
			<executions>
				<execution>
					<id>sign</id>
					<goals>
						<goal>sign</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<keystore>${sign.keystore}</keystore>
				<alias>${sign.alias}</alias>
				<storepass>${sign.storepass}</storepass>
			</configuration>
		</plugin>
		...
	</plugins>
</build>

Maven uses the idea of Profiles to externalize properties.

Dev would have this for testing
Build (continuous integration) would have this, keeping it secret
sign.alias=mykey
sign.keystore=testcerts.jks
sign.storepass=changeit
sign.alias=mykey
sign.keystore=c:/build/keepSecretFromEveryone.jks
sign.storepass=N0bodyE1se5houldKnowThis

By taking these actions, developers can automate their build to sign jar files without exposing the private key to the entire development team.

Other build system

If your development team is using an alternate build system, please review its documentation for the appropriate call. Most build systems should have this, but the default jarsigner command will always work if you need.

Leveraging internal code signing authorities

Many large organizations have an internal code signing group. The benefit of these groups is that they help keep the private keys secret in a way that neither developers nor build integrators have access. Separation also allows this group to dedicate more time towards best practices, like keeping keys on tamper-proof hardware/smart-cards. If your organization has this group, the usual practice is to automate sending code artifacts to this team’s service and retrieving the resulting signed artifacts. Reference documentation for using separate hardware key components is available in the PKCS#11 Reference Guide.

A word on timestamps

The optional timestamping provides a notary-like capability of identifying when the signature was applied. This is helpful if your private key is compromised at any point. If a jar was signed before the key was thought to be compromised, the signature is still valid even through the certificate may be revoked. Certificate Authorities should allow back-dating of revocations in case the loss is not noticed right away. If a certificate is revoked and the signed jar is:

  • Timestamped before the stated date of compromise: then the signature is valid because the owner still had sole possession.
  • Timestamped on or after the stated date of compromise: the signature is not valid. Whoever else had the certificate could have faked this.
  • Not timestamped: the signature is not valid. We have no way of knowing when the jar was signed and assume it was after the compromise.

Although not required, it is a good practice to timestamp anything that you sign. Popular timestamping authority URLs include:

  • http://timestamp.verisign.com
  • http://tsa.starfieldtech.com
  • https://timestamp.geotrust.com/tsa
About

Science Duke
This blog contains topics related to Java SE, Java Security and Usability. The target audience is developers, sysadmins and architects that build, deploy and manage Java applications. Contributions come from the Java SE Product Management team.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
8
9
10
11
12
13
14
15
16
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today