-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify_input.go
76 lines (67 loc) · 1.64 KB
/
verify_input.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
package main
import (
"crypto"
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"github.com/gibheer/pki"
)
func VerifyInput(args []string) error {
fs := flag.NewFlagSet("pkictl verify-input", flag.ExitOnError)
flagPublic := fs.String("public-key", "", "path to the public key or read from stdin")
flagInput := fs.String("input", "stdin", "path to the message or stdin")
flagSignature := fs.String("signature", "", "the signature to check the message against")
fs.Parse(args)
sig, err := base64.StdEncoding.DecodeString(*flagSignature)
if err != nil {
return err
}
in, err := openInput(*flagInput)
if err != nil {
return err
}
defer in.Close()
msg, err := ioutil.ReadAll(in)
if err != nil {
return err
}
pub_raw, err := openInput(*flagPublic)
if err != nil {
return err
}
defer pub_raw.Close()
pem, err := parseFile(pub_raw)
if err != nil {
return err
}
if len(pem) > 1 {
return fmt.Errorf("too many objects in public key file")
}
if len(pem[pki.PemLabelPublic]) > 1 {
return fmt.Errorf("too many public keys found")
}
public, err := loadPublicKey(pem[pki.PemLabelPublic][0])
if err != nil {
return err
}
valid, err := public.Verify(msg, sig, crypto.SHA256)
if valid {
fmt.Println("valid")
return nil
}
fmt.Println("invalid")
return err
}
func loadPublicKey(raw_pu []byte) (pki.PublicKey, error) {
if public, err := pki.LoadPublicKeyEd25519(raw_pu); err != nil {
return public, nil
}
if public, err := pki.LoadPublicKeyEcdsa(raw_pu); err == nil {
return public, nil
}
if public, err := pki.LoadPublicKeyRsa(raw_pu); err == nil {
return public, nil
}
return nil, fmt.Errorf("no valid public key found")
}