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