labunix's blog

labunixのラボUnix

LPIC303 Ver2.0、公開鍵証明書のライフサイクルの復習をしてみる。

■LPIC303 Ver2.0、公開鍵証明書のライフサイクルの復習をしてみる。

 KVM上にLPIC303 Ver2.0の学習環境を構築する。
 http://labunix.hateblo.jp/entry/20180715/1531661773

 LPIC303 Ver2.0、公開鍵の基礎の復習をしてみる。
 http://labunix.hateblo.jp/entry/20180716/1531669914

■公開鍵証明書のライフサイクルを確認するには、gpgが分かりやすい。
 認証局(CA)の場合CRLの管理は別途、無効化中および失効中の証明書を失効リスト(CRL)リストに登録することで行う。

 gpgによるテキスト暗号化、署名と暗号化
 http://d.hatena.ne.jp/labunix/20130527

 gnupgのソースの署名の検証
 http://d.hatena.ne.jp/labunix/20130528

$ echo "([無効化中] [使用中] [更新中] [失効中])
        (証明書の更新 [更新手続き] --> [更新中] --> [使用中]) \
        (証明書の保留 [保留手続き] --> [無効化中] --> [再有効化手続き] --> [使用中]) \
        (証明書の破棄 [破棄手続き] --> [失効中] --> [再発行手続き] --> [ 発行中]) \
        (証明書の基本サイクル [発行手続き] --> [発行中] --> [使用中] --> [有効 期限切れ]) \
         [使用中] --> [更新中],[無効化中],[失効中]
       " | graph-easy --dot | dot -T svg -o lifecycle.svg

f:id:labunix:20180716185239p:plain

■余談とはなるけれど、プロキシ環境だから
 opensslコマンドで直接外に出れないよという方は以下の方法で。

# apt-file search bin/proxytunnel
proxytunnel: /usr/bin/proxytunnel

# apt-get install -y proxytunnel
# proxytunnel -v -p 172.31.31.93:8080 -d www.google.com:443 -a 7000 &
# openssl s_client -connect localhost:7000 </dev/null 2>/dev/null | openssl x509 -noout -subject
subject=C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
# jobs
[1]+  実行中               proxytunnel -v -p 172.31.31.93:8080 -d www.google.com:443 -a 7000 &
# fg 1
proxytunnel -v -p 172.31.31.93:8080 -d www.google.com:443 -a 7000
^C

■上記の無効化中および失効中のリストを管理するCRLやOCSPによって、
 有効期限よりも前に無効化も含めて失効されているか否かを確認出来る。

$ openssl s_client -connect www.google.com:443 </dev/null 2>/dev/null | \
      openssl x509 -ocsp_uri 2>&1 | head -1
http://ocsp.pki.goog/GTSGIAG3

$ openssl s_client -connect www.google.com:443 </dev/null | \
    openssl x509 -noout -text | grep "crl\|ocsp"
depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
verify return:1
DONE
                OCSP - URI:http://ocsp.pki.goog/GTSGIAG3
                  URI:http://crl.pki.goog/GTSGIAG3.crl

■CRLファイルを別に取得するよう0以外を返す。

$ openssl s_client -connect www.google.com:443 -crl_check_all < /dev/null 2>/dev/null | grep CRL
Verification error: unable to get certificate CRL
    Verify return code: 3 (unable to get certificate CRL)

■「OCSP response: no response sent」なので、OCSPでの取得には対応していない様子。

$ openssl s_client -connect www.google.com:443 -status -servername www.google.com < /dev/null | head
depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
verify return:1
DONE
CONNECTED(00000003)
OCSP response: no response sent
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
   i:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
 1 s:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
   i:/OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
---
Server certificate

■「OCSP Response」を返す場合は以下のような表示になる。

$ openssl s_client -connect jp.globalsign.com:443 -status < /dev/null | head 
depth=2 OU = GlobalSign Root CA - R3, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = BE, O = GlobalSign nv-sa, CN = GlobalSign Extended Validation CA - SHA256 - G3
verify return:1
depth=0 businessCategory = Private Organization, serialNumber = 0110-01-040181, jurisdictionC = JP, C = JP, ST = Tokyo, L = Shibuya, street = 26-1 Sakuragaoka, O = GMO GlobalSign K.K., CN = jp.globalsign.com
verify return:1
CONNECTED(00000003)
OCSP response: 
======================================
OCSP Response Data:
    OCSP Response Status: successful (0x0)
    Response Type: Basic OCSP Response
    Version: 1 (0x0)
    Responder Id: E2F07AE356B24498C382E97E9F4333AC74B7773D
    Produced At: Jul 15 11:02:01 2018 GMT
    Responses:
DONE

$ openssl s_client -connect www.symantec.com:443 -status < /dev/null | head 
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert High Assurance EV Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert SHA2 Extended Validation Server CA
verify return:1
depth=0 businessCategory = Private Organization, jurisdictionC = US, jurisdictionST = Delaware, serialNumber = 2158113, C = US, ST = California, L = Mountain View, O = Symantec Corporation, OU = Corp Mktg & Comms - Online Exp, CN = www.symantec.com
verify return:1
CONNECTED(00000003)
OCSP response: 
======================================
OCSP Response Data:
    OCSP Response Status: successful (0x0)
    Response Type: Basic OCSP Response
    Version: 1 (0x0)
    Responder Id: 3DD350A5D6A0ADEEF34A600A65D321D4F8F8D60F
    Produced At: Jul 14 23:05:43 2018 GMT
    Responses:
