| 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/x509/pkix" |
| 23 | "encoding/asn1" |
| 24 | "encoding/binary" |
| 25 | "encoding/hex" |
| 26 | "errors" |
| 27 | "time" |
| 28 | "unicode/utf16" |
| 29 | |
| 30 | "github.com/google/uuid" |
| 31 | |
| 32 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 33 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 34 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 35 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 36 | ) |
| 37 | |
| 38 | type Catalog struct { |
| 39 | Version int |
| 40 | Hash crypto.Hash |
| 41 | Sha1Entries, Sha2Entries []CertTrustEntry |
| 42 | } |
| 43 | |
| 44 | func NewCatalog(hash crypto.Hash) *Catalog { |
| 45 | if hash == crypto.SHA1 { |
| 46 | return &Catalog{Version: 1, Hash: hash} |
| 47 | } |
| 48 | return &Catalog{Version: 2, Hash: hash} |
| 49 | } |
| 50 | |
| 51 | func (cat *Catalog) makeCatalog() CertTrustList { |
| 52 | memberOid := OidCatalogListMember |
| 53 | if cat.Version == 2 { |
| 54 | memberOid = OidCatalogListMemberV2 |
| 55 | } |
| 56 | listID := uuid.Must(uuid.NewRandom()) |
| 57 | return CertTrustList{ |
| 58 | SubjectUsage: []asn1.ObjectIdentifier{OidCatalogList}, |
| 59 | ListIdentifier: listID[:], |
| 60 | EffectiveDate: time.Now().UTC(), |
| 61 | SubjectAlgorithm: pkix.AlgorithmIdentifier{Algorithm: memberOid, Parameters: asn1.NullRawValue}, |
| 62 | Entries: append(cat.Sha2Entries, cat.Sha1Entries...), |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (cat *Catalog) Marshal() ([]byte, error) { |
| 67 | return asn1.Marshal(cat.makeCatalog()) |
| 68 | } |
| 69 | |
| 70 | func (cat *Catalog) Sign(ctx context.Context, cert *certloader.Certificate, params *OpusParams) (*pkcs9.TimestampedSignature, error) { |
| 71 | sig := pkcs7.NewBuilder(cert.Signer(), cert.Chain(), cat.Hash) |
| 72 | if err := sig.SetContent(OidCertTrustList, cat.makeCatalog()); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | if err := addOpusAttrs(sig, params); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | psd, err := sig.Sign() |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | return pkcs9.TimestampAndMarshal(ctx, psd, cert.Timestamper, true) |
| 83 | } |
| 84 | |
| 85 | func (cat *Catalog) Add(indirect SpcIndirectDataContentPe) error { |
| 86 | sha2 := !indirect.MessageDigest.DigestAlgorithm.Algorithm.Equal(x509tools.OidDigestSHA1) |
| 87 | if sha2 && cat.Version == 1 { |
| 88 | return errors.New("can't add SHA2 digest to v1 catalog") |
| 89 | } |
| 90 | indirectBytes, err := asn1.Marshal(indirect) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | indirectEntry := CertTrustValue{Attribute: OidSpcIndirectDataContent, Value: makeSet(indirectBytes)} |
| 95 | value := indirect.MessageDigest.Digest |
| 96 | if cat.Version == 1 { |
| 97 | memberInfo := CertTrustMemberInfoV1{ |
| 98 | ClassID: x509tools.ToBMPString(CryptSipCreateIndirectData), |
| 99 | Unknown1: 512, |
| 100 | } |
| 101 | memberInfoEnc, err := asn1.Marshal(memberInfo) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | catValue := CertTrustValue{Attribute: OidCatalogMemberInfo, Value: makeSet(memberInfoEnc)} |
| 106 | cat.Sha1Entries = append(cat.Sha1Entries, CertTrustEntry{ |
| 107 | Tag: tagV1(value), |
| 108 | Values: []CertTrustValue{indirectEntry, catValue}, |
| 109 | }) |
| 110 | } else { |
| 111 | // this supposed to always be empty? |
| 112 | memberInfoEnc := []byte{0x80, 0} |
| 113 | catValue := CertTrustValue{Attribute: OidCatalogMemberInfoV2, Value: makeSet(memberInfoEnc)} |
| 114 | if sha2 { |
| 115 | cat.Sha2Entries = append(cat.Sha2Entries, CertTrustEntry{ |
| 116 | Tag: value, |
| 117 | Values: []CertTrustValue{catValue, indirectEntry}, |
| 118 | }) |
| 119 | } else { |
| 120 | cat.Sha1Entries = append(cat.Sha1Entries, CertTrustEntry{ |
| 121 | Tag: value, |
| 122 | Values: []CertTrustValue{catValue}, |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | func tagV1(value []byte) []byte { |
| 130 | // The tag is a UTF-16-LE encoding of the hex of the imprint |
| 131 | runes := utf16.Encode([]rune(hex.EncodeToString(value))) |
| 132 | tag := make([]byte, 2*len(runes)) |
| 133 | for i, r := range runes { |
| 134 | binary.LittleEndian.PutUint16(tag[i*2:], r) |
| 135 | } |
| 136 | return tag |
| 137 | } |
| 138 | |
| 139 | func makeSet(contents []byte) asn1.RawValue { |
| 140 | return asn1.RawValue{Tag: asn1.TagSet, IsCompound: true, Bytes: contents} |
| 141 | } |