summaryrefslogtreecommitdiff
path: root/pkg/vpn/certs.go
blob: 300871e03edc7969dacfb9bc5adf21342e76582e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package vpn

import (
	"crypto/x509"
	"encoding/pem"
	"io/ioutil"
	"log"
	"time"
)

func isValidCert(path string) bool {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return false
	}
	// skip private key, but there should be one
	_, rest := pem.Decode(data)
	certBlock, rest := pem.Decode(rest)
	if len(rest) != 0 {
		log.Println("ERROR bad cert data")
		return false
	}
	cert, err := x509.ParseCertificate(certBlock.Bytes)
	loc, _ := time.LoadLocation("UTC")
	expires := cert.NotAfter
	tomorrow := time.Now().In(loc).Add(24 * time.Hour)

	if !expires.After(tomorrow) {
		return false
	} else {
		log.Println("DEBUG We have a valid cert:", path)
		return true
	}
}