Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle client certificate chain #3

Open
Klaus-Tockloth opened this issue Oct 1, 2024 · 1 comment
Open

Handle client certificate chain #3

Klaus-Tockloth opened this issue Oct 1, 2024 · 1 comment

Comments

@Klaus-Tockloth
Copy link

Klaus-Tockloth commented Oct 1, 2024

The client should send the full certificate chain to the server (best practice) and not only the (single) client certificate. This use case is currently not handled. The defect is here:

func GetClientCertificate(clientCertPath string, clientKeyPath string) (*tls.Certificate, error) {
	fmt.Printf("Server requested certificate\n")
	if clientCertPath == "" || clientKeyPath == "" {
		return nil, errors.New("client certificate and key are required")
	}
	clientBytes, err := os.ReadFile(clientCertPath)
	if err != nil {
		return nil, fmt.Errorf("error reading client certificate: %w", err)
	}
	var cert *x509.Certificate
	for block, rest := pem.Decode(clientBytes); block != nil; block, rest = pem.Decode(rest) {
		if block.Type == "CERTIFICATE" {
			cert, err = x509.ParseCertificate(block.Bytes)
			if err != nil {
				return nil, fmt.Errorf("error parsing client certificate: %v", err)
			}
		}
	}

	certificate := tls.Certificate{
		Certificate: [][]byte{cert.Raw},
		PrivateKey: &CustomSigner{
			x509Cert:       cert,
			clientCertPath: clientCertPath,
			clientKeyPath:  clientKeyPath,
		},
	}
	return &certificate, nil
}

In case of a chain: cert contains the last certificate in the chain.

@getvictor
Copy link
Owner

I agree. I will plan to fix this once I come back to refresh this codebase and articles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants