| 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 appmanifest |
| 18 | |
| 19 | // Sign Microsoft ClickOnce application manifests and deployment manifests. |
| 20 | // These take the form of an XML file using XML DSIG signatures and, unlike all |
| 21 | // other Microsoft signatures, does not use an Authenticode PKCS#7 structure. |
| 22 | |
| 23 | import ( |
| 24 | "fmt" |
| 25 | "io" |
| 26 | "io/ioutil" |
| 27 | |
| 28 | "github.com/rs/zerolog" |
| 29 | "github.com/sassoftware/relic/v7/lib/appmanifest" |
| 30 | "github.com/sassoftware/relic/v7/lib/audit" |
| 31 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 32 | "github.com/sassoftware/relic/v7/lib/magic" |
| 33 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 34 | "github.com/sassoftware/relic/v7/signers" |
| 35 | ) |
| 36 | |
| 37 | var AppSigner = &signers.Signer{ |
| 38 | Name: "appmanifest", |
| 39 | Magic: magic.FileTypeAppManifest, |
| 40 | CertTypes: signers.CertTypeX509, |
| 41 | FormatLog: formatLog, |
| 42 | Sign: sign, |
| 43 | VerifyStream: verify, |
| 44 | } |
| 45 | |
| 46 | func init() { |
| 47 | AppSigner.Flags().Bool("rfc3161-timestamp", true, "(APPMANIFEST) Timestamp with RFC3161 server") |
| 48 | signers.Register(AppSigner) |
| 49 | } |
| 50 | |
| 51 | func formatLog(info *audit.Info) *zerolog.Event { |
| 52 | return info.AttrsForLog("assembly.") |
| 53 | } |
| 54 | |
| 55 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 56 | blob, err := ioutil.ReadAll(r) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | signed, err := appmanifest.Sign(blob, cert, opts.Hash) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | if cert.Timestamper != nil { |
| 65 | tsreq := &pkcs9.Request{ |
| 66 | EncryptedDigest: signed.EncryptedDigest, |
| 67 | Legacy: !opts.Flags.GetBool("rfc3161-timestamp"), |
| 68 | Hash: opts.Hash, |
| 69 | } |
| 70 | |
| 71 | token, err := cert.Timestamper.Timestamp(opts.Context(), tsreq) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | if err := signed.AddTimestamp(token); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | } |
| 79 | opts.Audit.SetMimeType("application/xml") |
| 80 | opts.Audit.Attributes["assembly.name"] = signed.AssemblyName |
| 81 | opts.Audit.Attributes["assembly.version"] = signed.AssemblyVersion |
| 82 | opts.Audit.Attributes["assembly.publicKeyToken"] = signed.PublicKeyToken |
| 83 | opts.Audit.SetCounterSignature(signed.Signature.CounterSignature) |
| 84 | return signed.Signed, nil |
| 85 | } |
| 86 | |
| 87 | func verify(r io.Reader, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 88 | blob, err := ioutil.ReadAll(r) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | sig, err := appmanifest.Verify(blob) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | return []*signers.Signature{&signers.Signature{ |
| 97 | Package: fmt.Sprintf("%s %s", sig.AssemblyName, sig.AssemblyVersion), |
| 98 | Hash: sig.Hash, |
| 99 | X509Signature: sig.Signature, |
| 100 | }}, nil |
| 101 | } |