fix: parse ssl certificate error #270

This commit is contained in:
0xJacky 2024-02-06 15:41:36 +08:00
parent e1c38e28a8
commit 371472e67b
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
5 changed files with 16 additions and 17 deletions

View file

@ -6,13 +6,12 @@ import (
"os"
)
func IsPublicKey(pemStr string) bool {
func IsCertificate(pemStr string) bool {
block, _ := pem.Decode([]byte(pemStr))
if block == nil {
return false
}
_, err := x509.ParsePKIXPublicKey(block.Bytes)
_, err := x509.ParseCertificate(block.Bytes)
return err == nil
}
@ -31,8 +30,8 @@ func IsPrivateKey(pemStr string) bool {
return errECDSA == nil
}
// IsPublicKeyPath checks if the file at the given path is a public key or not exists.
func IsPublicKeyPath(path string) bool {
// IsCertificatePath checks if the file at the given path is a certificate or not exists.
func IsCertificatePath(path string) bool {
if path == "" {
return false
}
@ -50,7 +49,7 @@ func IsPublicKeyPath(path string) bool {
return false
}
return IsPublicKey(string(bytes))
return IsCertificate(string(bytes))
}
// IsPrivateKeyPath checks if the file at the given path is a private key or not exists.

View file

@ -5,16 +5,16 @@ import (
val "github.com/go-playground/validator/v10"
)
func isPublicKey(fl val.FieldLevel) bool {
return cert.IsPublicKey(fl.Field().String())
func isCertificate(fl val.FieldLevel) bool {
return cert.IsCertificate(fl.Field().String())
}
func isPrivateKey(fl val.FieldLevel) bool {
return cert.IsPrivateKey(fl.Field().String())
}
func isPublicKeyPath(fl val.FieldLevel) bool {
return cert.IsPublicKeyPath(fl.Field().String())
func isCertificatePath(fl val.FieldLevel) bool {
return cert.IsCertificatePath(fl.Field().String())
}
func isPrivateKeyPath(fl val.FieldLevel) bool {

View file

@ -18,7 +18,7 @@ func Init() {
logger.Fatal(err)
}
err = v.RegisterValidation("publickey", isPublicKey)
err = v.RegisterValidation("certificate", isCertificate)
if err != nil {
logger.Fatal(err)
@ -30,7 +30,7 @@ func Init() {
logger.Fatal(err)
}
err = v.RegisterValidation("publickey_path", isPublicKeyPath)
err = v.RegisterValidation("certificate_path", isCertificatePath)
if err != nil {
logger.Fatal(err)