-
Notifications
You must be signed in to change notification settings - Fork 424
/
certs.go
175 lines (151 loc) · 4.73 KB
/
certs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright 2021 by the contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package certs
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"math/big"
"net"
"os"
"time"
"github.com/sirupsen/logrus"
)
type CertificateOptions struct {
CertPath string
KeyPath string
Hostname string
Address string
Lifetime time.Duration
}
func GetOrCreateX509KeyPair(opts CertificateOptions) (*tls.Certificate, error) {
// first try to load the existing keypair
cert, err := LoadX509KeyPair(opts.CertPath, opts.KeyPath)
if err != nil {
return nil, err
}
// if we found it,
if cert != nil {
return cert, nil
}
// generate a self-signed certificate and write out the certificate and private key
certBytes, keyBytes, err := selfSignedCertificate(opts.Address, opts.Hostname, opts.Lifetime)
if err != nil {
return nil, err
}
logrus.WithFields(logrus.Fields{
"certPath": opts.CertPath,
"keyPath": opts.KeyPath,
}).Info("saving new key and certificate")
err = dumpPEM(opts.CertPath, 0666, "CERTIFICATE", certBytes)
if err != nil {
return nil, err
}
err = dumpPEM(opts.KeyPath, 0600, "RSA PRIVATE KEY", keyBytes)
if err != nil {
return nil, err
}
newCert, err := tls.LoadX509KeyPair(opts.CertPath, opts.KeyPath)
return &newCert, err
}
func LoadX509KeyPair(certPath, keyPath string) (*tls.Certificate, error) {
// if either file does not exist, we'll consider that not an error but
// return a nil
if _, err := os.Stat(certPath); os.IsNotExist(err) {
return nil, nil
}
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
return nil, nil
}
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
logrus.WithFields(logrus.Fields{
"certPath": certPath,
"keyPath": keyPath,
}).Info("loaded existing keypair")
return &cert, nil
}
func dumpPEM(filename string, mode os.FileMode, blockType string, bytes []byte) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
defer f.Close()
return pem.Encode(f, &pem.Block{Type: blockType, Bytes: bytes})
}
func selfSignedCertificate(address, hostname string, lifetime time.Duration) ([]byte, []byte, error) {
// generate a new RSA-2048 keypair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
// choose a beginning and end for the cert's lifetime (currently ~infinite)
notBefore := time.Now()
notAfter := notBefore.Add(lifetime)
// choose a random 128 bit serial number
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
logrus.WithField("address", address).Infof("Creating certificate with address")
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: "aws-iam-authenticator",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
addrIP := net.ParseIP(address)
if !addrIP.IsUnspecified() {
template.IPAddresses = append(template.IPAddresses, addrIP)
}
if ip := net.ParseIP(hostname); ip != nil {
// is an IP literal
if !addrIP.Equal(ip) {
template.IPAddresses = append(template.IPAddresses, ip)
}
} else {
// is a hostname (not an IP literal)
template.DNSNames = append(template.DNSNames, hostname)
}
certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
if err != nil {
return nil, nil, err
}
keyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
logrus.WithFields(logrus.Fields{
"certBytes": len(certBytes),
"keyBytes": len(keyBytes),
}).Info("generated a new private key and certificate")
return certBytes, keyBytes, nil
}
// certToPEMBase64 returns the Base64 encoded PEM block for a given DER
// certificate (i.e., it returns "Base64(PEM(asn1))").
func CertToPEMBase64(der []byte) string {
return base64.StdEncoding.EncodeToString(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: der,
}))
}