-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
58 lines (53 loc) · 1.9 KB
/
templates.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
package tinyca
import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math"
"math/big"
"github.com/RealImage/bifrost"
"github.com/google/uuid"
)
// Gauntlet is the signature for a function that validates a certificate request.
// If the second return value is non-nil, then the certificate request is denied.
// If the first return value is nil, the default template TLSClientCertTemplate will be used.
// The template will be used to issue a client certificate.
// Consult the x509 package for the full list of fields that can be set.
// tinyca will overwrite the following template fields:
// - NotBefore
// - NotAfter
// - SignatureAlgorithm
// - Issuer
// - Subject.Organization
// - Subject.CommonName
// - BasicConstraintsValid
//
// If SerialNumber is nil, a random value will be generated.
type Gauntlet func(csr *bifrost.CertificateRequest) (tmpl *x509.Certificate, err error)
// TLSClientCertTemplate returns a new x509.Certificate template for a client certificate.
func TLSClientCertTemplate() *x509.Certificate {
return &x509.Certificate{
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
}
}
// CACertTemplate returns a new x509.Certificate template for a CA certificate.
func CACertTemplate(ns, id uuid.UUID) (*x509.Certificate, error) {
serialNumber, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
if err != nil {
return nil, fmt.Errorf("bifrost: unexpected error generating certificate serial: %w", err)
}
return &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{ns.String()},
CommonName: id.String(),
},
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: true,
}, nil
}