| 1 | // Copyright © SAS Institute Inc. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package tsclient |
| 15 | |
| 16 | import ( |
| 17 | "context" |
| 18 | "crypto/tls" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "log" |
| 23 | "net/http" |
| 24 | "time" |
| 25 | |
| 26 | "github.com/prometheus/client_golang/prometheus" |
| 27 | "github.com/prometheus/client_golang/prometheus/promauto" |
| 28 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 29 | "github.com/sassoftware/relic/v7/config" |
| 30 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 31 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 32 | "github.com/sassoftware/relic/v7/lib/pkcs9/ratelimit" |
| 33 | "github.com/sassoftware/relic/v7/lib/pkcs9/timestampcache" |
| 34 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 35 | ) |
| 36 | |
| 37 | type tsClient struct { |
| 38 | conf *config.TimestampConfig |
| 39 | client *http.Client |
| 40 | } |
| 41 | |
| 42 | var ( |
| 43 | buckets = []float64{.01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10} |
| 44 | |
| 45 | metricCount = promauto.NewCounterVec( |
| 46 | prometheus.CounterOpts{ |
| 47 | Name: "timestamper_request_count", |
| 48 | Help: "Outcome of timestamper requests", |
| 49 | }, |
| 50 | []string{"code"}, |
| 51 | ) |
| 52 | metricDuration = promauto.NewHistogramVec( |
| 53 | prometheus.HistogramOpts{ |
| 54 | Name: "timestamper_request_duration_seconds", |
| 55 | Help: "Histogram of timestamper request durations", |
| 56 | Buckets: buckets, |
| 57 | }, |
| 58 | nil, |
| 59 | ) |
| 60 | ) |
| 61 | |
| 62 | func New(conf *config.TimestampConfig) (t pkcs9.Timestamper, err error) { |
| 63 | tlsconf := &tls.Config{} |
| 64 | if err := x509tools.LoadCertPool(conf.CaCert, tlsconf); err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | client := &http.Client{ |
| 68 | Timeout: time.Second * time.Duration(conf.Timeout), |
| 69 | Transport: &http.Transport{ |
| 70 | TLSClientConfig: tlsconf, |
| 71 | }, |
| 72 | } |
| 73 | client.Transport = promhttp.InstrumentRoundTripperCounter(metricCount, client.Transport) |
| 74 | client.Transport = promhttp.InstrumentRoundTripperDuration(metricDuration, client.Transport) |
| 75 | t = tsClient{conf, client} |
| 76 | if conf.RateLimit != 0 { |
| 77 | t = ratelimit.New(t, conf.RateLimit, conf.RateBurst) |
| 78 | } |
| 79 | if len(conf.Memcache) != 0 { |
| 80 | t, err = timestampcache.New(t, conf.Memcache) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | } |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | func (c tsClient) Timestamp(ctx context.Context, req *pkcs9.Request) (*pkcs7.ContentInfoSignedData, error) { |
| 89 | var urls []string |
| 90 | if req.Legacy { |
| 91 | urls = c.conf.MsURLs |
| 92 | if len(urls) == 0 { |
| 93 | return nil, errors.New("timestamp.msurls is empty") |
| 94 | } |
| 95 | } else { |
| 96 | urls = c.conf.URLs |
| 97 | if len(urls) == 0 { |
| 98 | return nil, errors.New("timestamp.urls is empty") |
| 99 | } |
| 100 | } |
| 101 | imprint := req.EncryptedDigest |
| 102 | if !req.Legacy { |
| 103 | d := req.Hash.New() |
| 104 | d.Write(imprint) |
| 105 | imprint = d.Sum(nil) |
| 106 | } |
| 107 | var err error |
| 108 | for _, url := range urls { |
| 109 | if err != nil { |
| 110 | log.Printf("warning: timestamping failed: %s\n trying next server %s...\n", err, url) |
| 111 | } |
| 112 | var token *pkcs7.ContentInfoSignedData |
| 113 | token, err = c.do(ctx, url, req, imprint) |
| 114 | if err == nil { |
| 115 | return token, nil |
| 116 | } |
| 117 | if ctx.Err() != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | } |
| 121 | return nil, fmt.Errorf("timestamping failed: %w", err) |
| 122 | } |
| 123 | |
| 124 | func (c tsClient) do(ctx context.Context, url string, req *pkcs9.Request, imprint []byte) (*pkcs7.ContentInfoSignedData, error) { |
| 125 | var msg *pkcs9.TimeStampReq |
| 126 | var httpReq *http.Request |
| 127 | var err error |
| 128 | if !req.Legacy { |
| 129 | msg, httpReq, err = pkcs9.NewRequest(url, req.Hash, imprint) |
| 130 | } else { |
| 131 | httpReq, err = pkcs9.NewLegacyRequest(url, imprint) |
| 132 | } |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | httpReq.Header.Set("User-Agent", config.UserAgent) |
| 137 | resp, err := c.client.Do(httpReq.WithContext(ctx)) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | body, err := io.ReadAll(resp.Body) |
| 142 | resp.Body.Close() |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } else if resp.StatusCode != 200 { |
| 146 | return nil, fmt.Errorf("%s: HTTP %s\n%s", url, resp.Status, body) |
| 147 | } |
| 148 | if req.Legacy { |
| 149 | return pkcs9.ParseLegacyResponse(body) |
| 150 | } |
| 151 | return msg.ParseResponse(body) |
| 152 | } |