Debian Codegraph @

signers/pecoff/signer.go

raw symbols SCIP
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 pecoff
18
19 // Sign Microsoft PE/COFF executables
20
21 import (
22 "fmt"
23 "io"
24 "os"
25 "strings"
26
27 "github.com/sassoftware/relic/v7/lib/authenticode"
28 "github.com/sassoftware/relic/v7/lib/certloader"
29 "github.com/sassoftware/relic/v7/lib/magic"
30 "github.com/sassoftware/relic/v7/signers"
31 )
32
33 var PeSigner = &signers.Signer{
34 Name: "pe-coff",
35 Magic: magic.FileTypePECOFF,
36 CertTypes: signers.CertTypeX509,
37 Sign: sign,
38 Fixup: authenticode.FixPEChecksum,
39 Verify: verify,
40 }
41
42 func init() {
43 PeSigner.Flags().Bool("page-hashes", false, "(PE-COFF) Add page hashes to signature")
44 AddOpusFlags(PeSigner)
45 signers.Register(PeSigner)
46 }
47
48 func AddOpusFlags(s *signers.Signer) {
49 s.Flags().String("description", "", "(Win) Set description of signed content")
50 s.Flags().String("desc-url", "", "(Win) Set URL for description of signed content")
51 }
52
53 func OpusFlags(opts signers.SignOpts) *authenticode.OpusParams {
54 return &authenticode.OpusParams{
55 Description: opts.Flags.GetString("description"),
56 URL: opts.Flags.GetString("desc-url"),
57 }
58 }
59
60 func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
61 pageHashes := opts.Flags.GetBool("page-hashes")
62 digest, err := authenticode.DigestPE(r, opts.Hash, pageHashes)
63 if err != nil {
64 return nil, err
65 }
66 patch, ts, err := digest.Sign(opts.Context(), cert, OpusFlags(opts))
67 if err != nil {
68 return nil, err
69 }
70 opts.Audit.Attributes["pe-coff.pagehashes"] = pageHashes
71 opts.Audit.SetCounterSignature(ts.CounterSignature)
72 return opts.SetBinPatch(patch)
73 }
74
75 func FormatOpus(info *authenticode.SpcSpOpusInfo) string {
76 if info == nil {
77 return ""
78 }
79 var infos []string
80 if desc := info.ProgramName.String(); desc != "" {
81 infos = append(infos, fmt.Sprintf("[desc:%q]", desc))
82 }
83 if u := info.MoreInfo.URL; u != "" {
84 infos = append(infos, fmt.Sprintf("[url:%q]", u))
85 }
86 return strings.Join(infos, "")
87 }
88
89 func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
90 sigs, err := authenticode.VerifyPE(f, opts.NoDigests)
91 if err != nil {
92 return nil, err
93 }
94 var ret []*signers.Signature
95 for _, sig := range sigs {
96 ret = append(ret, &signers.Signature{
97 SigInfo: FormatOpus(sig.OpusInfo),
98 Hash: sig.ImageHashFunc,
99 X509Signature: &sig.TimestampedSignature,
100 })
101 }
102 return ret, nil
103 }