| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 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 | |
| 17 | package signers |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "os" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/rs/zerolog" |
| 28 | "github.com/spf13/cobra" |
| 29 | "github.com/spf13/pflag" |
| 30 | |
| 31 | "golang.org/x/crypto/openpgp" |
| 32 | |
| 33 | "github.com/sassoftware/relic/v7/lib/audit" |
| 34 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 35 | "github.com/sassoftware/relic/v7/lib/magic" |
| 36 | "github.com/sassoftware/relic/v7/lib/pgptools" |
| 37 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 38 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 39 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 40 | ) |
| 41 | |
| 42 | type Signer struct { |
| 43 | Name string |
| 44 | Aliases []string |
| 45 | Magic magic.FileType |
| 46 | CertTypes CertType |
| 47 | AllowStdin bool |
| 48 | // Return true if the given filename is associated with this signer |
| 49 | TestPath func(string) bool |
| 50 | // Format audit attributes for logfile |
| 51 | FormatLog func(*audit.Info) *zerolog.Event |
| 52 | // Verify a file, returning the set of signatures found. Performs integrity |
| 53 | // checks but does not build X509 chains. |
| 54 | Verify func(*os.File, VerifyOpts) ([]*Signature, error) |
| 55 | // VerifyStream is like Verify but doesn't need to seek. |
| 56 | VerifyStream func(io.Reader, VerifyOpts) ([]*Signature, error) |
| 57 | // Transform a file into a stream to upload |
| 58 | Transform func(*os.File, SignOpts) (Transformer, error) |
| 59 | // Sign a input stream (possibly transformed) and return a mode-specific result blob |
| 60 | Sign func(io.Reader, *certloader.Certificate, SignOpts) ([]byte, error) |
| 61 | // Final step to run on the client after the file is patched |
| 62 | Fixup func(*os.File) error |
| 63 | |
| 64 | flags *pflag.FlagSet |
| 65 | } |
| 66 | |
| 67 | type CertType uint |
| 68 | |
| 69 | const ( |
| 70 | CertTypeX509 CertType = 1 << iota |
| 71 | CertTypePgp |
| 72 | ) |
| 73 | |
| 74 | type Signature struct { |
| 75 | Package string |
| 76 | SigInfo string |
| 77 | CreationTime time.Time |
| 78 | Hash crypto.Hash |
| 79 | Signer string |
| 80 | SignerPgp *openpgp.Entity |
| 81 | X509Signature *pkcs9.TimestampedSignature |
| 82 | } |
| 83 | |
| 84 | func (s *Signature) SignerName() string { |
| 85 | if s.Signer != "" { |
| 86 | return s.Signer |
| 87 | } |
| 88 | if s.X509Signature != nil { |
| 89 | return fmt.Sprintf("`%s`", x509tools.FormatSubject(s.X509Signature.Certificate)) |
| 90 | } |
| 91 | if s.SignerPgp != nil { |
| 92 | return fmt.Sprintf("`%s`(%x)", pgptools.EntityName(s.SignerPgp), s.SignerPgp.PrimaryKey.KeyId) |
| 93 | } |
| 94 | return "UNKNOWN" |
| 95 | } |
| 96 | |
| 97 | var registered []*Signer |
| 98 | var flagMap map[string][]string |
| 99 | |
| 100 | func Register(s *Signer) { |
| 101 | registered = append(registered, s) |
| 102 | } |
| 103 | |
| 104 | // Return the signer module with the given name or alias |
| 105 | func ByName(name string) *Signer { |
| 106 | for _, s := range registered { |
| 107 | if s.Name == name { |
| 108 | return s |
| 109 | } |
| 110 | for _, n2 := range s.Aliases { |
| 111 | if n2 == name { |
| 112 | return s |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // Return the signer module responsible for the given file magic |
| 120 | func ByMagic(m magic.FileType) *Signer { |
| 121 | if m == magic.FileTypeUnknown { |
| 122 | return nil |
| 123 | } |
| 124 | for _, s := range registered { |
| 125 | if s.Magic == m { |
| 126 | return s |
| 127 | } |
| 128 | } |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // Return the signer associated with the given filename extension |
| 133 | func ByFileName(name string) *Signer { |
| 134 | for _, s := range registered { |
| 135 | if s.TestPath != nil && s.TestPath(name) { |
| 136 | return s |
| 137 | } |
| 138 | } |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // Return the named signer module if given, otherwise identify the file at the |
| 143 | // given path by contents or extension |
| 144 | func ByFile(name, sigtype string) (*Signer, error) { |
| 145 | if sigtype != "" { |
| 146 | mod := ByName(sigtype) |
| 147 | if mod == nil { |
| 148 | return nil, errors.New("no signer with that name") |
| 149 | } |
| 150 | return mod, nil |
| 151 | } |
| 152 | if name == "-" { |
| 153 | return nil, errors.New("reading from standard input is not supported") |
| 154 | } |
| 155 | f, err := os.Open(name) |
| 156 | if err != nil { |
| 157 | return nil, err |
| 158 | } |
| 159 | defer f.Close() |
| 160 | fileType, compressionType := magic.DetectCompressed(f) |
| 161 | if compressionType != magic.CompressedNone { |
| 162 | return nil, errors.New("cannot sign compressed file") |
| 163 | } |
| 164 | if mod := ByMagic(fileType); mod != nil { |
| 165 | return mod, nil |
| 166 | } else if mod := ByFileName(name); mod != nil { |
| 167 | return mod, nil |
| 168 | } |
| 169 | return nil, errors.New("unknown filetype") |
| 170 | } |
| 171 | |
| 172 | // Create a FlagSet for flags associated with this module. These will be added |
| 173 | // to "sign" and "remote sign", and transferred to a remote server via the URL |
| 174 | // query parameters. |
| 175 | func (s *Signer) Flags() *pflag.FlagSet { |
| 176 | if s.flags == nil { |
| 177 | s.flags = pflag.NewFlagSet(s.Name, pflag.ExitOnError) |
| 178 | } |
| 179 | return s.flags |
| 180 | } |
| 181 | |
| 182 | // Add this module's flags to a command FlagSet |
| 183 | func MergeFlags(cmd *cobra.Command) { |
| 184 | if flagMap == nil { |
| 185 | flagMap = make(map[string][]string) |
| 186 | } |
| 187 | fs := cmd.Flags() |
| 188 | fs.AddFlagSet(common) |
| 189 | for _, s := range registered { |
| 190 | if s.flags == nil { |
| 191 | continue |
| 192 | } |
| 193 | fs.AddFlagSet(s.flags) |
| 194 | s.flags.VisitAll(func(flag *pflag.Flag) { |
| 195 | flagMap[flag.Name] = append(flagMap[flag.Name], s.Name) |
| 196 | }) |
| 197 | } |
| 198 | // customize the usage function so that if --sig-type is set then only those flags are displayed |
| 199 | orig := cmd.UsageFunc() |
| 200 | cmd.SetUsageFunc(func(c *cobra.Command) error { |
| 201 | if t, _ := fs.GetString("sig-type"); t != "" { |
| 202 | for name, signers := range flagMap { |
| 203 | var ok bool |
| 204 | for _, signer := range signers { |
| 205 | if signer == t { |
| 206 | ok = true |
| 207 | break |
| 208 | } |
| 209 | } |
| 210 | if !ok { |
| 211 | _ = fs.MarkHidden(name) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return orig(c) |
| 216 | }) |
| 217 | } |
| 218 | |
| 219 | // IsSigned checks if a file contains a signature |
| 220 | func (s *Signer) IsSigned(f *os.File) (bool, error) { |
| 221 | var err error |
| 222 | if s.VerifyStream != nil { |
| 223 | _, err = s.VerifyStream(f, VerifyOpts{NoDigests: true, NoChain: true}) |
| 224 | } else if s.Verify != nil { |
| 225 | _, err = s.Verify(f, VerifyOpts{NoDigests: true, NoChain: true}) |
| 226 | } else { |
| 227 | return false, errors.New("cannot check if this type of file is signed") |
| 228 | } |
| 229 | if err == nil { |
| 230 | return true, nil |
| 231 | } |
| 232 | switch err.(type) { |
| 233 | case sigerrors.NotSignedError: |
| 234 | return false, nil |
| 235 | case pgptools.ErrNoKey: |
| 236 | return true, nil |
| 237 | } |
| 238 | return false, err |
| 239 | } |