-
Notifications
You must be signed in to change notification settings - Fork 20
/
certgen.go
304 lines (270 loc) · 8.97 KB
/
certgen.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Package certgen id set of utilities used to generate ssh certificates
*/
package certgen
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"math/big"
"os/exec"
"time"
"golang.org/x/crypto/ssh"
)
// GetUserPubKeyFromSSSD user authorized keys content based on the running sssd configuration
func GetUserPubKeyFromSSSD(username string) (string, error) {
cmd := exec.Command("/usr/bin/sss_ssh_authorizedkeys", username)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func goCertToFileString(c ssh.Certificate, username string) (string, error) {
certBytes := c.Marshal()
encoded := base64.StdEncoding.EncodeToString(certBytes)
fileComment := "/tmp/" + username + "-cert.pub"
return "ssh-rsa-cert-v01@openssh.com " + encoded + " " + fileComment, nil
}
// gen_user_cert a username and key, returns a short lived cert for that user
func GenSSHCertFileString(username string, userPubKey string, signer ssh.Signer, host_identity string, duration time.Duration) (string, []byte, error) {
userKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(userPubKey))
if err != nil {
return "", nil, err
}
keyIdentity := host_identity + "_" + username
currentEpoch := uint64(time.Now().Unix())
expireEpoch := currentEpoch + uint64(duration.Seconds())
// The values of the permissions are taken from the default values used
// by ssh-keygen
cert := ssh.Certificate{
Key: userKey,
CertType: ssh.UserCert,
SignatureKey: signer.PublicKey(),
ValidPrincipals: []string{username},
KeyId: keyIdentity,
ValidAfter: currentEpoch,
ValidBefore: expireEpoch,
Permissions: ssh.Permissions{Extensions: map[string]string{
"permit-X11-forwarding": "",
"permit-agent-forwarding": "",
"permit-port-forwarding": "",
"permit-pty": "",
"permit-user-rc": ""}}}
err = cert.SignCert(bytes.NewReader(cert.Marshal()), signer)
if err != nil {
return "", nil, err
}
certString, err := goCertToFileString(cert, username)
if err != nil {
return "", nil, err
}
return certString, cert.Marshal(), nil
}
func GenSSHCertFileStringFromSSSDPublicKey(userName string, signer ssh.Signer, hostIdentity string, duration time.Duration) (string, []byte, error) {
userPubKey, err := GetUserPubKeyFromSSSD(userName)
if err != nil {
return "", nil, err
}
cert, certBytes, err := GenSSHCertFileString(userName, userPubKey, signer, hostIdentity, duration)
if err != nil {
return "", nil, err
}
return cert, certBytes, err
}
/// X509 section
func getPubKeyFromPem(pubkey string) (pub interface{}, err error) {
block, rest := pem.Decode([]byte(pubkey))
if block == nil || block.Type != "PUBLIC KEY" {
err := errors.New(fmt.Sprintf("Cannot decode user public Key '%s' rest='%s'", pubkey, string(rest)))
if block != nil {
err = errors.New(fmt.Sprintf("public key bad type %s", block.Type))
}
return nil, err
}
return x509.ParsePKIXPublicKey(block.Bytes)
}
func GetSignerFromPEMBytes(privateKey []byte) (crypto.Signer, error) {
block, _ := pem.Decode(privateKey)
if block == nil {
err := errors.New("Cannot decode Private Key")
return nil, err
}
switch block.Type {
case "RSA PRIVATE KEY":
return x509.ParsePKCS1PrivateKey(block.Bytes)
case "EC PRIVATE KEY":
return x509.ParseECPrivateKey(block.Bytes)
default:
err := errors.New("Cannot process that key")
return nil, err
}
}
//copied from https://golang.org/src/crypto/tls/generate_cert.go
func publicKey(priv interface{}) interface{} {
switch k := priv.(type) {
case *rsa.PrivateKey:
return &k.PublicKey
//case *ecdsa.PrivateKey:
// return &k.PublicKey
default:
return nil
}
}
/*
func derBytesCertToCertAndPem(derBytes []byte) (*x509.Certificate, string, error) {
cert, err := x509.ParseCertificate(derBytes)
if err != nil {
return nil, "", err
}
pemCert := string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}))
return cert, pemCert, nil
}
*/
// return both an internal representation an the pem representation of the string
// As long as the issuer value matches THEN the serial number can be different every time
func GenSelfSignedCACert(commonName string, organization string, caPriv crypto.Signer) ([]byte, error) {
//// Now do the actual work...
notBefore := time.Now()
notAfter := notBefore.Add(24 * 365 * 8 * time.Hour)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, err
}
sum := sha256.Sum256([]byte(commonName))
signedCN, err := caPriv.Sign(rand.Reader, sum[:], crypto.SHA256)
if err != nil {
return nil, err
}
sigSum := sha256.Sum256(signedCN)
sig := base64.StdEncoding.EncodeToString(sigSum[:])
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: commonName,
Organization: []string{organization},
SerialNumber: sig,
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
//ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
return x509.CreateCertificate(rand.Reader, &template, &template, publicKey(caPriv), caPriv)
}
// From RFC 4120 section 5.2.2 (https://tools.ietf.org/html/rfc4120)
type KerberosPrincipal struct {
Len int `asn1:"explicit,tag:0"`
Principal []string `asn1:"explicit,tag:1"`
}
// From RFC 4556 section 3.2.2 (https://tools.ietf.org/html/rfc4556.html)
type KRB5PrincipalName struct {
Realm string `asn1:"explicit,tag:0"`
Principal KerberosPrincipal `asn1:"explicit,tag:1"`
}
type PKInitSANAnotherName struct {
Id asn1.ObjectIdentifier
Value KRB5PrincipalName `asn1:"explicit,tag:0"`
}
// Since currently asn1 cannot mashal into GeneralString (https://github.com/golang/go/issues/18832)
// We make this hack since we know the positions of the items we want to change
func changePrintableStringToGeneralString(kerberosRealm string, inString []byte) []byte {
position := 16
inString[position] = 27
position = position + 1 + len(kerberosRealm) + 14
inString[position] = 27
return inString
}
func genSANExtension(userName string, kerberosRealm *string) (*pkix.Extension, error) {
if kerberosRealm == nil {
return nil, nil
}
krbRealm := *kerberosRealm
//1.3.6.1.5.2.2
krbSanAnotherName := PKInitSANAnotherName{
Id: []int{1, 3, 6, 1, 5, 2, 2},
Value: KRB5PrincipalName{
Realm: krbRealm,
Principal: KerberosPrincipal{Len: 1, Principal: []string{userName}},
},
}
krbSanAnotherNameDer, err := asn1.Marshal(krbSanAnotherName)
if err != nil {
return nil, err
}
//fmt.Printf("ext: %+x\n", krbSanAnotherNameDer)
krbSanAnotherNameDer = changePrintableStringToGeneralString(krbRealm, krbSanAnotherNameDer)
krbSanAnotherNameDer[0] = 0xA0
//fmt.Printf("ext: %+x\n", krbSanAnotherNameDer)
// inspired by marshalSANs in x509.go
var rawValues []asn1.RawValue
rawValues = append(rawValues, asn1.RawValue{FullBytes: krbSanAnotherNameDer})
rawSan, err := asn1.Marshal(rawValues)
if err != nil {
return nil, err
}
sanExtension := pkix.Extension{
Id: []int{2, 5, 29, 17},
Value: rawSan,
}
return &sanExtension, nil
}
// returns an x509 cert that has the username in the common name,
// optionally if a kerberos Realm is present it will also add a kerberos
// SAN exention for pkinit
func GenUserX509Cert(userName string, userPub interface{},
caCert *x509.Certificate, caPriv crypto.Signer,
kerberosRealm *string, duration time.Duration,
organizations *[]string) ([]byte, error) {
//// Now do the actual work...
notBefore := time.Now()
notAfter := notBefore.Add(duration)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, err
}
sanExtension, err := genSANExtension(userName, kerberosRealm)
if err != nil {
return nil, err
}
// need to add the extended key usage... that is special for kerberos
//and also the client key usage
kerberosClientExtKeyUsage := []int{1, 3, 6, 1, 5, 2, 3, 4}
subject := pkix.Name{
CommonName: userName,
Organization: []string{"Keymaster"},
}
if organizations != nil {
subject.Organization = *organizations
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: subject,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
UnknownExtKeyUsage: []asn1.ObjectIdentifier{kerberosClientExtKeyUsage},
BasicConstraintsValid: true,
IsCA: false,
}
if sanExtension != nil {
template.ExtraExtensions = []pkix.Extension{*sanExtension}
}
return x509.CreateCertificate(rand.Reader, &template, caCert, userPub, caPriv)
}