← Keystores
Keystore·Guide
All tools

Keystores

A keystore holds private keys with their certificate chains, and trusted certificates. Apps ship them as truststores (often BKS on Android, for certificate pinning), signing setups use them, and client certificates come as PKCS#12. The editor reads, changes and writes four formats in your browser; the code is a library you can use in your own pages.

Contents

  1. The formats
  2. The editor
  3. The Keystore library
  4. Entries
  5. The crypto algorithms

1The formats

FormatIntegrity checkPrivate keysNotes
JKSSHA-1 of the password, “Mighty Aphrodite” and the filea SHA-1 keystream (Sun's KeyProtector)Java's old default. Aliases are case-insensitive. Fast to brute-force.
JCEKSas JKSPBE with MD5 and triple DESCan hold secret keys, as serialized Java objects.
BKSHMAC-SHA1 (version 1 uses a 2-byte key)sealed: PBE with SHA-1 and triple DESBouncy Castle; what Android apps usually ship.
PKCS#12HMAC-SHA256 or SHA-1, PKCS#12 key derivationshrouded: PBES2 (AES) or legacy triple DES / RC2The standard (.p12, .pfx); keytool's default since JDK 9. Certificates are usually encrypted too.

2The editor

Java refuses EC certificates and keys that spell out their curve parameters instead of naming the curve. The editor names the curve of such keys and refuses such certificates, except in BKS (Bouncy Castle reads them).

3The Keystore library

lib/keystore.js is a JavaScript module with no user interface. It exports Keystore, a class whose instances are opened (or new) keystores; it imports what it needs itself (lib/asn1.js, lib/pem.js, lib/x509.js, documented in the Certificates tool guide, and the lib/crypto files below). Everything except keyMatches is computed by these files, so it also works without WebCrypto (in a worker, or in jsc). Methods that use passwords are async.

import { Keystore } from "../lib/keystore.js";

const store = await Keystore.read(bytes, "changeit");
console.log(store.format, store.integrity.verified);
for (const entry of store.entries) {
	console.log(entry.kind, entry.alias);
}

/* add a trusted certificate and save as BKS */
const bks = Keystore.create("BKS");
bks.setTrusted({ alias: "my-ca", cert: certificateDer });
const file = await bks.write("changeit");    // Uint8Array

Opening and creating (static methods)

MethodWhat it does
Keystore.detect(bytes)"JKS", "JCEKS", "BKS", "PKCS12" or null.
await Keystore.read(bytes, password)The keystore. Without the password, integrity.verified is null and a PKCS#12 with encrypted certificates is locked.
Keystore.create(format)An empty one: "JKS", "JCEKS", "BKS" or "PKCS12" (its fresh is true).
await Keystore.parseKey(bytesOrText, password){ pkcs8, renamedCurve }: a private key from PKCS#8, PKCS#1 (RSA), SEC1 (EC) or encrypted PKCS#8, PEM or DER. EC keys with explicit parameters get their curve's name; renamedCurve says which.
await Keystore.keyMatches(pkcs8, certificateDer)true, false or null: whether the key belongs to the certificate (RSA and EC, through WebCrypto; null when it can't tell).
Keystore.hasExplicitCurve(certificateDer)Whether an EC certificate spells out its curve (Java can't read it).
Keystore.keyAlgorithm(pkcs8)"RSA", "EC" or another algorithm's name.
Keystore.certificateInfo(der)new X509Certificate(der) (subject, issuer, validity, public key, fingerprints), or null when it isn't one.
Keystore.OIDThe object identifiers it uses.

A keystore

Method / propertyWhat it does
format, version, entries, notesThe format, its version, the entries (next section) and notes about the file.
integrity{ verified, algorithm }: verified is true, false, or null (not checked).
lockedTrue for a PKCS#12 whose certificates are still encrypted (no password yet).
await store.write(password)The file (Uint8Array), with a new integrity check (PKCS#12 certificates are encrypted again).
await store.verify(password){ verified, algorithm }: checks the integrity with a password.
await store.privateKey(entry, keyPassword)The PKCS#8 DER of a private key. Throws on a wrong password.
await store.setPrivate({ alias, pkcs8, chain, keyPassword, entry })Adds a private key (or replaces entry), protected the way the format does it; returns the entry.
store.setTrusted({ alias, cert, entry })Adds a trusted certificate (or replaces entry); returns the entry.
store.capabilities(){ addPrivate, addTrusted, reason }: what can be added now (nothing to a locked PKCS#12).
store.aliasTaken(alias, except), store.normalizeAlias(alias)Alias checks; JKS and JCEKS compare aliases without case.

4Entries

Every entry has { id, alias, date, kind } (date is null for PKCS#12, which doesn't store one).

kindFields
privatechain: certificates (DER), the key's own first. key: { scheme, blob, pkcs8 }: scheme is how the key is protected (jks, jceks, bks-sealed, bks-plain, p12-shrouded, p12-plain), blob the protected key, pkcs8 the decrypted key once known.
trustedcert: the certificate (DER).
certcert: a PKCS#12 certificate that is neither trusted nor in a key's chain.
secretlabel, and raw: kept as read and written back unchanged.
Entries can't move between formats with their keys still protected: to copy a private key into another keystore, decrypt it with privateKey() and add it with setPrivate().

5The crypto algorithms

Keystores use algorithms WebCrypto doesn't offer. Each is a module of its own in lib/crypto, exporting one class named after the algorithm. They follow the standards closely, which makes them easy to read and slow: meant for keys and small data. Every one is synchronous (except Pbe's methods, which are async), so it works in a page, in a worker and in jsc.

import { TripleDes } from "../lib/crypto/triple-des.js";
import { Cbc } from "../lib/crypto/cbc.js";
import { Hmac } from "../lib/crypto/hmac.js";
import { Sha256 } from "../lib/crypto/sha256.js";

const plain = Cbc.decrypt(new TripleDes(key), iv, encrypted);
const mac = Hmac.sign(Sha256, key, data);
FileClass and methodsWhat it is
sha1.js, sha256.js, md5.jsSha1.digest(bytes), Sha256.digest(bytes), Md5.digest(bytes)Hashes (FIPS 180-4, RFC 1321); each also has DIGEST_LENGTH and BLOCK_SIZE. The class itself is what Hmac, Pbkdf2 and Pkcs12Kdf take as hash.
hmac.jsHmac.sign(hash, key, message)HMAC (RFC 2104) with any of the hashes, for example Hmac.sign(Sha256, key, data).
pbkdf2.jsPbkdf2.derive({ hash, password, salt, iterations, length })PBKDF2 (RFC 8018); password is text (UTF-8) or bytes.
pkcs12-kdf.jsPkcs12Kdf.derive({ hash, password, salt, iterations, purpose, length })The PKCS#12 key derivation (RFC 7292, appendix B); purpose is Pkcs12Kdf.KEY, IV or MAC.
aes.jsnew Aes(key): blockSize, encryptBlock(block), decryptBlock(block)AES-128/192/256 (FIPS 197), 16-byte blocks.
des.jsnew Des(key): the same methodsDES (FIPS 46-3): 8-byte key, 8-byte blocks.
triple-des.jsnew TripleDes(key)Encrypt-decrypt-encrypt with three DES keys (24 bytes, or 16 with K3 = K1).
rc2.jsnew Rc2(key, effectiveBits)RC2 (RFC 2268), 8-byte blocks; old PKCS#12 files use 40 effective bits.
cbc.jsCbc.encrypt(cipher, iv, data, { padding }), Cbc.decrypt(…)Cipher block chaining for any of the block ciphers; PKCS#7 padding unless padding: false.
pkcs7-padding.jsPkcs7Padding.pad(data, blockSize), Pkcs7Padding.unpad(data, blockSize)PKCS#7 (PKCS#5) padding; unpad throws when it is wrong.
pbe.jsawait Pbe.decrypt(algorithmDer, encrypted, password), await Pbe.encrypt(plain, password, { iterations }), Pbe.schemeName(algorithmDer)Password-based encryption as PKCS#8 and PKCS#12 files use it: PBES2 (PBKDF2 + AES or 3DES) and the PKCS#12 schemes (SHA-1 with 3DES or RC2). encrypt writes PBES2 with PBKDF2-HMAC-SHA256 and AES-256-CBC and returns { algorithm, encrypted }. A failed decryption's error has a reason: password-needed, wrong-password or unsupported.