<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd">
<?xml-stylesheet type="text/xsl" href="rfc2629.xslt" ?>

<rfc category="std" ipr="trust200902" docName="draft-eaton-oauth-bodyhash-00">

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

  <front>
    <title abbrev="OAuth Request Body Hash">OAuth Request Body Hash</title>

    <author initials="B.E" surname="Eaton" fullname="Brian Eaton">
      <organization>Google</organization>
      <address>
        <email>beaton@google.com</email>
      </address>
    </author>

    <author initials="E.HL" surname="Hammer-Lahav" fullname="Eran Hammer-Lahav" role="editor">
      <organization>
        Yahoo!
      </organization>
      <address>
        <email>eran@hueniverse.com</email>
        <uri>http://hueniverse.com</uri>
      </address>
    </author>

    <date year="2009"/>

    <abstract>
      <t>
        This specification extends the OAuth signature to include
        integrity checks on HTTP request bodies with content types other than
        <spanx style="verb">application/x-www-form-urlencoded</spanx>.
      </t>
    </abstract>

  </front>
  <middle>

    <section title="Introduction">
      <t>
        The OAuth Core specification <xref target="OAuth Core 1.0" /> provides
        body integrity checking only for
        <spanx style="verb">application/x-www-form-urlencoded</spanx>
        request bodies.  Other types of request bodies are left unsigned.
        An eavesdropper or man-in-the-middle who captures a signed request URI
        may be able to forward or replay that URI with a different HTTP
        request body.  Nonce checking and the use of https can mitigate this
        risk, but may not be available in some environments.  Even when 
        nonce checking and https are used, signing the request body provides
        an additional layer of defense.
      </t>
      <t>
        This specification describes a method to provide an integrity check
        on non-form-encoded request bodies.  The normal OAuth signature
        base string is enhanced by adding an additional parameter with the hash
        of the request body.  An unkeyed hash is used for the reasons
        described in <xref target="unkeyed" />.
      </t>
      <t>
        This extension is forward compatible: Service Providers
        that have not implemented this extension can verify requests
        sent by Consumers that have implemented this extension.
        If the Service Provider
        implements this specification the integrity of the body is guaranteed.
        If the Service Provider does not check body signatures, the remainder
        of the request will still validate using the OAuth Core signature
        algorithm.
      </t>
      <t>
        This specification is only useful when cryptographic signatures
        are used.  The OAuth <spanx style="verb">PLAINTEXT</spanx> signature algorithm does not provide
        integrity checks for any portion of the request and is not supported
        by this specification.
      </t>
	  <t>
	    The use of this specification with versions of the OAuth protocol other than
		<spanx style="verb">1.0</spanx> is undefined.
	  </t>
    </section>
    
    <section title="Notation and Conventions">
      <t>
        The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
        "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
        document are to be interpreted as described in
        <xref target="RFC2119" />.
      </t>
      <t>
        Unless otherwise noted, this specification is written as a direct
        continuation of <xref target="OAuth Core 1.0" />, inheriting the
        definitions and guidelines set by it.
      </t>
      <t>
        To make the specification easier to read, HTTP entity-bodies
        following the encoding
        requirements of the
        <spanx style="verb">application/x-www-form-urlencoded</spanx>
        content-type as defined by <xref target="W3C.REC-html40-19980424" />
        are simply referred to as form-encoded.
      </t>
    </section>

    <section title="The oauth_body_hash Parameter" anchor="parameter">
      
      <section title="Hash Algorithm">
        <t>
          The body hash algorithm is determined by the OAuth signature method used:

          <list style="symbols">
            <t>
              If the OAuth signature method is <spanx style="verb">HMAC-SHA1</spanx>
              or <spanx style="verb">RSA-SHA1</spanx>, SHA1 <xref target="RFC3174" /> MUST be used
              as the body hash algorithm.
            </t>
            <t>
              If the OAuth signature method is <spanx style="verb">PLAINTEXT</spanx>, use of this
              specification provides no security benefit and is NOT RECOMMENDED.
            </t>
          </list>
        </t>
        <t>
          New OAuth signature method specifications SHOULD specify the hash
          algorithm used to generate the body hash.
        </t>
      </section>
      
      <section title="Hash Calculation">
        <t>
          The value of the oauth_body_hash parameter SHALL be set as follows:
        </t>
        <t>
          <list style="numbers">
            <t>
              The body hash value is calculated by executing the selected
              hash algorithm over the request body.  The request body is
              the entity body as defined in <xref target="RFC2616" />
              section 7.2.  If the request does not have an entity body, the
              hash should be taken over the empty string.
            </t>
            <t>
              The calculated body hash value is encoded using
              Base64 per <xref target="RFC2045" />.
            </t>
          </list>
        </t>
      </section>
      
    </section>
    
    <section title="Changes to OAuth Request Signing">
      
      <section title="Sending Requests">
        <t>
          OAuth Consumers
          include the oauth_body_hash parameter according to the rules
          described in this section, and continue to sign requests in the
          manner described by <xref target="OAuth Core 1.0" /> section 9.
        </t>
        
        <section title="When to Include the Body Hash" anchor="when_to_include">
          <t>
            Not all requests should contain the oauth_body_hash parameter.
          </t>
          <t>
            <list style="symbols">
              <t>
                OAuth Consumers SHOULD NOT include an oauth_body_hash parameter 
                when making Request Token or Access Token OAuth requests.
              </t>
              <t>
                OAuth Consumers MUST NOT include an oauth_body_hash parameter on
                requests with form-encoded request bodies.  The presence or absence
              </t>
              <t>
                OAuth Consumers SHOULD include the oauth_body_hash parameter on all
                other requests.
              </t>
            </list>
          </t>
          <t>
            Some OAuth Service
            Providers have implemented Request Token and Access Token endpoints
            that reject requests that include unknown parameters.  Sending
            an oauth_body_hash parameter to such endpoints will cause protocol
            failures.  The oauth_body_hash parameter does not provide additional
            security for OAuth request token and access token requests since
            all of 
            the protocol parameters are signed by the OAuth Core signature
            mechanism.  Omitting the parameter improves interoperability without
            reducing security.
          </t>
          <t>
            The presence or absence
            of the oauth_body_hash parameter is used to indicate to Service
            Providers how they should check the integrity of the request body.
            If no oauth_body_hash parameter is present, that indicates that
            the request body is form-encoded and signed using the OAuth Core
            signature algorithm.  If the oauth_body_hash parameter is present,
            the body is signed according to this extension.  Including
            an oauth_body_hash on form-encoded requests would make it impossible
            for Service Providers to determine which body signing mechanism
            was used for the request.  This ambiguity would, in turn, allow
            the attack described in <xref target="removing_body" />.
          </t>
        </section>
        
        <section title="Adding oauth_body_hash to Requests">
          <t>
            To include the oauth_body_hash parameter in OAuth-authenticated
            requests, Consumers SHALL:
            <list style="numbers">
              <t>
                Determine whether the parameter should be included using
                the rules described in <xref target="when_to_include" />.
              </t>
              <t>
                Calculate the body hash value as described in 
                <xref target="parameter" />
              </t>
              <t>
                Set the oauth_body_hash parameter to the obtained value.
              </t>
              <t>
                Sign the request as per 
                <xref target="OAuth Core 1.0" /> section 9.
                The oauth_body_hash parameter MUST be
                included in the Signature Base String together with the
                other request parameters.
              </t>
              <t>
                Transmit the oauth_body_hash parameter along with the other
                OAuth Protocol parameters in the signed OAuth request.
              </t>
            </list>
          </t>
        </section>
        
      </section>
      
      <section title="Verifying Requests">
        <t>
          Service Providers verify the integrity of request bodies
          by verifying the OAuth signature as described in
          <xref target="OAuth Core 1.0" /> and also verifying the value of the
          oauth_body_hash parameter.
        </t>
        
        <section title="When to Verify the Hash">
          <t>
            Service Providers MUST decide whether the oauth_body_hash parameter
            is necessary according to the rules described in
            <xref target="when_to_include" />.
          </t>
          <t>
            If the request MUST NOT have an oauth_body_hash parameter,
            Service Providers MUST verify that no oauth_body_hash parameter is
            present.
            In particular, any requests with a form-encoded content-type
            and an oauth_body_hash parameter MUST be rejected because of
            of the attack described in <xref target="removing_body" />.
          </t>
          <t>
            If the request should have an oauth_body_hash parameter
            but does not contain one, the request was sent by a
            Consumer that does not support this extension.  The integrity
            of the request body cannot be checked using this specification.
            Service Providers MAY accept such requests for interoperability,
            or they MAY reject such requests in favor of security.
          </t>
        </section>
        
        <section title="Verifying the Hash">
          <t>
            If the Service Provider determines body hash verification is
            necessary, the Service Provider calculates the expected
            body hash for the request as described in
            <xref target="parameter" />.
          </t>
          <t>
            The Service Provider then compares the expected body hash with
            the value sent by the Consumer in the oauth_body_hash parameter.
            If the values match, the body is intact and the request can
            proceed.
          </t>
          <t>
            If the values do not match, the request MUST be rejected.
          </t>
          <t>
            Rather than comparing text values, Service Providers MAY
            Base64 decode (per <xref target="RFC2045" />) the oauth_body_hash parameter and perform the
            comparison on the octets of the hash.  This reduces the risk of
            minor differences in URI encoding or Base64 encoding causing
            spurious integrity check failures.
          </t>
        </section>
        
      </section>
      
    </section>

    <section title="IANA Considerations" anchor="IANA">
      <t>
        This memo includes no request to IANA.
      </t>
    </section>

    <section title="Security Considerations">
      
      <section title="Only Trust What is Signed">
        <t>
          Many factors besides the bytes of the request body can
          influence the interpretation of the body of the HTTP
          request.  For example, a content-type or content-encoding
          header can change the way a server handles an HTTP request.
          This specification does not include an integrity check
          on the HTTP request headers.  OAuth deployments whose
          security could be impacted by an attacker who tampers with
          HTTP request headers should use other mechanisms (such as HTTPS)
          to protect the confidentiality and integrity of the entire HTTP
          request.
        </t>
      </section>
      
      <section title="Deprecation of SHA-1">
        <t>
          In <xref target="NIST 800-107" />
          the NIST Computer Security Division has recommended that applications
          stop using SHA-1 for digital signatures.
          As of the time
          of this writing, all of the cryptographic signature schemes defined
          for OAuth are based on SHA-1.  OAuth signature methods based on
          stronger hash functions need to be developed, and those signature
          methods will need to specify algorithms for calculating the
          oauth_body_hash as well.
        </t>
      </section>
      
    </section>

    <appendix title="Examples">
      
      <appendix title="Example PUT Request">
        <t>
          Sample HTTP request:
          <figure>
            <artwork>
  PUT /resource HTTP/1.1
  Host: www.example.com
  Content-Type: text/plain
  Content-Length: 12

  Hello World!
            </artwork>
          </figure>
        </t>
        <t>
          Base64 encoded SHA-1 hash of the body:
          <figure>
            <artwork>
  Lve95gjOVATpfV8EL5X4nxwjKHE=
            </artwork>
          </figure>
        </t>
        <t>
          Signature Base String (with some line breaks added for
          readability):
          <figure>
            <artwork>
  PUT&amp;http%3A%2F%2Fwww.example.com%2Fresource&amp;oauth_body_hash%3D
  Lve95gjOVATpfV8EL5X4nxwjKHE%253D%26oauth_consumer_key%3Dconsum
  er%26oauth_nonce%3D10369470270925%26oauth_signature_method%3DH
  MAC-SHA1%26oauth_timestamp%3D1236874236%26oauth_token%3Dtoken%
  26oauth_version%3D1.0
            </artwork>
          </figure>
        </t>
        <t>
          Signed request with body hash (with some line breaks added for
          readability):
          <figure>
            <artwork>
  PUT /resource HTTP/1.1
  Host: www.example.com
  Authorization: OAuth realm="http%3A%2F%2Fwww.example.com",
      oauth_body_hash="Lve95gjOVATpfV8EL5X4nxwjKHE%3D",
      oauth_token="token",
      oauth_consumer_key="consumer",
      oauth_signature_method="HMAC-SHA1",
      oauth_timestamp="1236874155", oauth_nonce="10288510250934",
      oauth_version="1.0",
      oauth_signature="08bUFF%2Fjmp59mWB7cSgCYBUpJ0U%3D"
  Content-Type: text/plain
  Content-Length: 12

  Hello World!
            </artwork>
          </figure>
        </t>
      </appendix>
      
      <appendix title="Example GET Request">
        <t>
          Sample HTTP request:
          <figure>
            <artwork>
  GET /resource HTTP/1.1
  Host: www.example.com

            </artwork>
          </figure>
        </t>
        <t>
          Base64 encoded SHA-1 hash of the (non-existent) body:
          <figure>
            <artwork>
  2jmj7l5rSw0yVb/vlWAYkK/YBwk=
            </artwork>
          </figure>
        </t>
        <t>
          Signature Base String (line breaks added for readability):
          <figure>
            <artwork>
  GET&amp;http%3A%2F%2Fwww.example.com%2Fresource&amp;oauth_body_hash%3D2jmj7
  l5rSw0yVb%252FvlWAYkK%252FYBwk%253D%26oauth_consumer_key%3Dconsumer
  %26oauth_nonce%3D8628868109991%26oauth_signature_method%3DHMAC-SHA1
  %26oauth_timestamp%3D1238395022%26oauth_token%3Dtoken%26oauth_versi
  on%3D1.0
            </artwork>
          </figure>
        </t>
        <t>
          Signed request with body hash (with some line breaks added for
          readability):
          <figure>
            <artwork>
  GET /resource HTTP/1.1
  Host: www.example.com
  Authorization: OAuth realm="http%3A%2F%2Fwww.example.com",
      oauth_body_hash="2jmj7l5rSw0yVb/vlWAYkK/YBwk%3D",
      oauth_token="token",
      oauth_consumer_key="consumer",
      oauth_signature_method="HMAC-SHA1",
      oauth_timestamp="1236874155", oauth_nonce="10288510250934",
      oauth_version="1.0",
      oauth_signature="08bUFF%2Fjmp59mWB7cSgCYBUpJ0U%3D"
            </artwork>
          </figure>
        </t>
      </appendix>
    </appendix>

    <appendix title="Obtaining the HTTP Entity Body">
      <t>
        Not all programming platforms provide an easy mechanism to obtain
        the raw octets of the HTTP entity body.  Reading the entity body
        as raw octets may have side effects, such as inhibiting automatic
        character set conversion.  Some platforms do not allow
        direct access to the entity body at all if the content-type 
        is application/x-www-form-urlencoded.  Transfer-encodings such as gzip
        also complicate implementation.  On the other hand,
        reading the entity body as text may perform lossy conversions
        that prevent recovery of the original octet stream in some
        situtations.  Character set conversions are not always one-to-one
        mappings, so solutions that rely on converting text back to the
        original octet stream are likely to fail in environments with
        multibyte characters.
      </t>
      <t>
        All of these factors, and others, can make it difficult to drop in a
        "verify-the-signature" filtering layer prior to other request
        processing.  The verification layer must consider the assumptions
        of downstream processing code on the state of the request body.
      </t>
      <t>
        Implementers of this specification should be aware of these
        difficulties and consider the best way to address them in their
        programming frameworks.
      </t>
    </appendix>

    <appendix anchor="unkeyed" title="Unkeyed vs Keyed Hash Algorithms">
      <t>
        This specification deliberately uses an unkeyed hash algorithm
        (SHA-1) to provide an integrity check on the body instead of
        a keyed hash algorithm such as HMAC-SHA1.  This decision was made
        because signing arbitrary octet streams is poor cryptographic hygiene.
        It can lead to unexpected problems with cryptographic protocols.
      </t>
      <t>
        For example, consider a proxy that uses OAuth to add authentication
        information to requests sent by an untrusted third-party.  If the
        proxy signs arbitrary octet streams, the third-party can use the
        proxy as an oracle to forge authentication messages.
      </t>
      <t>
        Including the result of an unkeyed hash in the normal signature 
        base string allows the proxy to add an integrity check on the
        original message without creating a signing oracle.
      </t>
    </appendix>

    <appendix anchor="removing_body" title="Preventing Removal of Request Bodies">
      <t>
        This specification requires that Consumers not send the oauth_body_hash
        parameter on requests with form-encoded bodies, and requires that
        Service Providers reject requests that have form-encoded bodies
        and an oauth_body_hash parameter.  These restrictions are necessary
        in order to prevent a MITM from removing non-form-encoded request
        bodies.  The attack would work as follows:
      </t>
      <t>
        <list style="numbers">
          <t>
            Consumer signs a request with a non-form-encoded request body
            and includes a matching content-type header such as
            "application/json" or "text/plain".  The oauth_body_hash parameter
            is included as well.
          </t>
          <t>
            MITM intercepts request and modifies the content-type of the
            request to be "application/x-www-form-urlencoded".  The MITM
            also removes the request body.  The request is then forwarded
            to the Service Provider.
          </t>
          <t>
            The Service Provider receives the request and the signature
            validates according to the OAuth Core signature specification.
          </t>
          <t>
            The Service Provider then needs to decide whether to check
            the oauth_body_hash value as well.  Since the request content-type
            is form-encoded, the Service Provider does not check the
            oauth_body_hash.
          </t>
          <t>
            The removal of the body goes undetected.  
          </t>
        </list>
      </t>
      <t>
        The impact of this attack is probably minimal.  The attacker can
        remove the request body, but cannot replace it with their own.  Stil,
        the goal of this specification is guaranteeing body integrity
        when both Consumers and Service Providers use the oauth_body_hash
        parameter.  Out of an excess of caution, this specification requires
        that Service Providers reject request that have both a form-encoded
        request body and an oauth_body_hash parameter.
      </t>
      <t>
        An alternate solution, requiring that Service Providers check the
        oauth_body_hash parameter even on form-encoded request bodies, was
        rejected due to implementation challenges.  Some web development
        frameworks block access to the raw entity body for form-encoded
        requests.
      </t>
    </appendix>

    <appendix title="Acknowledgements">
      <t>
        Several members of the community contributed valuable feedback and suggestions, including
        Allen Tom, Ben Laurie, Dirk Balfanz, George Fletcher, John Panzer, Louis Ryan, and Marc Worrell.
      </t>
    </appendix>

  </middle>

  <back>
    
    <references title="Normative References">

      <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2045.xml"?>
      <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
      <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2616.xml"?>
      <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.3174.xml"?>
      <?rfc include="http://xml.resource.org/public/rfc/bibxml4/reference.W3C.REC-html40-19980424.xml"?>
      
      <reference anchor="OAuth Core 1.0">
        <front>
          <title>OAuth Core 1.0</title>
          <author initials="OCW" surname="OAuth" fullname="OAuth Core Workgroup">
            <organization />
          </author>
        </front>
        <format type="HTML" target="http://oauth.net/core/1.0" />
      </reference>

    </references>

    <references title="Informative References">

      <reference anchor="NIST 800-107">
        <front>
          <title>Special Publication 800-107, Recommendation for Applications Using Approved Hash Algorithms</title>
          <author initials="Q" surname="Dang" fullname="Quynh Dang">
            <organization>NIST Computer Security Division</organization>
          </author>
        </front>
        <format type="PDF" target="http://csrc.nist.gov/publications/nistpubs/800-107/NIST-SP-800-107.pdf" />
      </reference>

    </references>
    
  </back>

</rfc>
