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

public final class Der

  1. Object
  2. Der

The little ASN.1 DER a server needs to move keys and signatures between the shapes they travel in: a JSON Web Key’s numbers and the SubjectPublicKeyInfo that Crypto.verify takes; a PKCS#1 or SEC 1 private key out of an older PEM file and the PKCS#8 that Crypto.sign takes; an ECDSA signature as OpenSSL and the JDK write it and as a JSON Web Signature carries it.

byte[] publicKey = Der.rsaPublicKey(modulus, exponent);      // from a JWK's n and e
byte[] jose = Der.ecdsaDerToJose(Crypto.sign(Crypto.ES256, key, data), 32);

Nothing here computes with a key. It reads and writes the envelope, and refuses one that is not exactly what it expects with an IOException.

Fields

public static final String RSA = "RSA"The key type of an RSA key, as a JSON Web Key names it.
public static final String EC = "EC"The key type of an elliptic curve key, as a JSON Web Key names it.
public static final String P256 = "P-256"The curve of ES256, as a JSON Web Key names it.
public static final String P384 = "P-384"The curve of ES384, as a JSON Web Key names it.

Methods

public static byte[] rsaPublicKey(byte[] modulus, byte[] exponent) throws IOExceptionThe SubjectPublicKeyInfo of an RSA public key.
public static byte[][] rsaPublicKeyParts(byte[] publicKey) throws IOExceptionThe modulus and the public exponent of an RSA SubjectPublicKeyInfo, each an unsigned big-endian number without leading zeros.
public static int rsaModulusBits(byte[] publicKey) throws IOExceptionThe size of an RSA public key: the bits of its modulus.
public static byte[] ecPublicKey(String curve, byte[] x, byte[] y) throws IOExceptionThe SubjectPublicKeyInfo of a public key on P256 or P384.
public static String ecCurve(byte[] publicKey) throws IOExceptionThe curve of an EC SubjectPublicKeyInfo: P256 or P384.
public static byte[][] ecPublicKeyParts(byte[] publicKey) throws IOExceptionThe two coordinates of an EC SubjectPublicKeyInfo’s point, each as long as ecCoordinateLength says.
public static int ecCoordinateLength(String curve) throws IOExceptionThe bytes one coordinate – and each half of a JOSE signature – takes on a curve: 32 on P256, 48 on P384.
public static String publicKeyType(byte[] publicKey) throws IOExceptionRSA or EC: what a SubjectPublicKeyInfo is the key of.
public static String privateKeyType(byte[] privateKey) throws IOExceptionRSA or EC: what a PKCS#8 private key is the key of.
public static byte[] publicKeyOf(byte[] privateKey) throws IOExceptionThe public half of a PKCS#8 private key, as a SubjectPublicKeyInfo.
public static byte[] pkcs1ToPkcs8(byte[] rsaPrivateKey) throws IOExceptionWraps a PKCS#1 RSAPrivateKey – the content of a PEM file that says RSA PRIVATE KEY – as PKCS#8.
public static byte[] sec1ToPkcs8(byte[] ecPrivateKey) throws IOExceptionWraps a SEC 1 ECPrivateKey – the content of a PEM file that says EC PRIVATE KEY – as PKCS#8.
public static byte[] ecdsaDerToJose(byte[] der, int partLength) throws IOExceptionAn ECDSA signature as a JSON Web Signature carries it – r and s side by side, each padded to the curve’s size – from the ASN.1 DER that Crypto.sign returns.
public static byte[] ecdsaJoseToDer(byte[] jose) throws IOExceptionThe ASN.1 DER Crypto.verify takes, from an ECDSA signature as a JSON Web Signature carries it.

Inherited methods

Field details

RSA

public static final String RSA = "RSA"
The key type of an RSA key, as a JSON Web Key names it.

EC

public static final String EC = "EC"
The key type of an elliptic curve key, as a JSON Web Key names it.

P256

public static final String P256 = "P-256"
The curve of ES256, as a JSON Web Key names it.

P384

public static final String P384 = "P-384"
The curve of ES384, as a JSON Web Key names it.

Method details

rsaPublicKey

public static byte[] rsaPublicKey(byte[] modulus, byte[] exponent) throws IOException
The SubjectPublicKeyInfo of an RSA public key.

Parameters

modulus byte[]
the modulus as an unsigned big-endian number, as a JWK’s n decodes
exponent byte[]
the public exponent, likewise: a JWK’s e

rsaPublicKeyParts

public static byte[][] rsaPublicKeyParts(byte[] publicKey) throws IOException
The modulus and the public exponent of an RSA SubjectPublicKeyInfo, each an unsigned big-endian number without leading zeros.

rsaModulusBits

public static int rsaModulusBits(byte[] publicKey) throws IOException
The size of an RSA public key: the bits of its modulus.

ecPublicKey

public static byte[] ecPublicKey(String curve, byte[] x, byte[] y) throws IOException
The SubjectPublicKeyInfo of a public key on P256 or P384.

Parameters

curve String
the curve as a JWK’s crv names it
x byte[]
the point’s first coordinate, big-endian: a JWK’s x
y byte[]
its second: a JWK’s y

ecCurve

public static String ecCurve(byte[] publicKey) throws IOException
The curve of an EC SubjectPublicKeyInfo: P256 or P384.

ecPublicKeyParts

public static byte[][] ecPublicKeyParts(byte[] publicKey) throws IOException
The two coordinates of an EC SubjectPublicKeyInfo’s point, each as long as ecCoordinateLength says.

ecCoordinateLength

public static int ecCoordinateLength(String curve) throws IOException
The bytes one coordinate – and each half of a JOSE signature – takes on a curve: 32 on P256, 48 on P384.

publicKeyType

public static String publicKeyType(byte[] publicKey) throws IOException
RSA or EC: what a SubjectPublicKeyInfo is the key of.

privateKeyType

public static String privateKeyType(byte[] privateKey) throws IOException
RSA or EC: what a PKCS#8 private key is the key of.

publicKeyOf

public static byte[] publicKeyOf(byte[] privateKey) throws IOException

The public half of a PKCS#8 private key, as a SubjectPublicKeyInfo.

An RSA private key always holds its public numbers. An EC private key holds its public point only when whatever wrote it put it there, which openssl does; one that does not is refused, and its public key has to be given beside it.

pkcs1ToPkcs8

public static byte[] pkcs1ToPkcs8(byte[] rsaPrivateKey) throws IOException
Wraps a PKCS#1 RSAPrivateKey – the content of a PEM file that says RSA PRIVATE KEY – as PKCS#8.

sec1ToPkcs8

public static byte[] sec1ToPkcs8(byte[] ecPrivateKey) throws IOException
Wraps a SEC 1 ECPrivateKey – the content of a PEM file that says EC PRIVATE KEY – as PKCS#8. The key has to name its curve, as one openssl wrote does.

ecdsaDerToJose

public static byte[] ecdsaDerToJose(byte[] der, int partLength) throws IOException
An ECDSA signature as a JSON Web Signature carries it – r and s side by side, each padded to the curve’s size – from the ASN.1 DER that Crypto.sign returns.

Parameters

der byte[]
Not documented.
partLength int
ecCoordinateLength of the key’s curve

ecdsaJoseToDer

public static byte[] ecdsaJoseToDer(byte[] jose) throws IOException
The ASN.1 DER Crypto.verify takes, from an ECDSA signature as a JSON Web Signature carries it.