DONE

■OCSPの確認自体は出来るが、必要なファイルはすべてローカルに準備しているので、
 OCSPとしての意味は持たない。

$ openssl ocsp -noverify -no_nonce -issuer Intermediate.pem -cert google.pem -url http://ocsp.pki.goog/
google.pem: good
	This Update: Jul 16 07:43:57 2018 GMT
	Next Update: Jul 23 07:43:57 2018 GMT

■Googleの証明書から直接、CN、有効期限およびシリアル番号を確認

$ openssl s_client -connect www.google.com:443 </dev/null 2>/dev/null | openssl x509 -noout -subject
subject=C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com

$ openssl s_client -connect www.google.com:443 </dev/null 2>/dev/null | openssl x509 -noout --dates
notBefore=Jun 19 11:40:49 2018 GMT
notAfter=Aug 28 11:32:00 2018 GMT

$ openssl s_client -connect www.google.com:443 </dev/null 2>/dev/null | openssl x509 -noout -serial
serial=6175535D87BF94B6

■Googleの証明書を取得、CN、有効期限およびシリアル番号を確認

$ openssl s_client -connect www.google.com:443 </dev/null 2>/dev/null | \
    awk 'BEGIN{f=1}{if($1=="-----BEGIN")f=0}{if(f==0)print $0;if($1=="-----END")f=1}' > google.pem

$ openssl x509 -noout -subject -in google.pem
subject=C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com

$ openssl x509 -noout -dates -in google.pem 
notBefore=Jun 19 11:40:49 2018 GMT
notAfter=Aug 28 11:32:00 2018 GMT

$ date --date "Jun 19 11:40:49 2018 GMT"
2018619日 火曜日 20:40:49 JST
$ date --date "Aug 28 11:32:00 2018 GMT"
2018828日 火曜日 20:32:00 JST

$ openssl x509 -noout -text -in google.pem | grep Serial
        Serial Number: 7022610855128700086 (0x6175535d87bf94b6)

$ echo "6175535d87bf94b6" | tr '[a-z]' '[A-Z]'
6175535D87BF94B6

■中間証明書を取得、CN、有効期限およびシリアル番号を確認

$ openssl s_client -connect www.google.com:443 -showcerts < /dev/null 2>/dev/null | \
    awk 'BEGIN{f=2}{if($1=="-----BEGIN")f-=1}{if(f==0)print $0;if($1=="-----END")f=1}' > Intermediate.pem

$ openssl x509 -noout -subject -in Intermediate.pem
subject=C = US, O = Google Trust Services, CN = Google Internet Authority G3

$ openssl x509 -noout -dates -in Intermediate.pem 
notBefore=Jun 15 00:00:42 2017 GMT
notAfter=Dec 15 00:00:42 2021 GMT

$ openssl x509 -noout -text -in Intermediate.pem | grep -A 1 Serial
        Serial Number:
            01:e3:a9:30:1c:fc:72:06:38:3f:9a:53:1d

■組み込みのルート証明書から、CN、有効期限およびシリアル番号を確認

$ openssl x509 -noout -subject -in /etc/ssl/certs/GlobalSign_Root_CA_-_R2.pem 
subject=OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign

$ openssl x509 -noout -dates -in /etc/ssl/certs/GlobalSign_Root_CA_-_R2.pem 
notBefore=Dec 15 08:00:00 2006 GMT
notAfter=Dec 15 08:00:00 2021 GMT

$ openssl x509 -noout -text -in /etc/ssl/certs/GlobalSign_Root_CA_-_R2.pem | grep -A 1 Serial
        Serial Number:
            04:00:00:00:00:01:0f:86:26:e6:0d

■組み込みのルート証明書をCApathとして加える。
 CAfileの順序は関係ないが、分かりやすいようにdepthが2,1,0の順になるように並べて検証する。

$ openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ -CAfile Intermediate.pem -CAfile google.pem < /dev/null | \
    grep Verify
depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
verify return:1
DONE
    Verify return code: 0 (ok)

■CRLファイルをダウンロード、シリアル番号で比較。
 失効リストに無い有効な証明書であることが分かる。

$ wget http://crl.pki.goog/GTSGIAG3.crl
--2018-07-16 16:31:50--  http://crl.pki.goog/GTSGIAG3.crl
172.31.31.93:8080 に接続しています... 接続しました。
Proxy による接続要求を送信しました、応答を待っています... 200 OK
長さ: 特定できません [application/pkix-crl]
'GTSGIAG3.crl' に保存中

GTSGIAG3.crl                          [ <=>                ]     635  --.-KB/s    in 0s      

2018-07-16 16:31:50 (24.1 MB/s) - 'GTSGIAG3.crl' へ保存終了 [635]

$ openssl crl -inform DER -in GTSGIAG3.crl -out GTSGIAG3.pem

$ openssl crl -inform DER -in GTSGIAG3.crl -text | grep 6175535D87BF94B6
$ openssl crl -inform DER -in GTSGIAG3.crl -text | grep "^Rev\|Serial"
Revoked Certificates:
    Serial Number: 3BC1FF92F32FC6DF
    Serial Number: 1CAF5665073F79C3
    Serial Number: 4851FF0CF0F1CD19
    Serial Number: 67168E04F45AAF5B