-
Notifications
You must be signed in to change notification settings - Fork 10
/
verify.go
75 lines (63 loc) · 1.6 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
package cmd
import (
"encoding/hex"
"io/ioutil"
"os"
"time"
"github.com/ProtonMail/gosop/utils"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
// Verify checks the validity of a signature against a set of certificates.
func Verify(input ...string) error {
switch len(input) {
case 0:
return Err3
case 1:
println("Please provide a certificate (public key)")
return Err19
}
if notBefore != "-" || notAfter != "now" {
println("--not-after and --not-before are not implemented.")
return Err37
}
// Collect keyring
keyRing, err := utils.CollectKeys(input[1:]...)
if err != nil {
return verErr(err)
}
plaintextBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return verErr(err)
}
var text bool
if asType == textOpt {
text = true
}
message := &crypto.PlainMessage{Data: plaintextBytes, TextType: text}
// Collect signature
sigBytes, err := utils.ReadFileOrEnv(input[0])
if err != nil {
return verErr(err)
}
var signature *crypto.PGPSignature
signature, err = crypto.NewPGPSignatureFromArmored(string(sigBytes))
if err != nil {
signature = crypto.NewPGPSignature(sigBytes)
}
creationTime, err := keyRing.GetVerifiedSignatureTimestamp(message, signature, crypto.GetUnixTime())
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
return Err3
}
// TODO: This is fake
fgp, err := hex.DecodeString(keyRing.GetKeys()[0].GetFingerprint())
if err != nil {
return verErr(err)
}
ver := utils.VerificationString(time.Unix(creationTime, 0), fgp, fgp)
_, err = os.Stdout.WriteString(ver + "\n")
return err
}
func verErr(err error) error {
return Err99("verify", err)
}