123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package certvalidate
- import (
- "crypto/x509"
- "encoding/pem"
- "io"
- "mime/multipart"
- "strings"
- )
- func isValidTLSFile(file multipart.File) bool {
-
- contents, err := io.ReadAll(file)
- if err != nil {
-
- return false
- }
-
- block, _ := pem.Decode(contents)
- if block == nil {
-
- return false
- }
-
- if strings.Contains(block.Type, "CERTIFICATE") {
-
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
-
- return false
- }
-
- return cert.IsCA == false && cert.KeyUsage&x509.KeyUsageDigitalSignature != 0 && cert.KeyUsage&x509.KeyUsageKeyEncipherment != 0
- } else if strings.Contains(block.Type, "PRIVATE KEY") {
-
- _, err := x509.ParsePKCS1PrivateKey(block.Bytes)
- if err != nil {
-
- return false
- }
- return true
- } else {
- return false
- }
- }
|