Debian Codegraph @

crypto/x509util/rootProfile.go

raw symbols SCIP
1 package x509util
2
3 import (
4 "crypto/x509"
5 "crypto/x509/pkix"
6 "time"
7
8 "github.com/pkg/errors"
9 )
10
11 // DefaultRootCertValidity is the default validity of a root certificate in the step PKI.
12 var DefaultRootCertValidity = time.Hour * 24 * 365 * 10
13
14 // Root implements the Profile for a root certificate.
15 type Root struct {
16 base
17 }
18
19 // DefaultDuration returns the default Root Certificate duration.
20 func (r *Root) DefaultDuration() time.Duration {
21 return DefaultRootCertValidity
22 }
23
24 // NewRootProfile returns a new root x509 Certificate profile.
25 func NewRootProfile(name string, withOps ...WithOption) (Profile, error) {
26 crt := defaultRootTemplate(name)
27 return NewRootProfileWithTemplate(crt, withOps...)
28 }
29
30 // NewRootProfileWithTemplate returns a new root x509 Certificate profile.
31 func NewRootProfileWithTemplate(crt *x509.Certificate, withOps ...WithOption) (Profile, error) {
32 p, err := newProfile(&Root{}, crt, crt, nil, withOps...)
33 if err != nil {
34 return nil, errors.WithStack(err)
35 }
36 // self-signed certificate
37 p.SetIssuerPrivateKey(p.SubjectPrivateKey())
38 return p, nil
39 }
40
41 func defaultRootTemplate(cn string) *x509.Certificate {
42 notBefore := time.Now()
43 return &x509.Certificate{
44 IsCA: true,
45 NotBefore: notBefore,
46 NotAfter: notBefore.Add(DefaultRootCertValidity),
47 KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
48 BasicConstraintsValid: true,
49 MaxPathLen: 1,
50 MaxPathLenZero: false,
51 Issuer: pkix.Name{CommonName: cn},
52 Subject: pkix.Name{CommonName: cn},
53 }
54 }