One Tip to Avoid Bouncy Castle Error

java.lang.NoSuchMethodError: org.bouncycastle.math.ec.ECCurve$Fp – Issue

In this post, I will discuss one tip everyone can use to avoid bouncy castle error.

Problem

Recently I was working on building a SOAP webservice where we were using Apache CXF libraries along with Spring boot. We built the webservice, but when we were sending a SOAP request through the client, we kept getting following error:

Caused by: java.lang.NoSuchMethodError: org.bouncycastle.math.ec.ECCurve$Fp.<init>(Ljava/math/BigInteger;Ljava/math/BigInteger;Ljava/math/BigInteger;)V
  at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(Unknown Source)
  at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertPoint(Unknown Source)
  at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey.<init>(Unknown Source)
  at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
  at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
  at sun.security.ssl.HandshakeMessage$ECDH_ServerKeyExchange.<init>(HandshakeMessage.java:1075)
  at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:284)
  at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
  at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
  at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
  at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:553)
  at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:412)
  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179)
  at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:328)
  at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447)
  at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
  at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:121)

A Simple tip to avoid this error

We tried different things to resolve this issue. We tried to exclude bouncycastle jars from cxf-rt-ws-security dependencies we were using as we were pulling them from other dependencies. But this error would still pop up intermittently. Eventually, I figured out the issue. We had to add this dependency of bouncycastle explicitly as below in our Gradle build file. That’s when the error went away.

compile (‘org.bouncycastle:bcprov-jdk15on:1.56’) 

Conclusion

In this post, I showed how to use this one tip to avoid the error caused by the bouncy castle. If you enjoyed this post, subscribe to my blog here.