2021-07-12 16:11:00 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-20 02:28:06 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/sha256"
|
2021-07-12 16:11:00 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-07-20 02:28:06 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-12 16:11:00 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-03-22 02:24:05 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2021-08-06 01:45:50 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
2021-07-20 02:28:06 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
"go.step.sm/crypto/keyutil"
|
|
|
|
"go.step.sm/crypto/tlsutil"
|
|
|
|
"go.step.sm/crypto/x509util"
|
2021-07-12 16:11:00 +00:00
|
|
|
"go.step.sm/linkedca"
|
2021-07-21 01:16:24 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2021-07-12 16:11:00 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
const uuidPattern = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
|
|
|
|
2021-07-12 16:11:00 +00:00
|
|
|
type linkedCaClient struct {
|
2021-07-20 02:28:06 +00:00
|
|
|
renewer *tlsutil.Renewer
|
2021-07-12 16:11:00 +00:00
|
|
|
client linkedca.MajordomoClient
|
|
|
|
authorityID string
|
|
|
|
}
|
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
type linkedCAClaims struct {
|
|
|
|
jose.Claims
|
|
|
|
SANs []string `json:"sans"`
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLinkedCAClient(token string) (*linkedCaClient, error) {
|
|
|
|
tok, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
2021-07-12 16:11:00 +00:00
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
var claims linkedCAClaims
|
|
|
|
if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
// Validate claims
|
|
|
|
if len(claims.Audience) != 1 {
|
|
|
|
return nil, errors.New("error parsing token: invalid aud claim")
|
|
|
|
}
|
|
|
|
if claims.SHA == "" {
|
|
|
|
return nil, errors.New("error parsing token: invalid sha claim")
|
|
|
|
}
|
|
|
|
// Get linkedCA endpoint from audience.
|
|
|
|
u, err := url.Parse(claims.Audience[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("error parsing token: invalid aud claim")
|
|
|
|
}
|
|
|
|
// Get authority from SANs
|
|
|
|
authority, err := getAuthority(claims.SANs)
|
2021-07-12 16:11:00 +00:00
|
|
|
if err != nil {
|
2021-07-20 02:28:06 +00:00
|
|
|
return nil, err
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
2021-07-20 02:28:06 +00:00
|
|
|
|
|
|
|
// Create csr to login with
|
|
|
|
signer, err := keyutil.GenerateDefaultSigner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
csr, err := x509util.CreateCertificateRequest(claims.Subject, claims.SANs, signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and verify root certificate
|
|
|
|
root, err := getRootCertificate(u.Host, claims.SHA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:11:00 +00:00
|
|
|
pool := x509.NewCertPool()
|
2021-07-20 02:28:06 +00:00
|
|
|
pool.AddCert(root)
|
|
|
|
|
|
|
|
// Login with majordomo and get certificates
|
|
|
|
cert, tlsConfig, err := login(authority, token, csr, signer, u.Host, pool)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
// Start TLS renewer and set the GetClientCertificate callback to it.
|
|
|
|
renewer, err := tlsutil.NewRenewer(cert, tlsConfig, func() (*tls.Certificate, *tls.Config, error) {
|
|
|
|
return login(authority, token, csr, signer, u.Host, pool)
|
|
|
|
})
|
2021-07-12 16:11:00 +00:00
|
|
|
if err != nil {
|
2021-07-20 02:28:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig.GetClientCertificate = renewer.GetClientCertificate
|
|
|
|
|
|
|
|
// Start mTLS client
|
|
|
|
conn, err := grpc.Dial(u.Host, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error connecting %s", u.Host)
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &linkedCaClient{
|
2021-07-20 02:28:06 +00:00
|
|
|
renewer: renewer,
|
2021-07-12 16:11:00 +00:00
|
|
|
client: linkedca.NewMajordomoClient(conn),
|
2021-07-20 02:28:06 +00:00
|
|
|
authorityID: authority,
|
2021-07-12 16:11:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
func (c *linkedCaClient) Run() {
|
|
|
|
c.renewer.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) Stop() {
|
|
|
|
c.renewer.Stop()
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:11:00 +00:00
|
|
|
func (c *linkedCaClient) CreateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
|
|
|
|
resp, err := c.client.CreateProvisioner(ctx, &linkedca.CreateProvisionerRequest{
|
|
|
|
Type: prov.Type,
|
|
|
|
Name: prov.Name,
|
|
|
|
Details: prov.Details,
|
|
|
|
Claims: prov.Claims,
|
|
|
|
X509Template: prov.X509Template,
|
|
|
|
SshTemplate: prov.SshTemplate,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating provisioner")
|
|
|
|
}
|
|
|
|
prov.Id = resp.Id
|
|
|
|
prov.AuthorityId = resp.AuthorityId
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) GetProvisioner(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
2021-08-02 21:45:59 +00:00
|
|
|
resp, err := c.client.GetProvisioner(ctx, &linkedca.GetProvisionerRequest{
|
|
|
|
Id: id,
|
2021-07-12 16:11:00 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting provisioners")
|
|
|
|
}
|
2021-08-02 21:45:59 +00:00
|
|
|
return resp, nil
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) GetProvisioners(ctx context.Context) ([]*linkedca.Provisioner, error) {
|
2022-03-23 21:56:39 +00:00
|
|
|
resp, err := c.GetConfiguration(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Provisioners, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) GetConfiguration(ctx context.Context) (*linkedca.ConfigurationResponse, error) {
|
2021-07-12 16:11:00 +00:00
|
|
|
resp, err := c.client.GetConfiguration(ctx, &linkedca.ConfigurationRequest{
|
|
|
|
AuthorityId: c.authorityID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-03-23 21:56:39 +00:00
|
|
|
return nil, errors.Wrap(err, "error getting configuration")
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
2022-03-23 21:56:39 +00:00
|
|
|
return resp, nil
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) UpdateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
|
|
|
|
_, err := c.client.UpdateProvisioner(ctx, &linkedca.UpdateProvisionerRequest{
|
|
|
|
Id: prov.Id,
|
|
|
|
Name: prov.Name,
|
|
|
|
Details: prov.Details,
|
|
|
|
Claims: prov.Claims,
|
|
|
|
X509Template: prov.X509Template,
|
|
|
|
SshTemplate: prov.SshTemplate,
|
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error updating provisioner")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) DeleteProvisioner(ctx context.Context, id string) error {
|
|
|
|
_, err := c.client.DeleteProvisioner(ctx, &linkedca.DeleteProvisionerRequest{
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error deleting provisioner")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) CreateAdmin(ctx context.Context, adm *linkedca.Admin) error {
|
|
|
|
resp, err := c.client.CreateAdmin(ctx, &linkedca.CreateAdminRequest{
|
|
|
|
Subject: adm.Subject,
|
|
|
|
ProvisionerId: adm.ProvisionerId,
|
|
|
|
Type: adm.Type,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating admin")
|
|
|
|
}
|
|
|
|
adm.Id = resp.Id
|
|
|
|
adm.AuthorityId = resp.AuthorityId
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) GetAdmin(ctx context.Context, id string) (*linkedca.Admin, error) {
|
2021-08-02 21:45:59 +00:00
|
|
|
resp, err := c.client.GetAdmin(ctx, &linkedca.GetAdminRequest{
|
|
|
|
Id: id,
|
2021-07-12 16:11:00 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting admins")
|
|
|
|
}
|
2021-08-02 21:45:59 +00:00
|
|
|
return resp, nil
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) GetAdmins(ctx context.Context) ([]*linkedca.Admin, error) {
|
2022-03-23 21:56:39 +00:00
|
|
|
resp, err := c.GetConfiguration(ctx)
|
2021-07-12 16:11:00 +00:00
|
|
|
if err != nil {
|
2022-03-23 21:56:39 +00:00
|
|
|
return nil, err
|
2021-07-12 16:11:00 +00:00
|
|
|
}
|
|
|
|
return resp.Admins, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) UpdateAdmin(ctx context.Context, adm *linkedca.Admin) error {
|
|
|
|
_, err := c.client.UpdateAdmin(ctx, &linkedca.UpdateAdminRequest{
|
|
|
|
Id: adm.Id,
|
|
|
|
Type: adm.Type,
|
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error updating admin")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) DeleteAdmin(ctx context.Context, id string) error {
|
|
|
|
_, err := c.client.DeleteAdmin(ctx, &linkedca.DeleteAdminRequest{
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error deleting admin")
|
|
|
|
}
|
2021-07-20 02:28:06 +00:00
|
|
|
|
2022-03-22 02:24:05 +00:00
|
|
|
func (c *linkedCaClient) StoreCertificateChain(prov provisioner.Interface, fullchain ...*x509.Certificate) error {
|
2021-07-21 01:16:24 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := c.client.PostCertificate(ctx, &linkedca.CertificateRequest{
|
|
|
|
PemCertificate: serializeCertificateChain(fullchain[0]),
|
|
|
|
PemCertificateChain: serializeCertificateChain(fullchain[1:]...),
|
2022-03-22 02:24:05 +00:00
|
|
|
Provisioner: createProvisionerIdentity(prov),
|
2021-07-21 01:16:24 +00:00
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error posting certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) StoreRenewedCertificate(parent *x509.Certificate, fullchain ...*x509.Certificate) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := c.client.PostCertificate(ctx, &linkedca.CertificateRequest{
|
|
|
|
PemCertificate: serializeCertificateChain(fullchain[0]),
|
|
|
|
PemCertificateChain: serializeCertificateChain(fullchain[1:]...),
|
|
|
|
PemParentCertificate: serializeCertificateChain(parent),
|
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error posting certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) StoreSSHCertificate(crt *ssh.Certificate) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := c.client.PostSSHCertificate(ctx, &linkedca.SSHCertificateRequest{
|
2021-07-26 21:31:42 +00:00
|
|
|
Certificate: string(ssh.MarshalAuthorizedKey(crt)),
|
2021-07-21 01:16:24 +00:00
|
|
|
})
|
|
|
|
return errors.Wrap(err, "error posting ssh certificate")
|
|
|
|
}
|
|
|
|
|
2021-08-06 01:45:50 +00:00
|
|
|
func (c *linkedCaClient) Revoke(crt *x509.Certificate, rci *db.RevokedCertificateInfo) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := c.client.RevokeCertificate(ctx, &linkedca.RevokeCertificateRequest{
|
|
|
|
Serial: rci.Serial,
|
|
|
|
PemCertificate: serializeCertificate(crt),
|
|
|
|
Reason: rci.Reason,
|
|
|
|
ReasonCode: linkedca.RevocationReasonCode(rci.ReasonCode),
|
|
|
|
Passive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return errors.Wrap(err, "error revoking certificate")
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func (c *linkedCaClient) RevokeSSH(cert *ssh.Certificate, rci *db.RevokedCertificateInfo) error {
|
2021-08-06 01:45:50 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := c.client.RevokeSSHCertificate(ctx, &linkedca.RevokeSSHCertificateRequest{
|
|
|
|
Serial: rci.Serial,
|
2021-10-08 18:59:57 +00:00
|
|
|
Certificate: serializeSSHCertificate(cert),
|
2021-08-06 01:45:50 +00:00
|
|
|
Reason: rci.Reason,
|
|
|
|
ReasonCode: linkedca.RevocationReasonCode(rci.ReasonCode),
|
|
|
|
Passive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return errors.Wrap(err, "error revoking ssh certificate")
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:10:13 +00:00
|
|
|
func (c *linkedCaClient) IsRevoked(serial string) (bool, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := c.client.GetCertificateStatus(ctx, &linkedca.GetCertificateStatusRequest{
|
|
|
|
Serial: serial,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "error getting certificate status")
|
|
|
|
}
|
|
|
|
return resp.Status != linkedca.RevocationStatus_ACTIVE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linkedCaClient) IsSSHRevoked(serial string) (bool, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := c.client.GetSSHCertificateStatus(ctx, &linkedca.GetSSHCertificateStatusRequest{
|
|
|
|
Serial: serial,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "error getting certificate status")
|
|
|
|
}
|
|
|
|
return resp.Status != linkedca.RevocationStatus_ACTIVE, nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 02:24:05 +00:00
|
|
|
func createProvisionerIdentity(prov provisioner.Interface) *linkedca.ProvisionerIdentity {
|
|
|
|
if prov == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &linkedca.ProvisionerIdentity{
|
|
|
|
Id: prov.GetID(),
|
|
|
|
Type: linkedca.Provisioner_Type(prov.GetType()),
|
|
|
|
Name: prov.GetName(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 01:45:50 +00:00
|
|
|
func serializeCertificate(crt *x509.Certificate) string {
|
|
|
|
if crt == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: crt.Raw,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-07-21 01:16:24 +00:00
|
|
|
func serializeCertificateChain(fullchain ...*x509.Certificate) string {
|
|
|
|
var chain string
|
|
|
|
for _, crt := range fullchain {
|
|
|
|
chain += string(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: crt.Raw,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2021-08-06 01:45:50 +00:00
|
|
|
func serializeSSHCertificate(crt *ssh.Certificate) string {
|
|
|
|
if crt == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(ssh.MarshalAuthorizedKey(crt))
|
|
|
|
}
|
|
|
|
|
2021-07-20 02:28:06 +00:00
|
|
|
func getAuthority(sans []string) (string, error) {
|
|
|
|
for _, s := range sans {
|
|
|
|
if strings.HasPrefix(s, "urn:smallstep:authority:") {
|
|
|
|
if regexp.MustCompile(uuidPattern).MatchString(s[24:]) {
|
|
|
|
return s[24:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("error parsing token: invalid sans claim")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRootCertificate creates an insecure majordomo client and returns the
|
|
|
|
// verified root certificate.
|
|
|
|
func getRootCertificate(endpoint, fingerprint string) (*x509.Certificate, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
})))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error connecting %s", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client := linkedca.NewMajordomoClient(conn)
|
|
|
|
resp, err := client.GetRootCertificate(ctx, &linkedca.GetRootCertificateRequest{
|
|
|
|
Fingerprint: fingerprint,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting root certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var block *pem.Block
|
|
|
|
b := []byte(resp.PemCertificate)
|
|
|
|
for len(b) > 0 {
|
|
|
|
block, b = pem.Decode(b)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the sha256
|
|
|
|
sum := sha256.Sum256(cert.Raw)
|
|
|
|
if !strings.EqualFold(fingerprint, hex.EncodeToString(sum[:])) {
|
|
|
|
return nil, fmt.Errorf("error verifying certificate: SHA256 fingerprint does not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error getting root certificate: certificate not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// login creates a new majordomo client with just the root ca pool and returns
|
|
|
|
// the signed certificate and tls configuration.
|
|
|
|
func login(authority, token string, csr *x509.CertificateRequest, signer crypto.PrivateKey, endpoint string, rootCAs *x509.CertPool) (*tls.Certificate, *tls.Config, error) {
|
|
|
|
// Connect to majordomo
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
})))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "error connecting %s", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login to get the signed certificate
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client := linkedca.NewMajordomoClient(conn)
|
|
|
|
resp, err := client.Login(ctx, &linkedca.LoginRequest{
|
|
|
|
AuthorityId: authority,
|
|
|
|
Token: token,
|
|
|
|
PemCertificateRequest: string(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE REQUEST",
|
|
|
|
Bytes: csr.Raw,
|
|
|
|
})),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "error logging in %s", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse login response
|
|
|
|
var block *pem.Block
|
|
|
|
var bundle []*x509.Certificate
|
|
|
|
rest := []byte(resp.PemCertificateChain)
|
|
|
|
for {
|
|
|
|
block, rest = pem.Decode(rest)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.Type != "CERTIFICATE" {
|
|
|
|
return nil, nil, errors.New("error decoding login response: pemCertificateChain is not a certificate bundle")
|
|
|
|
}
|
|
|
|
crt, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing login response")
|
|
|
|
}
|
|
|
|
bundle = append(bundle, crt)
|
|
|
|
}
|
|
|
|
if len(bundle) == 0 {
|
|
|
|
return nil, nil, errors.New("error decoding login response: pemCertificateChain should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build tls.Certificate with PemCertificate and intermediates in the
|
|
|
|
// PemCertificateChain
|
|
|
|
cert := &tls.Certificate{
|
|
|
|
PrivateKey: signer,
|
|
|
|
}
|
|
|
|
rest = []byte(resp.PemCertificate)
|
|
|
|
for {
|
|
|
|
block, rest = pem.Decode(rest)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.Type == "CERTIFICATE" {
|
|
|
|
leaf, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing pemCertificate")
|
|
|
|
}
|
|
|
|
cert.Certificate = append(cert.Certificate, block.Bytes)
|
|
|
|
cert.Leaf = leaf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add intermediates to the tls.Certificate
|
|
|
|
last := len(bundle) - 1
|
|
|
|
for i := 0; i < last; i++ {
|
|
|
|
cert.Certificate = append(cert.Certificate, bundle[i].Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add root to the pool if it's not there yet
|
|
|
|
rootCAs.AddCert(bundle[last])
|
|
|
|
|
|
|
|
return cert, &tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
}, nil
|
|
|
|
}
|