Debian Codegraph @

lib/authenticode/cabfile.go

raw symbols SCIP
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 "crypto/hmac"
23 "errors"
24 "fmt"
25 "io"
26
27 "github.com/sassoftware/relic/v7/lib/binpatch"
28 "github.com/sassoftware/relic/v7/lib/cabfile"
29 "github.com/sassoftware/relic/v7/lib/certloader"
30 "github.com/sassoftware/relic/v7/lib/pkcs7"
31 "github.com/sassoftware/relic/v7/lib/pkcs9"
32 "github.com/sassoftware/relic/v7/lib/x509tools"
33 "github.com/sassoftware/relic/v7/signers/sigerrors"
34 )
35
36 type CabSignature struct {
37 pkcs9.TimestampedSignature
38 Indirect *SpcIndirectDataContentPe
39 OpusInfo *SpcSpOpusInfo
40 HashFunc crypto.Hash
41 PatchSet *binpatch.PatchSet
42 }
43
44 // Extract and verify the signature of a CAB file. Does not check X509 chains.
45 func VerifyCab(f io.ReaderAt, skipDigests bool) (*CabSignature, error) {
46 cab, err := cabfile.Parse(io.NewSectionReader(f, 0, 1<<63-1))
47 if err != nil {
48 return nil, err
49 }
50 if len(cab.Signature) == 0 {
51 return nil, sigerrors.NotSignedError{Type: "cabinet"}
52 }
53 psd, err := pkcs7.Unmarshal(cab.Signature)
54 if err != nil {
55 return nil, err
56 }
57 if !psd.Content.ContentInfo.ContentType.Equal(OidSpcIndirectDataContent) {
58 return nil, errors.New("not an authenticode signature")
59 }
60 pksig, err := psd.Content.Verify(nil, false)
61 if err != nil {
62 return nil, err
63 }
64 ts, err := pkcs9.VerifyOptionalTimestamp(pksig)
65 if err != nil {
66 return nil, err
67 }
68 indirect := new(SpcIndirectDataContentPe)
69 if err := psd.Content.ContentInfo.Unmarshal(indirect); err != nil {
70 return nil, err
71 }
72 hash, err := x509tools.PkixDigestToHashE(indirect.MessageDigest.DigestAlgorithm)
73 if err != nil {
74 return nil, err
75 }
76 opus, err := GetOpusInfo(pksig.SignerInfo)
77 if err != nil {
78 return nil, err
79 }
80 cabsig := &CabSignature{
81 TimestampedSignature: ts,
82 Indirect: indirect,
83 HashFunc: hash,
84 OpusInfo: opus,
85 }
86 if !skipDigests {
87 digest, err := cabfile.Digest(io.NewSectionReader(f, 0, 1<<63-1), hash)
88 if err != nil {
89 return nil, err
90 }
91 if !hmac.Equal(digest.Imprint, indirect.MessageDigest.Digest) {
92 return nil, fmt.Errorf("digest mismatch: %x != %x", digest.Imprint, indirect.MessageDigest.Digest)
93 }
94 // cab signatures seem to come with a "page hash" link in the same way
95 // as PE files can, using OidSpcCabPageHash as the type, but it's not
96 // clear what it's hashing.
97 }
98 return cabsig, nil
99 }
100
101 // Create the Authenticode structure for a CAB file signature using a previously-calculated digest (imprint).
102 func SignCabImprint(ctx context.Context, digest *cabfile.CabinetDigest, cert *certloader.Certificate, params *OpusParams) (*binpatch.PatchSet, *pkcs9.TimestampedSignature, error) {
103 indirect, err := makePeIndirect(digest.Imprint, digest.HashFunc, OidSpcCabImageData)
104 if err != nil {
105 return nil, nil, err
106 }
107 ts, err := signIndirect(ctx, indirect, digest.HashFunc, cert, params)
108 if err != nil {
109 return nil, nil, err
110 }
111 patch := digest.MakePatch(ts.Raw)
112 return patch, ts, nil
113 }