in api/internal/handler/ssl/ssl.go [364:423]
func ParseCert(crt, key string) (*entity.SSL, error) {
if crt == "" || key == "" {
return nil, consts.ErrSSLCertificate
}
certDERBlock, _ := pem.Decode([]byte(crt))
if certDERBlock == nil {
return nil, consts.ErrSSLCertificateResolution
}
// match
_, err := tls.X509KeyPair([]byte(crt), []byte(key))
if err != nil {
return nil, consts.ErrSSLKeyAndCert
}
x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)
if err != nil {
return nil, consts.ErrSSLCertificateResolution
}
ssl := entity.SSL{}
//domain
snis := []string{}
if x509Cert.DNSNames != nil && len(x509Cert.DNSNames) > 0 {
snis = x509Cert.DNSNames
} else if x509Cert.IPAddresses != nil && len(x509Cert.IPAddresses) > 0 {
for _, ip := range x509Cert.IPAddresses {
snis = append(snis, ip.String())
}
} else {
if x509Cert.Subject.Names != nil && len(x509Cert.Subject.Names) > 1 {
var attributeTypeNames = map[string]string{
"2.5.4.6": "C",
"2.5.4.10": "O",
"2.5.4.11": "OU",
"2.5.4.3": "CN",
"2.5.4.5": "SERIALNUMBER",
"2.5.4.7": "L",
"2.5.4.8": "ST",
"2.5.4.9": "STREET",
"2.5.4.17": "POSTALCODE",
}
for _, tv := range x509Cert.Subject.Names {
oidString := tv.Type.String()
typeName, ok := attributeTypeNames[oidString]
if ok && typeName == "CN" {
valueString := fmt.Sprint(tv.Value)
snis = append(snis, valueString)
}
}
}
}
ssl.Snis = snis
ssl.Key = key
ssl.Cert = crt
return &ssl, nil
}