|
| 1 | +// |
| 2 | +// Copyright 2024 The Sigstore Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package trustedroot |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "crypto" |
| 21 | + "crypto/sha256" |
| 22 | + "crypto/x509" |
| 23 | + "encoding/base64" |
| 24 | + "encoding/pem" |
| 25 | + "errors" |
| 26 | + "fmt" |
| 27 | + "os" |
| 28 | + |
| 29 | + "github.com/sigstore/sigstore-go/pkg/root" |
| 30 | + |
| 31 | + "github.com/sigstore/cosign/v2/cmd/cosign/cli/rekor" |
| 32 | + "github.com/sigstore/cosign/v2/internal/ui" |
| 33 | +) |
| 34 | + |
| 35 | +type TrustedRootCreateCmd struct { |
| 36 | + CAIntermediates string |
| 37 | + CARoots string |
| 38 | + CertChain string |
| 39 | + Out string |
| 40 | + RekorURL string |
| 41 | + TSACertChainPath string |
| 42 | +} |
| 43 | + |
| 44 | +func (c *TrustedRootCreateCmd) Exec(ctx context.Context) error { |
| 45 | + var fulcioCertAuthorities []root.CertificateAuthority |
| 46 | + var timestampAuthorities []root.CertificateAuthority |
| 47 | + rekorTransparencyLogs := make(map[string]*root.TransparencyLog) |
| 48 | + |
| 49 | + if c.CertChain != "" { |
| 50 | + fulcioAuthority, err := parsePEMFile(c.CertChain) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + fulcioCertAuthorities = append(fulcioCertAuthorities, *fulcioAuthority) |
| 55 | + |
| 56 | + } else if c.CARoots != "" { |
| 57 | + roots, err := parseCerts(c.CARoots) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + var intermediates []*x509.Certificate |
| 63 | + if c.CAIntermediates != "" { |
| 64 | + intermediates, err = parseCerts(c.CAIntermediates) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Here we're trying to "flatten" the x509.CertPool cosign was using |
| 71 | + // into a trusted root with a clear mapping between roots and |
| 72 | + // intermediates. Make a guess that if there are intermediates, there |
| 73 | + // is one per root. |
| 74 | + |
| 75 | + for i, rootCert := range roots { |
| 76 | + var fulcioAuthority root.CertificateAuthority |
| 77 | + fulcioAuthority.Root = rootCert |
| 78 | + if i < len(intermediates) { |
| 79 | + fulcioAuthority.Intermediates = []*x509.Certificate{intermediates[i]} |
| 80 | + } |
| 81 | + fulcioCertAuthorities = append(fulcioCertAuthorities, fulcioAuthority) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if c.RekorURL != "" { |
| 86 | + rekorClient, err := rekor.NewClient(c.RekorURL) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("creating Rekor client: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + rekorPubKey, err := rekorClient.Pubkey.GetPublicKey(nil) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + block, _ := pem.Decode([]byte(rekorPubKey.Payload)) |
| 97 | + if block == nil { |
| 98 | + return errors.New("failed to decode public key of server") |
| 99 | + } |
| 100 | + |
| 101 | + pub, err := x509.ParsePKIXPublicKey(block.Bytes) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + keyHash := sha256.Sum256(block.Bytes) |
| 107 | + keyID := base64.StdEncoding.EncodeToString(keyHash[:]) |
| 108 | + |
| 109 | + rekorTransparencyLog := root.TransparencyLog{ |
| 110 | + BaseURL: c.RekorURL, |
| 111 | + HashFunc: crypto.Hash(crypto.SHA256), |
| 112 | + ID: keyHash[:], |
| 113 | + PublicKey: pub, |
| 114 | + SignatureHashFunc: crypto.Hash(crypto.SHA256), |
| 115 | + } |
| 116 | + |
| 117 | + rekorTransparencyLogs[keyID] = &rekorTransparencyLog |
| 118 | + } |
| 119 | + |
| 120 | + if c.TSACertChainPath != "" { |
| 121 | + timestampAuthority, err := parsePEMFile(c.TSACertChainPath) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + timestampAuthorities = append(timestampAuthorities, *timestampAuthority) |
| 126 | + } |
| 127 | + |
| 128 | + newTrustedRoot, err := root.NewTrustedRoot(root.TrustedRootMediaType01, |
| 129 | + fulcioCertAuthorities, nil, timestampAuthorities, rekorTransparencyLogs, |
| 130 | + ) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + var trBytes []byte |
| 136 | + |
| 137 | + trBytes, err = newTrustedRoot.MarshalJSON() |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + if c.Out != "" { |
| 143 | + err = os.WriteFile(c.Out, trBytes, 0640) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } else { |
| 148 | + ui.Infof(ctx, string(trBytes)) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func parsePEMFile(path string) (*root.CertificateAuthority, error) { |
| 155 | + certs, err := parseCerts(path) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + |
| 160 | + var ca root.CertificateAuthority |
| 161 | + ca.Root = certs[len(certs)-1] |
| 162 | + if len(certs) > 1 { |
| 163 | + ca.Intermediates = certs[:len(certs)-1] |
| 164 | + } |
| 165 | + |
| 166 | + return &ca, nil |
| 167 | +} |
| 168 | + |
| 169 | +func parseCerts(path string) ([]*x509.Certificate, error) { |
| 170 | + var certs []*x509.Certificate |
| 171 | + |
| 172 | + contents, err := os.ReadFile(path) |
| 173 | + if err != nil { |
| 174 | + return nil, err |
| 175 | + } |
| 176 | + |
| 177 | + for block, contents := pem.Decode(contents); ; block, contents = pem.Decode(contents) { |
| 178 | + cert, err := x509.ParseCertificate(block.Bytes) |
| 179 | + if err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + certs = append(certs, cert) |
| 183 | + |
| 184 | + if len(contents) == 0 { |
| 185 | + break |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if len(certs) == 0 { |
| 190 | + return nil, fmt.Errorf("No certificates in file %s", path) |
| 191 | + } |
| 192 | + |
| 193 | + return certs, nil |
| 194 | +} |
0 commit comments