Extensions to Salted Challenge Response (SCRAM) for 2 factor authentication
Alexey Melnikov
Isode Ltd
Alexey.Melnikov@isode.com
2024
Security
SASL
SCRAM
KITTEN
2FA
reauthentication
Passkey
This specification describes an extension to family of Simple Authentication and Security Layer (SASL; RFC 4422) authentication mechanisms called the Salted Challenge Response Authentication Mechanism (SCRAM), which provides support for 2 factor authentication. It also includes a separate extension for quick reauthentication.
This specification also gives 2 examples of second factors: TOTP (RFC 6238) and FIDO CTAP1/U2F (Passkey).

Introduction
SCRAM [RFC5802] is a password based SASL [RFC4422] authentication mechanism that provides (among other things) mutual authentication and binding to an external security layer such as TLS.
Two-factor authentication (2FA) is a way to add additional security to an authentication exchange. The first "factor" is a password. The second "factor" is a verification code retrieved from an application on a mobile device or computer. 2FA is conceptually similar to a security token device that banks in some countries require for online banking. Some examples of 2FA systems are OTP (one-time password) and TOTP (Time-based One-time Password algorithm, such as [RFC6238]).
This specification describes an extension to SCRAM to provide 2 factor authentication. SCRAM already relies on passwords for authentication. This document specifies how second "factors" can be incorporated into SCRAM authentication. It also includes a separate (but frequently used together with the 2 factor authentication) extension for quick reauthentication.

Conventions Used in This Document
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 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
Formal syntax is defined by [RFC5234] including the core rules defined in Appendix B of [RFC5234].
Example lines prefaced by "C:" are sent by the client and ones prefaced by "S:" by the server. If a single "C:" or "S:" label applies to multiple lines, then the line breaks between those lines are for editorial clarity only, and are not part of the actual protocol exchange.

Terminology
This document uses several terms defined in [RFC4949] ("Internet Security Glossary") including the following: authentication, authentication exchange, authentication information, brute force, challenge-response, cryptographic hash function, dictionary attack, eavesdropping, hash result, keyed hash, man-in-the-middle, nonce, one-way encryption function, password, replay attack and salt. Readers not familiar with these terms should use that glossary as a reference. Other terms defined in [RFC5802] are also used in this document.

SCRAM Extension for 2FA
This extension doesn't add any extra roundtrips to SCRAM authentication. SCRAM was designed to be extensible, so it allows for optional and mandatory attributes, which covered by MAC codes. Second "factors" are conveyed in the second message ("client-final-message-without-proof" ABNF production) sent from the client to the server.
This extension doesn't change how the client authenticates the server. This extension also doesn't cover enrollment with a 2FA system, such enrollment happends out-of-band.
The server authenticates the client after receiving the second message as described in Section 3 of [RFC5802]. If the client included "type" and "second-factor" attributes defined in this document (see Section 5) and the server supports the specified second factor type, the server verifies content of the "second-factor" according to the "type". If the second factor verification fails, the server MUST fail authentication and SHOULD return either "replayed-second-factor" or "invalid-second-factor" error in the "e" attribute.
[[It would be possible to make the extra attributes mandatory by using SCRAM's "m=", but the text above doesn't do that. This is one of open issues to resolve.]]

SCRAM Extension for reauthentication
This reauthentication extension to SCRAM allows the server to return a token that can be used for quick reauthentication and bypasses 2 factor authentication prompt to the user. The reauthentication token is a randomly generated value [RFC4086]. The reauthentication token is returned in the "o" attribute that is appended to the end of the "server-final-message".
[[Note: it would be possible to extend SCRAM itself to do reauthentication, by including an earlier received reauthentication token in the "client-first-message" of a subsequent SCRAM authentication. This will also turn off the server checking for 2 factor authentication information, unless the reauthentication attempt is rejected by the server. In the meantime, this document presents a couple of other alternatives on how to use other SASL mechanisms with the reauthentication token.]]
When the HT-* mechanism [draft-schmaus-kitten-sasl-ht] is used for the reauthentication after a successful SCRAM authentication, the reauthentication token is the SASL-HT token (see [draft-schmaus-kitten-sasl-ht]). [[Note that the HT hash should probably match the SCRAM hash used or "be better".]]

