| 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 signjarscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/ |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 21 | "cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/" |
| 22 | "encoding/base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/" |
| 23 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 24 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 25 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 26 | "sortscip-go gomod github.com/golang/go/src go1.18 sort/" |
| 27 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 28 | |
| 29 | "github.com/sassoftware/relic/v7/configscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/config`/" |
| 30 | "github.com/sassoftware/relic/v7/lib/x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/" |
| 31 | ) |
| 32 | |
| 33 | // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#JAR_Manifest |
| 34 | |
| 35 | const ( |
| 36 | metaInfscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/metaInf. = "META-INF/" |
| 37 | manifestNamescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/manifestName. = metaInfscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/metaInf. + "MANIFEST.MF" |
| 38 | ) |
| 39 | |
| 40 | var ErrManifestLineEndingsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/ErrManifestLineEndings. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("manifest has incorrect line ending sequence") |
| 41 | |
| 42 | type FilesMapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap# struct { |
| 43 | Mainscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Main. httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header# |
| 44 | Orderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Order. []string |
| 45 | Filesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Files. map[string]httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header# |
| 46 | } |
| 47 | |
| 48 | func ParseManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/ParseManifest().(manifest []byte) (files *FilesMapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#, err error) { |
| 49 | files, malformed, err := parseManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/parseManifest().(manifest) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } else if malformed { |
| 53 | return nil, ErrManifestLineEndingsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/ErrManifestLineEndings. |
| 54 | } |
| 55 | return files, nil |
| 56 | } |
| 57 | |
| 58 | func parseManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/parseManifest().(manifest []byte) (files *FilesMapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#, malformed bool, err error) { |
| 59 | sections, malformed := splitManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/splitManifest().(manifest) |
| 60 | if len(sections) == 0 { |
| 61 | return nil, false, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("manifest has no sections") |
| 62 | } |
| 63 | files = &FilesMapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#{ |
| 64 | Orderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Order.: make([]string, 0, len(sections)-1), |
| 65 | Filesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Files.: make(map[string]httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#, len(sections)-1), |
| 66 | } |
| 67 | for i, section := range sections { |
| 68 | if i > 0 && len(section) == 0 { |
| 69 | continue |
| 70 | } |
| 71 | hdr, err := parseSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/parseSection().(section) |
| 72 | if err != nil { |
| 73 | return nil, false, err |
| 74 | } |
| 75 | if i == 0 { |
| 76 | files.Mainscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Main. = hdr |
| 77 | } else { |
| 78 | name := hdr.Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().("Name") |
| 79 | if name == "" { |
| 80 | return nil, false, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("manifest has section with no \"Name\" attribute") |
| 81 | } |
| 82 | files.Orderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Order. = append(files.Orderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Order., name) |
| 83 | files.Filesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Files.[name] = hdr |
| 84 | } |
| 85 | } |
| 86 | return files, malformed, nil |
| 87 | } |
| 88 | |
| 89 | func (m *FilesMapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#) Dumpscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Dump().() []byte { |
| 90 | var out bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer# |
| 91 | writeSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeSection().(&out, m.Mainscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Main., "Manifest-Version") |
| 92 | for _, name := range m.Orderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Order. { |
| 93 | section := m.Filesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/FilesMap#Files.[name] |
| 94 | if section != nil { |
| 95 | writeSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeSection().(&out, section, "Name") |
| 96 | } |
| 97 | } |
| 98 | return out.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().() |
| 99 | } |
| 100 | |
| 101 | func splitManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/splitManifest().(manifest []byte) ([][]byte, bool) { |
| 102 | var malformed bool |
| 103 | sections := make([][]byte, 0) |
| 104 | for len(manifest) != 0 { |
| 105 | i1 := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Indexscip-go gomod github.com/golang/go/src go1.18 bytes/Index().(manifest, []byte("\r\n\r\n")) |
| 106 | i2 := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Indexscip-go gomod github.com/golang/go/src go1.18 bytes/Index().(manifest, []byte("\n\n")) |
| 107 | var idx int |
| 108 | switch { |
| 109 | case i1 >= 0: |
| 110 | idx = i1 + 4 |
| 111 | case i2 >= 0: |
| 112 | idx = i2 + 2 |
| 113 | default: |
| 114 | // If there is not a proper 2x line ending, |
| 115 | // then it's technically not valid but we can sign it anyway |
| 116 | // as long as it gets rewritten with correct endings. |
| 117 | idx = len(manifest) |
| 118 | malformed = true |
| 119 | } |
| 120 | section := manifest[:idx] |
| 121 | manifest = manifest[idx:] |
| 122 | if len(bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 bytes/TrimSpace().(section)) == 0 { |
| 123 | // Excessive line endings have created an empty section |
| 124 | malformed = true |
| 125 | continue |
| 126 | } |
| 127 | sections = append(sections, section) |
| 128 | } |
| 129 | return sections, malformed |
| 130 | } |
| 131 | |
| 132 | func parseSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/parseSection().(section []byte) (httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#, error) { |
| 133 | section = bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.ReplaceAllscip-go gomod github.com/golang/go/src go1.18 bytes/ReplaceAll().(section, []byte("\r\n"), []byte{'\n'}) |
| 134 | section = bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.ReplaceAllscip-go gomod github.com/golang/go/src go1.18 bytes/ReplaceAll().(section, []byte("\n "), []byte{}) |
| 135 | keys := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Splitscip-go gomod github.com/golang/go/src go1.18 bytes/Split().(section, []byte{'\n'}) |
| 136 | hdr := make(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#) |
| 137 | for _, line := range keys { |
| 138 | if len(line) == 0 { |
| 139 | continue |
| 140 | } |
| 141 | idx := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.IndexRunescip-go gomod github.com/golang/go/src go1.18 bytes/IndexRune().(line, ':') |
| 142 | if idx < 0 { |
| 143 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("jar manifest is malformed") |
| 144 | } |
| 145 | key := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(string(line[:idx])) |
| 146 | value := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(string(line[idx+1:])) |
| 147 | hdr.Setscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Set().(key, value) |
| 148 | } |
| 149 | return hdr, nil |
| 150 | } |
| 151 | |
| 152 | func hashSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/hashSection().(hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, section []byte) string { |
| 153 | d := hash.Newscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#New().() |
| 154 | d.Writescip-go gomod github.com/golang/go/src go1.18 io/Writer#Write().(section) |
| 155 | return base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(d.Sumscip-go gomod github.com/golang/go/src go1.18 hash/Hash#Sum().(nil)) |
| 156 | } |
| 157 | |
| 158 | // Transform a MANIFEST.MF into a *.SF by digesting each section with the |
| 159 | // specified hash |
| 160 | func DigestManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/DigestManifest().(manifest []byte, hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, sectionsOnly, apkV2 bool) ([]byte, error) { |
| 161 | sections, malformed := splitManifestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/splitManifest().(manifest) |
| 162 | if malformed { |
| 163 | return nil, ErrManifestLineEndingsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/ErrManifestLineEndings. |
| 164 | } |
| 165 | hashName := x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/.HashNamesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/HashNames.[hash] |
| 166 | if hashName == "" { |
| 167 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("unsupported hash type") |
| 168 | } |
| 169 | var output bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer# |
| 170 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, "Signature-Version", "1.0") |
| 171 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, hashName+"-Digest-Manifest-Main-Attributes", hashSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/hashSection().(hash, sections[0])) |
| 172 | if !sectionsOnly { |
| 173 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, hashName+"-Digest-Manifest", hashSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/hashSection().(hash, manifest)) |
| 174 | } |
| 175 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, "Created-By", fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%s (%s)", configscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/config`/.UserAgentscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/config`/UserAgent., configscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/config`/.Authorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/config`/Author.)) |
| 176 | if apkV2 { |
| 177 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, "X-Android-APK-Signed", "2") |
| 178 | } |
| 179 | output.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().("\r\n") |
| 180 | for _, section := range sections[1:] { |
| 181 | hdr, err := parseSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/parseSection().(section) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | name := hdr.Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().("Name") |
| 186 | if name == "" { |
| 187 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("File section was missing Name attribute") |
| 188 | } |
| 189 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, "Name", name) |
| 190 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(&output, hashName+"-Digest", hashSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/hashSection().(hash, section)) |
| 191 | output.WriteStringscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#WriteString().("\r\n") |
| 192 | } |
| 193 | return output.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().(), nil |
| 194 | } |
| 195 | |
| 196 | const maxLineLengthscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/maxLineLength. = 70 |
| 197 | |
| 198 | // Write a key-value pair, wrapping long lines as necessary |
| 199 | func writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(out *bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#, key, value string) { |
| 200 | line := []byte(fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%s: %s", key, value)) |
| 201 | for i := 0; i < len(line); { |
| 202 | goal := maxLineLengthscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/maxLineLength. |
| 203 | if i != 0 { |
| 204 | out.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().([]byte{' '}) |
| 205 | goal-- |
| 206 | } |
| 207 | j := i + goal |
| 208 | if j > len(line) { |
| 209 | j = len(line) |
| 210 | } |
| 211 | out.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(line[i:j]) |
| 212 | out.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().([]byte("\r\n")) |
| 213 | i = j |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func writeSectionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeSection().(out *bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#, hdr httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#, first string) { |
| 218 | value := hdr.Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().(first) |
| 219 | if value != "" { |
| 220 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(out, first, value) |
| 221 | } |
| 222 | keys := make([]string, 0, len(hdr)) |
| 223 | for key := range hdr { |
| 224 | if key == first { |
| 225 | continue |
| 226 | } |
| 227 | keys = append(keys, key) |
| 228 | } |
| 229 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(keys) |
| 230 | for _, key := range keys { |
| 231 | for _, value := range hdr[key] { |
| 232 | writeAttributescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/signjar`/writeAttribute().(out, key, value) |
| 233 | } |
| 234 | } |
| 235 | out.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().([]byte("\r\n")) |
| 236 | } |