-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (145 loc) · 6.32 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
package main
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"fmt"
"log/slog"
"os"
"strings"
)
const (
challengeFile = "mmx_challenge.txt"
responseFile = "mmx_response.txt"
)
type mmxChallenge struct {
algorithmType []byte
randomChallenge []byte
softwareVersion []byte
chipID []byte
fazit []byte
size int
}
func loadChallenge(challengeFile string) (*mmxChallenge, error) {
hexEncodedChallenge, err := os.ReadFile(challengeFile)
if err != nil {
slog.Error("Failed to decode file as hex",
"error", err,
"challengeFile", challengeFile,
)
return nil, err
}
hexEncodedChallenge = bytes.TrimSpace(hexEncodedChallenge)
challengeSize := len(hexEncodedChallenge)
challengeData := make([]byte, challengeSize*2)
_, err = hex.Decode(challengeData, hexEncodedChallenge)
if err != nil {
slog.Error("Failed to hex decode data from challenge file",
"error", err,
"challengeData", challengeData,
"challengeFile", challengeFile,
)
return nil, err
}
parts := bytes.Split(challengeData, []byte{10})
challenge := mmxChallenge{
algorithmType: parts[0],
randomChallenge: parts[1],
softwareVersion: parts[2],
chipID: parts[3],
fazit: parts[4],
size: challengeSize,
}
slog.Info("Parsed challenge",
"algorithmType", challenge.algorithmType,
"randomChallenge", challenge.randomChallenge,
"softwareVersion", challenge.softwareVersion,
"chipID", challenge.chipID,
"FAZIT", challenge.fazit,
)
return &challenge, nil
}
// sign constructs a payload from the challenge and signs the hashed payload.
func (c *mmxChallenge) sign() ([]byte, error) {
dataToSign := make([]byte, 0, c.size*2)
dataToSign = append(dataToSign, c.randomChallenge...)
dataToSign = append(dataToSign, make([]byte, 0x13)...)
dataToSign = append(dataToSign, c.chipID...)
dataToSign = append(dataToSign, 10) // Line feed
dataToSign = append(dataToSign, c.softwareVersion...)
slog.Debug("Generated data to sign",
"string", string(dataToSign),
"binary", hex.EncodeToString(dataToSign),
)
signature, err := signData(dataToSign)
if err != nil {
slog.Error("Failed to sign response",
"error", err,
)
return nil, err
}
return signature, nil
}
func main() {
challenge, err := loadChallenge(challengeFile)
if err != nil {
slog.Error("Failed to load challenge from file",
"error", err,
)
os.Exit(1)
}
signature, err := challenge.sign()
if err != nil {
slog.Error("Failed to sign challenge data",
"error", err,
)
os.Exit(1)
}
const responsePad = 24
const signatureLen = 384 // sha256
responseLen := len(challenge.randomChallenge) + responsePad + signatureLen
responseData := make([]byte, 0, responseLen)
responseData = append(responseData, challenge.randomChallenge...)
responseData = append(responseData, make([]byte, 24)...)
responseData = append(responseData, signature...)
responseString := make([]byte, responseLen*2)
hex.Encode(responseString, responseData)
responseFile, err := os.Create(responseFile)
if err != nil {
slog.Error("Failed to create response file",
"error", err,
)
os.Exit(1)
}
defer responseFile.Close()
fmt.Fprint(responseFile, strings.ToUpper(string(responseString)))
slog.Info("Successfully generated response")
}
const key = `MIIG5QIBAAKCAYEA5H77tYCfMobgUw/UPKSWKa2Jq1GFla5veRjfyTZki5BhleueKgRPLLWKkZV8mukQJhOVCoB6DR9q5lmr3QuPpwxVredQ0yV2bMj/kdKf+dylG8lKgUkiyGyL2WHDpZgrRfrLvQEDeAR9j7XcVaHYUvkfM55yTcT/GXC7aulwd1lh5e5yTtoIB+V40DUDZv3V0PbgZVlKr3x4pvAOek7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ePdJwMCgcEA8Opzjf5kFa207vmWPWjYm4pfCFu5FDCJDzuv4vf4Gr7orGG8AuNi/Yzg7zTXdzKNfjWH4zFr7QkF/h0Q5U58mRtCbmeMaL35nQuerdlU+LUt0K3XvKytHZ6FCt7j2WmhhnbAdYCuys+ZmBaiwzTWQVVC+hgBqasdHAwptu0FFANBIjJ75gmE50Vv1gAxcuU3AoSerLtOlIWrsLA7mFUUVWNe0tGHwzZxOPkUGpNYmEWlXPeTRfVzIDDDI8ZYGSodAoHAP0c8uw13zDCkJFR5TMO+AV+8ulIC+2PCP1+HeHvI7BxFTl2SvlqRmzvjc0MmDuuYONE9VlhXLLLrfOaHdDyOmKoOHdUfqTSF5h7gob6NuTjAhrwbdQP9oDBuod0MvY+2z6pP0zoX3hXUKr6Yj3GSPTq1VlH67mzGzUJKYYyxcr8Wjkuux93gUpE74IfluCrDE6ixEI/DnyAXcXScAJUD/wxCsc2lFnCpK08wqExS6+gDMqzekl+IdipzRIk9kY1xAoHBANNT1alozUJ27Y/zP+b+YYOPDW23h9I+APxrzw25ltlfPZp44QNnkx32xhkOsTLOFW/wZRLV92Yl1CvkMz3yazmiv9M44eG/Q4aO+tJlIjRIObgjxmqqzfB9bRbsDdJY5medI5XvG2SsVn8i3AOABbGpqObYyBydDRvdT3o2z42OjUQCJMzU7NAyCLgf00CF8Is06jt60qNV3hVPgfdOKlf8ouErC3wh9Y+Ubh4hwkVQUo4KXhWwCRzjqUloYz8vwQKBwQCIEqK//x3sh7v+WXYogZlrNAwhwbjkEDb1LFxtrWoW0el9LQl6kwjnmSmIu0TN7JMu2h9Rk5xb7R7KfULluBb/tXbYDKNTva5iq+F5+1doB7lioonsoUprWm9PoBbQDev94O155iBkm95skXn7kqWpAZIaNDeP/ml/oDGz11OS2XDLwxs5dBbcaPVXE+3onJvillcnHDC4PWMyViF6m3n9hxYAmKetaQADy1GacUw4Xk1THL7/cjEAn09D0AqnRUE=`
func signData(message []byte) ([]byte, error) {
privateKey, err := base64.StdEncoding.DecodeString(key)
if err != nil {
panic(fmt.Errorf("error base64 decoding private key: %w", err))
}
privKey, err := x509.ParsePKCS1PrivateKey(privateKey)
if err != nil {
panic(fmt.Errorf("error loading private key: %w", err))
}
messageHash := sha256.Sum256(message)
slog.Debug("Hashed message",
"hash", hex.EncodeToString(messageHash[:]),
)
signature, err := rsa.SignPKCS1v15(nil, privKey, crypto.SHA256, messageHash[:])
if err != nil {
return nil, fmt.Errorf("error signing message: %w", err)
}
slog.Debug("Signed response",
"length", len(signature),
"signature", hex.EncodeToString(signature),
)
return signature, nil
}