GeekFactory

int128.hatenablog.com

certtoolコマンドとテンプレートで簡単に証明書を作る

Apache httpdOpenVPNなどで自前のSSL証明書を使うには、一般に以下の手順が必要になります。

  1. CAの秘密鍵を生成する。
  2. CAの自己署名証明書を生成する。
  3. サーバの秘密鍵を生成する。
  4. サーバのCSRを生成してCAに送る。
  5. CAで証明書を発行する。
  6. クライアントの証明書が必要な場合は3〜5を行う。

実際にこれらの手順を行うにはopensslコマンドで煩雑な手順が必要になります。また、途中で間違えた場合はDN(Distinguished Name)を何度も入力する必要があり、面倒でミスを誘発しやすい作業です。

そこで、gnutlsパッケージに含まれるcerttoolコマンドを使うと、あらかじめDNやKey Usageを記述したテンプレートファイルから証明書を生成できます。

certtoolによる秘密鍵や証明書の生成

certtoolコマンドを使うにはgnutlsパッケージをインストールします。Linuxでは yum installapt-get install などでインストールできます。OS Xでは brew install でインストールできますが、コマンド名は gnutls-certtool となります(OS X標準のcerttoolと区別するためのようです)。

certtoolの詳細な使い方は http://www.gnutls.org/manual/html_node/certtool-Invocation.html に書いてあります。

例えば、自己署名証明書を生成するには下記を実行します。

certtool -s --hash "アルゴリズム" --load-privkey "秘密鍵ファイル" --template "テンプレートファイル" --outfile "出力先ファイル"

テンプレートファイルの書式は http://www.gnutls.org/manual/html_node/certtool-Invocation.html#Certtool_0027s-template-file-format を参照してください。例えば、下記のように書くとサーバ証明書が生成されます。

# X.509 Certificate options

# DN options
organization = "Some Organization"
state = "Tokyo"
country = JP
cn = "client"
expiration_days = 365

# X.509 v3 extensions
signing_key
encryption_key
tls_www_server

certtoolを簡単に使う

.zshrc や .bashrc に下記を書きます。

function certtool_generate_key () {
  local key_file="$1"
  certtool -p --bits 4096 --outfile "$key_file"
}

function certtool_generate_csr () {
  local key_file="$1"
  local template_file="$2"
  local csr_file="$3"
  certtool -q --hash SHA512 --load-privkey "$key_file" --template "$template_file" --outfile "$csr_file"
}

function certtool_sign_crt () {
  local ca_key="$1"
  local ca_crt="$2"
  local csr_file="$3"
  local crt_file="$4"
  certtool -c --hash SHA512 --load-ca-privkey "$ca_key" --load-ca-certificate "$ca_crt" --load-request "$csr_file" --outfile "$crt_file"
}

function certtool_generate_self_crt () {
  local key_file="$1"
  local template_file="$2"
  local crt_file="$3"
  certtool -s --hash SHA512 --load-privkey "$key_file" --template "$template_file" --outfile "$crt_file"
}

function certtool_generate_dh_patams () {
  local dh_file="$1"
  certtool --generate-dh-params --bits 4096 --outfile "$dh_file"
}

先ほどの自己署名証明書を生成する例は下記に変わります。

certtool_generate_self_crt "秘密鍵ファイル" "テンプレートファイル" "出力先ファイル"

引数を忘れた時は .zshrc などを見れば一目瞭然です。

それほど利用頻度が高いコマンドではありませんが、いざという時にcerttoolが簡単に使えると便利です。いつもopensslやcerttoolの引数を忘れて困るという方はお試しください。

おまけ:opensslコマンド

多くの環境にデフォルトで入っているopensslコマンドを使いたいという方もいるでしょう。おまけで書いておきます。

opensslコマンドで秘密鍵を生成するには下記を実行します。

% openssl genrsa -out client.key 4096

CSRの生成は下記になります。

% openssl req -new -key client.key -out client.csr
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]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:.
Organization Name (eg, company) [Internet Widgits Pty Ltd]:.
Organizational Unit Name (eg, section) []:.
Common Name (e.g. server FQDN or YOUR name) []:client
Email Address []:

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