~qbit/ogvt

ogvt/main.go -rw-r--r-- 3.3 KiB
8c5c295fAaron Bieber +goreleaser stuff 28 days ago
                                                                                
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"text/tabwriter"

	"github.com/ProtonMail/go-crypto/openpgp"
	"github.com/ProtonMail/go-crypto/openpgp/clearsign"
	"github.com/ProtonMail/go-crypto/openpgp/packet"
	"suah.dev/protect"
)

func errExit(err error) {
	if err != nil {
		if err == io.EOF {
			fmt.Println("invalid signature file")
		} else {
			fmt.Println(err)
		}
		os.Exit(1)
	}
}

var flags struct {
	sig, file, pub string
}

func main() {
	_ = protect.Pledge("stdio unveil rpath")

	flag.StringVar(&flags.sig, "sig", "",
		"path to signature file; if file contains cleartext message\n"+
			"with signature, -file must be unset")
	flag.StringVar(&flags.file, "file", "",
		"path to unsigned message file; incompatible with cleartext\n"+
			"signatures")
	flag.StringVar(&flags.pub, "pub", "", "path to pubkey file")
	flag.Parse()

	if len(os.Args) == 1 {
		flag.PrintDefaults()
		os.Exit(1)
	}

	_ = protect.Unveil(flags.sig, "r")
	_ = protect.Unveil(flags.file, "r")
	_ = protect.Unveil(flags.pub, "r")
	ext := filepath.Ext(flags.sig)
	var signoext string
	if flags.file == "" && ext != "" {
		signoext = flags.sig[:len(flags.sig)-len(ext)]
		_ = protect.Unveil(signoext, "r")
	}
	_ = protect.UnveilBlock()

	pubFi, err := os.Open(flags.pub)
	errExit(err)

	kr, err := openpgp.ReadArmoredKeyRing(pubFi)
	if err != nil {
		fmt.Printf("Can't parse public key %q\n%s\n", flags.pub, err)
		os.Exit(1)
	}

	if flags.sig == "" && flags.file == "" {
		for _, ent := range kr {
			for name := range ent.Identities {
				fmt.Printf("%q (%X)\n", name, ent.PrimaryKey.Fingerprint)
			}
		}
		return
	}

	var sig, message io.Reader
	var clearsigBlock *clearsign.Block
	var armored bool
	clearsigned := func(data []byte) bool {
		clearsigBlock, _ = clearsign.Decode(data)
		return clearsigBlock != nil
	}

	sigBytes, err := ioutil.ReadFile(flags.sig)
	errExit(err)
	switch {
	case clearsigned(sigBytes):
		if flags.file != "" {
			fmt.Printf("-file is incompatible with cleartext signatures\n")
			os.Exit(1)
		}
		message = bytes.NewReader(clearsigBlock.Bytes)
		sig = clearsigBlock.ArmoredSignature.Body
		armored = false // Body provides decoded signature
	case flags.file == "":
		// Check for a message file with the .sig extensions removed
		flags.file = signoext
		fallthrough
	default:
		messageFi, err := os.Open(flags.file)
		if os.IsNotExist(err) {
			fmt.Printf("signature %s does not provide cleartext, and no "+
				"message %s found\n", flags.sig, flags.file)
			os.Exit(1)
		}
		errExit(err)

		message = messageFi
		sig = bytes.NewReader(sigBytes)
		// Unless signature file uses .gpg or .sig extensions, read
		// ascii armored input.  This covers .asc signatures, and
		// assumes armoring if the extension is otherwise unknown.
		armored = ext != ".gpg" && ext != ".sig"
	}

	var ent *openpgp.Entity
	var pkt *packet.Config
	if armored {
		ent, err = openpgp.CheckArmoredDetachedSignature(kr, message, sig, pkt)
	} else {
		ent, err = openpgp.CheckDetachedSignature(kr, message, sig, pkt)
	}
	errExit(err)

	w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
	for _, id := range ent.Identities {
		_, err := fmt.Fprintf(w, "%q\t(%X)\n", id.Name, ent.PrimaryKey.Fingerprint)
		if err != nil {
			log.Println(err)
		}
	}
	err = w.Flush()
	errExit(err)
	fmt.Println("Signature OK.")
}