GitLabgitlab-org
cli
main
1package api
3import (
4 "crypto/tls"
5 "crypto/x509"
6 "encoding/asn1"
7 "encoding/pem"
8 "errors"
9 "fmt"
10 "os"
12 "software.sslmate.com/src/go-pkcs12"
14 "gitlab.com/gitlab-org/cli/internal/dbg"
15)
17var (
18 oidPKCS7Data = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
19 oidPKCS7SignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
20)
22// loadClientCertificates loads a PEM or DER certificate and key pair, or, when
23// keyFile is empty, a PKCS#12 bundle decrypted with password. A certFile without
24// a keyFile that is not a PKCS#12 bundle yields no certificate and no error,
25// because glab ignored a lone client_cert before PKCS#12 support and CI
26// auto-login can set CI_SERVER_TLS_CERT_FILE without CI_SERVER_TLS_KEY_FILE.
27// A PKCS#12 bundle that cannot be decoded is an error.
28func loadClientCertificates(certFile, keyFile, password string) ([]tls.Certificate, error) {
29 if keyFile == "" {
30 return loadPKCS12ClientCertificate(certFile, password)
31 }
33 certData, err := os.ReadFile(certFile)
34 if err != nil {
35 return nil, fmt.Errorf("reading client certificate: %w", err)
36 }
37 keyData, err := os.ReadFile(keyFile)
38 if err != nil {
39 return nil, fmt.Errorf("reading client key: %w", err)
40 }
42 if !isPEM(certData) {
43 if _, err := x509.ParseCertificate(certData); err != nil {
44 return nil, fmt.Errorf("client certificate %q is not a PEM or DER certificate (for a PKCS#12 bundle, leave client_key unset): %w", certFile, err)
45 }
46 certData = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certData})
47 }
48 if !isPEM(keyData) {
49 // tls.X509KeyPair tries PKCS#1, PKCS#8 and SEC 1 on any "PRIVATE KEY" block.
50 keyData = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyData})
51 }
53 cert, err := tls.X509KeyPair(certData, keyData)
54 if err != nil {
55 return nil, fmt.Errorf("loading client certificate %q and key %q: %w", certFile, keyFile, err)
56 }
57 return []tls.Certificate{cert}, nil
58}
60// LoadClientCertificates loads client_cert and client_key the way the API
61// client does, so a bundle that works for API calls also works for callers
62// outside this package. Like loadClientCertificates, it returns no certificate
63// and no error for a lone client_cert that is not a PKCS#12 bundle.
64func LoadClientCertificates(certFile, keyFile string, password func() (string, error)) ([]tls.Certificate, error) {
65 pass, err := pkcs12Password(certFile, keyFile, password)
66 if err != nil {
67 return nil, err
68 }
69 return loadClientCertificates(certFile, keyFile, pass)
70}
72// pkcs12Password calls password only when certFile, used without keyFile, is a
73// PKCS#12 bundle that an empty password does not open, so an unreadable keyring
74// cannot fail a client that never uses the password. When certFile is not a
75// readable bundle, a client_cert_password that would be ignored is an error;
76// failing to read it there is not.
77func pkcs12Password(certFile, keyFile string, password func() (string, error)) (string, error) {
78 if certFile == "" || keyFile != "" || password == nil {
79 return "", nil
80 }
81 data, err := os.ReadFile(certFile)
82 if err != nil || !isPKCS12(data) {
83 if configured, readErr := password(); readErr == nil && configured != "" {
84 return "", unusedPasswordError(certFile, err)
85 }
86 return "", nil
87 }
88 if _, _, _, err := pkcs12.DecodeChain(data, ""); !errors.Is(err, pkcs12.ErrIncorrectPassword) {
89 return "", nil
90 }
91 return password()
92}
94func unusedPasswordError(certFile string, readErr error) error {
95 if readErr != nil {
96 return fmt.Errorf("client_cert_password is set, but the client certificate %q cannot be read: %w", certFile, readErr)
97 }
98 return fmt.Errorf("client_cert_password is set, but the client certificate %q is not a PKCS#12 bundle (for a PEM or DER certificate, set client_key)", certFile)
99}
101func loadPKCS12ClientCertificate(certFile, password string) ([]tls.Certificate, error) {
102 data, err := os.ReadFile(certFile)
103 if err != nil {
104 dbg.Debugf("api: not using client certificate %q: client_key is unset and the file cannot be read: %v", certFile, err)
105 return nil, nil
106 }
107 if !isPKCS12(data) {
108 dbg.Debugf("api: not using client certificate %q: client_key is unset and the file is not a PKCS#12 bundle", certFile)
109 return nil, nil
110 }
112 key, leaf, chain, err := pkcs12.DecodeChain(data, password)
113 if errors.Is(err, pkcs12.ErrIncorrectPassword) {
114 return nil, fmt.Errorf("incorrect password for the PKCS#12 client certificate %q: set client_cert_password", certFile)
115 }
116 if err != nil {
117 return nil, fmt.Errorf("loading PKCS#12 client certificate %q: %w", certFile, err)
118 }
120 cert := tls.Certificate{
121 Certificate: [][]byte{leaf.Raw},
122 PrivateKey: key,
123 Leaf: leaf,
124 }
125 for _, c := range chain {
126 cert.Certificate = append(cert.Certificate, c.Raw)
127 }
128 return []tls.Certificate{cert}, nil
129}
131// isPKCS12 checks only the outer PFX structure from RFC 7292: a version, then
132// PKCS#7 data or signed-data content. go-pkcs12 reports every later failure,
133// such as an unsupported algorithm or a missing key, for a file that has it.
134func isPKCS12(data []byte) bool {
135 var pfx struct {
136 Version int
137 AuthSafe struct{ ContentType asn1.ObjectIdentifier }
138 }
139 if _, err := asn1.Unmarshal(data, &pfx); err != nil {
140 return false
141 }
142 return pfx.AuthSafe.ContentType.Equal(oidPKCS7Data) || pfx.AuthSafe.ContentType.Equal(oidPKCS7SignedData)
143}
145func isPEM(data []byte) bool {
146 block, _ := pem.Decode(data)
147 return block != nil
148}
Diagnostics6 git calls · 610 ms
CallStatusSizeTime0610 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:02001.1 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=602 git.response.bytes=1165 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetch2001.8 KiB602 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1853 git.response.cache=MISS git.response.duration_ms=602 git.fetch.wants=4862d26b8c4cfbb720227247396266ea8a02c819 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1853 git.pkt.payload.max=1826 git.sideband.band1.bytes=1826 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1516ecbde0acb
- Request
- 54adf7bc…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format