2022-12-03 14:14:15 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2023-01-18 12:06:36 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2025-04-16 09:39:52 +08:00
|
|
|
"crypto/sha256"
|
2022-12-03 14:14:15 +08:00
|
|
|
"crypto/tls"
|
2023-01-18 12:06:36 +08:00
|
|
|
"crypto/x509"
|
2025-04-16 09:39:52 +08:00
|
|
|
"encoding/hex"
|
2023-01-18 12:06:36 +08:00
|
|
|
"encoding/pem"
|
2022-12-03 14:14:15 +08:00
|
|
|
"fmt"
|
2023-01-18 12:06:36 +08:00
|
|
|
"math/big"
|
2022-12-03 14:14:15 +08:00
|
|
|
)
|
2022-12-04 23:05:13 +08:00
|
|
|
|
2023-10-01 12:04:34 +08:00
|
|
|
type Path interface {
|
|
|
|
Resolve(path string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseCert(certificate, privateKey string, path Path) (tls.Certificate, error) {
|
2023-05-26 23:00:54 +08:00
|
|
|
if certificate == "" && privateKey == "" {
|
2025-04-16 09:39:52 +08:00
|
|
|
var err error
|
|
|
|
certificate, privateKey, _, err = NewRandomTLSKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
return tls.Certificate{}, err
|
|
|
|
}
|
2023-01-18 12:06:36 +08:00
|
|
|
}
|
2022-12-03 14:14:15 +08:00
|
|
|
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
|
|
|
|
if painTextErr == nil {
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
2023-10-01 12:04:34 +08:00
|
|
|
certificate = path.Resolve(certificate)
|
|
|
|
privateKey = path.Resolve(privateKey)
|
2022-12-03 14:14:15 +08:00
|
|
|
cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey)
|
|
|
|
if loadErr != nil {
|
2022-12-04 23:05:13 +08:00
|
|
|
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
2022-12-03 14:14:15 +08:00
|
|
|
}
|
|
|
|
return cert, nil
|
2022-12-04 23:05:13 +08:00
|
|
|
}
|
2023-01-18 12:06:36 +08:00
|
|
|
|
2025-04-16 09:39:52 +08:00
|
|
|
func NewRandomTLSKeyPair() (certificate string, privateKey string, fingerprint string, err error) {
|
2023-01-18 12:06:36 +08:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
2025-04-16 09:39:52 +08:00
|
|
|
return
|
2023-01-18 12:06:36 +08:00
|
|
|
}
|
|
|
|
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
|
|
certDER, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
&template,
|
|
|
|
&template,
|
|
|
|
&key.PublicKey,
|
|
|
|
key)
|
|
|
|
if err != nil {
|
2025-04-16 09:39:52 +08:00
|
|
|
return
|
2023-01-18 12:06:36 +08:00
|
|
|
}
|
2025-04-16 09:39:52 +08:00
|
|
|
cert, err := x509.ParseCertificate(certDER)
|
2023-01-18 12:06:36 +08:00
|
|
|
if err != nil {
|
2025-04-16 09:39:52 +08:00
|
|
|
return
|
2023-01-18 12:06:36 +08:00
|
|
|
}
|
2025-04-16 09:39:52 +08:00
|
|
|
hash := sha256.Sum256(cert.Raw)
|
|
|
|
fingerprint = hex.EncodeToString(hash[:])
|
|
|
|
privateKey = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}))
|
|
|
|
certificate = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}))
|
|
|
|
return
|
2023-01-18 12:06:36 +08:00
|
|
|
}
|