更新时间:2025-11-28 GMT+08:00

申请证书 - CreateCertificate

功能介绍

申请证书。

调试

您可以在API Explorer中调试该接口,支持自动认证鉴权。API Explorer可以自动生成SDK代码示例,并提供SDK代码示例调试功能。

授权信息

账号具备所有API的调用权限,如果使用账号下的IAM用户调用当前API,该IAM用户需具备调用API所需的权限。

  • 如果使用角色与策略授权,具体权限要求请参见权限和授权项
  • 如果使用身份策略授权,需具备如下身份策略权限。

    授权项

    访问级别

    资源类型(*为必须)

    条件键

    别名

    依赖的授权项

    pca:ca:issueCert

    Write

    ca *

    -

    -

    -

    -

    • g:EnterpriseProjectId

    • g:RequestTag/<tag-key>

    • g:TagKeys

    • pca:CommonName

URI

POST /v1/private-certificates

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户token,参见:IAM token获取方法

表2 请求Body参数

参数

是否必选

参数类型

描述

issuer_id

String

父CA证书ID。

最小长度:36

最大长度:36

key_algorithm

String

密钥算法。若用户不输入该字段,默认使用签发CA的密钥算法。可选值如下:

  • RSA2048 : RSA算法,密钥长度2048位;

  • RSA4096 : RSA算法,密钥长度4096位;

  • EC256 : 椭圆曲线算法(Elliptic Curve Digital Signature Algorithm (ECDSA)),密钥长度256位;

  • EC384 : 椭圆曲线算法(Elliptic Curve Digital Signature Algorithm (ECDSA)),密钥长度384位;

  • SM2 : 国家密码管理局颁发的椭圆曲线算法(签名哈希算法SM3),密钥长度256位。(中国站)

signature_algorithm

String

签名哈希算法。若用户不输入该字段,默认使用签发CA的签名哈希算法。可选值如下:

  • SHA256

  • SHA384

  • SHA512

  • SM3(中国站)

distinguished_name

CertDistinguishedName object

证书名称配置,详情请参见CertDistinguishedName字段数据结构说明。

validity

Validity object

证书有效期,详情请参见Validity字段数据结构说明。

key_usages

Array of strings

密钥用法,具体标准参见RFC 5280中:4.2.1.3节

  • digitalSignature : 数字签名;

  • nonRepudiation : 不可抵赖;

  • keyEncipherment : 密钥用于加密密钥数据;

  • dataEncipherment : 用于加密数据;

  • keyAgreement : 密钥协商;

  • keyCertSign : 签发证书;

  • cRLSign : 签发吊销列表;

  • encipherOnly : 仅用于加密;

  • decipherOnly : 仅用于解密。

subject_alternative_names

Array of SubjectAlternativeName objects

主体备用名称,详情请参见SubjectAlternativeName字段数据结构说明。

  • array大小:[0,20]。

数组长度:0 - 20

extended_key_usage

ExtendedKeyUsage object

增强型密钥用法,详情请参见ExtendedKeyUsage字段数据结构说明。

customized_extension

CustomizedExtension object

自定义扩展信息,详情请参见CustomizedExtension字段数据结构说明。

enterprise_project_id

String

企业多项目ID。用户未开通企业多项目时,不需要输入该字段。 用户开通企业多项目时,查询资源可以输入该字段。 若用户不输入该字段,默认查询租户所有有权限的企业多项目下的资源。 此时“enterprise_project_id”取值为“all”。 若用户输入该字段,取值满足以下任一条件.

取值为“all”

取值为“0”

满足正则匹配:“^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$”

表3 CertDistinguishedName

参数

是否必选

参数类型

描述

common_name

String

证书通用名称(CN),名称只能由"-"、"_"、" "、"."、","、"*"、字母、数字、汉字组成,长度不能超过64位字符。

最小长度:1

最大长度:64

country

String

国家编码,只能由英文组成,长度为2位字符。若不传入,则默认继承父CA对应的值。

最小长度:2

最大长度:2

state

String

