123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package certvalidate
- import (
- "crypto/x509"
- "encoding/pem"
- "io"
- "mime/multipart"
- "strings"
- )
- func isValidTLSFile(file multipart.File) bool {
- // Read the contents of the uploaded file
- contents, err := io.ReadAll(file)
- if err != nil {
- // Handle the error
- return false
- }
- // Parse the contents of the file as a PEM-encoded certificate or key
- block, _ := pem.Decode(contents)
- if block == nil {
- // The file is not a valid PEM-encoded certificate or key
- return false
- }
- // Parse the certificate or key
- if strings.Contains(block.Type, "CERTIFICATE") {
- // The file contains a certificate
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- // Handle the error
- return false
- }
- // Check if the certificate is a valid TLS/SSL certificate
- return cert.IsCA == false && cert.KeyUsage&x509.KeyUsageDigitalSignature != 0 && cert.KeyUsage&x509.KeyUsageKeyEncipherment != 0
- } else if strings.Contains(block.Type, "PRIVATE KEY") {
- // The file contains a private key
- _, err := x509.ParsePKCS1PrivateKey(block.Bytes)
- if err != nil {
- // Handle the error
- return false
- }
- return true
- } else {
- return false
- }
- }
|