|
| 1 | +/** Provides classes and predicates to reason about unsafe certificate trust vulnerablities. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.frameworks.Networking |
| 5 | +private import semmle.code.java.security.Encryption |
| 6 | +private import semmle.code.java.dataflow.DataFlow |
| 7 | + |
| 8 | +/** |
| 9 | + * The creation of an object that prepares an SSL connection. |
| 10 | + * |
| 11 | + * This is a source for `SslEndpointIdentificationFlowConfig`. |
| 12 | + */ |
| 13 | +class SslConnectionInit extends DataFlow::Node { |
| 14 | + SslConnectionInit() { |
| 15 | + exists(MethodAccess ma, Method m | |
| 16 | + this.asExpr() = ma and |
| 17 | + ma.getMethod() = m |
| 18 | + | |
| 19 | + m instanceof CreateSslEngineMethod |
| 20 | + or |
| 21 | + m instanceof CreateSocketMethod and isSslSocket(ma) |
| 22 | + ) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * A call to a method that establishes an SSL connection. |
| 28 | + * |
| 29 | + * This is a sink for `SslEndpointIdentificationFlowConfig`. |
| 30 | + */ |
| 31 | +class SslConnectionCreation extends DataFlow::Node { |
| 32 | + SslConnectionCreation() { |
| 33 | + exists(MethodAccess ma, Method m | |
| 34 | + m instanceof BeginHandshakeMethod or |
| 35 | + m instanceof SslWrapMethod or |
| 36 | + m instanceof SslUnwrapMethod or |
| 37 | + m instanceof SocketGetOutputStreamMethod |
| 38 | + | |
| 39 | + ma.getMethod() = m and |
| 40 | + this.asExpr() = ma.getQualifier() |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * An SSL object that correctly verifies hostnames, or doesn't need to (for instance, because it's a server). |
| 47 | + * |
| 48 | + * This is a sanitizer for `SslEndpointIdentificationFlowConfig`. |
| 49 | + */ |
| 50 | +abstract class SslUnsafeCertTrustSanitizer extends DataFlow::Node { } |
| 51 | + |
| 52 | +/** |
| 53 | + * An `SSLEngine` set in server mode. |
| 54 | + */ |
| 55 | +private class SslEngineServerMode extends SslUnsafeCertTrustSanitizer { |
| 56 | + SslEngineServerMode() { |
| 57 | + exists(MethodAccess ma, Method m | |
| 58 | + m.hasName("setUseClientMode") and |
| 59 | + m.getDeclaringType().getASupertype*() instanceof SSLEngine and |
| 60 | + ma.getMethod() = m and |
| 61 | + ma.getArgument(0).(CompileTimeConstantExpr).getBooleanValue() = false and |
| 62 | + this.asExpr() = ma.getQualifier() |
| 63 | + ) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Holds if the return value of `createSocket` is cast to `SSLSocket` |
| 69 | + * or the qualifier of `createSocket` is an instance of `SSLSocketFactory`. |
| 70 | + */ |
| 71 | +private predicate isSslSocket(MethodAccess createSocket) { |
| 72 | + createSocket = any(CastExpr ce | ce.getType() instanceof SSLSocket).getExpr() |
| 73 | + or |
| 74 | + createSocket.getQualifier().getType().(RefType).getASupertype*() instanceof SSLSocketFactory |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * A call to a method that enables SSL (`useSslProtocol` or `setSslContextFactory`) |
| 79 | + * on an instance of `com.rabbitmq.client.ConnectionFactory` that doesn't set `enableHostnameVerification`. |
| 80 | + */ |
| 81 | +class RabbitMQEnableHostnameVerificationNotSet extends MethodAccess { |
| 82 | + RabbitMQEnableHostnameVerificationNotSet() { |
| 83 | + this.getMethod().hasName(["useSslProtocol", "setSslContextFactory"]) and |
| 84 | + this.getMethod().getDeclaringType() instanceof RabbitMQConnectionFactory and |
| 85 | + exists(Variable v | |
| 86 | + v.getType() instanceof RabbitMQConnectionFactory and |
| 87 | + this.getQualifier() = v.getAnAccess() and |
| 88 | + not exists(MethodAccess ma | |
| 89 | + ma.getMethod().hasName("enableHostnameVerification") and |
| 90 | + ma.getQualifier() = v.getAnAccess() |
| 91 | + ) |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +private class RabbitMQConnectionFactory extends RefType { |
| 97 | + RabbitMQConnectionFactory() { this.hasQualifiedName("com.rabbitmq.client", "ConnectionFactory") } |
| 98 | +} |
0 commit comments