title: [WebPush][VAPID] Request fails with 400 UnauthorizedRegistration
body: <p>I'm working on a pure java implementation for WebPush with VAPID and payload encryption (I've already made implementations for GCM and FCM). However the documentation is still marginal and also the code samples are still not substantial. At this moment i'm trying to get it to work in Chrome. allthough i get succesful subscriptions using VAPID, when i send either a Tickle or a Payload push message i get a 400 UnauthorizedRegistration. My guess is that it has something to do with the authorization header or the Crypto-Key header. This is what i'm sending so far for a Tickle (A push notification without payload):</p>
  <pre><code>URL: https://fcm.googleapis.com/fcm/send/xxxxx:xxxxxxxxxxx...
  Action: POST/PUT (Both give same result)
  With headers:
      Authorization: Bearer URLBase64(JWT_HEAD).URLBase64(JWT_Payload).SIGN
      Crypto-Key: p265ecdsa=X9.62(PublicKey)
      Content-Type: &quot;text/plain;charset=utf8&quot;
      Content-Length: 0
      TTL: 120
  
  JWT_HEAD=&quot;{\&quot;typ\&quot;:\&quot;JWT\&quot;,\&quot;alg\&quot;:\&quot;ES256\&quot;}&quot;
  JWT_Payload={
      aud: &quot;https://fcm.googleapis.com&quot;,
      exp: (System.currentTimeMillis() / 1000) + (60 * 60 * 12)),
      sub: &quot;mailto:webpush@mydomain.com&quot;
  }
  SIGN = the &quot;SHA256withECDSA&quot; signature algorithm over: &quot;URLBase64(JWT_HEAD).URLBase64(JWT_Payload)&quot;
  </code></pre>
  <p>I've stripped the whitespaces from both JSON's in the JWT since the spec is not very clear about whitespace usage that seemed the safest thing to do.
  The signature validates after decoding the x9.62 to ECPoint again, so the publicKey seems validly encoded. However i keep getting the response:</p>
  <pre><code>&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;UnauthorizedRegistration&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot;&gt;&lt;H1&gt;UnauthorizedRegistration&lt;/H1&gt;&lt;H2&gt;Error 400&lt;/H2&gt;&lt;/BODY&gt;&lt;/HTML&gt;
  </code></pre>
  <p>According to the FCM documentation this only happends when a JSON error occurs, however i feel the specification does not cover WebPush at all. For now i've both tried the build in Java Crypto providers and BC both produce the same results.</p>
  <p>Some code Snippets for clarification:</p>
  <p>KeyGeneration:</p>
  <pre><code>KeyPairGenerator keyGen = KeyPairGenerator.getInstance(&quot;EC&quot;, &quot;BC&quot;);
  ECGenParameterSpec spec = new ECGenParameterSpec(&quot;secp256r1&quot;);
  keyGen.initialize(spec, secureRandom);
  KeyPair vapidPair = keyGen.generateKeyPair();
  </code></pre>
  <p>ECPublicKey to x9.62:</p>
  <pre><code>public byte[] toUncompressedPoint(ECPublicKey publicKey){
      final ECPoint publicPoint = publicKey.getW();
      
      final int keySizeBytes = (publicKey.getParams().getOrder().bitLength() + Byte.SIZE - 1) / Byte.SIZE;
      final byte[] x = publicPoint.getAffineX().toByteArray();
      final byte[] y = publicPoint.getAffineY().toByteArray();
      final byte[] res = new byte[1 + 2 * keySizeBytes];
      int offset = 0;
      res[offset++] = 0x04; //Indicating no key compression is used
      if(x.length &lt;= keySizeBytes)
          System.arraycopy(x, 0, res, offset + keySizeBytes - x.length, x.length);
      else if(x.length == keySizeBytes + 1) System.arraycopy(x, 1, res, offset, keySizeBytes);
      else throw new IllegalArgumentException(&quot;X value is too large!&quot;);
  
      offset += keySizeBytes;
      if(y.length &lt;= keySizeBytes)
          System.arraycopy(y, 0, res, offset + keySizeBytes - y.length, y.length);
      else if(y.length == keySizeBytes + 1 &amp;&amp; y[0] == 0) System.arraycopy(y, 1, res, offset, keySizeBytes);
      else throw new IllegalArgumentException(&quot;Y value is too large!&quot;);
  
      return res;
  }
  </code></pre>
  <p>Signing the JWT claim:</p>
  <pre><code>    ObjectNode claim = om.createObjectNode();
      claim.put(&quot;aud&quot;, host);
      claim.put(&quot;exp&quot;, (System.currentTimeMillis() / 1000) + (60 * 60 * 12));
      claim.put(&quot;sub&quot;, &quot;mailto:webpush_ops@mydomain.com&quot;);
      String claimString = claim.toString();
      String encHeader = URLBase64.encodeString(VAPID_HEADER, false);
      String encPayload =  URLBase64.encodeString(claimString, false);
      String vapid = null;
      ECPublicKey pubKey = (ECPublicKey) vapidPair.getPublic();
      byte[] point = toUncompressedPoint(pubKey);
      String vapidKey = URLBase64.encodeToString(point, false);
      try{
          Signature dsa = Signature.getInstance(&quot;SHA256withECDSA&quot;, &quot;BC&quot;);
          dsa.initSign(vapidPair.getPrivate());
          dsa.update((encHeader + &quot;.&quot; + encPayload).getBytes(StandardCharsets.US_ASCII));
          byte[] signature = dsa.sign();
          vapid = encHeader + &quot;.&quot; + encPayload + &quot;.&quot; + URLBase64.encodeToString(signature, false);
  </code></pre>
  <p>Some questions that reside in my mind:</p>
  <ul>
  <li><p>what is the auth field for in the registration reply JSON? Since to my knowledge for encryption only the p256dh is used for generating the encryption keys together with a server based KeyPair.</p>
  <p>Further research of the ietf draft 03 gave me the answer in section: 2.3
  Link: <a href="https://datatracker.ietf.org/doc/html/draft-ietf-webpush-encryption-03" rel="nofollow noreferrer">https://datatracker.ietf.org/doc/html/draft-ietf-webpush-encryption-03</a>
  Also the link in Vincent Cheung's answer gives a good explanation</p>
  </li>
  <li><p>The documentation speaks of different header usage for VAPID using Bearer/WebPush and using the Crypto-Key header or the Encryption-Key header. Wat is the correct thing to do?</p>
  </li>
  <li><p>Any ideas why the FCM server keeps returning a: 400 UnauthorizedRegistration ?</p>
  </li>
  </ul>
  <p>Can somebody add the VAPID tag to this question? It does not yet seem to exist.</p>