如何在 Ubuntu 20.04 上創建證書頒發機構 (CA)

證書頒發機構 (CA) 是負責頒發數字證書以確保通信安全的實體。 它充當證書所有者和依賴證書的一方的受信任第三方。

證書頒發機構實體可以是公共的或私有的。 公共 CA 通常用於驗證網站的身份,私有 CA 用於為客戶端到站點 VPN、用戶、內部服務器或基礎架構中的單個程序和服務(例如本地 Web 服務器)生成證書。

在本教程中,我們將學習如何創建一個 私有證書頒發機構 (CA)Ubuntu 20.04. 這裡我們使用 易RSA 用於構建和管理 CA 服務器的 CLI 實用程序。

先決條件

  • 一個帶有 Ubuntu 20.04 的節點來託管 CA 服務器
  • 一個用戶 sudo 特權

第 1 步:更新您的系統

首先,更新您的 Ubuntu 系統,運行以下命令:

apt update

如果您從官方存儲庫安裝 easy-rsa,則可以跳過此步驟。

第二步:在CA服務器上安裝Easy-RSA

Easy-RSA 是一個命令行工具,它極大地促進了證書頒發機構 (CA) 的建立和證書的管理。 它生成一個私鑰和公共根證書。

Easy-RSA 在默認的 apt 存儲庫中可用。 從官方安裝最新版本 簡單的 rsa GitHub 存儲庫。

從 Github 下載 Easy-RSA PKI 管理工具:

wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz

這裡已經下載了 3.0.8 版本。 現在讓我們將文件移動到 /opt 文件夾

sudo mv EasyRSA-3.0.8.tgz /opt

現在將目錄更改為 /opt:

cd /opt

運行以下命令解壓縮 .tgz 文件:

sudo tar xvf EasyRSA-3.0.8.tgz

使用 mv 命令重命名目錄:

sudo mv EasyRSA-3.0.8 easy-rsa

現在讓非 root 用戶成為目錄的所有者:

sudo chown -R franck:franck easy-rsa/

限制對 PKI 目錄的訪問,僅限所有者:

sudo chmod 700 easy-rsa

步驟 3:設置 CA 服務器

在這裡,我們將設置公鑰基礎結構目錄並為 CA 服務器創建一個公共/私有證書。

現在將目錄更改為之前創建的 easy-rsa:

cd easy-rsa

我們將創建一個 vars 將用於存儲組織信息的文件

$ cp vars.example vars

現在在文件末尾添加組織信息

$ vim vars

set_var EASYRSA_REQ_COUNTRY "CM"
set_var EASYRSA_REQ_PROVINCE "Centre"
set_var EASYRSA_REQ_CITY "Yaounde"
set_var EASYRSA_REQ_ORG "LINUXSHARE"
set_var EASYRSA_REQ_EMAIL "[email protected]"
set_var EASYRSA_REQ_OU "Com"
set_var EASYRSA_ALGO "ec"
set_var EASYRSA_DIGEST "sha512"

現在我們將初始化公鑰基礎設施目錄:

$ ./easyrsa init-pki

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /opt/easy-rsa/pki

生成 根公共私鑰對 對於 CA 服務器,鍵入:

$ ./easyrsa build-ca

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
read EC key
writing EC key
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]:
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/opt/easy-rsa/pki/ca.crt

在您需要簽署或撤銷證書的任何時候,系統都會要求您輸入密鑰對的密碼。 系統還會要求您提供 CA 的通用名稱 (CN),如果需要,您可以使用默認名稱。

該操作將創建兩個主要文件:

  • 公證 文件 ca.crt 服務器和客戶端將使用該文件來驗證它們是否在同一信任範圍內
  • 私鑰 文件 ca.key 在裡面 pki/private CA 用於簽署服務器和客戶端證書的目錄

第四步:導入CA公證書

現在我們生成了公共證書,我們需要將其導入到另一台服務器上。

登錄到要導入證書的服務器,然後執行遠程複製 ca.crt 來自 CA 服務器的文件。

scp [email protected]:/opt/easy-rsa/pki/ca.crt

