]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/certgen/main.go
Raised copyright years
[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-2022 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/ecdsa"
23         "crypto/elliptic"
24         "crypto/rand"
25         "crypto/x509"
26         "crypto/x509/pkix"
27         "encoding/pem"
28         "flag"
29         "io"
30         "log"
31         "math/big"
32         "os"
33         "time"
34 )
35
36 func main() {
37         cn := flag.String("cn", "tofuproxy.localhost", "CommonName")
38         flag.Parse()
39         log.SetFlags(log.Lshortfile)
40
41         prv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
42         if err != nil {
43                 log.Fatalln(err)
44         }
45         pub := prv.Public()
46         notBefore := time.Now()
47         notAfter := notBefore.Add(365 * 24 * time.Hour)
48
49         serialRaw := make([]byte, 16)
50         if _, err = io.ReadFull(rand.Reader, serialRaw); err != nil {
51                 log.Fatalln(err)
52         }
53         serial := big.NewInt(0)
54         serial = serial.SetBytes(serialRaw)
55
56         template := x509.Certificate{
57                 SerialNumber:          serial,
58                 Subject:               pkix.Name{CommonName: *cn},
59                 DNSNames:              []string{*cn},
60                 NotBefore:             notBefore,
61                 NotAfter:              notAfter,
62                 BasicConstraintsValid: true,
63                 IsCA:                  true,
64         }
65         certRaw, err := x509.CreateCertificate(
66                 rand.Reader, &template, &template, pub, prv,
67         )
68         if err != nil {
69                 log.Fatalln(err)
70         }
71         if _, err = x509.ParseCertificate(certRaw); err != nil {
72                 log.Fatalln(err)
73         }
74         pkcs8, err := x509.MarshalPKCS8PrivateKey(prv)
75         if err != nil {
76                 log.Fatalln(err)
77         }
78
79         err = pem.Encode(os.Stdout, &pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8})
80         if err != nil {
81                 log.Fatalln(err)
82         }
83         err = pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: certRaw})
84         if err != nil {
85                 log.Fatalln(err)
86         }
87 }