-
Notifications
You must be signed in to change notification settings - Fork 14
/
signature.go
127 lines (102 loc) · 2.9 KB
/
signature.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
/*
pipethis: Stop piping the internet into your shell
Copyright 2016 Ellotheth
Use of this source code is governed by the GNU Public License version 2
(GPLv2). You should have received a copy of the GPLv2 along with your copy of
the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
*/
package main
import (
"errors"
"io"
"os"
"golang.org/x/crypto/openpgp"
)
// Signature represents the PGP signature to be verified against a key and
// Script.
type Signature struct {
key openpgp.KeyRing
script *Script
filename string
source string
}
// NewSignature loads a key ring and Script into a new Signature.
func NewSignature(key openpgp.KeyRing, script *Script, source string) *Signature {
sig := &Signature{key: key, script: script, source: source}
sig.filename = script.Name() + ".sig"
return sig
}
// Name is the name of the temporary file holding the signature.
func (s Signature) Name() string {
return s.filename
}
// Source is the original location of the signature file. It defaults to
// <script source>.sig.
func (s *Signature) Source() string {
if s.source != "" || s.script == nil || s.script.IsClearsigned() || s.script.IsPiped() {
return s.source
}
s.source = s.script.Source() + ".sig"
return s.source
}
// Download saves the signature to a temporary file.
func (s *Signature) Download() error {
if s.script != nil && s.script.IsClearsigned() {
return nil
}
source := s.Source()
if source == "" {
return errors.New("The signature source location is missing")
}
body, err := getFile(source)
if err != nil {
return errors.New("Couldn't open the signature source file at " + source)
}
defer body.Close()
file, err := os.Create(s.Name())
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, body)
if err != nil {
return nil
}
return nil
}
// Body opens Signature.Name() for reading, downloading it first if necessary.
func (s *Signature) Body() (ReadSeekCloser, error) {
info, err := os.Stat(s.Name())
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if os.IsNotExist(err) || info.Size() == 0 {
err := s.Download()
if err != nil {
return nil, errors.New(err.Error() + " (do you need to set -signature?)")
}
}
return os.Open(s.Name())
}
// Verify checks Signature.Name() against the public key and script file, and
// returns an error if the signature cannot be verified.
func (s *Signature) Verify() error {
signed, err := s.script.Body()
if err != nil {
return err
}
defer signed.Close()
signature, err := s.Body()
if err != nil {
return err
}
defer signature.Close()
if _, err := openpgp.CheckDetachedSignature(s.key, signed, signature); err == nil {
return nil
}
signature.Seek(0, 0) // i'm sure there's a good reason i don't need to reset the script...
if _, err := openpgp.CheckArmoredDetachedSignature(s.key, signed, signature); err == nil {
return nil
}
return errors.New("Failed to verify signature")
}