| 1 |
/* |
| 2 |
* Copyright (C) 2017 The Android Open Source Project |
| 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 net.dongliu.apk.parser.cert.asn1.ber; |
| 18 |
|
| 19 |
import java.nio.ByteBuffer; |
| 20 |
|
| 21 |
/** |
| 22 |
* {@link BerDataValueReader} which reads from a {@link ByteBuffer} containing BER-encoded data |
| 23 |
* values. See {@code X.690} for the encoding. |
| 24 |
*/ |
| 25 |
public class ByteBufferBerDataValueReader implements BerDataValueReader { |
| 26 |
private final ByteBuffer mBuf; |
| 27 |
|
| 28 |
public ByteBufferBerDataValueReader(ByteBuffer buf) { |
| 29 |
if (buf == null) { |
| 30 |
throw new NullPointerException("buf == null"); |
| 31 |
} |
| 32 |
mBuf = buf; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public BerDataValue readDataValue() throws BerDataValueFormatException { |
| 37 |
int startPosition = mBuf.position(); |
| 38 |
if (!mBuf.hasRemaining()) { |
| 39 |
return null; |
| 40 |
} |
| 41 |
byte firstIdentifierByte = mBuf.get(); |
| 42 |
int tagNumber = readTagNumber(firstIdentifierByte); |
| 43 |
boolean constructed = BerEncoding.isConstructed(firstIdentifierByte); |
| 44 |
|
| 45 |
if (!mBuf.hasRemaining()) { |
| 46 |
throw new BerDataValueFormatException("Missing length"); |
| 47 |
} |
| 48 |
int firstLengthByte = mBuf.get() & 0xff; |
| 49 |
int contentsLength; |
| 50 |
int contentsOffsetInTag; |
| 51 |
if ((firstLengthByte & 0x80) == 0) { |
| 52 |
// short form length |
| 53 |
contentsLength = readShortFormLength(firstLengthByte); |
| 54 |
contentsOffsetInTag = mBuf.position() - startPosition; |
| 55 |
skipDefiniteLengthContents(contentsLength); |
| 56 |
} else if (firstLengthByte != 0x80) { |
| 57 |
// long form length |
| 58 |
contentsLength = readLongFormLength(firstLengthByte); |
| 59 |
contentsOffsetInTag = mBuf.position() - startPosition; |
| 60 |
skipDefiniteLengthContents(contentsLength); |
| 61 |
} else { |
| 62 |
// indefinite length -- value ends with 0x00 0x00 |
| 63 |
contentsOffsetInTag = mBuf.position() - startPosition; |
| 64 |
contentsLength = |
| 65 |
constructed |
| 66 |
? skipConstructedIndefiniteLengthContents() |
| 67 |
: skipPrimitiveIndefiniteLengthContents(); |
| 68 |
} |
| 69 |
|
| 70 |
// Create the encoded data value ByteBuffer |
| 71 |
int endPosition = mBuf.position(); |
| 72 |
mBuf.position(startPosition); |
| 73 |
int bufOriginalLimit = mBuf.limit(); |
| 74 |
mBuf.limit(endPosition); |
| 75 |
ByteBuffer encoded = mBuf.slice(); |
| 76 |
mBuf.position(mBuf.limit()); |
| 77 |
mBuf.limit(bufOriginalLimit); |
| 78 |
|
| 79 |
// Create the encoded contents ByteBuffer |
| 80 |
encoded.position(contentsOffsetInTag); |
| 81 |
encoded.limit(contentsOffsetInTag + contentsLength); |
| 82 |
ByteBuffer encodedContents = encoded.slice(); |
| 83 |
encoded.clear(); |
| 84 |
|
| 85 |
return new BerDataValue( |
| 86 |
encoded, |
| 87 |
encodedContents, |
| 88 |
BerEncoding.getTagClass(firstIdentifierByte), |
| 89 |
constructed, |
| 90 |
tagNumber); |
| 91 |
} |
| 92 |
|
| 93 |
private int readTagNumber(byte firstIdentifierByte) throws BerDataValueFormatException { |
| 94 |
int tagNumber = BerEncoding.getTagNumber(firstIdentifierByte); |
| 95 |
if (tagNumber == 0x1f) { |
| 96 |
// high-tag-number form, where the tag number follows this byte in base-128 |
| 97 |
// big-endian form, where each byte has the highest bit set, except for the last |
| 98 |
// byte |
| 99 |
return readHighTagNumber(); |
| 100 |
} else { |
| 101 |
// low-tag-number form |
| 102 |
return tagNumber; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private int readHighTagNumber() throws BerDataValueFormatException { |
| 107 |
// Base-128 big-endian form, where each byte has the highest bit set, except for the last |
| 108 |
// byte |
| 109 |
int b; |
| 110 |
int result = 0; |
| 111 |
do { |
| 112 |
if (!mBuf.hasRemaining()) { |
| 113 |
throw new BerDataValueFormatException("Truncated tag number"); |
| 114 |
} |
| 115 |
b = mBuf.get(); |
| 116 |
if (result > Integer.MAX_VALUE >>> 7) { |
| 117 |
throw new BerDataValueFormatException("Tag number too large"); |
| 118 |
} |
| 119 |
result <<= 7; |
| 120 |
result |= b & 0x7f; |
| 121 |
} while ((b & 0x80) != 0); |
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
private int readShortFormLength(int firstLengthByte) { |
| 126 |
return firstLengthByte & 0x7f; |
| 127 |
} |
| 128 |
|
| 129 |
private int readLongFormLength(int firstLengthByte) throws BerDataValueFormatException { |
| 130 |
// The low 7 bits of the first byte represent the number of bytes (following the first |
| 131 |
// byte) in which the length is in big-endian base-256 form |
| 132 |
int byteCount = firstLengthByte & 0x7f; |
| 133 |
if (byteCount > 4) { |
| 134 |
throw new BerDataValueFormatException("Length too large: " + byteCount + " bytes"); |
| 135 |
} |
| 136 |
int result = 0; |
| 137 |
for (int i = 0; i < byteCount; i++) { |
| 138 |
if (!mBuf.hasRemaining()) { |
| 139 |
throw new BerDataValueFormatException("Truncated length"); |
| 140 |
} |
| 141 |
int b = mBuf.get(); |
| 142 |
if (result > Integer.MAX_VALUE >>> 8) { |
| 143 |
throw new BerDataValueFormatException("Length too large"); |
| 144 |
} |
| 145 |
result <<= 8; |
| 146 |
result |= b & 0xff; |
| 147 |
} |
| 148 |
return result; |
| 149 |
} |
| 150 |
|
| 151 |
private void skipDefiniteLengthContents(int contentsLength) throws BerDataValueFormatException { |
| 152 |
if (mBuf.remaining() < contentsLength) { |
| 153 |
throw new BerDataValueFormatException( |
| 154 |
"Truncated contents. Need: " + contentsLength + " bytes, available: " |
| 155 |
+ mBuf.remaining()); |
| 156 |
} |
| 157 |
mBuf.position(mBuf.position() + contentsLength); |
| 158 |
} |
| 159 |
|
| 160 |
private int skipPrimitiveIndefiniteLengthContents() throws BerDataValueFormatException { |
| 161 |
// Contents are terminated by 0x00 0x00 |
| 162 |
boolean prevZeroByte = false; |
| 163 |
int bytesRead = 0; |
| 164 |
while (true) { |
| 165 |
if (!mBuf.hasRemaining()) { |
| 166 |
throw new BerDataValueFormatException( |
| 167 |
"Truncated indefinite-length contents: " + bytesRead + " bytes read"); |
| 168 |
|
| 169 |
} |
| 170 |
int b = mBuf.get(); |
| 171 |
bytesRead++; |
| 172 |
if (bytesRead < 0) { |
| 173 |
throw new BerDataValueFormatException("Indefinite-length contents too long"); |
| 174 |
} |
| 175 |
if (b == 0) { |
| 176 |
if (prevZeroByte) { |
| 177 |
// End of contents reached -- we've read the value and its terminator 0x00 0x00 |
| 178 |
return bytesRead - 2; |
| 179 |
} |
| 180 |
prevZeroByte = true; |
| 181 |
} else { |
| 182 |
prevZeroByte = false; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
private int skipConstructedIndefiniteLengthContents() throws BerDataValueFormatException { |
| 188 |
// Contents are terminated by 0x00 0x00. However, this data value is constructed, meaning it |
| 189 |
// can contain data values which are themselves indefinite length encoded. As a result, we |
| 190 |
// must parse the direct children of this data value to correctly skip over the contents of |
| 191 |
// this data value. |
| 192 |
int startPos = mBuf.position(); |
| 193 |
while (mBuf.hasRemaining()) { |
| 194 |
// Check whether the 0x00 0x00 terminator is at current position |
| 195 |
if ((mBuf.remaining() > 1) && (mBuf.getShort(mBuf.position()) == 0)) { |
| 196 |
int contentsLength = mBuf.position() - startPos; |
| 197 |
mBuf.position(mBuf.position() + 2); |
| 198 |
return contentsLength; |
| 199 |
} |
| 200 |
// No luck. This must be a BER-encoded data value -- skip over it by parsing it |
| 201 |
readDataValue(); |
| 202 |
} |
| 203 |
|
| 204 |
throw new BerDataValueFormatException( |
| 205 |
"Truncated indefinite-length contents: " |
| 206 |
+ (mBuf.position() - startPos) + " bytes read"); |
| 207 |
} |
| 208 |
} |