...

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

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

     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 cab
    18  
    19  // Sign Microsoft cabinet files
    20  
    21  import (
    22  	"io"
    23  	"os"
    24  
    25  	"github.com/sassoftware/relic/lib/authenticode"
    26  	"github.com/sassoftware/relic/lib/cabfile"
    27  	"github.com/sassoftware/relic/lib/certloader"
    28  	"github.com/sassoftware/relic/lib/magic"
    29  	"github.com/sassoftware/relic/signers"
    30  )
    31  
    32  var CabSigner = &signers.Signer{
    33  	Name:      "cab",
    34  	Magic:     magic.FileTypeCAB,
    35  	CertTypes: signers.CertTypeX509,
    36  	Sign:      sign,
    37  	Verify:    verify,
    38  }
    39  
    40  func init() {
    41  	signers.Register(CabSigner)
    42  }
    43  
    44  func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
    45  	digest, err := cabfile.Digest(r, opts.Hash)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	patch, ts, err := authenticode.SignCabImprint(opts.Context(), digest, cert)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	opts.Audit.SetCounterSignature(ts.CounterSignature)
    54  	return opts.SetBinPatch(patch)
    55  }
    56  
    57  func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
    58  	sig, err := authenticode.VerifyCab(f, opts.NoDigests)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return []*signers.Signature{&signers.Signature{
    63  		Hash:          sig.HashFunc,
    64  		X509Signature: &sig.TimestampedSignature,
    65  	}}, nil
    66  }
    67  

View as plain text