...

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

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

     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 appx
    18  
    19  // Sign Windows Universal (UWP) .appx and .appxbundle
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  
    26  	"github.com/sassoftware/relic/lib/certloader"
    27  	"github.com/sassoftware/relic/lib/magic"
    28  	"github.com/sassoftware/relic/lib/signappx"
    29  	"github.com/sassoftware/relic/signers"
    30  	"github.com/sassoftware/relic/signers/zipbased"
    31  )
    32  
    33  var AppxSigner = &signers.Signer{
    34  	Name:      "appx",
    35  	Magic:     magic.FileTypeAPPX,
    36  	CertTypes: signers.CertTypeX509,
    37  	Transform: zipbased.Transform,
    38  	Sign:      sign,
    39  	Verify:    verify,
    40  }
    41  
    42  func init() {
    43  	signers.Register(AppxSigner)
    44  }
    45  
    46  func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
    47  	digest, err := signappx.DigestAppxTar(r, opts.Hash, false)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	patch, priSig, _, err := digest.Sign(opts.Context(), cert)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	opts.Audit.SetCounterSignature(priSig.CounterSignature)
    56  	return opts.SetBinPatch(patch)
    57  }
    58  
    59  func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
    60  	size, err := f.Seek(0, io.SeekEnd)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	sig, err := signappx.Verify(f, size, opts.NoDigests)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	appxSig := sig
    69  	if sig.IsBundle {
    70  		for _, nested := range sig.Bundled {
    71  			appxSig = nested
    72  			break
    73  		}
    74  	}
    75  	return []*signers.Signature{&signers.Signature{
    76  		Package:       fmt.Sprintf("{%s} %s %s", appxSig.Name, appxSig.DisplayName, appxSig.Version),
    77  		Hash:          sig.Hash,
    78  		X509Signature: sig.Signature,
    79  	}}, nil
    80  }
    81  

View as plain text