<?xml version="1.0" encoding="US-ASCII"?>
  <?xml-stylesheet type="text/xsl" href="rfc2629.xslt" ?>
  

<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
]>

<?rfc toc="yes"?>
<?rfc sortrefs="yes"?>
<?rfc symrefs="yes"?>
<?rfc compact="yes" ?>
<?rfc subcompact="no" ?>

<rfc number="8291" ipr="trust200902" category="std" submissionType="IETF" consensus="yes">

  <front>
    <title abbrev="Web Push Encryption">Message Encryption for Web Push</title>

    <author initials="M." surname="Thomson" fullname="Martin Thomson">
      <organization>Mozilla</organization>
      <address>
        <email>martin.thomson@gmail.com</email>
      </address>
    </author>

    <date year="2017" month="November" />

    
    <abstract>
<t>This document describes a message encryption scheme for the Web Push protocol.  This scheme
provides confidentiality and integrity for messages sent from an application
server to a user agent.</t>
    </abstract>


  </front>

  <middle>


<section anchor="introduction" title="Introduction">


<t>The Web Push protocol <xref target="RFC8030"/> is an intermediated protocol by necessity.
Messages from an application server are delivered to a user agent (UA) via a
push service, as shown in <xref target="fig-push-service"/>.</t>

<figure anchor="fig-push-service"><artwork><![CDATA[
 +-------+           +--------------+       +-------------+
 |  UA   |           | Push Service |       | Application |
 +-------+           +--------------+       +-------------+
     |                      |                      |
     |        Setup         |                      |
     |<====================>|                      |
     |           Provide Subscription              |
     |-------------------------------------------->|
     |                      |                      |
     :                      :                      :
     |                      |     Push Message     |
     |    Push Message      |<---------------------|
     |<---------------------|                      |
     |                      |                      |
]]></artwork></figure>

<t>This document describes how messages sent using this protocol can be secured
against inspection, modification, and forgery by a push service.</t>

<t>Web Push messages are the payload of an HTTP message <xref target="RFC7230"/>.  These
messages are encrypted using an encrypted content encoding <xref target="RFC8188"/>.  This
document describes how this content encoding is applied and describes a
recommended key management scheme.</t>

<t>Multiple users of Web Push at the same user agent often share a central agent
that aggregates push functionality.  This agent can enforce the use of this
encryption scheme by applications that use push messaging.  An agent that only
delivers messages that are properly encrypted strongly encourages the end-to-end
protection of messages.</t>



<t>A web browser that implements the Push API <xref target="API"/> can enforce the use of
encryption by forwarding only those messages that were properly encrypted.</t>

<section anchor="notational-conventions" title="Notational Conventions">

<t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in BCP 14 <xref target="RFC2119"/> <xref target="RFC8174"/>
when, and only when, they appear in all capitals, as shown here.</t>

<t>This document uses the terminology from <xref target="RFC8030"/>, primarily
"user agent", "push service", and "application server".</t>

</section>
</section>
<section anchor="overview" title="Push Message Encryption Overview">

<t>Encrypting a push message uses Elliptic Curve Diffie-Hellman (ECDH) <xref target="ECDH"/> on
the P-256 curve <xref target="FIPS186"/> to establish a shared secret (see <xref target="dh"/>) and a
symmetric secret for authentication (see <xref target="auth"/>).</t>

<t>A user agent generates an ECDH key pair and authentication secret that it
associates with each subscription it creates.  The ECDH public key and the
authentication secret are sent to the application server with other details of
the push subscription.</t>

<t>When sending a message, an application server generates an ECDH key pair and a
random salt.  
The ECDH public key is encoded into the <spanx
style="verb">keyid</spanx> parameter of the
encrypted content coding header, and the salt is encoded into the <spanx
style="verb">salt</spanx> parameter of that same
header (see Section 2.1 of <xref target="RFC8188"/>).  The ECDH key pair can be discarded
after encrypting the message.</t>


