| 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 signjar |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "encoding/base64" |
| 22 | "errors" |
| 23 | "fmt" |
| 24 | "io" |
| 25 | "net/http" |
| 26 | "strings" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | "github.com/sassoftware/relic/v7/lib/zipslicer" |
| 30 | ) |
| 31 | |
| 32 | // found in the "extra" field of JAR files, not strictly required but it makes |
| 33 | // `file` output actually say JAR |
| 34 | var jarMagic = []byte{0xfe, 0xca, 0, 0} |
| 35 | |
| 36 | type JarDigest struct { |
| 37 | Digests map[string]string |
| 38 | Manifest []byte |
| 39 | Hash crypto.Hash |
| 40 | inz *zipslicer.Directory |
| 41 | } |
| 42 | |
| 43 | func DigestJarStream(r io.Reader, hash crypto.Hash) (*JarDigest, error) { |
| 44 | inz, err := zipslicer.ReadZipTar(r) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return updateManifest(inz, hash) |
| 49 | } |
| 50 | |
| 51 | // Digest all of the files in the JAR |
| 52 | func digestFiles(jar *zipslicer.Directory, hash crypto.Hash) (*JarDigest, error) { |
| 53 | jd := &JarDigest{ |
| 54 | Hash: hash, |
| 55 | Digests: make(map[string]string), |
| 56 | inz: jar, |
| 57 | } |
| 58 | for _, f := range jar.File { |
| 59 | if f.Name == manifestName { |
| 60 | r, err := f.Open() |
| 61 | if err != nil { |
| 62 | return nil, fmt.Errorf("failed to read JAR manifest: %w", err) |
| 63 | } |
| 64 | jd.Manifest, err = io.ReadAll(r) |
| 65 | if err != nil { |
| 66 | return nil, fmt.Errorf("failed to read JAR manifest: %w", err) |
| 67 | } |
| 68 | } else if strings.HasSuffix(f.Name, "/") || !keepFile(f.Name) { |
| 69 | // not hashing |
| 70 | } else { |
| 71 | r, err := f.Open() |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | d := hash.New() |
| 76 | if _, err := io.Copy(d, r); err != nil { |
| 77 | return nil, fmt.Errorf("failed to digest JAR file %s: %w", f.Name, err) |
| 78 | } |
| 79 | if err := r.Close(); err != nil { |
| 80 | return nil, fmt.Errorf("failed to digest JAR file %s: %w", f.Name, err) |
| 81 | } |
| 82 | jd.Digests[f.Name] = base64.StdEncoding.EncodeToString(d.Sum(nil)) |
| 83 | } |
| 84 | // Ensure we get a copy of the zip metadata even if the file isn't |
| 85 | // digested, because if we're reading from a stream we can't go back |
| 86 | // and get it later. |
| 87 | if _, err := f.GetDataDescriptor(); err != nil { |
| 88 | return nil, fmt.Errorf("failed to read JAR manifest: %w", err) |
| 89 | } |
| 90 | } |
| 91 | return jd, nil |
| 92 | } |
| 93 | |
| 94 | // Check JAR contents against its manifest and adds digests if necessary |
| 95 | func updateManifest(jar *zipslicer.Directory, hash crypto.Hash) (*JarDigest, error) { |
| 96 | jd, err := digestFiles(jar, hash) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } else if jd.Manifest == nil { |
| 100 | return nil, errors.New("JAR did not contain a manifest") |
| 101 | } |
| 102 | files, malformed, err := parseManifest(jd.Manifest) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | hashName := x509tools.HashNames[hash] |
| 108 | if hashName == "" { |
| 109 | return nil, errors.New("unsupported hash type") |
| 110 | } |
| 111 | hashName += "-Digest" |
| 112 | changed := false |
| 113 | for name, calculated := range jd.Digests { |
| 114 | // if the manifest has a matching digest, check it. otherwise add to the manifest. |
| 115 | attrs := files.Files[name] |
| 116 | if attrs == nil { |
| 117 | // file is not mentioned in the manifest at all |
| 118 | files.Files[name] = http.Header{ |
| 119 | "Name": []string{name}, |
| 120 | hashName: []string{calculated}, |
| 121 | } |
| 122 | files.Order = append(files.Order, name) |
| 123 | changed = true |
| 124 | } else if attrs.Get("Magic") != "" { |
| 125 | // magic means a special digester is required. hopefully it's already been digested. |
| 126 | } else if existing := attrs.Get(hashName); existing != "" { |
| 127 | // manifest has a digest already, check it |
| 128 | if existing != calculated { |
| 129 | return nil, fmt.Errorf("%s mismatch for JAR file %s: manifest %s != calculated %s", hashName, name, existing, calculated) |
| 130 | } |
| 131 | } else { |
| 132 | // file in manifest but no matching digest |
| 133 | attrs.Set(hashName, calculated) |
| 134 | changed = true |
| 135 | } |
| 136 | } |
| 137 | // Add empty digests for any dir entries with metadata |
| 138 | for name, attrs := range files.Files { |
| 139 | if strings.HasSuffix(name, "/") && attrs.Get(hashName) == "" { |
| 140 | d := hash.New() |
| 141 | calculated := base64.StdEncoding.EncodeToString(d.Sum(nil)) |
| 142 | attrs.Set(hashName, calculated) |
| 143 | changed = true |
| 144 | } |
| 145 | } |
| 146 | if changed || malformed { |
| 147 | jd.Manifest = files.Dump() |
| 148 | } |
| 149 | return jd, nil |
| 150 | } |