gunk-dev/the-valley

sshsig.go

sourcedca208d74f9emain's published tip history
3.6 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 // The reserved field is read and ignored, as ssh-keygen does. The bytes
51 // a key signs always hold an empty reserved field, so what the envelope
52 // carries here cannot change what was signed. It does mean two
53 // envelopes can carry one signature, which is one reason signature
54 // bytes are no release identity.
55 r.str()
56 env.hashAlg = string(r.str())
57 env.signature = r.str()
58 if err := r.end(); err != nil {
59 return nil, fmt.Errorf("the SSHSIG blob %w", err)
60 }
61 if version != sshsigVersion {
62 return nil, fmt.Errorf("it is SSHSIG version %d, and only version %d exists", version, sshsigVersion)
63 }
64 return env, nil
65}
66
67// dearmor follows sshsig_dearmor in OpenSSH, more strictly. The header
68// opens the input and the footer follows a newline, as there. Between them
69// is base64 wrapped onto lines, with canonical padding and nothing else.
70// After the footer only whitespace may follow, where ssh-keygen ignores
71// anything at all.
72func dearmor(armored []byte) ([]byte, error) {
73 rest := armored[len(armorBegin):]
74 switch {
75 case bytes.HasPrefix(rest, []byte("\r\n")):
76 rest = rest[2:]
77 case bytes.HasPrefix(rest, []byte("\n")):
78 rest = rest[1:]
79 default:
80 return nil, errors.New("the armor header is not followed by a newline")
81 }
82 end := bytes.Index(rest, []byte("\n"+armorEnd))
83 if end < 0 {
84 return nil, errors.New("the armor has no footer")
85 }
86 if after := rest[end+1+len(armorEnd):]; len(bytes.TrimSpace(after)) != 0 {
87 return nil, errors.New("the armor footer is followed by more than whitespace")
88 }
89 encoded := strings.NewReplacer("\r", "", "\n", "").Replace(string(rest[:end]))
90 blob, err := base64.StdEncoding.Strict().DecodeString(encoded)
91 if err != nil {
92 return nil, fmt.Errorf("the armored signature is not canonical base64: %w", err)
93 }
94 return blob, nil
95}
96
97// signedData rebuilds the bytes a signer's key signed for message: the
98// magic preamble, the namespace, an empty reserved field, the hash
99// algorithm, and the hash of the message.
100func signedData(namespace, hashAlg string, message []byte) ([]byte, error) {
101 var sum []byte
102 switch hashAlg {
103 case "sha512":
104 s := sha512.Sum512(message)
105 sum = s[:]
106 case "sha256":
107 s := sha256.Sum256(message)
108 sum = s[:]
109 default:
110 return nil, fmt.Errorf("the message hash %q is not one SSHSIG allows", hashAlg)
111 }
112 data := []byte(sshsigMagic)
113 data = appendString(data, []byte(namespace))
114 data = appendString(data, nil)
115 data = appendString(data, []byte(hashAlg))
116 data = appendString(data, sum)
117 return data, nil
118}