forked from brianredbeard/gpget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (197 loc) · 5.99 KB
/
main.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
GPget - A tool for securely retrieving files from hostile storage
Copyright (C) 2016 Brian 'redbeard' Harrington
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"bytes"
"code.google.com/p/go.crypto/openpgp"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
)
/*
Define our various flags to be used in parsing CLI options. The basic idea is
to provide the user with a minimal number of sane options. The reality is that
as this is related to security, the user should likely be more involved in
understanding that they need to validate. If this means specifying an explicit
GPG/PGP versus just assuming it's on the local keyring, so be it.
*/
var (
flagHelp = flag.Bool("help", false, "print help and exit")
flagURL = flag.String("url", "", "url to retrieve")
flagMem = flag.Bool("O", false, "save the file using it's original name")
flagPath = flag.String("o", "./", "location to place successful download")
flagKeyid = flag.String("k", "", "GPG key identity expected to be used for the remote file")
flagBin = flag.Bool("b", false, "Use a binary signature instead of armored ASCII")
)
/*
Define a struct to handle the file to be downloaded. As a part of this we define
fields related to verification of our file. In the end this results in a single
data structure which SHOULD include at least one signature type:
binary (signature)
armor (base64)
A resulting File struct MAY include both types. In this case the binary
signature takes precidence as per RFC 4880.
*/
type File struct {
name string
signature []byte
content []byte
}
type SigState struct {
success bool
sig string
}
func main() {
flag.Parse()
if *flagHelp {
flag.Usage()
os.Exit(1)
}
gpfile, err := dlFile(*flagURL)
if err != nil {
fmt.Println("Failed to download file: %s", err)
os.Exit(3)
}
state, err := checkGPG(gpfile)
if err != nil {
fmt.Println("Failed to validate GPG signature: %s", err)
os.Exit(3)
}
if !state.success {
fmt.Println("Unknown error in retrieving your file")
os.Exit(3)
}
if *flagMem {
f, err := os.Create(gpfile.name)
if err != nil {
fmt.Printf("Error opening file %s for write - %s", gpfile.name, err)
os.Exit(5)
}
n, err := f.Write(gpfile.content)
if err != nil {
fmt.Printf("Unable to save file, %d bytes written of %d bytes total - %s\n", n, len(gpfile.content), err)
os.Exit(5)
}
} else {
fmt.Println(string(gpfile.content))
}
os.Exit(0)
}
// implements a simple function to retrieve a remote file, check for errors
// and return the byte stream
func getRemote(url string) (data []byte, err error) {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Could not download " + url)
return data, err
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("Could not download the url %s\n", url)
os.Exit(2)
}
data, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
return data, err
}
// download a specified file and look for a well formatted signature of the
// form "url" + ".asc" or ".sig" representing the binary and base64 gpg
// signatures respectively
func dlFile(url string) (file File, err error) {
var myfile File
var sig []byte
var ext string
if *flagBin {
ext = url + ".sig"
} else {
ext = url + ".asc"
}
myfile.name = path.Base(url)
myfile.content, err = getRemote(url)
if err != nil {
if err == http.ErrMissingFile {
fmt.Printf("Error (404): the file %s did not exist\n", url)
os.Exit(1)
} else {
fmt.Printf("Transport error retreiving %s\n" + url)
os.Exit(1)
}
}
sig, err = getRemote(ext)
if err != nil {
if err == http.ErrMissingFile {
fmt.Printf("Error (404): the file %s did not exist, you must sign all files with an ASCII armored signature", ext)
os.Exit(1)
} else {
fmt.Println("Transport error retreiving armored signature: ",
ext)
os.Exit(1)
}
}
myfile.signature = sig
return myfile, nil
}
func checkGPG(file File) (state SigState, err error) {
var signer *openpgp.Entity
keypath := path.Join(os.Getenv("HOME"), "/.gnupg/pubring.gpg")
keys, err := os.Open(keypath)
if err != nil {
fmt.Printf("Could not open public keyring at %s\n", keypath)
os.Exit(2)
}
keyring, err := openpgp.ReadKeyRing(keys)
if err != nil {
fmt.Printf("Error reading public keyring: %s\n", err)
os.Exit(2)
}
if *flagBin {
signer, err = openpgp.CheckDetachedSignature(keyring, bytes.NewReader(file.content), bytes.NewReader(file.signature))
} else {
signer, err = openpgp.CheckArmoredDetachedSignature(keyring, bytes.NewReader(file.content), bytes.NewReader(file.signature))
}
if err != nil {
fmt.Printf("Invalid signature or public key not present: %s\n", err)
os.Exit(2)
}
state.sig = signer.PrimaryKey.KeyIdString()
l := len(*flagKeyid)
if l > 0 {
var rid string
// Force the local id to be all uppercase
lid := strings.ToUpper(*flagKeyid)
// check the number of chars on the remote id to see if it's a
// short or long id. If it's not 8 or 16, it's not valid.
switch l {
case 8:
rid = signer.PrimaryKey.KeyIdShortString()
case 16:
rid = signer.PrimaryKey.KeyIdString()
}
if len(rid) == 0 {
fmt.Printf("You did not specify a valid GPG keyid length. Must be 8 or 16 characters.")
os.Exit(2)
}
if lid != rid {
fmt.Printf("The remote file was not signed by the expected GPG Public key. Expected %s and got %s\n", lid, rid)
os.Exit(2)
}
}
state.success = true
return state, nil
}