...

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

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

     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 cat
    18  
    19  // Sign Microsoft security catalog files
    20  
    21  import (
    22  	"errors"
    23  	"io"
    24  	"io/ioutil"
    25  
    26  	"github.com/sassoftware/relic/lib/authenticode"
    27  	"github.com/sassoftware/relic/lib/certloader"
    28  	"github.com/sassoftware/relic/lib/magic"
    29  	"github.com/sassoftware/relic/lib/pkcs7"
    30  	"github.com/sassoftware/relic/lib/pkcs9"
    31  	"github.com/sassoftware/relic/signers"
    32  	"github.com/sassoftware/relic/signers/pkcs"
    33  )
    34  
    35  var CatSigner = &signers.Signer{
    36  	Name:      "cat",
    37  	Magic:     magic.FileTypeCAT,
    38  	CertTypes: signers.CertTypeX509,
    39  	Sign:      sign,
    40  	Verify:    pkcs.Verify,
    41  }
    42  
    43  func init() {
    44  	signers.Register(CatSigner)
    45  }
    46  
    47  func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
    48  	blob, err := ioutil.ReadAll(r)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	oldpsd, err := pkcs7.Unmarshal(blob)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	if !oldpsd.Content.ContentInfo.ContentType.Equal(authenticode.OidCertTrustList) {
    57  		return nil, errors.New("not a security catalog")
    58  	}
    59  	sig := pkcs7.NewBuilder(cert.Signer(), cert.Chain(), opts.Hash)
    60  	if err := sig.SetContentInfo(oldpsd.Content.ContentInfo); err != nil {
    61  		return nil, err
    62  	}
    63  	newpsd, err := sig.Sign()
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	ts, err := pkcs9.TimestampAndMarshal(opts.Context(), newpsd, cert.Timestamper, true)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return opts.SetPkcs7(ts)
    72  }
    73  

View as plain text