Java KeyStores—the gory details
Tags: java, cryptography, keystore
Published: 2017-11-18 17:54:22

---
title: Java KeyStores—the gory details
published: true
description: Deep-dive into how Java KeyStores encrypt the keys on disk
tags: java, cryptography, keystore
cover_image: https://openclipart.org/image/800px/svg_to_png/277321/Safe.png&disposition=attachment
---

*Originally posted on my [blog](https://neilmadden.wordpress.com/2017/11/17/java-keystores-the-gory-details/)*

Java KeyStores are used to store key material and associated certificates in an encrypted and integrity protected fashion. Like all things Java, this mechanism is pluggable and so there exist a variety of different options. There are lots of articles out there that describe the different types and how you can initialise them, load keys and certificates, etc. However, there is a lack of detailed technical information about exactly how these keystores store and protect your key material. This post attempts to gather those important details in one place for the most common KeyStores.

Each key store has an overall password used to protect the entire store, and can optionally have per-entry passwords for each secret- or private-key entry (if your backend supports it).
<h2>Java Key Store (JKS)</h2>
The original Sun JKS (Java Key Store) format is a proprietary binary format file that can only store asymmetric private keys and associated X.509 certificates.

Individual private key entries are protected with <a href="http://www.docjar.com/docs/api/sun/security/provider/KeyProtector.html">a simple home-spun stream cipher</a>—basically the password is salted (160-bits) and hashed with SHA-1 in a trivial chained construction until it has generated enough output bytes to XOR into the private key. It then stores a simple authenticator tag consisting of SHA-1(password + private key bytes) — that's the unencrypted private key bytes. In other words, this is an <a href="https://en.wikipedia.org/wiki/Authenticated_encryption#Encrypt-and-MAC_.28E.26M.29">Encrypt-and-MAC</a> scheme with homespun constructions both based on simple prefix-keyed SHA-1. (This scheme has OID 1.3.6.1.4.1.42.2.17.1.1).

The whole archive is again integrity protected by a home-spun prefix keyed hash construction, consisting of the SHA1 hash of the UTF-16 bytes of the raw keystore password, followed by the UTF-8 bytes of the phrase "Mighty Aphrodite" (I'm not kidding) followed by the bytes of the encoded key store entries.

If every part of this description has not got you screaming at your screen in equal parts terror and bemusement, then you probably haven't fully grasped how awful this is. Don't use it, even for just storing certificates — it's tampering resistance is if anything even worse than the encryption.
<h2>JCE Key Store (JCEKS)</h2>
Sun later updated the cryptographic capabilities of the JVM with the Java Cryptography Extensions (JCE). With this they also introduced a new proprietary key store format: JCEKS.

JCEKS uses "PBEWithMD5AndTripleDES" to encrypt individual key entries, with a 64-bit random salt and 200,000 iterations of PBKDF1 to derive the key. TripleDES is used with 3 keys ("encrypt-decrypt-encrypt") in CBC mode. There is no separate integrity protection of individual keys, which is fine if the archive as a whole is integrity protected, but it means that access control is effectively at the level of the whole keystore. This is not <em>terrible</em> from a crypto point of view, but can definitely be improved—neither MD5 nor <a href="https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA">TripleDES</a> are considered secure any more, and it's been a <em>long</em> time since anyone recommended them for new projects. However, it would also not be a trivial effort to break it.

JCEKS uses the same ridiculous "Mighty Aphrodite" prefix-keyed hash as JKS for integrity protection of the entire archive. It is probably best to assume that there is no serious integrity protection of either of these key stores.
<h2>PKCS#12</h2>
Apart from these proprietary key stores, Java also supports "standard" PKCS#12 format key stores. The reason for the scare quotes around "standard" is that while it is indeed a standard format, it is a very flexible one, and so in practice there are significant differences between what "key bag" formats and encryption algorithms are supported by different software. For instance, when you store symmetric SecretKey objects in a PKCS#12 key store from Java, then OpenSSL cannot read them as they use a bag type ("secretBag" - OID 1.2.840.113549.1.12.10.1.5) that it does not understand.

Java uses version 3 of the PKCS#12 standard format. It stores secret keys in the aforementioned "secretBag" format, and asymmetric private keys in "PKCS#8 Shrouded Key Bag" format (OID 1.2.840.113549.1.12.10.1.2). This just dictates the format of bytes on the disk. In both cases the actual key material is encrypted using some form of password-based encryption (PBE) mode. By default this is "PBEWithSHA1AndDESede" — "DESede" is another name for TripleDES in encrypt-decrypt-encrypt mode, so this is pretty similar to the mode used by JCEKS apart from using a slightly better (but still deprecated) hash in the form of SHA-1. By default this uses a 160-bit salt and 50,000 iterations.

<strong>But</strong>, there is an important improvement in the PKCS#12 implementation—<em>you get to choose the encryption algorithm!</em> By passing in a <a href="https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.PasswordProtection.html#PasswordProtection-char:A-java.lang.String-java.security.spec.AlgorithmParameterSpec-">PasswordProtection</a> parameter (from Java 8 onwards) when saving a key you can specify a particular (password-based) cipher to use. I haven't checked exactly what ciphers are allowed, but you can at least specify a stronger PBE mode, such as "PBEWithHmacSHA512AndAES_256", which will derive a 256-bit AES key using salted PBKDF2 and then encrypt the stored key using AES/CBC/PKCS5Padding with that key. You can also increase the number of iterations of PBKDF2 used. For example:
```java
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.KeyStore.PasswordProtection;
import java.security.KeyStore.SecretKeyEntry;
import java.security.SecureRandom;

import javax.crypto.SecretKey;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class scratch {
    public static void main(String... args) throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(null, null); // Initialize a blank keystore

        SecretKey key = new SecretKeySpec(new byte[32], "AES");

        char[] password = "changeit".toCharArray();
        byte[] salt = new byte[20];
        new SecureRandom().nextBytes(salt);
        keyStore.setEntry("test", new SecretKeyEntry(key),
            new PasswordProtection(password,
                "PBEWithHmacSHA512AndAES_128",
                new PBEParameterSpec(salt, 100_000)));

        keyStore.store(new FileOutputStream("/tmp/keystore.p12"), password);
    }
}
```
Note that despite the inclusion of "HmacSHA512" in the above PBE mode that only applies to the key derivation from the password. There is no integrity protection at the level of individual entries.

It is also worth noting that the keystore and individual key passwords <em>should be the same</em>. I don't think this is a fundamental limitation of PKCS#12 in Java, but certainly standard Java tools like the command line "keytool" utility will fail to handle PKCS#12 keystores with different passwords used for the store vs individual keys. If you don't need to use those tools then you might be able to get away with different passwords for each key.

In contrast to the previous entries, the PKCS#12 key store format does actually encrypt certificates too. It does this with a hard-coded algorithm "PBEWithSHA1AndRC2_40". This uses 50,000 rounds of salted PBKDF1 to derive a <em>40-bit</em> key for RC2 encryption. RC2 is an old stream cipher that I certainly wouldn't recommend. The 40-bit key is <em>far</em> too small to provide any serious security. It makes me wonder why bother applying 50,000 rounds of PBKDF1 to protect the password while generating a key that is itself vulnerable to brute-force. It is probably actually faster to brute force the derived key than the original password. I can only assume it is maintaining compatibility with some decision taken way back in the depths of time that everyone involved now deeply regrets.

The integrity of the overall PKCS#12 key store is protected with "HmacPBESHA1". This is HMAC-SHA1 using a key derived from the store password using 100,000 iterations of salted PBKDF2-HMAC-SHA1. This is all hard-coded so cannot be changed. This is an ok choice, although it would be nice to be able to use something other than SHA-1 here, as it appears that PKCS#12 allows other MACs to be used. For HMAC usage, SHA-1 is still just about ok for now, but it would be better to remove it. It would also be nice to be able to tune the iteration count.

Overall, the PKCS#12 key store is considerably better than either of the Sun-designed proprietary options. If you specify your own PasswordProtection instances with AES and SHA2 and use high iteration counts and good random salts, then it's actually a pretty solid design even by modern standards. The only really ugly part is the 40-bit RC2 encryption of trusted certificates, but if you do not care about the confidentiality of certificates then we can overlook that detail and just consider them lightly obfuscated. At least the use of HMAC-SHA1 is a decent integrity protection at last.
<h2>PKCS#11</h2>
There's not much to say about PKCS#11. It is a standard interface, intended for use with <em>hardware</em> security tokens of various kinds: in particular Hardware Security Modules (HSMs). These range from <a href="https://shop.nitrokey.com/shop/product/nitrokey-hsm-7">50 Euro USB sticks</a> up to network-attached behemoths that cost tens or hundreds of thousands of dollars. The hardware is usually proprietary and closed, so it's hard to say exactly how your keys will be stored. Generally, though, there are significant protections against access to keys from either remote attackers or even those with physical access to the hardware and a lot of time on their hands. This isn't a guarantee of security, as there are lots of ways that keys might accidentally leak from the hardware, as the recent <a href="https://crocs.fi.muni.cz/public/papers/rsa_ccs17">ROCA vulnerability in Infineon hardware</a> demonstrated. Still, a well-tested HSM is probably a pretty secure option for high-value keys.

I won't go into the details of how to set up a PKCS#11 key store, as it really varies from vendor to vendor. As for PKCS#12, while the interface is standardised there is enormous room for variation within that standard. In most cases you would let the HSM generate keys in the secure hardware and never export the private key material (except perhaps for backup).
<h2>Summary</h2>
Use a HSM or a PKCS#12 keystore, and specify manual PasswordProtection arguments when storing keys.  Avoid the proprietary key stores. 

Alternatively, farm out key management to somebody else and use a Key Management System (KMS) like <a href="//www.vaultproject.io">Hashicorp Vault.</a>