HMAC ポリシー

<ph type="x-smartling-placeholder"></ph> 現在、Apigee Edge のドキュメントが表示されています。
Apigee X のドキュメント
詳細

ハッシュベースのメッセージ認証コード(HMAC)を計算して検証します。HMAC(鍵付きのメッセージ認証コード、鍵付きハッシュとも呼ばれる)は、SHA-1、SHA-224、SHA-256、SHA-384、SHA-512、MD-5 などの暗号ハッシュ関数を秘密鍵ともに「メッセージ」に適用して、そのメッセージに署名またはメッセージ認証コードを生成します。ここでの「メッセージ」という用語は、任意のバイト ストリームを指します。メッセージの送信者は HMAC を受信者に送信することもでき、その受信者は HMAC を使用してメッセージを認証できます。

HMAC の詳細については、HMAC: 鍵付きハッシュ用のメッセージ認証(rfc2104)をご覧ください。

サンプル

HMAC を生成する

<HMAC name='HMAC-1'>

  <Algorithm>SHA256</Algorithm>

  <SecretKey ref='private.secretkey'/>

  <IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables> <!-- optional -->

  <!--
    The "message" can include fixed and multiple variable parts,
    including newlines and static functions.
    Whitespace is significant.
   -->
  <Message>Fixed Part
    {a_variable}
    {timeFormatUTCMs(timeFormatString1,system.timestamp)}
    {nonce}
  </Message>

  <!-- default encoding is base64 -->
  <Output encoding='base16'>name_of_variable</Output>

</HMAC>

HMAC を検証する

<HMAC name='HMAC-1'>

  <Algorithm>SHA256</Algorithm>

  <SecretKey ref='private.secretkey'/>

  <IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables> <!-- optional -->

  <!--
    The "message" can include fixed and multiple variable parts,
    including newlines and static functions.
    Whitespace is significant.
   -->
  <Message>Fixed Part
    {a_variable}
    {timeFormatUTCMs(timeFormatString1,system.timestamp)}
    {nonce}
  </Message>

  <!--
    VerificationValue is optional.
    Include it to perform an HMAC check.
  -->
  <VerificationValue encoding='base16' ref='expected_hmac_value'/>

  <!-- default encoding is base64 -->
  <Output encoding='base16'>name_of_variable</Output>

</HMAC>

署名の計算とその署名の検証は、同じプロセスで行います。HMAC ポリシーは HMAC を計算し、計算された署名を期待値と照らして任意で検証します。任意の検証値の要素(存在する場合)によって、ポリシーが既知の値または指定された値に対して計算済みの値を確認します。


HMAC の要素リファレンス

このポリシー リファレンスでは、HMAC ポリシーの要素と属性について説明します。

最上位の要素に適用される属性

<HMAC name="HMAC" continueOnError="false" enabled="true" async="false">

次の属性は、すべてのポリシーの親要素に共通です。

属性 説明 デフォルト 要否
name ポリシーの内部名。名前に使用できる文字は A-Z0-9._\-$ % のみです。ただし、Apigee UI では、英数字以外の文字を自動的に削除するなど、追加の制限が適用されます。

必要に応じて、<displayname></displayname> 要素を使用して、Apigee UI プロキシ エディタでポリシーに別の自然言語名でラベルを付けます。

なし 必須
continueOnError ポリシーが失敗した場合にエラーを返すには、false に設定します。これはほとんどのポリシーで想定される動作です。

ポリシーが失敗した後もフローの実行を続行する場合は、true に設定します。

false 省略可
有効 ポリシーを適用するには、true に設定します。

ポリシーを「turn off」するには、false に設定します。ポリシーがフローに接続されている場合でも適用されません。

true 省略可
async この属性は非推奨となりました。 false 非推奨

<Algorithm>

<Algorithm>algorithm-name</Algorithm>

HMAC を計算するハッシュ アルゴリズムを指定します。

