2013年2月24日日曜日

[SSLの勉強2]:クライアント証明書と認証

本などで勉強しても全く頭に入らないので、手を動かして理解する。
今回は、クライアントの認証を導入してみる。前回の設定等が済まされた状態を前提とする。

  • Mac Book Air
  • Mac OS X 10.8.2
  • OpenSSL 0.9.8
  • Apache 2.2.22
  • Google Chrome 24.0.1312

  1. クライントの秘密鍵・証明書作成
  2. 前回と同じように~/pkiで作業を行う。
    $ cd ~/pki
    $ mkdir ~/pki/client
    
    まずは、クライアントの秘密鍵の作成。
    $ openssl genrsa -des3 -out ~/pki/client/client.key 1024
    Generating RSA private key, 1024 bit long modulus
    ..++++++
    .....++++++
    e is 65537 (0x10001)
    Enter pass phrase for /Users/shu222/pki/client/client.key:
    Verifying - Enter pass phrase for /Users/shu222/pki/client/client.key:
    次に、クライアントの認証局への署名要求書の作成。
    $ openssl req -new -days 365 -key ~/pki/client/client.key -out ~/pki/client/csr.pem
    Enter pass phrase for /Users/shu222/pki/client/client.key:
    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) [JP]:
    State or Province Name (full name) [Kobe]:
    Locality Name (eg, city) [Chuo-ku]:
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:
    Organizational Unit Name (eg, section) []:
    Common Name (eg, YOUR name) []:noah
    Email Address []:
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    CAで署名する。クライアント証明書の出来上がり。
    $ openssl ca -in ~/pki/client/csr.pem -keyfile ~/pki/demoCA/private/cakey.pem -cert ~/pki/demoCA/cacert.pem -out ~/pki/client/cert.pem
    Using configuration from /System/Library/OpenSSL/openssl.cnf
    Enter pass phrase for /Users/testuser/pki/demoCA/private/cakey.pem:
    Check that the request matches the signature
    Signature ok
    Certificate Details:
            Serial Number:
                bd:48:b8:5b:4c:1c:57:12
            Validity
                Not Before: Feb 23 13:55:05 2013 GMT
                Not After : Feb 21 13:55:05 2023 GMT
            Subject:
                countryName               = JP
                stateOrProvinceName       = Kobe
                organizationName          = Internet Widgits Pty Ltd
                commonName                = noah
            X509v3 extensions:
                X509v3 Basic Constraints: 
                    CA:FALSE
                Netscape Cert Type: 
                    SSL Server
                Netscape Comment: 
                    OpenSSL Generated Certificate
                X509v3 Subject Key Identifier: 
                    0A:F6:CB:01:4E:FD:0C:4B:DB:3E:E1:6B:76:9A:C6:63:B8:49:64:57
                X509v3 Authority Key Identifier: 
                    keyid:09:18:4F:0E:9D:62:76:25:9D:1D:7F:34:9E:CC:5F:47:C0:DA:41:6B
    
    Certificate is to be certified until Feb 21 13:55:05 2023 GMT (3650 days)
    Sign the certificate? [y/n]:y
    
    
    1 out of 1 certificate requests certified, commit? [y/n]y
    Write out database with 1 new entries
    Data Base Updated
    
  3. ApacheのSSL設定
  4. Apacheを停止しておく。
    $ sudo apachectl stop
    httpd-ssl.confに以下の設定を記述する。

    認証局の証明書があるディレクトリのパス
    SSLCACertificatePath "/Users/testuser/pki/demoCA"
    認証局の証明書ファイルのパス
    SSLCACertificateFile "/Users/testuser/pki/demoCA/cacert.pem"
    クライントの認証方式。{none | optional | require | optional_no_ca}が選択できるよう。
    SSLVerifyClient require
    証明書チェインにおいて認証局を辿る深さ。今回はオレオレなので1。
    SSLVerifyDepth  1
  5. ブラウザにクライアント証明書を設定
  6. まず、クライアント証明書をpkcs12形式に変換する。
    $ openssl pkcs12 -export -in ~/pki/client/cert.pem -inkey ~/pki/client/client.key -certfile ~/pki/demoCA/cacert.pem -out ~/pki/client/cert.p12
    Chromeの場合、メニューバーの「Chrome」→「環境設定...」→一番下にスクロールして「詳細設定を表示...」
    →HTTP/SSLのエリアの「証明書の管理」→キーチェーンアクセスが起動→メニューバーの「ファイル」→「読み込む...」
    →クライアント証明書のファイル(上の場合だと、cert.p12)を選択して「開く」
    これでブラウザはクライアント証明書をサーバに送れるようになる。
    ちなみに設定前は、以下のような画面で警告される。

おわり

0 件のコメント:

コメントを投稿