blob: acc89f953f67465b488ed7d5a9b6d0a15c0a48ef [file] [log] [blame]
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +02001package main
2
3import (
Serge Bazanski3a6d67e2023-03-31 22:36:27 +00004 "crypto/ed25519"
5 "crypto/rand"
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +02006 "crypto/tls"
Serge Bazanski3a6d67e2023-03-31 22:36:27 +00007 "crypto/x509"
8 "crypto/x509/pkix"
9 "encoding/pem"
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020010 "fmt"
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000011 "math/big"
12 "net"
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020013 "time"
14
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020015 "github.com/golang/glog"
16 "google.golang.org/grpc"
17 "google.golang.org/grpc/credentials"
18)
19
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000020func serializeCert(der []byte) []byte {
21 return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
22}
23
24func serializeKey(priv ed25519.PrivateKey) []byte {
25 pkcs8, err := x509.MarshalPKCS8PrivateKey(priv)
26 if err != nil {
27 return nil
28 }
29
30 block := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8})
31 return block
32}
33
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020034func (p *prodvider) selfCreds() grpc.ServerOption {
35 glog.Infof("Bootstrapping certificate for self (%q)...", flagProdviderCN)
36
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020037 // Create a cert
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000038 keyRaw, certRaw, err := p.makeSelfCertificate()
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020039 if err != nil {
40 glog.Exitf("Could not sign certificate for self: %v", err)
41 }
42
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000043 serverCert, err := tls.X509KeyPair(serializeCert(certRaw), serializeKey(keyRaw))
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020044 if err != nil {
45 glog.Exitf("Could not use gRPC certificate: %v", err)
46 }
47
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000048 serverCert.Certificate = append(serverCert.Certificate, p.intermediateCACert.Raw)
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020049
50 return grpc.Creds(credentials.NewTLS(&tls.Config{
51 Certificates: []tls.Certificate{serverCert},
52 }))
53}
54
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000055func (p *prodvider) makeSelfCertificate() (ed25519.PrivateKey, []byte, error) {
56 serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 127)
57 serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
58 if err != nil {
59 return nil, nil, err
60 }
61 template := &x509.Certificate{
62 Subject: pkix.Name{
63 CommonName: flagProdviderCN,
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020064 },
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000065 NotBefore: time.Now(),
66 NotAfter: time.Now().Add(30 * 24 * time.Hour),
67 KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
68 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
69 SerialNumber: serialNumber,
70 DNSNames: []string{flagProdviderCN},
71 IPAddresses: []net.IP{
72 {127, 0, 0, 1},
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020073 },
74 }
75
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000076 pkey, skey, err := ed25519.GenerateKey(rand.Reader)
77 if err != nil {
78 return nil, nil, err
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020079 }
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000080 bytes, err := x509.CreateCertificate(rand.Reader, template, p.intermediateCACert, pkey, p.intermediateCAKey)
81 if err != nil {
82 return nil, nil, err
83 }
84 return skey, bytes, nil
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020085}
86
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000087func (p *prodvider) makeKubernetesCertificate(username, o string, notAfter time.Time) (ed25519.PrivateKey, []byte, error) {
88 serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 127)
89 serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
90 if err != nil {
91 return nil, nil, err
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020092 }
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000093 template := &x509.Certificate{
94 Subject: pkix.Name{
95 Organization: []string{o},
96 OrganizationalUnit: []string{fmt.Sprintf("Prodvider Kubernetes Cert for %s/%s", username, o)},
97 CommonName: username,
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020098 },
Serge Bazanski3a6d67e2023-03-31 22:36:27 +000099 NotBefore: time.Now(),
100 NotAfter: notAfter,
101 KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
102 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
103 DNSNames: []string{
104 username,
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200105 },
Serge Bazanski3a6d67e2023-03-31 22:36:27 +0000106 SerialNumber: serialNumber,
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200107 }
108
Serge Bazanski3a6d67e2023-03-31 22:36:27 +0000109 pkey, skey, err := ed25519.GenerateKey(rand.Reader)
110 if err != nil {
111 return nil, nil, err
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200112 }
Serge Bazanski3a6d67e2023-03-31 22:36:27 +0000113 bytes, err := x509.CreateCertificate(rand.Reader, template, p.intermediateCACert, pkey, p.intermediateCAKey)
114 if err != nil {
115 return nil, nil, err
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200116 }
Serge Bazanski3a6d67e2023-03-31 22:36:27 +0000117 return skey, bytes, nil
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200118}