Skip to content

Commit ec693c2

Browse files
committed
Merge pull request #70 from arduino/verify
Verify
2 parents 092da5b + 9f13038 commit ec693c2

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

conn.go

+41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
package main
44

55
import (
6+
"crypto"
7+
"crypto/rsa"
8+
"crypto/sha256"
9+
"crypto/x509"
10+
"encoding/hex"
11+
"encoding/pem"
12+
"errors"
613
"net/http"
714
"strconv"
815

@@ -61,6 +68,23 @@ func uploadHandler(c *gin.Context) {
6168
if commandline == "undefined" {
6269
commandline = ""
6370
}
71+
72+
signature := c.PostForm("signature")
73+
if signature == "" {
74+
c.String(http.StatusBadRequest, "signature is required")
75+
log.Error("signature is required")
76+
return
77+
}
78+
79+
err := verifyCommandLine(commandline, signature)
80+
81+
if err != nil {
82+
c.String(http.StatusBadRequest, "signature is invalid")
83+
log.Error("signature is invalid")
84+
log.Error(err)
85+
return
86+
}
87+
6488
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
6589
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
6690
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
@@ -90,6 +114,23 @@ func uploadHandler(c *gin.Context) {
90114
}
91115
}
92116

117+
func verifyCommandLine(input string, signature string) error {
118+
sign, _ := hex.DecodeString(signature)
119+
block, _ := pem.Decode([]byte(*signatureKey))
120+
if block == nil {
121+
return errors.New("invalid key")
122+
}
123+
key, err := x509.ParsePKIXPublicKey(block.Bytes)
124+
if err != nil {
125+
return err
126+
}
127+
rsaKey := key.(*rsa.PublicKey)
128+
h := sha256.New()
129+
h.Write([]byte(input))
130+
d := h.Sum(nil)
131+
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
132+
}
133+
93134
func wsHandler() *WsServer {
94135
server, err := socketio.NewServer(nil)
95136
if err != nil {

main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
port string
4444
portSSL string
4545
origins = flag.String("origins", "", "Allowed origin list for CORS")
46+
signatureKey = flag.String("signatureKey", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----", "Pem-encoded public key to verify signed commandlines")
4647
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
4748
)
4849

@@ -242,8 +243,7 @@ func main() {
242243
log.Printf("Error trying to bind to port: %v, so exiting...", err)
243244
continue
244245
} else {
245-
ip := "0.0.0.0"
246-
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
246+
log.Print("Starting server and websocket (SSL) on " + *address + "" + port)
247247
break
248248
}
249249
}
@@ -260,8 +260,7 @@ func main() {
260260
log.Printf("Error trying to bind to port: %v, so exiting...", err)
261261
continue
262262
} else {
263-
ip := "0.0.0.0"
264-
log.Print("Starting server and websocket on " + ip + "" + port)
263+
log.Print("Starting server and websocket on " + *address + "" + port)
265264
break
266265
}
267266
}

0 commit comments

Comments
 (0)