Skip to content

Commit

Permalink
fix detect
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Nazarov committed Oct 7, 2022
1 parent e61852f commit b5482da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import java.math.BigInteger
*
*
* @param tag Tag or Identifier
* @param length Length of the field
* @param value Encoded octet string for the field.
*/
open class Asn1Object(
private val tag: Int,
private val length: Int,
private val value: ByteArray
) {
val type: Int = tag and 0x1F
Expand Down Expand Up @@ -88,10 +86,18 @@ open class Asn1Object(
get() {
val encoding =
when (type) {
DerParser.NUMERIC_STRING, DerParser.PRINTABLE_STRING, DerParser.VIDEOTEX_STRING, DerParser.IA5_STRING, DerParser.GRAPHIC_STRING, DerParser.ISO646_STRING, DerParser.GENERAL_STRING -> "ISO-8859-1" // $NON-NLS-1$
DerParser.NUMERIC_STRING,
DerParser.PRINTABLE_STRING,
DerParser.VIDEOTEX_STRING,
DerParser.IA5_STRING,
DerParser.GRAPHIC_STRING,
DerParser.ISO646_STRING,
DerParser.GENERAL_STRING -> "ISO-8859-1" // $NON-NLS-1$
DerParser.BMP_STRING -> "UTF-16BE" // $NON-NLS-1$
DerParser.UTF8_STRING -> "UTF-8" // $NON-NLS-1$
DerParser.UNIVERSAL_STRING -> throw IOException("Invalid DER: can't handle UCS-4 string") // $NON-NLS-1$
DerParser.UNIVERSAL_STRING -> {
throw IOException("Invalid DER: can't handle UCS-4 string")
} // $NON-NLS-1$
else -> throw IOException("Invalid DER: object is not a string") // $NON-NLS-1$
}
return String(value, charset(encoding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ import java.math.BigInteger
*
* @author zhang
*/
@Suppress("MagicNumber")
open class DerParser
/**
* Create a new DER decoder from an input stream.
*
* @param in The DER encoded stream
*/(private var `in`: InputStream) {
* @param input The DER encoded stream
*/(private var input: InputStream) {
/**
* Create a new DER decoder from a byte array.
*
* @param The encoded bytes
* @throws IOException
*/
constructor(bytes: ByteArray?) : this(ByteArrayInputStream(bytes)) {}
constructor(bytes: ByteArray?) : this(ByteArrayInputStream(bytes))

/**
* Read next object. If it's constructed, the value holds
Expand All @@ -59,13 +60,13 @@ open class DerParser
*/
@Throws(IOException::class)
fun read(): Asn1Object {
val tag = `in`.read()
val tag = input.read()
if (tag == -1) throw IOException("Invalid DER: stream too short, missing tag") // $NON-NLS-1$
val length = length
val value = ByteArray(length)
val n = `in`.read(value)
val n = input.read(value)
if (n < length) throw IOException("Invalid DER: stream too short, missing value") // $NON-NLS-1$
return Asn1Object(tag, length, value)
return Asn1Object(tag, value)
} // $NON-NLS-1$

// $NON-NLS-1$
Expand Down Expand Up @@ -97,7 +98,7 @@ open class DerParser
@get:Throws(IOException::class)
private val length: Int
get() {
val i = `in`.read()
val i = input.read()
if (i == -1) throw IOException("Invalid DER: length missing") // $NON-NLS-1$

// A single byte short length
Expand All @@ -107,7 +108,7 @@ open class DerParser
// We can't handle length longer than 4 bytes
if (i >= 0xFF || num > 4) throw IOException("Invalid DER: length field too big ( $i )") // $NON-NLS-1$
val bytes = ByteArray(num)
val n = `in`.read(bytes)
val n = input.read(bytes)
if (n < num) throw IOException("Invalid DER: length too short") // $NON-NLS-1$
return BigInteger(1, bytes).toInt()
}
Expand Down

0 comments on commit b5482da

Please sign in to comment.