-
Notifications
You must be signed in to change notification settings - Fork 289
/
tls.go
124 lines (104 loc) · 4.78 KB
/
tls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package gatewayapi
import (
"crypto/x509"
"encoding/pem"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
v1 "sigs.k8s.io/gateway-api/apis/v1"
)
// validateTLSSecretData ensures the cert and key provided in a secret
// is not malformed and can be properly parsed
func validateTLSSecretsData(secrets []*corev1.Secret, host *v1.Hostname) error {
var publicKeyAlgorithm string
var parseErr error
pkaSecretSet := make(map[string][]string)
for _, secret := range secrets {
certData := secret.Data[corev1.TLSCertKey]
if err := validateCertificate(certData); err != nil {
return fmt.Errorf("%s/%s must contain valid %s and %s, unable to validate certificate in %s: %w", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSCertKey, err)
}
certBlock, _ := pem.Decode(certData)
if certBlock == nil {
return fmt.Errorf("%s/%s must contain valid %s and %s, unable to decode pem data in %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSCertKey)
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return fmt.Errorf("%s/%s must contain valid %s and %s, unable to parse certificate in %s: %w", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSCertKey, err)
}
publicKeyAlgorithm = cert.PublicKeyAlgorithm.String()
keyData := secret.Data[corev1.TLSPrivateKeyKey]
keyBlock, _ := pem.Decode(keyData)
if keyBlock == nil {
return fmt.Errorf("%s/%s must contain valid %s and %s, unable to decode pem data in %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSPrivateKeyKey)
}
matchedFQDN, err := verifyHostname(cert, host)
if err != nil {
return fmt.Errorf("%s/%s must contain valid %s and %s, hostname %s does not match Common Name or DNS Names in the certificate %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, string(*host), corev1.TLSCertKey)
}
pkaSecretKey := fmt.Sprintf("%s/%s", publicKeyAlgorithm, matchedFQDN)
// Check whether the public key algorithm and matched certificate FQDN in the referenced secrets are unique.
if matchedFQDN, ok := pkaSecretSet[pkaSecretKey]; ok {
return fmt.Errorf("%s/%s public key algorithm must be unique, matched certificate FQDN %s has a conflicting algorithm [%s]",
secret.Namespace, secret.Name, matchedFQDN, publicKeyAlgorithm)
}
pkaSecretSet[pkaSecretKey] = matchedFQDN
switch keyBlock.Type {
case "PRIVATE KEY":
_, err := x509.ParsePKCS8PrivateKey(keyBlock.Bytes)
if err != nil {
parseErr = fmt.Errorf("%s/%s must contain valid %s and %s, unable to parse PKCS8 formatted private key in %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSPrivateKeyKey)
}
case "RSA PRIVATE KEY":
_, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
if err != nil {
parseErr = fmt.Errorf("%s/%s must contain valid %s and %s, unable to parse PKCS1 formatted private key in %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSPrivateKeyKey)
}
case "EC PRIVATE KEY":
_, err := x509.ParseECPrivateKey(keyBlock.Bytes)
if err != nil {
parseErr = fmt.Errorf("%s/%s must contain valid %s and %s, unable to parse EC formatted private key in %s", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, corev1.TLSPrivateKeyKey)
}
default:
return fmt.Errorf("%s/%s must contain valid %s and %s, %s key format found in %s, supported formats are PKCS1, PKCS8 or EC", secret.Namespace, secret.Name, corev1.TLSCertKey, corev1.TLSPrivateKeyKey, keyBlock.Type, corev1.TLSPrivateKeyKey)
}
}
return parseErr
}
// verifyHostname checks if the listener Hostname matches any domain in the certificate, returns a list of matched hosts.
func verifyHostname(cert *x509.Certificate, host *v1.Hostname) ([]string, error) {
var matchedHosts []string
if len(cert.DNSNames) > 0 {
matchedHosts = computeHosts(cert.DNSNames, host)
} else {
matchedHosts = computeHosts([]string{cert.Subject.CommonName}, host)
}
if len(matchedHosts) > 0 {
return matchedHosts, nil
}
return nil, x509.HostnameError{Certificate: cert, Host: string(*host)}
}
func validateCertificate(data []byte) error {
block, _ := pem.Decode(data)
if block == nil {
return fmt.Errorf("pem decode failed")
}
certs, err := x509.ParseCertificates(block.Bytes)
if err != nil {
return err
}
now := time.Now()
for _, cert := range certs {
if now.After(cert.NotAfter) {
return fmt.Errorf("certificate is expired")
}
if now.Before(cert.NotBefore) {
return fmt.Errorf("certificate is not yet valid")
}
}
return nil
}