バージョンごとのドキュメント一覧

55.3. SASL認証 #

<title>SASL Authentication</title>

<firstterm>SASL</firstterm> is a framework for authentication in connection-oriented protocols. At the moment, <productname>PostgreSQL</productname> implements two SASL authentication mechanisms, SCRAM-SHA-256 and SCRAM-SHA-256-PLUS. More might be added in the future. The below steps illustrate how SASL authentication is performed in general, while the next subsection gives more details on SCRAM-SHA-256 and SCRAM-SHA-256-PLUS. SASLは接続指向のプロトコルでの認証のためのフレームワークです。 現時点ではPostgreSQLは2つのSASLの認証機構、SCRAM-SHA-256とSCRAM-SHA-256-PLUSを実装しています。 将来には他の機構が追加されるかもしれません。 以下の手順は、SASLの認証が一般的にどのように行われるかを示したものですが、次の副節ではSCRAM-SHA-256とSCRAM-SHA-256-PLUSにおけるより詳細について説明します。

SASL認証のメッセージフロー

<title>SASL Authentication Message Flow</title>
  1. To begin a SASL authentication exchange, the server sends an AuthenticationSASL message. It includes a list of SASL authentication mechanisms that the server can accept, in the server's preferred order. SASL認証の交換を開始するため、サーバはAuthenticationSASLメッセージを送信します。 これにはサーバが受け付けることができるSASLの認証機構を、サーバにとって望ましいものから順に並べたリストが含まれます。

  2. The client selects one of the supported mechanisms from the list, and sends a SASLInitialResponse message to the server. The message includes the name of the selected mechanism, and an optional Initial Client Response, if the selected mechanism uses that. クライアントはリストからサポートされる機構を1つ選択し、サーバにSASLInitialResponseメッセージを送信します。 このメッセージには選択された機構の名前が含まれ、また選択した機構がInitial Client Response(最初のクライアントの応答)を使用するなら、オプションでそれも含まれます。

  3. One or more server-challenge and client-response message will follow. Each server-challenge is sent in an AuthenticationSASLContinue message, followed by a response from client in a SASLResponse message. The particulars of the messages are mechanism specific. サーバのチャレンジメッセージおよびクライアントのレスポンスメッセージが1つ以上続きます。 サーバのチャレンジはそれぞれがAuthenticationSASLContinueメッセージで送信され、それにクライアントからのレスポンスがSASLResponseメッセージで続きます。 メッセージの詳細は機構に固有のものです。

  4. Finally, when the authentication exchange is completed successfully, the server sends an AuthenticationSASLFinal message, followed immediately by an AuthenticationOk message. The AuthenticationSASLFinal contains additional server-to-client data, whose content is particular to the selected authentication mechanism. If the authentication mechanism doesn't use additional data that's sent at completion, the AuthenticationSASLFinal message is not sent. 最後に、認証の交換が成功裏に終了すると、サーバはAuthenticationSASLFinalメッセージを送信し、その直後にAuthenticationOkメッセージを送信します。 AuthenticationSASLFinalにはサーバからクライアントへの追加のデータが含まれ、その内容は選択した認証機構毎に異なります。 完了時に送信する追加データを認証機構が使用していない場合、AuthenticationSASLFinalメッセージは送信されません。

On error, the server can abort the authentication at any stage, and send an ErrorMessage. エラーが発生したときは、サーバは認証を任意の段階で終了してErrorMessageを送信することができます。

55.3.1. SCRAM-SHA-256 認証 #

<title>SCRAM-SHA-256 Authentication</title>

The implemented SASL mechanisms at the moment are <literal>SCRAM-SHA-256</literal> and its variant with channel binding <literal>SCRAM-SHA-256-PLUS</literal>. They are described in detail in <ulink url="https://tools.ietf.org/html/rfc7677">RFC 7677</ulink> and <ulink url="https://tools.ietf.org/html/rfc5802">RFC 5802</ulink>. 今のところ実装されているSASL機構はSCRAM-SHA-256とチャンネルバインディングを伴う変種のSCRAM-SHA-256-PLUSです。 詳細はRFC 7677およびRFC 5802で説明されています。

When SCRAM-SHA-256 is used in PostgreSQL, the server will ignore the user name that the client sends in the <structname>client-first-message</structname>. The user name that was already sent in the startup message is used instead. <productname>PostgreSQL</productname> supports multiple character encodings, while SCRAM dictates UTF-8 to be used for the user name, so it might be impossible to represent the PostgreSQL user name in UTF-8. PostgreSQLでSCRAM-SHA-256を使用する場合、クライアントがclient-first-messageで送信するユーザ名をサーバは無視します。 その代わりに、開始メッセージで送信済みのユーザ名が使用されます。 SCRAMはユーザ名としてUTF-8の使用を指示していますが、PostgreSQLは複数の文字符号化方式をサポートするため、PostgreSQLのユーザ名をUTF-8で表現できないかもしれません。

