2020-07-02 01:30:41 +00:00
|
|
|
package x509util
|
|
|
|
|
2020-07-09 18:40:37 +00:00
|
|
|
import "crypto/x509"
|
|
|
|
|
2020-07-08 01:56:05 +00:00
|
|
|
const (
|
2020-07-09 18:40:37 +00:00
|
|
|
UserKey = "User"
|
|
|
|
SubjectKey = "Subject"
|
|
|
|
SANsKey = "SANs"
|
|
|
|
TokenKey = "Token"
|
|
|
|
CertificateRequestKey = "CR"
|
2020-07-08 01:56:05 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 22:53:11 +00:00
|
|
|
// TemplateData is an alias for map[string]interface{}. It represents the data
|
|
|
|
// passed to the templates.
|
|
|
|
type TemplateData map[string]interface{}
|
|
|
|
|
2020-07-09 22:17:32 +00:00
|
|
|
// NewTemplateData creates a new map for templates data.
|
|
|
|
func NewTemplateData() TemplateData {
|
|
|
|
return TemplateData{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTemplateData creates a new TemplateData with the given common name and SANs.
|
|
|
|
func CreateTemplateData(commonName string, sans []string) TemplateData {
|
|
|
|
return TemplateData{
|
|
|
|
SubjectKey: Subject{
|
|
|
|
CommonName: commonName,
|
|
|
|
},
|
|
|
|
SANsKey: CreateSANs(sans),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:53:11 +00:00
|
|
|
func (t TemplateData) Set(key string, v interface{}) {
|
|
|
|
t[key] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TemplateData) SetUserData(v Subject) {
|
|
|
|
t[UserKey] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TemplateData) SetSubject(v Subject) {
|
|
|
|
t[SubjectKey] = v
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:47:26 +00:00
|
|
|
func (t TemplateData) SetCommonName(cn string) {
|
|
|
|
s, _ := t[SubjectKey].(Subject)
|
|
|
|
s.CommonName = cn
|
|
|
|
t[SubjectKey] = s
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:53:11 +00:00
|
|
|
func (t TemplateData) SetSANs(sans []string) {
|
|
|
|
t[SANsKey] = CreateSANs(sans)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TemplateData) SetToken(v interface{}) {
|
|
|
|
t[TokenKey] = v
|
|
|
|
}
|
|
|
|
|
2020-07-09 18:40:37 +00:00
|
|
|
func (t TemplateData) SetCertificateRequest(cr *x509.CertificateRequest) {
|
|
|
|
t[CertificateRequestKey] = newCertificateRequest(cr)
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:47:26 +00:00
|
|
|
// DefaultLeafTemplate is the default templated used to generate a leaf
|
|
|
|
// certificate. The keyUsage "keyEncipherment" is special and it will be only
|
|
|
|
// used for RSA keys.
|
2020-07-02 01:30:41 +00:00
|
|
|
const DefaultLeafTemplate = `{
|
|
|
|
"subject": {{ toJson .Subject }},
|
2020-07-08 01:56:05 +00:00
|
|
|
"sans": {{ toJson .SANs }},
|
2020-07-02 01:30:41 +00:00
|
|
|
"keyUsage": ["keyEncipherment", "digitalSignature"],
|
|
|
|
"extKeyUsage": ["serverAuth", "clientAuth"]
|
|
|
|
}`
|
2020-07-08 23:39:19 +00:00
|
|
|
|
2020-07-13 19:47:26 +00:00
|
|
|
// DefaultIIDLeafTemplate is the template used by default on instance identity
|
|
|
|
// provisioners like AWS, GCP or Azure. By default, those provisioners allow the
|
|
|
|
// SANs provided in the certificate request, but the option `DisableCustomSANs`
|
|
|
|
// can be provided to force only the verified domains, if the option is true
|
|
|
|
// `.SANs` will be set with the verified domains.
|
|
|
|
//
|
|
|
|
// The keyUsage "keyEncipherment" is special and it will be only used for RSA
|
|
|
|
// keys.
|
|
|
|
const DefaultIIDLeafTemplate = `{
|
|
|
|
{{- if .SANs }}
|
2020-07-14 19:42:46 +00:00
|
|
|
"subject": {"commonName": "{{ .CR.Subject.CommonName }}"},
|
2020-07-13 19:47:26 +00:00
|
|
|
"sans": {{ toJson .SANs }},
|
|
|
|
{{- else }}
|
2020-07-14 19:42:46 +00:00
|
|
|
"subject": {{ toJson .CR.Subject }},
|
2020-07-13 19:47:26 +00:00
|
|
|
"dnsNames": {{ toJson .CR.DNSNames }},
|
|
|
|
"emailAddresses": {{ toJson .CR.EmailAddresses }},
|
|
|
|
"ipAddresses": {{ toJson .CR.IPAddresses }},
|
|
|
|
"uris": {{ toJson .CR.URIs }},
|
|
|
|
{{- end }}
|
|
|
|
"keyUsage": ["keyEncipherment", "digitalSignature"],
|
|
|
|
"extKeyUsage": ["serverAuth", "clientAuth"]
|
|
|
|
}`
|
|
|
|
|
|
|
|
// DefaultIntermediateTemplate is a template that can be used to generate an
|
|
|
|
// intermediate certificate.
|
2020-07-08 23:39:19 +00:00
|
|
|
const DefaultIntermediateTemplate = `{
|
|
|
|
"subject": {{ toJson .Subject }},
|
|
|
|
"keyUsage": ["certSign", "crlSign"],
|
|
|
|
"basicConstraints": {
|
|
|
|
"isCA": true,
|
|
|
|
"maxPathLen": 0
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2020-07-13 19:47:26 +00:00
|
|
|
// DefaultRootTemplate is a template that can be used to generate a root
|
|
|
|
// certificate.
|
2020-07-08 23:39:19 +00:00
|
|
|
const DefaultRootTemplate = `{
|
|
|
|
"subject": {{ toJson .Subject }},
|
|
|
|
"issuer": {{ toJson .Subject }},
|
|
|
|
"keyUsage": ["certSign", "crlSign"],
|
|
|
|
"basicConstraints": {
|
|
|
|
"isCA": true,
|
|
|
|
"maxPathLen": 1
|
|
|
|
}
|
|
|
|
}`
|
2020-07-13 21:36:59 +00:00
|
|
|
|
|
|
|
// CertificateRequestTemplate is a template that will sign the given certificate
|
|
|
|
// request.
|
|
|
|
const CertificateRequestTemplate = `{{ toJson .CR }}`
|