デフォルト なし
要否 必須
文字列
有効な値 SHA-1SHA-224SHA-256SHA-384SHA-512 および MD-5

ポリシー構成で受け入れられるアルゴリズム名には、大文字小文字の区別や、文字と数字の間にダッシュがあるかないかの区別は必要ありません。たとえば、SHA256SHA-256sha256 は同等です。

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

name 属性に加えて、Apigee UI プロキシ エディタでポリシーを別の自然言語名でラベル付けするために使用します。

デフォルト この要素を省略した場合、ポリシーの name 属性の値が使用されます。
要否 省略可
文字列

<Message>

<Message>message_template_here</Message>
or
<Message ref='variable_here'/>

署名するメッセージ ペイロードを指定します。この要素の入力では、メッセージ テンプレート(変数置換)がサポートされているため、タイムスタンプ、ノンス、ヘッダーのリストなど、追加の項目を実行時に含めることができます。次に例を示します。

<Message>Fixed Part
    {a_variable}
    {timeFormatUTCMs(timeFormatString1,system.timestamp)}
    {nonce}
</Message>

メッセージ テンプレートには、改行や静的関数など、固定部と変数部を含めることができます。空白文字は、有意です。

デフォルト なし
要否 必須
文字列
有効な値 テキスト値には任意の文字列を使用できます。ref 属性を指定すると、テキスト値よりも優先されます。このポリシーは、テキスト値または参照先の変数をメッセージ テンプレートとして評価します。

<Output>

<Output encoding='encoding_name'>variable_name</Output>

ポリシーが計算済みの HMAC 値を使用して設定する必要がある変数の名前を指定します。また、出力に使用するエンコードを指定します。

デフォルト

デフォルトの出力変数は hmac.POLICYNAME.output です。

encoding 属性のデフォルト値は base64 です。

要否 省略可。この要素が存在しない場合、ポリシーは base64 でエンコードされた値を使用してフロー変数 hmac.POLICYNAME.output を設定します。
文字列
有効な値

エンコードの場合、hexbase16base64base64url

値の大文字と小文字は区別されません。hexbase16 は類義語です。

Output 要素のテキスト値は、任意の有効なフロー変数名となります。

<SecretKey>

<SecretKey encoding='encoding_name' ref='private.secretkey'/>

HMAC の計算に使用する秘密鍵を指定します。この鍵は参照先の変数から取得され、特定のエンコードに従ってデコードされます。

デフォルト

参照先の変数にデフォルト値はありません。ref 属性が必要です。

encoding 属性がない場合、ポリシーはデフォルトで秘密鍵文字列を UTF-8 でデコードして鍵のバイト数を取得します。

要否 必須
文字列
有効な値

encoding の有効な値は、hexbase16base64utf8 です。デフォルト値は UTF-8 です。値の大文字と小文字は区別されず、ダッシュは無効な文字と判断されます。Base16 は、base-16bAse16 と同じです。Base16Hex は類義語です。

エンコード属性を使用すると、UTF-8 の印刷可能な文字の範囲外のバイト数を含む鍵を指定できます。たとえば、次のような構成をポリシー構成に含めるとします。

 <SecretKey encoding='hex' ref='private.encodedsecretkey'/>

また、private.encodedsecretkey が文字列 536563726574313233 を保持しているとします。

この場合、鍵のバイト数は [53 65 63 72 65 74 31 32 33] のようにデコードされます(各バイト数は 16 進数で表されます)。別の例として、encoding='base64'private.encodedsecretkey が文字列 U2VjcmV0MTIz を保持している場合、キーに対して同じバイト数のセットになります。エンコード属性がない場合、またはエンコード属性 UTF8 がある場合、文字列値 Secret123 は同じバイト数のセットになります。

<VerificationValue>

<VerificationValue encoding='encoding_name' ref='variable_name'/>
or
<VerificationValue encoding='encoding_name'>string_value</VerificationValue>

