pkiapi/internal/storage/util.go

38 lines
870 B
Go

package storage
import (
"crypto/x509"
"encoding/pem"
)
// marshalPrivateKey encode une clé privée en bytes
func marshalPrivateKey(privateKey interface{}) ([]byte, error) {
privKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
return privKeyBytes, nil
}
// unmarshalPrivateKey décode une clé privée depuis bytes
func unmarshalPrivateKey(privKeyBytes []byte) (interface{}, error) {
return x509.ParsePKCS8PrivateKey(privKeyBytes)
}
// parseCertificate parse un certificat X.509
func parseCertificate(certBytes []byte) (*x509.Certificate, error) {
// Essayer de parser en DER
cert, err := x509.ParseCertificate(certBytes)
if err == nil {
return cert, nil
}
// Essayer de parser en PEM
block, _ := pem.Decode(certBytes)
if block != nil {
return x509.ParseCertificate(block.Bytes)
}
return nil, err
}