-
Notifications
You must be signed in to change notification settings - Fork 39
/
pesign.go
129 lines (121 loc) · 3.46 KB
/
pesign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package authenticode
import (
"bytes"
"context"
"crypto"
"debug/pe"
"encoding/asn1"
"encoding/binary"
"errors"
"github.com/sassoftware/relic/v8/lib/binpatch"
"github.com/sassoftware/relic/v8/lib/certloader"
"github.com/sassoftware/relic/v8/lib/pkcs9"
)
// Sign the digest and return an Authenticode structure
func (pd *PEDigest) Sign(ctx context.Context, cert *certloader.Certificate, params *OpusParams) (*binpatch.PatchSet, *pkcs9.TimestampedSignature, error) {
indirect, err := pd.GetIndirect()
if err != nil {
return nil, nil, err
}
ts, err := signIndirect(ctx, indirect, pd.Hash, cert, params)
if err != nil {
return nil, nil, err
}
patch, err := pd.MakePatch(ts.Raw)
if err != nil {
return nil, nil, err
}
return patch, ts, nil
}
func (pd *PEDigest) GetIndirect() (indirect SpcIndirectDataContentPe, err error) {
indirect, err = makePeIndirect(pd.Imprint, pd.Hash, OidSpcPeImageData)
if err != nil {
return
}
if len(pd.PageHashes) > 0 {
if err2 := pd.imprintPageHashes(&indirect); err2 != nil {
err = err2
return
}
}
return
}
func (pd *PEDigest) imprintPageHashes(indirect *SpcIndirectDataContentPe) error {
var attr SpcAttributePageHashes
switch pd.Hash {
case crypto.SHA1:
attr.Type = OidSpcPageHashV1
case crypto.SHA256:
attr.Type = OidSpcPageHashV2
default:
return errors.New("unsupported page hash type")
}
attr.Hashes = make([][]byte, 1)
attr.Hashes[0] = pd.PageHashes
blob, err := asn1.Marshal(attr)
if err != nil {
return err
}
serdata, err := asn1.Marshal(makeSet(blob))
if err != nil {
return err
}
indirect.Data.Value.File = SpcLink{}
indirect.Data.Value.File.Moniker.ClassID = SpcUUIDPageHashes
indirect.Data.Value.File.Moniker.SerializedData = serdata
return nil
}
// Create a patchset that will add or replace the signature from a previously
// digested image with a new one
func (pd *PEDigest) MakePatch(sig []byte) (*binpatch.PatchSet, error) {
// pack new cert table
padded := (len(sig) + 7) / 8 * 8
info := certInfo{
Length: uint32(8 + padded),
Revision: 0x0200,
CertificateType: 0x0002,
}
var buf bytes.Buffer
pad2 := pd.CertStart - pd.OrigSize
if pad2 != 0 {
buf.Write(make([]byte, pad2))
}
_ = binary.Write(&buf, binary.LittleEndian, info)
_, _ = buf.Write(sig)
_, _ = buf.Write(make([]byte, padded-len(sig)))
// pack data directory
certTbl := buf.Bytes()
var dd pe.DataDirectory
if pd.CertStart >= (1 << 32) {
return nil, errors.New("PE file is too big")
}
dd.VirtualAddress = uint32(pd.CertStart)
dd.Size = uint32(len(certTbl)) - uint32(pad2)
var buf2 bytes.Buffer
_ = binary.Write(&buf2, binary.LittleEndian, dd)
// make patch
patch := binpatch.New()
patch.Add(pd.markers.posDDCert, 8, buf2.Bytes())
patch.Add(pd.OrigSize, int64(pd.markers.certSize), certTbl)
return patch, nil
}
type certInfo struct {
Length uint32
Revision uint16
CertificateType uint16
}