gunk-dev/the-valley

sshsig.go

sourcefe4ab5bf3cabmain's published tip history
3.1 KiB
1package sigverify
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "crypto/sha512"
7 "encoding/base64"
8 "errors"
9 "fmt"
10 "strings"
11)
12
13// The SSHSIG format, from PROTOCOL.sshsig in openssh-portable.
14const (
15 sshsigMagic = "SSHSIG"
16 sshsigVersion = 1
17 armorBegin = "-----BEGIN SSH SIGNATURE-----"
18 armorEnd = "-----END SSH SIGNATURE-----"
19)
20
21// envelope is a parsed SSHSIG blob: who signed, in which namespace, with
22// which hash, and the signature itself.
23type envelope struct {
24 publicKey []byte
25 namespace string
26 hashAlg string
27 signature []byte
28}
29
30// parseSignature reads an SSHSIG in either form: armored, as
31// `ssh-keygen -Y sign` writes it and git embeds it in a tag, or the raw
32// binary blob inside the armor.
33func parseSignature(sig []byte) (*envelope, error) {
34 blob := sig
35 if bytes.HasPrefix(sig, []byte(armorBegin)) {
36 var err error
37 if blob, err = dearmor(sig); err != nil {
38 return nil, err
39 }
40 }
41 if !bytes.HasPrefix(blob, []byte(sshsigMagic)) {
42 return nil, errors.New("it is neither an armored SSH signature nor an SSHSIG blob")
43 }
44 r := reader{rest: blob[len(sshsigMagic):]}
45 version := r.u32()
46 env := &envelope{
47 publicKey: r.str(),
48 namespace: string(r.str()),
49 }
50 r.str() // reserved: ssh-keygen ignores it here, and it is never signed
51 env.hashAlg = string(r.str())
52 env.signature = r.str()
53 if err := r.end(); err != nil {
54 return nil, fmt.Errorf("the SSHSIG blob %w", err)
55 }
56 if version != sshsigVersion {
57 return nil, fmt.Errorf("it is SSHSIG version %d, and only version %d exists", version, sshsigVersion)
58 }
59 return env, nil
60}
61
62// dearmor follows sshsig_dearmor in OpenSSH: the header opens the input,
63// the footer follows a newline, and the base64 between them may be wrapped.
64// Anything after the footer is ignored.
65func dearmor(armored []byte) ([]byte, error) {
66 rest := armored[len(armorBegin):]
67 switch {
68 case bytes.HasPrefix(rest, []byte("\r\n")):
69 rest = rest[2:]
70 case bytes.HasPrefix(rest, []byte("\n")):
71 rest = rest[1:]
72 default:
73 return nil, errors.New("the armor header is not followed by a newline")
74 }
75 end := bytes.Index(rest, []byte("\n"+armorEnd))
76 if end < 0 {
77 return nil, errors.New("the armor has no footer")
78 }
79 encoded := strings.Join(strings.Fields(string(rest[:end])), "")
80 blob, err := base64.StdEncoding.DecodeString(encoded)
81 if err != nil {
82 return nil, fmt.Errorf("the armored signature is not base64: %w", err)
83 }
84 return blob, nil
85}
86
87// signedData rebuilds the bytes a signer's key signed for message: the
88// magic preamble, the namespace, an empty reserved field, the hash
89// algorithm, and the hash of the message.
90func signedData(namespace, hashAlg string, message []byte) ([]byte, error) {
91 var sum []byte
92 switch hashAlg {
93 case "sha512":
94 s := sha512.Sum512(message)
95 sum = s[:]
96 case "sha256":
97 s := sha256.Sum256(message)
98 sum = s[:]
99 default:
100 return nil, fmt.Errorf("the message hash %q is not one SSHSIG allows", hashAlg)
101 }
102 data := []byte(sshsigMagic)
103 data = appendString(data, []byte(namespace))
104 data = appendString(data, nil)
105 data = appendString(data, []byte(hashAlg))
106 data = appendString(data, sum)
107 return data, nil
108}