2015-08-21 07:22:52 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-08-21 07:22:52 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2016-05-05 19:05:45 +00:00
|
|
|
"io"
|
2015-08-21 07:22:52 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/signature"
|
2015-08-24 14:24:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/upgrade"
|
2024-02-26 12:23:14 +00:00
|
|
|
_ "go.uber.org/automaxprocs"
|
2015-08-21 07:22:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
2017-12-10 18:42:08 +00:00
|
|
|
log.Print(`Usage:
|
2015-08-21 07:22:52 +00:00
|
|
|
stsigtool <command>
|
|
|
|
|
|
|
|
Where command is one of:
|
|
|
|
|
|
|
|
gen
|
|
|
|
- generate a new key pair
|
|
|
|
|
2016-05-05 19:05:45 +00:00
|
|
|
sign <privkeyfile> [datafile]
|
2015-08-21 07:22:52 +00:00
|
|
|
- sign a file
|
|
|
|
|
2015-08-24 14:24:00 +00:00
|
|
|
verify <signaturefile> <datafile>
|
|
|
|
- verify a signature, using the built in public key
|
|
|
|
|
|
|
|
verify <signaturefile> <datafile> <pubkeyfile>
|
|
|
|
- verify a signature, using the specified public key file
|
2017-12-10 18:42:08 +00:00
|
|
|
|
2015-08-21 07:22:52 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch flag.Arg(0) {
|
|
|
|
case "gen":
|
|
|
|
gen()
|
|
|
|
case "sign":
|
|
|
|
sign(flag.Arg(1), flag.Arg(2))
|
|
|
|
case "verify":
|
2015-08-24 14:24:00 +00:00
|
|
|
if flag.NArg() == 4 {
|
|
|
|
verifyWithFile(flag.Arg(1), flag.Arg(2), flag.Arg(3))
|
|
|
|
} else {
|
|
|
|
verifyWithKey(flag.Arg(1), flag.Arg(2), upgrade.SigningKey)
|
|
|
|
}
|
2015-08-21 07:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func gen() {
|
|
|
|
priv, pub, err := signature.GenerateKeys()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdout.Write(priv)
|
|
|
|
os.Stdout.Write(pub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sign(keyname, dataname string) {
|
2021-11-22 07:59:47 +00:00
|
|
|
privkey, err := os.ReadFile(keyname)
|
2015-08-21 07:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:05:45 +00:00
|
|
|
var input io.Reader
|
|
|
|
if dataname == "-" || dataname == "" {
|
|
|
|
input = os.Stdin
|
|
|
|
} else {
|
|
|
|
fd, err := os.Open(dataname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
input = fd
|
2015-08-21 07:22:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 19:05:45 +00:00
|
|
|
sig, err := signature.Sign(privkey, input)
|
2015-08-21 07:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdout.Write(sig)
|
|
|
|
}
|
|
|
|
|
2015-08-24 14:24:00 +00:00
|
|
|
func verifyWithFile(signame, dataname, keyname string) {
|
2021-11-22 07:59:47 +00:00
|
|
|
pubkey, err := os.ReadFile(keyname)
|
2015-08-21 07:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-08-24 14:24:00 +00:00
|
|
|
verifyWithKey(signame, dataname, pubkey)
|
|
|
|
}
|
2015-08-21 07:22:52 +00:00
|
|
|
|
2015-08-24 14:24:00 +00:00
|
|
|
func verifyWithKey(signame, dataname string, pubkey []byte) {
|
2021-11-22 07:59:47 +00:00
|
|
|
sig, err := os.ReadFile(signame)
|
2015-08-21 07:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(dataname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
err = signature.Verify(pubkey, sig, fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-08-24 14:24:00 +00:00
|
|
|
|
|
|
|
log.Println("correct signature")
|
2015-08-21 07:22:52 +00:00
|
|
|
}
|