forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
44 lines (38 loc) · 1.05 KB
/
identity.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
package x509util
import (
"crypto/x509"
"io/ioutil"
"github.com/pkg/errors"
"github.com/smallstep/cli/crypto/pemutil"
)
// Identity contains a public/private x509 certificate/key pair.
type Identity struct {
Crt *x509.Certificate
Key interface{}
}
// NewIdentity returns a new Identity.
func NewIdentity(c *x509.Certificate, k interface{}) *Identity {
return &Identity{
Crt: c,
Key: k,
}
}
// LoadIdentityFromDisk load a public certificate and private key (both in PEM
// format) from disk.
func LoadIdentityFromDisk(crtPath, keyPath string, pemOpts ...pemutil.Options) (*Identity, error) {
// Read using stepx509 to parse the PublicKey
crt, err := pemutil.ReadStepCertificate(crtPath)
if err != nil {
return nil, errors.WithStack(err)
}
keyBytes, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, errors.WithStack(err)
}
pemOpts = append(pemOpts, pemutil.WithFilename(keyPath))
key, err := pemutil.Parse(keyBytes, pemOpts...)
if err != nil {
return nil, errors.WithStack(err)
}
return NewIdentity(ToX509Certificate(crt), key), nil
}