手当たり次第に書くんだ

飽きっぽいのは本能

CentOS 8 easy-rsa を使用した SSL 証明書の作成

目次に戻る

概要

easy-rsaを使用して内部向けの自己署名証明書を作成します。easy-rsaはOpenVPNの証明書を作成する場合に良く使用されますが、easy-rsaは単にOpenSSLのフロントエンドとして機能しているだけですので他の用途でも問題なく使用可能です。また、やたらと長いOpenSSLコマンドを書く必要が無いので管理が楽です。

前提条件

OS

CentOS Stream 8を使用します。

SELinux

有効です。無効にする場合はこちらを参照して下さい。

Firewalld

無効です。有効化する場合はこちらを参照して必要な許可設定をして下さい。

その他

EPELリポジトリが必要です。

所感

SSLに拒否反応を示す人が意外と多いです。SSLは特に最近では必須であり、アプリケーション自体がSSLを要求している場合も多くなっています。

設定

インストール

[root@centos ~]# dnf install easy-rsa

easy-rsa本体とvarsを配置

配置場所やディレクトリ名は任意です。コピー元のバージョンは読み替えて下さい。

[root@centos ~]# cp -r /usr/share/easy-rsa/3.0.8 /root/work/easy-rsa
[root@centos ~]# cp /usr/share/doc/easy-rsa/vars.example /root/work/easy-rsa/vars

環境変数設定(vars)

easy-rsa/varsのデフォルト値

コメント行等を削除したvarsは下記の通りです。

if [ -z "$EASYRSA_CALLER" ]; then
    echo "You appear to be sourcing an Easy-RSA 'vars' file." >&2
    echo "This is no longer necessary and is disallowed. See the section called" >&2
    echo "'How to use this file' near the top comments for more details." >&2
    return 1
fi

easy-rsa/varsの設定

varsを編集します。

[root@centos ~]# cat <<EOF > /root/work/easy-rsa/vars
if [ -z "$EASYRSA_CALLER" ]; then
    echo "You appear to be sourcing an Easy-RSA 'vars' file." >&2
    echo "This is no longer necessary and is disallowed. See the section called" >&2
    echo "'How to use this file' near the top comments for more details." >&2
    return 1
fi
set_var EASYRSA_REQ_COUNTRY "JP"
set_var EASYRSA_REQ_PROVINCE "PROVINCE"
set_var EASYRSA_REQ_CITY "CITY"
set_var EASYRSA_REQ_ORG "si1230.com"
set_var EASYRSA_REQ_EMAIL "myadmin@si1230.com"
set_var EASYRSA_REQ_OU "administrator"
set_var EASYRSA_CERT_EXPIRE 365
set_var EASYRSA_CRL_DAYS 365
set_var EASYRSA_DIGEST "sha512"
EOF
設定項目 説明 デフォルト値 備考
EASYRSA_REQ_COUNTRY 国名を指定します。 blank
EASYRSA_REQ_PROVINCE 都道府県を指定します。 blank
EASYRSA_REQ_CITY 市区町村を指定します。 blank
EASYRSA_REQ_ORG 組織名(会社名等)を指定します。 blank
EASYRSA_REQ_EMAIL メールアドレスを指定します。 blank
EASYRSA_REQ_OU 部署名を指定します。 blank
EASYRSA_CERT_EXPIRE 証明書の有効期限を指定します。 825 サーバー/クライアント証明書の両方が対象です。Apple製品からもこの証明書を使用したサーバーに接続したい場合、Appleの証明書要件を考慮する必要があります。
EASYRSA_CRL_DAYS 失効リストの有効期限を指定します。 180 失効リストを使用するサーバーはこの期限を超えると接続不可となります。
EASYRSA_DIGEST ダイジェスト関数のアルゴリズムを指定します。 sha256 ダイジェスト関数(ハッシュ関数)は改竄検知に使用されます。

easy-rsa/varsでコメントアウトされている考慮すべきデフォルト値

