| 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 | import ( |
| 20 | "crypto" |
| 21 | "crypto/x509" |
| 22 | "encoding/base64" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | |
| 26 | "github.com/beevik/etree" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/pkcs7" |
| 29 | "github.com/sassoftware/relic/v7/lib/pkcs9" |
| 30 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 31 | "github.com/sassoftware/relic/v7/lib/xmldsig" |
| 32 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 33 | ) |
| 34 | |
| 35 | type ManifestSignature struct { |
| 36 | Signature *pkcs9.TimestampedSignature |
| 37 | Hash crypto.Hash |
| 38 | AssemblyName string |
| 39 | AssemblyVersion string |
| 40 | PublicKeyToken string |
| 41 | } |
| 42 | |
| 43 | // Extract and verify signature on an application manifest. Does not verify X509 chains. |
| 44 | func Verify(manifest []byte) (*ManifestSignature, error) { |
| 45 | doc := etree.NewDocument() |
| 46 | if err := doc.ReadFromString(string(manifest)); err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | root := doc.Root() |
| 50 | primary, err := xmldsig.Verify(root, "Signature", nil) |
| 51 | if err != nil { |
| 52 | if _, ok := err.(sigerrors.NotSignedError); ok { |
| 53 | return nil, err |
| 54 | } |
| 55 | return nil, fmt.Errorf("invalid primary signature: %w", err) |
| 56 | } |
| 57 | license := root.FindElement("Signature/KeyInfo/msrel:RelData/r:license") |
| 58 | if license == nil { |
| 59 | return nil, sigerrors.NotSignedError{Type: "application manifest"} |
| 60 | } |
| 61 | secondary, err := xmldsig.Verify(license, "issuer/Signature", nil) |
| 62 | if err != nil { |
| 63 | if _, ok := err.(sigerrors.NotSignedError); ok { |
| 64 | return nil, err |
| 65 | } |
| 66 | return nil, fmt.Errorf("invalid authenticode signature: %w", err) |
| 67 | } |
| 68 | if !x509tools.SameKey(primary.PublicKey, secondary.PublicKey) { |
| 69 | return nil, errors.New("signatures were made with different keys") |
| 70 | } |
| 71 | asi := root.SelectElement("assemblyIdentity") |
| 72 | if asi == nil { |
| 73 | return nil, errors.New("missing assemblyIdentity") |
| 74 | } |
| 75 | token, err := PublicKeyToken(primary.PublicKey) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | if token2 := asi.SelectAttrValue("publicKeyToken", ""); token2 != token { |
| 80 | return nil, fmt.Errorf("publicKeyToken mismatch: expected %s, got %s", token, token2) |
| 81 | } |
| 82 | sig := pkcs7.Signature{Intermediates: secondary.Certificates, Certificate: secondary.Leaf()} |
| 83 | if sig.Certificate == nil { |
| 84 | return nil, errors.New("leaf x509 certificate not found") |
| 85 | } |
| 86 | ts := &pkcs9.TimestampedSignature{Signature: sig} |
| 87 | if tse := license.FindElement("r:issuer/Signature/Object/as:Timestamp"); tse != nil { |
| 88 | blob, err := base64.StdEncoding.DecodeString(tse.Text()) |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("invalid timestamp: %w", err) |
| 91 | } |
| 92 | timestamp, err := pkcs7.Unmarshal(blob) |
| 93 | if err != nil { |
| 94 | return nil, fmt.Errorf("invalid timestamp: %w", err) |
| 95 | } |
| 96 | |
| 97 | cs, err := VerifyTimestamp(timestamp, secondary.EncryptedDigest, secondary.Certificates) |
| 98 | if err != nil { |
| 99 | return nil, fmt.Errorf("invalid timestamp: %w", err) |
| 100 | } |
| 101 | ts.CounterSignature = cs |
| 102 | } |
| 103 | return &ManifestSignature{ |
| 104 | Signature: ts, |
| 105 | AssemblyName: asi.SelectAttrValue("name", ""), |
| 106 | AssemblyVersion: asi.SelectAttrValue("version", ""), |
| 107 | Hash: primary.Hash, |
| 108 | PublicKeyToken: token, |
| 109 | }, nil |
| 110 | } |
| 111 | |
| 112 | func VerifyTimestamp(timestamp *pkcs7.ContentInfoSignedData, encryptedDigest []byte, extraCerts []*x509.Certificate) (*pkcs9.CounterSignature, error) { |
| 113 | var cs *pkcs9.CounterSignature |
| 114 | var err error |
| 115 | |
| 116 | if timestamp.Content.ContentInfo.ContentType.Equal(pkcs9.OidTSTInfo) { |
| 117 | // pkcs9 timestamp |
| 118 | cs, err = pkcs9.Verify(timestamp, encryptedDigest, extraCerts) |
| 119 | } else { |
| 120 | // legacy timestamp |
| 121 | cs, err = pkcs9.VerifyMicrosoftToken(timestamp, encryptedDigest) |
| 122 | } |
| 123 | |
| 124 | return cs, err |
| 125 | } |