Class VAPIDKeys
- Author:
- Christopher Mindus
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe length in bytes of a P-256 private scalar.static final intThe length in bytes of an uncompressed P-256 public point: the 0x04 tag plus the 32-byte X and Y coordinates.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic String[]Generates a public key and a private key.static StringvalidatePrivateKey(String privateKey) Validates the private key Base64 encoded string.static StringvalidatePublicKey(String publicKey) Validates the public key Base64 encoded string.
Field Details
PUBLIC_KEY_LENGTH
public static final int PUBLIC_KEY_LENGTHThe length in bytes of an uncompressed P-256 public point: the 0x04 tag plus the 32-byte X and Y coordinates.- See Also:
PRIVATE_KEY_LENGTH
public static final int PRIVATE_KEY_LENGTHThe length in bytes of a P-256 private scalar.- See Also:
Constructor Details
VAPIDKeys
public VAPIDKeys()
Method Details
generateKeys
Generates a public key and a private key.The keys are the RAW forms that Web Push requires, base64url encoded without padding: the public key is the uncompressed P-256 point,
0x04 || X || Y, 65 bytes; the private key is the unsigned scalard, 32 bytes, left-padded so its length never varies with the value.These are the forms
Utils.loadPublicKey(String)andUtils.loadPrivateKey(String)parse, and the public key is passed unchanged to the browser'spushManager.subscribeasapplicationServerKey.Until r19738 this method encoded
getEncoded()on both keys instead - X.509 SubjectPublicKeyInfo DER for the public key, 91 bytes, and PKCS#8 DER for the private. Neither is usable:loadPublicKeycallscurve.decodePointand threw on the DER, so the Designer's "Generate new VAPID Key Pair" produced keys that this class's own validators rejected and that no browser would accept. The private key was worse than a rejection -BigIntegers.fromUnsignedByteArrayread the whole DER blob as the scalar and returned a wrong key without complaint.- Returns:
- Array of 2 String's: first the public key, then the private key.
- Throws:
IllegalStateException- NOTE: try-catch on it! In case of BouncyCastle errors.
validatePublicKey
validatePrivateKey