...

Source file src/github.com/sassoftware/relic/lib/signappx/manifest.go

Documentation: github.com/sassoftware/relic/lib/signappx

     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 signappx
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/x509"
    22  	"encoding/xml"
    23  	"fmt"
    24  
    25  	"github.com/beevik/etree"
    26  
    27  	"github.com/sassoftware/relic/lib/x509tools"
    28  )
    29  
    30  type appxPackage struct {
    31  	XMLName xml.Name
    32  
    33  	Identity appxIdentity
    34  
    35  	DisplayName          string `xml:"Properties>DisplayName"`
    36  	PublisherDisplayName string `xml:"Properties>PublisherDisplayName"`
    37  	Logo                 string `xml:"Properties>Logo"`
    38  
    39  	Etree *etree.Document `xml:"-"`
    40  }
    41  
    42  type appxIdentity struct {
    43  	Name                  string `xml:",attr"`
    44  	Publisher             string `xml:",attr"`
    45  	Version               string `xml:",attr"`
    46  	ProcessorArchitecture string `xml:",attr"`
    47  }
    48  
    49  func parseManifest(blob []byte) (*appxPackage, error) {
    50  	manifest := new(appxPackage)
    51  	if err := xml.Unmarshal(blob, manifest); err != nil {
    52  		return nil, err
    53  	}
    54  	manifest.Etree = etree.NewDocument()
    55  	if err := manifest.Etree.ReadFromBytes(blob); err != nil {
    56  		return nil, err
    57  	}
    58  	return manifest, nil
    59  }
    60  
    61  func checkManifest(files zipFiles, sig *AppxSignature) error {
    62  	blob, err := readZipFile(files[appxManifest])
    63  	if err != nil {
    64  		return fmt.Errorf("appx manifest: %s", err)
    65  	}
    66  	var manifest appxPackage
    67  	if err := xml.Unmarshal(blob, &manifest); err != nil {
    68  		return fmt.Errorf("appx manifest: %s", err)
    69  	}
    70  	sig.Name = manifest.Identity.Name
    71  	sig.DisplayName = manifest.DisplayName
    72  	sig.Version = manifest.Identity.Version
    73  	publisher := x509tools.FormatPkixName(sig.Signature.Certificate.RawSubject, x509tools.NameStyleMsOsco)
    74  	if manifest.Identity.Publisher != publisher {
    75  		return fmt.Errorf("appx manifest: publisher identity mismatch:\nexpected: %s\nactual: %s", publisher, manifest.Identity.Publisher)
    76  	}
    77  	return nil
    78  }
    79  
    80  func (m *appxPackage) SetPublisher(cert *x509.Certificate) {
    81  	subj := x509tools.FormatPkixName(cert.RawSubject, x509tools.NameStyleMsOsco)
    82  	m.Identity.Publisher = subj
    83  	el := m.Etree.FindElement("Package/Identity")
    84  	if el != nil {
    85  		el.CreateAttr("Publisher", subj)
    86  	}
    87  }
    88  
    89  func (m *appxPackage) Marshal() ([]byte, error) {
    90  	b, err := m.Etree.WriteToBytes()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	return bytes.Replace(b, []byte{'\n'}, []byte{'\r', '\n'}, -1), nil
    95  }
    96  

View as plain text