省市名称,名称只能由"-"、"_"、" "、"."、","、字母、数字、汉字组成,长度不能超过128位字符。若不传入,则默认继承父CA对应的值。

最小长度:1

最大长度:128

locality

String

地区名称,名称只能由"-"、"_"、" "、"."、","、字母、数字、汉字组成,长度不能超过128位字符。若不传入,则默认继承父CA对应的值。

最小长度:1

最大长度:128

organization

String

组织名称,名称只能由"-"、"_"、" "、"."、","、字母、数字、汉字组成,长度不能超过64位字符。若不传入,则默认继承父CA对应的值。

最小长度:1

最大长度:64

organizational_unit

String

组织单元名称,名称只能由"-"、"_"、" "、"."、","、字母、数字、汉字组成,长度不能超过64位字符。若不传入,则默认继承父CA对应的值。

最小长度:1

最大长度:64

表4 Validity

参数

是否必选

参数类型

描述

type

String

有效期类型,为必填值:

  • YEAR : 年(12个月)

  • MONTH : 月(统一按31天)

  • DAY : 日

  • HOUR : 小时

value

Integer

证书有效期值,与type对应的类型值,换算成年需满足以下规则:

  • 根CA,有效期小于等于30年;

  • 从属CA与私有证书,有效期小于等于20年。

start_from

Integer

起始时间,为可选值:

  • 格式为时间戳(毫秒级),如1645146939688代表2022-02-18 09:15:39;

  • 不早于当前时间5分钟,即start_from > (current_time - 5min)。

表5 SubjectAlternativeName

参数

是否必选

参数类型

描述

type

String

备用名称类型,当前仅支持DNS、IP、EMAIL、URI、UPN。

  • DNS

  • IP

  • EMAIL

  • URI

  • UPN

value

String

对应备用名称类型的值。

  • DNS类型,长度为不超过253;

  • IP类型,长度不超过39,支持IPV4、IPV6;

  • EMAIL类型,长度不超过256;

  • URI类型,长度不超过253;

  • UPN类型,长度不超过256。

表6 ExtendedKeyUsage

参数

是否必选

参数类型

描述

server_auth

Boolean

服务器身份验证,OID为:1.3.6.1.5.5.7.3.1。

  • true

  • false

说明:

服务器证书请启用本增强型密钥用法,默认为false。

缺省值:false

client_auth

Boolean

客户端身份验证,OID为:1.3.6.1.5.5.7.3.2。

  • true

  • false

说明:

客户端证书请启用本增强型密钥用法,默认为false。

缺省值:false

code_signing

Boolean

代码签名,OID为:1.3.6.1.5.5.7.3.3。

  • true

  • false

说明:

签署可下载的可执行代码客户端认证,默认为false。

缺省值:false

email_protection

Boolean

安全电子邮件,OID为:1.3.6.1.5.5.7.3.4。

  • true

  • false

说明:

电子邮件保护,默认为false。

缺省值:false

time_stamping

Boolean

时间戳,OID为:1.3.6.1.5.5.7.3.8。

  • true

  • false

说明:

将一个对象的哈希绑定到一个时间,默认为false。

缺省值:false

smart_card_logon

Boolean

智能卡登录,OID为:1.3.6.1.4.1.311.20.2.2。

  • true

  • false

说明:

Active Directory登录,默认为false。

缺省值:false

表7 CustomizedExtension

参数

是否必选

参数类型

描述

object_identifier

String

对象标识符。

说明:

本参数需要满足ASN1规范的点分十进制符号格式的字符串,如1.3.6.1.4.1.2011.4.99。

最小长度:1

最大长度:64

value

String

自定义属性内容。

最小长度:1

最大长度:64

响应参数

状态码:200

表8 响应Body参数

参数

参数类型

描述

certificate_id

String

当前签发的证书ID。

最小长度:36

最大长度:36

状态码:400

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误请求返回的错误码。

最小长度:3

最大长度:36

error_msg

String

错误请求返回的错误信息。

最小长度:0

最大长度:1024

状态码:401

表10 响应Body参数

参数

