-
Notifications
You must be signed in to change notification settings - Fork 3
/
pem_writer.go
59 lines (54 loc) · 1.5 KB
/
pem_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io"
"reflect"
"github.com/pkg/errors"
"github.com/rakutentech/jwk-go/okp"
)
func pemBlockFor(obj interface{}) (*pem.Block, error) {
var err error
var der []byte
switch o := obj.(type) {
case *rsa.PrivateKey:
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(o)}, nil
case *ecdsa.PrivateKey:
der, err = x509.MarshalECPrivateKey(o)
if err != nil {
return nil, errors.Wrap(err, "Unable to marshal ECDSA private key")
}
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: der}, nil
case *rsa.PublicKey, *ecdsa.PublicKey:
der, err = x509.MarshalPKIXPublicKey(obj)
if err != nil {
return nil, errors.Wrap(err, "Unable to marshal RSA private key")
}
return &pem.Block{Type: "PUBLIC KEY", Bytes: der}, nil
case okp.CurveOctetKeyPair:
// ASN.1 representation for PEM is currently an IETF dratf:
// https://tools.ietf.org/id/draft-ietf-curdle-pkix-04.txt
// It's bothersome to implement so I skip it for now
return nil, nil
default:
return nil, errors.Errorf("Unknown key type: %v", reflect.TypeOf(obj))
}
}
func writePem(writer io.Writer, obj interface{}) error {
block, err := pemBlockFor(obj)
if err != nil {
return err
} else if block == nil {
return nil // Skipped PEM for this type
}
return pem.Encode(writer, block)
}
func writePemFor(objInfo ObjectInfo, obj interface{}) error {
w, err := writerFor(objInfo)
if err != nil {
return err
}
return writePem(w, obj)
}