* validateCertificates(): split the function's code

This commit is contained in:
Simon Zolin 2019-02-27 14:31:53 +03:00
parent 766fbab071
commit f4a6ca726c

View file

@ -1078,24 +1078,15 @@ func handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
} }
} }
/* Process certificate data and its private key. // Return 0 on success
CertificateChain, PrivateKey parameters are optional. func verifyCertChain(data *tlsConfigStatus, certChain string, serverName string) int {
On error, return partially set object log.Tracef("got certificate: %s", certChain)
with 'WarningValidation' field containing error description.
*/
func validateCertificates(CertificateChain, PrivateKey, ServerName string) tlsConfigStatus {
var err error
var data tlsConfigStatus
// check only public certificate separately from the key
if CertificateChain != "" {
log.Tracef("got certificate: %s", CertificateChain)
// now do a more extended validation // now do a more extended validation
var certs []*pem.Block // PEM-encoded certificates var certs []*pem.Block // PEM-encoded certificates
var skippedBytes []string // skipped bytes var skippedBytes []string // skipped bytes
pemblock := []byte(CertificateChain) pemblock := []byte(certChain)
for { for {
var decoded *pem.Block var decoded *pem.Block
decoded, pemblock = pem.Decode(pemblock) decoded, pemblock = pem.Decode(pemblock)
@ -1115,14 +1106,14 @@ func validateCertificates(CertificateChain, PrivateKey, ServerName string) tlsCo
parsed, err := x509.ParseCertificate(cert.Bytes) parsed, err := x509.ParseCertificate(cert.Bytes)
if err != nil { if err != nil {
data.WarningValidation = fmt.Sprintf("Failed to parse certificate: %s", err) data.WarningValidation = fmt.Sprintf("Failed to parse certificate: %s", err)
return data return 1
} }
parsedCerts = append(parsedCerts, parsed) parsedCerts = append(parsedCerts, parsed)
} }
if len(parsedCerts) == 0 { if len(parsedCerts) == 0 {
data.WarningValidation = fmt.Sprintf("You have specified an empty certificate") data.WarningValidation = fmt.Sprintf("You have specified an empty certificate")
return data return 1
} }
data.ValidCert = true data.ValidCert = true
@ -1130,7 +1121,7 @@ func validateCertificates(CertificateChain, PrivateKey, ServerName string) tlsCo
// spew.Dump(parsedCerts) // spew.Dump(parsedCerts)
opts := x509.VerifyOptions{ opts := x509.VerifyOptions{
DNSName: ServerName, DNSName: serverName,
} }
log.Printf("number of certs - %d", len(parsedCerts)) log.Printf("number of certs - %d", len(parsedCerts))
@ -1164,16 +1155,18 @@ func validateCertificates(CertificateChain, PrivateKey, ServerName string) tlsCo
data.NotBefore = mainCert.NotBefore data.NotBefore = mainCert.NotBefore
data.DNSNames = mainCert.DNSNames data.DNSNames = mainCert.DNSNames
} }
}
// validate private key (right now the only validation possible is just parsing it) return 0
if PrivateKey != "" { }
// Return 0 on success
func validatePkey(data *tlsConfigStatus, pkey string) int {
// now do a more extended validation // now do a more extended validation
var key *pem.Block // PEM-encoded certificates var key *pem.Block // PEM-encoded certificates
var skippedBytes []string // skipped bytes var skippedBytes []string // skipped bytes
// go through all pem blocks, but take first valid pem block and drop the rest // go through all pem blocks, but take first valid pem block and drop the rest
pemblock := []byte(PrivateKey) pemblock := []byte(pkey)
for { for {
var decoded *pem.Block var decoded *pem.Block
decoded, pemblock = pem.Decode(pemblock) decoded, pemblock = pem.Decode(pemblock)
@ -1190,23 +1183,46 @@ func validateCertificates(CertificateChain, PrivateKey, ServerName string) tlsCo
if key == nil { if key == nil {
data.WarningValidation = "No valid keys were found" data.WarningValidation = "No valid keys were found"
return data return 1
} }
// parse the decoded key // parse the decoded key
_, keytype, err := parsePrivateKey(key.Bytes) _, keytype, err := parsePrivateKey(key.Bytes)
if err != nil { if err != nil {
data.WarningValidation = fmt.Sprintf("Failed to parse private key: %s", err) data.WarningValidation = fmt.Sprintf("Failed to parse private key: %s", err)
return data return 1
} }
data.ValidKey = true data.ValidKey = true
data.KeyType = keytype data.KeyType = keytype
return 0
}
/* Process certificate data and its private key.
All parameters are optional.
On error, return partially set object
with 'WarningValidation' field containing error description.
*/
func validateCertificates(certChain, pkey, serverName string) tlsConfigStatus {
var data tlsConfigStatus
// check only public certificate separately from the key
if certChain != "" {
if verifyCertChain(&data, certChain, serverName) != 0 {
return data
}
}
// validate private key (right now the only validation possible is just parsing it)
if pkey != "" {
if validatePkey(&data, pkey) != 0 {
return data
}
} }
// if both are set, validate both in unison // if both are set, validate both in unison
if PrivateKey != "" && CertificateChain != "" { if pkey != "" && certChain != "" {
_, err = tls.X509KeyPair([]byte(CertificateChain), []byte(PrivateKey)) _, err := tls.X509KeyPair([]byte(certChain), []byte(pkey))
if err != nil { if err != nil {
data.WarningValidation = fmt.Sprintf("Invalid certificate or key: %s", err) data.WarningValidation = fmt.Sprintf("Invalid certificate or key: %s", err)
return data return data