LoginSignup
13
12

More than 5 years have passed since last update.

Keytoolを使用したSHA2 SSL証明書作成メモ

Posted at

Https通信する際に、自前でオレオレ証明書を作成する際のメモ

環境

レガシーな環境を想定して、Javaは1.7で実施

[vagrant@vagrant-centos63 ~]$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode, sharing)
[vagrant@vagrant-centos63 ~]$ openssl version
OpenSSL 1.0.0-fips 29 Mar 2010

キーストアの作成

発行した秘密鍵や証明書チェーンを保存するためのキーストアを作成する。

# keytool -genkeypair -alias testkeystore -keyalg RSA -keysize 2048 -keystore test.jks -storetype JKS -validity 3650 -keypass hogehoge -storepass hogehoge

上記コマンドの実行後、いくつか聞かれるの内容の応答サンプル。

What is your first and last name?
  [Unknown]:  example.com
What is the name of your organizational unit?
  [Unknown]:  TestUnit
What is the name of your organization?
  [Unknown]:  Test
What is the name of your City or Locality?
  [Unknown]:  Chiyoda-ku
What is the name of your State or Province?
  [Unknown]:  Tokyo
What is the two-letter country code for this unit?
  [Unknown]:  JP
Is CN=example.com, OU=TestUnit, O=Test, L=Chiyoda-ku, ST=Tokyo, C=JP correct?
  [no]:  yes

完了すると、test.jksが作成される。

[root@vagrant-centos63 keytest]# ls
test.jks

CAの作成

自前の証明書を作成するために、同じサーバにCAを作成しておく。

 # cd /usr/local/ssl
 # ./CA.sh  -newca

証明書要求(CSR)の作成

認証局に署名してもらうための証明書要求を作成する。

 # keytool -certreq -alias testkeystore -sigalg SHA256withRSA -file test.csr -keypass hogehoge -keystore test.jks -storepass hogehoge -storetype JKS

CAでの署名

CAで署名して、証明書を発行する。この際にsha2ハッシュの証明書にしてもらうように、オプションで指定する。

# openssl ca -md sha256 -keyfile /usr/local/ssl/CA/private/cakey.pem -cert /usr/local/ssl/CA/cacert.pem -in test.csr -out testservercert.crt -days 1460 -policy policy_anything

上記を実行するとtestservercert.crtが作成される。これの中身を、-----BEGIN CERTIFICATE----------END CERTIFICATE-----とその中身だけになるように、ほかを削除しておく。

CAの登録

作成した自前CAを登録する。

# keytool -importcert -alias testRootCA -file /etc/pki/CA/certs/ca.crt -keystore test.jks -trustcacerts -storepass hogehoge

証明書の登録

# keytool -import -alias test -file testservercert.crt -keypass hogehoge -keystore test.jks -storepass hogehoge

証明書の確認

 # keytool -list -v -keystore /root/key4/test.jks -storepass hogehoge
13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12