...
1
2
3
4
5
6
7 package ocsp
8
9 import (
10 "crypto/x509"
11 "errors"
12 "fmt"
13 "net/http"
14
15 "go.mongodb.org/mongo-driver/internal/httputil"
16 "golang.org/x/crypto/ocsp"
17 )
18
19 type config struct {
20 serverCert, issuer *x509.Certificate
21 cache Cache
22 disableEndpointChecking bool
23 ocspRequest *ocsp.Request
24 ocspRequestBytes []byte
25 httpClient *http.Client
26 }
27
28 func newConfig(certChain []*x509.Certificate, opts *VerifyOptions) (config, error) {
29 cfg := config{
30 cache: opts.Cache,
31 disableEndpointChecking: opts.DisableEndpointChecking,
32 httpClient: opts.HTTPClient,
33 }
34
35 if cfg.httpClient == nil {
36 cfg.httpClient = httputil.DefaultHTTPClient
37 }
38
39 if len(certChain) == 0 {
40 return cfg, errors.New("verified certificate chain contained no certificates")
41 }
42
43
44 cfg.serverCert = certChain[0]
45 cfg.issuer = certChain[0]
46 if len(certChain) > 1 {
47
48
49 cfg.issuer = certChain[1]
50
51 if err := cfg.serverCert.CheckSignatureFrom(cfg.issuer); err != nil {
52 errString := "error checking if server certificate is signed by the issuer in the verified chain: %v"
53 return cfg, fmt.Errorf(errString, err)
54 }
55 }
56
57 var err error
58 cfg.ocspRequestBytes, err = ocsp.CreateRequest(cfg.serverCert, cfg.issuer, nil)
59 if err != nil {
60 return cfg, fmt.Errorf("error creating OCSP request: %w", err)
61 }
62 cfg.ocspRequest, err = ocsp.ParseRequest(cfg.ocspRequestBytes)
63 if err != nil {
64 return cfg, fmt.Errorf("error parsing OCSP request bytes: %w", err)
65 }
66
67 return cfg, nil
68 }
69
View as plain text