参数类型

描述

error_code

String

错误请求返回的错误码。

最小长度:3

最大长度:36

error_msg

String

错误请求返回的错误信息。

最小长度:0

最大长度:1024

状态码:403

表11 响应Body参数

参数

参数类型

描述

error_code

String

错误请求返回的错误码。

最小长度:3

最大长度:36

error_msg

String

错误请求返回的错误信息。

最小长度:0

最大长度:1024

状态码:404

表12 响应Body参数

参数

参数类型

描述

error_code

String

错误请求返回的错误码。

最小长度:3

最大长度:36

error_msg

String

错误请求返回的错误信息。

最小长度:0

最大长度:1024

状态码:500

表13 响应Body参数

参数

参数类型

描述

error_code

String

错误请求返回的错误码。

最小长度:3

最大长度:36

error_msg

String

错误请求返回的错误信息。

最小长度:0

最大长度:1024

请求示例

申请证书,请求头中的X-Auth-Token字段需要填写token,且该token需要具有本API的访问权限。

POST https://ccm.cn-north-4.myhuaweicloud.com/v1/private-certificates

{
  "key_algorithm" : "RSA2048",
  "distinguished_name" : {
    "country" : "CN",
    "state" : "Sichuan",
    "locality" : "Chengdu",
    "organization" : "Huawei",
    "organizational_unit" : "CloudBU",
    "common_name" : "TestCert"
  },
  "subject_alternative_names" : [ {
    "type" : "IP",
    "value" : "156.127.116.38"
  } ],
  "signature_algorithm" : "SHA256",
  "validity" : {
    "type" : "YEAR",
    "value" : 3
  },
  "issuer_id" : "2cb2878b-6cd1-460d-bd25-afe655159bdc",
  "key_usages" : [ "digitalSignature", "nonRepudiation" ]
}

响应示例

状态码:200

请求已成功

{
  "certificate_id" : "ae9a326a-b61e-4446-854d-cda30ffe31f5"
}

状态码:400

请求参数有误。

{
  "error_code" : "PCA.XXX",
  "error_msg" : "XXX"
}

状态码:401

被请求的页面需要鉴权。

{
  "error_code" : "PCA.XXX",
  "error_msg" : "XXX"
}

状态码:403

认证失败。

{
  "error_code" : "PCA.XXX",
  "error_msg" : "XXX"
}

状态码:404

资源不存在或资源未找到。

{
  "error_code" : "PCA.XXX",
  "error_msg" : "XXX"
}

状态码:500

服务内部错误。

{
  "error_code" : "PCA.XXX",
  "error_msg" : "XXX"
}

SDK代码示例

SDK代码示例如下。

Java

