...

Source file src/github.com/google/certificate-transparency-go/internal/witness/client/http/witness_client.go

Documentation: github.com/google/certificate-transparency-go/internal/witness/client/http

     1  // Copyright 2021 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package http is a simple client for interacting with witnesses over HTTP.
    16  package http
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"net/http"
    26  	"net/url"
    27  	"os"
    28  
    29  	wit_api "github.com/google/certificate-transparency-go/internal/witness/api"
    30  )
    31  
    32  // ErrSTHTooOld is returned if the STH passed to Update needs to be updated.
    33  var ErrSTHTooOld = errors.New("STH too old")
    34  
    35  // Witness consists of the witness' URL and signature verifier.
    36  type Witness struct {
    37  	URL *url.URL
    38  }
    39  
    40  // GetLatestSTH returns a recent STH from the witness for the specified log ID.
    41  func (w Witness) GetLatestSTH(ctx context.Context, logID string) ([]byte, error) {
    42  	u, err := w.URL.Parse(fmt.Sprintf(wit_api.HTTPGetSTH, url.PathEscape(logID)))
    43  	if err != nil {
    44  		return nil, fmt.Errorf("failed to parse URL: %v", err)
    45  	}
    46  	req, err := http.NewRequest("GET", u.String(), nil)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("failed to create request: %v", err)
    49  	}
    50  	resp, err := http.DefaultClient.Do(req.WithContext(ctx))
    51  	if err != nil {
    52  		return nil, fmt.Errorf("failed to do http request: %v", err)
    53  	}
    54  	defer resp.Body.Close()
    55  	if resp.StatusCode == 404 {
    56  		return nil, os.ErrNotExist
    57  	} else if resp.StatusCode != 200 {
    58  		return nil, fmt.Errorf("bad status response: %s", resp.Status)
    59  	}
    60  	return io.ReadAll(resp.Body)
    61  }
    62  
    63  // Update attempts to clock the witness forward for the given logID.
    64  // The latest signed STH will be returned if this succeeds, or if the error is
    65  // http.ErrSTHTooOld. In all other cases no STH should be expected.
    66  func (w Witness) Update(ctx context.Context, logID string, sth []byte, proof [][]byte) ([]byte, error) {
    67  	reqBody, err := json.MarshalIndent(&wit_api.UpdateRequest{
    68  		STH:   sth,
    69  		Proof: proof,
    70  	}, "", " ")
    71  	if err != nil {
    72  		return nil, fmt.Errorf("failed to marshal update request: %v", err)
    73  	}
    74  	u, err := w.URL.Parse(fmt.Sprintf(wit_api.HTTPUpdate, url.PathEscape(logID)))
    75  	if err != nil {
    76  		return nil, fmt.Errorf("failed to parse URL: %v", err)
    77  	}
    78  	req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(reqBody))
    79  	if err != nil {
    80  		return nil, fmt.Errorf("failed to create request: %v", err)
    81  	}
    82  	resp, err := http.DefaultClient.Do(req.WithContext(ctx))
    83  	if err != nil {
    84  		return nil, fmt.Errorf("failed to do http request: %v", err)
    85  	}
    86  	defer resp.Body.Close()
    87  	body, err := io.ReadAll(resp.Body)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("failed to read body: %v", err)
    90  	}
    91  	if resp.StatusCode != 200 {
    92  		if resp.StatusCode == 409 {
    93  			return body, ErrSTHTooOld
    94  		}
    95  		return nil, fmt.Errorf("bad status response (%s): %q", resp.Status, body)
    96  	}
    97  	return body, nil
    98  }
    99  

View as plain text