demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
tls.go · 2.7 KB · 94 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ecdsa" |
| 5 | "crypto/elliptic" |
| 6 | "crypto/rand" |
| 7 | "crypto/sha256" |
| 8 | "crypto/tls" |
| 9 | "crypto/x509" |
| 10 | "crypto/x509/pkix" |
| 11 | "encoding/hex" |
| 12 | "encoding/pem" |
| 13 | "math/big" |
| 14 | "net" |
| 15 | "os" |
| 16 | "path/filepath" |
| 17 | "time" |
| 18 | ) |
| 19 | |
| 20 | // selfSignedCert loads, or on first run creates, a self-signed certificate in |
| 21 | // dir. It is persisted so its fingerprint stays the same across restarts: |
| 22 | // clients pin that fingerprint instead of trusting a certificate authority, |
| 23 | // which gives real TLS without owning a domain. |
| 24 | func selfSignedCert(dir string, names []string) (certFile, keyFile, fingerprint string, err error) { |
| 25 | certFile = filepath.Join(dir, "cert.pem") |
| 26 | keyFile = filepath.Join(dir, "key.pem") |
| 27 | if _, statErr := os.Stat(certFile); statErr != nil { |
| 28 | if err = os.MkdirAll(dir, 0o700); err != nil { |
| 29 | return |
| 30 | } |
| 31 | if err = writeSelfSigned(certFile, keyFile, names); err != nil { |
| 32 | return |
| 33 | } |
| 34 | } |
| 35 | pair, err := tls.LoadX509KeyPair(certFile, keyFile) |
| 36 | if err != nil { |
| 37 | return |
| 38 | } |
| 39 | sum := sha256.Sum256(pair.Certificate[0]) |
| 40 | return certFile, keyFile, hex.EncodeToString(sum[:]), nil |
| 41 | } |
| 42 | |
| 43 | func writeSelfSigned(certFile, keyFile string, names []string) error { |
| 44 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | tmpl := x509.Certificate{ |
| 53 | SerialNumber: serial, |
| 54 | Subject: pkix.Name{CommonName: "nc-rendezvous"}, |
| 55 | NotBefore: time.Now().Add(-time.Hour), |
| 56 | NotAfter: time.Now().AddDate(10, 0, 0), |
| 57 | KeyUsage: x509.KeyUsageDigitalSignature, |
| 58 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 59 | BasicConstraintsValid: true, |
| 60 | } |
| 61 | seen := map[string]bool{} |
| 62 | for _, n := range names { |
| 63 | if n == "" || seen[n] { |
| 64 | continue |
| 65 | } |
| 66 | seen[n] = true |
| 67 | if ip := net.ParseIP(n); ip != nil { |
| 68 | tmpl.IPAddresses = append(tmpl.IPAddresses, ip) |
| 69 | } else { |
| 70 | tmpl.DNSNames = append(tmpl.DNSNames, n) |
| 71 | } |
| 72 | } |
| 73 | der, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &key.PublicKey, key) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | keyDER, err := x509.MarshalECPrivateKey(key) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if err := os.WriteFile(certFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o644); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | return os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}), 0o600) |
| 85 | } |
| 86 | |
| 87 | // defaultStateDir is where the rendezvous keeps its cert and ACME cache. |
| 88 | // Under systemd running as root there may be no $HOME, hence the fallback. |
| 89 | func defaultStateDir() string { |
| 90 | if d, err := os.UserHomeDir(); err == nil && d != "" && d != "/" { |
| 91 | return filepath.Join(d, ".local", "state", "nc-rendezvous") |
| 92 | } |
| 93 | return "/var/lib/nc-rendezvous" |
| 94 | } |