前述の設定以外で主に考慮すべき個所は下記の通りです。今回はデフォルトから変更する必要性が無い為、明示的に設定していませんが(デフォルトはコメントアウト)、要件に応じて変更して下さい。

#set_var EASYRSA_KEY_SIZE 2048
#set_var EASYRSA_ALGO rsa
#set_var EASYRSA_CA_EXPIRE 3650
設定項目 説明 デフォルト値 備考
EASYRSA_KEY_SIZE 証明書の鍵長を指定します。 2048
EASYRSA_ALGO 証明書の暗号アルゴリズムを指定します。 rsa
EASYRSA_CA_EXPIRE ルート証明書の有効期限を指定する。 3650 少なくともサーバー/クライアント証明書より長い有効期限が必要です。

認証局構築

認証局を構築します(ルート証明書が発行されます)。鍵のパスワードとCommon Nameを決める(任意ですが分かりやすい名前にしましょう)必要があります。尚、以降のeasy-rsaのコマンドはeasy-rsaディレクトリ内で実行が必要です。

[root@centos ~]# cd /root/work/easy-rsa
[root@centos easy-rsa]# ./easyrsa init-pki

Note: using Easy-RSA configuration from: /root/easy-rsa/vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /root/easy-rsa/pki

[root@centos easy-rsa]# ./easyrsa build-ca

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020

Enter New CA Key Passphrase: password
Re-Enter New CA Key Passphrase: password
Generating RSA private key, 2048 bit long modulus (2 primes)
.................................................................................................+++++
........+++++
e is 65537 (0x010001)
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]: si1230.com CA

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/root/easy-rsa/pki/ca.crt

発行したルート証明書は下記のコマンドで確認します。

[root@centos ~]# openssl x509 -text -noout -in /root/work/easy-rsa/pki/ca.crt

サーバー証明書

サーバー署名要求(CSR)作成

CSRを作成します。署名は後述の手順で行います。

CSR作成のポイントは、①ワイルドカード証明書(*.si1230.com)で作成している、②alt-nameで*.si1230.comとsi1230.comを指定している、の2つです。有償のワイルドカード証明書は高額ですが、プライベートな環境で自己署名証明書を使用する場合はコストは関係ない為、ワイルドカード証明書としたほうが管理が楽です。

alt-nameは最近のブラウザが要求するため設定しています。alt-nameに2つ登録している理由は、ルートドメイン(例:www.si1230.comのwww.がないもの)が必要とされるケース(ads.txtなど)があるからです。これは内部用の証明書で必須ではありませんが、外部に公開する有償の証明書と合わせてみました。

[root@centos easy-rsa]# ./easyrsa --subject-alt-name='DNS:*.si1230.com,DNS:si1230.com,DNS:*.dev.si1230.com' gen-req server nopass

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020
Generating a RSA private key
...........+++++
..............................................................+++++
writing new private key to '/root/easy-rsa/pki/easy-rsa-254140.N8ccpD/tmp.hYgOLQ'
-----
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) [server]: *.si1230.com

Keypair and certificate request completed. Your files are:
req: /root/easy-rsa/pki/reqs/server.req
key: /root/easy-rsa/pki/private/server.key

CSRに署名

CSRに署名します。行末にserverが2つありますが、最初はコマンドの一部でサーバー証明書として署名するための指示、次はCSR(server.req)の指定です。

[root@centos easy-rsa]# ./easyrsa sign-req server server

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 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 365 days:

subject=
    commonName                = *.si1230.com

X509v3 Subject Alternative Name:
    DNS:*.si1230.com,DNS:si1230.com


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /root/easy-rsa/pki/easy-rsa-254839.X3WPdL/tmp.9Q1krJ
Enter pass phrase for /root/easy-rsa/pki/private/ca.key: password
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'*.si1230.com'
Certificate is to be certified until Apr 22 07:37:04 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /root/easy-rsa/pki/issued/server.crt