(省略可)検証値と、検証値のエンコードに使用されたエンコード アルゴリズムを指定します。ポリシーはこのアルゴリズムを使用して値をデコードします。

デフォルト デフォルトの検証値はありません。要素が存在しても encoding 属性が存在しない場合、ポリシーはデフォルトのエンコード base64 を使用します。
要否 省略可
文字列
有効な値

エンコード属性の有効な値は、hexbase16base64base64url です。値の大文字と小文字は区別されません。hexbase16 は類義語です。

VerificationValue のエンコードは、Output 要素に使用されるエンコードと同一である必要はありません。

<IgnoreUnresolvedVariables>

<IgnoreUnresolvedVariables>true|false</IgnoreUnresolvedVariables>

false に設定すると、ポリシーで指定された参照先の変数を解決できない場合にエラーを返します。true に設定すると、解決できない変数を空の文字列(null)として扱います。

IgnoreUnresolvedVariables ブール値は、メッセージ テンプレートで参照される変数にのみ影響します。SecretKeyVerificationValue は変数を参照できますが、両方とも解決可能である必要があるため、ignore 設定はそれらには適用されません。

デフォルト false
要否 省略可
ブール値
有効な値 true または false

フロー変数

ポリシーは実行時にこれらの変数を設定できます。

変数 説明
hmac.policy_name.message ポリシーは、有効なメッセージ(Message 要素で指定されたメッセージ テンプレートの評価結果)を使用して、この変数を設定します。 hmac.HMAC-Policy.message = "Hello, World"
hmac.policy_name.output Output 要素が変数名を指定していない場合に、HMAC 計算の結果を取得します。 hmac.HMAC-Policy.output = /yyRjydfP+fBHTwXFgc5AZhLAg2kwCri+e35girrGw4=
hmac.policy_name.outputencoding 出力エンコード名を取得します。 hmac.HMAC-Policy.outputencoding = base64

Error reference

This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Occurs when
steps.hmac.UnresolvedVariable 401

This error occurs if a variable specified in the HMAC policy is either:

  • Out of scope (not available in the specific flow where the policy is being executed)

    or

  • Can't be resolved (is not defined)
steps.hmac.HmacVerificationFailed 401 The HMAC verification failed; the verification value provided does not match the calculated value.
steps.hmac.HmacCalculationFailed 401 The policy was unable to calculate the HMAC.
steps.hmac.EmptySecretKey 401 The value of the secret key variable is empty.
steps.hmac.EmptyVerificationValue 401 The variable holding the verification value is empty.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name HTTP status Occurs when
steps.hmac.MissingConfigurationElement 401 This error occurs when a required element or attribute is missing.
steps.hmac.InvalidValueForElement 401 This error occurs if the value specified in the Algorithm element is not one of the following values: SHA-1, SHA-224, SHA-256, SHA-512, or MD-5.
steps.hmac.InvalidSecretInConfig 401 This error occurs if there is a text value explicitly provided for SecretKey.
steps.hmac.InvalidVariableName 401 This error occurs if the SecretKey variable does not contain the private prefix (private.).

Fault variables

These variables are set when a runtime error occurs. For more information, see What you need to know about policy errors.

Variables Where Example
fault.name="fault_name" fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. fault.name Matches "UnresolvedVariable"
hmac.policy_name.failed The policy sets this variable in the case of a failure. hmac.HMAC-Policy.failed = true

Example error response

For error handling, the best practice is to trap the errorcode part of the error response. Do not rely on the text in the faultstring, because it could change.

Example fault rule

<FaultRules>
    <FaultRule name="HMAC Policy Errors">
        <Step>
            <Name>AM-Unauthorized</Name>
            <Condition>(fault.name Matches "HmacVerificationFailed")</Condition>
        </Step>
        <Condition>hmac.HMAC-1.failed = true</Condition>
    </FaultRule>
</FaultRules>