| 1 |
package x509util |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"crypto/x509/pkix" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/pkg/errors" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
var DefaultRootCertValidity = time.Hour * 24 * 365 * 10 |
| 13 |
|
| 14 |
|
| 15 |
type Root struct { |
| 16 |
base |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func (r *Root) DefaultDuration() time.Duration { |
| 21 |
return DefaultRootCertValidity |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func NewRootProfile(name string, withOps ...WithOption) (Profile, error) { |
| 26 |
crt := defaultRootTemplate(name) |
| 27 |
return NewRootProfileWithTemplate(crt, withOps...) |
| 28 |
} |
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
} |