-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
398 lines (325 loc) · 10.2 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
/**libraries that the lock-firmware will use throught the code*/
import (
"fmt"
"flag"
"os"
"io"
"strings"
"bufio"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/asn1"
"crypto/rand"
"net/http"
"encoding/json"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"math/big"
"bytes"
"crypto/tls"
"crypto/sha256"
"strconv"
)
/**object Door; used to read/send json response from controller*/
type Door struct {
Id int64 `json:"id,omitempty"`
System int64 `json:"system,omitempty"`
KeyX *big.Int `json:"keyX,omitempty"`
KeyY *big.Int `json:"keyY,omitempty"`
Challenge []byte `json:"challenge,omitempty"`
Response []byte `json:"response,omitempty"`
Name string `json:"name,omitempty"`
Method int `json:"method,omitemtpy"`
Totp string `json:"totp,omitempty"`
}
/**object RandS; used to send json response to controller with hashed challenge string*/
type RandS struct {
R *big.Int
S *big.Int
}
/**object UniqueLockNumber; used to read json response from controller of unique lock id*/
type UniqueLockNumber struct {
Id int64 `json:"id"`
}
/**object AccessDoor; used to send pin code to the server*/
type AccessDoor struct {
Pin string
}
/**object AccessDoorCard; used to send card number to the server*/
type AccessDoorCard struct {
Card string
}
func main(){
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
var (
lockUniqueID int64
lockPrivateKey *ecdsa.PrivateKey
isRegistered bool = false
err error
)
if _, err = os.Stat("lockID.txt"); os.IsNotExist(err) {
fmt.Println("\n---Lock is Un-Registered---\n")
lockPrivateKey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
panic(err)
}
lockPrivateKeyBytes, err := x509.MarshalECPrivateKey(lockPrivateKey)
if err != nil {
panic(err)
}
pemFile, err := os.Create("lockPrivateKey.pem")
if err != nil {
panic(err)
}
var pemPrivateKeyBlock = &pem.Block{
Type: "ECDSA Private Key",
Bytes: lockPrivateKeyBytes,
}
err = pem.Encode(pemFile, pemPrivateKeyBlock)
if err != nil {
panic(err)
}
pemFile.Close()
}else{
fmt.Println("\n---Lock is Registered---")
isRegistered = true
lockIDfile, err := os.Open("lockID.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(lockIDfile)
line := strings.TrimSpace(scanner.Text())
lockUniqueID, err = strconv.ParseInt(line,10,64)
if err != nil {
panic(err)
}
pemFile, err := os.Open("lockPrivateKey.pem")
if err != nil {
panic(err)
}
privatePublicKeyInfo, _ := pemFile.Stat()
var size int64 = privatePublicKeyInfo.Size()
pemBytes := make([]byte, size)
buffer := bufio.NewReader(pemFile)
_, err = buffer.Read(pemBytes)
data, _ := pem.Decode([]byte(pemBytes))
pemFile.Close()
privatePublicKeyImported, err := x509.ParseECPrivateKey(data.Bytes)
if err != nil{
panic(err)
}
lockPrivateKey = privatePublicKeyImported
jwt, err := login(lockUniqueID, lockPrivateKey)
if jwt != "" && err == nil{
fmt.Println("---Login Successful---")
isRegistered = true
}else{
os.Exit(1)
}
}
var (
pinPipe string
cardPipe string
)
flag.StringVar(&pinPipe, "pin-pipe", "pin-pipe", "Location of a named pipe to read pin-data from")
flag.StringVar(&cardPipe, "card-pipe", "card-pipe", "Location of a named pipe to read card-data from")
flag.Parse()
fmt.Println("pin pipe ", pinPipe)
fmt.Println("card pipe ", cardPipe)
filePipe0, err := os.OpenFile(pinPipe, os.O_RDONLY, os.ModeNamedPipe)
if err != nil{
panic(err)
}
fmt.Println("Opened pin-pipe")
reader := bufio.NewReader(filePipe0)
filePipe1, err := os.OpenFile(cardPipe, os.O_RDONLY, os.ModeNamedPipe)
if err != nil{
panic(err)
}
fmt.Println("Opened card-pipe")
reader1 := bufio.NewReader(filePipe1)
for{
//if loop will read the data from filepipe1
if line, err := reader1.ReadString('\n'); err == nil{
fmt.Println("-----------------------------------------------")
fmt.Println("\n--Card: ", line)
line = strings.TrimSpace(line)
fmt.Println("---Accessing Door---")
accessRequestBody, err := json.Marshal(AccessDoorCard{
Card: line,
})
if err != nil {
continue
}
var accessDoorURL string = "https://bast-security.xyz:8080/locks/" + strconv.FormatInt(lockUniqueID,10) + "/access"
accessDoorResponse, err := http.Post(accessDoorURL,"",bytes.NewBuffer(accessRequestBody))
if err != nil{
continue
}
defer accessDoorResponse.Body.Close()
if accessDoorResponse.StatusCode == 200{
fmt.Println("---Access Granted---")
}else{
fmt.Println("---Access Denied---")
}
fmt.Println("-----------------------------------------------")
}else if err != io.EOF{
panic(err)
}
//if loop will read the data from filepipe0
if line, err := reader.ReadString('\n'); err == nil{
fmt.Println("-----------------------------------------------")
fmt.Println("\n--Pin: ", line)
//removes the '\n\' from line
line = strings.TrimSpace(line)
/**if look checks to see if lock is registered or not*/
if !isRegistered{
fmt.Println("\n---Lock is Un-Registered---\n")
tokens := strings.Split(line, "*")
if len(tokens) < 2 {
continue
}
systemID, err := strconv.ParseInt(tokens[0], 10, 64)
if err != nil{
continue
}
//creating a json string containing systemID and public x and y key
requestBody, err := json.Marshal(Door{
Name: fmt.Sprintf("Lock %s", tokens[1]),
System: systemID,
KeyX: lockPrivateKey.PublicKey.X,
KeyY: lockPrivateKey.PublicKey.Y,
Totp: tokens[1],
})
//incase requestBody could not be generated
if err != nil{
fmt.Println("requestBody could not be created")
continue
}
//post request will send information to the server
registerResponse, err := http.Post("https://bast-security.xyz:8080/locks/register","",bytes.NewBuffer(requestBody))
//incase registerResponse could not post
if err != nil{
fmt.Println("Registration Failed")
continue
}
defer registerResponse.Body.Close()
registerResponseBody, err := ioutil.ReadAll(registerResponse.Body)
if err != nil{
fmt.Println("Failed to read response")
continue
}
registerResponseJSON := UniqueLockNumber{}
json.Unmarshal([]byte(string(registerResponseBody)), ®isterResponseJSON)
lockUniqueID = registerResponseJSON.Id
lockIDfile, err := os.Create("lockID.txt")
if err != nil{
fmt.Println("lockID.txt unable to be created")
continue
}
//writing into the lockID.txt file
_, err = io.WriteString(lockIDfile, strconv.FormatInt(lockUniqueID, 10))
//incase lockUniqueID was not written into the lockIDfile
if err != nil{
fmt.Println("Could not write to lockID.txt")
continue
}
/*****************************************************/
/************************LOGIN************************/
/*****************************************************/
jwt, err := login(lockUniqueID, lockPrivateKey)
if jwt != "" && err == nil{
fmt.Println("\n---Login Successful---\n")
isRegistered = true
}else{
continue
}
/***************************************************************/
}else if isRegistered{
fmt.Println("---Accessing Door---")
//creating a json string containing pin number to send to server
accessRequestBody, err := json.Marshal(AccessDoor{
Pin: line,
})
//incase accessRequestBody could not be generated
if err != nil{
fmt.Println("Failed to marshal json")
continue
}
//post request will send accessDoor information to the server
var accessDoorURL string = "https://bast-security.xyz:8080/locks/" + strconv.FormatInt(lockUniqueID,10) + "/access"
accessDoorResponse, err := http.Post(accessDoorURL,"",bytes.NewBuffer(accessRequestBody))
//incase accessDoorResponse could not post
if err != nil{
fmt.Println("Request failed")
continue
}
defer accessDoorResponse.Body.Close()
//if loop checks to see if the pin entered has accesses to the door
if accessDoorResponse.StatusCode == 200{
fmt.Println("---Accessing Granted---")
}else{
fmt.Println("---Accessing Denied---")
}
fmt.Println("-----------------------------------------------")
}
}else if err != io.EOF{
panic(err)
}
}
}
/**Login function for the lock to connect to server*/
func login(lockUniqueID int64, lockPrivateKey *ecdsa.PrivateKey)(string, error){
//get request will receive a challenge string from the server
loginResponse, err := http.Get("https://bast-security.xyz:8080/locks/" + strconv.FormatInt(lockUniqueID,10))
//incase the server dosent respond
if err != nil{
fmt.Println("Challenge String was not recieved")
panic(err)
}
defer loginResponse.Body.Close()
//reads the response from the server
loginResponseBody, err := ioutil.ReadAll(loginResponse.Body)
//incase not able to read the response from the server
if err != nil{
panic(err)
}
var response map[string][]byte
if err := json.Unmarshal(loginResponseBody, &response); err != nil {
panic(err)
}
challengeString := response["challenge"]
hashedChallengeString := sha256.Sum256([]byte(challengeString))
r, s, err := ecdsa.Sign(rand.Reader, lockPrivateKey, hashedChallengeString[:])
if err != nil{
panic(err)
}
var payload []byte
if payload, err = asn1.Marshal(struct{ R, S *big.Int }{ r, s }); err != nil {
panic(err)
}
//sending the hashed challenge string to the server
var challengeStringRequest string = "https://bast-security.xyz:8080/locks/" + strconv.FormatInt(lockUniqueID,10) + "/login"
challengeStringResponse, err := http.Post(challengeStringRequest, "", bytes.NewBuffer(payload))
//incase post request was not sent
if err!= nil{
panic(err)
}
defer challengeStringResponse.Body.Close()
//successful, the server responded
if challengeStringResponse.StatusCode == 200{
//reads the response from the server
challengeStringBody, err := ioutil.ReadAll(challengeStringResponse.Body)
//incase not able to read the response from the server
if err != nil{
panic(err)
}
//saves the response from the server to jwt
var jwt = string(challengeStringBody)
return jwt, nil
}else{
return "", fmt.Errorf("Failed to login")
}
}