google.golang.org/grpc@v1.74.2/internal/credentials/spiffe/spiffe.go (about)

     1  /*
     2   *
     3   * Copyright 2025 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package spiffe defines APIs for working with SPIFFE Bundle Maps.
    20  //
    21  // All APIs in this package are experimental.
    22  package spiffe
    23  
    24  import (
    25  	"crypto/x509"
    26  	"encoding/json"
    27  	"fmt"
    28  
    29  	"github.com/spiffe/go-spiffe/v2/bundle/spiffebundle"
    30  	"github.com/spiffe/go-spiffe/v2/spiffeid"
    31  )
    32  
    33  type partialParsedSPIFFEBundleMap struct {
    34  	Bundles map[string]json.RawMessage `json:"trust_domains"`
    35  }
    36  
    37  // BundleMapFromBytes parses bytes into a SPIFFE Bundle Map. See the
    38  // SPIFFE Bundle Map spec for more detail -
    39  // https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE_Trust_Domain_and_Bundle.md#4-spiffe-bundle-format
    40  // If duplicate keys are encountered in the JSON parsing, Go's default unmarshal
    41  // behavior occurs which causes the last processed entry to be the entry in the
    42  // parsed map.
    43  func BundleMapFromBytes(bundleMapBytes []byte) (map[string]*spiffebundle.Bundle, error) {
    44  	var result partialParsedSPIFFEBundleMap
    45  	if err := json.Unmarshal(bundleMapBytes, &result); err != nil {
    46  		return nil, err
    47  	}
    48  	if result.Bundles == nil {
    49  		return nil, fmt.Errorf("spiffe: BundleMapFromBytes() no bundles parsed from spiffe bundle map bytes")
    50  	}
    51  	bundleMap := map[string]*spiffebundle.Bundle{}
    52  	for td, jsonBundle := range result.Bundles {
    53  		trustDomain, err := spiffeid.TrustDomainFromString(td)
    54  		if err != nil {
    55  			return nil, fmt.Errorf("spiffe: BundleMapFromBytes() invalid trust domain %q found when parsing SPIFFE Bundle Map: %v", td, err)
    56  		}
    57  		bundle, err := spiffebundle.Parse(trustDomain, jsonBundle)
    58  		if err != nil {
    59  			return nil, fmt.Errorf("spiffe: BundleMapFromBytes() failed to parse bundle for trust domain %q: %v", td, err)
    60  		}
    61  		bundleMap[td] = bundle
    62  	}
    63  	return bundleMap, nil
    64  }
    65  
    66  // GetRootsFromSPIFFEBundleMap returns the root trust certificates from the
    67  // SPIFFE bundle map for the given trust domain from the leaf certificate.
    68  func GetRootsFromSPIFFEBundleMap(bundleMap map[string]*spiffebundle.Bundle, leafCert *x509.Certificate) (*x509.CertPool, error) {
    69  	// 1. Upon receiving a peer certificate, verify that it is a well-formed SPIFFE
    70  	//    leaf certificate.  In particular, it must have a single URI SAN containing
    71  	//    a well-formed SPIFFE ID ([SPIFFE ID format]).
    72  	spiffeID, err := idFromCert(leafCert)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("spiffe: could not get spiffe ID from peer leaf cert but verification with spiffe trust map was configured: %v", err)
    75  	}
    76  
    77  	// 2. Use the trust domain in the peer certificate's SPIFFE ID to lookup
    78  	//    the SPIFFE trust bundle. If the trust domain is not contained in the
    79  	//    configured trust map, reject the certificate.
    80  	spiffeBundle, ok := bundleMap[spiffeID.TrustDomain().Name()]
    81  	if !ok {
    82  		return nil, fmt.Errorf("spiffe: no bundle found for peer certificates trust domain %q but verification with a SPIFFE trust map was configured", spiffeID.TrustDomain().Name())
    83  	}
    84  	roots := spiffeBundle.X509Authorities()
    85  	rootPool := x509.NewCertPool()
    86  	for _, root := range roots {
    87  		rootPool.AddCert(root)
    88  	}
    89  	return rootPool, nil
    90  }
    91  
    92  // idFromCert parses the SPIFFE ID from the x509.Certificate. If the certificate
    93  // does not have a valid SPIFFE ID, returns an error.
    94  func idFromCert(cert *x509.Certificate) (*spiffeid.ID, error) {
    95  	if cert == nil {
    96  		return nil, fmt.Errorf("input cert is nil")
    97  	}
    98  	// A valid SPIFFE Certificate should have exactly one URI.
    99  	if len(cert.URIs) != 1 {
   100  		return nil, fmt.Errorf("input cert has %v URIs but should have 1", len(cert.URIs))
   101  	}
   102  	id, err := spiffeid.FromURI(cert.URIs[0])
   103  	if err != nil {
   104  		return nil, fmt.Errorf("invalid spiffeid: %v", err)
   105  	}
   106  	return &id, nil
   107  }