2013/08/12

golang certificate ssl handshake

2013/08/14 追記
Go界のジェフ・ベゾスが勝手にウンコードを添削してくれたよ!! みんな、こっち見てね!

ヒャッハー、証明書好きのオレ様が帰ってきたぜ!!!というわけで遅延証明書よりも在留証明書よりも公開鍵証明書を愛する私が Go を使ってサーバから証明書をゲットするその日がやってきました。Go には crypto/tls という素敵パッケージもあるので何の心配もありません。Config としてサーバ認証を真面目にしない InsecureSkipVerify を true としてあげればどんな IP address, port のペアに対してだって証明書を取ってくることが可能なのです。実行するとこんな:
$ go run get_cert.go www.google.com
i=0
[[{[2 5 4 6] US}] [{[2 5 4 10] Google Inc}] [{[2 5 4 3] Google Internet Authority}]]
2013-07-31 11:40:49 +0000 UTC
2013-10-31 23:59:59 +0000 UTC
[[{[2 5 4 6] US}] [{[2 5 4 10] Google Inc}] [{[2 5 4 7] Mountain View}] [{[2 5 4 8] California}] [{[2 5 4 3] www.google.com}]]
i=1
[[{[2 5 4 6] US}] [{[2 5 4 10] Equifax}] [{[2 5 4 11] Equifax Secure Certificate Authority}]]
2012-12-12 15:58:50 +0000 UTC
2013-12-31 15:58:50 +0000 UTC
[[{[2 5 4 6] US}] [{[2 5 4 10] Google Inc}] [{[2 5 4 3] Google Internet Authority}]]
OpenSSL みたいな pretty な感じに出してくれるのはとりあえず見つからなかったけど頑張れば自分で書けるぜ!!
package main

import (
        "crypto/tls"
        "crypto/x509"
        "fmt"
        "log"
        "os"
)

func main() {
        if len(os.Args) < 1 {
                log.Fatal("You must input a hostname")
        }
        peerCertificates, err := GetCert(os.Args[1])
        if err == nil {
                for i, Cert := range peerCertificates {
                        fmt.Printf("i=%d\n", i)
                        fmt.Println(Cert.Issuer.ToRDNSequence())
                        fmt.Println(Cert.NotBefore)
                        fmt.Println(Cert.NotAfter)
                        fmt.Println(Cert.Subject.ToRDNSequence())
                }
        } else {
                log.Fatal(err)
        }
}

func GetCert(host string) ([]*x509.Certificate, error) {
        config := &tls.Config{InsecureSkipVerify: true}
        conn, err := tls.Dial("tcp", host+":443", config)
        var peerCertificates []*x509.Certificate
        if err == nil {
                connectionState := conn.ConnectionState()
                peerCertificates = connectionState.PeerCertificates
        }
        return peerCertificates, err
}
ポートは決め打ちだぜ!!

0 件のコメント:

コメントを投稿