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


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

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

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

先決條件

  • 託管 CA 服務器的 Ubuntu 20.04 節點
  • 具有 sudo 權限的用戶

第一步:更新系統

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

apt update

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

步驟 2:在 CA 服務器上安裝 Easy-RSA

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

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

從 Github 下載 Easy-RSAPKI 管理工具。

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 用於簽署服務器和客戶端證書的目錄

第 4 步:導入 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 隧道證書(例如,使用 OpenVPN)。

注意:出於安全原因,我們建議您不要在 CA 服務器上運行任何其他服務。它只能用作導入、簽名和撤銷證書請求的獨立服務器。

結論是

在本教程中,您學習瞭如何在 Ubuntu 20.04 上創建私有證書頒發機構 (CA)。感謝您的閱讀,請在評論部分提供您的反饋和建議。