...

Source file src/github.com/sassoftware/relic/lib/pkcs9/verify.go

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

     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 pkcs9
    18  
    19  import (
    20  	"crypto"
    21  	"crypto/hmac"
    22  	"crypto/x509"
    23  	"fmt"
    24  	"time"
    25  
    26  	"github.com/pkg/errors"
    27  	"github.com/sassoftware/relic/lib/pkcs7"
    28  	"github.com/sassoftware/relic/lib/x509tools"
    29  )
    30  
    31  // Verify that the digest (imprint) in a timestamp token matches the given data
    32  func (i MessageImprint) Verify(data []byte) error {
    33  	hash, err := x509tools.PkixDigestToHashE(i.HashAlgorithm)
    34  	if err != nil {
    35  		return errors.Wrap(err, "pkcs9")
    36  	}
    37  	w := hash.New()
    38  	w.Write(data)
    39  	digest := w.Sum(nil)
    40  	if !hmac.Equal(digest, i.HashedMessage) {
    41  		return errors.New("pkcs9: digest check failed")
    42  	}
    43  	return nil
    44  }
    45  
    46  // Verify a timestamp token using external data
    47  func Verify(tst *pkcs7.ContentInfoSignedData, data []byte, certs []*x509.Certificate) (*CounterSignature, error) {
    48  	if len(tst.Content.SignerInfos) != 1 {
    49  		return nil, errors.New("timestamp should have exactly one SignerInfo")
    50  	}
    51  	tsi := tst.Content.SignerInfos[0]
    52  	tsicerts, err := tst.Content.Certificates.Parse()
    53  	if err != nil {
    54  		return nil, err
    55  	} else if len(tsicerts) != 0 {
    56  		// keep both sets of certs just in case
    57  		certs = append(certs, tsicerts...)
    58  	}
    59  	// verify the imprint in the TSTInfo
    60  	tstinfo, err := unpackTokenInfo(tst)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	if err := tstinfo.MessageImprint.Verify(data); err != nil {
    65  		return nil, fmt.Errorf("failed to verify timestamp imprint: %s", err)
    66  	}
    67  	imprintHash, _ := x509tools.PkixDigestToHash(tstinfo.MessageImprint.HashAlgorithm)
    68  	// now the signature is over the TSTInfo blob
    69  	verifyBlob, err := tst.Content.ContentInfo.Bytes()
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return finishVerify(&tsi, verifyBlob, certs, imprintHash)
    74  }
    75  
    76  func finishVerify(tsi *pkcs7.SignerInfo, blob []byte, certs []*x509.Certificate, hash crypto.Hash) (*CounterSignature, error) {
    77  	cert, err := tsi.Verify(blob, false, certs)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	var signingTime time.Time
    82  	if err := tsi.AuthenticatedAttributes.GetOne(pkcs7.OidAttributeSigningTime, &signingTime); err != nil {
    83  		return nil, err
    84  	}
    85  	return &CounterSignature{
    86  		Signature: pkcs7.Signature{
    87  			SignerInfo:    tsi,
    88  			Certificate:   cert,
    89  			Intermediates: certs,
    90  		},
    91  		Hash:        hash,
    92  		SigningTime: signingTime,
    93  	}, nil
    94  }
    95  

View as plain text