-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (148 loc) · 3.27 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/tls"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
)
var issuerURL string
var verbose bool
var mdfive bool
type Thumbrint struct {
Thumbprint string `json:"thumbprint"`
}
type partialOIDCConfig struct {
JwksURI string `json:"jwks_uri"`
}
func GetOIDCConfigURL(issuerURL string) (string, error) {
parsedURL, err := url.Parse(issuerURL)
if err != nil {
return "", err
}
parsedURL.Path = path.Join(parsedURL.Path, ".well-known", "openid-configuration")
openidConfigURL := parsedURL.String()
return openidConfigURL, nil
}
func getJwksURL(openidConfigURL string) (string, error) {
resp, err := http.Get(openidConfigURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var partialOIDCConfig partialOIDCConfig
if err := json.Unmarshal(body, &partialOIDCConfig); err != nil {
return "", err
}
return partialOIDCConfig.JwksURI, nil
}
func sha1Hash(data []byte) string {
hasher := sha1.New()
hasher.Write(data)
hashed := hasher.Sum(nil)
return hex.EncodeToString(hashed)
}
func mdpFingerprint(data []byte) string {
fingerprint := md5.Sum(data)
var buf bytes.Buffer
for i, f := range fingerprint {
if i > 0 {
fmt.Fprintf(&buf, ":")
}
fmt.Fprintf(&buf, "%02X", f)
}
return buf.String()
}
func getThumbprint(jwksURL string) (string, error) {
parsedURL, err := url.Parse(jwksURL)
if err != nil {
return "", err
}
hostname := parsedURL.Host
if parsedURL.Port() == "" {
hostname = net.JoinHostPort(hostname, "443")
}
if verbose {
fmt.Println("Hostname")
fmt.Println(hostname)
}
tlsConfig := tls.Config{ServerName: parsedURL.Host}
conn, err := tls.Dial("tcp", hostname, &tlsConfig)
if err != nil {
return "", err
}
defer conn.Close()
state := conn.ConnectionState()
peerCerts := state.PeerCertificates
numCerts := len(peerCerts)
if numCerts == 0 {
return "", err
}
// root CA certificate is the last one in the list
root := peerCerts[numCerts-1]
if verbose {
fmt.Println("root cert")
fmt.Println(root)
}
if mdfive {
md5F := mdpFingerprint(root.Raw)
fmt.Println(md5F)
}
return sha1Hash(root.Raw), nil
}
func GetOIDCThumb(issuerURL string) (*Thumbrint, error) {
openidConfigURL, err := GetOIDCConfigURL(issuerURL)
if err != nil {
return nil, err
}
if verbose {
fmt.Println("openidConfigURL")
fmt.Println(openidConfigURL)
}
jwksURL, err := getJwksURL(openidConfigURL)
if err != nil {
return nil, err
}
if verbose {
fmt.Println("jwksURL")
fmt.Println(jwksURL)
}
thumb, err := getThumbprint(jwksURL)
if err != nil {
return nil, err
}
if verbose {
fmt.Printf("Retrieved OIDC Issuer (%s) CA Thumbprint: %s\n", issuerURL, thumb)
}
return &Thumbrint{Thumbprint: thumb}, nil
}
func main() {
flag.StringVar(&issuerURL, "issuerURL", "", "issuer URL to get thumbrint from")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.BoolVar(&mdfive, "mdfive", false, "md5 thumbprint")
flag.Parse()
thumb, err := GetOIDCThumb(issuerURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
data, err := json.Marshal(thumb)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(data))
}