誰得?だけどTomcatとかでつかうkeystoreのテスト書いてみた。

JKSのファイルが正しくできているかどうかを確かめたくて作ってみた。

JRuby + RSpecで実行する。JRubyあんまりつかったことなくて、to_javaとか、
to_inputstreamで引っかかった。

証明書のインポートがちゃんとできてるか、キーチェインがちゃんと解決できるかを確かめてる。

キーストアにはドメイン名をエイリアスにしてRSA鍵をつくっていて、発行された証明書をインポート、ルート証明書(root)、中間証明書(pubcag3)は括弧内のエイリアスにしている。

require 'spec_helper'
require 'java'

describe "tomcat.jks" do
  let(:keystore_passwd) { "KEYSTORE_PASSWD"}
  let(:keystore_file) { File.new("fixtures/tomcat.jks") }
  subject(:keystore) { java.security.KeyStore.getInstance("jks") }

  it "should be exists" do
    expect( File.exist?(keystore_file)).to be_truthy 
  end

  before do
    file = keystore_file.to_inputstream
    passwd = keystore_passwd.to_java.toCharArray
    keystore.load(file,passwd)
    file.close
  end

  %w/YOUR_DOMAIN root pubcag3/.each do |a|
    it "should has #{a} alias" do
      expect(subject.containsAlias(a)).to be_truthy 
    end
  end
  
  describe "YOUR_DOMAIN" do
    let(:target_cert) { keystore.getCertificate('YOUR_DOMAIN') }
    let(:cert_chain) { keystore.getCertificateChain('YOUR_DOMAIN') }
    let(:root_cert) { keystore.getCertificate('root') }
    let(:pubcag3_cert) { keystore.getCertificate('pubcag3') }

    it "should have key" do
      expect(keystore.isKeyEntry('YOUR_DOMAIN')).to be_truthy
    end

    it "should contains certificate" do
      expect(target_cert).not_to be_nil
    end

    it "should have signed certificate by SHA2withRSA" do
      expect(target_cert.getSigAlgName).to eq('SHA256withRSA')
    end

    it "should have certificate chain." do 
      expect(cert_chain.length).to eq(3) 
    end

    it "should be resolved with certificate chain." do 
      expect(cert_chain).to include(target_cert, pubcag3_cert, root_cert)
    end
  end

end