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