|
| 1 | +package ca |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/ed25519" |
| 7 | + "crypto/elliptic" |
| 8 | + "crypto/rand" |
| 9 | + "crypto/rsa" |
| 10 | + "crypto/tls" |
| 11 | + "crypto/x509" |
| 12 | + "encoding/pem" |
| 13 | + "fmt" |
| 14 | + "math/big" |
| 15 | +) |
| 16 | + |
| 17 | +type Path interface { |
| 18 | + Resolve(path string) string |
| 19 | +} |
| 20 | + |
| 21 | +// LoadTLSKeyPair loads a TLS key pair from the provided certificate and private key data or file paths, supporting fallback resolution. |
| 22 | +// Returns a tls.Certificate and an error, where the error indicates issues during parsing or file loading. |
| 23 | +// If both certificate and privateKey are empty, generates a random TLS RSA key pair. |
| 24 | +// Accepts a Path interface for resolving file paths when necessary. |
| 25 | +func LoadTLSKeyPair(certificate, privateKey string, path Path) (tls.Certificate, error) { |
| 26 | + if certificate == "" && privateKey == "" { |
| 27 | + var err error |
| 28 | + certificate, privateKey, _, err = NewRandomTLSKeyPair(KeyPairTypeRSA) |
| 29 | + if err != nil { |
| 30 | + return tls.Certificate{}, err |
| 31 | + } |
| 32 | + } |
| 33 | + cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey)) |
| 34 | + if painTextErr == nil { |
| 35 | + return cert, nil |
| 36 | + } |
| 37 | + if path == nil { |
| 38 | + return tls.Certificate{}, painTextErr |
| 39 | + } |
| 40 | + |
| 41 | + certificate = path.Resolve(certificate) |
| 42 | + privateKey = path.Resolve(privateKey) |
| 43 | + cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey) |
| 44 | + if loadErr != nil { |
| 45 | + return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error()) |
| 46 | + } |
| 47 | + return cert, nil |
| 48 | +} |
| 49 | + |
| 50 | +type KeyPairType string |
| 51 | + |
| 52 | +const ( |
| 53 | + KeyPairTypeRSA KeyPairType = "rsa" |
| 54 | + KeyPairTypeP256 KeyPairType = "p256" |
| 55 | + KeyPairTypeP384 KeyPairType = "p384" |
| 56 | + KeyPairTypeEd25519 KeyPairType = "ed25519" |
| 57 | +) |
| 58 | + |
| 59 | +// NewRandomTLSKeyPair generates a random TLS key pair based on the specified KeyPairType and returns it with a SHA256 fingerprint. |
| 60 | +// Note: Most browsers do not support KeyPairTypeEd25519 type of certificate, and utls.UConn will also reject this type of certificate. |
| 61 | +func NewRandomTLSKeyPair(keyPairType KeyPairType) (certificate string, privateKey string, fingerprint string, err error) { |
| 62 | + var key crypto.Signer |
| 63 | + switch keyPairType { |
| 64 | + case KeyPairTypeRSA: |
| 65 | + key, err = rsa.GenerateKey(rand.Reader, 2048) |
| 66 | + case KeyPairTypeP256: |
| 67 | + key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 68 | + case KeyPairTypeP384: |
| 69 | + key, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| 70 | + case KeyPairTypeEd25519: |
| 71 | + _, key, err = ed25519.GenerateKey(rand.Reader) |
| 72 | + default: // fallback to KeyPairTypeRSA |
| 73 | + key, err = rsa.GenerateKey(rand.Reader, 2048) |
| 74 | + } |
| 75 | + if err != nil { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + template := x509.Certificate{SerialNumber: big.NewInt(1)} |
| 80 | + certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, key.Public(), key) |
| 81 | + if err != nil { |
| 82 | + return |
| 83 | + } |
| 84 | + privBytes, err := x509.MarshalPKCS8PrivateKey(key) |
| 85 | + if err != nil { |
| 86 | + return |
| 87 | + } |
| 88 | + fingerprint = CalculateFingerprint(certDER) |
| 89 | + privateKey = string(pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})) |
| 90 | + certificate = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})) |
| 91 | + return |
| 92 | +} |
0 commit comments