Class KeyFiles

java.lang.Object
com.codename1.backend.security.crypto.KeyFiles

public final class KeyFiles extends Object

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 says It is
PRIVATE KEY PKCS#8, RSA or EC: openssl genpkey
RSA PRIVATE KEY PKCS#1: openssl genrsa of old, ssh-keygen -m PEM
EC PRIVATE KEY SEC 1: openssl ecparam -genkey
PUBLIC KEY SubjectPublicKeyInfo: 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.

  • Method Details

    • privateKey

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

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

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

      public static byte[] readPublicKey(String path) throws IOException
      The public key in the PEM file at path, as SubjectPublicKeyInfo DER.
      Throws:
      IOException
    • 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.
      Throws:
      IOException