Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class Der
- Object
- 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
Inherited methods
Field details
RSA
public static final String RSA = "RSA"EC
public static final String EC = "EC"P256
public static final String P256 = "P-256"P384
public static final String P384 = "P-384"Method details
rsaPublicKey
public static byte[] rsaPublicKey(byte[] modulus, byte[] exponent)
throws IOExceptionParameters
modulusbyte[]- the modulus as an unsigned big-endian number, as a JWK’s
ndecodes exponentbyte[]- the public exponent, likewise: a JWK’s
e
Throws
rsaPublicKeyParts
public static byte[][] rsaPublicKeyParts(byte[] publicKey)
throws IOExceptionThrows
rsaModulusBits
public static int rsaModulusBits(byte[] publicKey)
throws IOExceptionThrows
ecPublicKey
public static byte[] ecPublicKey(String curve, byte[] x, byte[] y)
throws IOExceptionParameters
curveString- the curve as a JWK’s
crvnames it xbyte[]- the point’s first coordinate, big-endian: a JWK’s
x ybyte[]- its second: a JWK’s
y
Throws
ecCurve
public static String ecCurve(byte[] publicKey)
throws IOExceptionThrows
ecPublicKeyParts
public static byte[][] ecPublicKeyParts(byte[] publicKey)
throws IOExceptionecCoordinateLength says.Throws
ecCoordinateLength
public static int ecCoordinateLength(String curve)
throws IOExceptionP256, 48 on P384.Throws
publicKeyType
public static String publicKeyType(byte[] publicKey)
throws IOExceptionThrows
privateKeyType
public static String privateKeyType(byte[] privateKey)
throws IOExceptionThrows
publicKeyOf
public static byte[] publicKeyOf(byte[] privateKey)
throws IOExceptionThe 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.
Throws
pkcs1ToPkcs8
public static byte[] pkcs1ToPkcs8(byte[] rsaPrivateKey)
throws IOExceptionRSAPrivateKey – the content of a PEM file that says
RSA PRIVATE KEY – as PKCS#8.Throws
sec1ToPkcs8
public static byte[] sec1ToPkcs8(byte[] ecPrivateKey)
throws IOExceptionECPrivateKey – 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.Throws
ecdsaDerToJose
public static byte[] ecdsaDerToJose(byte[] der, int partLength)
throws IOExceptionCrypto.sign returns.Parameters
derbyte[]- Not documented.
partLengthintecCoordinateLengthof the key’s curve
Throws
ecdsaJoseToDer
public static byte[] ecdsaJoseToDer(byte[] jose)
throws IOExceptionCrypto.verify takes, from an ECDSA
signature as a JSON Web Signature carries it.