forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509.go
202 lines (172 loc) · 5.96 KB
/
x509.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2015 The Cockroach Authors.
//
// 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.
//
// Author: Marc Berhault (marc@cockroachlabs.com)
package security
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"time"
"github.com/cockroachdb/cockroach/util"
)
// Utility to generate x509 certificates, both CA and not.
// This is mostly based on http://golang.org/src/crypto/tls/generate_cert.go
// Most fields and settings are hard-coded. TODO(marc): allow customization.
const (
// Make certs valid a day before to handle clock issues, specifically
// boot2docker: https://github.com/boot2docker/boot2docker/issues/69
validFrom = -time.Hour * 24
validFor = time.Hour * 24 * 365
maxPathLength = 1
caCommonName = "Cockroach CA"
)
// generateKeyPair returns a random 'keySize' bit RSA key pair.
func generateKeyPair(keySize int) (crypto.PrivateKey, crypto.PublicKey, error) {
private, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return nil, nil, err
}
public := private.Public()
return private, public, err
}
// privateKeyPEMBlock generates a PEM block from a private key.
func privateKeyPEMBlock(key crypto.PrivateKey) (*pem.Block, error) {
switch k := key.(type) {
case *rsa.PrivateKey:
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}, nil
case *ecdsa.PrivateKey:
bytes, err := x509.MarshalECPrivateKey(k)
if err != nil {
return nil, util.Errorf("error marshalling ECDSA key: %s", err)
}
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: bytes}, nil
default:
return nil, util.Errorf("unknown key type: %v", k)
}
}
// certificatePEMBlock generates a PEM block from a certificate.
func certificatePEMBlock(cert []byte) (*pem.Block, error) {
return &pem.Block{Type: "CERTIFICATE", Bytes: cert}, nil
}
// newTemplate returns a partially-filled template.
// It should be further populated based on whether the cert is for a CA or node.
func newTemplate(commonName string) (*x509.Certificate, error) {
// Generate a random serial number.
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, err
}
notBefore := time.Now().Add(validFrom)
notAfter := notBefore.Add(validFor)
cert := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Cockroach"},
CommonName: commonName,
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
}
return cert, nil
}
// GenerateCA generates a CA certificate and returns the cert bytes as
// well as the private key used to generate the certificate.
func GenerateCA(keySize int) ([]byte, crypto.PrivateKey, error) {
privateKey, publicKey, err := generateKeyPair(keySize)
if err != nil {
return nil, nil, err
}
template, err := newTemplate(caCommonName)
if err != nil {
return nil, nil, err
}
// Set CA-specific fields.
template.BasicConstraintsValid = true
template.IsCA = true
template.MaxPathLen = maxPathLength
template.KeyUsage |= x509.KeyUsageCertSign
template.KeyUsage |= x509.KeyUsageContentCommitment
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, publicKey, privateKey)
if err != nil {
return nil, nil, err
}
return certBytes, privateKey, nil
}
// GenerateServerCert generates a server certificate and returns the cert bytes as
// well as the private key used to generate the certificate.
// Takes in the CA cert and key, the size of the key to generate, and the list
// of hosts/ip addresses this certificate applies to.
func GenerateServerCert(caCert *x509.Certificate, caKey crypto.PrivateKey, keySize int, hosts []string) (
[]byte, crypto.PrivateKey, error) {
privateKey, publicKey, err := generateKeyPair(keySize)
if err != nil {
return nil, nil, err
}
template, err := newTemplate(NodeUser)
if err != nil {
return nil, nil, err
}
// Only server authentication is allowed.
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
if hosts != nil {
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
}
certBytes, err := x509.CreateCertificate(rand.Reader, template, caCert, publicKey, caKey)
if err != nil {
return nil, nil, err
}
return certBytes, privateKey, nil
}
// GenerateClientCert generates a client certificate and returns the cert bytes as
// well as the private key used to generate the certificate.
// The CA cert and private key should be passed in.
// 'user' is the unique username stored in the Subject.CommonName field.
func GenerateClientCert(caCert *x509.Certificate, caKey crypto.PrivateKey, keySize int, name string) (
[]byte, crypto.PrivateKey, error) {
privateKey, publicKey, err := generateKeyPair(keySize)
if err != nil {
return nil, nil, err
}
// TODO(marc): should we add extra checks?
if len(name) == 0 {
return nil, nil, util.Errorf("name cannot be empty")
}
template, err := newTemplate(name)
if err != nil {
return nil, nil, err
}
// Set client-specific fields.
// Client authentication only.
template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
certBytes, err := x509.CreateCertificate(rand.Reader, template, caCert, publicKey, caKey)
if err != nil {
return nil, nil, err
}
return certBytes, privateKey, nil
}