Skip to content
File

Blob: spec/pki/pem.go

go30 lines
1package pki
2 
3import (
4 "bytes"
5 "crypto/ed25519"
6 "crypto/x509"
7 "encoding/pem"
8)
9 
10func UnmarshalPrivateKey(pemBytes []byte) (ed25519.PrivateKey, error) {
11 p, _ := pem.Decode(pemBytes)
12 key, err := x509.ParsePKCS8PrivateKey(p.Bytes)
13 if err != nil {
14 return nil, err
15 }
16 if ed, ok := key.(ed25519.PrivateKey); ok {
17 return ed, nil
18 }
19 return nil, x509.ErrUnsupportedAlgorithm
20}
21 
22func MarshalCertificate(derBytes []byte) (pemBytes []byte) {
23 certPEM := new(bytes.Buffer)
24 pem.Encode(certPEM, &pem.Block{
25 Type: "CERTIFICATE",
26 Bytes: derBytes,
27 })
28 return certPEM.Bytes()
29}