func (cert *Certificate) ExportRaw() []byte {
	return cert.raw
}
ExportRaw returns the raw format of the original certificate
package client
import (
	"bytes"
	"crypto/ecdsa"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"strings"
	"golang.org/x/crypto/pkcs12"
)
type CertificateKeyType string
const (
	CertificateKeyTypeRsa CertificateKeyType = "rsa"
	CertificateKeyTypeEcdsa = "ecdsa"
)
type Certificate struct {
	Certificates []*x509.Certificate
	PrivateKeyRaw   []byte
	PrivateKeyRsa   *rsa.PrivateKey
	PrivateKeyEcdsa *ecdsa.PrivateKey
	raw []byte
	PrivateKeyType CertificateKeyType
	HasPrivateKey bool
}
func NewCertificateFromPem(pem string) (*Certificate, error) {
	cert, err := importPem(pem, false)
	if err != nil {
		return nil, err
	}
	cert.raw = []byte(pem)
	return cert, nil
}
func NewCertificateFromPfx(pfx []byte, ensureServerFirst bool) (*Certificate, error) {
	pemList, err := pkcs12.ToPEM(pfx, "")
	if err != nil {
		return nil, fmt.Errorf("failed to convert pfx to pem, error: %+v", err)
	}
	var mergedPems bytes.Buffer
	for _, pemCert := range pemList {
		mergedPems.WriteString(string(pem.EncodeToMemory(pemCert)))
	}
	cert, err := importPem(mergedPems.String(), ensureServerFirst)
	if err != nil {
		return nil, err
	}
	cert.raw = pfx
	return cert, nil
}
func NewCertificateFromDer(der []byte) (*Certificate, error) {
	var cert Certificate
	pubCerts, err := x509.ParseCertificates(der)
	if err != nil {
		return nil, fmt.Errorf("failed to parse der certificate, error: *%+v", err)
	}
	cert.HasPrivateKey = false
	cert.Certificates = append(cert.Certificates, pubCerts...)
	cert.raw = der
	return &cert, nil
}
func (cert *Certificate) ExportPrivateKeyAsPem() ([]byte, error) {
	if !cert.HasPrivateKey {
		return nil, fmt.Errorf("certificate has no private key")
	}
	var derKey []byte
	var keyType string
	var err error
	switch cert.PrivateKeyType {
	case CertificateKeyTypeRsa:
		derKey = x509.MarshalPKCS1PrivateKey(cert.PrivateKeyRsa)
		keyType = "RSA PRIVATE KEY"
	case CertificateKeyTypeEcdsa:
		derKey, err = x509.MarshalECPrivateKey(cert.PrivateKeyEcdsa)
		if err != nil {
			return nil, err
		}
		keyType = "EC PRIVATE KEY"
	default:
		return nil, fmt.Errorf("private key type '%s' currently not supported for pem export", cert.PrivateKeyType)
	}
	privKeyBlock := &pem.Block{
		Type:  keyType,
		Bytes: derKey,
	}
	return pem.EncodeToMemory(privKeyBlock), nil
}
func (cert *Certificate) ExportPublicKeyAsPem() ([]byte, error) {
	if len(cert.Certificates) == 0 {
		return nil, fmt.Errorf("certificate has no public key")
	}
	var certs strings.Builder
	for _, pubCert := range cert.Certificates {
		privKeyBlock := &pem.Block{
			Type:  "CERTIFICATE",
			Bytes: pubCert.Raw,
		}
		certs.Write(pem.EncodeToMemory(privKeyBlock))
	}
	return []byte(certs.String()), nil
}