-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcerts.go
More file actions
155 lines (126 loc) · 4.69 KB
/
certs.go
File metadata and controls
155 lines (126 loc) · 4.69 KB
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
package scamjamtls
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"time"
"fmt"
"github.com/rs/zerolog"
)
// KeyPairWithPin returns PEM encoded Certificate and Key along with an SKPI
// fingerprint of the public key.
func GetRootCa(logger *zerolog.Logger, certOrgName string) (caCertAsPem []byte, caPrivateKeyAsPem []byte, err error) {
caTemplate := x509.Certificate{
SerialNumber: big.NewInt(100),
Subject: pkix.Name{
CommonName: "localhost",
Organization: []string{certOrgName},
Country: []string{"US"}},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(2, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
caPrivateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
logger.Error().Msg("Error generating RSA private key pair for CA!" + err.Error())
return nil, nil, err
}
caPrivateKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivateKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivateKey),
})
caBytes, err := x509.CreateCertificate(rand.Reader, &caTemplate, &caTemplate, &caPrivateKey.PublicKey, caPrivateKey)
if err != nil {
logger.Error().Msg("Error creating CA x.509!" + err.Error())
return nil, nil, err
}
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
return caPEM.Bytes(), caPrivateKeyPEM.Bytes(), nil
}
func GetLeaf(logger *zerolog.Logger, certOrgName string, caCertAsBytes []byte, caPrivateKeyAsBytes []byte,) (leafCertAsPem []byte, leafPrivateKeyAsPem []byte, err error) {
caCertPEM, restOfCaCertPEM := pem.Decode(caCertAsBytes)
switch {
case caCertPEM == nil:
err = fmt.Errorf("Failed to decode root CA cert from bytes! (PEM was empty)")
logger.Error().Err(err)
return nil, nil, err
case caCertPEM.Type != "CERTIFICATE":
err = fmt.Errorf("Failed to decode root CA cert from bytes! (PEM is not a certificate)")
logger.Error().Err(err)
return nil, nil, err
case restOfCaCertPEM != nil:
logger.Warn().Msg("Found extra data after root CA cert: " + string(restOfCaCertPEM[:]) + " Will try to continue.")
}
caPrivateKeyPEM, restOfCAPrivateKeyPEM := pem.Decode(caPrivateKeyAsBytes)
switch {
case caPrivateKeyPEM == nil:
err = fmt.Errorf("Failed to decode root CA private key from bytes! (PEM was empty)")
logger.Error().Err(err)
return nil, nil, err
case caPrivateKeyPEM.Type != "RSA PRIVATE KEY":
err = fmt.Errorf("Failed to decode root CA private key from bytes! (PEM is not a private key)")
logger.Error().Err(err)
return nil, nil, err
case restOfCAPrivateKeyPEM != nil:
logger.Warn().Msg("Found extra data after root CA private key: " + string(restOfCAPrivateKeyPEM[:]) + " Will try to continue.")
}
caCert, err := x509.ParseCertificate(caCertPEM.Bytes)
if err != nil {
logger.Error().Err(err).Msg("Failed to decode root CA cert from PEM!")
return nil, nil, err
}
caPrivateKey, err := x509.ParsePKCS1PrivateKey(caPrivateKeyPEM.Bytes)
if err != nil {
logger.Error().Err(err).Msg("Failed to decode root CA private key from PEM!")
return nil, nil, err
}
leafCertDetails := &x509.Certificate{
SerialNumber: big.NewInt(200),
Subject: pkix.Name{
CommonName: "localhost",
Organization: []string{certOrgName},
Country: []string{"US"},
},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
DNSNames: []string{"localhost"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 6},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
leafCertPrivateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
logger.Error().Msg("Error generating RSA private key pair for server cert!" + err.Error())
return nil, nil, err
}
leafCertBytes, err := x509.CreateCertificate(rand.Reader, leafCertDetails, caCert, &leafCertPrivateKey.PublicKey, caPrivateKey)
if err != nil {
logger.Error().Msg("Error creating server x.509!" + err.Error())
return nil, nil, err
}
leafCertPEM := new(bytes.Buffer)
pem.Encode(leafCertPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: leafCertBytes,
})
leafCertPrivateKeyPEM := new(bytes.Buffer)
pem.Encode(leafCertPrivateKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(leafCertPrivateKey),
})
return leafCertPEM.Bytes(), leafCertPrivateKeyPEM.Bytes(), nil
}