| 1 |
package x509util |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"io/ioutil" |
| 6 |
|
| 7 |
"github.com/pkg/errors" |
| 8 |
"github.com/smallstep/cli/crypto/pemutil" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
type Identity struct { |
| 13 |
Crt *x509.Certificate |
| 14 |
Key interface{} |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
func NewIdentity(c *x509.Certificate, k interface{}) *Identity { |
| 19 |
return &Identity{ |
| 20 |
Crt: c, |
| 21 |
Key: k, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func LoadIdentityFromDisk(crtPath, keyPath string, pemOpts ...pemutil.Options) (*Identity, error) { |
| 28 |
|
| 29 |
crt, err := pemutil.ReadCertificate(crtPath) |
| 30 |
if err != nil { |
| 31 |
return nil, errors.WithStack(err) |
| 32 |
} |
| 33 |
keyBytes, err := ioutil.ReadFile(keyPath) |
| 34 |
if err != nil { |
| 35 |
return nil, errors.WithStack(err) |
| 36 |
} |
| 37 |
pemOpts = append(pemOpts, pemutil.WithFilename(keyPath)) |
| 38 |
key, err := pemutil.Parse(keyBytes, pemOpts...) |
| 39 |
if err != nil { |
| 40 |
return nil, errors.WithStack(err) |
| 41 |
} |
| 42 |
|
| 43 |
return NewIdentity(crt, key), nil |
| 44 |
} |