-
Notifications
You must be signed in to change notification settings - Fork 2
/
verify.go
80 lines (65 loc) · 1.71 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/bwesterb/go-xmssmt"
"github.com/urfave/cli"
)
func cmdVerify(c *cli.Context) error {
var err error
var pk xmssmt.PublicKey
var sig xmssmt.Signature
if c.NArg() != 0 {
return cli.NewExitError("I don't expect arguments; only flags", 10)
}
pkBytes, err := ioutil.ReadFile(c.String("pubkey"))
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"os.Open(%s): %v", c.String("pubkey"), err), 17)
}
if err := pk.UnmarshalBinary(pkBytes); err != nil {
return cli.NewExitError(fmt.Sprintf(
"%s: %v", c.String("pubkey"), err), 17)
}
var sigPath string
if c.IsSet("signature") {
sigPath = c.String("signature")
} else if c.IsSet("file") {
sigPath = c.String("file") + ".xmssmt-signature"
} else {
return cli.NewExitError(
"Either --file or --signature should be provided", 18)
}
sigBytes, err := ioutil.ReadFile(sigPath)
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"os.Open(%s): %v", sigPath, err), 19)
}
if err := sig.UnmarshalBinary(sigBytes); err != nil {
return cli.NewExitError(fmt.Sprintf(
"%s: %v", sigPath, err), 19)
}
var rd io.ReadCloser
if c.IsSet("file") {
rd, err = os.Open(c.String("file"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("os.Open(%s): %v",
c.String("file"), err), 20)
}
} else {
fmt.Fprintf(os.Stderr, "Go ahead and type the message to be verified ...\n\n")
rd = os.Stdin
}
valid, err := pk.VerifyFrom(&sig, rd)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Verify: %v", err), 21)
}
if !valid {
return cli.NewExitError(fmt.Sprintf("Signature is *not* valid: %v", err), 22)
}
fmt.Fprintf(os.Stderr, "Signature is valid\n")
rd.Close()
return nil
}