[ACM.2] ACM が管理する RSA 証明書は、少なくとも 2,048 ビットのキーの長さを使用する必要があります を CloudFormation Guard でチェックする

[ACM.2] ACM が管理する RSA 証明書は、少なくとも 2,048 ビットのキーの長さを使用する必要があります を CloudFormation Guard でチェックする

Clock Icon2025.02.22

こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。

CloudFormation テンプレートをデプロイする前に、 Security Hub の検知項目に引っかかっていないか、チェックしたいなぁと思ったことはありますでしょうか。

そういった時に、役に立つのが CloudFormation Guard や cfn-nag、Trivy、Snyk IaC などです。

ただし、どれも AWS Foundational Security Best Practices v1.0.0 (FSBP) 標準には完全に準拠しきれていない状況です。AWS FSBP ベースでシステム構築している方も多いのではないでしょうか。

そこで今回は、コントロールの1つである ACM が管理する RSA 証明書は、少なくとも 2,048 ビットのキーの長さを使用する必要があります を CloudFormation Guard で表現してみます。

コントロールについて

このコントロールは ACM によって管理される RSA 証明書のキーの長さが 2,048 ビット以上であるかどうかをチェックします。

ACM の場合、RSA 暗号化のビット数は 1024, 2048, 3072, 4096 をサポートしています。つまり、 1024 ビットの RSA 暗号化を利用しているかどうかをチェックします。

1024 ビットの RSA 暗号化は、暗号強度の安全性が担保できない観点から非推奨となっています。

もし、利用している場合は、コントロールに従い 2048 ビット数以上の暗号強度がある暗号化方式の証明書を利用しましょう。

https://docs.aws.amazon.com/ja_jp/securityhub/latest/userguide/acm-controls.html#acm-2

作成したコード

作成したコードは以下になります。

acm-certificate-rsa-check.guard
################################################
#                     ACM.2                     
################################################
# [ACM.2] RSA certificates managed by ACM should use a key length of at least 2,048 bits
# Control URL: https://docs.aws.amazon.com/securityhub/latest/userguide/acm-controls.html#acm-2
#
# Category: Identify > Inventory > Inventory services
# Severity: High
# Resource type: AWS::ACM::Certificate
# AWS Config rule: acm-certificate-rsa-check
# Config Rule URL: https://docs.aws.amazon.com/config/latest/developerguide/acm-certificate-rsa-check.html

let deprecated_key_algorithm = ['RSA_1024']

let acm_certificate_rsa_check_enabled = Resources.*[ Type == 'AWS::ACM::Certificate'
  Metadata.guard.SuppressedRules !exists or
  Metadata.guard.SuppressedRules.* != "ACM_CERTIFICATE_RSA_CHECK"
]

# AWS::ACM::Certificate
rule acm_certificate_rsa_check when %acm_certificate_rsa_check_enabled !empty {
  %acm_certificate_rsa_check_enabled.Properties {
    # KeyAlgorithm が存在した場合、 deprecated_key_algorithm 以外 かつ文字列は true とする
    when KeyAlgorithm exists {
      # 文字列であること
      KeyAlgorithm is_string
      <<
        RuleID: ACM.2
        Category: Identify > Inventory > Inventory services
        Severity: High
        Resource type: AWS::ACM::Certificate
        Description: RSA certificates managed by ACM should use a key length of at least 2,048 bits
      >>

      # 非推奨の暗号化アルゴリズムでは無いこと
      KeyAlgorithm !in %deprecated_key_algorithm
      <<
        RuleID: ACM.2
        Category: Identify > Inventory > Inventory services
        Severity: High
        Resource type: AWS::ACM::Certificate
        Description: RSA certificates managed by ACM should use a key length of at least 2,048 bits
      >>
    }
  }
}

KeyAlgorithm

今回のコントロールは AWS::ACM::CertificateKeyAlgorithm で設定されている値をチェックします。デフォルト値は RSA_2048(準拠したアルゴリズム)が設定されます。

Specifies the algorithm of the public and private key pair that your certificate uses to encrypt data. RSA is the default key algorithm for ACM certificates. Elliptic Curve Digital Signature Algorithm (ECDSA) keys are smaller, offering security comparable to RSA keys but with greater computing efficiency. However, ECDSA is not supported by all network clients. Some AWS services may require RSA keys, or only support ECDSA keys of a particular size, while others allow the use of either RSA and ECDSA keys to ensure that compatibility is not broken. Check the requirements for the AWS service where you plan to deploy your certificate. For more information about selecting an algorithm, see Key algorithms.
Default: RSA_2048

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-certificatemanager-certificate.html#cfn-certificatemanager-certificate-keyalgorithm

チェックロジック部分

チェックロジックはブラックリスト方式を採用しました。

ホワイトリスト方式の場合、次のように記述できます。ブラックリスト方式に比べ、コードの記述量は短くなります。

acm-certificate-rsa-check.guard
################################################
#                     ACM.2                     
################################################
# [ACM.2] RSA certificates managed by ACM should use a key length of at least 2,048 bits
# Control URL: https://docs.aws.amazon.com/securityhub/latest/userguide/acm-controls.html#acm-2
#
# Category: Identify > Inventory > Inventory services
# Severity: High
# Resource type: AWS::ACM::Certificate
# AWS Config rule: acm-certificate-rsa-check
# Config Rule URL: https://docs.aws.amazon.com/config/latest/developerguide/acm-certificate-rsa-check.html

