bc-java icon indicating copy to clipboard operation
bc-java copied to clipboard

ASN1 decode mistake

Open itlabers opened this issue 1 year ago • 0 comments

The same code snippet reads the first element of the ASN1Sequence structure in signedValue. der. Since the first element has a large number of bytes, the first 100 bytes are selected to obtain different results ,The result format is hex. When I open the file with HEX editor, the result of Version 1.62 is correct compared to the result of reading, so the version >= Version 1.63 may have a bug in reading some files with ASN1 decoding, The sample file contains the name signedValue.der in the attachment (SignedValue.zip)

[SignedValue.zip](https://github.com/bcgit/bc-java/files/9278029/SignedValue.zip)           

import java.io.ByteArrayInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.util.encoders.Hex;

public class Diff {
 
    public static void main(String[] args) throws Exception {
    String path = "replace it with a local .der file  path  ";  
    byte[] stream = Files.readAllBytes(Paths.get(path));
    ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(stream));
    ASN1Primitive pkcs = din.readObject();
    ASN1Sequence signedData = (ASN1Sequence) pkcs;
    //the first element
    ASN1Sequence tbs_sign = (ASN1Sequence) signedData.getObjectAt(0);
    byte[] tbs_sign_bytes = tbs_sign.getEncoded();
    // get 100 byte
    byte[] hex_bytes = Hex.encode(tbs_sign_bytes, 0, 100);
    // show result as hex
    System.out.println(new String(hex_bytes));
    din.close();
    }
  }

##################################################################

Bouncy Castle version 1.62 and 1.62 below

 

            <groupId>org.bouncycastle</groupId>

            <artifactId>bcprov-jdk15on</artifactId>

            1.62

  

Result :

308303158402010430830315273083031153301316024553020104160a537577656c6c5f53444b161049434243526563656970743030303031308203ed0201010c39e4b8ade59bbde5b7a5e59586e993b6e8a18ce882a1e4bbbde69c89e99990e585ace5

 Bouncy Castle version 1.63 and 1.63 above

 

            <groupId>org.bouncycastle</groupId>

            <artifactId>bcprov-jdk15on</artifactId>

            1.63

Result :

308303158c020104308303152d3083031159301316024553020104160a537577656c6c5f53444b161049434243526563656970743030303031308203f30201010c39e4b8ade59bbde5b7a5e59586e993b6e8a18ce882a1e4bbbde69c89e99990e585ace5

itlabers avatar Aug 08 '22 01:08 itlabers