| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
maxIssuingCertificateURLs = 3 |
| 12 |
maxCertChainLength = 4 |
| 13 |
) |
| 14 |
|
| 15 |
// GetCertificateChain constructs the certificate chain for the key's certificate. |
| 16 |
// If an error is encountered in the process, return what has been constructed so far. |
| 17 |
func GetCertificateChain(cert *x509.Certificate, client *http.Client) ([][]byte, error) { |
| 18 |
var certs [][]byte |
| 19 |
currentCert := cert |
| 20 |
for len(certs) <= maxCertChainLength { |
| 21 |
issuingCert, err := fetchIssuingCertificate(client, currentCert) |
| 22 |
if err != nil { |
| 23 |
return nil, err |
| 24 |
} |
| 25 |
if issuingCert == nil { |
| 26 |
return certs, nil |
| 27 |
} |
| 28 |
certs = append(certs, issuingCert.Raw) |
| 29 |
currentCert = issuingCert |
| 30 |
} |
| 31 |
return nil, fmt.Errorf("max certificate chain length (%v) exceeded", maxCertChainLength) |
| 32 |
} |
| 33 |
|
| 34 |
// Given a certificate, iterates through its IssuingCertificateURLs and returns |
| 35 |
// the certificate that signed it. If the certificate lacks an |
| 36 |
// IssuingCertificateURL, return nil. If fetching the certificates fails or the |
| 37 |
// cert chain is malformed, return an error. |
| 38 |
func fetchIssuingCertificate(client *http.Client, cert *x509.Certificate) (*x509.Certificate, error) { |
| 39 |
// Check if we should event attempt fetching. |
| 40 |
if cert == nil || len(cert.IssuingCertificateURL) == 0 { |
| 41 |
return nil, nil |
| 42 |
} |
| 43 |
// For each URL, fetch and parse the certificate, then verify whether it signed cert. |
| 44 |
// If successful, return the parsed certificate. If any step in this process fails, try the next url. |
| 45 |
// If all the URLs fail, return the last error we got. |
| 46 |
// TODO(Issue #169): Return a multi-error here |
| 47 |
var lastErr error |
| 48 |
for i, url := range cert.IssuingCertificateURL { |
| 49 |
// Limit the number of attempts. |
| 50 |
if i >= maxIssuingCertificateURLs { |
| 51 |
break |
| 52 |
} |
| 53 |
resp, err := client.Get(url) |
| 54 |
if err != nil { |
| 55 |
lastErr = fmt.Errorf("failed to retrieve certificate at %v: %w", url, err) |
| 56 |
continue |
| 57 |
} |
| 58 |
|
| 59 |
if resp.StatusCode != http.StatusOK { |
| 60 |
lastErr = fmt.Errorf("certificate retrieval from %s returned non-OK status: %v", url, resp.StatusCode) |
| 61 |
continue |
| 62 |
} |
| 63 |
certBytes, err := io.ReadAll(resp.Body) |
| 64 |
resp.Body.Close() |
| 65 |
if err != nil { |
| 66 |
lastErr = fmt.Errorf("failed to read response body from %s: %w", url, err) |
| 67 |
continue |
| 68 |
} |
| 69 |
|
| 70 |
parsedCert, err := x509.ParseCertificate(certBytes) |
| 71 |
if err != nil { |
| 72 |
lastErr = fmt.Errorf("failed to parse response from %s into a certificate: %w", url, err) |
| 73 |
continue |
| 74 |
} |
| 75 |
|
| 76 |
// Check if the parsed certificate signed the current one. |
| 77 |
if err = cert.CheckSignatureFrom(parsedCert); err != nil { |
| 78 |
lastErr = fmt.Errorf("parent certificate from %s did not sign child: %w", url, err) |
| 79 |
continue |
| 80 |
} |
| 81 |
return parsedCert, nil |
| 82 |
} |
| 83 |
return nil, lastErr |
| 84 |
} |