Debian Codegraph @

main.go

raw symbols SCIP
1 //go:generate goversioninfo -file-version=$GIT_VERSION -ver-major=$VERSION_MAJOR -ver-minor=$VERSION_MINOR -ver-patch=$VERSION_PATCH -platform-specific=true windows-installer/versioninfo.json
2
3 package main
4
5 import (
6 "fmt"
7 "io"
8 "os"
9
10 "github.com/github/smimesign/certstore"
11 "github.com/pborman/getopt/v2"
12 "github.com/pkg/errors"
13 )
14
15 var (
16 // This can be set at build time by running
17 // go build -ldflags "-X main.versionString=$(git describe --tags)"
18 versionString = "undefined"
19
20 // default timestamp authority URL. This can be set at build time by running
21 // go build -ldflags "-X main.defaultTSA=${https://whatever}"
22 defaultTSA = ""
23
24 // Action flags
25 helpFlag = getopt.BoolLong("help", 'h', "print this help message")
26 versionFlag = getopt.BoolLong("version", 'v', "print the version number")
27 signFlag = getopt.BoolLong("sign", 's', "make a signature")
28 verifyFlag = getopt.BoolLong("verify", 0, "verify a signature")
29 listKeysFlag = getopt.BoolLong("list-keys", 0, "show keys")
30
31 // Option flags
32 localUserOpt = getopt.StringLong("local-user", 'u', "", "use USER-ID to sign", "USER-ID")
33 detachSignFlag = getopt.BoolLong("detach-sign", 'b', "make a detached signature")
34 armorFlag = getopt.BoolLong("armor", 'a', "create ascii armored output")
35 statusFdOpt = getopt.IntLong("status-fd", 0, -1, "write special status strings to the file descriptor n.", "n")
36 keyFormatOpt = getopt.EnumLong("keyid-format", 0, []string{"long"}, "long", "select how to display key IDs.", "{long}")
37 tsaOpt = getopt.StringLong("timestamp-authority", 't', defaultTSA, "URL of RFC3161 timestamp authority to use for timestamping", "url")
38 includeCertsOpt = getopt.IntLong("include-certs", 0, -2, "-3 is the same as -2, but ommits issuer when cert has Authority Information Access extension. -2 includes all certs except root. -1 includes all certs. 0 includes no certs. 1 includes leaf cert. >1 includes n from the leaf. Default -2.", "n")
39
40 // Remaining arguments
41 fileArgs []string
42
43 idents []certstore.Identity
44
45 // these are changed in tests
46 stdin io.ReadCloser = os.Stdin
47 stdout io.WriteCloser = os.Stdout
48 stderr io.WriteCloser = os.Stderr
49 )
50
51 func main() {
52 if err := runCommand(); err != nil {
53 fmt.Fprintln(os.Stderr, err)
54 os.Exit(1)
55 }
56 }
57
58 func runCommand() error {
59 // Parse CLI args
60 getopt.HelpColumn = 40
61 getopt.SetParameters("[files]")
62 getopt.Parse()
63 fileArgs = getopt.Args()
64
65 if *helpFlag {
66 getopt.Usage()
67 return nil
68 }
69
70 if *versionFlag {
71 fmt.Println(versionString)
72 return nil
73 }
74
75 // Open certificate store
76 store, err := certstore.Open()
77 if err != nil {
78 return errors.Wrap(err, "failed to open certificate store")
79 }
80 defer store.Close()
81
82 // Get list of identities
83 idents, err = store.Identities()
84 if err != nil {
85 return errors.Wrap(err, "failed to get identities from certificate store")
86 }
87 for _, ident := range idents {
88 defer ident.Close()
89 }
90
91 if *signFlag {
92 if *verifyFlag || *listKeysFlag {
93 return errors.New("specify --help, --sign, --verify, or --list-keys")
94 } else if len(*localUserOpt) == 0 {
95 return errors.New("specify a USER-ID to sign with")
96 } else {
97 return commandSign()
98 }
99 }
100
101 if *verifyFlag {
102 if *signFlag || *listKeysFlag {
103 return errors.New("specify --help, --sign, --verify, or --list-keys")
104 } else if len(*localUserOpt) > 0 {
105 return errors.New("local-user cannot be specified for verification")
106 } else if *detachSignFlag {
107 return errors.New("detach-sign cannot be specified for verification")
108 } else if *armorFlag {
109 return errors.New("armor cannot be specified for verification")
110 } else {
111 return commandVerify()
112 }
113 }
114
115 if *listKeysFlag {
116 if *signFlag || *verifyFlag {
117 return errors.New("specify --help, --sign, --verify, or --list-keys")
118 } else if len(*localUserOpt) > 0 {
119 return errors.New("local-user cannot be specified for list-keys")
120 } else if *detachSignFlag {
121 return errors.New("detach-sign cannot be specified for list-keys")
122 } else if *armorFlag {
123 return errors.New("armor cannot be specified for list-keys")
124 } else {
125 return commandListKeys()
126 }
127 }
128
129 return errors.New("specify --help, --sign, --verify, or --list-keys")
130 }