Smile Engineering Blog

ジェイエスピーからTipsや技術特集、プロジェクト物語を発信します

Easy-RSA による公開鍵基盤(PKI)

はじめに

いわゆるオレオレ的な証明書の発行のためにプライベートな認証局(CA)を構築する、なんて経験は誰もがあるはず。自分も今までは CA スクリプトなど使いながらわりと苦労して構築してたんですが Easy-RSA なるシェルで簡単に構築できることが分かったので備忘録として残しておきます。

前提:

認証局(CA)の構築

インストール

Easy-RSA は apt でもインストール可能ですが今回の手順では GitHub から clone して利用します。作業ディレクトリ内に認証局(CA)を構築することになるのですが、Easy-RSA によって作られるディレクトリやファイルを確認できる、というのがその理由です。失敗したら git clean で作り直せば良いし。なのでインストールは clone して終わりです。

$ git clone git@github.com:OpenVPN/easy-rsa.git -b v3.0.8

clone すると作業ディレクトリの中に easyrsa3 ディレクトリがあり、

$ tree -L 1 -F easy-rsa
easy-rsa
├── build/
├── ChangeLog
├── COPYING.md
├── distro/
├── doc/
├── easyrsa3/               // ★ここです
├── KNOWN_ISSUES
├── Licensing/
├── op_test.orig*
├── op_test.sh*
├── README.md
├── README.quickstart.md
├── release-keys/
├── wop_test.bat
└── wop_test.sh

6 directories, 9 files

その下に easyrsa 本体(シェルスクリプト)があります。

$ tree -F easy-rsa/easyrsa3
easy-rsa/easyrsa3
├── easyrsa*                // ★本体(シェルスクリプト)
├── openssl-easyrsa.cnf
├── pki/
│   ├── openssl-easyrsa.cnf
│   ├── private/
│   ├── reqs/
│   └── safessl-easyrsa.cnf
├── vars.example
└── x509-types/
    ├── ca
    ├── client
    ├── code-signing
    ├── COMMON
    ├── email
    ├── kdc
    ├── server
    └── serverClient

4 directories, 13 files

以降の手順は easy-rsa/easyrsa3 の中で作業を行うので移動します。

$ cd easy-rsa/easyrsa3

公開鍵基盤(PKI)の初期化

認証局(CA)を構築する前に公開鍵基盤(PKI)を初期化します。初期化には init-pki コマンドを使用します。

$ ./easyrsa init-pki

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

初期化された pki ディレクトリが新たに作られます。

$ tree -F
.
├── easyrsa*
├── openssl-easyrsa.cnf
├── pki/                        // ★これ
│   ├── openssl-easyrsa.cnf
│   ├── private/
│   ├── reqs/
│   └── safessl-easyrsa.cnf
├── vars.example
└── x509-types/
    ├── ca
    ├── client
    ├── code-signing
    ├── COMMON
    ├── email
    ├── kdc
    ├── server
    └── serverClient

4 directories, 13 files

認証局(CA)の構築

認証局(CA)を構築します。構築には build-ca コマンドを使用します。途中、パスフレーズCommon Name を聞かれるので応えます。

$ ./easyrsa build-ca
Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020

Enter New CA Key Passphrase:                                        // ★パスフレーズ入力
Re-Enter New CA Key Passphrase:                                     // ★パスフレーズ再入力(確認)
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]:    // ★Common Name 入力

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

認証局(CA)が構築され、証明書と秘密鍵ができあがりました。

$ tree -F
.
├── easyrsa*
├── openssl-easyrsa.cnf
├── pki/
│   ├── ca.crt                      // ★認証局(CA)の証明書
│   ├── certs_by_serial/
│   ├── index.txt
│   ├── index.txt.attr
│   ├── issued/
│   ├── openssl-easyrsa.cnf
│   ├── private/
│   │   └── ca.key                  // ★認証局(CA)の秘密鍵
│   ├── renewed/
│   │   ├── certs_by_serial/
│   │   ├── private_by_serial/
│   │   └── reqs_by_serial/
│   ├── reqs/
│   ├── revoked/
│   │   ├── certs_by_serial/
│   │   ├── private_by_serial/
│   │   └── reqs_by_serial/
│   ├── safessl-easyrsa.cnf
│   └── serial
├── vars.example
└── x509-types/
    ├── ca
    ├── client
    ├── code-signing
    ├── COMMON
    ├── email
    ├── kdc
    ├── server
    └── serverClient

