|
3 | 3 | package main
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "crypto" |
| 7 | + "crypto/rsa" |
| 8 | + "crypto/sha256" |
| 9 | + "crypto/x509" |
| 10 | + "encoding/pem" |
| 11 | + "io/ioutil" |
6 | 12 | "net/http"
|
7 | 13 | "strconv"
|
8 | 14 |
|
@@ -61,6 +67,22 @@ func uploadHandler(c *gin.Context) {
|
61 | 67 | if commandline == "undefined" {
|
62 | 68 | commandline = ""
|
63 | 69 | }
|
| 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 | + |
64 | 86 | extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
|
65 | 87 | extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
|
66 | 88 | extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
|
@@ -90,6 +112,24 @@ func uploadHandler(c *gin.Context) {
|
90 | 112 | }
|
91 | 113 | }
|
92 | 114 |
|
| 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 | + |
93 | 133 | func wsHandler() *WsServer {
|
94 | 134 | server, err := socketio.NewServer(nil)
|
95 | 135 | if err != nil {
|
|
0 commit comments