Skip to content

Commit da360fc

Browse files
committed
move verifyCommandLine to utilities package
1 parent 6d4a2ca commit da360fc

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

conn.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ package main
1919

2020
import (
2121
"bytes"
22-
"crypto"
23-
"crypto/rsa"
24-
"crypto/sha256"
25-
"crypto/x509"
26-
"encoding/hex"
2722
"encoding/json"
28-
"encoding/pem"
29-
"errors"
3023
"fmt"
3124
"net/http"
3225
"os"
@@ -114,7 +107,7 @@ func uploadHandler(c *gin.Context) {
114107
return
115108
}
116109

117-
err := verifyCommandLine(data.Commandline, data.Signature)
110+
err := utilities.VerifyInput(data.Commandline, data.Signature)
118111

119112
if err != nil {
120113
c.String(http.StatusBadRequest, "signature is invalid")
@@ -219,23 +212,6 @@ func send(args map[string]string) {
219212
h.broadcastSys <- mapB
220213
}
221214

222-
func verifyCommandLine(input string, signature string) error {
223-
sign, _ := hex.DecodeString(signature)
224-
block, _ := pem.Decode([]byte(*signatureKey))
225-
if block == nil {
226-
return errors.New("invalid key")
227-
}
228-
key, err := x509.ParsePKIXPublicKey(block.Bytes)
229-
if err != nil {
230-
return err
231-
}
232-
rsaKey := key.(*rsa.PublicKey)
233-
h := sha256.New()
234-
h.Write([]byte(input))
235-
d := h.Sum(nil)
236-
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
237-
}
238-
239215
func wsHandler() *WsServer {
240216
server, err := socketio.NewServer(nil)
241217
if err != nil {

utilities/utilities.go

+27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ package utilities
1818
import (
1919
"archive/zip"
2020
"bytes"
21+
"crypto"
22+
"crypto/rsa"
23+
"crypto/sha256"
24+
"crypto/x509"
25+
"encoding/hex"
26+
"encoding/pem"
2127
"errors"
2228
"fmt"
2329
"io"
@@ -26,6 +32,8 @@ import (
2632
"path"
2733
"path/filepath"
2834
"strings"
35+
36+
"github.com/arduino/arduino-create-agent/globals"
2937
)
3038

3139
// SaveFileonTempDir creates a temp directory and saves the file data as the
@@ -162,3 +170,22 @@ func SafeJoin(parent, subdir string) (string, error) {
162170
}
163171
return res, nil
164172
}
173+
174+
// VerifyInput will verify an input against a signature
175+
// A valid signature is indicated by returning a nil error.
176+
func VerifyInput(input string, signature string) error {
177+
sign, _ := hex.DecodeString(signature)
178+
block, _ := pem.Decode([]byte(globals.SignatureKey))
179+
if block == nil {
180+
return errors.New("invalid key")
181+
}
182+
key, err := x509.ParsePKIXPublicKey(block.Bytes)
183+
if err != nil {
184+
return err
185+
}
186+
rsaKey := key.(*rsa.PublicKey)
187+
h := sha256.New()
188+
h.Write([]byte(input))
189+
d := h.Sum(nil)
190+
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
191+
}

0 commit comments

Comments
 (0)