Formal Syntax
This document defines the following new SCRAM attributes:
- t: This attribute specifies the type of second factor. This document defines two possible types: "totp" (see Section 6) and "ctap1" (see Section 8). If this attribute is specified, the "f" attribute MUST also be specified.
- f: This attribute specifies the value of the second factor. For "t=totp" it is 6 digit decimal number. [[Use 8 digits per Rick van Rein?]] This attribute MUST be ignored unless the "t" attribute is also specified.
- l: This attribute is used by some second factors (e.g. CTAP1) to specify the challenge returned by the SCRAM server.
- o: This attribute specifies the base64-encoded value of the reauthentication token.
The following syntax specification uses the Augmented Backus-Naur Form (ABNF) notation as specified in [RFC5234].

   type            = "t=" type-value
                 ; Complies with "attr-val" syntax.
                 ; Returned in client-final-message.
   type-value      = "totp" / "ctap1" / value
                 ; Type of second factor.
                 ; Should be registered with IANA.
   second-factor   = "f=" second-factor-value
                 ; Complies with "attr-val" syntax.
                 ; Returned in client-final-message.
   second-factor-value = 6DIGIT / value
                 ; 6DIGIT when "t=totp".
   server-challenge = "l=" base64
                 ; Data returned by SCRAM server in server-first-message
                 ; to be used for construction of second-factor-value.

   server-error-value-ext =
           "replayed-second-factor" /
           "invalid-second-factor" /
           "second-factor-value-missing"

   value = <as defined in RFC 5802>

   reauth-token = "o=" base64
                  ; base64 encoding of reauthentication
                  ; token.
                  ; Returned in server-final-message.

Use of TOTP with SCRAM
When TOTP is used with SCRAM, the following values for "t" and "f" attributes (see Section 5 for their generic syntax) are used:
- t: This attribute specifies the type of second factor. For TOTP the value is "totp". If this attribute is specified, the "f" attribute MUST also be specified.
- f: This attribute specifies the value of the second factor. For "t=totp" it is 6 digit decimal number. This attribute MUST be ignored unless the "t" attribute is also specified.
A TOTP URI is specified with the following ABNF:

totp-uri = "otpauth" "://" "totp/" label "?secret=" secret
           "&issuer=" issuer
label = issuer (":" / "%3A") identity
identity = 1*CHAR ; URI-encoded SASL identity
secret = 40 * HEXCHAR ; Base32 (hex) encoded secret with no padding.
issuer = 1*CHAR ; Issuer name.

Example
The following example extends the example from Section 5 of [RFC5802] to demonstrate use of TOTP:

   C: n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL
   S: r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,
      i=4096
   C: c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,
      t=totp,f=776804,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=
   S: v=lz59pqV8S7suAoZWja4dJRkFsKQ=

Please note that TOTP extension described in this document works in the same way with SCRAM-SHA-256/SCRAM-SHA-256-PLUS, SCRAM-SHA-512/SCRAM-SHA-512-PLUS or any other SCRAM variants that use other hash functions.

Use of FIDO CTAP1/U2F with SCRAM
Note that this section describes steps to achieve Client-to-Authenticator Protocols v1 (CTAP1)/U2F 2FA in terms of CTAP2 API.
When FIDO CTAP1/U2F is used with SCRAM, the following values for "t", "l" and "f" attributes (see Section 5 for their generic syntax) are used:
- t: This attribute specifies the type of second factor. For FIDO CTAP1/U2F the value is "ctap1". If this attribute is specified, the "f" attribute MUST also be specified.
- l: base64-encoded challenge as returned by SCRAM server.
- f: This attribute specifies the value of the second factor. For "t=ctap1" it is the CBOR-serialized [RFC8949] value of authenticatorGetAssertionResponse map described below. This attribute MUST be ignored unless the "t" attribute is also specified.
SCRAM client sends U2F_AUTHENTICATE command formatted as specified in [FIDO-U2F-Raw-Message-Formats] to the authenticator (e.g. a USB or NFC device).
The "P1" framing parameter to U2F_AUTHENTICATE is a single octet parameter defined as follows:
- If "up" is set to false, set it to 0x08 (dont-enforce-user-presence-and-sign).
- For USB, set it to 0x07 (check-only). This should prevent call getting blocked on waiting for user input. If response returns success, then call again setting the enforce-user-presence-and-sign.
- For NFC, set it to 0x03 (enforce-user-presence-and-sign). The tap has already provided the presence and won't block.
The "P2" framing parameter to U2F_AUTHENTICATE is a single octet that always has the value 0x00.
The "request-data" framing parameter (u2fAuthenticateRequest) to U2F_AUTHENTICATE is constructed as follows:
- The clientData structure (see Section 5.8.1 of [W3C_webauthn_3]) is filled in as follows:
  - The "type" field has value "webauthn.get".
  - The "challenge" field contains the base64url encoding of the challenge provided by the Relying Party. This is the value of the "l" attribute described above. (Note, possibly need to base64-decode and base64url-encode.)
  - The "origin" field contains the origin of the SCRAM client in the format specified in [RFC6454], for example "smtp://client.example.net:587".
  - Other fields are specified in Section 5.8.1 of [W3C_webauthn_3].
