| 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 jar |
| 18 | |
| 19 | // Sign Java archives |
| 20 | |
| 21 | import ( |
| 22 | "archive/zip" |
| 23 | "io" |
| 24 | "os" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 27 | "github.com/sassoftware/relic/v7/lib/magic" |
| 28 | "github.com/sassoftware/relic/v7/lib/signjar" |
| 29 | "github.com/sassoftware/relic/v7/signers" |
| 30 | "github.com/sassoftware/relic/v7/signers/zipbased" |
| 31 | ) |
| 32 | |
| 33 | var JarSigner = &signers.Signer{ |
| 34 | Name: "jar", |
| 35 | Magic: magic.FileTypeJAR, |
| 36 | CertTypes: signers.CertTypeX509, |
| 37 | Transform: zipbased.Transform, |
| 38 | Sign: sign, |
| 39 | Verify: verify, |
| 40 | } |
| 41 | |
| 42 | func init() { |
| 43 | JarSigner.Flags().Bool("sections-only", false, "(JAR) Don't compute hash of entire manifest") |
| 44 | JarSigner.Flags().Bool("inline-signature", false, "(JAR) Include .SF inside the signature block") |
| 45 | JarSigner.Flags().Bool("apk-v2-present", false, "(JAR) Add X-Android-APK-Signed header to signature") |
| 46 | JarSigner.Flags().String("key-alias", "RELIC", "(JAR, APK) Alias to use for the signed manifest") |
| 47 | signers.Register(JarSigner) |
| 48 | } |
| 49 | |
| 50 | // sign a manifest and return the PKCS#7 blob |
| 51 | func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) { |
| 52 | argSectionsOnly := opts.Flags.GetBool("sections-only") |
| 53 | argInlineSignature := opts.Flags.GetBool("inline-signature") |
| 54 | argApkV2 := opts.Flags.GetBool("apk-v2-present") |
| 55 | argAlias := opts.Flags.GetString("key-alias") |
| 56 | if argAlias == "" { |
| 57 | argAlias = "RELIC" |
| 58 | } |
| 59 | digest, err := signjar.DigestJarStream(r, opts.Hash) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | patch, ts, err := digest.Sign(opts.Context(), cert, argAlias, argSectionsOnly, argInlineSignature, argApkV2) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | opts.Audit.SetCounterSignature(ts.CounterSignature) |
| 68 | return opts.SetBinPatch(patch) |
| 69 | } |
| 70 | |
| 71 | func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) { |
| 72 | inz, err := openZip(f) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | sigs, err := signjar.Verify(inz, opts.NoDigests) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | var ret []*signers.Signature |
| 81 | for _, ts := range sigs { |
| 82 | ret = append(ret, &signers.Signature{ |
| 83 | Hash: ts.Hash, |
| 84 | X509Signature: &ts.TimestampedSignature, |
| 85 | }) |
| 86 | } |
| 87 | return ret, nil |
| 88 | } |
| 89 | |
| 90 | func openZip(f *os.File) (*zip.Reader, error) { |
| 91 | size, err := f.Seek(0, io.SeekEnd) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | if _, err := f.Seek(0, 0); err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | return zip.NewReader(f, size) |
| 99 | } |