| 1 | package objectscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 6 | |
| 7 | "github.com/go-git/go-git/v6/plumbingscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/" |
| 8 | "github.com/go-git/go-git/v6/utils/ioutilscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/ioutil`/" |
| 9 | "github.com/go-git/go-git/v6/utils/syncscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/sync`/" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | signatureTypeUnknownscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeUnknown. signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType# = iota |
| 14 | signatureTypeOpenPGPscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeOpenPGP. |
| 15 | signatureTypeX509scip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeX509. |
| 16 | signatureTypeSSHscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeSSH. |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | // openPGPSignatureFormat is the format of an OpenPGP signature. |
| 21 | openPGPSignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/openPGPSignatureFormat. = signatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureFormat#{ |
| 22 | []byte("-----BEGIN PGP SIGNATURE-----"), |
| 23 | []byte("-----BEGIN PGP MESSAGE-----"), |
| 24 | } |
| 25 | // x509SignatureFormat is the format of an X509 signature, which is |
| 26 | // a PKCS#7 (S/MIME) signature. |
| 27 | x509SignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/x509SignatureFormat. = signatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureFormat#{ |
| 28 | []byte("-----BEGIN SIGNED MESSAGE-----"), |
| 29 | } |
| 30 | |
| 31 | // sshSignatureFormat is the format of an SSH signature. |
| 32 | sshSignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/sshSignatureFormat. = signatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureFormat#{ |
| 33 | []byte("-----BEGIN SSH SIGNATURE-----"), |
| 34 | } |
| 35 | ) |
| 36 | |
| 37 | // knownSignatureFormats is a map of known signature formats, indexed by |
| 38 | // their signatureType. |
| 39 | var knownSignatureFormatsscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/knownSignatureFormats. = map[signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType#]signatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureFormat#{ |
| 40 | signatureTypeOpenPGPscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeOpenPGP.: openPGPSignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/openPGPSignatureFormat., |
| 41 | signatureTypeX509scip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeX509.: x509SignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/x509SignatureFormat., |
| 42 | signatureTypeSSHscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeSSH.: sshSignatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/sshSignatureFormat., |
| 43 | } |
| 44 | |
| 45 | // signatureType represents the type of the signature. |
| 46 | type signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType# int8 |
| 47 | |
| 48 | // signatureFormat represents the beginning of a signature. |
| 49 | type signatureFormatscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureFormat# [][]byte |
| 50 | |
| 51 | // typeForSignature returns the type of the signature based on its format. |
| 52 | func typeForSignaturescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/typeForSignature().(b []byte) signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType# { |
| 53 | for t, i := range knownSignatureFormatsscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/knownSignatureFormats. { |
| 54 | for _, begin := range i { |
| 55 | if bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(b, begin) { |
| 56 | return t |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return signatureTypeUnknownscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeUnknown. |
| 61 | } |
| 62 | |
| 63 | // parseSignedBytes returns the position of the last signature block found in |
| 64 | // the given bytes. If no signature block is found, it returns -1. |
| 65 | // |
| 66 | // When multiple signature blocks are found, the position of the last one is |
| 67 | // returned. Any tailing bytes after this signature block start should be |
| 68 | // considered part of the signature. |
| 69 | // |
| 70 | // Given this, it would be safe to use the returned position to split the bytes |
| 71 | // into two parts: the first part containing the message, the second part |
| 72 | // containing the signature. |
| 73 | // |
| 74 | // Example: |
| 75 | // |
| 76 | // message := []byte(`Message with signature |
| 77 | // |
| 78 | // -----BEGIN SSH SIGNATURE----- |
| 79 | // ...`) |
| 80 | // |
| 81 | // var signature string |
| 82 | // if pos, _ := parseSignedBytes(message); pos != -1 { |
| 83 | // signature = string(message[pos:]) |
| 84 | // message = message[:pos] |
| 85 | // } |
| 86 | // |
| 87 | // This logic is on par with git's gpg-interface.c:parse_signed_buffer(). |
| 88 | // https://github.com/git/git/blob/7c2ef319c52c4997256f5807564523dfd4acdfc7/gpg-interface.c#L668 |
| 89 | func parseSignedBytesscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/parseSignedBytes().(b []byte) (int, signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType#) { |
| 90 | n, match := 0, -1 |
| 91 | var t signatureTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureType# |
| 92 | for n < len(b) { |
| 93 | i := b[n:] |
| 94 | if st := typeForSignaturescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/typeForSignature().(i); st != signatureTypeUnknownscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeUnknown. { |
| 95 | match = n |
| 96 | t = st |
| 97 | } |
| 98 | if eol := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/IndexByte().(i, '\n'); eol >= 0 { |
| 99 | n += eol + 1 |
| 100 | continue |
| 101 | } |
| 102 | // If we reach this point, we've reached the end. |
| 103 | break |
| 104 | } |
| 105 | return match, t |
| 106 | } |
| 107 | |
| 108 | // countSignatureBlocks reports how many distinct armored signature blocks |
| 109 | // start at a line boundary in b. Used by verification paths to reject |
| 110 | // multi-signature payloads, matching upstream's check in gpg-interface.c |
| 111 | // where parse_gpg_output bails out the first time it sees a second |
| 112 | // exclusive status line (a second GOODSIG/BADSIG/etc.). |
| 113 | func countSignatureBlocksscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/countSignatureBlocks().(b []byte) int { |
| 114 | n, count := 0, 0 |
| 115 | for n < len(b) { |
| 116 | i := b[n:] |
| 117 | if typeForSignaturescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/typeForSignature().(i) != signatureTypeUnknownscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/signatureTypeUnknown. { |
| 118 | count++ |
| 119 | } |
| 120 | if eol := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/IndexByte().(i, '\n'); eol >= 0 { |
| 121 | n += eol + 1 |
| 122 | continue |
| 123 | } |
| 124 | break |
| 125 | } |
| 126 | return count |
| 127 | } |
| 128 | |
| 129 | // isSignatureHeader reports whether line is a canonical "gpgsig "/ |
| 130 | // "gpgsig-sha256 " header line. Other "gpgsig"-prefixed extra headers |
| 131 | // are intentionally not matched. |
| 132 | func isSignatureHeaderscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/isSignatureHeader().(line []byte) bool { |
| 133 | return bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(line, []byte(headerpgpscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/headerpgp.+" ")) || |
| 134 | bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 bytes/HasPrefix().(line, []byte(headerpgp256scip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/headerpgp256.+" ")) |
| 135 | } |
| 136 | |
| 137 | // stripObjectSignatures streams src into dst, producing the byte sequence |
| 138 | // over which a PGP/GPG signature is computed: |
| 139 | // |
| 140 | // - Canonical "gpgsig" and "gpgsig-sha256" headers (and their |
| 141 | // continuation lines) are dropped, mirroring upstream's |
| 142 | // remove_signature in commit.c. |
| 143 | // - For tag objects, the inline trailing PGP signature is additionally |
| 144 | // truncated, mirroring upstream's parse_signature in gpg-interface.c |
| 145 | // used by gpg_verify_tag. |
| 146 | // |
| 147 | // The returned object's type is set to objType. Used by both |
| 148 | // Commit.EncodeWithoutSignature and Tag.EncodeWithoutSignature to |
| 149 | // reproduce the exact bytes the signature was computed over. |
| 150 | func stripObjectSignaturesscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/stripObjectSignatures().(dst, src plumbingscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/.EncodedObjectscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/EncodedObject#, objType plumbingscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/.ObjectTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/ObjectType#) (err error) { |
| 151 | dst.SetTypescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/EncodedObject#SetType.(objType) |
| 152 | |
| 153 | r, err := src.Readerscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/EncodedObject#Reader.() |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | defer ioutilscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/ioutil`/.CheckClosescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/ioutil`/CheckClose().(r, &err) |
| 158 | |
| 159 | var input 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# = r |
| 160 | if objType == plumbingscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/.TagObjectscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/TagObject. { |
| 161 | raw, 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().(r) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | if sm, _ := parseSignedBytesscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/parseSignedBytes().(raw); sm >= 0 { |
| 166 | raw = raw[:sm] |
| 167 | } |
| 168 | input = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewReader().(raw) |
| 169 | } |
| 170 | |
| 171 | w, err := dst.Writerscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing`/EncodedObject#Writer.() |
| 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | defer ioutilscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/ioutil`/.CheckClosescip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/ioutil`/CheckClose().(w, &err) |
| 176 | |
| 177 | return stripHeaderSignaturesscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/stripHeaderSignatures().(w, input) |
| 178 | } |
| 179 | |
| 180 | // stripHeaderSignatures copies r to w, dropping canonical signature header |
| 181 | // lines (gpgsig and gpgsig-sha256) and their continuation lines. Lines |
| 182 | // past the blank line that closes the header block are copied verbatim. |
| 183 | func stripHeaderSignaturesscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/stripHeaderSignatures().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#, r 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#) error { |
| 184 | br := syncscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/sync`/.GetBufioReaderscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/sync`/GetBufioReader().(r) |
| 185 | defer syncscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/sync`/.PutBufioReaderscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/utils/sync`/PutBufioReader().(br) |
| 186 | |
| 187 | var inBody, skipping bool |
| 188 | for { |
| 189 | line, rerr := br.ReadBytesscip-go gomod github.com/golang/go/src go1.25.0 bufio/Reader#ReadBytes().('\n') |
| 190 | if rerr != nil && rerr != ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. { |
| 191 | return rerr |
| 192 | } |
| 193 | |
| 194 | write := true |
| 195 | if !inBody { |
| 196 | switch { |
| 197 | case skipping && len(line) > 0 && line[0] == ' ': |
| 198 | write = false |
| 199 | case isSignatureHeaderscip-go gomod github.com/go-git/go-git/v6 . `github.com/go-git/go-git/v6/plumbing/object`/isSignatureHeader().(line): |
| 200 | skipping = true |
| 201 | write = false |
| 202 | case len(line) == 1 && line[0] == '\n': |
| 203 | skipping = false |
| 204 | inBody = true |
| 205 | default: |
| 206 | skipping = false |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if write && len(line) > 0 { |
| 211 | if _, werr := w.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(line); werr != nil { |
| 212 | return werr |
| 213 | } |
| 214 | } |
| 215 | if rerr == ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. { |
| 216 | return nil |
| 217 | } |
| 218 | } |
| 219 | } |