forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
45 lines (38 loc) · 1.04 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
45
package x509util
import (
"io/ioutil"
"github.com/pkg/errors"
"github.com/smallstep/cli/crypto/pemutil"
"github.com/smallstep/cli/pkg/x509"
)
// 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) {
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),
pemutil.WithStepCrypto())
key, err := pemutil.Parse(keyBytes, pemOpts...)
if err != nil {
return nil, errors.WithStack(err)
}
return NewIdentity(crt, key), nil
}