/* tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU manager, WARC/geminispace browser Copyright (C) 2021-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package tofuproxy import ( "crypto" "crypto/ed25519" "crypto/rand" "crypto/x509" "crypto/x509/pkix" "log" "math/big" "sync" "time" ) type Keypair struct { cert *x509.Certificate prv crypto.PrivateKey } var ( hostCerts = make(map[string]*Keypair) hostCertsM sync.Mutex Serial *big.Int ) func init() { max := big.NewInt(0) max = max.SetBit(max, 128, 1) var err error Serial, err = rand.Int(rand.Reader, max) if err != nil { panic(err) } } func newKeypair( host string, caCert *x509.Certificate, caPrv crypto.PrivateKey, ) *Keypair { pub, prv, err := ed25519.GenerateKey(rand.Reader) if err != nil { log.Fatalln(err) } notBefore := time.Now() notAfter := notBefore.Add(24 * time.Hour) Serial = Serial.Add(Serial, big.NewInt(1)) template := x509.Certificate{ SerialNumber: Serial, Subject: pkix.Name{CommonName: host}, DNSNames: []string{host}, NotBefore: notBefore, NotAfter: notAfter, } certRaw, err := x509.CreateCertificate( rand.Reader, &template, caCert, pub, caPrv, ) if err != nil { log.Fatalln(err) } cert, err := x509.ParseCertificate(certRaw) if err != nil { log.Fatalln(err) } return &Keypair{cert, prv} }