使用证书模板请求证书

本页介绍了如何使用证书模板请求证书。

借助证书模板,您可以对证书颁发实施精细的政策控制。例如,您可以使用证书模板来标准化组织中 CA 池中的服务器 TLS 证书的颁发。或者,您也可以使用证书模板在更精细的级别(例如对特定用户)应用政策。如果您需要限制不同用户可以签发的证书类型,这会非常有用。您还可以针对常见的签发场景重复使用模板。

准备工作

  1. 为 CA 服务准备好环境

  2. 如需获得使用证书模板签发证书所需的权限,请让您的管理员为您授予证书模板的 CA Service Certificate Template User (roles/privateca.templateUser) IAM 角色。

    如需详细了解 CA 服务的预定义 IAM 角色,请参阅使用 IAM 进行访问权限控制

    如需了解如何向正文授予 IAM 角色,请参阅授予单个角色

测试证书颁发

在使用证书模板请求已签名证书之前,我们建议您验证证书模板是否能够成功生成证书。如果 CA 池的颁发政策与证书模板的政策之间存在冲突,证书颁发将会失败。通过测试签发,您可以主动发现并解决这些冲突。请注意,测试证书未采用 PEM 编码,未签名,且生成不收费。

如需使用证书模板测试证书颁发,请按以下步骤操作:

控制台

  1. 前往 Google Cloud 控制台中的 Certificate Authority Service 页面。

    前往 Certificate Authority Service

  2. 点击模板管理器标签页。

  3. 点击要测试的证书模板。 系统随即会显示模板详情页面。

  4. 如需创建测试请求,请点击创建证书,然后点击测试证书签发。系统随即会显示证书请求表单。

  5. 指定创建证书请求所需的以下详细信息:

    • 区域:证书的位置。此值必须与 CA 池的位置相同。
    • CA 池:负责颁发证书的 CA 池。
    • 证书模板:您要用于颁发证书的模板。
    • 域名:您要使用 SSL 或 TLS 证书保护的网站的域名。
  6. 点击生成证书

  7. 证书创建完毕后,点击查看。测试或样本证书会显示在同一页面上的单独面板中。

  8. 如果证书签发因冲突而失败,请解决冲突,然后重新提交测试证书请求。

使用证书模板颁发证书

如需使用证书模板颁发已签名证书,请执行以下操作:

控制台

  1. 前往 Google Cloud 控制台中的 Certificate Authority Service 页面。

    前往 Certificate Authority Service

  2. 点击模板管理器标签页。

  3. 证书模板页面中,点击要使用的证书模板。 系统随即会显示模板详情页面。

  4. 点击创建证书

  5. 选择区域。此区域必须与您打算使用的 CA 池的区域相同。

  6. 选择 CA 池。

  7. 如需使用证书签名请求 (CSR) 生成证书,请参阅使用 CSR 请求证书

  8. 如需使用自动生成的密钥生成证书,请参阅使用自动生成的密钥请求证书

生成证书

  1. 点击生成证书。如果证书成功生成,系统会显示一条消息。
  2. 如需查看生成的证书,请点击查看证书,然后点击查看

可选:下载已签名的证书

  1. 如需下载 PEM 编码的证书链,请点击下载证书链
  2. 如需下载关联的 PEM 编码私钥,请点击下载私钥

gcloud

如需使用证书模板签发证书,请按照以下格式将 --template 标志添加到 gcloud privateca certificates create 命令:

--template=projects/PROJECT_ID/locations/LOCATION/certificateTemplates/CERTIFICATE_TEMPLATE

CERTIFICATE_TEMPLATE 替换为您要用于颁发此证书的证书模板的名称。指定的模板必须与发证 CA 池位于同一位置。如需了解详情,请参阅用于生成测试 DNS 证书生成生产证书的示例。

Terraform

