使用证书模板申请证书

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

借助证书模板,您可以对证书颁发实施精细的政策控制。例如,您可以使用证书模板来标准化组织中各个 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 命令

如果检测到 CA 池的颁发政策与证书模板之间存在政策冲突,证书请求可能会失败。 发生这种情况时,您必须先解决政策冲突,然后才能再次提交证书申请。

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

控制台

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

查看使用模板颁发的证书

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

控制台

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

后续步骤