発行したサーバー証明書は下記のコマンドで確認します。

[root@centos ~]# openssl x509 -text -noout -in /root/work/easy-rsa/pki/issued/server.crt

証明書廃止リスト

証明書廃止リスト(CRL)は一度失効させた証明書が必要になるため、dummyの証明書を作成して失効させてからCRLを作成します。CRLはOpenVPN(VyOS)での使用を想定しています。

[root@centos easy-rsa]# ./easyrsa build-client-full dummy nopass

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020
Generating a RSA private key
.......................................................................................................+++++
......................................................................................................................+++++
writing new private key to '/root/easy-rsa/pki/easy-rsa-255090.zLJNk1/tmp.ADsvqM'
-----
Using configuration from /root/easy-rsa/pki/easy-rsa-255090.zLJNk1/tmp.WyyyZF
Enter pass phrase for /root/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'dummy'
Certificate is to be certified until Apr 22 07:54:03 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

[root@centos easy-rsa]# ./easyrsa revoke dummy

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020


Please confirm you wish to revoke the certificate with the following subject:

subject=
    commonName                = dummy


Type the word 'yes' to continue, or any other input to abort.
  Continue with revocation: yes
Using configuration from /root/easy-rsa/pki/easy-rsa-255167.NDYQUV/tmp.CntwRj
Enter pass phrase for /root/easy-rsa/pki/private/ca.key: password
Revoking Certificate FB4907E6A161278F63ED1BED2AECE10E.
Data Base Updated

IMPORTANT!!!

Revocation was successful. You must run gen-crl and upload a CRL to your
infrastructure in order to prevent the revoked cert from being accepted.


[root@centos easy-rsa]# ./easyrsa gen-crl

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020
Using configuration from /root/easy-rsa/pki/easy-rsa-255212.cPSOgd/tmp.mEbPBF
Enter pass phrase for /root/easy-rsa/pki/private/ca.key: password

An updated CRL has been created.
CRL file: /root/easy-rsa/pki/crl.pem

DHパラメータ

DHパラメータを作成します。DHパラメータはOpenVPN(VyOS)での使用を想定しています。

[root@centos ~]# ./easyrsa gen-dh

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time

DH parameters of size 2048 created at /root/easy-rsa/pki/dh.pem

クライアント証明書

クライアント証明書はOpenVPN(VyOS)の認証に使用します。

[root@centos easy-rsa]# ./easyrsa build-client-full client1 nopass

Note: using Easy-RSA configuration from: /root/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1g FIPS  21 Apr 2020
Generating a RSA private key
..........+++++
....+++++
writing new private key to '/root/easy-rsa/pki/easy-rsa-258174.fpPynp/tmp.2Pv2lo'
-----
Using configuration from /root/easy-rsa/pki/easy-rsa-258174.fpPynp/tmp.QQedZ8
Enter pass phrase for /root/easy-rsa/pki/private/ca.key: password
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'client1'
Certificate is to be certified until Apr 22 09:34:48 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

発行したサーバー証明書は下記のコマンドで確認します。

[root@centos ~]# openssl x509 -text -noout -in /root/easy-rsa/pki/issued/client1.crt

証明書関連ファイル

ここまでで作成した証明書関連ファイルの一覧です。

項目 ファイルパス 備考
ルート証明書 /root/easy-rsa/pki/ca.crt クライアントの信頼する証明書機関に追加しましょう。
サーバー証明書の秘密鍵 /root/easy-rsa/pki/private/server.key
サーバー証明書 /root/easy-rsa/pki/issued/server.crt
CRL /root/easy-rsa/pki/crl.pem
DHパラメータ /root/easy-rsa/pki/dh.pem
クライアント証明書の秘密鍵 /root/easy-rsa/pki/private/client1.key
クライアント証明書 /root/easy-rsa/pki/issued/client1.crt

目次に戻る

CentOS 8 easy-rsa を使用した SSL 証明書の作成

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

トップへ戻る