|
| 1 | +// |
| 2 | +// Copyright 2024 The Chainloop 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 signserver |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "crypto" |
| 21 | + "crypto/tls" |
| 22 | + "crypto/x509" |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "mime/multipart" |
| 27 | + "net/http" |
| 28 | + "net/url" |
| 29 | + "os" |
| 30 | + "strings" |
| 31 | + |
| 32 | + sigstoresigner "github.com/sigstore/sigstore/pkg/signature" |
| 33 | +) |
| 34 | + |
| 35 | +// ReferenceScheme is the scheme to be used when using signserver keys. Ex: signserver://host/worker |
| 36 | +const ReferenceScheme = "signserver" |
| 37 | + |
| 38 | +// Signer implements a signer for SignServer |
| 39 | +type Signer struct { |
| 40 | + host, worker, caPath string |
| 41 | +} |
| 42 | + |
| 43 | +var _ sigstoresigner.Signer = (*Signer)(nil) |
| 44 | + |
| 45 | +func NewSigner(host, worker, caPath string) *Signer { |
| 46 | + return &Signer{ |
| 47 | + host: host, |
| 48 | + worker: worker, |
| 49 | + caPath: caPath, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (s Signer) PublicKey(_ ...sigstoresigner.PublicKeyOption) (crypto.PublicKey, error) { |
| 54 | + return nil, errors.New("public key not yet supported for SignServer") |
| 55 | +} |
| 56 | + |
| 57 | +// SignMessage Signs a message by calling to SignServer |
| 58 | +func (s Signer) SignMessage(message io.Reader, _ ...sigstoresigner.SignOption) ([]byte, error) { |
| 59 | + url, err := url.Parse(fmt.Sprintf("https://%s/signserver/process", s.host)) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to parse url: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + body := &bytes.Buffer{} |
| 65 | + mpwriter := multipart.NewWriter(body) |
| 66 | + |
| 67 | + // Send worker name |
| 68 | + err = mpwriter.WriteField("workerName", s.worker) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to write field: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + // Send payload |
| 74 | + part, err := mpwriter.CreateFormFile("file", "attestation.json") |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to create form field: %w", err) |
| 77 | + } |
| 78 | + _, err = io.Copy(part, message) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("failed to copy message: %w", err) |
| 81 | + } |
| 82 | + err = mpwriter.Close() |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to close multipart writer: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + req, err := http.NewRequest("POST", url.String(), body) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 90 | + } |
| 91 | + req.Header.Add("Content-Type", mpwriter.FormDataContentType()) |
| 92 | + client := &http.Client{} |
| 93 | + |
| 94 | + var caPool *x509.CertPool |
| 95 | + if s.caPath != "" { |
| 96 | + caPool = x509.NewCertPool() |
| 97 | + caContents, err := os.ReadFile(s.caPath) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("failed to read ca cert: %w", err) |
| 100 | + } |
| 101 | + caPool.AppendCertsFromPEM(caContents) |
| 102 | + client.Transport = &http.Transport{ |
| 103 | + TLSClientConfig: &tls.Config{RootCAs: caPool, MinVersion: tls.VersionTLS12}} |
| 104 | + } |
| 105 | + |
| 106 | + res, err := client.Do(req) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + if res.StatusCode != http.StatusOK { |
| 112 | + return nil, fmt.Errorf("failed to send request: status %d", res.StatusCode) |
| 113 | + } |
| 114 | + |
| 115 | + resBytes, err := io.ReadAll(res.Body) |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + return resBytes, nil |
| 121 | +} |
| 122 | + |
| 123 | +// ParseKeyReference interprets a key reference for SignServer |
| 124 | +func ParseKeyReference(keyPath string) (string, string, error) { |
| 125 | + parts := strings.SplitAfter(keyPath, "://") |
| 126 | + if len(parts) != 2 { |
| 127 | + return "", "", fmt.Errorf("invalid key path: %s", keyPath) |
| 128 | + } |
| 129 | + parts = strings.Split(parts[1], "/") |
| 130 | + if len(parts) != 2 { |
| 131 | + return "", "", fmt.Errorf("invalid key path: %s", keyPath) |
| 132 | + } |
| 133 | + return parts[0], parts[1], nil |
| 134 | +} |
0 commit comments