| 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 apk |
| 18 | |
| 19 | import ( |
| 20 | "encoding/binary" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "reflect" |
| 25 | ) |
| 26 | |
| 27 | // implement the uint32-prefixed structure of a APK Signature Scheme v2 Block |
| 28 | // https://source.android.com/security/apksigning/v2#apk-signature-scheme-v2-block-format |
| 29 | |
| 30 | var errTrailingData = errors.New("trailing data after structure") |
| 31 | |
| 32 | type apkRaw []byte |
| 33 | |
| 34 | // Bytes returns the inner content of the raw item, without the length prefix |
| 35 | func (r apkRaw) Bytes() []byte { |
| 36 | return []byte(r[4:]) |
| 37 | } |
| 38 | |
| 39 | var ( |
| 40 | bytesType = reflect.TypeOf([]byte(nil)) |
| 41 | rawType = reflect.TypeOf(apkRaw(nil)) |
| 42 | uint32Type = reflect.TypeOf(uint32(0)) |
| 43 | ) |
| 44 | |
| 45 | func unmarshal(blob []byte, dest interface{}) error { |
| 46 | v := reflect.ValueOf(dest) |
| 47 | if v.Kind() != reflect.Ptr || v.IsNil() { |
| 48 | return errors.New("target of unmarshal must be a non-nil pointer") |
| 49 | } |
| 50 | v = v.Elem() |
| 51 | blob, err := unmarshalR(blob, v) //, "x") |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } else if len(blob) != 0 { |
| 55 | return errTrailingData |
| 56 | } |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | func unmarshalR(blob []byte, v reflect.Value /*, path string*/) ([]byte, error) { |
| 61 | // scalar types (no prefix) |
| 62 | switch { |
| 63 | case v.Type() == uint32Type: |
| 64 | if len(blob) < 4 { |
| 65 | return nil, io.ErrUnexpectedEOF |
| 66 | } |
| 67 | i := binary.LittleEndian.Uint32(blob) |
| 68 | //fmt.Printf("%s = 0x%x\n", path, i) |
| 69 | v.SetUint(uint64(i)) |
| 70 | return blob[4:], nil |
| 71 | } |
| 72 | // read uint32 length prefix |
| 73 | if len(blob) < 4 { |
| 74 | return nil, io.ErrUnexpectedEOF |
| 75 | } |
| 76 | size := int(binary.LittleEndian.Uint32(blob)) |
| 77 | if 4+len(blob) < size { |
| 78 | return nil, io.ErrUnexpectedEOF |
| 79 | } |
| 80 | remainder := blob[4+size:] |
| 81 | raw := blob[:4+size] |
| 82 | blob = raw[4:] |
| 83 | switch { |
| 84 | case v.Type() == bytesType: |
| 85 | // []byte |
| 86 | //fmt.Printf("%s = []byte(\"%x\")\n", path, blob) |
| 87 | v.SetBytes(blob) |
| 88 | case v.Type() == rawType: |
| 89 | // apkRaw (same as above but keep the prefix) |
| 90 | //fmt.Printf("%s = raw(\"%x\")\n", path, raw) |
| 91 | v.SetBytes(raw) |
| 92 | // compound types |
| 93 | case v.Kind() == reflect.Slice: |
| 94 | // slice other than []byte |
| 95 | itemType := v.Type().Elem() |
| 96 | //fmt.Printf("%s = %s{}\n", path, v.Type()) |
| 97 | v.SetLen(0) |
| 98 | for len(blob) > 0 { |
| 99 | var err error |
| 100 | // append a zero value and unmarshal directly into the slice |
| 101 | n := v.Len() |
| 102 | v.Set(reflect.Append(v, reflect.Zero(itemType))) |
| 103 | blob, err = unmarshalR(blob, v.Index(n)) //, fmt.Sprintf("%s[%d]", path, n)) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | } |
| 108 | case v.Kind() == reflect.Struct: |
| 109 | // structure |
| 110 | //fmt.Printf("%s = %s{}\n", path, v.Type()) |
| 111 | for i := 0; i < v.NumField(); i++ { |
| 112 | var err error |
| 113 | blob, err = unmarshalR(blob, v.Field(i)) //, fmt.Sprintf("%s.%s", path, v.Type().Field(i).Name)) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | } |
| 118 | if len(blob) > 0 { |
| 119 | return nil, errTrailingData |
| 120 | } |
| 121 | default: |
| 122 | panic("can't unmarshal type " + v.Type().String()) |
| 123 | } |
| 124 | return remainder, nil |
| 125 | } |
| 126 | |
| 127 | func marshal(src interface{}) (apkRaw, error) { |
| 128 | v := reflect.ValueOf(src) |
| 129 | m := new(marshaller) |
| 130 | if err := m.marshal(v); err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | return apkRaw(m.buf), nil |
| 134 | } |
| 135 | |
| 136 | type marshaller struct { |
| 137 | buf []byte |
| 138 | pos int |
| 139 | } |
| 140 | |
| 141 | func (m *marshaller) grow(n int) []byte { |
| 142 | if cap(m.buf)-m.pos < n { |
| 143 | buf := make([]byte, 2*cap(m.buf)+n) |
| 144 | copy(buf, m.buf) |
| 145 | m.buf = buf |
| 146 | } |
| 147 | m.buf = m.buf[:m.pos+n] |
| 148 | ret := m.buf[m.pos : m.pos+n] |
| 149 | m.pos += n |
| 150 | return ret |
| 151 | } |
| 152 | |
| 153 | func (m *marshaller) write(d []byte) { |
| 154 | copy(m.grow(len(d)), d) |
| 155 | } |
| 156 | |
| 157 | func (m *marshaller) marshal(v reflect.Value) error { |
| 158 | if v.Type() == rawType { |
| 159 | // raw |
| 160 | m.write(v.Bytes()) |
| 161 | return nil |
| 162 | } |
| 163 | // scalar types |
| 164 | switch { |
| 165 | case v.Type() == uint32Type: |
| 166 | binary.LittleEndian.PutUint32(m.grow(4), uint32(v.Uint())) |
| 167 | return nil |
| 168 | } |
| 169 | // prefixed types |
| 170 | start := m.pos |
| 171 | m.grow(4) |
| 172 | switch { |
| 173 | case v.Type() == bytesType: |
| 174 | // []byte |
| 175 | m.write(v.Bytes()) |
| 176 | case v.Kind() == reflect.Slice: |
| 177 | // slice other than []byte |
| 178 | for i := 0; i < v.Len(); i++ { |
| 179 | if err := m.marshal(v.Index(i)); err != nil { |
| 180 | return err |
| 181 | } |
| 182 | } |
| 183 | case v.Kind() == reflect.Struct: |
| 184 | // structure |
| 185 | for i := 0; i < v.NumField(); i++ { |
| 186 | if err := m.marshal(v.Field(i)); err != nil { |
| 187 | return err |
| 188 | } |
| 189 | } |
| 190 | default: |
| 191 | return fmt.Errorf("can't marshal type %s", v.Type()) |
| 192 | } |
| 193 | // put prefix |
| 194 | end := m.pos |
| 195 | binary.LittleEndian.PutUint32(m.buf[start:], uint32(end-start-4)) |
| 196 | return nil |
| 197 | } |