Ubuntu 20.04で認証局(CA)を作成する方法

認証局(CA)は、通信を安全にするためにデジタル証明書を発行する責任を負うエンティティです。 これは、証明書の所有者および証明書に依存する当事者にとって信頼できる第三者として機能します。

認証局エンティティは、パブリックまたはプライベートのいずれかです。 パブリックCAは通常、WebサイトのIDを確認するために使用され、プライベートCAは、クライアントからサイトへのVPN、ユーザー、内部サーバー、またはローカルWebサーバーなどのインフラストラクチャ内の個々のプログラムとサービスの証明書を生成するために使用されます。

このチュートリアルでは、を作成する方法を学びます プライベート認証局(CA) の上 Ubuntu 20.04。 ここでは使用しています easy-rsa CAサーバーを構築および管理するためのCLIユーティリティ。

前提条件

  • CAサーバーをホストするUbuntu20.04のノード
  • のユーザー sudo 特権

ステップ1:システムを更新する

まず、Ubuntuシステムを更新し、次のコマンドを実行します。

apt update

公式リポジトリからeasy-rsaをインストールする場合は、この手順をスキップできます。

ステップ2:CAサーバーにEasy-RSAをインストールする

Easy-RSAは、認証局(CA)の確立と証明書の管理を大幅に容易にするコマンドラインツールです。 秘密鍵と公開ルート証明書を生成します。

Easy-RSAはデフォルトのaptリポジトリで利用できます。 公式から最新バージョンをインストールする easy-rsa GitHub リポジトリ。

Easy-RSAPKI管理ツールをGithubからダウンロードします。

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)の入力を求められます。必要に応じて、デフォルトの名前を使用できます。

この操作により、2つのメインファイルが作成されます。

  • The 公開証明書 ファイル ca.crt サーバーとクライアントが同じ信頼境界上にあることを確認するために使用するファイル
  • The 秘密鍵 ファイル 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/

次に、次のコマンドを使用してCAServerの証明書をインポートします。

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)を作成する方法を学びました。 読んでくれてありがとう、コメントセクションであなたのフィードバックと提案を提供してください。