'Extract Modulus and Exponent from a RSA Public key in ASN.1 Format in Android

I have a problem where a hardware component returns the public key portion of a RSA key pair in ASN1 format.

Buffer Contents
[0x30 0x82 0x01 0x22 0x30 0x0D 0x06 0x09 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x01 0x01 0x05 0x00
0x03 0x82 0x01 0x0F 0x00 0x30 0x82 0x01 0x0A 0x02 0x82 0x01 0x01 0x00 0xAA 0x07 0xE5 0xCB 0x6F
0x80 0xBD 0x77 0x99 0xCC 0x10 0x0A 0xBE 0x23 0x17 0xF4 0x27 0x2E 0x46 0xFC 0x60 0xD3 0xF9 0x1E
0x5E 0x34 0x7F 0xD4 0x86 0x2D 0x33 0xA2 0xD4 0xFE 0x7E 0x00 0x1D 0x80 0x86 0x1B 0x97 0x00 0x1F
0x99 0x9F 0xAD 0x7A 0xA3 0x20 0x25 0xEE 0x41 0x7B 0x82 0x03 0xC5 0xCB 0x84 0x7C 0x02 0x29 0xB1
0x78 0x05 0x03 0xD2 0x12 0xE8 0xB7 0x97 0xB9 0xE9 0x11 0x55 0xCE 0x94 0xD5 0xE8 0x8A 0xC2 0x00
0x6A 0x7C 0xD5 0x83 0x52 0xAE 0xEB 0x8D 0xFB 0x4D 0x96 0xDC 0x08 0x52 0x2F 0x0D 0x37 0x1D 0xB2
0x91 0x1E 0x85 0xD9 0xDC 0xDE 0xE6 0x9B 0x7D 0xA2 0xB4 0x83 0x5F 0x44 0x14 0x48 0xBD 0x58 0xBE
0x42 0x87 0x0F 0x07 0xE9 0xBE 0x05 0x76 0x02 0x41 0x34 0x78 0x66 0xDD 0x03 0x91 0x3F 0xAF 0xCB
0x5A 0xF8 0x6C 0xDA 0xEE 0xE3 0x52 0x86 0x76 0x80 0x48 0x4A 0xC5 0x2D 0xC1 0xD6 0x3C 0xC5 0x09
0xC9 0x96 0xAF 0x27 0xE6 0xA6 0x9A 0xD1 0xFE 0x93 0x32 0x81 0xCF 0x47 0xD1 0xCD 0x23 0x2E 0x10
0x86 0x1F 0xFA 0xD4 0x54 0x63 0xD0 0xDF 0xAC 0x14 0xFB 0x34 0x04 0x8E 0x9E 0xB6 0x3D 0x87 0xBB
0x2C 0x48 0x0A 0xE1 0x3B 0x8A 0x62 0x28 0xF0 0x14 0xA0 0x22 0xFD 0x30 0xB7 0xF8 0x09 0xA0 0x9A
0x92 0xDA 0xA4 0x4F 0x5D 0x24 0xB9 0x59 0xCC 0x26 0x46 0x0D 0x78 0x49 0xDC 0xF1 0x05 0x32 0x06
0x04 0x49 0xE1 0xC2 0x4F 0x83 0x3A 0x4C 0x3C 0xAE 0xE5 0x00 0xF7 0xA8 0xE3 0x42 0x8A 0x47 0x62
0xF0 0x48 0x36 0x27 0x02 0x03 0x06 0x55 0x37]

I've tried to use bouncycastle to extract the relevant Modulus and Exponent from the ASN.1 buffer but it crashes with SigSegV when it tries to do the Modulus in the example below.

val sequence = ASN1Sequence.getInstance(modus)
try {
    val modulus = ASN1Integer.getInstance(sequence.getObjectAt(0))
    val exponent = ASN1Integer.getInstance(sequence.getObjectAt(1))
    val keySpec = RSAPublicKeySpec(
           modulus.positiveValue,
           exponent.positiveValue
    )
    val factory = KeyFactory.getInstance("RSA")
    val publicKey = factory.generatePublic(keySpec)
} catch (e: ASN1Exception) {
    LogUtils.d(TAG,"Exception ASN1 Integer: " + e.message)
}

Is there a better way of extracting the Modulus and Exponent from an ASN.1 ByteArray?

If I decode the ASN.1 byte array with an online tool I get the following layout

SEQUENCE
  SEQUENCE
    ObjectIdentifier rsaEncryption (1 2 840 113549 1 1 1)
    NULL
  BITSTRING, encapsulates
    SEQUENCE
      INTEGER 00AA07E5CB6F80BD7799CC100ABE2317..(total 257bytes)..4C3CAEE500F7A8E3428A4762F0483627
      INTEGER 065537


Solution 1:[1]

After a lot of struggling, I realized that my biggest issue is not understanding how ASN1 structures work.

So here is the solution for the above structure.

val asn1 = ASN1Sequence.getInstance(modus)
val dlSeq1: DLSequence = asn1.getObjectAt(0) as DLSequence
val dlSeq2: ASN1BitString = asn1.getObjectAt(1) as ASN1BitString
val asn2 = ASN1Sequence.getInstance(dlSeq2.bytes)
val modulus: ASN1Integer = asn2.getObjectAt(0) as ASN1Integer
val exponent: ASN1Integer = asn2.getObjectAt(1) as ASN1Integer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pieter Beukes