...

Source file src/github.com/sassoftware/relic/lib/authenticode/authenticode.go

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

     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 authenticode
    18  
    19  import (
    20  	"context"
    21  	"crypto"
    22  	"encoding/asn1"
    23  	"errors"
    24  
    25  	"github.com/sassoftware/relic/lib/certloader"
    26  	"github.com/sassoftware/relic/lib/pkcs7"
    27  	"github.com/sassoftware/relic/lib/pkcs9"
    28  	"github.com/sassoftware/relic/lib/x509tools"
    29  )
    30  
    31  func makePeIndirect(imprint []byte, hash crypto.Hash, oid asn1.ObjectIdentifier) (indirect SpcIndirectDataContentPe, err error) {
    32  	alg, ok := x509tools.PkixDigestAlgorithm(hash)
    33  	if !ok {
    34  		err = errors.New("unsupported digest algorithm")
    35  		return
    36  	}
    37  	indirect.Data.Type = oid
    38  	indirect.MessageDigest.Digest = imprint
    39  	indirect.MessageDigest.DigestAlgorithm = alg
    40  	indirect.Data.Value.File.File.Unicode = "<<<Obsolete>>>"
    41  	return
    42  }
    43  
    44  func signIndirect(ctx context.Context, indirect interface{}, hash crypto.Hash, cert *certloader.Certificate) (*pkcs9.TimestampedSignature, error) {
    45  	sig := pkcs7.NewBuilder(cert.Signer(), cert.Chain(), hash)
    46  	if err := sig.SetContent(OidSpcIndirectDataContent, indirect); err != nil {
    47  		return nil, err
    48  	}
    49  	if err := addOpusAttrs(sig); err != nil {
    50  		return nil, err
    51  	}
    52  	psd, err := sig.Sign()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return pkcs9.TimestampAndMarshal(ctx, psd, cert.Timestamper, true)
    57  }
    58  
    59  func addOpusAttrs(sig *pkcs7.SignatureBuilder) error {
    60  	if err := sig.AddAuthenticatedAttribute(OidSpcStatementType, SpcSpStatementType{Type: OidSpcIndividualPurpose}); err != nil {
    61  		return err
    62  	}
    63  	if err := sig.AddAuthenticatedAttribute(OidSpcSpOpusInfo, SpcSpOpusInfo{}); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  func SignSip(ctx context.Context, imprint []byte, hash crypto.Hash, sipInfo SpcSipInfo, cert *certloader.Certificate) (*pkcs9.TimestampedSignature, error) {
    70  	alg, ok := x509tools.PkixDigestAlgorithm(hash)
    71  	if !ok {
    72  		return nil, errors.New("unsupported digest algorithm")
    73  	}
    74  	var indirect SpcIndirectDataContentMsi
    75  	indirect.Data.Type = OidSpcSipInfo
    76  	indirect.Data.Value = sipInfo
    77  	indirect.MessageDigest.Digest = imprint
    78  	indirect.MessageDigest.DigestAlgorithm = alg
    79  	return signIndirect(ctx, indirect, hash, cert)
    80  }
    81  

View as plain text