]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/certgen/main.go
Use faster modern EdDSA-based certificates
[tofuproxy.git] / cmd / certgen / main.go
1 /*
2 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
3              manager, WARC/geminispace browser
4 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package main
20
21 import (
22         "crypto/ed25519"
23         "crypto/rand"
24         "crypto/x509"
25         "crypto/x509/pkix"
26         "encoding/pem"
27         "flag"
28         "io"
29         "log"
30         "math/big"
31         "os"
32         "time"
33 )
34
35 func main() {
36         cn := flag.String("cn", "tofuproxy.localhost", "CommonName")
37         flag.Parse()
38         log.SetFlags(log.Lshortfile)
39
40         pub, prv, err := ed25519.GenerateKey(rand.Reader)
41         if err != nil {
42                 log.Fatalln(err)
43         }
44         notBefore := time.Now()
45         notAfter := notBefore.Add(365 * 24 * time.Hour)
46
47         serialRaw := make([]byte, 16)
48         if _, err = io.ReadFull(rand.Reader, serialRaw); err != nil {
49                 log.Fatalln(err)
50         }
51         serial := big.NewInt(0)
52         serial = serial.SetBytes(serialRaw)
53
54         template := x509.Certificate{
55                 SerialNumber:          serial,
56                 Subject:               pkix.Name{CommonName: *cn},
57                 DNSNames:              []string{*cn},
58                 NotBefore:             notBefore,
59                 NotAfter:              notAfter,
60                 BasicConstraintsValid: true,
61                 IsCA:                  true,
62         }
63         certRaw, err := x509.CreateCertificate(
64                 rand.Reader, &template, &template, pub, prv,
65         )
66         if err != nil {
67                 log.Fatalln(err)
68         }
69         if _, err = x509.ParseCertificate(certRaw); err != nil {
70                 log.Fatalln(err)
71         }
72         pkcs8, err := x509.MarshalPKCS8PrivateKey(prv)
73         if err != nil {
74                 log.Fatalln(err)
75         }
76
77         err = pem.Encode(os.Stdout, &pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8})
78         if err != nil {
79                 log.Fatalln(err)
80         }
81         err = pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: certRaw})
82         if err != nil {
83                 log.Fatalln(err)
84         }
85 }