Skip to content

Instantly share code, notes, and snippets.

@rekby
Created August 7, 2015 13:06
Show Gist options
  • Save rekby/348b0e2faac09ccb6477 to your computer and use it in GitHub Desktop.
Save rekby/348b0e2faac09ccb6477 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/x509"
"crypto/tls"
"fmt"
)
const rootCert = `-----BEGIN CERTIFICATE-----
MIIEMzCCAxugAwIBAgIBATANBgkqhkiG9w0BAQUFADA5MQswCQYDVQQGEwJSVTEM
MAoGA1UECBMDdG1wMQwwCgYDVQQKEwN0bXAxDjAMBgNVBAMTBXRtcENBMCAXDTE1
MDgwNzExMzMwMFoYDzIzODAwODA3MTEzMzAwWjA5MQswCQYDVQQGEwJSVTEMMAoG
A1UECBMDdG1wMQwwCgYDVQQKEwN0bXAxDjAMBgNVBAMTBXRtcENBMIIBIjANBgkq
hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuOiU93evlvTLEoQSa2jxaYDGw7ZrRDu5
OlUSwIrz6yQJxWTdvtbhsj2gKOfE9umNQTZrW5l+T+4FwXwuvKOaiM+RP2vqH1Ly
TKsAagpU/yv27k3DPP/vVCuoRs8lEDVSedcw+WAOEuIZEdf9o1VHT+KvFTSDp1As
pmwNEJ+oSIqI4LFuFWC3yDakU3tzmo/e6l1thnQvmBNhhZCClzRkKyEbM41QsZOQ
ZrEbD1LPjij9tFqgDn0lmwl/Pn4MGQNcDhMB+SBpEnDVR5RH8eBKDyLmaeFiuDru
d/IEOXPmet7ABuTiswVNbKWsOf8U/p585pTqSkgHEmOcoVFZEqMrbwIDAQABo4IB
QjCCAT4wDAYDVR0TBAUwAwEB/zAMBgNVHQ8EBQMDB/+AMIHsBgNVHSUEgeQwgeEG
CCsGAQUFBwMBBggrBgEFBQcDAgYIKwYBBQUHAwMGCCsGAQUFBwMEBggrBgEFBQcD
CAYKKwYBBAGCNwIBFQYKKwYBBAGCNwIBFgYKKwYBBAGCNwoDAQYKKwYBBAGCNwoD
AwYKKwYBBAGCNwoDBAYJYI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oZIhvcN
AQEFBQADggEBADiLfncfLicC1APdPvjnrK9e5qRpiRO8ZESf+Rryix9nA6uGTSuO
b3ayUZ/PdHTB/4/rpYDNmCjp9uLl2yO5JipbqwiO43sjo8k0JwKPsNVKvCa0tEcD
y1O/1vmjI5dYZmKsQ5mWHZbAtmQ+WThk85PIq4iEmhxT83BHPJvRnix+1JmjZ9Jl
//j0pb6ayMbk+hsjob1U+5NzbqTexoLLnpqBpCCprnlNl5F9VNL6682GdbDZQwAa
hMIKB2ykSOuFDxlhRr0TpCfgjTVIHK5IzusHjsAm+i/9QbK6HPbmu2batiniwUOO
++kFXRzBvOR2Gc0/tfRFfv03tDCjiRQC96Y=
-----END CERTIFICATE-----`
func main(){
roots := x509.NewCertPool()
if !roots.AppendCertsFromPEM([]byte(rootCert)){
panic("Can't parse root cert")
}
if !roots.AppendCertsFromPEM([]byte(serverCert)){
panic("Can't parse server cert")
}
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
panic(err)
}
conn, err := tls.Dial("tcp", "localhost:1234", &tls.Config{
RootCAs:roots,
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
})
if err != nil {
panic(err)
}
conn.Write([]byte{2})
fmt.Println(conn.ConnectionState().PeerCertificates[0].Subject.CommonName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment