Class VAPIDKeys

java.lang.Object
com.iizix.push.VAPIDKeys

public class VAPIDKeys extends Object
Helper class to generate VAPID keys for use with Push Notification.
Author:
Christopher Mindus
  • Field Details

    • PUBLIC_KEY_LENGTH

      public static final int PUBLIC_KEY_LENGTH
      The 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_LENGTH
      The length in bytes of a P-256 private scalar.
      See Also:
  • Constructor Details

    • VAPIDKeys

      public VAPIDKeys()
  • Method Details

    • generateKeys

      public static String[] generateKeys() throws IllegalStateException
      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 scalar d, 32 bytes, left-padded so its length never varies with the value.

      These are the forms Utils.loadPublicKey(String) and Utils.loadPrivateKey(String) parse, and the public key is passed unchanged to the browser's pushManager.subscribe as applicationServerKey.

      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: loadPublicKey calls curve.decodePoint and 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.fromUnsignedByteArray read 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

      public static String validatePublicKey(String publicKey)
      Validates the public key Base64 encoded string.
      Parameters:
      publicKey - The public key to validate.
      Returns:
      A validation message, or null for success.
    • validatePrivateKey

      public static String validatePrivateKey(String privateKey)
      Validates the private key Base64 encoded string.
      Parameters:
      privateKey - The private key to validate.
      Returns:
      A validation message, or null for success.