/**
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

resource "google_privateca_certificate_template" "template" {
  location    = "us-central1"
  name        = "my-certificate-template"
  description = "An updated sample certificate template"

  identity_constraints {
    allow_subject_alt_names_passthrough = true
    allow_subject_passthrough           = true

    cel_expression {
      description = "Always true"
      expression  = "true"
      location    = "any.file.anywhere"
      title       = "Sample expression"
    }
  }

  passthrough_extensions {
    additional_extensions {
      object_id_path = [1, 6]
    }

    known_extensions = ["EXTENDED_KEY_USAGE"]
  }

  predefined_values {
    additional_extensions {
      object_id {
        object_id_path = [1, 6]
      }

      value    = "c3RyaW5nCg=="
      critical = true
    }

    aia_ocsp_servers = ["string"]

    ca_options {
      is_ca                  = false
      max_issuer_path_length = 6
    }

    key_usage {
      base_key_usage {
        cert_sign          = false
        content_commitment = true
        crl_sign           = false
        data_encipherment  = true
        decipher_only      = true
        digital_signature  = true
        encipher_only      = true
        key_agreement      = true
        key_encipherment   = true
      }

      extended_key_usage {
        client_auth      = true
        code_signing     = true
        email_protection = true
        ocsp_signing     = true
        server_auth      = true
        time_stamping    = true
      }

      unknown_extended_key_usages {
        object_id_path = [1, 6]
      }
    }

    policy_ids {
      object_id_path = [1, 6]
    }
  }
}

resource "google_privateca_certificate_authority" "test_ca" {
  pool                     = "my-pool"
  certificate_authority_id = "my-certificate-authority-test-ca"
  location                 = "us-central1"
  deletion_protection      = false # set to true to prevent destruction of the resource
  config {
    subject_config {
      subject {
        organization = "HashiCorp"
        common_name  = "my-certificate-authority"
      }
      subject_alt_name {
        dns_names = ["hashicorp.com"]
      }
    }
    x509_config {
      ca_options {
        # is_ca *MUST* be true for certificate authorities
        is_ca = true
      }
      key_usage {
        base_key_usage {
          # cert_sign and crl_sign *MUST* be true for certificate authorities
          cert_sign = true
          crl_sign  = true
        }
        extended_key_usage {
          server_auth = false
        }
      }
    }
  }
  key_spec {
    algorithm = "RSA_PKCS1_4096_SHA256"
  }
}


resource "google_privateca_certificate" "default" {
  pool                  = "my-pool"
  location              = "us-central1"
  certificate_authority = google_privateca_certificate_authority.test_ca.certificate_authority_id
  lifetime              = "860s"
  name                  = "my-certificate-from-template"
  pem_csr               = tls_cert_request.example.cert_request_pem
  certificate_template  = google_privateca_certificate_template.template.id
}

resource "tls_private_key" "example" {
  algorithm = "RSA"
}

resource "tls_cert_request" "example" {
  private_key_pem = tls_private_key.example.private_key_pem

  subject {
    common_name  = "example.com"
    organization = "ACME Examples, Inc"
  }
}

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

如需与贵组织中的其他人共享证书申请表单链接,以便他们使用相同的参数申请证书,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中,前往 CA 池管理器标签页,然后点击分享请求表单链接
  2. 在随即显示的共享请求表单链接面板中,选择您在创建请求时选择的 CA 池和证书模板。系统会显示证书请求链接。
  3. 复制链接,然后根据需要进行分享。

解决政策冲突

如果 CA 池的发证政策与证书模板之间存在冲突,证书请求会失败并出现参数无效错误。例如,如果 CA 池的基准值和证书模板的预定义值中定义了相同的扩展(例如基本密钥用途)。或者,当一个政策具有排除特定扩展程序的扩展程序限制,而另一个政策在其基准值中为该扩展程序定义了值时。

如需查看和解决政策冲突,请按以下步骤操作:

控制台

  1. 点击随错误消息一起显示的发行政策问题排查工具链接。系统随即会显示一个问题排查页面,您可以在其中比较 CA 池颁发政策的基准值和扩展限制与证书模板政策的基准值和扩展限制。请注意,系统会突出显示存在政策冲突的内容。
  2. 访问 CA 池或证书模板,以更新冲突的值并解决冲突。
  3. 解决冲突后,请重新提交证书请求。

查看使用模板颁发的证书

如需查看使用证书模板颁发的证书,请执行以下操作:

控制台

  1. 在 Google Cloud 控制台中,前往模板管理器标签页。
  2. 点击您用于颁发证书的证书模板。
  3. 模板详细信息页面上,点击证书。 系统会显示使用所选证书模板颁发的证书列表。

后续步骤