2022-04-24 07:25:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-04-19 14:48:32 +00:00
|
|
|
"strings"
|
2022-04-16 20:24:32 +00:00
|
|
|
"time"
|
2022-04-19 14:48:32 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2022-04-16 20:24:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Settings struct {
|
2022-04-23 16:34:51 +00:00
|
|
|
Organization *pkix.Name
|
2022-04-19 14:48:32 +00:00
|
|
|
ValidityYears struct {
|
2022-04-23 16:34:51 +00:00
|
|
|
Root, Intermediary int
|
|
|
|
}
|
2022-04-19 14:48:32 +00:00
|
|
|
URLPatterns struct {
|
2022-04-23 16:34:51 +00:00
|
|
|
Ocsp, CRL, Issuer string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SettingsError struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (se SettingsError) Error() string {
|
|
|
|
return fmt.Sprintf("invalid Settings %s", se.msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Settings) UnmarshalYAML(n *yaml.Node) error {
|
|
|
|
data := struct {
|
|
|
|
Organization struct {
|
|
|
|
Country []string `yaml:"country"`
|
|
|
|
Organization []string `yaml:"organization"`
|
|
|
|
Locality []string `yaml:"locality"`
|
|
|
|
StreetAddress []string `yaml:"street-address"`
|
|
|
|
PostalCode []string `yaml:"postal-code"`
|
|
|
|
} `yaml:"organization"`
|
|
|
|
ValidityYears struct {
|
|
|
|
Root int `yaml:"root"`
|
|
|
|
Intermediary int `yaml:"intermediary"`
|
|
|
|
} `yaml:"validity-years"`
|
|
|
|
URLPatterns struct {
|
|
|
|
Ocsp string `yaml:"ocsp"`
|
|
|
|
CRL string `yaml:"crl"`
|
|
|
|
Issuer string `yaml:"issuer"`
|
|
|
|
} `yaml:"url-patterns"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := n.Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not decode YAML: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Organization.Organization == nil {
|
|
|
|
return SettingsError{"you need to specify 'organization'"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.ValidityYears.Root == 0 || data.ValidityYears.Intermediary == 0 {
|
|
|
|
return SettingsError{"you must specify validity years for 'root' and 'intermediary'"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.ValidityYears.Root < data.ValidityYears.Intermediary {
|
2022-04-24 07:25:04 +00:00
|
|
|
return SettingsError{"validity of root CA certificates must be equal or greater than those if" +
|
|
|
|
" intermediary CA certificates"}
|
2022-04-23 16:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.URLPatterns.Ocsp == "" {
|
|
|
|
return SettingsError{"you must specify an 'ocsp' URL pattern"}
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
if strings.Count(data.URLPatterns.Ocsp, "%s") > 1 {
|
|
|
|
return SettingsError{"url-pattern 'ocsp' must contain zero or one '%s' placeholder"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.URLPatterns.CRL == "" {
|
|
|
|
return SettingsError{"you must specify an 'crl' URL pattern"}
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
if strings.Count(data.URLPatterns.CRL, "%s") != 1 {
|
|
|
|
return SettingsError{"url-pattern 'crl' must contain one '%s' placeholder"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.URLPatterns.Issuer == "" {
|
|
|
|
return SettingsError{"you must specify an 'issuer' URL pattern"}
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
if strings.Count(data.URLPatterns.Issuer, "%s") != 1 {
|
|
|
|
return SettingsError{"url-pattern 'issuer' must contain one '%s' placeholder"}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Organization = &pkix.Name{}
|
|
|
|
s.Organization.Organization = data.Organization.Organization
|
|
|
|
s.Organization.Country = data.Organization.Country
|
|
|
|
s.Organization.Locality = data.Organization.Locality
|
|
|
|
s.Organization.StreetAddress = data.Organization.StreetAddress
|
|
|
|
s.Organization.PostalCode = data.Organization.PostalCode
|
|
|
|
|
|
|
|
s.ValidityYears.Root = data.ValidityYears.Root
|
|
|
|
s.ValidityYears.Intermediary = data.ValidityYears.Intermediary
|
|
|
|
|
|
|
|
s.URLPatterns.Ocsp = data.URLPatterns.Ocsp
|
|
|
|
s.URLPatterns.CRL = data.URLPatterns.CRL
|
|
|
|
s.URLPatterns.Issuer = data.URLPatterns.Issuer
|
|
|
|
|
|
|
|
return nil
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type KeyStorage struct {
|
|
|
|
Label string
|
|
|
|
Module string
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (k *KeyStorage) UnmarshalYAML(n *yaml.Node) error {
|
|
|
|
ks := struct {
|
|
|
|
TokenType string `yaml:"type"`
|
|
|
|
Label string `yaml:"label"`
|
|
|
|
Module string `yaml:"module"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := n.Decode(&ks)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not decode YAML: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ks.TokenType {
|
|
|
|
case "softhsm":
|
2022-04-23 16:34:51 +00:00
|
|
|
k.Module = SoftHsmModule
|
2022-04-19 14:48:32 +00:00
|
|
|
case "p11module":
|
|
|
|
if ks.Module == "" {
|
|
|
|
return errors.New("specify a 'module' field when using the 'p11module' type")
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
k.Module = ks.Module
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported KeyStorage type '%s'", ks.TokenType)
|
|
|
|
}
|
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
if ks.Label == "" {
|
|
|
|
return errors.New("element 'label' must be specified")
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
k.Label = ks.Label
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SignerConfig struct {
|
|
|
|
global *Settings `yaml:"Settings"`
|
|
|
|
caMap map[string]*CaCertificateEntry `yaml:"CAs"`
|
|
|
|
KeyStorage map[string]*KeyStorage `yaml:"KeyStorage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SignerConfig) GetCADefinition(label string) (*CaCertificateEntry, error) {
|
|
|
|
entry, ok := c.caMap[label]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no CA definition found for label %s", label)
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
func (c *SignerConfig) CalculateValidity(cert *CaCertificateEntry, relativeTo time.Time) (time.Time, time.Time) {
|
2022-04-16 20:24:32 +00:00
|
|
|
var notBefore, notAfter time.Time
|
2022-04-23 16:34:51 +00:00
|
|
|
notBefore = relativeTo
|
2022-04-16 20:24:32 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if cert.IsRoot() {
|
|
|
|
notAfter = notBefore.AddDate(c.global.ValidityYears.Root, 0, 0)
|
2022-04-16 20:24:32 +00:00
|
|
|
} else {
|
2022-04-19 14:48:32 +00:00
|
|
|
notAfter = notBefore.AddDate(c.global.ValidityYears.Intermediary, 0, 0)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
return notBefore, notAfter
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) CalculateSubject(cert *CaCertificateEntry) pkix.Name {
|
|
|
|
subject := pkix.Name{
|
|
|
|
Country: c.global.Organization.Country,
|
|
|
|
Organization: c.global.Organization.Organization,
|
|
|
|
Locality: c.global.Organization.Locality,
|
|
|
|
StreetAddress: c.global.Organization.StreetAddress,
|
|
|
|
PostalCode: c.global.Organization.PostalCode,
|
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
subject.CommonName = cert.CommonName
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
return subject
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) CertificateFileName(label string) string {
|
|
|
|
return fmt.Sprintf("%s.crt", label)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) BuildIssuerURL(cert *CaCertificateEntry) string {
|
2022-04-23 16:34:51 +00:00
|
|
|
return fmt.Sprintf(c.global.URLPatterns.Issuer, cert.Parent)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) BuildOCSPURL(cert *CaCertificateEntry) string {
|
|
|
|
// in case the configuration specifies a placeholder
|
|
|
|
if strings.Count(c.global.URLPatterns.Ocsp, "%s") == 1 {
|
2022-04-23 16:34:51 +00:00
|
|
|
return fmt.Sprintf(c.global.URLPatterns.Ocsp, cert.Parent)
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return c.global.URLPatterns.Ocsp
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) BuildCRLUrl(cert *CaCertificateEntry) string {
|
2022-04-23 16:34:51 +00:00
|
|
|
return fmt.Sprintf(c.global.URLPatterns.CRL, cert.Parent)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *SignerConfig) GetParentCA(label string) (*CaCertificateEntry, error) {
|
|
|
|
entry, ok := c.caMap[label]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no CA definition for %s", label)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if entry.IsRoot() {
|
|
|
|
return nil, fmt.Errorf("CA %s is a root CA and has no parent", label)
|
|
|
|
}
|
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
return c.caMap[entry.Parent], nil
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RootCAs returns the labels of all configured root CAs
|
|
|
|
func (c *SignerConfig) RootCAs() []string {
|
|
|
|
roots := make([]string, 0)
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
for label, entry := range c.caMap {
|
|
|
|
if entry.IsRoot() {
|
|
|
|
roots = append(roots, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return roots
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntermediaryCAs returns the labels of all configured intermediary CAs
|
|
|
|
func (c *SignerConfig) IntermediaryCAs() []string {
|
|
|
|
intermediaries := make([]string, 0)
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
for label, entry := range c.caMap {
|
|
|
|
if !entry.IsRoot() {
|
|
|
|
intermediaries = append(intermediaries, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return intermediaries
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfiguration reads YAML configuration from the given reader as a SignerConfig structure
|
|
|
|
func LoadConfiguration(r io.Reader) (*SignerConfig, error) {
|
|
|
|
config := struct {
|
|
|
|
Global *Settings `yaml:"Settings"`
|
|
|
|
CAs map[string]*CaCertificateEntry `yaml:"CAs"`
|
|
|
|
KeyStorage map[string]*KeyStorage `yaml:"KeyStorage"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
decoder := yaml.NewDecoder(r)
|
|
|
|
err := decoder.Decode(&config)
|
2022-04-16 20:24:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-19 14:48:32 +00:00
|
|
|
return nil, fmt.Errorf("could not parse YAML configuration: %w", err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 19:12:34 +00:00
|
|
|
if config.Global == nil {
|
|
|
|
return nil, errors.New("configuration entry 'Settings' is missing or empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CAs == nil {
|
|
|
|
return nil, errors.New("configuration entry 'CAs' is missing or empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.KeyStorage == nil {
|
|
|
|
return nil, errors.New("configuration entry 'KeyStorage' is missing or empty")
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return &SignerConfig{
|
|
|
|
global: config.Global,
|
|
|
|
caMap: config.CAs,
|
|
|
|
KeyStorage: config.KeyStorage,
|
|
|
|
}, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PrivateKeyInfo struct {
|
|
|
|
Algorithm x509.PublicKeyAlgorithm
|
|
|
|
EccCurve elliptic.Curve
|
|
|
|
RSABits int
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (p *PrivateKeyInfo) UnmarshalYAML(value *yaml.Node) error {
|
2022-04-16 20:24:32 +00:00
|
|
|
internalStructure := struct {
|
2022-04-19 14:48:32 +00:00
|
|
|
Algorithm string `yaml:"algorithm"`
|
|
|
|
EccCurve string `yaml:"ecc-curve,omitempty"`
|
|
|
|
RSABits *int `yaml:"rsa-bits,omitempty"`
|
2022-04-16 20:24:32 +00:00
|
|
|
}{}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
err := value.Decode(&internalStructure)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal private key info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch internalStructure.Algorithm {
|
|
|
|
case "RSA":
|
|
|
|
p.Algorithm = x509.RSA
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
if internalStructure.RSABits == nil {
|
2022-04-21 19:12:34 +00:00
|
|
|
return errors.New("element 'rsa-bits' with RSA key length required for algorithm RSA")
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
p.RSABits = *internalStructure.RSABits
|
|
|
|
case "EC":
|
|
|
|
p.Algorithm = x509.ECDSA
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-21 19:12:34 +00:00
|
|
|
if internalStructure.EccCurve == "" {
|
|
|
|
return errors.New("element 'ecc-curve' required for algorithm EC")
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
p.EccCurve, err = nameToCurve(internalStructure.EccCurve)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-21 19:12:34 +00:00
|
|
|
case "":
|
|
|
|
return errors.New("element 'algorithm' must be specified as 'EC' or 'RSA'")
|
2022-04-16 20:24:32 +00:00
|
|
|
default:
|
2022-04-21 19:12:34 +00:00
|
|
|
return fmt.Errorf(
|
|
|
|
"unsupported key algorithm %s, use either 'EC' or 'RSA'",
|
|
|
|
internalStructure.Algorithm,
|
|
|
|
)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (p *PrivateKeyInfo) MarshalYAML() (interface{}, error) {
|
2022-04-16 20:24:32 +00:00
|
|
|
internalStructure := struct {
|
2022-04-19 14:48:32 +00:00
|
|
|
Algorithm string `yaml:"algorithm"`
|
|
|
|
EccCurve string `yaml:"ecc-curve,omitempty"`
|
|
|
|
RSABits *int `yaml:"rsa-bits,omitempty"`
|
2022-04-16 20:24:32 +00:00
|
|
|
}{}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
switch p.Algorithm {
|
|
|
|
case x509.RSA:
|
|
|
|
internalStructure.Algorithm = "RSA"
|
|
|
|
internalStructure.RSABits = &p.RSABits
|
|
|
|
case x509.ECDSA:
|
|
|
|
internalStructure.Algorithm = "EC"
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
curveName, err := curveToName(p.EccCurve)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-24 07:25:04 +00:00
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
internalStructure.EccCurve = curveName
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
return internalStructure, nil
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func curveToName(curve elliptic.Curve) (string, error) {
|
|
|
|
switch curve {
|
|
|
|
case elliptic.P224():
|
|
|
|
return "P-224", nil
|
|
|
|
case elliptic.P256():
|
|
|
|
return "P-256", nil
|
|
|
|
case elliptic.P384():
|
|
|
|
return "P-384", nil
|
|
|
|
case elliptic.P521():
|
|
|
|
return "P-521", nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unsupported EC curve %s", curve)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nameToCurve maps a curve name to an elliptic curve
|
|
|
|
func nameToCurve(name string) (elliptic.Curve, error) {
|
|
|
|
switch name {
|
|
|
|
case "P-224", "secp224r1":
|
|
|
|
return elliptic.P224(), nil
|
|
|
|
case "P-256", "secp256r1":
|
|
|
|
return elliptic.P256(), nil
|
|
|
|
case "P-384", "secp384r1":
|
|
|
|
return elliptic.P384(), nil
|
|
|
|
case "P-521", "secp521r1":
|
|
|
|
return elliptic.P521(), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported EC curve %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CaCertificateEntry struct {
|
|
|
|
KeyInfo *PrivateKeyInfo
|
|
|
|
CommonName string
|
2022-04-19 14:48:32 +00:00
|
|
|
MaxPathLen int // maximum path length should be 0 for CAs that issue end entity certificates
|
|
|
|
ExtKeyUsage []x509.ExtKeyUsage
|
2022-04-16 20:24:32 +00:00
|
|
|
Certificate *x509.Certificate
|
|
|
|
KeyPair crypto.Signer
|
2022-04-23 16:34:51 +00:00
|
|
|
Parent string
|
2022-04-19 14:48:32 +00:00
|
|
|
Storage string
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *CaCertificateEntry) UnmarshalYAML(value *yaml.Node) error {
|
2022-04-16 20:24:32 +00:00
|
|
|
var m struct {
|
2022-04-24 07:25:04 +00:00
|
|
|
KeyInfo *PrivateKeyInfo `yaml:"key-info"`
|
|
|
|
CommonName string `yaml:"common-name"`
|
|
|
|
// maximum path length should be 0 for CAs that issue end entity certificates
|
|
|
|
MaxPathLen int `yaml:"max-path-len,omitempty"`
|
|
|
|
ExtKeyUsage []string `yaml:"ext-key-usages,omitempty"`
|
|
|
|
Parent string `yaml:"parent"`
|
|
|
|
Storage string `yaml:"storage"`
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
err := value.Decode(&m)
|
2022-04-16 20:24:32 +00:00
|
|
|
if err != nil {
|
2022-04-24 07:25:04 +00:00
|
|
|
return fmt.Errorf("could not unmarshal CA certificate entry: %w", err)
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 19:12:34 +00:00
|
|
|
if m.KeyInfo == nil {
|
|
|
|
return errors.New("element 'key-info' must be set")
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
c.KeyInfo = m.KeyInfo
|
2022-04-21 19:12:34 +00:00
|
|
|
|
|
|
|
if m.CommonName == "" {
|
|
|
|
return errors.New("element 'common-name' must be set")
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
c.CommonName = m.CommonName
|
|
|
|
c.MaxPathLen = m.MaxPathLen
|
2022-04-19 14:48:32 +00:00
|
|
|
|
2022-04-23 16:34:51 +00:00
|
|
|
if m.Parent == "" && m.ExtKeyUsage != nil {
|
|
|
|
return errors.New("a root CA must not specify 'ext-key-usages'")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Parent = m.Parent
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
if m.ExtKeyUsage != nil {
|
|
|
|
c.ExtKeyUsage, err = mapExtKeyUsageNames(m.ExtKeyUsage)
|
2022-04-23 16:34:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-16 20:24:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
if m.Storage != "" {
|
|
|
|
c.Storage = m.Storage
|
|
|
|
} else {
|
|
|
|
c.Storage = "default"
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:48:32 +00:00
|
|
|
func (c *CaCertificateEntry) IsRoot() bool {
|
2022-04-23 16:34:51 +00:00
|
|
|
return c.Parent == ""
|
2022-04-19 14:48:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:32 +00:00
|
|
|
func mapExtKeyUsageNames(usages []string) ([]x509.ExtKeyUsage, error) {
|
|
|
|
extKeyUsages := make([]x509.ExtKeyUsage, len(usages))
|
|
|
|
|
|
|
|
for idx, usage := range usages {
|
|
|
|
switch usage {
|
|
|
|
case "client":
|
|
|
|
extKeyUsages[idx] = x509.ExtKeyUsageClientAuth
|
|
|
|
case "code":
|
|
|
|
extKeyUsages[idx] = x509.ExtKeyUsageCodeSigning
|
|
|
|
case "email":
|
|
|
|
extKeyUsages[idx] = x509.ExtKeyUsageEmailProtection
|
|
|
|
case "server":
|
|
|
|
extKeyUsages[idx] = x509.ExtKeyUsageServerAuth
|
|
|
|
case "ocsp":
|
|
|
|
extKeyUsages[idx] = x509.ExtKeyUsageOCSPSigning
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported extended key usage %s", usage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return extKeyUsages, nil
|
|
|
|
}
|