1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-19 10:26:10 -04:00
v2fly/external/github.com/lucas-clemente/quic-go/internal/testdata/cert.go

58 lines
1.2 KiB
Go
Raw Normal View History

2018-11-20 17:51:25 -05:00
package testdata
import (
"crypto/tls"
2019-01-02 07:01:06 -05:00
"crypto/x509"
"encoding/pem"
"io/ioutil"
2018-11-20 17:51:25 -05:00
"path"
"runtime"
)
var certPath string
func init() {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("Failed to get current frame")
}
2019-01-02 07:01:06 -05:00
certPath = path.Dir(filename)
2018-11-20 17:51:25 -05:00
}
2019-01-02 07:01:06 -05:00
// GetCertificatePaths returns the paths to certificate and key
2018-11-20 17:51:25 -05:00
func GetCertificatePaths() (string, string) {
2019-01-02 07:01:06 -05:00
return path.Join(certPath, "cert.pem"), path.Join(certPath, "priv.key")
2018-11-20 17:51:25 -05:00
}
// GetTLSConfig returns a tls config for quic.clemente.io
func GetTLSConfig() *tls.Config {
cert, err := tls.LoadX509KeyPair(GetCertificatePaths())
if err != nil {
panic(err)
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
2019-01-02 07:01:06 -05:00
// GetRootCA returns an x509.CertPool containing the CA certificate
func GetRootCA() *x509.CertPool {
caCertPath := path.Join(certPath, "ca.pem")
caCertRaw, err := ioutil.ReadFile(caCertPath)
if err != nil {
panic(err)
}
p, _ := pem.Decode(caCertRaw)
if p.Type != "CERTIFICATE" {
panic("expected a certificate")
}
caCert, err := x509.ParseCertificate(p.Bytes)
2018-11-20 17:51:25 -05:00
if err != nil {
panic(err)
}
2019-01-02 07:01:06 -05:00
certPool := x509.NewCertPool()
certPool.AddCert(caCert)
return certPool
2018-11-20 17:51:25 -05:00
}