Debian Codegraph @

pkg/signature/ssh/sign.go

raw symbols SCIP
1 //
2 // Copyright 2021 The Sigstore Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package sshscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/
17
18 import (
19 "cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/"
20 "crypto/randscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/"
21 "crypto/sha256scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/"
22 "crypto/sha512scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha512`/"
23 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
24 "hashscip-go gomod github.com/golang/go/src go1.25.0 hash/"
25 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
26
27 "github.com/sigstore/sigstore/pkg/signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/"
28 "golang.org/x/crypto/sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/"
29 )
30
31 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L81
32 type messageWrapperscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper# struct {
33 Namespacescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#Namespace. string
34 Reservedscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#Reserved. string
35 HashAlgorithmscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#HashAlgorithm. string
36 Hashscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#Hash. string
37 }
38
39 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L34
40 type wrappedSigscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig# struct {
41 MagicHeaderscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#MagicHeader. [6]byte
42 Versionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#Version. uint32
43 PublicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#PublicKey. string
44 Namespacescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#Namespace. string
45 Reservedscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#Reserved. string
46 HashAlgorithmscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#HashAlgorithm. string
47 Signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/wrappedSig#Signature. string
48 }
49
50 const (
51 magicHeaderscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/magicHeader. = "SSHSIG"
52 defaultHashAlgorithmscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/defaultHashAlgorithm. = "sha512"
53 )
54
55 var supportedHashAlgorithmsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/supportedHashAlgorithms. = map[string]func() hashscip-go gomod github.com/golang/go/src go1.25.0 hash/.Hashscip-go gomod github.com/golang/go/src go1.25.0 hash/Hash#{
56 "sha256": sha256scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/New().,
57 "sha512": sha512scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha512`/.Newscip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha512`/New().,
58 }
59
60 func signscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/sign().(s sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.AlgorithmSignerscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/AlgorithmSigner#, m ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) (*sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.Signaturescip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signature#, error) {
61 hf := sha512scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha512`/.Newscip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha512`/New().()
62 if _, err := ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Copyscip-go gomod github.com/golang/go/src go1.25.0 io/Copy().(hf, m); err != nil {
63 return nil, err
64 }
65 mh := hf.Sumscip-go gomod github.com/golang/go/src go1.25.0 hash/Hash#Sum().(nil)
66
67 sp := messageWrapperscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#{
68 Namespacescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#Namespace.: "file",
69 HashAlgorithmscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#HashAlgorithm.: defaultHashAlgorithmscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/defaultHashAlgorithm.,
70 Hashscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/messageWrapper#Hash.: string(mh),
71 }
72
73 dataMessageWrapper := sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.Marshalscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Marshal().(sp)
74 dataMessageWrapper = append([]byte(magicHeaderscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/magicHeader.), dataMessageWrapper...)
75
76 // ssh-rsa is not supported for RSA keys:
77 // https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig#L71
78 // We can use the default value of "" for other key types though.
79 algo := ""
80 if s.PublicKeyscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().().Typescip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/PublicKey#Type().() == sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.KeyAlgoRSAscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/KeyAlgoRSA. {
81 algo = sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.KeyAlgoRSASHA512scip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/KeyAlgoRSASHA512.
82 }
83 sig, err := s.SignWithAlgorithmscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/AlgorithmSigner#SignWithAlgorithm().(randscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/Reader., dataMessageWrapper, algo)
84 if err != nil {
85 return nil, err
86 }
87 return sig, nil
88 }
89
90 // Signer implements signature.Signer for SSH keys.
91 type Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer# struct {
92 signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#signer. sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.AlgorithmSignerscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/AlgorithmSigner#
93 }
94
95 // PublicKey returns the public key for a Signer.
96 func (s *Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#) PublicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#PublicKey().(_ ...signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/.PublicKeyOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/PublicKeyOption#) (cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, error) {
97 return s.signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#signer..PublicKeyscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().(), nil
98 }
99
100 // SignMessage signs the supplied message.
101 func (s *Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#) SignMessagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#SignMessage().(message ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#, _ ...signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/.SignOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignOption#) ([]byte, error) {
102 b, err := ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.25.0 io/ReadAll().(message)
103 if err != nil {
104 return nil, err
105 }
106 sig, err := s.signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#signer..Signscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signer#Sign().(randscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/Reader., b)
107 if err != nil {
108 return nil, err
109 }
110 return Armorscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Armor().(sig, s.signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#signer..PublicKeyscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().()), nil
111 }
112
113 var _ signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/.Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Signer# = (*Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Signer#)(nil)
114
115 // Sign signs the supplied message with the private key.
116 func Signscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Sign().(sshPrivateKey string, data ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) ([]byte, error) {
117 s, err := sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.ParsePrivateKeyscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/ParsePrivateKey().([]byte(sshPrivateKey))
118 if err != nil {
119 return nil, err
120 }
121
122 as, ok := s.(sshscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/.AlgorithmSignerscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/AlgorithmSigner#)
123 if !ok {
124 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("private key %T is not a ssh.AlgorithmSigner", s)
125 }
126
127 sig, err := signscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/sign().(as, data)
128 if err != nil {
129 return nil, err
130 }
131
132 armored := Armorscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/ssh`/Armor().(sig, s.PublicKeyscip-go gomod golang.org/x/crypto v0.54.0 `golang.org/x/crypto/ssh`/Signer#PublicKey().())
133 return armored, nil
134 }