The SCRAM specification dictates that the password is also in UTF-8, and is processed with the <firstterm>SASLprep</firstterm> algorithm. <productname>PostgreSQL</productname>, however, does not require UTF-8 to be used for the password. When a user's password is set, it is processed with SASLprep as if it was in UTF-8, regardless of the actual encoding used. However, if it is not a legal UTF-8 byte sequence, or it contains UTF-8 byte sequences that are prohibited by the SASLprep algorithm, the raw password will be used without SASLprep processing, instead of throwing an error. This allows the password to be normalized when it is in UTF-8, but still allows a non-UTF-8 password to be used, and doesn't require the system to know which encoding the password is in. SCRAMの仕様ではパスワードもUTF-8であり、SASLprepアルゴリズムで処理されることが規定されています。 しかしPostgreSQLではパスワードにUTF-8が使用されることを必須としていません。 ユーザのパスワードが設定されたとき、実際に使用された符号化方式に関わらず、それがUTF-8であるかのようにSASLprepで処理されます。 しかし、それが正当なUTF-8バイト列でない場合、あるいはSASLprepが禁止するUTF-8バイト列を含む場合、エラーを発生させるのではなく、SASLprep処理のない生のパスワードが使用されます。 これにより、パスワードがUTF-8であればそれを正規化できる一方で、UTF-8以外のパスワードを使用することもでき、またシステムもパスワードがどの符号化であるかを知る必要もありません。

<firstterm>Channel binding</firstterm> is supported in PostgreSQL builds with SSL support. The SASL mechanism name for SCRAM with channel binding is <literal>SCRAM-SHA-256-PLUS</literal>. The channel binding type used by PostgreSQL is <literal>tls-server-end-point</literal>. SSLをサポートするPostgreSQLビルドでチャンネルバインディングがサポートされます。 チャンネルバインディングを伴うSCRAMに対するSASL機構名はSCRAM-SHA-256-PLUSです。 PostgreSQLで使われるチャンネルバインディングのタイプはtls-server-end-pointです。

In <acronym>SCRAM</acronym> without channel binding, the server chooses a random number that is transmitted to the client to be mixed with the user-supplied password in the transmitted password hash. While this prevents the password hash from being successfully retransmitted in a later session, it does not prevent a fake server between the real server and client from passing through the server's random value and successfully authenticating. チャンネルバインディングを伴わないSCRAMではサーバは、送信されるパスワードハッシュの中でユーザに応じたパスワードと混合してクライアントに送る乱数を選びます。 これはパスワードハッシュが後のセッションで再送信されて認証に成功してしまうことを防止しますが、真のサーバとクライアントの間の偽サーバがサーバのランダム値を中継して認証に成功してしまうことを防止しません。

<acronym>SCRAM</acronym> with channel binding prevents such man-in-the-middle attacks by mixing the signature of the server's certificate into the transmitted password hash. While a fake server can retransmit the real server's certificate, it doesn't have access to the private key matching that certificate, and therefore cannot prove it is the owner, causing SSL connection failure. チャンネルバインディングを伴うSCRAMはこのような中間者攻撃をサーバ証明書のシグネチャを送信されるパスワードハッシュと混合することで防止します。 偽サーバは真のサーバの証明書を再送信できますが、その証明書に一致する秘密鍵にアクセスできず、それゆえ所有者であることを証明できず、結果としてSSL接続は失敗します。

<title>Example</title>
  1. The server sends an AuthenticationSASL message. It includes a list of SASL authentication mechanisms that the server can accept. This will be <literal>SCRAM-SHA-256-PLUS</literal> and <literal>SCRAM-SHA-256</literal> if the server is built with SSL support, or else just the latter. サーバはAuthenticationSASLメッセージを送信します。 それにはサーバが受け付けることができるSASL認証機構のリストが含まれます。 サーバがSSLサポート有でビルドされていれば、これはSCRAM-SHA-256-PLUSSCRAM-SHA-256になり、そうでなければ後者のみとなります。

  2. The client responds by sending a SASLInitialResponse message, which indicates the chosen mechanism, <literal>SCRAM-SHA-256</literal> or <literal>SCRAM-SHA-256-PLUS</literal>. (A client is free to choose either mechanism, but for better security it should choose the channel-binding variant if it can support it.) In the Initial Client response field, the message contains the SCRAM <structname>client-first-message</structname>. The <structname>client-first-message</structname> also contains the channel binding type chosen by the client. クライアントはSASLInitialResponseメッセージを送信することで応答します。 これは選択した機構、すなわちSCRAM-SHA-256またはSCRAM-SHA-256-PLUSを示します。 (クライアントは何れかの機構を自由に選びますが、より良いセキュリティのためサポートされているならチャンネルバインディングを伴うものを選ぶべきです。) Initial Clientの応答フィールドでは、メッセージにSCRAMのclient-first-messageが含まれます。 client-first-messageにはクライアントが選んだチャンネルバインディングのタイプも含まれます。

  3. Server sends an AuthenticationSASLContinue message, with a SCRAM <structname>server-first-message</structname> as the content. サーバがAuthenticationSASLContinueメッセージを送信します。 その内容はSCRAMのserver-first-messageです。

  4. Client sends a SASLResponse message, with SCRAM <structname>client-final-message</structname> as the content. クライアントがSASLResponseメッセージを送信します。 その内容はSCRAMのclient-final-messageです。

  5. Server sends an AuthenticationSASLFinal message, with the SCRAM <structname>server-final-message</structname>, followed immediately by an AuthenticationOk message. サーバがSCRAMのserver-final-messageを含むAuthenticationSASLFinalメッセージを送信し、その直後にAuthenticationOkメッセージを送信します。