...

Source file src/github.com/sassoftware/relic/signers/pecoff/signer.go

Documentation: github.com/sassoftware/relic/signers/pecoff

     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  	"io"
    23  	"os"
    24  
    25  	"github.com/sassoftware/relic/lib/authenticode"
    26  	"github.com/sassoftware/relic/lib/certloader"
    27  	"github.com/sassoftware/relic/lib/magic"
    28  	"github.com/sassoftware/relic/signers"
    29  )
    30  
    31  var PeSigner = &signers.Signer{
    32  	Name:      "pe-coff",
    33  	Magic:     magic.FileTypePECOFF,
    34  	CertTypes: signers.CertTypeX509,
    35  	Sign:      sign,
    36  	Fixup:     authenticode.FixPEChecksum,
    37  	Verify:    verify,
    38  }
    39  
    40  func init() {
    41  	PeSigner.Flags().Bool("page-hashes", false, "(PE-COFF) Add page hashes to signature")
    42  	signers.Register(PeSigner)
    43  }
    44  
    45  func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
    46  	pageHashes := opts.Flags.GetBool("page-hashes")
    47  	digest, err := authenticode.DigestPE(r, opts.Hash, pageHashes)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	patch, ts, err := digest.Sign(opts.Context(), cert)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	opts.Audit.Attributes["pe-coff.pagehashes"] = pageHashes
    56  	opts.Audit.SetCounterSignature(ts.CounterSignature)
    57  	return opts.SetBinPatch(patch)
    58  }
    59  
    60  func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
    61  	sigs, err := authenticode.VerifyPE(f, opts.NoDigests)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	var ret []*signers.Signature
    66  	for _, sig := range sigs {
    67  		ret = append(ret, &signers.Signature{
    68  			Hash:          sig.ImageHashFunc,
    69  			X509Signature: &sig.TimestampedSignature,
    70  		})
    71  	}
    72  	return ret, nil
    73  }
    74  

View as plain text