<t>The content of the push message is encrypted or decrypted using a content
encryption key and nonce. These values are derived by taking the "keyid" and
"salt" as input to the process
described in <xref target="encryption"/>.</t>

<section anchor="key-and-secret-distribution" title="Key and Secret Distribution">

<t>The application using the subscription distributes the subscription public key
and authentication secret to an authorized application server.  This could be
sent along with other subscription information that is provided by the user
agent, such as the push subscription URI.</t>

<t>An application MUST use an authenticated, confidentiality-protected
communications medium for this purpose.  In addition to the reasons described in
<xref target="RFC8030"/>, this use ensures that the authentication secret is not revealed to
unauthorized entities, which would allow those entities to generate push
messages that will be accepted by the user agent.</t>

<t>Most applications that use push messaging have a preexisting relationship with
an application server that can be used for distribution of subscription data.
An authenticated communication mechanism that provides adequate confidentiality
and integrity protection, such as HTTPS <xref target="RFC2818"/>, is sufficient.</t>

</section>
</section>
<section anchor="encryption" title="Push Message Encryption">

<t>Push message encryption happens in four phases:</t>

<t><list style="symbols">
  <t>A shared secret is derived using ECDH <xref target="ECDH"/>
(see <xref target="dh"/> of this document).</t>
  <t>The shared secret is then combined with the authentication secret to produce
the input keying material (IKM) used in <xref target="RFC8188"/> (see <xref
target="combine"/> of this document).</t>
  <t>A content encryption key and nonce are derived using the process in
<xref target="RFC8188"/>.</t>
  <t>Encryption or decryption follows according to <xref target="RFC8188"/>.</t>
</list></t>

<t>The key derivation process is summarized in <xref target="summary"/>.  Restrictions on the
use of the encrypted content coding are described in <xref target="restrict"/>.</t>

<section anchor="dh" title="Diffie-Hellman Key Agreement">

<t>For each new subscription that the user agent generates for an application, it
also generates a P-256 <xref target="FIPS186"/> key pair for use in
ECDH <xref target="ECDH"/>.</t>

<t>When sending a push message, the application server also generates a new ECDH
key pair on the same P-256 curve.</t>

<t>The ECDH public key for the application server is included as the "keyid"
parameter in the encrypted content coding header (see Section 2.1 of
<xref target="RFC8188"/>).</t>

<t>An application server combines its ECDH private key with the public key provided
by the user agent using the process described in <xref target="ECDH"/>; on receipt of the
push message, a user agent combines its private key with the public key provided
by the application server in the <spanx style="verb">keyid</spanx> parameter in the same way.  These
operations produce the same value for the ECDH shared secret.</t>

</section>
<section anchor="auth" title="Push Message Authentication">

<t>To ensure that push messages are correctly authenticated, a symmetric
authentication secret is added to the information generated by a user agent.
The authentication secret is mixed into the key derivation process described in
<xref target="combine"/>.</t>

<t>A user agent MUST generate and provide a hard-to-guess sequence of 16 octets that
is used for authentication of push messages.  This SHOULD be generated by a
cryptographically strong random number generator <xref target="RFC4086"/>.</t>

</section>
<section anchor="combine" title="Combining Shared and Authentication Secrets">

<t>The shared secret produced by ECDH is combined with the authentication secret
using the HMAC-based key derivation
function (HKDF) <xref target="RFC5869"/>.  This produces the input keying material used by
<xref target="RFC8188"/>.</t>

<t>The HKDF function uses the SHA-256 hash algorithm <xref target="FIPS180-4"/> with the following
inputs:</t>

