2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2018-05-09 16:15:29 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
2019-11-01 13:20:26 +00:00
|
|
|
"crypto/rsa"
|
2018-05-09 16:15:29 +00:00
|
|
|
"crypto/x509"
|
2018-09-14 23:08:54 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/asn1"
|
2018-05-09 16:15:29 +00:00
|
|
|
"encoding/pem"
|
2022-04-14 13:26:14 +00:00
|
|
|
"fmt"
|
2020-01-17 22:25:26 +00:00
|
|
|
"net"
|
2018-05-09 16:15:29 +00:00
|
|
|
"net/url"
|
2022-11-14 18:27:03 +00:00
|
|
|
"strings"
|
2018-05-09 16:15:29 +00:00
|
|
|
)
|
|
|
|
|
2019-11-01 13:20:26 +00:00
|
|
|
// SigAlgoForKey returns the preferred x509.SignatureAlgorithm for a given key
|
|
|
|
// based on it's type. If the key type is not supported we return
|
|
|
|
// ECDSAWithSHA256 on the basis that it will fail anyway and we've already type
|
|
|
|
// checked keys by the time we call this in general.
|
|
|
|
func SigAlgoForKey(key crypto.Signer) x509.SignatureAlgorithm {
|
|
|
|
if _, ok := key.(*rsa.PrivateKey); ok {
|
|
|
|
return x509.SHA256WithRSA
|
|
|
|
}
|
|
|
|
// We default to ECDSA but don't bother detecting invalid key types as we do
|
|
|
|
// that in lots of other places and it will fail anyway if we try to sign with
|
|
|
|
// an incompatible type.
|
|
|
|
return x509.ECDSAWithSHA256
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigAlgoForKeyType returns the preferred x509.SignatureAlgorithm for a given
|
|
|
|
// key type string from configuration or an existing cert. If the key type is
|
|
|
|
// not supported we return ECDSAWithSHA256 on the basis that it will fail anyway
|
|
|
|
// and we've already type checked config by the time we call this in general.
|
|
|
|
func SigAlgoForKeyType(keyType string) x509.SignatureAlgorithm {
|
|
|
|
switch keyType {
|
|
|
|
case "rsa":
|
|
|
|
return x509.SHA256WithRSA
|
|
|
|
case "ec":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return x509.ECDSAWithSHA256
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:25:26 +00:00
|
|
|
// CreateCSR returns a CSR to sign the given service with SAN entries
|
|
|
|
// along with the PEM-encoded private key for this certificate.
|
2021-06-25 18:00:00 +00:00
|
|
|
func CreateCSR(uri CertURI, privateKey crypto.Signer,
|
2020-01-17 22:25:26 +00:00
|
|
|
dnsNames []string, ipAddresses []net.IP, extensions ...pkix.Extension) (string, error) {
|
2022-11-14 18:27:03 +00:00
|
|
|
|
|
|
|
// Drop everything after the ':' from the name when constructing the DNS SANs.
|
|
|
|
uniqueNames := make(map[string]struct{})
|
|
|
|
formattedDNSNames := make([]string, 0)
|
|
|
|
for _, host := range dnsNames {
|
|
|
|
hostSegments := strings.Split(host, ":")
|
|
|
|
if len(hostSegments) == 0 || hostSegments[0] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedHost := hostSegments[0]
|
|
|
|
if _, ok := uniqueNames[formattedHost]; !ok {
|
|
|
|
formattedDNSNames = append(formattedDNSNames, formattedHost)
|
|
|
|
uniqueNames[formattedHost] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 16:15:29 +00:00
|
|
|
template := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{uri.URI()},
|
2019-11-01 13:20:26 +00:00
|
|
|
SignatureAlgorithm: SigAlgoForKey(privateKey),
|
2018-09-14 23:08:54 +00:00
|
|
|
ExtraExtensions: extensions,
|
2022-11-14 18:27:03 +00:00
|
|
|
DNSNames: formattedDNSNames,
|
2020-01-17 22:25:26 +00:00
|
|
|
IPAddresses: ipAddresses,
|
2018-05-09 16:15:29 +00:00
|
|
|
}
|
2021-06-25 18:00:00 +00:00
|
|
|
HackSANExtensionForCSR(template)
|
2018-05-09 16:15:29 +00:00
|
|
|
|
|
|
|
// Create the CSR itself
|
|
|
|
var csrBuf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificateRequest(rand.Reader, template, privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pem.Encode(&csrBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return csrBuf.String(), nil
|
|
|
|
}
|
2018-09-14 23:08:54 +00:00
|
|
|
|
|
|
|
// CreateCSR returns a CA CSR to sign the given service along with the PEM-encoded
|
|
|
|
// private key for this certificate.
|
2021-06-25 18:00:00 +00:00
|
|
|
func CreateCACSR(uri CertURI, privateKey crypto.Signer) (string, error) {
|
2018-09-14 23:08:54 +00:00
|
|
|
ext, err := CreateCAExtension()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:00:00 +00:00
|
|
|
return CreateCSR(uri, privateKey, nil, nil, ext)
|
2018-09-14 23:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCAExtension creates a pkix.Extension for the x509 Basic Constraints
|
|
|
|
// IsCA field ()
|
|
|
|
func CreateCAExtension() (pkix.Extension, error) {
|
|
|
|
type basicConstraints struct {
|
|
|
|
IsCA bool `asn1:"optional"`
|
|
|
|
MaxPathLen int `asn1:"optional"`
|
|
|
|
}
|
|
|
|
basicCon := basicConstraints{IsCA: true, MaxPathLen: 0}
|
|
|
|
bitstr, err := asn1.Marshal(basicCon)
|
|
|
|
if err != nil {
|
|
|
|
return pkix.Extension{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkix.Extension{
|
|
|
|
Id: []int{2, 5, 29, 19}, // from x509 package
|
|
|
|
Critical: true,
|
|
|
|
Value: bitstr,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-04-14 13:26:14 +00:00
|
|
|
|
|
|
|
// InvalidCSRError returns an error with the given fmt.Sprintf-formatted message
|
|
|
|
// indicating certificate signing failed because the user supplied an invalid CSR.
|
|
|
|
//
|
|
|
|
// See: IsInvalidCSRError.
|
|
|
|
func InvalidCSRError(format string, args ...interface{}) error {
|
|
|
|
return invalidCSRError{fmt.Sprintf(format, args...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvalidCSRError returns whether the given error indicates that certificate
|
|
|
|
// signing failed because the user supplied an invalid CSR.
|
|
|
|
func IsInvalidCSRError(err error) bool {
|
|
|
|
_, ok := err.(invalidCSRError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type invalidCSRError struct {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidCSRError) Error() string { return e.s }
|