-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
46 lines (40 loc) · 1.27 KB
/
parse.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
package pki
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/jetstack-experimental/cert-manager/pkg/util/errors"
)
func DecodePKCS1PrivateKeyBytes(keyBytes []byte) (*rsa.PrivateKey, error) {
// decode the private key pem
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, errors.NewInvalidData("error decoding private key PEM block")
}
// parse the private key
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, errors.NewInvalidData("error parsing private key: %s", err.Error())
}
// validate the private key
if err = key.Validate(); err != nil {
return nil, errors.NewInvalidData("private key failed validation: %s", err.Error())
}
return key, nil
}
func DecodeX509CertificateBytes(certBytes []byte) (*x509.Certificate, error) {
// decode the tls certificate pem
block, _ := pem.Decode(certBytes)
if block == nil {
return nil, errors.NewInvalidData("error decoding cert PEM block")
}
// parse the tls certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.NewInvalidData("error parsing TLS certificate: %s", err.Error())
}
return cert, nil
}
func DecodeDERCertificateBytes(derBytes []byte) (*x509.Certificate, error) {
return x509.ParseCertificate(derBytes)
}