Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class KeyFiles

  1. Object
  2. KeyFiles

Reads keys out of PEM text, in the shapes key files come in, and hands back the one shape the runtime signs and verifies with: PKCS#8 DER for a private key, SubjectPublicKeyInfo DER for a public one.

byte[] signingKey = KeyFiles.readPrivateKey("/etc/orders/signing-key.pem");
byte[] publicKey = KeyFiles.publicKey(config.get("orders.public-key"));
The file saysIt is
PRIVATE KEYPKCS#8, RSA or EC: openssl genpkey
RSA PRIVATE KEYPKCS#1: openssl genrsa of old, ssh-keygen -m PEM
EC PRIVATE KEYSEC 1: openssl ecparam -genkey
PUBLIC KEYSubjectPublicKeyInfo: openssl pkey -pubout

A key protected by a passphrase is refused, with a message that says how to write it without one: a server that reads its key from a file has nobody to ask for the passphrase, and one that reads the passphrase from the file next to it has protected nothing. A certificate is refused too; extract its key with openssl x509 -pubkey -noout.

Methods

public static byte[] privateKey(String pem) throws IOExceptionThe private key in pem, as PKCS#8 DER.
public static byte[] publicKey(String pem) throws IOExceptionThe public key in pem, as SubjectPublicKeyInfo DER.
public static byte[] readPrivateKey(String path) throws IOExceptionThe private key in the PEM file at path, as PKCS#8 DER.
public static byte[] readPublicKey(String path) throws IOExceptionThe public key in the PEM file at path, as SubjectPublicKeyInfo DER.
public static String read(String path) throws IOExceptionThe text of a key file.

Inherited methods

Method details

privateKey

public static byte[] privateKey(String pem) throws IOException
The private key in pem, as PKCS#8 DER.

publicKey

public static byte[] publicKey(String pem) throws IOException
The public key in pem, as SubjectPublicKeyInfo DER.

readPrivateKey

public static byte[] readPrivateKey(String path) throws IOException
The private key in the PEM file at path, as PKCS#8 DER.

readPublicKey

public static byte[] readPublicKey(String path) throws IOException
The public key in the PEM file at path, as SubjectPublicKeyInfo DER.

read

public static String read(String path) throws IOException
The text of a key file. A file: in front of the path is allowed, so a setting copied from a Spring application reads the same file.