| 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 | "crypto/x509/pkix" |
| 21 | "encoding/asn1" |
| 22 | "encoding/binary" |
| 23 | "time" |
| 24 | "unicode/utf16" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | OidSpcIndirectDataContent = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 4} |
| 29 | OidSpcStatementType = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 11} |
| 30 | OidSpcSpOpusInfo = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 12} |
| 31 | OidSpcPeImageData = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 15} |
| 32 | OidSpcIndividualPurpose = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 21} |
| 33 | OidSpcCabImageData = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 25} |
| 34 | OidSpcSipInfo = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 30} |
| 35 | OidSpcPageHashV1 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 3, 1} |
| 36 | OidSpcPageHashV2 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 3, 2} |
| 37 | OidSpcCabPageHash = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 5, 1} |
| 38 | OidCertTrustList = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 10, 1} |
| 39 | OidCatalogList = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 1, 1} |
| 40 | OidCatalogListMember = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 1, 2} |
| 41 | OidCatalogListMemberV2 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 1, 3} |
| 42 | OidCatalogNameValue = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 2, 1} |
| 43 | OidCatalogMemberInfo = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 2, 2} |
| 44 | OidCatalogMemberInfoV2 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 12, 2, 3} |
| 45 | |
| 46 | SpcUUIDPageHashes = []byte{0xa6, 0xb5, 0x86, 0xd5, 0xb4, 0xa1, 0x24, 0x66, 0xae, 0x05, 0xa2, 0x17, 0xda, 0x8e, 0x60, 0xd6} |
| 47 | |
| 48 | // SIP or Subject Interface Package is an internal Microsoft API for |
| 49 | // transforming arbitrary files into a digestible stream. These ClassIDs |
| 50 | // are found in the indirect data section and identify the type of processor needed to validate the signature. |
| 51 | // SIP related DLLs are registered at |
| 52 | // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\OID\EncodingType 0\CryptSIPDllCreateIndirectData |
| 53 | // although these particular ClassIDs do not seem to appear there. |
| 54 | // Relevant DLLs include: WINTRUST.DLL, MSISIP.DLL, pwrshsip.dll |
| 55 | SpcUUIDSipInfoMsi = []byte{0xf1, 0x10, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} |
| 56 | SpcUUIDSipInfoPs = []byte{0x1f, 0xcc, 0x3b, 0x60, 0x59, 0x4b, 0x08, 0x4e, 0xb7, 0x24, 0xd2, 0xc6, 0x29, 0x7e, 0xf3, 0x51} |
| 57 | |
| 58 | // This one is used in V1 security catalogs |
| 59 | CryptSipCreateIndirectData = "{C689AAB8-8E78-11D0-8C47-00C04FC295EE}" |
| 60 | |
| 61 | // Filenames for MSI streams holding signature data |
| 62 | msiDigitalSignature = "\x05DigitalSignature" |
| 63 | msiDigitalSignatureEx = "\x05MsiDigitalSignatureEx" |
| 64 | ) |
| 65 | |
| 66 | type SpcIndirectDataContentPe struct { |
| 67 | Data SpcAttributePeImageData |
| 68 | MessageDigest DigestInfo |
| 69 | } |
| 70 | |
| 71 | type SpcAttributePeImageData struct { |
| 72 | Type asn1.ObjectIdentifier |
| 73 | Value SpcPeImageData `asn1:"optional"` |
| 74 | } |
| 75 | |
| 76 | type DigestInfo struct { |
| 77 | DigestAlgorithm pkix.AlgorithmIdentifier |
| 78 | Digest []byte |
| 79 | } |
| 80 | |
| 81 | type SpcPeImageData struct { |
| 82 | Flags asn1.BitString |
| 83 | File SpcLink `asn1:"tag:0"` |
| 84 | } |
| 85 | |
| 86 | type SpcLink struct { |
| 87 | URL string `asn1:"optional,tag:0,ia5"` |
| 88 | Moniker SpcSerializedObject `asn1:"optional,tag:1"` |
| 89 | File SpcString `asn1:"optional,tag:2"` |
| 90 | } |
| 91 | |
| 92 | type SpcString struct { |
| 93 | Unicode []byte `asn1:"optional,tag:0"` // BMPString |
| 94 | ASCII string `asn1:"optional,tag:1,ia5"` |
| 95 | } |
| 96 | |
| 97 | func NewSpcString(value string) SpcString { |
| 98 | runes := utf16.Encode([]rune(value)) |
| 99 | raw := make([]byte, 2*len(runes)) |
| 100 | for i, r := range runes { |
| 101 | binary.BigEndian.PutUint16(raw[i*2:], r) |
| 102 | } |
| 103 | return SpcString{Unicode: raw} |
| 104 | } |
| 105 | |
| 106 | func (s SpcString) String() string { |
| 107 | if len(s.Unicode) != 0 && len(s.Unicode)%2 == 0 { |
| 108 | words := make([]uint16, len(s.Unicode)/2) |
| 109 | for i := range words { |
| 110 | words[i] = binary.BigEndian.Uint16(s.Unicode[i*2:]) |
| 111 | } |
| 112 | runes := utf16.Decode(words) |
| 113 | return string(runes) |
| 114 | } |
| 115 | return s.ASCII |
| 116 | } |
| 117 | |
| 118 | type SpcSerializedObject struct { |
| 119 | ClassID []byte |
| 120 | SerializedData []byte |
| 121 | } |
| 122 | |
| 123 | type SpcAttributePageHashes struct { |
| 124 | Type asn1.ObjectIdentifier |
| 125 | Hashes [][]byte `asn1:"set"` |
| 126 | } |
| 127 | |
| 128 | type SpcSpOpusInfo struct { |
| 129 | ProgramName SpcString `asn1:"optional,tag:0"` |
| 130 | MoreInfo SpcLink `asn1:"optional,tag:1"` |
| 131 | } |
| 132 | |
| 133 | type SpcSpStatementType struct { |
| 134 | Type asn1.ObjectIdentifier |
| 135 | } |
| 136 | |
| 137 | type SpcIndirectDataContentMsi struct { |
| 138 | Data SpcAttributeMsiImageData |
| 139 | MessageDigest DigestInfo |
| 140 | } |
| 141 | |
| 142 | type SpcAttributeMsiImageData struct { |
| 143 | Type asn1.ObjectIdentifier |
| 144 | Value SpcSipInfo `asn1:"optional"` |
| 145 | } |
| 146 | |
| 147 | type SpcSipInfo struct { |
| 148 | A int |
| 149 | UUID []byte |
| 150 | B, C, D, E, F int |
| 151 | } |
| 152 | |
| 153 | var msiSipInfo = SpcSipInfo{1, SpcUUIDSipInfoMsi, 0, 0, 0, 0, 0} |
| 154 | var psSipInfo = SpcSipInfo{65536, SpcUUIDSipInfoPs, 0, 0, 0, 0, 0} |
| 155 | |
| 156 | type CertTrustList struct { |
| 157 | SubjectUsage []asn1.ObjectIdentifier |
| 158 | ListIdentifier []byte |
| 159 | EffectiveDate time.Time |
| 160 | SubjectAlgorithm pkix.AlgorithmIdentifier |
| 161 | Entries []CertTrustEntry |
| 162 | Attributes *CertTrustAttributes `asn1:"optional,explicit,tag:0"` |
| 163 | } |
| 164 | |
| 165 | type CertTrustEntry struct { |
| 166 | Tag []byte |
| 167 | Values []CertTrustValue `asn1:"set"` |
| 168 | } |
| 169 | |
| 170 | type CertTrustValue struct { |
| 171 | Attribute asn1.ObjectIdentifier |
| 172 | Value asn1.RawValue |
| 173 | } |
| 174 | |
| 175 | type CertTrustMemberInfoV1 struct { |
| 176 | ClassID asn1.RawValue |
| 177 | Unknown1 int |
| 178 | } |
| 179 | |
| 180 | type CertTrustAttributes struct { |
| 181 | // TODO |
| 182 | } |