<t><list style="hanging" hangIndent="6">
  <t hangText='salt:'>
  the authentication secret</t>

  <t hangText='IKM:'>
  the shared secret derived using ECDH</t>

  <t hangText='info:'>
     the concatenation of the ASCII-encoded string "WebPush: info"
     (this string is not NUL-terminated), a zero octet, the user
     agent ECDH public key, and the application server ECDH public key,
     (both ECDH public keys are in the uncompressed point form 
defined in <xref target="X9.62"/>. That is:
      <figure><artwork><![CDATA[
key_info = "WebPush: info" || 0x00 || ua_public || as_public
]]></artwork></figure>
  </t>
  <t hangText='L:'>
  32 octets (i.e., the output is the length of the underlying SHA-256 HMAC
function output)</t>
</list></t>

</section>
<section anchor="summary" title="Encryption Summary">

<t>This results in a final content encryption key and nonce generation using
the following sequence, which is shown here in pseudocode with HKDF expanded
into separate discrete steps using HMAC with SHA-256:</t>

<figure><artwork type="inline"><![CDATA[
   -- For a user agent:
   ecdh_secret = ECDH(ua_private, as_public)
   auth_secret = random(16)
   salt = <from content coding header>

   -- For an application server:
   ecdh_secret = ECDH(as_private, ua_public)
   auth_secret = <from user agent>
   salt = random(16)

   -- For both:

   ## Use HKDF to combine the ECDH and authentication secrets
   # HKDF-Extract(salt=auth_secret, IKM=ecdh_secret)
   PRK_key = HMAC-SHA-256(auth_secret, ecdh_secret)
   # HKDF-Expand(PRK_key, key_info, L_key=32)
   key_info = "WebPush: info" || 0x00 || ua_public || as_public
   IKM = HMAC-SHA-256(PRK_key, key_info || 0x01)

   ## HKDF calculations from RFC 8188
   # HKDF-Extract(salt, IKM)
   PRK = HMAC-SHA-256(salt, IKM)
   # HKDF-Expand(PRK, cek_info, L_cek=16)
   cek_info = "Content-Encoding: aes128gcm" || 0x00
   CEK = HMAC-SHA-256(PRK, cek_info || 0x01)[0..15]
   # HKDF-Expand(PRK, nonce_info, L_nonce=12)
   nonce_info = "Content-Encoding: nonce" || 0x00
   NONCE = HMAC-SHA-256(PRK, nonce_info || 0x01)[0..11]
]]></artwork></figure>

<t>Note that this omits the exclusive-OR of the final nonce with the record
sequence number, since push messages contain only a single record (see
<xref target="restrict"/>) and the sequence number of the first record is zero.</t>

</section>
</section>
<section anchor="restrict" title="Restrictions on Use of &quot;aes128gcm&quot; Content Coding">

<t>An application server MUST encrypt a push message with a single record.  This
allows for a minimal receiver implementation that handles a single record.  An
application server MUST set the <spanx style="verb">rs</spanx> parameter in the <spanx style="verb">aes128gcm</spanx> content coding
header to a size that is greater than the sum of the lengths of the plaintext,
the padding delimiter (1 octet), any padding, and the authentication tag (16
octets).</t>

<t>A push message MUST include the application server ECDH public key in the
<spanx style="verb">keyid</spanx> parameter of the encrypted content coding header.  The uncompressed
point form defined in <xref target="X9.62"/> (that is, a 65-octet sequence that starts with a
0x04 octet) forms the entirety of the <spanx style="verb">keyid</spanx>.  Note that this means that the
<spanx style="verb">keyid</spanx> parameter will not be valid UTF-8 as recommended in <xref target="RFC8188"/>.</t>

<t>A push service is not required to support more than 4096 octets of payload body
(see Section 7.2 of <xref target="RFC8030"/>).  Absent header (86 octets), padding (minimum
1 octet), and expansion for AEAD_AES_128_GCM (16 octets), this equates to, at
most, 3993 octets of plaintext.</t>

<t>An application server MUST NOT use other content encodings for push messages.
In particular, content encodings that compress could result in leaking of push
message contents.  The Content-Encoding header field therefore has exactly one
value, which is <spanx style="verb">aes128gcm</spanx>.  Multiple <spanx style="verb">aes128gcm</spanx> values are not permitted.</t>

<t>A user agent is not required to support multiple records.  A user agent MAY
ignore the <spanx style="verb">rs</spanx> parameter.  If a record size is unchecked, decryption will fail with
high probability for all valid cases.  The padding delimiter octet MUST be
checked; values other than 0x02 MUST cause the message to be discarded.</t>

</section>
<section anchor="example" title="Push Message Encryption Example">

<t>The following example shows a push message being sent to a push service.</t>

<figure><artwork type="example"><![CDATA[
POST /push/JzLQ3raZJfFBR0aqvOMsLrt54w4rJUsV HTTP/1.1
Host: push.example.net
TTL: 10
Content-Length: 145
Content-Encoding: aes128gcm

DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27ml
mlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A_yl95bQpu6cVPT
pK4Mqgkf1CXztLVBSt2Ks3oZwbuwXPXLWyouBWLVWGNWQexSgSxsj_Qulcy4a-fN
]]></artwork></figure>

<t>This example shows the ASCII-encoded string, "When I grow up, I want to be a
watermelon". The content body is shown here with line wrapping and URL-safe
base64url <xref target="RFC4648"/> encoding to meet presentation constraints.</t>

<t>The keys used are shown below using the uncompressed form <xref target="X9.62"/> encoded
using base64url.</t>

<figure><artwork type="example"><![CDATA[
   Authentication Secret: BTBZMqHH6r4Tts7J_aSIgg
   Receiver:
      private key: q1dXpw3UpT5VOmu_cf_v6ih07Aems3njxI-JWgLcM94
      public key: BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcx
                  aOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4
   Sender:
      private key: yfWPiYE-n46HLnH0KqZOF1fJJU3MYrct3AELtAQ-oRw
      public key: BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIg
                  Dll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8
]]></artwork></figure>

<t>Intermediate values for this example are included in <xref target="ex-intermediate"/>.</t>

</section>
<section anchor="iana" title="IANA Considerations">
<t>This document does not require any IANA actions.</t>
</section>


<section anchor="security-considerations" title="Security Considerations">

<t>The privacy and security considerations of <xref target="RFC8030"/> all apply to the use of
this mechanism.</t>

<t>The Security Considerations section of <xref target="RFC8188"/> describes the limitations of the
content encoding.  In particular, no HTTP header fields are protected by the
content encoding scheme.  A user agent MUST consider HTTP header fields to have
come from the push service.  Though header fields might be necessary for
processing an HTTP response correctly, they are not needed for correct
operation of the protocol.  An application on the user agent that uses
information from header fields to alter their processing of a push message is
exposed to a risk of attack by the push service.</t>

<t>The timing and length of communication cannot be hidden from the push service.
While an outside observer might see individual messages intermixed with each
other, the push service will see which application server is talking to which
user agent and the subscription that is used.  Additionally, the length of
messages could be revealed unless the padding provided by the content encoding
scheme is used to obscure length.</t>

<t>The user agent and application MUST verify that the public key they receive is
on the P-256 curve.  Failure to validate a public key can allow an attacker to
extract a private key.   The appropriate validation procedures are defined in
Section 4.3.7 of <xref target="X9.62"/> and, alternatively, in Section 5.6.2.3 of
<xref target="KEYAGREEMENT"/>.  This process consists of three steps:</t>

<t><list style="numbers">
  <t>Verify that Y is not the point at infinity (O),</t>
  <t>Verify that for Y = (x, y), both integers are in the correct interval,</t>
  <t>Ensure that (x, y) is a correct solution to the elliptic curve equation.</t>
</list></t>

<t>For these curves, implementers do not need to verify membership in
the correct subgroup.</t>

<t>In the event that this encryption scheme would need to be replaced, a new
content coding scheme could be defined.  In order to manage progressive
deployment of the new scheme, the user agent can expose information on the
content coding schemes that it supports.  The "supportedContentEncodings"
parameter of the Push API <xref target="API"/> is an example of how this might be done.</t>

</section>


  </middle>

  <back>

    <references title='Normative References'>


<reference anchor="ECDH" target="http://www.secg.org/">
  <front>
    <title>SEC 1: Elliptic Curve Cryptography</title>
    <author >
      <organization>SECG</organization>
    </author>
    <date month="May" year="2009"/>
  </front>
  <seriesInfo name="Version" value="2.0"/>
</reference>


<reference anchor="FIPS186" >
  <front>
    <title>Digital Signature Standard (DSS)</title>
    <author >
      <organization>National Institute of Standards and Technology
      (NIST)</organization>
    </author>
    <date year="2013" month="July"/>
  </front>
  <seriesInfo name="FIPS PUB" value="186-4"/>
<seriesInfo name='DOI' value='10.6028/NIST.FIPS.186-4' />
</reference>


<reference anchor="X9.62" >
  <front>
    <title>Public Key Cryptography for the Financial Services Industry: the
    Elliptic Curve Digital Signature Algorithm (ECDSA)</title>

    <author >
      <organization>ANSI</organization>
    </author>
    <date year="2005"/>
  </front>
  <seriesInfo name="ANSI" value="X9.62"/>
</reference>



<reference anchor="FIPS180-4" >
  <front>
    <title>Secure Hash Standard (SHS)</title>
    <author >
      <organization>National Institute of Standards and Technology (NIST)</organization>
    </author>
    <date year="2015" month="August"/>
  </front>
  <seriesInfo name="FIPS PUB" value="180-4"/>
<seriesInfo name='DOI' value='10.6028/NIST.FIPS.180-4' />
</reference>


<?rfc include="reference.RFC.8030"?>
<?rfc include="reference.RFC.8188"?>
<?rfc include="reference.RFC.2119"?>
<?rfc include="reference.RFC.8174"?>
<?rfc include="reference.RFC.4086"?>
<?rfc include="reference.RFC.5869"?>


    </references>

    <references title='Informative References'>

<reference anchor="API" target="https://www.w3.org/TR/push-api/">
  <front>
    <title>Push API</title>
    <author initials="P." surname="Beverloo">
      <organization></organization>
    </author>
    <author initials="M." surname="Thomson">
      <organization></organization>
    </author>
    <author initials="M." surname="van Ouwerkerk">
      <organization></organization>
    </author>
    <author initials="B." surname="Sullivan">
      <organization></organization>
    </author>
    <author initials="E." surname="Fullea">
      <organization></organization>
    </author>
    <date month="October" year="2017"/>
  </front>
</reference>



<reference anchor="KEYAGREEMENT" >
  <front>
    <title>Recommendation for Pair-Wise Key Establishment Schemes Using Discrete Logarithm Cryptography</title>
    <author initials="E." surname="Barker">
      <organization></organization>
    </author>
    <author initials="L." surname="Chen">
      <organization></organization>
    </author>
    <author initials="A." surname="Roginsky">
      <organization></organization>
    </author>
    <author initials="M." surname="Smid">
      <organization></organization>
    </author>
    <date year="2013" month="May"/>
  </front>
  <seriesInfo name="NIST" value="Special Publication 800-56A, Revision 2"/>
  <seriesInfo name='DOI' value='10.6028/NIST.SP.800-56Ar2' />
</reference>


<?rfc include="reference.RFC.7230"?>
<?rfc include="reference.RFC.2818"?>
<?rfc include="reference.RFC.4648"?>



    </references>


<section anchor="ex-intermediate" title="Intermediate Values for Encryption">

<t>The intermediate values calculated for the example in <xref target="example"/> are shown
here.  The base64url values in these examples include whitespace that can be
removed.</t>

<t>The following are inputs to the calculation:</t>

<t><list style="hanging">
  <t hangText='Plaintext:'>
  V2hlbiBJIGdyb3cgdXAsIEkgd2FudCB0byBiZSBhIHdhdGVybWVsb24</t>
  <t hangText='Application server public key (as_public):'>
  BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIg
Dll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8</t>
  <t hangText='Application server private key (as_private):'><vspace/>
  yfWPiYE-n46HLnH0KqZOF1fJJU3MYrct3AELtAQ-oRw</t>
  <t hangText='User agent public key (ua_public):'>
  BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcx
aOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4</t>
  <t hangText='User agent private key (ua_private):'>
  q1dXpw3UpT5VOmu_cf_v6ih07Aems3njxI-JWgLcM94</t>
  <t hangText='Salt:'>
  DGv6ra1nlYgDCS1FRnbzlw</t>
  <t hangText='Authentication secret (auth_secret):'>
  BTBZMqHH6r4Tts7J_aSIgg</t>
</list></t>

<t>Note that knowledge of just one of the private keys is necessary.  The
application server randomly generates the salt value, whereas salt is input to
the receiver.</t>

<t>This produces the following intermediate values:</t>

<t><list style="hanging">
  <t hangText='Shared ECDH secret (ecdh_secret):'><vspace/>
  kyrL1jIIOHEzg3sM2ZWRHDRB62YACZhhSlknJ672kSs</t>
  <t hangText='Pseudorandom key (PRK) for key combining (PRK_key):'>
  Snr3JMxaHVDXHWJn5wdC52WjpCtd2EIEGBykDcZW32k</t>
  <t hangText='Info for key combining (key_info):'>
  V2ViUHVzaDogaW5mbwAEJXGyvs3942BVGq8e0PTNNmwR
zr5VX4m8t7GGpTM5FzFo7OLr4BhZe9MEebhuPI-OztV3
ylkYfpJGmQ22ggCLDgT-M_SrDepxkU21WCP3O1SUj0Ew
bZIHMtu5pZpTKGSCIA5Zent7wmC6HCJ5mFgJkuk5cwAv
MBKiiujwa7t45ewP</t>
  <t hangText='Input keying material for content encryption key derivation (IKM):'>
  S4lYMb_L0FxCeq0WhDx813KgSYqU26kOyzWUdsXYyrg</t>
  <t hangText='PRK for content encryption (PRK):'><vspace/>
  09_eUZGrsvxChDCGRCdkLiDXrReGOEVeSCdCcPBSJSc</t>
  <t hangText='Info for content encryption key derivation (cek_info):'>
  Q29udGVudC1FbmNvZGluZzogYWVzMTI4Z2NtAA</t>
  <t hangText='Content encryption key (CEK):'>
  oIhVW04MRdy2XN9CiKLxTg</t>
  <t hangText='Info for content encryption nonce derivation (nonce_info):'>
  Q29udGVudC1FbmNvZGluZzogbm9uY2UA</t>
  <t hangText='Nonce (NONCE):'>
  4h_95klXJ5E_qnoN</t>
</list></t>



<t>The salt, record size of 4096, and application server public key produce an
86-octet header of: </t>
<t>
DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z
9KsN6nGRTbVYI_c7VJSPQTBtkgcy27ml mlMoZIIgDll6e3vCYLocInmYWAmS6Tlz
AC8wEqKK6PBru3jl7A8</t>

<t>The push message plaintext has the padding delimiter octet (0x02) appended to
produce: </t>
<t>
V2hlbiBJIGdyb3cgdXAsIEkgd2FudCB0 byBiZSBhIHdhdGVybWVsb24C  
</t>

<t>The
plaintext is then encrypted with AES-GCM, which emits ciphertext of:
</t>

<t>
8pfeW0KbunFT06SuDKoJH9Ql87S1QUrd irN6GcG7sFz1y1sqLgVi1VhjVkHsUoEs
bI_0LpXMuGvnzQ</t>

<t>The header and ciphertext are concatenated and produce the result shown in
<xref target="example"/>.</t>

</section>


  </back>


</rfc>

