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

ArrayIndexOutOfBoundsException

Open tanqiwei opened this issue 6 years ago • 4 comments

I am currently writing a project service that uses the bc library to generate certificates. I am currently experiencing a problem. The problem is that the service has been running from an external service, requesting a certificate, and there has been no problem for the first half month. The following problems have occurred for several consecutive hours later and now:

java.lang.ArrayIndexOutOfBoundsException: 496 at org.bouncycastle.crypto.digests.GeneralDigest.update(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.crypto.signers.RSADigestSigner.update(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.operator.bc.BcSignerOutputStream.write(Unknown Source) ~[bcpkix-jdk15on-1.64.jar:1.64.00.0] at org.bouncycastle.asn1.ASN1OutputStream.write(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.asn1.DERSequence.encode(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.asn1.DEROutputStream.writePrimitive(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.asn1.ASN1OutputStream.writeObject(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.asn1.ASN1Object.encodeTo(Unknown Source) ~[bcprov-jdk15on-1.64.jar:1.64.0] at org.bouncycastle.cert.X509v3CertificateBuilder.generateSig(Unknown Source) ~[bcpkix-jdk15on-1.64.jar:1.64.00.0] at org.bouncycastle.cert.X509v3CertificateBuilder.build(Unknown Source) ~[bcpkix-jdk15on-1.64.jar:1.64.00.0]

I use this bc in java 1.8.0_181。But in other environments, the service runs without problems even for half a year. Is this problem caused by the situation? And the certificate request file does not seem to be problematic.

tanqiwei avatar Sep 21 '20 10:09 tanqiwei

Unless you come up with much more details, like request data, test case etc. don't expect anyone to help.

martijnbrinkers avatar Sep 21 '20 11:09 martijnbrinkers

I reproduced the problem and found that the problem was caused by the same signer when concurrent requests were used. But if it is performed in a single thread, there is no problem with the same signer.

X500NameBuilder x500NameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
        x500NameBuilder.addMultiValuedRDN(getAttributeTypeAndValuesByRDNs(pkcs10CertificationRequest.getSubject().getRDNs()));
        BigInteger serial = new BigInteger(Integer.toString(serialNo));
        ImmutablePair<Date, Date> fromAndToDay = createFromDayAndToDayByValidDay(config.getValidDay());
        X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(
                resource.getRootX509Name(),
                serial,
                fromAndToDay.left,
                fromAndToDay.right,
                pkcs10CertificationRequest.getSubject(),
                pkcs10CertificationRequest.getSubjectPublicKeyInfo()
        );
        BcX509ExtensionUtils extensionUtils = new BcX509ExtensionUtils();
        x509v3CertificateBuilder.addExtension(Extension.basicConstraints, Boolean.FALSE, new BasicConstraints(Boolean.FALSE));
        x509v3CertificateBuilder.addExtension(Extension.subjectKeyIdentifier, Boolean.FALSE,
                extensionUtils.createSubjectKeyIdentifier(pkcs10CertificationRequest.getSubjectPublicKeyInfo()));
        x509v3CertificateBuilder.addExtension(Extension.authorityKeyIdentifier, Boolean.FALSE,
                extensionUtils.createAuthorityKeyIdentifier(resource.getRootCertBcHolder().getSubjectPublicKeyInfo()));
       // add some extends
        ContentSigner signer = resource.getSigner();
        if (signer != null) {
            X509CertificateHolder newCertHolder = x509v3CertificateBuilder.build(signer);
            return builder.wrapSuccess().data(newCertHolder).build();
        }

This problem does not occur every time I request a brand new signer. So is this a bug? I still want to get a reply at the moment.

tanqiwei avatar Sep 22 '20 01:09 tanqiwei

You can't use a single signer instance from multiple threads concurrently, at least not for any of the implementations included in BC. So either use a separate signer instance per request, or per thread, or else synchronize the usage of the signer.

peterdettman avatar Sep 22 '20 02:09 peterdettman

Ok. I thought it was possible to share the signer. But it doesn't look like this now.

tanqiwei avatar Sep 22 '20 11:09 tanqiwei