使用证书模板请求证书

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

借助证书模板,您可以在证书颁发过程中实现精细的政策控制。例如,您可以使用证书模板对跨组织中各 CA 池的服务器 TLS 证书颁发进行标准化。或者,您可以使用证书模板在更精细的级别应用政策,例如应用于特定用户。当您需要限制不同个人可以颁发的证书类型时,这种方法非常有用。您还可以重复使用模板来处理常见的发布场景。

准备工作

  1. 为 CA Service 准备好环境

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

    如需详细了解 CA Service 的预定义 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. Template Details 页面上,点击 Certificates。 系统会显示使用所选证书模板颁发的证书列表。

后续步骤