- let deprecated_key_algorithm = ['RSA_1024']
+ let key_algorithm = ['RSA_2048', 'RSA_3072', 'RSA_4096', 'EC_prime256v1', 'EC_secp384r1', 'EC_secp521r1']

let acm_certificate_rsa_check_enabled = Resources.*[ Type == 'AWS::ACM::Certificate'
  Metadata.guard.SuppressedRules !exists or
  Metadata.guard.SuppressedRules.* != "ACM_CERTIFICATE_RSA_CHECK"
]

# AWS::ACM::Certificate
rule acm_certificate_rsa_check when %acm_certificate_rsa_check_enabled !empty {
  %acm_certificate_rsa_check_enabled.Properties {
    # KeyAlgorithm が存在した場合、 key_algorithm 以外 かつ文字列は true とする
    when KeyAlgorithm exists {
-      # 文字列であること
-      KeyAlgorithm is_string
-      <<
-        RuleID: ACM.2
-        Category: Identify > Inventory > Inventory services
-        Severity: High
-        Resource type: AWS::ACM::Certificate
-        Description: RSA certificates managed by ACM should use a key length of at least 2,048 bits
-      >>
-      
-      # 非推奨の暗号化アルゴリズムでは無いこと
-      KeyAlgorithm !in %deprecated_key_algorithm
+      # 推奨の暗号化アルゴリズムであること
+      KeyAlgorithm in %key_algorithm
      <<
        RuleID: ACM.2
        Category: Identify > Inventory > Inventory services
        Severity: High
        Resource type: AWS::ACM::Certificate
        Description: RSA certificates managed by ACM should use a key length of at least 2,048 bits
      >>
    }
  }
}

ただし、この記述の場合、RSA_8192 など、新しい値が指定可能となった時に、メンテナンススピードが求められます。

スキャンをパイプラインに挟むなど、運用によってはデプロイが止まってしまうケースが考えられるため、なるべくブラックリスト方式でデプロイを止めないような方針に傾けました。

テストしてみる

今回は次のテストコードを用意しました。

acm-certificate-rsa-check.yaml
# acm-certificate-rsa-check.yaml
---
# テストケース1: 空のテンプレート
- name: Empty template
  input: {}
  expectations:
    rules:
      acm_certificate_rsa_check: SKIP

# テストケース2: 空のリソースセクション
- name: Empty resources
  input:
    Resources: {}
  expectations:
    rules:
      acm_certificate_rsa_check: SKIP

# テストケース3: RSA-2048を使用(準拠)
- name: RSA-2048 compliant
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: RSA_2048
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: PASS

# テストケース4: RSA-1024を使用(非準拠)
- name: RSA-1024 non-compliant
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: RSA_1024
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: FAIL

# テストケース5: KeyAlgorithmプロパティ未設定
- name: Missing KeyAlgorithm property
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Properties:
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: SKIP

# テストケース6: ルール抑制設定
- name: Rule suppressed
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Metadata:
          guard:
            SuppressedRules:
              - "ACM_CERTIFICATE_RSA_CHECK"
        Properties:
          KeyAlgorithm: RSA_1024
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: SKIP

# テストケース7: 複数の証明書(混在)
- name: Multiple certificates with mixed compliance
  input:
    Resources:
      CompliantCert:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: RSA_2048
          DomainName: example.com
      NonCompliantCert:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: RSA_1024
          DomainName: example.org
  expectations:
    rules:
      acm_certificate_rsa_check: FAIL

# テストケース8: EC2アルゴリズムを使用
- name: EC2 algorithm
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: EC_prime256v1
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: PASS

# テストケース9: RSA-4096を使用
- name: RSA-4096
  input:
    Resources:
      MyCertificate:
        Type: AWS::ACM::Certificate
        Properties:
          KeyAlgorithm: RSA_4096
          DomainName: example.com
  expectations:
    rules:
      acm_certificate_rsa_check: PASS

テストケースが通っていますね。

takakuni@Mac fsbp-guardrails % cfn-guard test -r rules/aws_certificate_manager/acm-certificate-rsa-check.guard -t rules/aws_certificate_manager/tests/acm-certificate-rsa-check.yaml
Test Case #1
Name: Empty template
  PASS Rules:
    acm_certificate_rsa_check: Expected = SKIP

Test Case #2
Name: Empty resources
  PASS Rules:
    acm_certificate_rsa_check: Expected = SKIP

Test Case #3
Name: RSA-2048 compliant
  PASS Rules:
    acm_certificate_rsa_check: Expected = PASS

Test Case #4
Name: RSA-1024 non-compliant
  PASS Rules:
    acm_certificate_rsa_check: Expected = FAIL

Test Case #5
Name: Missing KeyAlgorithm property
  PASS Rules:
    acm_certificate_rsa_check: Expected = SKIP

Test Case #6
Name: Rule suppressed
  PASS Rules:
    acm_certificate_rsa_check: Expected = SKIP

Test Case #7
Name: Multiple certificates with mixed compliance
  PASS Rules:
    acm_certificate_rsa_check: Expected = FAIL

Test Case #8
Name: EC2 algorithm
  PASS Rules:
    acm_certificate_rsa_check: Expected = PASS

Test Case #9
Name: RSA-4096
  PASS Rules:
    acm_certificate_rsa_check: Expected = PASS

まとめ

以上、「[ACM.2] ACM が管理する RSA 証明書は、少なくとも 2,048 ビットのキーの長さを使用する必要があります を CloudFormation Guard でチェックする」でした。

このブログがどなたかの参考になれば幸いです。

クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.