2020-09-09 02:26:32 +00:00
|
|
|
package cloudcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-15 02:13:40 +00:00
|
|
|
"crypto/rand"
|
2020-09-09 02:26:32 +00:00
|
|
|
"crypto/x509"
|
2020-09-10 23:19:18 +00:00
|
|
|
"encoding/asn1"
|
2021-06-09 00:43:52 +00:00
|
|
|
"encoding/json"
|
2020-09-09 02:26:32 +00:00
|
|
|
"encoding/pem"
|
2020-10-23 22:04:09 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-09-09 02:26:32 +00:00
|
|
|
"time"
|
|
|
|
|
2021-06-04 02:31:19 +00:00
|
|
|
privateca "cloud.google.com/go/security/privateca/apiv1"
|
2020-09-10 23:19:18 +00:00
|
|
|
"github.com/google/uuid"
|
2020-09-15 02:13:40 +00:00
|
|
|
gax "github.com/googleapis/gax-go/v2"
|
2020-09-09 02:26:32 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
2020-10-26 23:43:02 +00:00
|
|
|
"go.step.sm/crypto/x509util"
|
2020-09-10 23:19:18 +00:00
|
|
|
"google.golang.org/api/option"
|
2021-06-04 02:31:19 +00:00
|
|
|
pb "google.golang.org/genproto/googleapis/cloud/security/privateca/v1"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2020-09-09 02:26:32 +00:00
|
|
|
durationpb "google.golang.org/protobuf/types/known/durationpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiv1.Register(apiv1.CloudCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
|
|
|
|
return New(ctx, opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-26 23:43:02 +00:00
|
|
|
var now = func() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// The actual regular expression that matches a certificate authority is:
|
2021-06-04 02:31:19 +00:00
|
|
|
// ^projects/[a-z][a-z0-9-]{4,28}[a-z0-9]/locations/[a-z0-9-]+/caPools/[a-zA-Z0-9-_]+/certificateAuthorities/[a-zA-Z0-9-_]+$
|
2020-10-23 22:04:09 +00:00
|
|
|
// But we will allow a more flexible one to fail if this changes.
|
2021-06-04 02:31:19 +00:00
|
|
|
var caRegexp = regexp.MustCompile("^projects/[^/]+/locations/[^/]+/caPools/[^/]+/certificateAuthorities/[^/]+$")
|
2020-10-23 22:04:09 +00:00
|
|
|
|
2020-09-15 02:13:40 +00:00
|
|
|
// CertificateAuthorityClient is the interface implemented by the Google CAS
|
|
|
|
// client.
|
|
|
|
type CertificateAuthorityClient interface {
|
|
|
|
CreateCertificate(ctx context.Context, req *pb.CreateCertificateRequest, opts ...gax.CallOption) (*pb.Certificate, error)
|
|
|
|
RevokeCertificate(ctx context.Context, req *pb.RevokeCertificateRequest, opts ...gax.CallOption) (*pb.Certificate, error)
|
2020-09-21 22:27:20 +00:00
|
|
|
GetCertificateAuthority(ctx context.Context, req *pb.GetCertificateAuthorityRequest, opts ...gax.CallOption) (*pb.CertificateAuthority, error)
|
2020-10-23 22:04:09 +00:00
|
|
|
CreateCertificateAuthority(ctx context.Context, req *pb.CreateCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.CreateCertificateAuthorityOperation, error)
|
|
|
|
FetchCertificateAuthorityCsr(ctx context.Context, req *pb.FetchCertificateAuthorityCsrRequest, opts ...gax.CallOption) (*pb.FetchCertificateAuthorityCsrResponse, error)
|
|
|
|
ActivateCertificateAuthority(ctx context.Context, req *pb.ActivateCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.ActivateCertificateAuthorityOperation, error)
|
2021-06-04 02:31:19 +00:00
|
|
|
EnableCertificateAuthority(ctx context.Context, req *pb.EnableCertificateAuthorityRequest, opts ...gax.CallOption) (*privateca.EnableCertificateAuthorityOperation, error)
|
|
|
|
GetCaPool(ctx context.Context, req *pb.GetCaPoolRequest, opts ...gax.CallOption) (*pb.CaPool, error)
|
|
|
|
CreateCaPool(ctx context.Context, req *pb.CreateCaPoolRequest, opts ...gax.CallOption) (*privateca.CreateCaPoolOperation, error)
|
2020-09-10 23:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:13:40 +00:00
|
|
|
// recocationCodeMap maps revocation reason codes from RFC 5280, to Google CAS
|
|
|
|
// revocation reasons. Revocation reason 7 is not used, and revocation reason 8
|
|
|
|
// (removeFromCRL) is not supported by Google CAS.
|
|
|
|
var revocationCodeMap = map[int]pb.RevocationReason{
|
|
|
|
0: pb.RevocationReason_REVOCATION_REASON_UNSPECIFIED,
|
|
|
|
1: pb.RevocationReason_KEY_COMPROMISE,
|
|
|
|
2: pb.RevocationReason_CERTIFICATE_AUTHORITY_COMPROMISE,
|
|
|
|
3: pb.RevocationReason_AFFILIATION_CHANGED,
|
|
|
|
4: pb.RevocationReason_SUPERSEDED,
|
|
|
|
5: pb.RevocationReason_CESSATION_OF_OPERATION,
|
|
|
|
6: pb.RevocationReason_CERTIFICATE_HOLD,
|
|
|
|
9: pb.RevocationReason_PRIVILEGE_WITHDRAWN,
|
|
|
|
10: pb.RevocationReason_ATTRIBUTE_AUTHORITY_COMPROMISE,
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
// caPoolTierMap contains the map between apv1.Options.Tier and the pb type.
|
|
|
|
var caPoolTierMap = map[string]pb.CaPool_Tier{
|
|
|
|
"": pb.CaPool_DEVOPS,
|
|
|
|
"ENTERPRISE": pb.CaPool_ENTERPRISE,
|
|
|
|
"DEVOPS": pb.CaPool_DEVOPS,
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:26:32 +00:00
|
|
|
// CloudCAS implements a Certificate Authority Service using Google Cloud CAS.
|
|
|
|
type CloudCAS struct {
|
2020-09-15 02:13:40 +00:00
|
|
|
client CertificateAuthorityClient
|
2020-09-09 02:26:32 +00:00
|
|
|
certificateAuthority string
|
2020-10-23 22:04:09 +00:00
|
|
|
project string
|
|
|
|
location string
|
2021-06-04 02:31:19 +00:00
|
|
|
caPool string
|
2021-06-09 00:43:52 +00:00
|
|
|
caPoolTier pb.CaPool_Tier
|
2021-06-07 22:53:29 +00:00
|
|
|
gcsBucket string
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:13:40 +00:00
|
|
|
// newCertificateAuthorityClient creates the certificate authority client. This
|
|
|
|
// function is used for testing purposes.
|
|
|
|
var newCertificateAuthorityClient = func(ctx context.Context, credentialsFile string) (CertificateAuthorityClient, error) {
|
|
|
|
var cloudOpts []option.ClientOption
|
|
|
|
if credentialsFile != "" {
|
|
|
|
cloudOpts = append(cloudOpts, option.WithCredentialsFile(credentialsFile))
|
|
|
|
}
|
|
|
|
client, err := privateca.NewCertificateAuthorityClient(ctx, cloudOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error creating client")
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
2020-09-09 02:26:32 +00:00
|
|
|
|
|
|
|
// New creates a new CertificateAuthorityService implementation using Google
|
|
|
|
// Cloud CAS.
|
|
|
|
func New(ctx context.Context, opts apiv1.Options) (*CloudCAS, error) {
|
2021-06-09 00:43:52 +00:00
|
|
|
var caPoolTier pb.CaPool_Tier
|
|
|
|
if opts.IsCreator && opts.CertificateAuthority == "" {
|
2020-10-23 22:04:09 +00:00
|
|
|
switch {
|
|
|
|
case opts.Project == "":
|
|
|
|
return nil, errors.New("cloudCAS 'project' cannot be empty")
|
|
|
|
case opts.Location == "":
|
|
|
|
return nil, errors.New("cloudCAS 'location' cannot be empty")
|
2021-06-08 02:20:23 +00:00
|
|
|
case opts.CaPool == "":
|
2021-06-04 02:31:19 +00:00
|
|
|
return nil, errors.New("cloudCAS 'caPool' cannot be empty")
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
2021-06-09 00:43:52 +00:00
|
|
|
var ok bool
|
|
|
|
if caPoolTier, ok = caPoolTierMap[strings.ToUpper(opts.CaPoolTier)]; !ok {
|
|
|
|
return nil, errors.New("cloudCAS 'caPoolTier' is not a valid tier")
|
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
} else {
|
|
|
|
if opts.CertificateAuthority == "" {
|
|
|
|
return nil, errors.New("cloudCAS 'certificateAuthority' cannot be empty")
|
|
|
|
}
|
|
|
|
if !caRegexp.MatchString(opts.CertificateAuthority) {
|
|
|
|
return nil, errors.New("cloudCAS 'certificateAuthority' is not valid certificate authority resource")
|
|
|
|
}
|
|
|
|
// Extract project and location from CertificateAuthority
|
2021-06-04 02:31:19 +00:00
|
|
|
if parts := strings.Split(opts.CertificateAuthority, "/"); len(parts) == 8 {
|
2020-10-23 22:04:09 +00:00
|
|
|
if opts.Project == "" {
|
|
|
|
opts.Project = parts[1]
|
|
|
|
}
|
|
|
|
if opts.Location == "" {
|
|
|
|
opts.Location = parts[3]
|
|
|
|
}
|
2021-06-08 02:20:23 +00:00
|
|
|
if opts.CaPool == "" {
|
|
|
|
opts.CaPool = parts[5]
|
2021-06-04 02:31:19 +00:00
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
2020-09-11 02:09:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:13:40 +00:00
|
|
|
client, err := newCertificateAuthorityClient(ctx, opts.CredentialsFile)
|
2020-09-09 02:26:32 +00:00
|
|
|
if err != nil {
|
2020-09-15 02:13:40 +00:00
|
|
|
return nil, err
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
// GCSBucket is the the bucket name or empty for a managed bucket.
|
2020-09-09 02:26:32 +00:00
|
|
|
return &CloudCAS{
|
|
|
|
client: client,
|
2020-10-20 01:47:36 +00:00
|
|
|
certificateAuthority: opts.CertificateAuthority,
|
2020-10-23 22:04:09 +00:00
|
|
|
project: opts.Project,
|
|
|
|
location: opts.Location,
|
2021-06-08 02:20:23 +00:00
|
|
|
caPool: opts.CaPool,
|
2021-06-09 00:43:52 +00:00
|
|
|
gcsBucket: opts.GCSBucket,
|
|
|
|
caPoolTier: caPoolTier,
|
2020-09-09 02:26:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:27:20 +00:00
|
|
|
// GetCertificateAuthority returns the root certificate for the given
|
|
|
|
// certificate authority. It implements apiv1.CertificateAuthorityGetter
|
|
|
|
// interface.
|
|
|
|
func (c *CloudCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
|
|
|
|
name := req.Name
|
|
|
|
if name == "" {
|
|
|
|
name = c.certificateAuthority
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := c.client.GetCertificateAuthority(ctx, &pb.GetCertificateAuthorityRequest{
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS GetCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
if len(resp.PemCaCertificates) == 0 {
|
|
|
|
return nil, errors.New("cloudCAS GetCertificateAuthority: PemCACertificate should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last certificate in the chain is the root.
|
|
|
|
root, err := parseCertificate(resp.PemCaCertificates[len(resp.PemCaCertificates)-1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.GetCertificateAuthorityResponse{
|
|
|
|
RootCertificate: root,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:26:32 +00:00
|
|
|
// CreateCertificate signs a new certificate using Google Cloud CAS.
|
|
|
|
func (c *CloudCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
|
|
|
|
switch {
|
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateRequest `template` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
cert, chain, err := c.createCertificate(req.Template, req.Lifetime, req.RequestID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewCertificate renews the given certificate using Google Cloud CAS.
|
|
|
|
// Google's CAS does not support the renew operation, so this method uses
|
|
|
|
// CreateCertificate.
|
|
|
|
func (c *CloudCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
|
|
|
|
switch {
|
|
|
|
case req.Template == nil:
|
2020-09-11 02:09:46 +00:00
|
|
|
return nil, errors.New("renewCertificateRequest `template` cannot be nil")
|
2020-09-10 23:19:18 +00:00
|
|
|
case req.Lifetime == 0:
|
2020-09-11 02:09:46 +00:00
|
|
|
return nil, errors.New("renewCertificateRequest `lifetime` cannot be 0")
|
2020-09-10 23:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert, chain, err := c.createCertificate(req.Template, req.Lifetime, req.RequestID)
|
2020-09-09 02:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
return &apiv1.RenewCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// RevokeCertificate revokes a certificate using Google Cloud CAS.
|
2020-09-10 23:19:18 +00:00
|
|
|
func (c *CloudCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
|
2020-09-15 02:13:40 +00:00
|
|
|
reason, ok := revocationCodeMap[req.ReasonCode]
|
|
|
|
switch {
|
|
|
|
case !ok:
|
|
|
|
return nil, errors.Errorf("revokeCertificate 'reasonCode=%d' is invalid or not supported", req.ReasonCode)
|
|
|
|
case req.Certificate == nil:
|
2020-09-11 02:09:46 +00:00
|
|
|
return nil, errors.New("revokeCertificateRequest `certificate` cannot be nil")
|
2020-09-10 23:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ext, ok := apiv1.FindCertificateAuthorityExtension(req.Certificate)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("error revoking certificate: certificate authority extension was not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cae apiv1.CertificateAuthorityExtension
|
2020-09-15 02:13:40 +00:00
|
|
|
if _, err := asn1.Unmarshal(ext.Value, &cae); err != nil {
|
2020-09-10 23:19:18 +00:00
|
|
|
return nil, errors.Wrap(err, "error unmarshaling certificate authority extension")
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:26:32 +00:00
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
certpb, err := c.client.RevokeCertificate(ctx, &pb.RevokeCertificateRequest{
|
2020-09-15 02:13:40 +00:00
|
|
|
Name: c.certificateAuthority + "/certificates/" + cae.CertificateID,
|
|
|
|
Reason: reason,
|
|
|
|
RequestId: req.RequestID,
|
2020-09-09 02:26:32 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-10 23:19:18 +00:00
|
|
|
return nil, errors.Wrap(err, "cloudCAS RevokeCertificate failed")
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
cert, chain, err := getCertificateAndChain(certpb)
|
2020-09-09 02:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
return &apiv1.RevokeCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
2020-09-09 02:26:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// CreateCertificateAuthority creates a new root or intermediate certificate
|
|
|
|
// using Google Cloud CAS.
|
|
|
|
func (c *CloudCAS) CreateCertificateAuthority(req *apiv1.CreateCertificateAuthorityRequest) (*apiv1.CreateCertificateAuthorityResponse, error) {
|
|
|
|
switch {
|
|
|
|
case c.project == "":
|
|
|
|
return nil, errors.New("cloudCAS `project` cannot be empty")
|
|
|
|
case c.location == "":
|
|
|
|
return nil, errors.New("cloudCAS `location` cannot be empty")
|
2021-06-07 22:53:29 +00:00
|
|
|
case c.caPool == "":
|
|
|
|
return nil, errors.New("cloudCAS `caPool` cannot be empty")
|
2021-06-09 00:43:52 +00:00
|
|
|
case c.caPoolTier == 0:
|
|
|
|
return nil, errors.New("cloudCAS `caPoolTier` cannot be empty")
|
2020-10-23 22:04:09 +00:00
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `template` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `lifetime` cannot be 0")
|
|
|
|
case req.Type == apiv1.IntermediateCA && req.Parent == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `parent` cannot be nil")
|
2020-10-26 23:43:02 +00:00
|
|
|
case req.Type == apiv1.IntermediateCA && req.Parent.Name == "" && (req.Parent.Certificate == nil || req.Parent.Signer == nil):
|
2020-10-23 22:04:09 +00:00
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `parent.name` cannot be empty")
|
|
|
|
}
|
|
|
|
|
2020-10-28 00:59:32 +00:00
|
|
|
var caType pb.CertificateAuthority_Type
|
|
|
|
switch req.Type {
|
|
|
|
case apiv1.RootCA:
|
|
|
|
caType = pb.CertificateAuthority_SELF_SIGNED
|
|
|
|
case apiv1.IntermediateCA:
|
|
|
|
caType = pb.CertificateAuthority_SUBORDINATE
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("createCertificateAuthorityRequest `type=%d' is invalid or not supported", req.Type)
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// Select key and signature algorithm to use
|
|
|
|
var err error
|
|
|
|
var keySpec *pb.CertificateAuthority_KeyVersionSpec
|
|
|
|
if req.CreateKey == nil {
|
|
|
|
if keySpec, err = createKeyVersionSpec(0, 0); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "createCertificateAuthorityRequest `createKey` is not valid")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if keySpec, err = createKeyVersionSpec(req.CreateKey.SignatureAlgorithm, req.CreateKey.Bits); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "createCertificateAuthorityRequest `createKey` is not valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize or generate id.
|
2020-10-28 00:59:32 +00:00
|
|
|
caID := normalizeCertificateAuthorityName(req.Name)
|
|
|
|
if caID == "" {
|
2020-10-23 22:04:09 +00:00
|
|
|
id, err := createCertificateID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-28 00:59:32 +00:00
|
|
|
caID = id
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add CertificateAuthority extension
|
2020-10-28 00:59:32 +00:00
|
|
|
casExtension, err := apiv1.CreateCertificateAuthorityExtension(apiv1.CloudCAS, caID)
|
2020-10-23 22:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Template.ExtraExtensions = append(req.Template.ExtraExtensions, casExtension)
|
|
|
|
|
2021-06-04 02:31:19 +00:00
|
|
|
// Create the caPool if necessary
|
|
|
|
parent, err := c.createCaPoolIfNecessary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// Prepare CreateCertificateAuthorityRequest
|
|
|
|
pbReq := &pb.CreateCertificateAuthorityRequest{
|
2021-06-04 02:31:19 +00:00
|
|
|
Parent: parent,
|
2020-10-28 00:59:32 +00:00
|
|
|
CertificateAuthorityId: caID,
|
2020-10-23 22:04:09 +00:00
|
|
|
RequestId: req.RequestID,
|
|
|
|
CertificateAuthority: &pb.CertificateAuthority{
|
2020-10-28 00:59:32 +00:00
|
|
|
Type: caType,
|
2020-10-23 22:04:09 +00:00
|
|
|
Config: &pb.CertificateConfig{
|
|
|
|
SubjectConfig: &pb.CertificateConfig_SubjectConfig{
|
2021-06-04 02:31:19 +00:00
|
|
|
Subject: createSubject(req.Template),
|
|
|
|
SubjectAltName: createSubjectAlternativeNames(req.Template),
|
2020-10-23 22:04:09 +00:00
|
|
|
},
|
2021-06-04 02:31:19 +00:00
|
|
|
X509Config: createX509Parameters(req.Template),
|
2020-10-23 22:04:09 +00:00
|
|
|
},
|
2021-06-04 02:31:19 +00:00
|
|
|
Lifetime: durationpb.New(req.Lifetime),
|
|
|
|
KeySpec: keySpec,
|
2021-06-07 22:53:29 +00:00
|
|
|
GcsBucket: c.gcsBucket,
|
2021-06-04 02:31:19 +00:00
|
|
|
Labels: map[string]string{},
|
2020-10-23 22:04:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create certificate authority.
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := c.client.CreateCertificateAuthority(ctx, pbReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS CreateCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the long-running operation.
|
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ca, err := resp.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS CreateCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign Intermediate CAs with the parent.
|
|
|
|
if req.Type == apiv1.IntermediateCA {
|
2021-06-04 02:31:19 +00:00
|
|
|
ca, err = c.signIntermediateCA(parent, ca.Name, req)
|
2020-10-23 22:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
// Enable Certificate Authority.
|
|
|
|
ca, err = c.enableCertificateAuthority(ca)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
if len(ca.PemCaCertificates) == 0 {
|
|
|
|
return nil, errors.New("cloudCAS CreateCertificateAuthority failed: PemCaCertificates is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := parseCertificate(ca.PemCaCertificates[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chain []*x509.Certificate
|
|
|
|
if pemChain := ca.PemCaCertificates[1:]; len(pemChain) > 0 {
|
|
|
|
chain = make([]*x509.Certificate, len(pemChain))
|
|
|
|
for i, s := range pemChain {
|
|
|
|
if chain[i], err = parseCertificate(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
b, _ := json.MarshalIndent(ca, "", "\t")
|
|
|
|
println(string(b))
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
return &apiv1.CreateCertificateAuthorityResponse{
|
|
|
|
Name: ca.Name,
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 02:31:19 +00:00
|
|
|
func (c *CloudCAS) createCaPoolIfNecessary() (string, error) {
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pool, err := c.client.GetCaPool(ctx, &pb.GetCaPoolRequest{
|
|
|
|
Name: "projects/" + c.project + "/locations/" + c.location + "/caPools/" + c.caPool,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return pool.Name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.Code(err) != codes.NotFound {
|
|
|
|
return "", errors.Wrap(err, "cloudCAS GetCaPool failed")
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
// PublishCrl is only supported by the enterprise tier
|
|
|
|
var publishCrl bool
|
|
|
|
if c.caPoolTier == pb.CaPool_ENTERPRISE {
|
|
|
|
publishCrl = true
|
|
|
|
}
|
|
|
|
|
2021-06-04 02:31:19 +00:00
|
|
|
ctx, cancel = defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
op, err := c.client.CreateCaPool(ctx, &pb.CreateCaPoolRequest{
|
|
|
|
Parent: "projects/" + c.project + "/locations/" + c.location,
|
|
|
|
CaPoolId: c.caPool,
|
|
|
|
CaPool: &pb.CaPool{
|
2021-06-09 00:43:52 +00:00
|
|
|
Tier: c.caPoolTier,
|
2021-06-04 02:31:19 +00:00
|
|
|
IssuancePolicy: nil,
|
|
|
|
PublishingOptions: &pb.CaPool_PublishingOptions{
|
|
|
|
PublishCaCert: true,
|
2021-06-09 00:43:52 +00:00
|
|
|
PublishCrl: publishCrl,
|
2021-06-04 02:31:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "cloudCAS CreateCaPool failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pool, err = op.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "cloudCAS CreateCaPool failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool.Name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudCAS) enableCertificateAuthority(ca *pb.CertificateAuthority) (*pb.CertificateAuthority, error) {
|
|
|
|
if ca.State == pb.CertificateAuthority_ENABLED {
|
|
|
|
return ca, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := c.client.EnableCertificateAuthority(ctx, &pb.EnableCertificateAuthorityRequest{
|
|
|
|
Name: ca.Name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS EnableCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ca, err = resp.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS EnableCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ca, nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
func (c *CloudCAS) createCertificate(tpl *x509.Certificate, lifetime time.Duration, requestID string) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
// Removes the CAS extension if it exists.
|
|
|
|
apiv1.RemoveCertificateAuthorityExtension(tpl)
|
2020-09-09 02:26:32 +00:00
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
// Create new CAS extension with the certificate id.
|
|
|
|
id, err := createCertificateID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
casExtension, err := apiv1.CreateCertificateAuthorityExtension(apiv1.CloudCAS, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
tpl.ExtraExtensions = append(tpl.ExtraExtensions, casExtension)
|
|
|
|
|
|
|
|
// Create and submit certificate
|
|
|
|
certConfig, err := createCertificateConfig(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := defaultContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cert, err := c.client.CreateCertificate(ctx, &pb.CreateCertificateRequest{
|
2021-06-04 02:31:19 +00:00
|
|
|
Parent: "projects/" + c.project + "/locations/" + c.location + "/caPools/" + c.caPool,
|
2020-09-10 23:19:18 +00:00
|
|
|
CertificateId: id,
|
|
|
|
Certificate: &pb.Certificate{
|
|
|
|
CertificateConfig: certConfig,
|
|
|
|
Lifetime: durationpb.New(lifetime),
|
|
|
|
Labels: map[string]string{},
|
|
|
|
},
|
2021-06-09 00:43:52 +00:00
|
|
|
IssuingCertificateAuthorityId: getResourceName(c.certificateAuthority),
|
|
|
|
RequestId: requestID,
|
2020-09-10 23:19:18 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "cloudCAS CreateCertificate failed")
|
|
|
|
}
|
2020-09-09 02:26:32 +00:00
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
// Return certificate and certificate chain
|
|
|
|
return getCertificateAndChain(cert)
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 02:31:19 +00:00
|
|
|
func (c *CloudCAS) signIntermediateCA(parent, name string, req *apiv1.CreateCertificateAuthorityRequest) (*pb.CertificateAuthority, error) {
|
2020-10-23 22:04:09 +00:00
|
|
|
id, err := createCertificateID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch intermediate CSR
|
|
|
|
ctx, cancel := defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
csr, err := c.client.FetchCertificateAuthorityCsr(ctx, &pb.FetchCertificateAuthorityCsrRequest{
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS FetchCertificateAuthorityCsr failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the CSR with the ca.
|
2020-10-26 23:43:02 +00:00
|
|
|
var cert *pb.Certificate
|
|
|
|
if req.Parent.Certificate != nil && req.Parent.Signer != nil {
|
|
|
|
// Using a local certificate and key.
|
|
|
|
cr, err := parseCertificateRequest(csr.PemCsr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
template, err := x509util.CreateCertificateTemplate(cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
|
2020-10-26 23:43:02 +00:00
|
|
|
t := now()
|
|
|
|
template.NotBefore = t.Add(-1 * req.Backdate)
|
|
|
|
template.NotAfter = t.Add(req.Lifetime)
|
|
|
|
|
|
|
|
// Sign certificate
|
|
|
|
crt, err := x509util.CreateCertificate(template, req.Parent.Certificate, template.PublicKey, req.Parent.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build pb.Certificate for activaion
|
|
|
|
chain := []string{
|
|
|
|
encodeCertificate(req.Parent.Certificate),
|
|
|
|
}
|
|
|
|
for _, c := range req.Parent.CertificateChain {
|
|
|
|
chain = append(chain, encodeCertificate(c))
|
|
|
|
}
|
|
|
|
cert = &pb.Certificate{
|
|
|
|
PemCertificate: encodeCertificate(crt),
|
|
|
|
PemCertificateChain: chain,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Using the parent in CloudCAS.
|
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cert, err = c.client.CreateCertificate(ctx, &pb.CreateCertificateRequest{
|
2021-06-04 02:31:19 +00:00
|
|
|
Parent: parent,
|
2020-10-26 23:43:02 +00:00
|
|
|
CertificateId: id,
|
|
|
|
Certificate: &pb.Certificate{
|
|
|
|
CertificateConfig: &pb.Certificate_PemCsr{
|
|
|
|
PemCsr: csr.PemCsr,
|
|
|
|
},
|
|
|
|
Lifetime: durationpb.New(req.Lifetime),
|
|
|
|
Labels: map[string]string{},
|
2020-10-23 22:04:09 +00:00
|
|
|
},
|
2021-06-09 00:43:52 +00:00
|
|
|
IssuingCertificateAuthorityId: getResourceName(req.Parent.Name),
|
|
|
|
RequestId: req.RequestID,
|
2020-10-26 23:43:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS CreateCertificate failed")
|
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 23:43:02 +00:00
|
|
|
// Activate the intermediate certificate.
|
2020-10-23 22:04:09 +00:00
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
resp, err := c.client.ActivateCertificateAuthority(ctx, &pb.ActivateCertificateAuthorityRequest{
|
|
|
|
Name: name,
|
2020-10-26 23:43:02 +00:00
|
|
|
PemCaCertificate: cert.PemCertificate,
|
2020-10-23 22:04:09 +00:00
|
|
|
SubordinateConfig: &pb.SubordinateConfig{
|
|
|
|
SubordinateConfig: &pb.SubordinateConfig_PemIssuerChain{
|
|
|
|
PemIssuerChain: &pb.SubordinateConfig_SubordinateConfigChain{
|
2020-10-26 23:43:02 +00:00
|
|
|
PemCertificates: cert.PemCertificateChain,
|
2020-10-23 22:04:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RequestId: req.RequestID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS ActivateCertificateAuthority1 failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the long-running operation.
|
|
|
|
ctx, cancel = defaultInitiatorContext()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ca, err := resp.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cloudCAS ActivateCertificateAuthority failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ca, nil
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:26:32 +00:00
|
|
|
func defaultContext() (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
func defaultInitiatorContext() (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
func createCertificateID() (string, error) {
|
2020-09-15 02:13:40 +00:00
|
|
|
id, err := uuid.NewRandomFromReader(rand.Reader)
|
2020-09-10 23:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error creating certificate id")
|
|
|
|
}
|
|
|
|
return id.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:26:32 +00:00
|
|
|
func parseCertificate(pemCert string) (*x509.Certificate, error) {
|
|
|
|
block, _ := pem.Decode([]byte(pemCert))
|
|
|
|
if block == nil {
|
|
|
|
return nil, errors.New("error decoding certificate: not a valid PEM encoded block")
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing certificate")
|
|
|
|
}
|
|
|
|
return cert, nil
|
|
|
|
}
|
2020-09-10 23:19:18 +00:00
|
|
|
|
2020-10-26 23:43:02 +00:00
|
|
|
func parseCertificateRequest(pemCsr string) (*x509.CertificateRequest, error) {
|
|
|
|
block, _ := pem.Decode([]byte(pemCsr))
|
|
|
|
if block == nil {
|
|
|
|
return nil, errors.New("error decoding certificate request: not a valid PEM encoded block")
|
|
|
|
}
|
|
|
|
cr, err := x509.ParseCertificateRequest(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing certificate request")
|
|
|
|
}
|
|
|
|
return cr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeCertificate(cert *x509.Certificate) string {
|
|
|
|
return string(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: cert.Raw,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:19:18 +00:00
|
|
|
func getCertificateAndChain(certpb *pb.Certificate) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
cert, err := parseCertificate(certpb.PemCertificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pemChain := certpb.PemCertificateChain[:len(certpb.PemCertificateChain)-1]
|
|
|
|
chain := make([]*x509.Certificate, len(pemChain))
|
|
|
|
for i := range pemChain {
|
|
|
|
chain[i], err = parseCertificate(pemChain[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, chain, nil
|
2021-06-09 00:43:52 +00:00
|
|
|
}
|
2020-09-10 23:19:18 +00:00
|
|
|
|
2021-06-09 00:43:52 +00:00
|
|
|
// getResourceName returns the last part of a resource.
|
|
|
|
func getResourceName(name string) string {
|
|
|
|
parts := strings.Split(name, "/")
|
|
|
|
return parts[len(parts)-1]
|
2020-09-10 23:19:18 +00:00
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
|
|
|
|
// Normalize a certificate authority name to comply with [a-zA-Z0-9-_].
|
|
|
|
func normalizeCertificateAuthorityName(name string) string {
|
|
|
|
return strings.Map(func(r rune) rune {
|
|
|
|
switch {
|
|
|
|
case r >= 'a' && r <= 'z':
|
|
|
|
return r
|
|
|
|
case r >= 'A' && r <= 'Z':
|
|
|
|
return r
|
|
|
|
case r >= '0' && r <= '9':
|
|
|
|
return r
|
|
|
|
case r == '-':
|
|
|
|
return r
|
|
|
|
case r == '_':
|
|
|
|
return r
|
|
|
|
default:
|
|
|
|
return '-'
|
|
|
|
}
|
|
|
|
}, name)
|
|
|
|
}
|