
# 通过Easy-RSA自签发证书（服务端和客户端共用CA证书）
#### 场景描述
Easy-RSA是一个开源的证书管理工具，用于帮助用户生成和管理数字证书。
本示例介绍在Windows操作系统中，通过Easy-RSA自签发证书，服务端和客户端共用CA证书。本示例使用的软件版本为Easy-RSA 3.1.7，不同软件版本之间可能存在差异，具体请参考官方指导说明。
#### 操作步骤
1. 根据Windows操作系统下载Easy-RSA安装包至"D:\\"目录下。
   - Windows 32位操作系统，可以下载[EasyRSA-3.1.7-win32.zip](https://github.com/OpenVPN/easy-rsa/releases/download/v3.1.7/EasyRSA-3.1.7-win32.zip)。
   
   - Windows 64位操作系统，可以下载[EasyRSA-3.1.7-win64.zip](https://github.com/OpenVPN/easy-rsa/releases/download/v3.1.7/EasyRSA-3.1.7-win64.zip)。
   
   
   此处以安装EasyRSA-3.1.7-win64为示例。
   ![](https://support.huaweicloud.com/admin-vpn/figure/zh-cn_image_0000001919965422.png "点击放大")
   
2. 解压缩**"** EasyRSA-3.1.7-win64.zip**"**至指定目录，如"D:\\EasyRSA-3.1.7"。
3. 进入"D:\\EasyRSA-3.1.7"目录。
4. 在地址栏中输入**cmd**并按回车键，打开命令行窗口。
5. 执行以下命令，运行Easy-RSA。 .\EasyRSA-Start.bat
   系统显示如下类似信息：
   ```
   Welcome to the EasyRSA 3 Shell for Windows.
   Easy-RSA 3 is available under a GNU GPLv2 license.
   Invoke './easyrsa' to call the program. Without commands, help is displayed.
   EasyRSA Shell
   #
   ```
   
6. 执行以下命令，初始化PKI环境。 ./easyrsa init-pki
   系统显示如下类似信息：
   ```
   Notice
   ------
   'init-pki' complete; you may now create a CA or requests.
   Your newly created PKI dir is:
   * D:/EasyRSA-3.1.7/pki
   Using Easy-RSA configuration:
   * undefined
   EasyRSA Shell
   #
   ```
   执行命令后，在"D:\\EasyRSA-3.1.7"的目录下自动生成了"pki"的文件夹。
   
7. 配置变量参数。
   1. 将"D:\\EasyRSA-3.1.7"目录下的"vars.example"文件复制到"D:\\EasyRSA-3.1.7\\pki"目录下。
   
   2. 将"D:\\EasyRSA-3.1.7\\pki"目录下的"vars.example"重命名为"vars"。
      ![](https://support.huaweicloud.com/admin-vpn/public_sys-resources/note_3.0-zh-cn.png)
      默认按"vars.example"中描述的参数值进行配置。如需自定义参数值，按需设置"vars"文件的参数值。
      
    
8. 执行以下命令，生成CA证书。 ./easyrsa build-ca nopass
   系统显示如下类似信息：
   ```
   Using Easy-RSA 'vars' configuration:
   * D:/EasyRSA-3.1.7/pki/vars
   Using SSL:
   * openssl OpenSSL 3.1.2 1 Aug 2023 (Library: OpenSSL 3.1.2 1 Aug 2023)
   .....+..+.............+......+........+...+...+....+++++++++++++++++++++++++++++++++++++++*.+.+.....+..........+............+...+++++++++++++++++++++++++++++++++++++++*............+.....+......+...+....+..+..........+.....+....+...............+..+.........+.............+......+..+...+....+..+.+.........+.....+.........+....+............+...+...+.....+........................+...+.+.....+....+...+.........+...+...+...+.....+......+........................++++++
   .+++++++++++++++++++++++++++++++++++++++*.........+..........+++++++++++++++++++++++++++++++++++++++*.+......++++++
   -----
   You are about to be asked to enter information that will be incorporated
   into your certificate request.
   What you are about to enter is what is called a Distinguished Name or a DN.
   There are quite a few fields but you can leave some blank
   For some fields there will be a default value,
   If you enter '.', the field will be left blank.
   -----
   Common Name (eg: your user, host, or server name) [Easy-RSA CA]:p2cvpn.com    # 设置CA证书名称
   Notice
   ------
   CA creation complete. Your new CA certificate is at:
   * D:/EasyRSA-3.1.7/pki/ca.crt
   EasyRSA Shell
   #
   ```
   
9. 查看CA证书及其私钥。
   - 生成的CA证书默认存放在"D:\\EasyRSA-3.1.7\\pki"目录下。 本示例中生成的证书为"ca.crt"。
     
   
   - 生成的CA私钥默认存放在"D:\\EasyRSA-3.1.7\\pki\\private"目录下。 本示例中生成的私钥为"ca.key"。
     
    
10. 执行以下命令，生成服务端证书及其私钥。 **./easyrsa build-server-full** *p2cserver.com* **nopass**
    ![](https://support.huaweicloud.com/admin-vpn/public_sys-resources/note_3.0-zh-cn.png)
    此命令中，*"p2cserver.com"*为服务端证书的CN，必须是域名格式，如"p2c-server.com"。否则无法正常托管到云证书与管理服务，请根据实际填写。
    系统显示如下类似信息：
    ```
    Using Easy-RSA 'vars' configuration:
    * D:/EasyRSA-3.1.7/pki/vars
    Using SSL:
    * openssl OpenSSL 3.1.2 1 Aug 2023 (Library: OpenSSL 3.1.2 1 Aug 2023)
    .+............+........+............+............+.+...............+.....+....+....................+............+.+..+......+............+....+.........+..+.........+.+.........+..............+.........+++++++++++++++++++++++++++++++++++++++*...+......+.....+......+...+++++++++++++++++++++++++++++++++++++++*..+...+..........+......+...........+....+......+.....+....+.....+....+........+...+.......+...+..+.......+..+......+.............+..+....+......+...+.....+................+......+..+.............+..+................+.....+......+....+...........+....+.....+.........+.+..+.............+...........+..........+......+........+............+...+....+..+......+......................+.....+......+.+...+..+...+.+......+........+...+....+.....+......+....+...+..+................+..+...+.......+..+......+..........+.........+...+..+.........+......+......++++++
    ........+.+......+...+......+.....+...+.+.....+.+........+......+++++++++++++++++++++++++++++++++++++++*...+.....+...+.+.........+......+........+++++++++++++++++++++++++++++++++++++++*......+........+.+...+.....+.+..............+.+.....+.+...+...+.....+.......+.................+.+............+..+......+...+....+...+..+.+.....+.....................+.+..+.+...................................+....+........+.............+.....+....+.....+...+..........+........+.+.....+...+.............+........+....+......+.....+.......+..+............+.........+.+......+...+...............+......+...........+............+.......+...........+.......+...............+......+.................+...+.+...+..+...+.+..........................+.+.........+......+............+..+....+..+....+........+.......+........+...+...+.+...+...+..+...............+...+..........+..+.......+.........+.....+.........+................+......+...+......+.....+.......+...+..............+.+.....+.+...+...........+.+...+...+...+............+..+.......+...........+.......+...+...+...........+.....................+...+....+...........+............+...+......+..........+........+.+.....+....+.....+.+..+..........+..............+...+......+.+...+...........+.+......+...++++++
    -----
    Notice
    ------
    Private-Key and Public-Certificate-Request files created.
    Your files are:
    * req: D:/EasyRSA-3.1.7/pki/reqs/p2cserver.com.req
    * key: D:/EasyRSA-3.1.7/pki/private/p2cserver.com.key
    You are about to sign the following certificate:
    Request subject, to be signed as a server certificate
    for '825' days:
    subject=
        commonName                = p2cserver.com
    Type the word 'yes' to continue, or any other input to abort.
      Confirm request details: yes    # 输入“yes”以继续
    Using configuration from D:/EasyRSA-3.1.7/pki/openssl-easyrsa.cnf
    Check that the request matches the signature
    Signature ok
    The Subject's Distinguished Name is as follows
    commonName            :ASN.1 12:'p2cserver.com'
    Certificate is to be certified until Sep 22 09:56:54 2026 GMT (825 days)
    Write out database with 1 new entries
    Database updated
    Notice
    ------
    Certificate created at:
    * D:/EasyRSA-3.1.7/pki/issued/p2cserver.com.crt
    Notice
    ------
    Inline file created:
    * D:/EasyRSA-3.1.7/pki/inline/p2cserver.com.inline
    EasyRSA Shell
    #
    ```
    
11. 查看服务端证书及其私钥。
    - 生成的服务端证书默认存放在"D:\\EasyRSA-3.1.7\\pki\\issued"目录下。 本示例中生成的服务端证书为"p2cserver.com.crt"。
      
    
    - 生成的服务端私钥默认存放在"D:\\EasyRSA-3.1.7\\pki\\private"目录下。 本示例中生成的服务端私钥为"p2cserver.com.key"。
      
     
12. 执行以下命令，生成客户端证书及其私钥。 **./easyrsa build-client-full** *p2cclient.com* **nopass**
    ![](https://support.huaweicloud.com/admin-vpn/public_sys-resources/note_3.0-zh-cn.png)
    此命令中，客户端证书的命名（如*"p2cclient.com"* ）应与服务端证书的命名（如*"p2cserver.com"*）不一致。
    系统显示如下类似信息：
    ```
    Using Easy-RSA 'vars' configuration:
    * D:/EasyRSA-3.1.7/pki/vars
    Using SSL:
    * openssl OpenSSL 3.1.2 1 Aug 2023 (Library: OpenSSL 3.1.2 1 Aug 2023)
    .......+++++++++++++++++++++++++++++++++++++++*...+...+...+.....+...+....+......+......+........+.+.....+............+++++++++++++++++++++++++++++++++++++++*.+.....+.+.....+.........+.......+..+...+.......+...+..+......+.+......+........+....+...+...+..+.......+......+.....+..........+...........+....+......+.....+.........+......+.+..+...+..........+........+......+....+......+...........+.......+.....+.............+..+.+...........+..........+...+..+.........+......+.+........+.........+.+...............+..+..........+...+............+...+.....+.+...........+....+.....+.........+....+.......................+....+...+..+....+..+.......+...+............+.....+............+.+........+.......+.....+....+.........+..+............+..........+..+.............+...+...+..++++++
    ..+.....+.......+.....+.........+....+++++++++++++++++++++++++++++++++++++++*.....+......+..+++++++++++++++++++++++++++++++++++++++*.......+.+.....+....+.........+...+.....+.........+...+...............+...+....+.....+.+...+......+......+...+.........+..+...+...+....+.........+..+...+...................+......+.....+.+...+...+.........+.....+..................+...+...+......+.+..+......+.+......+.....+...+..........+..+............+.......+.........+.....+......+.+..+............+................+..+...+....+......+.....+...+....+..+......+.........+.........++++++
    -----
    Notice
    ------
    Private-Key and Public-Certificate-Request files created.
    Your files are:
    * req: D:/EasyRSA-3.1.7/pki/reqs/p2cclient.com.req
    * key: D:/EasyRSA-3.1.7/pki/private/p2cclient.com.key
    You are about to sign the following certificate:
    Request subject, to be signed as a client certificate
    for '825' days:
    subject=
        commonName                = p2cclient.com
    Type the word 'yes' to continue, or any other input to abort.
      Confirm request details: yes    # 输入“yes”以继续
    Using configuration from D:/EasyRSA-3.1.7/pki/openssl-easyrsa.cnf
    Check that the request matches the signature
    Signature ok
    The Subject's Distinguished Name is as follows
    commonName            :ASN.1 12:'p2cclient.com'
    Certificate is to be certified until Sep 22 09:58:26 2026 GMT (825 days)
    Write out database with 1 new entries
    Database updated
    Notice
    ------
    Certificate created at:
    * D:/EasyRSA-3.1.7/pki/issued/p2cclient.com.crt
    Notice
    ------
    Inline file created:
    * D:/EasyRSA-3.1.7/pki/inline/p2cclient.com.inline
    EasyRSA Shell
    #
    ```
    
13. 查看客户端证书及其私钥。
    - 生成的客户端证书默认存放在"D:\\EasyRSA-3.1.7\\pki\\issued"目录下。 本示例中生成的客户端证书为"p2cclient.com.crt"。
      
    
    - 生成的客户端私钥默认存放在"D:\\EasyRSA-3.1.7\\pki\\private"目录下。 本示例中生成的客户端私钥为"p2cclient.com.key"。
      
     
 