申请证书,请求头中的X-Auth-Token字段需要填写token,且该token需要具有本API的访问权限。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.ccm.v1.region.CcmRegion;
import com.huaweicloud.sdk.ccm.v1.*;
import com.huaweicloud.sdk.ccm.v1.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateCertificateSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new GlobalCredentials()
                .withAk(ak)
                .withSk(sk);

        CcmClient client = CcmClient.newBuilder()
                .withCredential(auth)
                .withRegion(CcmRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateCertificateRequest request = new CreateCertificateRequest();
        CreateCertificateRequestBody body = new CreateCertificateRequestBody();
        List<SubjectAlternativeName> listbodySubjectAlternativeNames = new ArrayList<>();
        listbodySubjectAlternativeNames.add(
            new SubjectAlternativeName()
                .withType("IP")
                .withValue("156.127.116.38")
        );
        List<String> listbodyKeyUsages = new ArrayList<>();
        listbodyKeyUsages.add("digitalSignature");
        listbodyKeyUsages.add("nonRepudiation");
        Validity validitybody = new Validity();
        validitybody.withType("YEAR")
            .withValue(3);
        CertDistinguishedName distinguishedNamebody = new CertDistinguishedName();
        distinguishedNamebody.withCommonName("TestCert")
            .withCountry("CN")
            .withState("Sichuan")
            .withLocality("Chengdu")
            .withOrganization("Huawei")
            .withOrganizationalUnit("CloudBU");
        body.withSubjectAlternativeNames(listbodySubjectAlternativeNames);
        body.withKeyUsages(listbodyKeyUsages);
        body.withValidity(validitybody);
        body.withDistinguishedName(distinguishedNamebody);
        body.withSignatureAlgorithm("SHA256");
        body.withKeyAlgorithm("RSA2048");
        body.withIssuerId("2cb2878b-6cd1-460d-bd25-afe655159bdc");
        request.withBody(body);
        try {
            CreateCertificateResponse response = client.createCertificate(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

申请证书,请求头中的X-Auth-Token字段需要填写token,且该token需要具有本API的访问权限。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkccm.v1.region.ccm_region import CcmRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkccm.v1 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = GlobalCredentials(ak, sk)

    client = CcmClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CcmRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateCertificateRequest()
        listSubjectAlternativeNamesbody = [
            SubjectAlternativeName(
                type="IP",
                value="156.127.116.38"
            )
        ]
        listKeyUsagesbody = [
            "digitalSignature",
            "nonRepudiation"
        ]
        validitybody = Validity(
            type="YEAR",
            value=3
        )
        distinguishedNamebody = CertDistinguishedName(
            common_name="TestCert",
            country="CN",
            state="Sichuan",
            locality="Chengdu",
            organization="Huawei",
            organizational_unit="CloudBU"
        )
        request.body = CreateCertificateRequestBody(
            subject_alternative_names=listSubjectAlternativeNamesbody,
            key_usages=listKeyUsagesbody,
            validity=validitybody,
            distinguished_name=distinguishedNamebody,
            signature_algorithm="SHA256",
            key_algorithm="RSA2048",
            issuer_id="2cb2878b-6cd1-460d-bd25-afe655159bdc"
        )
        response = client.create_certificate(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

申请证书,请求头中的X-Auth-Token字段需要填写token,且该token需要具有本API的访问权限。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
    ccm "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ccm/v1"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ccm/v1/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ccm/v1/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := global.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := ccm.NewCcmClient(
        ccm.CcmClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateCertificateRequest{}
	var listSubjectAlternativeNamesbody = []model.SubjectAlternativeName{
        {
            Type: "IP",
            Value: "156.127.116.38",
        },
    }
	var listKeyUsagesbody = []string{
        "digitalSignature",
	    "nonRepudiation",
    }
	validitybody := &model.Validity{
		Type: "YEAR",
		Value: int32(3),
	}
	countryDistinguishedName:= "CN"
	stateDistinguishedName:= "Sichuan"
	localityDistinguishedName:= "Chengdu"
	organizationDistinguishedName:= "Huawei"
	organizationalUnitDistinguishedName:= "CloudBU"
	distinguishedNamebody := &model.CertDistinguishedName{
		CommonName: "TestCert",
		Country: &countryDistinguishedName,
		State: &stateDistinguishedName,
		Locality: &localityDistinguishedName,
		Organization: &organizationDistinguishedName,
		OrganizationalUnit: &organizationalUnitDistinguishedName,
	}
	signatureAlgorithmCreateCertificateRequestBody:= "SHA256"
	keyAlgorithmCreateCertificateRequestBody:= "RSA2048"
	request.Body = &model.CreateCertificateRequestBody{
		SubjectAlternativeNames: &listSubjectAlternativeNamesbody,
		KeyUsages: &listKeyUsagesbody,
		Validity: validitybody,
		DistinguishedName: distinguishedNamebody,
		SignatureAlgorithm: &signatureAlgorithmCreateCertificateRequestBody,
		KeyAlgorithm: &keyAlgorithmCreateCertificateRequestBody,
		IssuerId: "2cb2878b-6cd1-460d-bd25-afe655159bdc",
	}
	response, err := client.CreateCertificate(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

请求已成功

400

请求参数有误。

401

被请求的页面需要鉴权。

403

认证失败。

404

资源不存在或资源未找到。

500

服务内部错误。

错误码

请参见错误码