| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 4 | package rpc |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "crypto/ed25519" |
| 9 | "crypto/rand" |
| 10 | "crypto/tls" |
| 11 | "crypto/x509" |
| 12 | "fmt" |
| 13 | "math/big" |
| 14 | "time" |
| 15 | |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 16 | "google.golang.org/grpc/credentials" |
| Serge Bazanski | 636032e | 2022-01-26 14:21:33 +0100 | [diff] [blame] | 17 | "google.golang.org/grpc/status" |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 18 | |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 19 | "source.monogon.dev/metropolis/node/core/identity" |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 20 | apb "source.monogon.dev/metropolis/proto/api" |
| 21 | ) |
| 22 | |
| Serge Bazanski | 4ac7112 | 2023-07-24 13:08:34 +0200 | [diff] [blame] | 23 | // UnknownNotAfter is a copy of //metroplis/pkg/pki.UnknownNotAfter. |
| 24 | // |
| 25 | // We copy it so that we can decouple the rpc package from the pki package, the |
| 26 | // former being used by metroctl (and thus needing to be portable), the latter |
| 27 | // having a dependency on fileargs (which isn't portable). The correct solution |
| 28 | // here is to clarify portability policy of each workspace path, and apply it. |
| 29 | // But this will do for now. |
| 30 | // |
| 31 | // TODO(issues/252): clean up and merge this back. |
| 32 | var UnknownNotAfter = time.Unix(253402300799, 0) |
| 33 | |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 34 | type verifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error |
| 35 | |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 36 | func verifyClusterCertificateAndNodeID(ca *x509.Certificate, nodeID string) verifyPeerCertificate { |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 37 | return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { |
| 38 | if len(rawCerts) != 1 { |
| 39 | return fmt.Errorf("server presented %d certificates, wanted exactly one", len(rawCerts)) |
| 40 | } |
| 41 | serverCert, err := x509.ParseCertificate(rawCerts[0]) |
| 42 | if err != nil { |
| 43 | return fmt.Errorf("server presented unparseable certificate: %w", err) |
| 44 | } |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame] | 45 | id, err := identity.VerifyNodeInCluster(serverCert, ca) |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 46 | if err != nil { |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 47 | return fmt.Errorf("node certificate verification failed: %w", err) |
| 48 | } |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame] | 49 | if nodeID != "" && id != nodeID { |
| 50 | return fmt.Errorf("wanted to reach node %q, got %q", nodeID, id) |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 51 | } |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 52 | |
| 53 | return nil |
| 54 | } |
| 55 | } |
| 56 | |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 57 | func verifyFail(err error) verifyPeerCertificate { |
| 58 | return func(_ [][]byte, _ [][]*x509.Certificate) error { |
| 59 | return err |
| 60 | } |
| 61 | } |
| 62 | |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 63 | // NewEphemeralCredentials returns gRPC TransportCredentials that can be used to |
| 64 | // dial a cluster without authenticating with a certificate, but instead |
| 65 | // authenticating by proving the possession of a private key, via an ephemeral |
| 66 | // self-signed certificate. |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 67 | // |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 68 | // Currently these credentials are used in two flows: |
| 69 | // |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 70 | // 1. Registration of nodes into a cluster, after which a node receives a proper |
| 71 | // node certificate |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 72 | // |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 73 | // 2. Escrow of initial owner credentials into a proper manager |
| 74 | // certificate |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 75 | // |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 76 | // The given opts can be used to lock down the remote side of the connection, eg. |
| 77 | // expecting a given cluster CA certificate or disabling remote side verification |
| 78 | // by using WantInsecure(). |
| 79 | func NewEphemeralCredentials(private ed25519.PrivateKey, opts ...CredentialsOpt) (credentials.TransportCredentials, error) { |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 80 | template := x509.Certificate{ |
| 81 | SerialNumber: big.NewInt(1), |
| 82 | NotBefore: time.Now(), |
| Serge Bazanski | 4ac7112 | 2023-07-24 13:08:34 +0200 | [diff] [blame] | 83 | NotAfter: UnknownNotAfter, |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 84 | |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 85 | KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 86 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 87 | BasicConstraintsValid: true, |
| 88 | } |
| 89 | certificateBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, private.Public(), private) |
| 90 | if err != nil { |
| 91 | return nil, fmt.Errorf("when generating self-signed certificate: %w", err) |
| 92 | } |
| 93 | certificate := tls.Certificate{ |
| 94 | Certificate: [][]byte{certificateBytes}, |
| 95 | PrivateKey: private, |
| 96 | } |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 97 | return NewAuthenticatedCredentials(certificate, opts...), nil |
| 98 | } |
| 99 | |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 100 | // CredentialsOpt are created using WantXXX functions and used in |
| 101 | // NewCredentials. |
| 102 | type CredentialsOpt struct { |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 103 | wantCA *x509.Certificate |
| 104 | wantNodeID string |
| 105 | insecureOkay bool |
| 106 | } |
| 107 | |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 108 | func (a *CredentialsOpt) merge(o *CredentialsOpt) { |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 109 | if a.wantNodeID == "" && o.wantNodeID != "" { |
| 110 | a.wantNodeID = o.wantNodeID |
| 111 | } |
| 112 | if a.wantCA == nil && o.wantCA != nil { |
| 113 | a.wantCA = o.wantCA |
| 114 | } |
| 115 | if !a.insecureOkay && o.insecureOkay { |
| 116 | a.insecureOkay = o.insecureOkay |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // WantRemoteCluster enables the verification of the remote cluster identity when |
| 121 | // using NewAuthanticatedCredentials. If the connection is not terminated at a |
| 122 | // cluster with the given CA certificate, an error will be returned. |
| 123 | // |
| 124 | // This is the bare minimum option required to implement secure connections to |
| 125 | // clusters. |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 126 | func WantRemoteCluster(ca *x509.Certificate) CredentialsOpt { |
| 127 | return CredentialsOpt{ |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 128 | wantCA: ca, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // WantRemoteNode enables the verification of the remote node identity when using |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 133 | // NewCredentials. If the connection is not terminated at the node |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 134 | // ID 'id', an error will be returned. For this function to work, |
| 135 | // WantRemoteCluster must also be set. |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 136 | func WantRemoteNode(id string) CredentialsOpt { |
| 137 | return CredentialsOpt{ |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 138 | wantNodeID: id, |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // WantInsecure disables the verification of the remote side of the connection |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 143 | // via NewCredentials. This is unsafe. |
| 144 | func WantInsecure() CredentialsOpt { |
| 145 | return CredentialsOpt{ |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 146 | insecureOkay: true, |
| 147 | } |
| Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 148 | } |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 149 | |
| Serge Bazanski | a3e38cf | 2024-07-31 14:40:04 +0000 | [diff] [blame] | 150 | // NewAuthenticatedTLSConfig returns a tls.Config that can be used to dial a |
| 151 | // cluster with a given TLS certificate (from node or manager credentials). |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 152 | // |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 153 | // The provided CredentialsOpt specify the verification of the remote side of the |
| 154 | // connection. When connecting to a cluster (any node), use WantRemoteCluster. If |
| 155 | // you also want to verify the connection to a particular node, specify |
| 156 | // WantRemoteNode alongside it. If no verification should be performed use |
| 157 | // WantInsecure. |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 158 | // |
| 159 | // The given options are parsed on a first-wins basis. |
| Serge Bazanski | a3e38cf | 2024-07-31 14:40:04 +0000 | [diff] [blame] | 160 | func NewAuthenticatedTLSConfig(cert tls.Certificate, opts ...CredentialsOpt) *tls.Config { |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 161 | config := &tls.Config{ |
| 162 | Certificates: []tls.Certificate{cert}, |
| 163 | InsecureSkipVerify: true, |
| 164 | } |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 165 | |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 166 | var merged CredentialsOpt |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 167 | for _, o := range opts { |
| 168 | merged.merge(&o) |
| Serge Bazanski | 399ce55 | 2022-03-29 12:52:42 +0200 | [diff] [blame] | 169 | } |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 170 | |
| 171 | if merged.insecureOkay { |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 172 | if merged.wantNodeID != "" { |
| 173 | config.VerifyPeerCertificate = verifyFail(fmt.Errorf("WantInsecure specified alongside WantRemoteNode")) |
| 174 | } else if merged.wantCA != nil { |
| 175 | config.VerifyPeerCertificate = verifyFail(fmt.Errorf("WantInsecure specified alongside WantRemoteCluster")) |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 176 | } |
| 177 | } else { |
| 178 | switch { |
| Serge Bazanski | 0c28015 | 2024-02-05 14:33:19 +0100 | [diff] [blame] | 179 | case merged.wantNodeID == "" && merged.wantCA == nil: |
| 180 | config.VerifyPeerCertificate = verifyFail(fmt.Errorf("WantRemoteNode/WantRemoteCluster/WantInsecure not specified")) |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 181 | case merged.wantNodeID != "" && merged.wantCA == nil: |
| 182 | config.VerifyPeerCertificate = verifyFail(fmt.Errorf("WantRemoteNode also requires WantRemoteCluster")) |
| 183 | case merged.wantCA == nil: |
| 184 | config.VerifyPeerCertificate = verifyFail(fmt.Errorf("no AuthenticaedCreentialsOpts specified")) |
| 185 | default: |
| 186 | config.VerifyPeerCertificate = verifyClusterCertificateAndNodeID(merged.wantCA, merged.wantNodeID) |
| 187 | } |
| 188 | } |
| 189 | |
| Serge Bazanski | a3e38cf | 2024-07-31 14:40:04 +0000 | [diff] [blame] | 190 | return config |
| 191 | } |
| 192 | |
| 193 | // NewAuthenticatedCredentials returns gRPC TransportCredentials that can be used |
| 194 | // to dial a cluster with a given TLS certificate (from node or manager |
| 195 | // credentials). |
| 196 | // |
| 197 | // The provided CredentialsOpt specify the verification of the remote side of the |
| 198 | // connection. When connecting to a cluster (any node), use WantRemoteCluster. If |
| 199 | // you also want to verify the connection to a particular node, specify |
| 200 | // WantRemoteNode alongside it. If no verification should be performed use |
| 201 | // WantInsecure. |
| 202 | // |
| 203 | // The given options are parsed on a first-wins basis. |
| 204 | func NewAuthenticatedCredentials(cert tls.Certificate, opts ...CredentialsOpt) credentials.TransportCredentials { |
| 205 | return credentials.NewTLS(NewAuthenticatedTLSConfig(cert, opts...)) |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| Serge Bazanski | 8535cb5 | 2023-03-29 14:15:08 +0200 | [diff] [blame] | 208 | // RetrieveOwnerCertificate uses AAA.Escrow to retrieve a cluster manager |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 209 | // certificate for the initial owner of the cluster, authenticated by the |
| 210 | // public/private key set in the clusters NodeParameters.ClusterBoostrap. |
| 211 | // |
| 212 | // The retrieved certificate can be used to dial further cluster RPCs. |
| 213 | func RetrieveOwnerCertificate(ctx context.Context, aaa apb.AAAClient, private ed25519.PrivateKey) (*tls.Certificate, error) { |
| 214 | srv, err := aaa.Escrow(ctx) |
| 215 | if err != nil { |
| Serge Bazanski | 636032e | 2022-01-26 14:21:33 +0100 | [diff] [blame] | 216 | if st, ok := status.FromError(err); ok { |
| 217 | return nil, status.Errorf(st.Code(), "Escrow call failed: %s", st.Message()) |
| 218 | } |
| 219 | return nil, err |
| Serge Bazanski | d7d6e02 | 2021-09-01 15:03:06 +0200 | [diff] [blame] | 220 | } |
| 221 | if err := srv.Send(&apb.EscrowFromClient{ |
| 222 | Parameters: &apb.EscrowFromClient_Parameters{ |
| 223 | RequestedIdentityName: "owner", |
| 224 | PublicKey: private.Public().(ed25519.PublicKey), |
| 225 | }, |
| 226 | }); err != nil { |
| 227 | return nil, fmt.Errorf("when sending client parameters: %w", err) |
| 228 | } |
| 229 | resp, err := srv.Recv() |
| 230 | if err != nil { |
| 231 | return nil, fmt.Errorf("when receiving server message: %w", err) |
| 232 | } |
| 233 | if len(resp.EmittedCertificate) == 0 { |
| 234 | return nil, fmt.Errorf("expected certificate, instead got needed proofs: %+v", resp.Needed) |
| 235 | } |
| 236 | |
| 237 | return &tls.Certificate{ |
| 238 | Certificate: [][]byte{resp.EmittedCertificate}, |
| 239 | PrivateKey: private, |
| 240 | }, nil |
| 241 | } |