Skip to content

Verify #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
package main

import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"net/http"
"strconv"

Expand Down Expand Up @@ -61,6 +68,23 @@ func uploadHandler(c *gin.Context) {
if commandline == "undefined" {
commandline = ""
}

signature := c.PostForm("signature")
if signature == "" {
c.String(http.StatusBadRequest, "signature is required")
log.Error("signature is required")
return
}

err := verifyCommandLine(commandline, signature)

if err != nil {
c.String(http.StatusBadRequest, "signature is invalid")
log.Error("signature is invalid")
log.Error(err)
return
}

extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
Expand Down Expand Up @@ -90,6 +114,23 @@ func uploadHandler(c *gin.Context) {
}
}

func verifyCommandLine(input string, signature string) error {
sign, _ := hex.DecodeString(signature)
block, _ := pem.Decode([]byte(*signatureKey))
if block == nil {
return errors.New("invalid key")
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
rsaKey := key.(*rsa.PublicKey)
h := sha256.New()
h.Write([]byte(input))
d := h.Sum(nil)
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
}

func wsHandler() *WsServer {
server, err := socketio.NewServer(nil)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
port string
portSSL string
origins = flag.String("origins", "", "Allowed origin list for CORS")
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")
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
)

Expand Down Expand Up @@ -242,8 +243,7 @@ func main() {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
continue
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
log.Print("Starting server and websocket (SSL) on " + *address + "" + port)
break
}
}
Expand All @@ -260,8 +260,7 @@ func main() {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
continue
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket on " + ip + "" + port)
log.Print("Starting server and websocket on " + *address + "" + port)
break
}
}
Expand Down