Skip to content

Commit 3140b06

Browse files
committed
Check for signature of commandline
Read the commandline.pub key and throw error if the signature of commandline is invalid. Need to update the webide to generate the signature
1 parent 3037740 commit 3140b06

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

conn.go

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
package main
44

55
import (
6+
"crypto"
7+
"crypto/rsa"
8+
"crypto/sha256"
9+
"crypto/x509"
10+
"encoding/pem"
11+
"io/ioutil"
612
"net/http"
713
"strconv"
814

@@ -61,6 +67,22 @@ func uploadHandler(c *gin.Context) {
6167
if commandline == "undefined" {
6268
commandline = ""
6369
}
70+
71+
signature := c.PostForm("signature")
72+
if signature == "" {
73+
c.String(http.StatusBadRequest, "signature is required")
74+
log.Error("signature is required")
75+
return
76+
}
77+
78+
err := verifyCommandLine(commandline, signature)
79+
80+
if err != nil {
81+
c.String(http.StatusBadRequest, "signature is invalid")
82+
log.Error("signature is invalid")
83+
return
84+
}
85+
6486
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
6587
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
6688
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
@@ -90,6 +112,24 @@ func uploadHandler(c *gin.Context) {
90112
}
91113
}
92114

115+
func verifyCommandLine(input string, signature string) error {
116+
publicKey, err := ioutil.ReadFile("commandline.pub")
117+
if err != nil {
118+
return err
119+
}
120+
121+
block, _ := pem.Decode(publicKey)
122+
key, err := x509.ParsePKIXPublicKey(block.Bytes)
123+
if err != nil {
124+
return err
125+
}
126+
rsaKey := key.(*rsa.PublicKey)
127+
h := sha256.New()
128+
h.Write([]byte(input))
129+
d := h.Sum(nil)
130+
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, []byte(signature))
131+
}
132+
93133
func wsHandler() *WsServer {
94134
server, err := socketio.NewServer(nil)
95135
if err != nil {

0 commit comments

Comments
 (0)