14 directories, 18 files

これで証明書要求に対して署名したり、証明書を失効させたりできるようになりました。めちゃ簡単ですね。

オプション

上記は全てデフォルト値で構築しましたが、暗号アルゴリズムや鍵の有効期限など各種パラメータを指定して構築するには vars ファイルを使用します。vars ファイルは build-ca する前に新規作成し、変更したいパラメータを記載しておきます。

set_var EASYRSA_CA_EXPIRE   365         # 有効期限を 365 日
set_var EASYRSA_ALGO        "ec"        # 暗号アルゴリズムに楕円曲線暗号(ECC)
set_var EASYRSA_DIGEST      "sha512"    # 署名のためのハッシュ関数

vars ファイルの詳細については vars.example ファイルを参照してください。

これらは build-ca コマンドのオプションで指定することもできます。

$ ./easyrsa help options

Easy-RSA Global Option Flags

The following options may be provided before the command. Options specified
at runtime override env-vars and any 'vars' file in use. Unless noted,
non-empty values to options are mandatory.

General options:

--batch         : set automatic (no-prompts when possible) mode
--passin=ARG    : set -passin ARG for openssl
--passout=ARG   : set -passout ARG for openssl
--pki-dir=DIR   : declares the PKI directory
--vars=FILE     : define a specific 'vars' file to use for Easy-RSA config
--version       : prints EasyRSA version and build information, then exits

Certificate & Request options: (these impact cert/req field values)

--days=#        : sets the signing validity to the specified number of days
--digest=ALG    : digest to use in the requests & certificates
--dn-mode=MODE  : DN mode to use (cn_only or org)
--keysize=#     : size in bits of keypair to generate
--req-cn=NAME   : default CN to use
--subca-len=#   : path length of signed intermediate CA certs; must be >= 0 if used
--subject-alt-name : Add a subjectAltName. For more info and syntax, see:
                     ./easyrsa help altname
--use-algo=ALG  : crypto alg to use: choose rsa (default) or ec
--curve=NAME    : for elliptic curve, sets the named curve to use
--copy-ext      : Copy included request X509 extensions (namely subjAltName

Organizational DN options: (only used with the 'org' DN mode)
  (values may be blank for org DN options)

--req-c=CC        : country code (2-letters)
--req-st=NAME     : State/Province
--req-city=NAME   : City/Locality
--req-org=NAME    : Organization
--req-email=NAME  : Email addresses
--req-ou=NAME     : Organizational Unit

Deprecated features:

--ns-cert=YESNO       : yes or no to including deprecated NS extensions
--ns-comment=COMMENT  : NS comment to include (value may be blank)

構築した後に再構築する場合はもう一度 init-pki コマンドで初期化する必要があります。

認証局(CA)の証明書配布

上記手順で作成した証明書 ca.crt を各システムにインストールするには以下の手順に従います。

Ubuntu and Debian derived distributions

$ sudo cp /tmp/ca.crt /usr/local/share/ca-certificates/
$ sudo update-ca-certificates

CentOS, Fedora, RedHat distributions

$ sudo cp /tmp/ca.crt /etc/pki/ca-trust/source/anchors/
$ sudo update-ca-trust

Windows

「信頼されたルート証明機関」のストアにインストールします。

続く

今回はいったんここまで。次回 は公開鍵基盤(PKI)の「利用者」の視点から、

  • 鍵ペアを作成
  • 認証局(CA)に公開鍵を登録
  • 認証局(CA)から証明書を発行してもらう

あたりをまとめたいと思います。

参考