-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_util.go
167 lines (148 loc) · 4.49 KB
/
certificate_util.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package jwt_certificate
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
jwtgo "github.com/taliesins/traefik-plugin-oidc/jwt"
"reflect"
)
func GetPrivateKey(privateKeyData []byte) (privateKey interface{}, err error) {
privateKeyBlockBytes := privateKeyData
privateKeyBlock, rest := pem.Decode(privateKeyData)
if !(privateKeyBlock == nil || len(rest) > 0) {
switch privateKeyBlock.Type {
case "PRIVATE KEY":
privateKeyBlockBytes = privateKeyBlock.Bytes
case "RSA PRIVATE KEY":
privateKeyBlockBytes = privateKeyBlock.Bytes
case "EC PRIVATE KEY":
privateKeyBlockBytes = privateKeyBlock.Bytes
default:
return nil, fmt.Errorf("Unsupported private key type %q", privateKeyBlock.Type)
}
} else {
privateKeyBlockBytes, err = x509.MarshalPKCS8PrivateKey(privateKeyData)
if err != nil {
privateKeyBlockBytes = privateKeyData
}
}
privateKey, errParsePKCS8PrivateKey := x509.ParsePKCS8PrivateKey(privateKeyBlockBytes)
if errParsePKCS8PrivateKey == nil {
return privateKey, nil
}
privateKey, errParsePKCS1PrivateKey := x509.ParsePKCS1PrivateKey(privateKeyBlockBytes)
if errParsePKCS1PrivateKey == nil {
return privateKey, nil
}
privateKey, errParseECPrivateKey := x509.ParseECPrivateKey(privateKeyBlockBytes)
if errParseECPrivateKey == nil {
return privateKey, nil
}
return nil, errParsePKCS8PrivateKey
}
func GetPublicKey(publicKeyData []byte) (publicKey interface{}, err error) {
publicKeyBlockBytes := publicKeyData
publicKeyBlock, rest := pem.Decode(publicKeyData)
if !(publicKeyBlock == nil || len(rest) > 0) {
switch publicKeyBlock.Type {
case "CERTIFICATE":
{
publicKeyBlockBytes = publicKeyBlock.Bytes
}
case "PUBLIC KEY":
{
publicKeyBlockBytes = publicKeyBlock.Bytes
}
default:
return nil, fmt.Errorf("Unsupported private key type %q", publicKeyBlock.Type)
}
} else {
publicKeyBlockBytes, err = x509.MarshalPKIXPublicKey(publicKeyData)
if err != nil {
publicKeyBlockBytes = publicKeyData
}
}
publicKey, errParsePKIXPublicKey := x509.ParsePKIXPublicKey(publicKeyBlockBytes)
if errParsePKIXPublicKey == nil {
return publicKey, nil
}
publicKey, errParsePKCS1PublicKey := x509.ParsePKCS1PublicKey(publicKeyBlockBytes)
if errParsePKCS1PublicKey == nil {
return publicKey, nil
}
cert, err := x509.ParseCertificate(publicKeyBlockBytes)
if err == nil {
return cert.PublicKey, nil
}
return nil, errParsePKIXPublicKey
}
func GetJwtParametersFromPublicKey(publicKey interface{}) (string, string, string, x509.SignatureAlgorithm, error) {
var keyType string
var algorithm string
var curve string
var signatureAlgorithm x509.SignatureAlgorithm
switch key := publicKey.(type) {
case *rsa.PublicKey:
length := publicKey.(*rsa.PublicKey).N.BitLen() / 8
/*
// https://www.scottbrady91.com/jose/jwts-which-signing-algorithm-should-i-use
//Deterministic
switch length {
case 256:
signatureAlgorithm = x509.SHA256WithRSA
algorithm = jwtgo.SigningMethodRS256.Alg()
case 384:
signatureAlgorithm = x509.SHA384WithRSA
algorithm = jwtgo.SigningMethodRS384.Alg()
case 512:
signatureAlgorithm = x509.SHA512WithRSA
algorithm = jwtgo.SigningMethodRS512.Alg()
}
*/
//Probabilistic
switch length {
case 256:
signatureAlgorithm = x509.SHA256WithRSAPSS
algorithm = jwtgo.SigningMethodPS256.Alg()
case 384:
signatureAlgorithm = x509.SHA384WithRSAPSS
algorithm = jwtgo.SigningMethodPS384.Alg()
case 512:
signatureAlgorithm = x509.SHA512WithRSAPSS
algorithm = jwtgo.SigningMethodPS512.Alg()
}
keyType = "RSA"
case *ecdsa.PublicKey:
length := publicKey.(*ecdsa.PublicKey).Curve.Params().BitSize
switch length {
case 256:
signatureAlgorithm = x509.ECDSAWithSHA256
algorithm = jwtgo.SigningMethodES256.Alg()
case 384:
signatureAlgorithm = x509.ECDSAWithSHA384
algorithm = jwtgo.SigningMethodES384.Alg()
case 521:
signatureAlgorithm = x509.ECDSAWithSHA512
algorithm = jwtgo.SigningMethodES512.Alg()
}
keyType = "EC"
/*
case *dsa.PublicKey:
length := publicKey.(*dsa.PublicKey).P.BitLen()
signatureAlgorithm = x509.DSAWithSHA256
algorithm = not supported
keyType = "DSA"
*/
case *ed25519.PublicKey:
curve = "Ed25519"
signatureAlgorithm = x509.PureEd25519
algorithm = (&jwtgo.SigningMethodEd25519{}).Alg()
keyType = "OKP"
default:
return "", "", "", x509.UnknownSignatureAlgorithm, fmt.Errorf("unknown private key type '%s'", reflect.TypeOf(key))
}
return keyType, algorithm, curve, signatureAlgorithm, nil
}