getClientCertificate reads the pair of client cert and key from disk and returns a tls.Certificate.
(_ *tls.CertificateRequestInfo)
| 969 | |
| 970 | // getClientCertificate reads the pair of client cert and key from disk and returns a tls.Certificate. |
| 971 | func (c *TLSConfig) getClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) { |
| 972 | var ( |
| 973 | certData, keyData []byte |
| 974 | err error |
| 975 | ) |
| 976 | |
| 977 | if c.CertFile != "" { |
| 978 | certData, err = os.ReadFile(c.CertFile) |
| 979 | if err != nil { |
| 980 | return nil, fmt.Errorf("unable to read specified client cert (%s): %w", c.CertFile, err) |
| 981 | } |
| 982 | } else { |
| 983 | certData = []byte(c.Cert) |
| 984 | } |
| 985 | |
| 986 | if c.KeyFile != "" { |
| 987 | keyData, err = os.ReadFile(c.KeyFile) |
| 988 | if err != nil { |
| 989 | return nil, fmt.Errorf("unable to read specified client key (%s): %w", c.KeyFile, err) |
| 990 | } |
| 991 | } else { |
| 992 | keyData = []byte(c.Key) |
| 993 | } |
| 994 | |
| 995 | cert, err := tls.X509KeyPair(certData, keyData) |
| 996 | if err != nil { |
| 997 | return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %w", c.CertFile, c.KeyFile, err) |
| 998 | } |
| 999 | |
| 1000 | return &cert, nil |
| 1001 | } |
| 1002 | |
| 1003 | // readCAFile reads the CA cert file from disk. |
| 1004 | func readCAFile(f string) ([]byte, error) { |