| 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 | "encoding/asn1" |
| 23 | "errors" |
| 24 | |
| 25 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 26 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 27 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | ) |
| 30 | |
| 31 | type OpusParams struct { |
| 32 | Description string |
| 33 | URL string |
| 34 | } |
| 35 | |
| 36 | func makePeIndirect(imprint []byte, hash crypto.Hash, oid asn1.ObjectIdentifier) (indirect SpcIndirectDataContentPe, err error) { |
| 37 | alg, ok := x509tools.PkixDigestAlgorithm(hash) |
| 38 | if !ok { |
| 39 | err = errors.New("unsupported digest algorithm") |
| 40 | return |
| 41 | } |
| 42 | indirect.Data.Type = oid |
| 43 | indirect.MessageDigest.Digest = imprint |
| 44 | indirect.MessageDigest.DigestAlgorithm = alg |
| 45 | indirect.Data.Value.File.File = NewSpcString("") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | func signIndirect(ctx context.Context, indirect interface{}, hash crypto.Hash, cert *certloader.Certificate, params *OpusParams) (*pkcs9.TimestampedSignature, error) { |
| 50 | sig := pkcs7.NewBuilder(cert.Signer(), cert.Chain(), hash) |
| 51 | if err := sig.SetContent(OidSpcIndirectDataContent, indirect); err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | if err := addOpusAttrs(sig, params); err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | psd, err := sig.Sign() |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | return pkcs9.TimestampAndMarshal(ctx, psd, cert.Timestamper, true) |
| 62 | } |
| 63 | |
| 64 | func addOpusAttrs(sig *pkcs7.SignatureBuilder, params *OpusParams) error { |
| 65 | if err := sig.AddAuthenticatedAttribute(OidSpcStatementType, SpcSpStatementType{Type: OidSpcIndividualPurpose}); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | var info SpcSpOpusInfo |
| 69 | if params != nil { |
| 70 | if params.Description != "" { |
| 71 | info.ProgramName = NewSpcString(params.Description) |
| 72 | } |
| 73 | if params.URL != "" { |
| 74 | info.MoreInfo.URL = params.URL |
| 75 | } |
| 76 | } |
| 77 | if err := sig.AddAuthenticatedAttribute(OidSpcSpOpusInfo, info); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func SignSip(ctx context.Context, imprint []byte, hash crypto.Hash, sipInfo SpcSipInfo, cert *certloader.Certificate, params *OpusParams) (*pkcs9.TimestampedSignature, error) { |
| 84 | alg, ok := x509tools.PkixDigestAlgorithm(hash) |
| 85 | if !ok { |
| 86 | return nil, errors.New("unsupported digest algorithm") |
| 87 | } |
| 88 | var indirect SpcIndirectDataContentMsi |
| 89 | indirect.Data.Type = OidSpcSipInfo |
| 90 | indirect.Data.Value = sipInfo |
| 91 | indirect.MessageDigest.Digest = imprint |
| 92 | indirect.MessageDigest.DigestAlgorithm = alg |
| 93 | return signIndirect(ctx, indirect, hash, cert, params) |
| 94 | } |