...

Source file src/github.com/sassoftware/relic/signers/starman/info.go

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

     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 starman
    18  
    19  import (
    20  	"archive/tar"
    21  	"errors"
    22  	"io"
    23  	"io/ioutil"
    24  	"strings"
    25  
    26  	"github.com/sassoftware/relic/lib/readercounter"
    27  )
    28  
    29  const (
    30  	mdPrefix  = ".metadata/"
    31  	sigSuffix = ".sig"
    32  	padSuffix = ".pad"
    33  	blockSize = 512
    34  )
    35  
    36  type starmanInfo struct {
    37  	mdblob, sigblob  []byte
    38  	sigStart, sigEnd int64
    39  	mdname           string
    40  	md               TarMD
    41  	hasSig           bool
    42  }
    43  
    44  func verifyMeta(r io.Reader) (*starmanInfo, error) {
    45  	info := new(starmanInfo)
    46  	rc := readercounter.New(r)
    47  	tr := tar.NewReader(rc)
    48  	hdr, err := tr.Next()
    49  	if err != nil {
    50  		return nil, err
    51  	} else if !strings.HasPrefix(hdr.Name, mdPrefix) {
    52  		return nil, errors.New("unsupported archive format")
    53  	}
    54  	info.mdname = hdr.Name
    55  	info.mdblob, err = ioutil.ReadAll(tr)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	info.sigStart = (rc.N + blockSize - 1) / blockSize * blockSize
    60  
    61  	hdr, err = tr.Next()
    62  	if err != nil && err != io.EOF {
    63  		return nil, err
    64  	} else if hdr.Name == info.mdname+sigSuffix || hdr.Name == info.mdname+padSuffix {
    65  		// read existing signature
    66  		info.sigblob, err = ioutil.ReadAll(tr)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		if hdr.Name == info.mdname+sigSuffix {
    71  			info.hasSig = true
    72  		}
    73  		info.sigEnd = (rc.N + blockSize - 1) / blockSize * blockSize
    74  	} else {
    75  		info.sigEnd = info.sigStart
    76  	}
    77  	if err := info.verifyFiles(tr); err != nil {
    78  		return nil, err
    79  	}
    80  	return info, nil
    81  }
    82  

View as plain text