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