| 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 | "context" |
| 21 | "crypto" |
| 22 | "crypto/x509" |
| 23 | "fmt" |
| 24 | "net/url" |
| 25 | "strconv" |
| 26 | "time" |
| 27 | |
| 28 | "github.com/spf13/pflag" |
| 29 | "golang.org/x/crypto/openpgp" |
| 30 | |
| 31 | "github.com/sassoftware/relic/v7/lib/audit" |
| 32 | "github.com/sassoftware/relic/v7/lib/binpatch" |
| 33 | "github.com/sassoftware/relic/v7/lib/magic" |
| 34 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 35 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 36 | ) |
| 37 | |
| 38 | var common *pflag.FlagSet |
| 39 | |
| 40 | func init() { |
| 41 | common = pflag.NewFlagSet("common", pflag.ExitOnError) |
| 42 | common.Bool("no-timestamp", false, "Do not attach a trusted timestamp even if the selected key configures one") |
| 43 | } |
| 44 | |
| 45 | type SignOpts struct { |
| 46 | Path string |
| 47 | Hash crypto.Hash |
| 48 | Time time.Time |
| 49 | Flags *FlagValues |
| 50 | Audit *audit.Info |
| 51 | ctx context.Context |
| 52 | } |
| 53 | |
| 54 | // Convenience method to return a binary patch |
| 55 | func (o SignOpts) SetBinPatch(p *binpatch.PatchSet) ([]byte, error) { |
| 56 | o.Audit.SetMimeType(binpatch.MimeType) |
| 57 | return p.Dump(), nil |
| 58 | } |
| 59 | |
| 60 | // Convenience method to return a PKCS#7 blob |
| 61 | func (o SignOpts) SetPkcs7(ts *pkcs9.TimestampedSignature) ([]byte, error) { |
| 62 | o.Audit.SetCounterSignature(ts.CounterSignature) |
| 63 | o.Audit.SetMimeType(pkcs7.MimeType) |
| 64 | return ts.Raw, nil |
| 65 | } |
| 66 | |
| 67 | // WithContext attaches a context to the signature operation, and can be used to cancel long-running operations. |
| 68 | func (o SignOpts) WithContext(ctx context.Context) SignOpts { |
| 69 | o.ctx = ctx |
| 70 | return o |
| 71 | } |
| 72 | |
| 73 | // Context returns the context attached to the signature operation. |
| 74 | // |
| 75 | // The returned context is always non-nil; it defaults to the background context. |
| 76 | func (o SignOpts) Context() context.Context { |
| 77 | if o.ctx != nil { |
| 78 | return o.ctx |
| 79 | } |
| 80 | return context.Background() |
| 81 | } |
| 82 | |
| 83 | type VerifyOpts struct { |
| 84 | FileName string |
| 85 | TrustedX509 []*x509.Certificate |
| 86 | TrustedPgp openpgp.EntityList |
| 87 | TrustedPool *x509.CertPool |
| 88 | NoDigests bool |
| 89 | NoChain bool |
| 90 | Content string |
| 91 | Compression magic.CompressionType |
| 92 | } |
| 93 | |
| 94 | type FlagValues struct { |
| 95 | Defs *pflag.FlagSet |
| 96 | Values map[string]string |
| 97 | } |
| 98 | |
| 99 | func (v *FlagValues) mergeAll(defs *pflag.FlagSet, getter func(string) string) { |
| 100 | if defs != nil { |
| 101 | v.mergeSet(defs, getter) |
| 102 | } |
| 103 | v.mergeSet(common, getter) |
| 104 | } |
| 105 | |
| 106 | func (v *FlagValues) mergeSet(defs *pflag.FlagSet, getter func(string) string) { |
| 107 | defs.VisitAll(func(flag *pflag.Flag) { |
| 108 | value := getter(flag.Name) |
| 109 | if value != "" { |
| 110 | v.Values[flag.Name] = value |
| 111 | } |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | // FlagsFromCmdline creates a FlagValues from the (merged) command-line options of a command |
| 116 | func (s *Signer) FlagsFromCmdline(fs *pflag.FlagSet) (*FlagValues, error) { |
| 117 | for flag, users := range flagMap { |
| 118 | if !fs.Changed(flag) { |
| 119 | continue |
| 120 | } |
| 121 | allowed := false |
| 122 | for _, name := range users { |
| 123 | if name == s.Name { |
| 124 | allowed = true |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | if !allowed { |
| 129 | return nil, fmt.Errorf("flag \"%s\" is not allowed for signature type \"%s\"", flag, s.Name) |
| 130 | } |
| 131 | } |
| 132 | values := &FlagValues{ |
| 133 | Defs: s.flags, |
| 134 | Values: make(map[string]string), |
| 135 | } |
| 136 | values.mergeAll(s.flags, func(name string) string { |
| 137 | if !fs.Changed(name) { |
| 138 | return "" |
| 139 | } |
| 140 | return fs.Lookup(name).Value.String() |
| 141 | }) |
| 142 | return values, nil |
| 143 | } |
| 144 | |
| 145 | // FlagsFromQuery creates a FlagValues from URL query parameters |
| 146 | func (s *Signer) FlagsFromQuery(q url.Values) (*FlagValues, error) { |
| 147 | values := &FlagValues{ |
| 148 | Defs: s.flags, |
| 149 | Values: make(map[string]string), |
| 150 | } |
| 151 | values.mergeAll(s.flags, q.Get) |
| 152 | return values, nil |
| 153 | } |
| 154 | |
| 155 | // ToQuery appends query parameters to a URL for each option in the flag set |
| 156 | func (values *FlagValues) ToQuery(q url.Values) error { |
| 157 | for key, value := range values.Values { |
| 158 | q.Set(key, value) |
| 159 | } |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | // GetString returns the flag's value as a string |
| 164 | func (values *FlagValues) GetString(name string) string { |
| 165 | flag := common.Lookup(name) |
| 166 | if flag == nil && values.Defs != nil { |
| 167 | flag = values.Defs.Lookup(name) |
| 168 | } |
| 169 | if flag == nil { |
| 170 | panic("flag " + name + " not defined for signer module") |
| 171 | } |
| 172 | if v, ok := values.Values[name]; ok { |
| 173 | return v |
| 174 | } |
| 175 | return flag.DefValue |
| 176 | } |
| 177 | |
| 178 | // GetBool returns the flag's value as a bool |
| 179 | func (values *FlagValues) GetBool(name string) bool { |
| 180 | str := values.GetString(name) |
| 181 | b, _ := strconv.ParseBool(str) |
| 182 | return b |
| 183 | } |