-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
226 lines (201 loc) · 5.22 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Idnaf MTLS PKCS11 Client
*/
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
"syscall"
"github.com/ThalesIgnite/crypto11"
"github.com/miekg/pkcs11"
"golang.org/x/term"
)
// Check if source contains data 'find'
func contains(source []uint, find uint) bool {
for _, v := range source {
if v == find {
return true
}
}
return false
}
// Main function
func main() {
var pkcsLibFile, tokenSerial, tokenPin, uri, httpMethod, userAgent, outputFile string
fmt.Println("Idnaf MTLS PKCS11 HTTPS Client")
flag.StringVar(&pkcsLibFile, "pkcs11", "", "pkcs11 library path")
flag.StringVar(&tokenPin, "token-pin", "", "token pin, it will prompt to user if it is empty")
flag.StringVar(&uri, "uri", "", "Target URI")
flag.StringVar(&httpMethod, "http-method", "GET", "HTTP request method")
flag.StringVar(&userAgent, "ua", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36", "User agent (optional)")
flag.StringVar(&outputFile, "output", "", "Output to file (optional), if empty it will be printed to stdout")
flag.Parse()
if len(os.Args) == 1 {
fmt.Println("Use -h to see full commands")
os.Exit(1)
}
fmt.Println("Configuration:")
fmt.Println("PKCS11 lib : ", pkcsLibFile)
fmt.Println("HTTP method : ", httpMethod)
fmt.Println("URI : ", uri)
if _, err := os.Stat(pkcsLibFile); err != nil {
log.Fatalln(err)
}
p11 := pkcs11.New(pkcsLibFile)
err := p11.Initialize()
if err != nil {
p11.Finalize()
p11.Destroy()
log.Fatalln(err)
}
slots, err := p11.GetSlotList(true)
if err != nil {
p11.Finalize()
p11.Destroy()
log.Fatalln(err)
}
if len(slots) <= 0 {
log.Fatalf("Slot is empty, please connect your token", nil)
}
var selectedSlot uint = math.MaxUint
for _, slot := range slots {
tokenInfo, err := p11.GetTokenInfo(slot)
if err != nil {
p11.Finalize()
p11.Destroy()
log.Fatalln(err)
}
if selectedSlot == math.MaxUint {
selectedSlot = slot
}
fmt.Println("Slot #", slot)
fmt.Println(" - Label : ", tokenInfo.Label)
fmt.Println(" - Model : ", tokenInfo.Model)
fmt.Println(" - Serial Number : ", tokenInfo.SerialNumber)
fmt.Println(" - Manufacturer : ", tokenInfo.ManufacturerID)
fmt.Println(" - HW version : ", tokenInfo.HardwareVersion.Major, ".", tokenInfo.HardwareVersion.Minor)
}
if len(slots) > 1 {
fmt.Print("Select slot: ")
_, err := fmt.Scanf("%d", &selectedSlot)
if err != nil {
p11.Finalize()
p11.Destroy()
log.Fatal(err)
}
if !contains(slots, selectedSlot) {
log.Println("Slot selection is false", nil)
}
} else {
fmt.Println("Automatically select slot: ", selectedSlot)
}
tokenInfo, err := p11.GetTokenInfo(selectedSlot)
if err != nil {
log.Fatalln(err)
}
tokenSerial = tokenInfo.SerialNumber
p11.Finalize()
p11.Destroy()
if tokenPin == "" {
fmt.Print("Enter PIN: ")
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatalln(err)
}
fmt.Println()
tokenPin = string(bytepw)
}
config := crypto11.Config{
Path: pkcsLibFile,
TokenSerial: tokenSerial,
Pin: tokenPin,
}
fmt.Println("Configuring crypto11")
context, err := crypto11.Configure(&config)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Find all paired certificates")
certificates, err := context.FindAllPairedCertificates()
if err != nil {
log.Fatalln(err)
}
if len(certificates) == 0 {
log.Fatalf("No certificates found!", nil)
}
fmt.Println("Total certificates found: ", len(certificates))
index := 0
for _, cert := range certificates {
fmt.Println(index, " ", cert.Leaf.Subject)
fmt.Println(" Validity: ", cert.Leaf.NotBefore.Local(), " to ", cert.Leaf.NotAfter.Local())
fmt.Println(" Serial Number: ", cert.Leaf.SerialNumber.String())
fmt.Println(" Issuer: ", cert.Leaf.Issuer)
index++
}
selectedCert := 0
if len(certificates) > 1 {
fmt.Print("Select certificate number: ")
reader := bufio.NewReader(os.Stdin)
str, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
str = strings.TrimSpace(str)
selection, err := strconv.Atoi(str)
if err != nil {
log.Fatal(err)
}
selectedCert = selection
}
cert := certificates[selectedCert]
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
Renegotiation: tls.RenegotiateOnceAsClient,
},
},
}
req, err := http.NewRequest(httpMethod, uri, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("User-Agent", userAgent)
for k, v := range req.Header {
fmt.Println("> ", k, ":", v)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
fmt.Println("HTTP Status code: ", resp.StatusCode)
for k, v := range resp.Header {
fmt.Println("< ", k, ":", v)
}
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if outputFile == "" {
bodyString := string(bodyBytes)
fmt.Println(bodyString)
} else {
fmt.Println("Writing output to ", outputFile)
err = os.WriteFile(outputFile, bodyBytes, 0644)
if err != nil {
log.Fatal(err)
}
}
}
}