The authenticity of host 'X.Y.Z.T (X.Y.Z.T)' can't be established.
ECDSA key fingerprint is SHA256:ffUgP5/d0Z3miOKqxBVoF9JbFvIZFs/gxr7ESBZ0kmQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '139.177.204.145' (ECDSA) to the list of known hosts.
[email protected]'s password:
ca.crt
100% 749 2.4KB/s 00:00

將證書文件移動到 /usr/local/share/ca-certificates/ 目錄:

sudo mv ca.crt /usr/local/share/ca-certificates/

現在使用以下命令導入 CA 服務器的證書:

sudo update-ca-certificates

Updating certificates in /etc/ssl/certs…
1 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d…
done.

這樣,您的服務器現在將信任由您的 CA 服務器簽名的證書。

步驟 5:創建服務器證書請求和私鑰

我們可以在不同的服務器上創建一些證書籤名請求 (CSR),讓我們的 CA 對這些請求進行簽名。

我們將使用 OpenSSL 來創建 CSR 文件。 如果未安裝 OpenSSL,請使用以下命令進行安裝:

sudo apt install openssl

創建一個名為 server1-csr 的目錄來保存 CSR 和私鑰

mkdir server1-csr

切換到 server1-csr 目錄

cd server1-csr

現在使用 OpenSSL 生成私鑰:

openssl genrsa -out server1.key

Generating RSA private key, 2048 bit long modulus (2 primes)
..........+++++
..................................................+++++
e is 65537 (0x010001)

您可以使用生成的密鑰生成相應的 CSR:

$ openssl req -new -key server1.key -out server1.req

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.
-----
Country Name (2 letter code) [AU]:CM
State or Province Name (full name) [Some-State]:CE
Locality Name (eg, city) []:Yaounde
Organization Name (eg, company) [Internet Widgits Pty Ltd]:LinuxShare
Organizational Unit Name (eg, section) []:Tech-B
Common Name (e.g. server FQDN or YOUR name) []:server1
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

現在您需要將 CSR 文件複製到 CA 服務器:

$ scp server1.req scp [email protected]:/tmp/server1.req

步驟 6:在 CA 服務器上簽署服務器 CSR

生成的 CSR 應由 CA 服務器簽名。 為此,首先移動到 easy-rsa 目錄以導入服務器的證書籤名請求。

./easyrsa import-req /opt/sign-cert/server1.req server1

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
The request has been successfully imported with a short name of: server1
You may now use this name to perform signing operations on this request.

現在使用以下命令簽署 CSR:

./easyrsa sign-req server server1

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
Request subject, to be signed as a server certificate for 825 days:
subject=
countryName = CM
stateOrProvinceName = CE
localityName = Yaounde
organizationName = LinuxShare
organizationalUnitName = Tech-B
commonName = server1
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/easy-rsa/pki/easy-rsa-161486.BI2HwH/tmp.lIqZoF
Enter pass phrase for /opt/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CM'
stateOrProvinceName :ASN.1 12:'CE'
localityName :ASN.1 12:'Yaounde'
organizationName :ASN.1 12:'LinuxShare'
organizationalUnitName:ASN.1 12:'Tech-B'
commonName :ASN.1 12:'server1'
Certificate is to be certified until Jan 5 16:57:26 2024 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /opt/easy-rsa/pki/issued/server1.crt

從輸出中您可以看到 /opt/easy-rsa/pki/issued/ 目錄下頒發的證書。 您還可以通過列出證書來驗證證書:

$ ls -l /opt/easy-rsa/pki/issued

輸出:

total 4
-rw------- 1 franck franck 3996 Oct 2 16:57 server1.crt

通過所有這些步驟,您可以自己管理內部服務器的證書。 使用 CA 服務器,您可以為 Web 服務器或 VPN 隧道簽署證書 example 使用 OpenVPN。

注意:出於安全原因,建議不要在 CA 服務器上運行任何其他服務。 它應該僅用於作為獨立服務器導入、簽署和撤銷證書請求。

結論

在本教程中,我們學習瞭如何在 Ubuntu 20.04 上創建私有證書頒發機構 (CA)。 感謝閱讀,歡迎在評論區提出您的意見和建議。