-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
132 lines (115 loc) · 3.05 KB
/
func.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gocrypto
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
// ParsePrivateKey parses an RSA private key from PKCS #1 or PKCS #8 format.
// If isPEM is true, it will attempt to decode the key as PEM.
func ParsePrivateKey(key []byte, isPEM bool) (*RSAKey, error) {
var raw []byte
// Decode the PEM block
if isPEM {
block, _ := pem.Decode(key)
if block == nil {
return nil, errors.New("failed to decode PEM block")
}
raw = make([]byte, len(block.Bytes))
copy(raw, block.Bytes)
} else {
raw = make([]byte, len(key))
copy(raw, key)
}
// Parse the key PKCS #1
private, err := x509.ParsePKCS1PrivateKey(raw)
if err == nil {
return &RSAKey{key: private}, nil
}
// If not PKCS #1, try parsing as PKCS #8
parsed, err := x509.ParsePKCS8PrivateKey(raw)
if err != nil {
return nil, err
} else if private, ok := parsed.(*rsa.PrivateKey); !ok {
return nil, fmt.Errorf("failed to parse RSA private key")
} else {
return &RSAKey{key: private}, nil
}
}
// ParsePublicKey parses an RSA public key from PKIX or PKCS #1 format.
// If isPEM is true, it will attempt to decode the key as PEM.
func ParsePublicKey(key []byte, isPEM bool) (*rsa.PublicKey, error) {
var raw []byte
// Decode the PEM block
if isPEM {
block, _ := pem.Decode(key)
if block == nil {
return nil, errors.New("failed to decode PEM block")
}
raw = make([]byte, len(block.Bytes))
copy(raw, block.Bytes)
} else {
raw = make([]byte, len(key))
copy(raw, key)
}
public, err := x509.ParsePKCS1PublicKey(raw)
if err == nil {
return public, nil
}
// Parse the key
parsed, err := x509.ParsePKIXPublicKey(raw)
if err != nil {
return nil, err
} else if public, ok := parsed.(*rsa.PublicKey); !ok {
return nil, fmt.Errorf("failed to parse RSA public key")
} else {
return public, nil
}
}
// ParseCertificateRequest parses an x509 certificate request from CSR.
// If isPEM is true, it will attempt to decode the CSR as PEM.
func ParseCertificateRequest(csr []byte, isPEM bool) (*x509.CertificateRequest, error) {
var raw []byte
// Decode the PEM block
if isPEM {
block, _ := pem.Decode(csr)
if block == nil {
return nil, errors.New("failed to decode PEM block")
}
raw = make([]byte, len(block.Bytes))
copy(raw, block.Bytes)
} else {
raw = make([]byte, len(csr))
copy(raw, csr)
}
// Parse the Certificate Signing Request
request, err := x509.ParseCertificateRequest(raw)
if err != nil {
return nil, err
}
return request, nil
}
// ParseCertificate parses an x509 certificate from CSR.
// If isPEM is true, it will attempt to decode the CSR as PEM.
func ParseCertificate(csr []byte, isPEM bool) (*x509.Certificate, error) {
var raw []byte
// Decode the PEM block
if isPEM {
block, _ := pem.Decode(csr)
if block == nil {
return nil, errors.New("failed to decode PEM block")
}
raw = make([]byte, len(block.Bytes))
copy(raw, block.Bytes)
} else {
raw = make([]byte, len(csr))
copy(raw, csr)
}
// Parse the certificate
cert, err := x509.ParseCertificate(raw)
if err != nil {
return nil, err
}
return cert, nil
}