- Use clientDataHash parameter of [CTAP2] request as CTAP1/U2F challenge parameter (32 bytes).
- Let rpIdHash be a byte string of size 32 initialized with SHA-256 hash of rp.id parameter as CTAP1/U2F application parameter (32 bytes). (The rp.id parameter is the hostname of the SCRAM server.)
- Let credentialId is the byte string initialized with the id for this PublicKeyCredentialDescriptor.
- Let keyHandleLength be a byte initialized with length of credentialId byte string.
- Let u2fAuthenticateRequest be a byte string with the following structure:
Length (in bytes) | Description | Value
32 | Challenge parameter | Initialized with clientDataHash parameter bytes.
32 | Application parameter | Initialized with rpIdHash bytes.
1 | Key handle length | Initialized with keyHandleLength's value.
keyHandleLength | Key handle | Initialized with credentialId bytes.
Generate authenticatorData from the U2F authentication response message received from the authenticator:
- Copy bits 0 (the UP bit) and bit 1 from the CTAP2/U2F response user presence byte to bits 0 and 1 of the CTAP2 flags, respectively. Set all other bits of flags to zero. Note: bit zero is the least significant bit. See also Authenticator Data section of [W3C_webauthn_3].
- Let signCount be a 4-byte unsigned integer initialized with CTAP1/U2F response counter field.
Let authenticatorData is a byte string of following structure:
Length (in bytes) | Description | Value
32 | SHA-256 hash of the rp.id. | Initialized with rpIdHash bytes.
1 | Flags | Initialized with flags' value.
4 | Signature counter (signCount) | Initialized with signCount bytes.
Let authenticatorGetAssertionResponse be a CBOR map with the following keys whose values are as follows:
- Set 0x01 with the credential from allowList that whose response succeeded.
- Set 0x02 with authenticatorData bytes.
- Set 0x03 with signature field from CTAP1/U2F authentication response message. Note: An ASN.1-encoded ECDSA signature value ranges over 8-72 bytes in length.

Open Issues
Simon Josefsson: should this be a new SASL mechanism name, e.g. CROTP-SHA-XXX?
Should we pick between TOTP and FIDO2 (CTAP1)?
Rick van Rein: specify a HOTP variant as well?
Rick van Rein: use TOTP with 6 or 8 digits? Register both variants?

Security Considerations
An OTP value is a sensitive piece of data and thus should only be sent over a secure channel (such as TLS). Otherwise an attacker might be able to intercept it and reuse it for authentication to the SCRAM server.
TBD

IANA Considerations
IANA is requested to update the definition of the SASL family SCRAM in the SASL Mechanism registry established by [RFC4422] to also point to this document.
IANA is also requested to create a new subregistry of "SASL mechanism" for registering second factor schemes used in the "t" attribute as specified in this document.
The registration template is as follows:

SCRAM Second Factor Scheme Name:
Pointer to specification text:
Notes (optional):

The registration procedure for the above subregistry is Expert Review.
IANA is requested to register a new value in the subregistry defined above:

SCRAM Second Factor Scheme Name: TOTP
Pointer to specification text: [[ this document ]]
Notes (optional): (none)

Acknowledgements
Thank you to Stephen Farrell for motivating creation of this document and to Dave Cridland for describing how TOTP can be used with XMPP in XEP-0400. Thank you to Rick van Rein, Simon Josefsson and Ludovic Bocquet for comments and corrections, but all final errors in this document remain mine.

Normative References
[FIDO-U2F-Raw-Message-Formats]

FIDO U2F Raw Message Formats
Dirk Balfanz
Jakob Ehrensvard
Juan Lang
April 2017
FIDO fido-u2f-raw-message-formats-v1.2-ps-20170411
[CTAP2]

Client to Authenticator Protocol (CTAP)
John Bradley
Jeff Hodges
Michael B. Jones
Akshay Kumar
Rolf Lindemann
Johan Verrept
June 2022
FIDO fido-client-to-authenticator-protocol-v2.1-ps-errata-20220621
[W3C_webauthn_3]

Web Authentication: An API for accessing Public Key Credentials - Level 3
W3C WD webauthn-3
W3C webauthn-3
[draft-schmaus-kitten-sasl-ht]

The Hashed Token SASL Mechanism
Florian Schmaus
FAU Erlangen-Nürnberg
Christoph Egger
FAU Erlangen-Nürnberg
6 November 2022
Internet-Draft draft-schmaus-kitten-sasl-ht-09
[RFC5802]
[RFC4422]
[RFC6238]
[RFC2119]
[RFC8174]
[RFC5234]
[RFC4949]
[RFC4086]
[RFC8949]
[RFC6454]