| 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 ps |
| 18 | |
| 19 | // Sign Microsoft PowerShell scripts, modules, and other bits that can be signed |
| 20 | |
| 21 | import ( |
| 22 | "errors" |
| 23 | "io" |
| 24 | "os" |
| 25 | "path/filepath" |
| 26 | "strings" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/authenticode" |
| 29 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 30 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 31 | "github.com/sassoftware/relic/v7/signers" |
| 32 | "github.com/sassoftware/relic/v7/signers/pecoff" |
| 33 | ) |
| 34 | |
| 35 | var PsSigner = &signers.Signer{ |
| 36 | Name: "ps", |
| 37 | CertTypes: signers.CertTypeX509, |
| 38 | TestPath: testPath, |
| 39 | Transform: transform, |
| 40 | Sign: sign, |
| 41 | Verify: verify, |
| 42 | } |
| 43 | |
| 44 | func init() { |
| 45 | PsSigner.Flags().String("ps-style", "", "(Powershell) signature type") |
| 46 | pecoff.AddOpusFlags(PsSigner) |
| 47 | signers.Register(PsSigner) |
| 48 | } |
| 49 | |
| 50 | func testPath(fp string) bool { |
| 51 | _, ok := authenticode.GetSigStyle(fp) |
| 52 | return ok |
| 53 | } |
| 54 | |
| 55 | func transform(f *os.File, opts signers.SignOpts) (signers.Transformer, error) { |
| 56 | // detect signature style and explicitly set it for the request |
| 57 | argStyle := opts.Flags.GetString("ps-style") |
| 58 | if argStyle == "" { |
| 59 | argStyle = filepath.Ext(opts.Path) |
| 60 | } |
| 61 | opts.Flags.Values["ps-style"] = argStyle |
| 62 | return signers.DefaultTransform(f), nil |
| 63 | } |
| 64 | |
| 65 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 66 | argStyle := opts.Flags.GetString("ps-style") |
| 67 | if argStyle == "" { |
| 68 | argStyle = opts.Path |
| 69 | } |
| 70 | style, err := getStyle(argStyle) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | digest, err := authenticode.DigestPowershell(r, style, opts.Hash) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | patch, ts, err := digest.Sign(opts.Context(), cert, pecoff.OpusFlags(opts)) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | opts.Audit.SetCounterSignature(ts.CounterSignature) |
| 83 | return opts.SetBinPatch(patch) |
| 84 | } |
| 85 | |
| 86 | func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 87 | style, err := getStyle(f.Name()) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | ts, err := authenticode.VerifyPowershell(f, style, opts.NoDigests) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | hash, _ := x509tools.PkixDigestToHash(ts.SignerInfo.DigestAlgorithm) |
| 96 | return []*signers.Signature{{ |
| 97 | Hash: hash, |
| 98 | X509Signature: &ts.TimestampedSignature, |
| 99 | SigInfo: pecoff.FormatOpus(ts.OpusInfo), |
| 100 | }}, nil |
| 101 | } |
| 102 | |
| 103 | func getStyle(name string) (authenticode.PsSigStyle, error) { |
| 104 | style, ok := authenticode.GetSigStyle(name) |
| 105 | if !ok { |
| 106 | return 0, errors.New("unknown powershell style, expected: " + strings.Join(authenticode.AllSigStyles(), " ")) |
| 107 | } |
| 108 | return style, nil |
| 109 | } |