2022-03-28 12:37:58 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-06-15 14:49:31 +00:00
|
|
|
"crypto/elliptic"
|
2022-03-28 12:37:58 +00:00
|
|
|
"crypto/rand"
|
2022-06-15 14:49:31 +00:00
|
|
|
"crypto/tls"
|
2022-03-28 12:37:58 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"math/big"
|
2022-06-15 14:49:31 +00:00
|
|
|
"net"
|
2022-03-28 12:37:58 +00:00
|
|
|
"time"
|
2023-11-07 01:51:15 +00:00
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2022-03-28 12:37:58 +00:00
|
|
|
)
|
|
|
|
|
2023-06-02 02:44:24 +00:00
|
|
|
var globalMediaCertificate *tls.Certificate = nil
|
|
|
|
var globalMediaPem string
|
2022-06-15 14:49:31 +00:00
|
|
|
|
|
|
|
func makeRandomSerialNumber() (*big.Int, error) {
|
2022-03-28 12:37:58 +00:00
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
2022-06-15 14:49:31 +00:00
|
|
|
return rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
}
|
2022-03-28 12:37:58 +00:00
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
func GenerateX509Cert(sn *big.Int, from, to time.Time, IPAddresses []net.IP, DNSNames []string) *x509.Certificate {
|
|
|
|
return &x509.Certificate{
|
2022-06-15 14:49:31 +00:00
|
|
|
SerialNumber: sn,
|
2022-03-28 12:37:58 +00:00
|
|
|
Subject: pkix.Name{Organization: []string{"Self-signed cert"}},
|
|
|
|
NotBefore: from,
|
|
|
|
NotAfter: to,
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
2023-08-22 16:18:14 +00:00
|
|
|
IPAddresses: IPAddresses,
|
|
|
|
DNSNames: DNSNames,
|
2022-03-28 12:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateX509PEMs(cert *x509.Certificate, key *ecdsa.PrivateKey) (certPem, keyPem []byte, err error) {
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
certPem = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
|
|
|
|
privBytes, err := x509.MarshalPKCS8PrivateKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyPem = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
func GenerateTLSCert(notBefore, notAfter time.Time, IPAddresses []net.IP, DNSNames []string) (*tls.Certificate, []byte, error) {
|
2022-06-15 14:49:31 +00:00
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
2023-06-02 02:44:24 +00:00
|
|
|
return nil, nil, err
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sn, err := makeRandomSerialNumber()
|
|
|
|
if err != nil {
|
2023-06-02 02:44:24 +00:00
|
|
|
return nil, nil, err
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
cert := GenerateX509Cert(sn, notBefore, notAfter, IPAddresses, DNSNames)
|
2022-06-15 14:49:31 +00:00
|
|
|
certPem, keyPem, err := GenerateX509PEMs(cert, priv)
|
|
|
|
if err != nil {
|
2023-06-02 02:44:24 +00:00
|
|
|
return nil, nil, err
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
finalCert, err := tls.X509KeyPair(certPem, keyPem)
|
2023-06-02 02:44:24 +00:00
|
|
|
return &finalCert, certPem, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMediaTLSCert() error {
|
|
|
|
if globalMediaCertificate != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-04 07:54:51 +00:00
|
|
|
now := time.Now()
|
|
|
|
notBefore := now.Add(-365 * 24 * time.Hour * 100)
|
|
|
|
notAfter := now.Add(365 * 24 * time.Hour * 100)
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug("generate media cert",
|
|
|
|
logutils.UnixTimeMs("system time", time.Now()),
|
|
|
|
logutils.UnixTimeMs("cert notBefore", notBefore),
|
|
|
|
logutils.UnixTimeMs("cert notAfter", notAfter),
|
|
|
|
)
|
2023-11-13 19:06:32 +00:00
|
|
|
finalCert, certPem, err := GenerateTLSCert(notBefore, notAfter, []net.IP{}, []string{Localhost})
|
2022-06-15 14:49:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-02 02:44:24 +00:00
|
|
|
globalMediaCertificate = finalCert
|
|
|
|
globalMediaPem = string(certPem)
|
2022-06-15 14:49:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-02 02:44:24 +00:00
|
|
|
func PublicMediaTLSCert() (string, error) {
|
|
|
|
err := generateMediaTLSCert()
|
2022-06-15 14:49:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-06-02 02:44:24 +00:00
|
|
|
return globalMediaPem, nil
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToECDSA takes a []byte of D and uses it to create an ecdsa.PublicKey on the elliptic.P256 curve
|
|
|
|
// this function is basically a P256 curve version of eth-node/crypto.ToECDSA without all the nice validation
|
|
|
|
func ToECDSA(d []byte) *ecdsa.PrivateKey {
|
|
|
|
k := new(ecdsa.PrivateKey)
|
|
|
|
k.D = new(big.Int).SetBytes(d)
|
|
|
|
k.PublicKey.Curve = elliptic.P256()
|
|
|
|
|
|
|
|
k.PublicKey.X, k.PublicKey.Y = k.PublicKey.Curve.ScalarBaseMult(d)
|
|
|
|
return k
|
|
|
|
}
|