|
| 1 | +package main |
| 2 | + |
| 3 | +// A real TLS handshake over an in-memory pipe. The stub crypto/tls has a |
| 4 | +// handshake that does nothing, so it cannot pass this. |
| 5 | + |
| 6 | +import ( |
| 7 | + "crypto/ecdsa" |
| 8 | + "crypto/elliptic" |
| 9 | + "crypto/rand" |
| 10 | + "crypto/tls" |
| 11 | + "crypto/x509" |
| 12 | + "crypto/x509/pkix" |
| 13 | + "io" |
| 14 | + "math/big" |
| 15 | + "net" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + cert, pool := selfSigned() |
| 21 | + |
| 22 | + // A client that trusts the certificate completes the handshake and |
| 23 | + // exchanges data. |
| 24 | + client, server := net.Pipe() |
| 25 | + go serve(server, cert) |
| 26 | + conn := tls.Client(client, &tls.Config{RootCAs: pool, ServerName: "tinygo.test"}) |
| 27 | + if err := conn.Handshake(); err != nil { |
| 28 | + println("handshake failed:", err.Error()) |
| 29 | + return |
| 30 | + } |
| 31 | + if v := conn.ConnectionState().Version; v < tls.VersionTLS12 { |
| 32 | + println("negotiated an unexpected version:", v) |
| 33 | + return |
| 34 | + } |
| 35 | + if _, err := conn.Write([]byte("ping")); err != nil { |
| 36 | + println("write failed:", err.Error()) |
| 37 | + return |
| 38 | + } |
| 39 | + buf := make([]byte, 4) |
| 40 | + if _, err := io.ReadFull(conn, buf); err != nil { |
| 41 | + println("read failed:", err.Error()) |
| 42 | + return |
| 43 | + } |
| 44 | + println("got:", string(buf)) |
| 45 | + conn.Close() |
| 46 | + |
| 47 | + // A client that does not trust the certificate must refuse it. |
| 48 | + client, server = net.Pipe() |
| 49 | + go serve(server, cert) |
| 50 | + conn = tls.Client(client, &tls.Config{ServerName: "tinygo.test"}) |
| 51 | + if err := conn.Handshake(); err == nil { |
| 52 | + println("an unknown certificate was accepted") |
| 53 | + return |
| 54 | + } |
| 55 | + conn.Close() |
| 56 | + println("unknown certificate refused") |
| 57 | +} |
| 58 | + |
| 59 | +func serve(conn net.Conn, cert tls.Certificate) { |
| 60 | + server := tls.Server(conn, &tls.Config{Certificates: []tls.Certificate{cert}}) |
| 61 | + if err := server.Handshake(); err != nil { |
| 62 | + conn.Close() |
| 63 | + return |
| 64 | + } |
| 65 | + buf := make([]byte, 4) |
| 66 | + if _, err := io.ReadFull(server, buf); err != nil { |
| 67 | + server.Close() |
| 68 | + return |
| 69 | + } |
| 70 | + server.Write([]byte("pong")) |
| 71 | +} |
| 72 | + |
| 73 | +func selfSigned() (tls.Certificate, *x509.CertPool) { |
| 74 | + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + template := &x509.Certificate{ |
| 79 | + SerialNumber: big.NewInt(1), |
| 80 | + Subject: pkix.Name{CommonName: "tinygo.test"}, |
| 81 | + DNSNames: []string{"tinygo.test"}, |
| 82 | + NotBefore: time.Now().Add(-time.Hour), |
| 83 | + NotAfter: time.Now().Add(time.Hour), |
| 84 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 85 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 86 | + BasicConstraintsValid: true, |
| 87 | + IsCA: true, |
| 88 | + } |
| 89 | + der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + leaf, err := x509.ParseCertificate(der) |
| 94 | + if err != nil { |
| 95 | + panic(err) |
| 96 | + } |
| 97 | + pool := x509.NewCertPool() |
| 98 | + pool.AddCert(leaf) |
| 99 | + return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf}, pool |
| 100 | +} |
0 commit comments