Debian Codegraph @

sign.go

raw symbols SCIP
1 // Modifications by 42wim
2 //
3 // Copyright 2021 The Sigstore Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package sshsig
18
19 import (
20 "crypto/rand"
21 "crypto/sha256"
22 "crypto/sha512"
23 "errors"
24 "hash"
25 "io"
26
27 "golang.org/x/crypto/ssh"
28 "golang.org/x/crypto/ssh/agent"
29 )
30
31 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L81
32 type MessageWrapper struct {
33 Namespace string
34 Reserved string
35 HashAlgorithm string
36 Hash string
37 }
38
39 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L34
40 type WrappedSig struct {
41 MagicHeader [6]byte
42 Version uint32
43 PublicKey string
44 Namespace string
45 Reserved string
46 HashAlgorithm string
47 Signature string
48 }
49
50 const (
51 magicHeader = "SSHSIG"
52 defaultHashAlgorithm = "sha512"
53 )
54
55 var supportedHashAlgorithms = map[string]func() hash.Hash{
56 "sha256": sha256.New,
57 "sha512": sha512.New,
58 }
59
60 func wrapData(m io.Reader, ns string) ([]byte, error) {
61 hf := sha512.New()
62 if _, err := io.Copy(hf, m); err != nil {
63 return nil, err
64 }
65 mh := hf.Sum(nil)
66
67 sp := MessageWrapper{
68 Namespace: ns,
69 HashAlgorithm: defaultHashAlgorithm,
70 Hash: string(mh),
71 }
72
73 dataMessageWrapper := ssh.Marshal(sp)
74 dataMessageWrapper = append([]byte(magicHeader), dataMessageWrapper...)
75
76 return dataMessageWrapper, nil
77 }
78
79 func sign(s ssh.AlgorithmSigner, m io.Reader, ns string) (*ssh.Signature, error) {
80 dataMessageWrapper, err := wrapData(m, ns)
81 if err != nil {
82 return nil, err
83 }
84 // ssh-rsa is not supported for RSA keys:
85 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L71
86 // We can use the default value of "" for other key types though.
87 algo := ""
88 if s.PublicKey().Type() == ssh.KeyAlgoRSA {
89 algo = ssh.SigAlgoRSASHA2512
90 }
91
92 return s.SignWithAlgorithm(rand.Reader, dataMessageWrapper, algo)
93 }
94
95 func signAgent(pk ssh.PublicKey, ag agent.Agent, m io.Reader, ns string) (*ssh.Signature, error) {
96 dataMessageWrapper, err := wrapData(m, ns)
97 if err != nil {
98 return nil, err
99 }
100
101 var sigFlag agent.SignatureFlags
102 if pk.Type() == ssh.KeyAlgoRSA {
103 sigFlag = agent.SignatureFlagRsaSha512
104 }
105
106 agExt, ok := ag.(agent.ExtendedAgent)
107 if !ok {
108 return nil, errors.New("couldn't cast to ExtendedAgent")
109 }
110
111 return agExt.SignWithFlags(pk, dataMessageWrapper, sigFlag)
112 }
113
114 // SignWithAgent asks the ssh Agent to sign the data with the signer matching the given publicKey and returns an armored signature.
115 // The purpose of the namespace value is to specify a unambiguous
116 // interpretation domain for the signature, e.g. file signing.
117 // This prevents cross-protocol attacks caused by signatures
118 // intended for one intended domain being accepted in another.
119 // If empty, the default is "file".
120 // This can be compared with `ssh-keygen -Y sign -f keyfile -n namespace data`
121 func SignWithAgent(publicKey []byte, ag agent.Agent, data io.Reader, namespace string) ([]byte, error) {
122 pk, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
123 if err != nil {
124 return nil, err
125 }
126
127 if namespace == "" {
128 namespace = defaultNamespace
129 }
130
131 sig, err := signAgent(pk, ag, data, namespace)
132 if err != nil {
133 return nil, err
134 }
135
136 armored := Armor(sig, pk, namespace)
137 return armored, nil
138 }
139
140 // Sign signs the data with the given private key in PEM format and returns an armored signature.
141 // The purpose of the namespace value is to specify a unambiguous
142 // interpretation domain for the signature, e.g. file signing.
143 // This prevents cross-protocol attacks caused by signatures
144 // intended for one intended domain being accepted in another.
145 // If empty, the default is "file".
146 // This can be compared with `ssh-keygen -Y sign -f keyfile -n namespace data`
147 func Sign(pemBytes []byte, data io.Reader, namespace string) ([]byte, error) {
148 s, err := ssh.ParsePrivateKey(pemBytes)
149 if err != nil {
150 return nil, err
151 }
152
153 as, ok := s.(ssh.AlgorithmSigner)
154 if !ok {
155 return nil, err
156 }
157
158 if namespace == "" {
159 namespace = defaultNamespace
160 }
161
162 sig, err := sign(as, data, namespace)
163 if err != nil {
164 return nil, err
165 }
166
167 armored := Armor(sig, s.PublicKey(), namespace)
168 return armored, nil
169 }