Debian Codegraph @

lfshttp/certs.go

raw symbols SCIP
1 package lfshttp
2
3 import (
4 "crypto/tls"
5 "crypto/x509"
6 "encoding/pem"
7 "fmt"
8 "net/url"
9 "os"
10 "path/filepath"
11
12 "github.com/git-lfs/git-lfs/v3/config"
13 "github.com/git-lfs/git-lfs/v3/errors"
14 "github.com/git-lfs/git-lfs/v3/tools"
15 "github.com/git-lfs/git-lfs/v3/tr"
16 "github.com/rubyist/tracerx"
17 )
18
19 // isCertVerificationDisabledForHost returns whether SSL certificate verification
20 // has been disabled for the given host, or globally
21 func isCertVerificationDisabledForHost(c *Client, host string) bool {
22 hostSslVerify, _ := c.uc.Get("http", fmt.Sprintf("https://%v", host), "sslverify")
23 if hostSslVerify == "false" {
24 return true
25 }
26
27 return c.SkipSSLVerify
28 }
29
30 // isClientCertEnabledForHost returns whether client certificate
31 // are configured for the given host
32 func isClientCertEnabledForHost(c *Client, host string) bool {
33 _, hostSslKeyOk := c.uc.Get("http", fmt.Sprintf("https://%v/", host), "sslKey")
34 _, hostSslCertOk := c.uc.Get("http", fmt.Sprintf("https://%v/", host), "sslCert")
35
36 return hostSslKeyOk && hostSslCertOk
37 }
38
39 // decryptPEMBlock decrypts an encrypted PEM block representing a private key,
40 // prompting for credentials using the credential helper, and returns a
41 // decrypted PEM block representing that same private key.
42 func decryptPEMBlock(c *Client, block *pem.Block, path string, key []byte) ([]byte, error) {
43 fileurl := fmt.Sprintf("cert:///%s", filepath.ToSlash(path))
44 url, err := url.Parse(fileurl)
45 if err != nil {
46 return nil, err
47 }
48 credWrapper := c.credHelperContext.GetCredentialHelper(nil, url)
49
50 credWrapper.Input["username"] = []string{""}
51
52 creds, err := credWrapper.CredentialHelper.Fill(credWrapper.Input)
53 if err != nil {
54 tracerx.Printf("Error filling credentials for %q: %v", fileurl, err)
55 return nil, err
56 }
57 pass := creds["password"][0]
58 decrypted, err := x509.DecryptPEMBlock(block, []byte(pass))
59 if err != nil {
60 credWrapper.CredentialHelper.Reject(creds)
61 return nil, err
62 }
63 credWrapper.CredentialHelper.Approve(creds)
64
65 // decrypted is a DER blob, but we need a PEM-encoded block.
66 toEncode := &pem.Block{Type: block.Type, Headers: nil, Bytes: decrypted}
67 buf := pem.EncodeToMemory(toEncode)
68 return buf, nil
69 }
70
71 // getClientCertForHost returns a client certificate for a specific host (which may
72 // be "host:port" loaded from the gitconfig
73 func getClientCertForHost(c *Client, host string) (*tls.Certificate, error) {
74 hostSslKey, _ := c.uc.Get("http", fmt.Sprintf("https://%v/", host), "sslKey")
75 hostSslCert, _ := c.uc.Get("http", fmt.Sprintf("https://%v/", host), "sslCert")
76
77 hostSslKey, err := tools.ExpandPath(hostSslKey, false)
78 if err != nil {
79 return nil, errors.Wrap(err, tr.Tr.Get("Error resolving key path %q", hostSslKey))
80 }
81
82 hostSslCert, err = tools.ExpandPath(hostSslCert, false)
83 if err != nil {
84 return nil, errors.Wrap(err, tr.Tr.Get("Error resolving cert path %q", hostSslCert))
85 }
86
87 cert, err := os.ReadFile(hostSslCert)
88 if err != nil {
89 tracerx.Printf("Error reading client cert file %q: %v", hostSslCert, err)
90 return nil, errors.Wrap(err, tr.Tr.Get("Error reading client cert file %q", hostSslCert))
91 }
92 key, err := os.ReadFile(hostSslKey)
93 if err != nil {
94 tracerx.Printf("Error reading client key file %q: %v", hostSslKey, err)
95 return nil, errors.Wrap(err, tr.Tr.Get("Error reading client key file %q", hostSslKey))
96 }
97
98 block, _ := pem.Decode(key)
99 if block == nil {
100 return nil, errors.New(tr.Tr.Get("Error decoding PEM block from %q", hostSslKey))
101 }
102 if x509.IsEncryptedPEMBlock(block) {
103 key, err = decryptPEMBlock(c, block, hostSslKey, key)
104 if err != nil {
105 tracerx.Printf("Unable to decrypt client key file %q: %v", hostSslKey, err)
106 return nil, errors.Wrap(err, tr.Tr.Get("Error reading client key file %q (not a PKCS#1 file?)", hostSslKey))
107 }
108 }
109
110 certobj, err := tls.X509KeyPair(cert, key)
111 if err != nil {
112 tracerx.Printf("Error reading client cert/key %v", err)
113 return nil, errors.Wrap(err, tr.Tr.Get("Error reading client cert/key"))
114 }
115 return &certobj, nil
116 }
117
118 // getRootCAsForHostFromGitconfig returns a certificate pool for that
119 // specific host (which may be "host:port" or just "host") loaded from the
120 // Git configuration and environment settings, if any are defined.
121 // May return nil if it doesn't have anything to add, in which case the default
122 // RootCAs will be used if passed to TLSClientConfig.RootCAs
123 func getRootCAsForHostFromGitconfig(c *Client, host string) *x509.CertPool {
124 // don't init pool, want to return nil not empty if none found; init only on successful add cert
125 var pool *x509.CertPool
126
127 url := fmt.Sprintf("https://%v/", host)
128 uc := config.NewURLConfig(c.gitEnv)
129
130 backend, _ := uc.Get("http", url, "sslbackend")
131 schannelUseSslCaInfoStrValue, _ := uc.Get("http", url, "schannelusesslcainfo")
132 schannelUseSslCaInfo := config.Bool(schannelUseSslCaInfoStrValue, false)
133
134 if backend == "schannel" && !schannelUseSslCaInfo {
135 return pool
136 }
137
138 // Accumulate certs from all these locations:
139
140 // GIT_SSL_CAINFO first
141 if cafile, _ := c.osEnv.Get("GIT_SSL_CAINFO"); len(cafile) > 0 {
142 return appendCertsFromFile(pool, cafile)
143 }
144 // http.<url>/.sslcainfo or http.<url>.sslcainfo
145 if cafile, ok := uc.Get("http", url, "sslcainfo"); ok {
146 return appendCertsFromFile(pool, cafile)
147 }
148 // GIT_SSL_CAPATH
149 if cadir, _ := c.osEnv.Get("GIT_SSL_CAPATH"); len(cadir) > 0 {
150 return appendCertsFromFilesInDir(pool, cadir)
151 }
152 // http.sslcapath
153 if cadir, ok := c.gitEnv.Get("http.sslcapath"); ok {
154 return appendCertsFromFilesInDir(pool, cadir)
155 }
156
157 return pool
158 }
159
160 func appendCertsFromFilesInDir(pool *x509.CertPool, dir string) *x509.CertPool {
161 dirpath, errpath := tools.TranslateCygwinPath(dir)
162 if errpath != nil {
163 tracerx.Printf("Error reading cert dir %q: %v", dirpath, errpath)
164 }
165 files, err := os.ReadDir(dirpath)
166 if err != nil {
167 tracerx.Printf("Error reading cert dir %q: %v", dir, err)
168 return pool
169 }
170 for _, f := range files {
171 pool = appendCertsFromFile(pool, filepath.Join(dir, f.Name()))
172 }
173 return pool
174 }
175
176 func appendCertsFromFile(pool *x509.CertPool, filename string) *x509.CertPool {
177 filenamepath, errfile := tools.TranslateCygwinPath(filename)
178 if errfile != nil {
179 tracerx.Printf("Error reading cert dir %q: %v", filenamepath, errfile)
180 }
181 data, err := os.ReadFile(filenamepath)
182 if err != nil {
183 tracerx.Printf("Error reading cert file %q: %v", filename, err)
184 return pool
185 }
186 // Firstly, try parsing as binary certificate
187 if certs, err := x509.ParseCertificates(data); err == nil {
188 return appendCerts(pool, certs)
189 }
190 // If not binary certs, try PEM data
191 return appendCertsFromPEMData(pool, data)
192 }
193
194 func appendCerts(pool *x509.CertPool, certs []*x509.Certificate) *x509.CertPool {
195 if len(certs) == 0 {
196 // important to return unmodified (may be nil)
197 return pool
198 }
199
200 if pool == nil {
201 pool = x509.NewCertPool()
202 }
203
204 for _, cert := range certs {
205 pool.AddCert(cert)
206 }
207
208 return pool
209 }
210
211 func appendCertsFromPEMData(pool *x509.CertPool, data []byte) *x509.CertPool {
212 if len(data) == 0 {
213 return pool
214 }
215
216 // Bit of a dance, need to ensure if AppendCertsFromPEM fails we still return
217 // nil and not an empty pool, so system roots still get used
218 var ret *x509.CertPool
219 if pool == nil {
220 ret = x509.NewCertPool()
221 } else {
222 ret = pool
223 }
224 if !ret.AppendCertsFromPEM(data) {
225 // Return unmodified input pool (may be nil, do not replace with empty)
226 return pool
227 }
228 return ret
229 }